MandelWidget.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. #include "MandelWidget.h"
  2. #include <cmath>
  3. using namespace mnd;
  4. #include <QOpenGLVertexArrayObject>
  5. Texture::Texture(const Bitmap<RGBColor>& bitmap, GLint param) :
  6. context{ nullptr }
  7. {
  8. glGenTextures(1, &id);
  9. 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. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, int(bitmap.width), int(bitmap.height), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  22. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  23. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  24. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, param);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, param);
  26. }
  27. Texture::Texture(const Bitmap<RGBColor>& bitmap, QOpenGLContext* context) :
  28. context{ context }
  29. {
  30. context->functions()->glGenTextures(1, &id);
  31. context->functions()->glBindTexture(GL_TEXTURE_2D, id);
  32. long lineLength = (bitmap.width * 3 + 3) & ~3;
  33. unsigned char* pixels = new unsigned char[lineLength * bitmap.height];
  34. for (int i = 0; i < bitmap.width; i++) {
  35. for (int j = 0; j < bitmap.height; j++) {
  36. int index = i * 3 + j * lineLength;
  37. RGBColor c = bitmap.get(i, j);
  38. pixels[index] = c.r;
  39. pixels[index + 1] = c.g;
  40. pixels[index + 2] = c.b;
  41. }
  42. }
  43. context->functions()->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, int(bitmap.width), int(bitmap.height), 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  44. context->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  45. context->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  46. context->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  47. context->functions()->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  48. }
  49. Texture::~Texture(void)
  50. {
  51. if (id != 0)
  52. glDeleteTextures(1, &id);
  53. }
  54. Texture::Texture(Texture&& other) :
  55. id{ other.id },
  56. context{ other.context }
  57. {
  58. other.id = 0;
  59. }
  60. Texture& Texture::operator=(Texture&& other)
  61. {
  62. this->id = other.id;
  63. this->context = other.context;
  64. other.id = 0;
  65. return *this;
  66. }
  67. void Texture::bind(void) const
  68. {
  69. glBindTexture(GL_TEXTURE_2D, id);
  70. }
  71. void Texture::drawRect(float x, float y, float width, float height)
  72. {
  73. glColor3ub(255, 255, 255);
  74. glEnable(GL_TEXTURE_2D);
  75. bind();
  76. glBegin(GL_TRIANGLE_STRIP);
  77. glTexCoord2f(0, 0);
  78. glVertex2f(x, y);
  79. glTexCoord2f(1, 0);
  80. glVertex2f(x + width, y);
  81. glTexCoord2f(0, 1);
  82. glVertex2f(x, y + height);
  83. glTexCoord2f(1, 1);
  84. glVertex2f(x + width, y + height);
  85. glEnd();
  86. glDisable(GL_TEXTURE_2D);
  87. }
  88. std::pair<int, int> TexGrid::getCellIndices(double x, double y)
  89. {
  90. return { ::floor(x / dpp / MandelV::chunkSize), ::floor(y / dpp / MandelV::chunkSize) };
  91. }
  92. std::pair<double, double> TexGrid::getPositions(int x, int y)
  93. {
  94. return { x * dpp * MandelV::chunkSize, y * dpp * MandelV::chunkSize };
  95. }
  96. Texture* TexGrid::getCell(int i, int j)
  97. {
  98. auto cIt = cells.find({i, j});
  99. if (cIt != cells.end()) {
  100. return cIt->second.get();
  101. }
  102. else {
  103. return nullptr;
  104. }
  105. }
  106. void TexGrid::setCell(int i, int j, std::unique_ptr<Texture> tex)
  107. {
  108. cells[{i, j}] = std::move(tex);
  109. }
  110. void TexGrid::clearCells(void)
  111. {
  112. cells.clear();
  113. }
  114. void Job::run(void)
  115. {
  116. auto [absX, absY] = grid->getPositions(i, j);
  117. double gw = grid->dpp * MandelV::chunkSize;
  118. Bitmap<float> f(MandelV::chunkSize, MandelV::chunkSize);
  119. mnd::MandelInfo mi;
  120. mi.view.x = absX;
  121. mi.view.y = absY;
  122. mi.view.width = mi.view.height = gw;
  123. mi.bWidth = mi.bHeight = MandelV::chunkSize;
  124. mi.maxIter = maxIter;
  125. mndContext.getDefaultGenerator().generate(mi, f.pixels.get());
  126. auto* rgb = new Bitmap<RGBColor>(f.map<RGBColor>([&mi, this](float i) {
  127. return i >= mi.maxIter ? RGBColor{ 0, 0, 0 } : gradient.get(i);
  128. }));
  129. emit done(level, i, j, rgb);
  130. }
  131. void Calcer::setMaxIter(int maxIter)
  132. {
  133. this->maxIter = maxIter;
  134. clearAll();
  135. }
  136. void Calcer::clearAll(void)
  137. {
  138. this->threadPool->clear();
  139. }
  140. void Calcer::calc(TexGrid& grid, int level, int i, int j)
  141. {
  142. if (jobs.find({ level, i, j }) == jobs.end()) {
  143. auto job = std::make_unique<Job>(mndContext, gradient, maxIter, &grid, level, i, j);
  144. connect(job.get(), &Job::done, this, &Calcer::redirect);
  145. jobs.insert({ std::tuple{ level, i, j }, std::move(job) });
  146. //jobs.push_back(std::move(job));
  147. threadPool->start(job.get());
  148. }
  149. }
  150. void Calcer::redirect(int level, int i, int j, Bitmap<RGBColor>* bmp)
  151. {
  152. jobs.erase({ level, i, j });
  153. emit done(level, i, j, bmp);
  154. }
  155. MandelV::MandelV(mnd::MandelContext& mndContext, Gradient& gradient, int maxIter) :
  156. mndContext{ mndContext },
  157. calcer{ mndContext, gradient, maxIter },
  158. gradient{ gradient },
  159. maxIter{ maxIter }
  160. {
  161. Bitmap<RGBColor> emp(8, 8);
  162. for(auto i = 0; i < emp.width; i++) {
  163. for(auto j = 0; j < emp.height; j++) {
  164. if((i + j) & 0x1) { // if i+j is odd
  165. emp.get(i, j) = RGBColor{ 255, 255, 255 };
  166. }
  167. else {
  168. emp.get(i, j) = RGBColor{ 120, 120, 120 };
  169. }
  170. }
  171. }
  172. empty = std::make_unique<Texture>(emp, GL_NEAREST);
  173. connect(&calcer, &Calcer::done, this, &MandelV::cellReady);
  174. }
  175. int MandelV::getLevel(double dpp) {
  176. return int(::log2(dpp / chunkSize));
  177. }
  178. double MandelV::getDpp(int level)
  179. {
  180. return ::pow(2, level) * chunkSize;
  181. }
  182. TexGrid& MandelV::getGrid(int level)
  183. {
  184. auto it = levels.find(level);
  185. if (it != levels.end()) {
  186. return it->second;
  187. }
  188. else {
  189. levels.insert({ level, TexGrid(getDpp(level)) });
  190. return levels[level];
  191. }
  192. }
  193. void MandelV::setMaxIter(int maxIter)
  194. {
  195. this->maxIter = maxIter;
  196. calcer.setMaxIter(maxIter);
  197. }
  198. void MandelV::clear(void)
  199. {
  200. for(auto[level, grid] : this->levels) {
  201. grid.clearCells();
  202. }
  203. }
  204. void MandelV::garbageCollect(int level)
  205. {
  206. for(auto& [l, grid] : levels) {
  207. int dist = ::abs(l - level);
  208. if (dist > 20) {
  209. grid.clearCells();
  210. }
  211. else if (dist > 10) {
  212. if (grid.countAllocatedCells() > 50)
  213. grid.clearCells();
  214. }
  215. else if (dist > 3) {
  216. if (grid.countAllocatedCells() > 150)
  217. grid.clearCells();
  218. }
  219. else if (dist > 0) {
  220. if (grid.countAllocatedCells() > 350)
  221. grid.clearCells();
  222. }
  223. else {
  224. if (grid.countAllocatedCells() > 2500)
  225. grid.clearCells();
  226. }
  227. }
  228. }
  229. void MandelV::paint(const mnd::MandelViewport& mvp)
  230. {
  231. double dpp = mvp.width / width;
  232. int level = getLevel(dpp) - 1;
  233. garbageCollect(level);
  234. auto& grid = getGrid(level);
  235. double gw = getDpp(level) * chunkSize;
  236. double w = width * gw / mvp.width;
  237. //double h = height * gw / mvp.height;
  238. printf("level: %d, dpp: %f, width: %f\n", level, dpp, w);
  239. auto [left, top] = grid.getCellIndices(mvp.x, mvp.y);
  240. auto [right, bottom] = grid.getCellIndices(mvp.right(), mvp.bottom());
  241. auto [realXLeft, realYTop] = grid.getPositions(left, top);
  242. realXLeft = (realXLeft - mvp.x) * width / mvp.width;
  243. realYTop = (realYTop - mvp.y) * height / mvp.height;
  244. for(int i = left; i <= right; i++) {
  245. for(int j = top; j <= bottom; j++) {
  246. double x = realXLeft + (i - left) * w;
  247. double y = realYTop + (j - top) * w;
  248. Texture* t = grid.getCell(i, j);
  249. if (t != nullptr) {
  250. t->drawRect(x, y, w, w);
  251. /*glBegin(GL_LINE_LOOP);
  252. glVertex2f(x, y);
  253. glVertex2f(x + w, y);
  254. glVertex2f(x + w, y + w);
  255. glVertex2f(x, y + w);
  256. glEnd();*/
  257. }
  258. else {
  259. calcer.calc(grid, level, i, j);
  260. this->empty->drawRect(x, y, w, w);
  261. }
  262. }
  263. }
  264. }
  265. void MandelV::cellReady(int level, int i, int j, Bitmap<RGBColor>* bmp)
  266. {
  267. this->getGrid(level).setCell(i, j, std::make_unique<Texture>(*bmp));
  268. delete bmp;
  269. emit redrawRequested();
  270. }
  271. MandelView::MandelView(mnd::Generator& generator, Gradient &gradient, MandelWidget* mWidget) :
  272. generator{ &generator },
  273. gradient{ gradient },
  274. mWidget{ mWidget }
  275. //context{ new QOpenGLContext(this) }
  276. {
  277. //context->setShareContext(mWidget->context()->contextHandle());
  278. hasToCalc.store(false);
  279. finish.store(false);
  280. }
  281. MandelView::~MandelView(void)
  282. {
  283. finish.store(true);
  284. condVar.notify_one();
  285. //calcThread.wait(100);
  286. calcThread.wait(100);
  287. calcThread.terminate();
  288. }
  289. void MandelView::setGenerator(mnd::Generator& value)
  290. {
  291. generator = &value;
  292. }
  293. void MandelView::start(void)
  294. {
  295. this->moveToThread(&calcThread);
  296. connect(&calcThread, SIGNAL(started()), this, SLOT(loop()));
  297. calcThread.start();
  298. }
  299. void MandelView::loop(void)
  300. {
  301. printf("thread!\n"); fflush(stdout);
  302. //QGLWidget* hiddenWidget = new QGLWidget(nullptr, mWidget);
  303. //hiddenWidget->setVisible(false);
  304. //hiddenWidget->context()->contextHandle()->moveToThread(&calcThread);
  305. //QOpenGLContext* context = hiddenWidget->context()->contextHandle();
  306. //context->setShareContext(mWidget->context()->contextHandle());
  307. //context->create();
  308. //printf("sharing: %d\n", QOpenGLContext::areSharing(hiddenWidget->context()->contextHandle(), mWidget->context()->contextHandle()));
  309. //fflush(stdout);
  310. //std::this_thread::sleep_for(std::chrono::milliseconds(3000));
  311. std::unique_lock<std::mutex> lock(mut);
  312. while(true) {
  313. printf("calcing!\n"); fflush(stdout);
  314. if (finish.load()) {
  315. break;
  316. }
  317. if (hasToCalc.exchange(false)) {
  318. const MandelInfo& mi = toCalc.load();
  319. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  320. generator->generate(mi, fmap.pixels.get());
  321. auto* bitmap = new Bitmap<RGBColor>(fmap.map<RGBColor>([&mi, this](float i) {
  322. return i >= mi.maxIter ? RGBColor{ 0, 0, 0 } : gradient.get(i);
  323. }));
  324. /*return i >= mi.maxIter ?
  325. RGBColor{ 0,0,0 } :
  326. RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127),
  327. uint8_t(sin(i * 0.01f) * 127 + 127),
  328. uint8_t(i) }; }));//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  329. */
  330. //hiddenWidget->makeCurrent();
  331. //Texture* tex = new Texture(bitmap);
  332. //hiddenWidget->doneCurrent();
  333. //Texture* tex = 0;
  334. emit updated(bitmap);
  335. }
  336. printf("finished calcing!\n"); fflush(stdout);
  337. condVar.wait(lock);
  338. printf("waking!\n"); fflush(stdout);
  339. }
  340. }
  341. void MandelView::adaptViewport(const MandelInfo mi)
  342. {
  343. //bmp->get(0, 0) = RGBColor{ 10, uint8_t(sin(1 / vp.width) * 127 + 127), 10 };
  344. /*printf("adapted\n");
  345. if (calc.valid()) {
  346. auto status = calc.wait_for(std::chrono::milliseconds(0));
  347. if (status == std::future_status::deferred) {
  348. printf("deferred\n");
  349. } else if (status == std::future_status::timeout) {
  350. printf("timeout\n");
  351. } else if (status == std::future_status::ready) {
  352. printf("ready!\n");
  353. }
  354. }*/
  355. /*if (!calc.valid() || calc.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) {
  356. toCalc = mi;
  357. hasToCalc = true;
  358. calc = std::async([this, mi] () {
  359. QGLWidget* hiddenWidget = new QGLWidget(nullptr, (QGLWidget*) mWidget);
  360. QOpenGLContext* context = hiddenWidget->context()->contextHandle();
  361. hiddenWidget->makeCurrent();
  362. //context->setShareContext(mWidget->context()->contextHandle());
  363. //context->create();
  364. printf("sharing: %d\n", QOpenGLContext::areSharing(context, mWidget->context()->contextHandle()));
  365. fflush(stdout);
  366. //std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  367. do {
  368. auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
  369. generator->generate(mi, fmap.pixels.get());
  370. auto bitmap = fmap.map<RGBColor>([&mi](float i) { return i > mi.maxIter ?
  371. RGBColor{ 0,0,0 } :
  372. RGBColor{ uint8_t(cos(i * 0.015f) * 127 + 127),
  373. uint8_t(sin(i * 0.01f) * 127 + 127),
  374. uint8_t(i) }; });//uint8_t(::sin(i * 0.01f) * 100 + 100), uint8_t(i) }; });
  375. Texture* tex = new Texture(bitmap, context);
  376. //Texture* tex = 0;
  377. emit updated(tex);
  378. } while(hasToCalc.exchange(false));
  379. });
  380. }
  381. else {*/
  382. //std::unique_lock<std::mutex> lock(mut, std::try_to_lock);
  383. toCalc = mi;
  384. hasToCalc.exchange(true);
  385. condVar.notify_one();
  386. //}
  387. }
  388. MandelWidget::MandelWidget(mnd::MandelContext& ctxt, QWidget* parent) :
  389. QOpenGLWidget{ parent },
  390. mndContext{ ctxt },
  391. mv{ ctxt.getDefaultGenerator(), gradient, this }
  392. {
  393. this->setContentsMargins(0, 0, 0, 0);
  394. this->setSizePolicy(QSizePolicy::Expanding,
  395. QSizePolicy::Expanding);
  396. QObject::connect(&mv, &MandelView::updated, this, &MandelWidget::viewUpdated, Qt::AutoConnection);
  397. QObject::connect(this, &MandelWidget::needsUpdate, &mv, &MandelView::adaptViewport, Qt::DirectConnection);
  398. /*if (!ctxt.getDevices().empty()) {
  399. if (auto* gen = ctxt.getDevices()[0].getGeneratorDouble(); gen) {
  400. mv.setGenerator(*gen);
  401. }
  402. }*/
  403. }
  404. MandelWidget::~MandelWidget()
  405. {
  406. }
  407. void MandelWidget::initializeGL(void)
  408. {
  409. this->context()->functions()->glClearColor(0, 0, 0, 0);
  410. this->context()->makeCurrent(nullptr);
  411. glDisable(GL_DEPTH_TEST);
  412. // looks not even better
  413. //glDisable(GL_FRAMEBUFFER_SRGB);
  414. //glShadeModel(GL_SMOOTH);
  415. /*CpuGenerator<double> cpg;
  416. MandelInfo mi;
  417. mi.bWidth = this->width();//ql.geometry().width();
  418. mi.bHeight = this->height(); //ql.geometry().height();
  419. mi.maxIter = 250;
  420. mi.view = viewport;
  421. auto bitmap = cpg.generate(mi);*/
  422. Bitmap<RGBColor> bitmap(1, 1);
  423. bitmap.get(0, 0) = RGBColor{50, 50, 50};
  424. v = nullptr;
  425. tex = std::make_unique<Texture>(bitmap, context());
  426. mv.start();
  427. requestRecalc();
  428. }
  429. void MandelWidget::paintGL(void)
  430. {
  431. if (v == nullptr) {
  432. v = std::make_unique<MandelV>(mndContext, gradient, maxIterations);
  433. QObject::connect(v.get(), &MandelV::redrawRequested, this, static_cast<void(QOpenGLWidget::*)(void)>(&QOpenGLWidget::update));
  434. }
  435. /*if (!initialized) {
  436. emit needsUpdate(viewport);
  437. initialized = true;
  438. }*/
  439. int width = this->width();
  440. int height = this->height();
  441. v->width = width;
  442. v->height = height;
  443. //v = std::make_unique<MandelV>(context());
  444. /*CpuGenerator<double> cpg;
  445. ClGenerator clg;
  446. MandelGenerator& mg = cpg;
  447. MandelInfo mi;
  448. mi.bWidth = width;
  449. mi.bHeight = height;
  450. mi.maxIter = 5000;
  451. mi.view = viewport;*/
  452. //auto bitmap = mg.generate(mi);
  453. /*Bitmap<RGBColor> bitmap(1000, 1000);
  454. for (int i = 0; i < 1000 * 1000; i++)
  455. bitmap.pixels[i] = RGBColor{5, uint8_t((i % 1000) ^ (i / 1000)), 50};
  456. tex = std::make_unique<Texture>(bitmap);*/
  457. glViewport(0, 0, width, height);
  458. glMatrixMode(GL_PROJECTION);
  459. glLoadIdentity();
  460. #ifdef QT_OPENGL_ES_1
  461. glOrthof(0, width, height, 0, -1.0, 1.0);
  462. #else
  463. glOrtho(0, width, height, 0, -1.0, 1.0);
  464. #endif
  465. glMatrixMode(GL_MODELVIEW);
  466. glClear(GL_COLOR_BUFFER_BIT);
  467. glLoadIdentity();
  468. //tex->drawRect(0, 0, width, height);
  469. //v->empty = std::move(*tex)
  470. //v->empty.bind();
  471. v->paint(this->viewport);
  472. //*tex = std::move(v->empty);
  473. if (rubberbandDragging)
  474. drawRubberband();
  475. }
  476. void MandelWidget::drawRubberband(void)
  477. {
  478. glColor3ub(10, 200, 10);
  479. glBegin(GL_LINE_LOOP);
  480. glVertex2d(rubberband.x(), rubberband.y());
  481. glVertex2d(rubberband.right(), rubberband.y());
  482. glVertex2d(rubberband.right(), rubberband.bottom());
  483. glVertex2d(rubberband.x(), rubberband.bottom());
  484. glEnd();
  485. }
  486. void MandelWidget::zoom(float scale)
  487. {
  488. viewport.zoomCenter(scale);
  489. requestRecalc();
  490. }
  491. void MandelWidget::setMaxIterations(int maxIter)
  492. {
  493. this->maxIterations = maxIter;
  494. if (v)
  495. v->setMaxIter(maxIter);
  496. requestRecalc();
  497. }
  498. void MandelWidget::requestRecalc()
  499. {
  500. //emit needsUpdate(MandelInfo{ viewport, this->width(), this->height(), maxIterations });
  501. this->update();
  502. }
  503. void MandelWidget::resizeGL(int width, int height)
  504. {
  505. //glViewport(0, 0, (GLint) width, (GLint) height);
  506. this->update();
  507. }
  508. /*void MandelWidget::redraw(void)
  509. {
  510. /*CpuGenerator<double> cpg;
  511. MandelInfo mi;
  512. mi.bWidth = this->geometry().width();//ql.geometry().width();
  513. mi.bHeight = this->geometry().height(); //ql.geometry().height();
  514. mi.maxIter = 250;
  515. mi.view = viewport;*/
  516. //update();
  517. //emit needsUpdate(viewport);
  518. //auto bitmap = cpg.generate(mi).map<uint32_t>([](RGBColor rgb) { return 255 << 24 | rgb.b << 16 | rgb.g << 8 | rgb.r; });
  519. //}
  520. void MandelWidget::resizeEvent(QResizeEvent* re)
  521. {
  522. QOpenGLWidget::resizeEvent(re);
  523. double aspect = double(geometry().width()) / geometry().height();
  524. //if (viewport.width > viewport.height * aspect)
  525. viewport.height = (viewport.width / aspect);
  526. //else
  527. // viewport.width = (viewport.height * aspect);
  528. if (v.get() != nullptr) {
  529. v->width = this->width();
  530. v->height = this->height();
  531. }
  532. printf("resized\n");
  533. requestRecalc();
  534. //redraw();
  535. }
  536. void MandelWidget::mousePressEvent(QMouseEvent* me)
  537. {
  538. rubberband.setCoords(me->x(), me->y(), 0, 0);
  539. rubberbandDragging = true;
  540. }
  541. void MandelWidget::mouseMoveEvent(QMouseEvent* me)
  542. {
  543. QRectF& rect = rubberband;
  544. float aspect = float(geometry().width()) / geometry().height();
  545. rect.setBottomRight(QPoint(me->x(), me->y()));
  546. if (rect.width() > rect.height() * aspect)
  547. rect.setHeight(rect.width() / aspect);
  548. else
  549. rect.setWidth(rect.height() * aspect);
  550. if (rubberbandDragging)
  551. emit repaint();
  552. }
  553. void MandelWidget::mouseReleaseEvent(QMouseEvent* me)
  554. {
  555. QRect rect = rubberband.toRect();
  556. QRect full = this->geometry();
  557. viewport.x += double(rect.left()) * viewport.width / full.width();
  558. viewport.y += double(rect.top()) * viewport.height / full.height();
  559. viewport.width *= double(rect.width()) / full.width();
  560. viewport.height *= double(rect.height()) / full.height();
  561. viewport.normalize();
  562. rubberbandDragging = false;
  563. requestRecalc();
  564. }
  565. void MandelWidget::viewUpdated(Bitmap<RGBColor>* bitmap)
  566. {
  567. if (bitmap != nullptr) {
  568. tex = std::make_unique<Texture>(*bitmap);
  569. delete bitmap;
  570. emit repaint();
  571. }
  572. }