MandelWidget.cpp 7.9 KB

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