MandelWidget.cpp 7.7 KB

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