MandelWidget.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "MandelWidget.h"
  2. #include <QOpenGLVertexArrayObject>
  3. Texture::Texture(const Bitmap<RGBColor>& bitmap)
  4. {
  5. glGenTextures(1, &id);
  6. glBindTexture(GL_TEXTURE_2D, id);
  7. long lineLength = (bitmap.width * 3 + 3) & ~3;
  8. unsigned char* pixels = new unsigned char[lineLength * bitmap.height];
  9. for (int i = 0; i < bitmap.width; i++) {
  10. for (int j = 0; j < bitmap.height; j++) {
  11. int index = i * 3 + j * lineLength;
  12. RGBColor c = bitmap.get(i, j);
  13. pixels[index] = c.r;
  14. pixels[index + 1] = c.g;
  15. pixels[index + 2] = c.b;
  16. }
  17. }
  18. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, int(bitmap.width), int(bitmap.height), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  19. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  20. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  21. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  22. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  23. }
  24. Texture::~Texture(void)
  25. {
  26. glDeleteTextures(1, &id);
  27. }
  28. void Texture::bind(void) const
  29. {
  30. glBindTexture(GL_TEXTURE_2D, id);
  31. }
  32. void Texture::drawRect(float x, float y, float width, float height)
  33. {
  34. glColor3ub(255, 255, 255);
  35. glEnable(GL_TEXTURE_2D);
  36. bind();
  37. glBegin(GL_TRIANGLE_STRIP);
  38. glTexCoord2f(0, 0);
  39. glVertex2f(x, y);
  40. glTexCoord2f(1, 0);
  41. glVertex2f(x + width, y);
  42. glTexCoord2f(0, 1);
  43. glVertex2f(x, y + height);
  44. glTexCoord2f(1, 1);
  45. glVertex2f(x + width, y + height);
  46. glEnd();
  47. glDisable(GL_TEXTURE_2D);
  48. }
  49. MandelWidget::MandelWidget(QWidget* parent) :
  50. QGLWidget{ QGLFormat(QGL::SampleBuffers), parent }
  51. {
  52. this->setContentsMargins(0, 0, 0, 0);
  53. this->setSizePolicy(QSizePolicy::Expanding,
  54. QSizePolicy::Expanding);
  55. }
  56. MandelWidget::~MandelWidget()
  57. {
  58. }
  59. void MandelWidget::initializeGL(void)
  60. {
  61. qglClearColor(Qt::black);
  62. glDisable(GL_DEPTH_TEST);
  63. //glShadeModel(GL_SMOOTH);
  64. /*CpuGenerator<double> cpg;
  65. MandelInfo mi;
  66. mi.bWidth = this->width();//ql.geometry().width();
  67. mi.bHeight = this->height(); //ql.geometry().height();
  68. mi.maxIter = 250;
  69. mi.view = viewport;
  70. auto bitmap = cpg.generate(mi);*/
  71. Bitmap<RGBColor> bitmap(1, 1);
  72. bitmap.get(0, 0) = RGBColor{5, 150, 50};
  73. tex = std::make_unique<Texture>(bitmap);
  74. }
  75. void MandelWidget::paintGL(void)
  76. {
  77. int width = this->width();
  78. int height = this->height();
  79. CpuGenerator<double> cpg;
  80. ClGenerator clg;
  81. MandelGenerator& mg = cpg;
  82. MandelInfo mi;
  83. mi.bWidth = width; //ql.geometry().width();
  84. mi.bHeight = height; //ql.geometry().height();
  85. mi.maxIter = 5000;
  86. mi.view = viewport;
  87. auto bitmap = mg.generate(mi);
  88. /*Bitmap<RGBColor> bitmap(1000, 1000);
  89. for (int i = 0; i < 1000 * 1000; i++)
  90. bitmap.pixels[i] = RGBColor{5, uint8_t((i % 1000) ^ (i / 1000)), 50};*/
  91. tex = std::make_unique<Texture>(bitmap);
  92. glViewport(0, 0, width, height);
  93. glMatrixMode(GL_PROJECTION);
  94. glLoadIdentity();
  95. #ifdef QT_OPENGL_ES_1
  96. glOrthof(0, width, height, 0, -1.0, 1.0);
  97. #else
  98. glOrtho(0, width, height, 0, -1.0, 1.0);
  99. #endif
  100. glMatrixMode(GL_MODELVIEW);
  101. glClear(GL_COLOR_BUFFER_BIT);
  102. glLoadIdentity();
  103. tex->drawRect(0, 0, width, height);
  104. drawRubberband();
  105. printf("omg\n");
  106. }
  107. void MandelWidget::drawRubberband(void)
  108. {
  109. glColor3ub(10, 200, 10);
  110. glBegin(GL_LINE_LOOP);
  111. glVertex2d(rubberband.x(), rubberband.y());
  112. glVertex2d(rubberband.right(), rubberband.y());
  113. glVertex2d(rubberband.right(), rubberband.bottom());
  114. glVertex2d(rubberband.x(), rubberband.bottom());
  115. glEnd();
  116. }
  117. void MandelWidget::resizeGL(int width, int height)
  118. {
  119. }
  120. void MandelWidget::redraw(void)
  121. {
  122. /*CpuGenerator<double> cpg;
  123. MandelInfo mi;
  124. mi.bWidth = this->geometry().width();//ql.geometry().width();
  125. mi.bHeight = this->geometry().height(); //ql.geometry().height();
  126. mi.maxIter = 250;
  127. mi.view = viewport;*/
  128. update();
  129. //auto bitmap = cpg.generate(mi).map<uint32_t>([](RGBColor rgb) { return 255 << 24 | rgb.b << 16 | rgb.g << 8 | rgb.r; });
  130. }
  131. void MandelWidget::resizeEvent(QResizeEvent* re)
  132. {
  133. double aspect = double(geometry().width()) / geometry().height();
  134. //if (viewport.width > viewport.height * aspect)
  135. viewport.height = (viewport.width / aspect);
  136. //else
  137. // viewport.width = (viewport.height * aspect);
  138. redraw();
  139. }
  140. void MandelWidget::mousePressEvent(QMouseEvent* me)
  141. {
  142. rubberband.setCoords(me->x(), me->y(), 0, 0);
  143. }
  144. void MandelWidget::mouseMoveEvent(QMouseEvent* me)
  145. {
  146. QRectF& rect = rubberband;
  147. float aspect = float(geometry().width()) / geometry().height();
  148. rect.setBottomRight(QPoint(me->x(), me->y()));
  149. if (rect.width() > rect.height() * aspect)
  150. rect.setHeight(rect.width() / aspect);
  151. else
  152. rect.setWidth(rect.height() * aspect);
  153. }
  154. void MandelWidget::mouseReleaseEvent(QMouseEvent* me)
  155. {
  156. QRect rect = rubberband.toRect();
  157. QRect full = this->geometry();
  158. viewport.x += double(rect.left()) * viewport.width / full.width();
  159. viewport.y += double(rect.top()) * viewport.height / full.height();
  160. viewport.width *= double(rect.width()) / full.width();
  161. viewport.height *= double(rect.height()) / full.height();
  162. redraw();
  163. }