MandelWidget.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #include "MandelWidget.h"
  2. #include <cmath>
  3. using namespace mnd;
  4. #include <QOpenGLVertexArrayObject>
  5. Texture::Texture(const Bitmap<RGBColor>& bitmap, GLint param)
  6. {
  7. glGenTextures(1, &id);
  8. glBindTexture(GL_TEXTURE_2D, id);
  9. int 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, param);
  24. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, param);
  25. }
  26. Texture::~Texture(void)
  27. {
  28. if (id != 0)
  29. glDeleteTextures(1, &id);
  30. }
  31. Texture::Texture(Texture&& other) :
  32. id{ other.id }
  33. {
  34. other.id = 0;
  35. }
  36. Texture& Texture::operator=(Texture&& other)
  37. {
  38. this->id = other.id;
  39. other.id = 0;
  40. return *this;
  41. }
  42. void Texture::bind(void) const
  43. {
  44. glBindTexture(GL_TEXTURE_2D, id);
  45. }
  46. void Texture::drawRect(float x, float y, float width, float height)
  47. {
  48. glColor3ub(255, 255, 255);
  49. glEnable(GL_TEXTURE_2D);
  50. bind();
  51. glBegin(GL_TRIANGLE_STRIP);
  52. glTexCoord2f(0, 0);
  53. glVertex2f(x, y);
  54. glTexCoord2f(1, 0);
  55. glVertex2f(x + width, y);
  56. glTexCoord2f(0, 1);
  57. glVertex2f(x, y + height);
  58. glTexCoord2f(1, 1);
  59. glVertex2f(x + width, y + height);
  60. glEnd();
  61. glDisable(GL_TEXTURE_2D);
  62. }
  63. CellImage::~CellImage(void)
  64. {
  65. }
  66. TextureClip::~TextureClip(void)
  67. {
  68. }
  69. void TextureClip::drawRect(float x, float y, float width, float height)
  70. {
  71. glColor3ub(255, 255, 255);
  72. glEnable(GL_TEXTURE_2D);
  73. glBindTexture(GL_TEXTURE_2D, texture->getId());
  74. glBegin(GL_TRIANGLE_STRIP);
  75. glTexCoord2f(tx, ty);
  76. glVertex2f(x, y);
  77. glTexCoord2f(tx + tw, ty);
  78. glVertex2f(x + width, y);
  79. glTexCoord2f(tx, ty + th);
  80. glVertex2f(x, y + height);
  81. glTexCoord2f(tx + tw, ty + th);
  82. glVertex2f(x + width, y + height);
  83. glEnd();
  84. glDisable(GL_TEXTURE_2D);
  85. }
  86. TextureClip TextureClip::clip(float x, float y, float w, float h)
  87. {
  88. float tx = this->tx + x * this->tw;
  89. float ty = this->ty + y * this->th;
  90. float tw = this->tw * w;
  91. float th = this->th * h;
  92. return TextureClip{ this->texture, tx, ty, tw, th };
  93. }
  94. std::shared_ptr<CellImage> TextureClip::clip(short i, short j)
  95. {
  96. return std::make_shared<TextureClip>(clip(i * 0.5f, j * 0.5f, 0.5f, 0.5f));
  97. }
  98. int TextureClip::getRecalcPriority() const
  99. {
  100. return int(1.0f / tw);
  101. }
  102. QuadImage::~QuadImage(void)
  103. {
  104. }
  105. void QuadImage::drawRect(float x, float y, float width, float height)
  106. {
  107. for (int i = 0; i < 2; i++) {
  108. for (int j = 0; j < 2; j++) {
  109. this->cells[i][j]->drawRect(x + i * 0.5f * width,
  110. y + j * 0.5f * height,
  111. width * 0.5f,
  112. height * 0.5f);
  113. }
  114. }
  115. }
  116. std::shared_ptr<CellImage> QuadImage::clip(short i, short j)
  117. {
  118. return cells[i][j];
  119. }
  120. int QuadImage::getRecalcPriority() const
  121. {
  122. return 1;
  123. }
  124. TexGrid::TexGrid(MandelView& owner, int level) :
  125. owner{ owner },
  126. level{ level },
  127. dpp{ owner.getDpp(level) }
  128. {
  129. }
  130. std::pair<GridIndex, GridIndex> TexGrid::getCellIndices(mnd::Real x, mnd::Real y)
  131. {
  132. return { ::floor(x / dpp / MandelView::chunkSize), ::floor(y / dpp / MandelView::chunkSize) };
  133. }
  134. std::pair<mnd::Real, mnd::Real> TexGrid::getPositions(GridIndex x, GridIndex y)
  135. {
  136. return { x * dpp * MandelView::chunkSize, y * dpp * MandelView::chunkSize };
  137. }
  138. GridElement* TexGrid::getCell(GridIndex i, GridIndex j)
  139. {
  140. auto cIt = cells.find({i, j});
  141. if (cIt != cells.end()) {
  142. return cIt->second.get();
  143. }
  144. else {
  145. return nullptr;
  146. }
  147. }
  148. void TexGrid::setCell(GridIndex i, GridIndex j, std::unique_ptr<GridElement> tex)
  149. {
  150. cells[{i, j}] = std::move(tex);
  151. }
  152. void TexGrid::clearCells(void)
  153. {
  154. cells.clear();
  155. }
  156. void Job::run(void)
  157. {
  158. auto [absX, absY] = grid->getPositions(i, j);
  159. mnd::Real gw = grid->dpp * MandelView::chunkSize;
  160. Bitmap<float> f(MandelView::chunkSize, MandelView::chunkSize);
  161. mnd::MandelInfo mi;
  162. mi.view.x = absX;
  163. mi.view.y = absY;
  164. mi.view.width = mi.view.height = gw;
  165. mi.bWidth = mi.bHeight = MandelView::chunkSize;
  166. mi.maxIter = maxIter;
  167. printf("w: %ld, h: %ld\n", mi.bWidth, mi.bHeight);
  168. fflush(stdout);
  169. generator->generate(mi, f.pixels.get());
  170. auto* rgb = new Bitmap<RGBColor>(f.map<RGBColor>([&mi, this](float i) {
  171. return i >= mi.maxIter ? RGBColor{ 0, 0, 0 } : gradient.get(i);
  172. }));
  173. emit done(level, i, j, calcState, rgb);
  174. }
  175. void Calcer::setMaxIter(int maxIter)
  176. {
  177. this->maxIter = maxIter;
  178. clearAll();
  179. }
  180. void Calcer::clearAll(void)
  181. {
  182. this->threadPool->clear();
  183. }
  184. void Calcer::calc(TexGrid& grid, int level, GridIndex i, GridIndex j, int priority)
  185. {
  186. jobsMutex.lock();
  187. if (jobs.find({ level, i, j }) == jobs.end()) {
  188. Job* job = new Job(generator, gradient, maxIter, &grid, level, i, j, calcState);
  189. connect(job, &Job::done, this, &Calcer::redirect);
  190. connect(job, &QObject::destroyed, this, [this, level, i, j] () { this->notFinished(level, i, j); });
  191. jobs.emplace(std::tuple{level, i, j}, job);
  192. threadPool->start(job, priority);
  193. }
  194. jobsMutex.unlock();
  195. }
  196. void Calcer::setCurrentLevel(int level)
  197. {
  198. if (this->currentLevel != level) {
  199. this->currentLevel = level;
  200. std::vector<QRunnable*> toCancel;
  201. jobsMutex.lock();
  202. for (auto&[tup, job] : jobs) {
  203. auto& [level, i, j] = tup;
  204. if(level != currentLevel) {
  205. toCancel.push_back(job);
  206. }
  207. }
  208. jobsMutex.unlock();
  209. for (auto* job : toCancel) {
  210. if (threadPool->tryTake(job)) {
  211. delete job;
  212. }
  213. }
  214. }
  215. }
  216. void Calcer::notFinished(int level, GridIndex i, GridIndex j)
  217. {
  218. jobsMutex.lock();
  219. jobs.erase({ level, i, j });
  220. jobsMutex.unlock();
  221. }
  222. void Calcer::redirect(int level, GridIndex i, GridIndex j, long calcState, Bitmap<RGBColor>* bmp)
  223. {
  224. jobsMutex.lock();
  225. jobs.erase({ level, i, j });
  226. jobsMutex.unlock();
  227. if (this->calcState == calcState) {
  228. emit done(level, i, j, bmp);
  229. }
  230. else {
  231. delete bmp;
  232. }
  233. }
  234. MandelView::MandelView(mnd::Generator* generator, MandelWidget& owner, int maxIter) :
  235. generator{ generator },
  236. calcer{ generator, owner.getGradient(), maxIter },
  237. owner{ owner },
  238. maxIter{ maxIter }
  239. {
  240. Bitmap<RGBColor> emp(8, 8);
  241. for(auto i = 0; i < emp.width; i++) {
  242. for(auto j = 0; j < emp.height; j++) {
  243. if((i + j) & 0x1) { // if i+j is odd
  244. emp.get(i, j) = RGBColor{ 255, 255, 255 };
  245. }
  246. else {
  247. emp.get(i, j) = RGBColor{ 120, 120, 120 };
  248. }
  249. }
  250. }
  251. empty = std::make_unique<Texture>(emp, GL_NEAREST);
  252. connect(&calcer, &Calcer::done, this, &MandelView::cellReady);
  253. }
  254. int MandelView::getLevel(mnd::Real dpp) {
  255. return int(::log2(dpp / chunkSize));
  256. }
  257. mnd::Real MandelView::getDpp(int level)
  258. {
  259. return ::pow(2, level) * chunkSize;
  260. }
  261. TexGrid& MandelView::getGrid(int level)
  262. {
  263. auto it = levels.find(level);
  264. if (it != levels.end()) {
  265. return it->second;
  266. }
  267. else {
  268. levels.insert(std::pair<int, TexGrid>{ level, TexGrid{ *this, level } });
  269. return levels.at(level);
  270. }
  271. }
  272. void MandelView::setMaxIter(int maxIter)
  273. {
  274. if (this->maxIter != maxIter) {
  275. this->maxIter = maxIter;
  276. calcer.setMaxIter(maxIter);
  277. clearCells();
  278. emit redrawRequested();
  279. }
  280. }
  281. void MandelView::setGenerator(mnd::Generator* generator)
  282. {
  283. if (this->generator != generator) {
  284. this->generator = generator;
  285. calcer.setGenerator(generator);
  286. clearCells();
  287. emit redrawRequested();
  288. }
  289. }
  290. void MandelView::clearCells(void)
  291. {
  292. for(auto& [level, grid] : this->levels) {
  293. grid.clearCells();
  294. }
  295. }
  296. void MandelView::garbageCollect(int level, GridIndex i, GridIndex j)
  297. {
  298. for(auto& [l, grid] : levels) {
  299. int dist = ::abs(l - level);
  300. if (dist > 20) {
  301. grid.clearCells();
  302. }
  303. else if (dist > 10) {
  304. if (grid.countAllocatedCells() > 50)
  305. grid.clearCells();
  306. }
  307. else if (dist > 3) {
  308. if (grid.countAllocatedCells() > 150)
  309. grid.clearCells();
  310. }
  311. else if (dist > 0) {
  312. if (grid.countAllocatedCells() > 350)
  313. grid.clearCells();
  314. }
  315. else {
  316. if (grid.countAllocatedCells() > 2500)
  317. grid.clearCells();
  318. }
  319. }
  320. }
  321. GridElement* MandelView::searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel)
  322. {
  323. auto& grid = getGrid(level);
  324. auto& gridAbove = getGrid(level + 1);
  325. GridIndex ai = (i < 0 ? (i - 1) : i) / 2;
  326. GridIndex aj = (j < 0 ? (j - 1) : j) / 2;
  327. GridElement* above = gridAbove.getCell(ai, aj);
  328. if (above == nullptr && recursionLevel > 0) {
  329. auto abFound = searchAbove(level + 1, ai, aj, recursionLevel - 1);
  330. if (abFound)
  331. above = abFound;
  332. }
  333. if (above != nullptr) {
  334. auto newElement = std::make_unique<GridElement>(
  335. false, above->img->clip((i & 1), (j & 1))
  336. );
  337. GridElement* ret = newElement.get();
  338. grid.setCell(i, j, std::move(newElement));
  339. return ret;
  340. }
  341. else {
  342. return nullptr;
  343. }
  344. }
  345. GridElement* MandelView::searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel)
  346. {
  347. if (recursionLevel == 0)
  348. return nullptr;
  349. auto& grid = getGrid(level);
  350. auto& gridUnder = getGrid(level - 1);
  351. GridIndex ai = i * 2;
  352. GridIndex aj = j * 2;
  353. GridElement* u00 = gridUnder.getCell(ai, aj);
  354. GridElement* u01 = gridUnder.getCell(ai, aj + 1);
  355. GridElement* u10 = gridUnder.getCell(ai + 1, aj);
  356. GridElement* u11 = gridUnder.getCell(ai + 1, aj + 1);
  357. /*if ( u00 == nullptr
  358. || u01 == nullptr
  359. || u10 == nullptr
  360. || u11 == nullptr) {
  361. auto abFound = searchUnder(level + 1, ai, aj, recursionLevel - 1);
  362. if (abFound)
  363. above = abFound;
  364. }*/
  365. if ( u00 != nullptr
  366. && u01 != nullptr
  367. && u10 != nullptr
  368. && u11 != nullptr) {
  369. GLuint FramebufferName = 0;
  370. auto newElement = std::make_unique<GridElement>(
  371. false, std::make_shared<QuadImage>(u00->img, u01->img, u10->img, u11->img)
  372. );
  373. GridElement* ret = newElement.get();
  374. grid.setCell(i, j, std::move(newElement));
  375. return ret;
  376. }
  377. else {
  378. return nullptr;
  379. }
  380. }
  381. void MandelView::paint(const mnd::MandelViewport& mvp)
  382. {
  383. mnd::Real dpp = mvp.width / width;
  384. int level = getLevel(dpp) - 1;
  385. auto& grid = getGrid(level);
  386. mnd::Real gw = getDpp(level) * chunkSize;
  387. auto [left, top] = grid.getCellIndices(mvp.x, mvp.y);
  388. auto [right, bottom] = grid.getCellIndices(mvp.right(), mvp.bottom());
  389. garbageCollect(level, (left + right) / 2, (top + bottom) / 2);
  390. emit calcer.setCurrentLevel(level);
  391. mnd::Real w = width * gw / mvp.width;
  392. auto [realXLeft, realYTop] = grid.getPositions(left, top);
  393. realXLeft = (realXLeft - mvp.x) * width / mvp.width;
  394. realYTop = (realYTop - mvp.y) * height / mvp.height;
  395. for(GridIndex i = left; i <= right; i++) {
  396. for(GridIndex j = top; j <= bottom; j++) {
  397. mnd::Real x = realXLeft + (i - left) * w;
  398. mnd::Real y = realYTop + (j - top) * w;
  399. GridElement* t = grid.getCell(i, j);
  400. if (t == nullptr) {
  401. auto under = searchUnder(level, i, j, 1);
  402. if (under) {
  403. t = under;
  404. }
  405. else {
  406. auto above = searchAbove(level, i, j, 2);
  407. if (above) {
  408. t = above;
  409. }
  410. }
  411. }
  412. if (t != nullptr) {
  413. t->img->drawRect(x, y, w, w);
  414. /*glBegin(GL_LINE_LOOP);
  415. glVertex2f(x, y);
  416. glVertex2f(x + w, y);
  417. glVertex2f(x + w, y + w);
  418. glVertex2f(x, y + w);
  419. glEnd();*/
  420. if (!t->enoughResolution) {
  421. calcer.calc(grid, level, i, j, t->img->getRecalcPriority());
  422. }
  423. }
  424. else {
  425. calcer.calc(grid, level, i, j, 1000);
  426. this->empty->drawRect(x, y, w, w);
  427. }
  428. }
  429. }
  430. }
  431. void MandelView::cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp)
  432. {
  433. this->getGrid(level).setCell(i, j,
  434. std::make_unique<GridElement>(true, std::make_shared<TextureClip>(std::make_shared<Texture>(*bmp))));
  435. delete bmp;
  436. emit redrawRequested();
  437. }
  438. MandelWidget::MandelWidget(mnd::MandelContext& ctxt, QWidget* parent) :
  439. QOpenGLWidget{ parent },
  440. mndContext{ ctxt },
  441. gradient{ Gradient::defaultGradient() }
  442. {
  443. this->setContentsMargins(0, 0, 0, 0);
  444. this->setSizePolicy(QSizePolicy::Expanding,
  445. QSizePolicy::Expanding);
  446. qRegisterMetaType<GridIndex>("GridIndex");
  447. this->format().setSwapInterval(1);
  448. /*gradient = Gradient {
  449. {
  450. { RGBColor{ 0, 0, 0 }, 0 },
  451. { RGBColor{ 180, 20, 10 }, 30 },
  452. { RGBColor{ 210, 180, 15 }, 70 },
  453. { RGBColor{ 160, 220, 45 }, 170 },
  454. { RGBColor{ 50, 150, 170 }, 300 },
  455. }
  456. };*/
  457. }
  458. MandelWidget::~MandelWidget()
  459. {
  460. }
  461. void MandelWidget::setGradient(Gradient g)
  462. {
  463. this->gradient = std::move(g);
  464. if (mandelView) {
  465. mandelView->clearCells();
  466. mandelView->calcer.changeState();
  467. }
  468. emit update();
  469. }
  470. void MandelWidget::setSmoothColoring(bool sc)
  471. {
  472. if (sc != this->smoothColoring) {
  473. this->smoothColoring = sc;
  474. if (mandelView) {
  475. mandelView->clearCells();
  476. mandelView->setGenerator(&mndContext.getDefaultGenerator(smoothColoring));
  477. }
  478. }
  479. }
  480. void MandelWidget::initializeGL(void)
  481. {
  482. this->context()->functions()->glClearColor(0, 0, 0, 0);
  483. this->context()->makeCurrent(nullptr);
  484. glDisable(GL_DEPTH_TEST);
  485. // looks not even better
  486. //glDisable(GL_FRAMEBUFFER_SRGB);
  487. //glShadeModel(GL_SMOOTH);
  488. mandelView = nullptr;
  489. requestRecalc();
  490. }
  491. void MandelWidget::paintGL(void)
  492. {
  493. if (mandelView == nullptr) {
  494. mandelView = std::make_unique<MandelView>(&mndContext.getDefaultGenerator(smoothColoring), *this, maxIterations);
  495. QObject::connect(mandelView.get(), &MandelView::redrawRequested, this, static_cast<void(QOpenGLWidget::*)(void)>(&QOpenGLWidget::update));
  496. }
  497. int width = this->width();
  498. int height = this->height();
  499. mandelView->width = width;
  500. mandelView->height = height;
  501. glViewport(0, 0, width, height);
  502. glMatrixMode(GL_PROJECTION);
  503. glLoadIdentity();
  504. #ifdef QT_OPENGL_ES_1
  505. glOrthof(0, width, height, 0, -1.0, 1.0);
  506. #else
  507. glOrtho(0, width, height, 0, -1.0, 1.0);
  508. #endif
  509. glMatrixMode(GL_MODELVIEW);
  510. glClear(GL_COLOR_BUFFER_BIT);
  511. glLoadIdentity();
  512. updateAnimations();
  513. mandelView->paint(this->currentViewport);
  514. if (rubberbanding)
  515. drawRubberband();
  516. }
  517. void MandelWidget::updateAnimations(void)
  518. {
  519. auto now = std::chrono::high_resolution_clock::now();
  520. auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now - lastAnimUpdate).count();
  521. double factor = ::pow(0.97, millis);
  522. currentViewport.x = currentViewport.x * factor + targetViewport.x * (1.0 - factor);
  523. currentViewport.y = currentViewport.y * factor + targetViewport.y * (1.0 - factor);
  524. currentViewport.width = currentViewport.width * factor + targetViewport.width * (1.0 - factor);
  525. currentViewport.height = currentViewport.height * factor + targetViewport.height * (1.0 - factor);
  526. lastAnimUpdate = now;
  527. if (::abs(currentViewport.width / targetViewport.width - 1.0) < 0.1e-5
  528. && ::abs(currentViewport.height / targetViewport.height - 1.0) < 0.1e-5) {
  529. // animation finished
  530. }
  531. else {
  532. emit update();
  533. }
  534. }
  535. void MandelWidget::drawRubberband(void)
  536. {
  537. glColor3ub(10, 200, 10);
  538. glBegin(GL_LINE_LOOP);
  539. glVertex2d(rubberband.x(), rubberband.y());
  540. glVertex2d(rubberband.right(), rubberband.y());
  541. glVertex2d(rubberband.right(), rubberband.bottom());
  542. glVertex2d(rubberband.x(), rubberband.bottom());
  543. glEnd();
  544. glEnable(GL_BLEND);
  545. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  546. glColor4f(0.1f, 0.9f, 0.1f, 0.2f);
  547. glBegin(GL_TRIANGLE_FAN);
  548. glVertex2d(rubberband.x(), rubberband.y());
  549. glVertex2d(rubberband.right(), rubberband.y());
  550. glVertex2d(rubberband.right(), rubberband.bottom());
  551. glVertex2d(rubberband.x(), rubberband.bottom());
  552. glEnd();
  553. glDisable(GL_BLEND);
  554. }
  555. void MandelWidget::zoom(float scale, float x, float y)
  556. {
  557. targetViewport.zoom(scale, x, y);
  558. lastAnimUpdate = std::chrono::high_resolution_clock::now();
  559. //currentViewport.zoom(scale, x, y);
  560. requestRecalc();
  561. }
  562. void MandelWidget::setViewport(const mnd::MandelViewport& viewport)
  563. {
  564. targetViewport = viewport;
  565. currentViewport = viewport;
  566. //lastAnimUpdate = std::chrono::high_resolution_clock::now();
  567. //currentViewport.zoom(scale, x, y);
  568. requestRecalc();
  569. }
  570. void MandelWidget::setMaxIterations(int maxIter)
  571. {
  572. this->maxIterations = maxIter;
  573. if (mandelView)
  574. mandelView->setMaxIter(maxIter);
  575. }
  576. void MandelWidget::requestRecalc()
  577. {
  578. emit update();
  579. }
  580. void MandelWidget::resizeGL(int width, int height)
  581. {
  582. emit update();
  583. }
  584. void MandelWidget::resizeEvent(QResizeEvent* re)
  585. {
  586. QOpenGLWidget::resizeEvent(re);
  587. double aspect = double(geometry().width()) / geometry().height();
  588. currentViewport.height = currentViewport.width / aspect;
  589. targetViewport = currentViewport;
  590. if (mandelView.get() != nullptr) {
  591. mandelView->width = this->width();
  592. mandelView->height = this->height();
  593. }
  594. requestRecalc();
  595. }
  596. void MandelWidget::mousePressEvent(QMouseEvent* me)
  597. {
  598. QOpenGLWidget::mousePressEvent(me);
  599. if (me->button() == Qt::RightButton) {
  600. rubberbanding = true;
  601. rubberband.setCoords(me->x(), me->y(), me->x(), me->y());
  602. emit repaint();
  603. me->accept();
  604. }
  605. else if (me->button() == Qt::LeftButton) {
  606. dragging = true;
  607. dragX = me->x();
  608. dragY = me->y();
  609. me->accept();
  610. }
  611. }
  612. void MandelWidget::mouseMoveEvent(QMouseEvent* me)
  613. {
  614. QOpenGLWidget::mouseMoveEvent(me);
  615. if (rubberbanding) {
  616. QRectF& rect = rubberband;
  617. double aspect = double(geometry().width()) / geometry().height();
  618. rect.setBottomRight(QPoint(me->x(), me->y()));
  619. if (rect.width() > rect.height() * aspect)
  620. rect.setHeight(rect.width() / aspect);
  621. else
  622. rect.setWidth(rect.height() * aspect);
  623. emit repaint();
  624. }
  625. else if (dragging) {
  626. double deltaX = me->x() - dragX;
  627. double deltaY = me->y() - dragY;
  628. this->currentViewport.x -= deltaX * currentViewport.width / this->width();
  629. this->currentViewport.y -= deltaY * currentViewport.height / this->height();
  630. targetViewport = currentViewport;
  631. dragX = me->x(); dragY = me->y();
  632. emit repaint();
  633. }
  634. me->accept();
  635. }
  636. void MandelWidget::mouseReleaseEvent(QMouseEvent* me)
  637. {
  638. QOpenGLWidget::mouseReleaseEvent(me);
  639. if (rubberbanding) {
  640. QRect rect = rubberband.toRect();
  641. if(rect.width() != 0 && rect.height() != 0) {
  642. QRect full = this->geometry();
  643. targetViewport.x += mnd::Real(rect.left()) * targetViewport.width / full.width();
  644. targetViewport.y += mnd::Real(rect.top()) * targetViewport.height / full.height();
  645. targetViewport.width *= mnd::Real(rect.width()) / full.width();
  646. targetViewport.height *= mnd::Real(rect.height()) / full.height();
  647. targetViewport.normalize();
  648. currentViewport = targetViewport;
  649. }
  650. requestRecalc();
  651. rubberbanding = false;
  652. }
  653. dragging = false;
  654. //requestRecalc();
  655. }
  656. void MandelWidget::wheelEvent(QWheelEvent* we)
  657. {
  658. QOpenGLWidget::wheelEvent(we);
  659. float x = float(we->x()) / this->width();
  660. float y = float(we->y()) / this->height();
  661. float scale = ::powf(0.9975f, we->angleDelta().y());
  662. zoom(scale, x, y);
  663. if (!we->pixelDelta().isNull())
  664. this->currentViewport = this->targetViewport;
  665. we->accept();
  666. }
  667. /*void MandelWidget::viewUpdated(Bitmap<RGBColor>* bitmap)
  668. {
  669. if (bitmap != nullptr) {
  670. delete bitmap;
  671. emit repaint();
  672. }
  673. }*/