MandelWidget.cpp 8.6 KB

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