MandelWidget.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "MandelWidget.h"
  2. using namespace mnd;
  3. #include <QOpenGLVertexArrayObject>
  4. Texture::Texture(const Bitmap<RGBColor>& bitmap)
  5. {
  6. glGenTextures(1, &id);
  7. glBindTexture(GL_TEXTURE_2D, id);
  8. long lineLength = (bitmap.width * 3 + 3) & ~3;
  9. unsigned char* pixels = new unsigned char[lineLength * bitmap.height];
  10. for (int i = 0; i < bitmap.width; i++) {
  11. for (int j = 0; j < bitmap.height; j++) {
  12. int index = i * 3 + j * lineLength;
  13. RGBColor c = bitmap.get(i, j);
  14. pixels[index] = c.r;
  15. pixels[index + 1] = c.g;
  16. pixels[index + 2] = c.b;
  17. }
  18. }
  19. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, int(bitmap.width), int(bitmap.height), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  20. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  21. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  22. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  23. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  24. }
  25. Texture::~Texture(void)
  26. {
  27. glDeleteTextures(1, &id);
  28. }
  29. void Texture::bind(void) const
  30. {
  31. glBindTexture(GL_TEXTURE_2D, id);
  32. }
  33. void Texture::drawRect(float x, float y, float width, float height)
  34. {
  35. glColor3ub(255, 255, 255);
  36. glEnable(GL_TEXTURE_2D);
  37. bind();
  38. glBegin(GL_TRIANGLE_STRIP);
  39. glTexCoord2f(0, 0);
  40. glVertex2f(x, y);
  41. glTexCoord2f(1, 0);
  42. glVertex2f(x + width, y);
  43. glTexCoord2f(0, 1);
  44. glVertex2f(x, y + height);
  45. glTexCoord2f(1, 1);
  46. glVertex2f(x + width, y + height);
  47. glEnd();
  48. glDisable(GL_TEXTURE_2D);
  49. }
  50. void MandelView::adaptViewport(const MandelViewport& vp)
  51. {
  52. //bmp->get(0, 0) = RGBColor{ 10, uint8_t(sin(1 / vp.width) * 127 + 127), 10 };
  53. /*printf("adapted\n");
  54. if (calc.valid()) {
  55. auto status = calc.wait_for(std::chrono::milliseconds(0));
  56. if (status == std::future_status::deferred) {
  57. printf("deferred\n");
  58. } else if (status == std::future_status::timeout) {
  59. printf("timeout\n");
  60. } else if (status == std::future_status::ready) {
  61. printf("ready!\n");
  62. }
  63. }*/
  64. if (!calc.valid() || calc.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) {
  65. toCalc = vp;
  66. hasToCalc = true;
  67. calc = std::async([this] () {
  68. do {
  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. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  76. generator.generate(mi, fmap.pixels.get());
  77. auto bitmap = fmap.map<RGBColor>([](float i) { return i < 0 ? RGBColor{ 0,0,0 } : RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127), uint8_t(sin(i * 0.01f) * 127 + 127), uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  78. emit updated(new Bitmap(std::move(bitmap)));
  79. } while(hasToCalc.exchange(false));
  80. });
  81. }
  82. else {
  83. toCalc = vp;
  84. hasToCalc = true;
  85. }
  86. }
  87. MandelWidget::MandelWidget(mnd::MandelContext& ctxt, QWidget* parent) :
  88. QGLWidget{ QGLFormat(QGL::SampleBuffers), parent },
  89. mndContext{ ctxt },
  90. mv{ *ctxt.getDevices()[0].getGeneratorDouble() }
  91. {
  92. this->setContentsMargins(0, 0, 0, 0);
  93. this->setSizePolicy(QSizePolicy::Expanding,
  94. QSizePolicy::Expanding);
  95. QObject::connect(&mv, &MandelView::updated, this, &MandelWidget::viewUpdated, Qt::AutoConnection);
  96. QObject::connect(this, &MandelWidget::needsUpdate, &mv, &MandelView::adaptViewport, Qt::AutoConnection);
  97. }
  98. MandelWidget::~MandelWidget()
  99. {
  100. }
  101. void MandelWidget::initializeGL(void)
  102. {
  103. qglClearColor(Qt::black);
  104. glDisable(GL_DEPTH_TEST);
  105. //glShadeModel(GL_SMOOTH);
  106. /*CpuGenerator<double> cpg;
  107. MandelInfo mi;
  108. mi.bWidth = this->width();//ql.geometry().width();
  109. mi.bHeight = this->height(); //ql.geometry().height();
  110. mi.maxIter = 250;
  111. mi.view = viewport;
  112. auto bitmap = cpg.generate(mi);*/
  113. Bitmap<RGBColor> bitmap(1, 1);
  114. bitmap.get(0, 0) = RGBColor{50, 50, 50};
  115. tex = std::make_unique<Texture>(bitmap);
  116. emit needsUpdate(viewport);
  117. }
  118. void MandelWidget::paintGL(void)
  119. {
  120. /*if (!initialized) {
  121. emit needsUpdate(viewport);
  122. initialized = true;
  123. }*/
  124. int width = this->width();
  125. int height = this->height();
  126. /*CpuGenerator<double> cpg;
  127. ClGenerator clg;
  128. MandelGenerator& mg = cpg;
  129. MandelInfo mi;
  130. mi.bWidth = width;
  131. mi.bHeight = height;
  132. mi.maxIter = 5000;
  133. mi.view = viewport;*/
  134. //auto bitmap = mg.generate(mi);
  135. /*Bitmap<RGBColor> bitmap(1000, 1000);
  136. for (int i = 0; i < 1000 * 1000; i++)
  137. bitmap.pixels[i] = RGBColor{5, uint8_t((i % 1000) ^ (i / 1000)), 50};
  138. tex = std::make_unique<Texture>(bitmap);*/
  139. glViewport(0, 0, width, height);
  140. glMatrixMode(GL_PROJECTION);
  141. glLoadIdentity();
  142. #ifdef QT_OPENGL_ES_1
  143. glOrthof(0, width, height, 0, -1.0, 1.0);
  144. #else
  145. glOrtho(0, width, height, 0, -1.0, 1.0);
  146. #endif
  147. glMatrixMode(GL_MODELVIEW);
  148. glClear(GL_COLOR_BUFFER_BIT);
  149. glLoadIdentity();
  150. tex->drawRect(0, 0, width, height);
  151. if (rubberbandDragging)
  152. drawRubberband();
  153. printf("painted GL\n");
  154. }
  155. void MandelWidget::drawRubberband(void)
  156. {
  157. glColor3ub(10, 200, 10);
  158. glBegin(GL_LINE_LOOP);
  159. glVertex2d(rubberband.x(), rubberband.y());
  160. glVertex2d(rubberband.right(), rubberband.y());
  161. glVertex2d(rubberband.right(), rubberband.bottom());
  162. glVertex2d(rubberband.x(), rubberband.bottom());
  163. glEnd();
  164. }
  165. void MandelWidget::resizeGL(int width, int height)
  166. {
  167. }
  168. /*void MandelWidget::redraw(void)
  169. {
  170. /*CpuGenerator<double> cpg;
  171. MandelInfo mi;
  172. mi.bWidth = this->geometry().width();//ql.geometry().width();
  173. mi.bHeight = this->geometry().height(); //ql.geometry().height();
  174. mi.maxIter = 250;
  175. mi.view = viewport;*/
  176. //update();
  177. //emit needsUpdate(viewport);
  178. //auto bitmap = cpg.generate(mi).map<uint32_t>([](RGBColor rgb) { return 255 << 24 | rgb.b << 16 | rgb.g << 8 | rgb.r; });
  179. //}
  180. void MandelWidget::resizeEvent(QResizeEvent* re)
  181. {
  182. double aspect = double(geometry().width()) / geometry().height();
  183. //if (viewport.width > viewport.height * aspect)
  184. viewport.height = (viewport.width / aspect);
  185. //else
  186. // viewport.width = (viewport.height * aspect);
  187. emit needsUpdate(viewport);
  188. //redraw();
  189. }
  190. void MandelWidget::mousePressEvent(QMouseEvent* me)
  191. {
  192. rubberband.setCoords(me->x(), me->y(), 0, 0);
  193. rubberbandDragging = true;
  194. }
  195. void MandelWidget::mouseMoveEvent(QMouseEvent* me)
  196. {
  197. QRectF& rect = rubberband;
  198. float aspect = float(geometry().width()) / geometry().height();
  199. rect.setBottomRight(QPoint(me->x(), me->y()));
  200. if (rect.width() > rect.height() * aspect)
  201. rect.setHeight(rect.width() / aspect);
  202. else
  203. rect.setWidth(rect.height() * aspect);
  204. if (rubberbandDragging)
  205. emit repaint();
  206. }
  207. void MandelWidget::mouseReleaseEvent(QMouseEvent* me)
  208. {
  209. QRect rect = rubberband.toRect();
  210. QRect full = this->geometry();
  211. viewport.x += double(rect.left()) * viewport.width / full.width();
  212. viewport.y += double(rect.top()) * viewport.height / full.height();
  213. viewport.width *= double(rect.width()) / full.width();
  214. viewport.height *= double(rect.height()) / full.height();
  215. viewport.normalize();
  216. rubberbandDragging = false;
  217. emit needsUpdate(viewport);
  218. }
  219. void MandelWidget::viewUpdated(const Bitmap<RGBColor>* bitmap)
  220. {
  221. tex = std::make_unique<Texture>(*bitmap);
  222. delete bitmap;
  223. printf("viewUpdated\n");
  224. emit repaint();
  225. }