MandelWidget.cpp 21 KB

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