MandelWidget.cpp 21 KB

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