MandelWidget.cpp 8.6 KB

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