MandelWidget.cpp 7.3 KB

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