MandelWidget.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. void MandelView::adaptViewport(const MandelViewport& vp)
  50. {
  51. //bmp->get(0, 0) = RGBColor{ 10, uint8_t(sin(1 / vp.width) * 127 + 127), 10 };
  52. /*printf("adapted\n");
  53. if (calc.valid()) {
  54. auto status = calc.wait_for(std::chrono::milliseconds(0));
  55. if (status == std::future_status::deferred) {
  56. printf("deferred\n");
  57. } else if (status == std::future_status::timeout) {
  58. printf("timeout\n");
  59. } else if (status == std::future_status::ready) {
  60. printf("ready!\n");
  61. }
  62. }*/
  63. if (!calc.valid() || calc.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) {
  64. toCalc = vp;
  65. hasToCalc = true;
  66. calc = std::async([this] () {
  67. do {
  68. CpuGenerator<float> cpg;
  69. MandelInfo mi;
  70. mi.bWidth = 1024;//ql.geometry().width();
  71. mi.bHeight = 1024; //ql.geometry().height();
  72. mi.maxIter = 4000;
  73. mi.view = toCalc;
  74. Bitmap<RGBColor>* bmp = //new Bitmap<RGBColor>(1, 1);
  75. new Bitmap<RGBColor>(cpg.generate(mi));
  76. emit updated(bmp);
  77. } while(hasToCalc.exchange(false));
  78. });
  79. }
  80. else {
  81. toCalc = vp;
  82. hasToCalc = true;
  83. }
  84. }
  85. MandelWidget::MandelWidget(QWidget* parent) :
  86. QGLWidget{ QGLFormat(QGL::SampleBuffers), parent }
  87. {
  88. this->setContentsMargins(0, 0, 0, 0);
  89. this->setSizePolicy(QSizePolicy::Expanding,
  90. QSizePolicy::Expanding);
  91. QObject::connect(&mv, &MandelView::updated, this, &MandelWidget::viewUpdated, Qt::AutoConnection);
  92. QObject::connect(this, &MandelWidget::needsUpdate, &mv, &MandelView::adaptViewport, Qt::AutoConnection);
  93. }
  94. MandelWidget::~MandelWidget()
  95. {
  96. }
  97. void MandelWidget::initializeGL(void)
  98. {
  99. qglClearColor(Qt::black);
  100. glDisable(GL_DEPTH_TEST);
  101. //glShadeModel(GL_SMOOTH);
  102. /*CpuGenerator<double> cpg;
  103. MandelInfo mi;
  104. mi.bWidth = this->width();//ql.geometry().width();
  105. mi.bHeight = this->height(); //ql.geometry().height();
  106. mi.maxIter = 250;
  107. mi.view = viewport;
  108. auto bitmap = cpg.generate(mi);*/
  109. Bitmap<RGBColor> bitmap(1, 1);
  110. bitmap.get(0, 0) = RGBColor{50, 50, 50};
  111. tex = std::make_unique<Texture>(bitmap);
  112. emit needsUpdate(viewport);
  113. }
  114. void MandelWidget::paintGL(void)
  115. {
  116. /*if (!initialized) {
  117. emit needsUpdate(viewport);
  118. initialized = true;
  119. }*/
  120. int width = this->width();
  121. int height = this->height();
  122. /*CpuGenerator<double> cpg;
  123. ClGenerator clg;
  124. MandelGenerator& mg = cpg;
  125. MandelInfo mi;
  126. mi.bWidth = width;
  127. mi.bHeight = height;
  128. mi.maxIter = 5000;
  129. mi.view = viewport;*/
  130. //auto bitmap = mg.generate(mi);
  131. /*Bitmap<RGBColor> bitmap(1000, 1000);
  132. for (int i = 0; i < 1000 * 1000; i++)
  133. bitmap.pixels[i] = RGBColor{5, uint8_t((i % 1000) ^ (i / 1000)), 50};
  134. tex = std::make_unique<Texture>(bitmap);*/
  135. glViewport(0, 0, width, height);
  136. glMatrixMode(GL_PROJECTION);
  137. glLoadIdentity();
  138. #ifdef QT_OPENGL_ES_1
  139. glOrthof(0, width, height, 0, -1.0, 1.0);
  140. #else
  141. glOrtho(0, width, height, 0, -1.0, 1.0);
  142. #endif
  143. glMatrixMode(GL_MODELVIEW);
  144. glClear(GL_COLOR_BUFFER_BIT);
  145. glLoadIdentity();
  146. tex->drawRect(0, 0, width, height);
  147. if (rubberbandDragging)
  148. drawRubberband();
  149. printf("painted GL\n");
  150. }
  151. void MandelWidget::drawRubberband(void)
  152. {
  153. glColor3ub(10, 200, 10);
  154. glBegin(GL_LINE_LOOP);
  155. glVertex2d(rubberband.x(), rubberband.y());
  156. glVertex2d(rubberband.right(), rubberband.y());
  157. glVertex2d(rubberband.right(), rubberband.bottom());
  158. glVertex2d(rubberband.x(), rubberband.bottom());
  159. glEnd();
  160. }
  161. void MandelWidget::resizeGL(int width, int height)
  162. {
  163. }
  164. /*void MandelWidget::redraw(void)
  165. {
  166. /*CpuGenerator<double> cpg;
  167. MandelInfo mi;
  168. mi.bWidth = this->geometry().width();//ql.geometry().width();
  169. mi.bHeight = this->geometry().height(); //ql.geometry().height();
  170. mi.maxIter = 250;
  171. mi.view = viewport;*/
  172. //update();
  173. //emit needsUpdate(viewport);
  174. //auto bitmap = cpg.generate(mi).map<uint32_t>([](RGBColor rgb) { return 255 << 24 | rgb.b << 16 | rgb.g << 8 | rgb.r; });
  175. //}
  176. void MandelWidget::resizeEvent(QResizeEvent* re)
  177. {
  178. double aspect = double(geometry().width()) / geometry().height();
  179. //if (viewport.width > viewport.height * aspect)
  180. viewport.height = (viewport.width / aspect);
  181. //else
  182. // viewport.width = (viewport.height * aspect);
  183. emit needsUpdate(viewport);
  184. //redraw();
  185. }
  186. void MandelWidget::mousePressEvent(QMouseEvent* me)
  187. {
  188. rubberband.setCoords(me->x(), me->y(), 0, 0);
  189. rubberbandDragging = true;
  190. }
  191. void MandelWidget::mouseMoveEvent(QMouseEvent* me)
  192. {
  193. QRectF& rect = rubberband;
  194. float aspect = float(geometry().width()) / geometry().height();
  195. rect.setBottomRight(QPoint(me->x(), me->y()));
  196. if (rect.width() > rect.height() * aspect)
  197. rect.setHeight(rect.width() / aspect);
  198. else
  199. rect.setWidth(rect.height() * aspect);
  200. if (rubberbandDragging)
  201. emit repaint();
  202. }
  203. void MandelWidget::mouseReleaseEvent(QMouseEvent* me)
  204. {
  205. QRect rect = rubberband.toRect();
  206. QRect full = this->geometry();
  207. viewport.x += double(rect.left()) * viewport.width / full.width();
  208. viewport.y += double(rect.top()) * viewport.height / full.height();
  209. viewport.width *= double(rect.width()) / full.width();
  210. viewport.height *= double(rect.height()) / full.height();
  211. rubberbandDragging = false;
  212. emit needsUpdate(viewport);
  213. }
  214. void MandelWidget::viewUpdated(const Bitmap<RGBColor>* bitmap)
  215. {
  216. tex = std::make_unique<Texture>(*bitmap);
  217. delete bitmap;
  218. printf("viewUpdated\n");
  219. emit repaint();
  220. }