MandelWidget.cpp 20 KB

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