MandelWidget.cpp 20 KB

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