MandelWidget.cpp 7.8 KB

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