MandelWidget.cpp 23 KB

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