EscapeTimeVisualWidget.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include "EscapeTimeVisualWidget.h"
  2. #include "Bitmap.h"
  3. #include <QOpenGLShaderProgram>
  4. #include <QOpenGLContext>
  5. #include <QOpenGLFunctions>
  6. #include <QOpenGLExtraFunctions>
  7. #include <QOpenGLFunctions_3_0>
  8. #include <QOpenGLFunctions_4_0_Core>
  9. #include <vector>
  10. ETVImage::ETVImage(EscapeTimeVisualWidget& owner,
  11. const Bitmap<float>& img) :
  12. ETVImage{ owner, owner.context(), img }
  13. {
  14. }
  15. ETVImage::ETVImage(EscapeTimeVisualWidget& owner,
  16. QOpenGLContext* context,
  17. const Bitmap<float>& img) :
  18. owner{ owner }
  19. {
  20. auto& gl = *context->functions();
  21. gl.glGenTextures(1, &textureId);
  22. gl.glActiveTexture(GL_TEXTURE0);
  23. gl.glBindTexture(GL_TEXTURE_2D, textureId);
  24. // workaround to weird bug appearing on OS X that the first time
  25. // a texture is displayed, it seems to display arbitrary graphics data
  26. // (e.g. parts of other windows or even old mandelbrot textures)
  27. //
  28. // bug doesn't appear if glTexImage2D is called twice with the image data
  29. #ifdef __APPLE__
  30. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, int(img.width), int(img.height), 0, GL_RED, GL_FLOAT, img.pixels.get());
  31. #endif // __APPLE__
  32. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, int(img.width), int(img.height), 0, GL_RED, GL_FLOAT, img.pixels.get());
  33. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  34. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  35. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  36. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  37. gl.glBindTexture(GL_TEXTURE_2D, 0);
  38. }
  39. ETVImage::~ETVImage(void)
  40. {
  41. auto& gl = *owner.context()->functions();
  42. gl.glDeleteTextures(1, &textureId);
  43. }
  44. void ETVImage::draw(float x, float y, float w, float h,
  45. float tx, float ty, float tw, float th)
  46. {
  47. auto& gl = *owner.context()->functions();
  48. auto& gle = *owner.context()->extraFunctions();
  49. GLfloat const fbVertices[] = {
  50. 0, 0, 0.0f,
  51. 0, 256, 0.0f,
  52. 256, 0, 0.0f,
  53. 256, 256, 0.0f,
  54. };
  55. GLfloat const vertices[] = {
  56. x, y, 0.0f,
  57. x, y + h, 0.0f,
  58. x + w, y, 0.0f,
  59. x + w, y + h, 0.0f,
  60. };
  61. GLfloat const texCoords[] = {
  62. tx, ty,
  63. tx, ty + th,
  64. tx + tw, ty,
  65. tx + tw, ty + th,
  66. };
  67. GLfloat const fullTexCoords[] = {
  68. 0, 0,
  69. 0, 1,
  70. 1, 0,
  71. 1, 1,
  72. };
  73. QColor color{ 255, 255, 255 };
  74. auto& program = owner.program;
  75. program->bind();
  76. int vertexLoc = program->attributeLocation("vertex");
  77. int texCoordsLoc = program->attributeLocation("texCoord");
  78. int colorLocation = program->uniformLocation("color");
  79. int texLoc = program->uniformLocation("tex");
  80. int gradLoc = program->uniformLocation("gradient");
  81. int gradientScaler = program->uniformLocation("gradientScaler");
  82. int maxIterations = program->uniformLocation("maxIterations");
  83. program->setAttributeArray(vertexLoc, vertices, 3);
  84. program->setAttributeArray(texCoordsLoc, texCoords, 2);
  85. program->enableAttributeArray(vertexLoc);
  86. program->enableAttributeArray(texCoordsLoc);
  87. program->setUniformValue(colorLocation, color);
  88. program->setUniformValue(gradientScaler, 1.0f / float(owner.gradientTextureMax));
  89. program->setUniformValue(maxIterations, float(owner.maxIterations));
  90. QMatrix4x4 pmvMatrix;
  91. pmvMatrix.ortho(QRect{ 0, 0, owner.getResolutionX(), owner.getResolutionY() });
  92. int matrixLocation = program->uniformLocation("matrix");
  93. program->setUniformValue(matrixLocation, pmvMatrix);
  94. gl.glEnable(GL_TEXTURE_2D);
  95. //GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
  96. //gle.glDrawBuffers(1, drawBuffers);
  97. gl.glBindFramebuffer(GL_FRAMEBUFFER, owner.tileFramebuffer);
  98. gl.glDisable(GL_DEPTH_TEST);
  99. if(gl.glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  100. printf("error intitializing framebuffer\n");
  101. }
  102. gl.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, owner.tileTexture, 0);
  103. //gl.glViewport(0, 0, 256, 256);
  104. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  105. gl.glUniform1i(texLoc, 0);
  106. gl.glUniform1i(gradLoc, 2);
  107. gl.glActiveTexture(GL_TEXTURE0);
  108. gl.glBindTexture(GL_TEXTURE_2D, textureId);
  109. gl.glActiveTexture(GL_TEXTURE2);
  110. gl.glBindTexture(GL_TEXTURE_2D, owner.gradientTextureId);
  111. gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  112. program->disableAttributeArray(vertexLoc);
  113. program->disableAttributeArray(texCoordsLoc);
  114. /*owner.renderTextures->bind();
  115. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  116. //gl.glViewport(0, 0, owner.getResolutionX(), owner.getResolutionY());
  117. int rtVertexLoc = owner.renderTextures->attributeLocation("vertex");
  118. int rtTexCoordsLoc = owner.renderTextures->attributeLocation("texCoord");
  119. int rtTexLoc = owner.renderTextures->attributeLocation("tex");
  120. gl.glActiveTexture(GL_TEXTURE0);
  121. gl.glUniform1i(rtTexLoc, 0);
  122. owner.renderTextures->setAttributeArray(rtVertexLoc, vertices, 3);
  123. owner.renderTextures->setAttributeArray(rtTexCoordsLoc, fullTexCoords, 2);
  124. owner.renderTextures->enableAttributeArray(rtVertexLoc);
  125. owner.renderTextures->enableAttributeArray(rtTexCoordsLoc);
  126. gl.glBindTexture(GL_TEXTURE_2D, owner.tileTexture);
  127. //if (rand() % 2)
  128. //gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  129. owner.renderTextures->disableAttributeArray(rtVertexLoc);
  130. owner.renderTextures->disableAttributeArray(rtTexCoordsLoc);
  131. */
  132. gl.glActiveTexture(GL_TEXTURE0);
  133. }
  134. EscapeTimeVisualWidget::EscapeTimeVisualWidget(QWidget* parent) :
  135. QOpenGLWidget{ parent },
  136. gradientTextureId{ 0 },
  137. gradientTextureMax{ 1.0f },
  138. gradientNeedsUpdate{ false }
  139. {
  140. }
  141. void EscapeTimeVisualWidget::setGradient(Gradient newGradient)
  142. {
  143. this->gradient = newGradient;
  144. gradientNeedsUpdate = true;
  145. update();
  146. }
  147. const Gradient& EscapeTimeVisualWidget::getGradient(void)
  148. {
  149. return gradient;
  150. }
  151. void EscapeTimeVisualWidget::initializeGL(void)
  152. {
  153. auto& gl = *this->context()->functions();
  154. gl.glClearColor(0, 0, 0, 0);
  155. gl.glDisable(GL_DEPTH_TEST);
  156. fprintf(stdout, "version: %s\n", gl.glGetString(GL_VERSION));
  157. fflush(stdout);
  158. // looks not even better
  159. //gl.glEnable(GL_FRAMEBUFFER_SRGB);
  160. //glShadeModel(GL_SMOOTH);
  161. renderTextures = new QOpenGLShaderProgram{ this->context() };
  162. renderTextures->addShaderFromSourceCode(QOpenGLShader::Vertex,
  163. "attribute highp vec4 vertex;\n"
  164. "attribute highp vec2 texCoord;\n"
  165. "uniform highp mat4 matrix;\n"
  166. "varying highp vec2 texc;\n"
  167. "void main(void)\n"
  168. "{\n"
  169. " gl_Position = matrix * vertex;\n"
  170. " texc = texCoord;\n"
  171. "}");
  172. renderTextures->addShaderFromSourceCode(QOpenGLShader::Fragment,
  173. // "#version 110\n"
  174. "uniform sampler2D tex;\n"
  175. "varying highp vec2 texc;\n"
  176. "void main(void)\n"
  177. "{\n"
  178. " gl_FragColor = texture2D(tex, texc);\n"
  179. "}");
  180. renderTextures->link();
  181. juliaPreviewer = new QOpenGLShaderProgram{ this->context() };
  182. juliaPreviewer->addShaderFromSourceCode(QOpenGLShader::Vertex,
  183. "attribute highp vec4 vertex;\n"
  184. "attribute highp vec2 texCoord;\n"
  185. "uniform highp mat4 matrix;\n"
  186. "varying highp vec2 texc;\n"
  187. "void main(void)\n"
  188. "{\n"
  189. " gl_Position = matrix * vertex;\n"
  190. " texc = texCoord;\n"
  191. "}");
  192. juliaPreviewer->addShaderFromSourceCode(QOpenGLShader::Fragment,
  193. // "#version 110\n"
  194. "uniform sampler2D gradient;\n"
  195. "uniform highp float gradientScaler;\n"
  196. "const highp float maxIterations = 350.0;\n"
  197. "varying highp vec2 texc;\n"
  198. "uniform highp float juliaX;\n"
  199. "uniform highp float juliaY;\n"
  200. "uniform int smoothB;\n"
  201. "const highp float left = -1.5;\n"
  202. "const highp float right = 1.5;\n"
  203. "const highp float top = -1.5;\n"
  204. "const highp float bottom = 1.5;\n"
  205. "highp float map(highp float a, highp float b, highp float v) {\n"
  206. " return (1.0 - v) * a + b * v;\n"
  207. "}\n"
  208. "highp float iterate(highp float x, highp float y, highp float ca, highp float cb) {\n"
  209. " int k = 0;\n"
  210. " highp float a = x;\n"
  211. " highp float b = y;\n"
  212. " while(k <= int(maxIterations)) {\n"
  213. " highp float aa = a * a;\n"
  214. " highp float bb = b * b;\n"
  215. " highp float abab = 2.0 * a * b;\n"
  216. " a = aa - bb + ca;\n"
  217. " b = abab + cb;\n"
  218. " if (aa + bb >= 16.0) break;\n"
  219. " k = k + 1;\n"
  220. " }\n"
  221. " if (smoothB != 0) {\n"
  222. " return float(k) + 1.0 - log2(log(a * a + b * b) * 0.5);\n"
  223. " } else {\n"
  224. " return float(k);\n"
  225. " }\n"
  226. "}\n"
  227. "void main(void)\n"
  228. "{\n"
  229. " highp float x = map(left, right, texc.x);\n"
  230. " highp float y = map(top, bottom, texc.y);\n"
  231. " highp float v = iterate(x, y, juliaX, juliaY);\n"
  232. " if (v >= maxIterations) {\n"
  233. " gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
  234. " } else {\n"
  235. " highp float vnorm = v * gradientScaler;\n"
  236. " gl_FragColor = texture2D(gradient, vec2(vnorm, 0.0));\n"
  237. " }\n"
  238. "}");
  239. juliaPreviewer->link();
  240. program = new QOpenGLShaderProgram{ this->context() };
  241. bool vert = program->addShaderFromSourceCode(QOpenGLShader::Vertex,
  242. "attribute highp vec4 vertex;\n"
  243. "attribute highp vec2 texCoord;\n"
  244. "uniform highp mat4 matrix;\n"
  245. "varying highp vec2 texc;\n"
  246. "void main(void)\n"
  247. "{\n"
  248. " gl_Position = matrix * vertex;\n"
  249. " texc = texCoord;\n"
  250. "}");
  251. // TODO rewrite this monster
  252. if (!context()->isOpenGLES() &&
  253. context()->versionFunctions<QOpenGLFunctions_4_0_Core>() != nullptr) {
  254. bool frag = program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  255. "#version 400\n"
  256. "uniform sampler2D gradient;\n"
  257. "uniform sampler2D tex;\n"
  258. "uniform mediump vec4 color;\n"
  259. "uniform highp float gradientScaler;\n"
  260. "uniform highp float maxIterations;\n"
  261. "varying highp vec2 texc;\n"
  262. "vec4 colorize(float pos) {\n"
  263. " if (pos >= maxIterations) {\n"
  264. " return vec4(0.0, 0.0, 0.0, 1.0);\n"
  265. " } else {\n"
  266. " return texture2D(gradient, vec2(pos * gradientScaler, 0.0));\n"
  267. " }\n"
  268. "}\n"
  269. "void main(void)\n"
  270. "{\n"
  271. " vec2 size = textureSize(tex, 0);\n"
  272. " size = vec2(256.0, 256.0);\n"
  273. " vec2 accPoint = texc * size;\n"
  274. " vec2 ip = floor(accPoint);\n"
  275. " vec2 fp = fract(accPoint);\n"
  276. " vec4 inter = textureGather(tex, ip / size, 0);\n"
  277. " vec4 col1 = colorize(inter.x);\n"
  278. " vec4 col2 = colorize(inter.y);\n"
  279. " vec4 col3 = colorize(inter.z);\n"
  280. " vec4 col4 = colorize(inter.w);\n"
  281. " vec4 col = mix(mix(col4, col3, fp.x), mix(col1, col2, fp.x), fp.y);\n"
  282. " gl_FragColor = col;\n"
  283. // " gl_FragColor = gl_FragColor * texture2D(tex, texc);\n"
  284. // " float v = texture2D(tex, texc).r;\n"
  285. // " gl_FragColor = vec4(v, 1.0 - v, v*v, 1);\n"
  286. // " gl_FragColor.g = 0.3;\n"
  287. "}");
  288. }
  289. else {
  290. bool frag = program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  291. // "#version 110\n"
  292. "uniform sampler2D gradient;\n"
  293. "uniform sampler2D tex;\n"
  294. "uniform mediump vec4 color;\n"
  295. "uniform highp float gradientScaler;\n"
  296. "uniform highp float maxIterations;\n"
  297. "varying highp vec2 texc;\n"
  298. "void main(void)\n"
  299. "{\n"
  300. " highp float v = texture2D(tex, texc).r;\n"
  301. " if (v >= maxIterations) {\n"
  302. " gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
  303. " } else {\n"
  304. " gl_FragColor = texture2D(gradient, vec2(v * gradientScaler, 0.0));\n"
  305. " }\n"
  306. // " gl_FragColor = gl_FragColor * texture2D(tex, texc);\n"
  307. // " float v = texture2D(tex, texc).r;\n"
  308. // " gl_FragColor = vec4(v, 1.0 - v, v*v, 1);\n"
  309. // " gl_FragColor.g = 0.3;\n"
  310. "}");
  311. }
  312. //program.link();
  313. bool bound = program->bind();
  314. bound = renderTextures->bind();
  315. int vertexLoc = program->attributeLocation("vertex");
  316. int texCoordsLoc = program->attributeLocation("texCoord");
  317. int colorLocation = program->uniformLocation("color");
  318. int texLoc = program->uniformLocation("tex");
  319. int gradLoc = program->uniformLocation("gradient");
  320. int gradientScaler = program->uniformLocation("gradientScaler");
  321. int maxIterations = program->uniformLocation("maxIterations");
  322. program->setUniformValue(gradientScaler, 0.005f);
  323. program->setUniformValue(maxIterations, 250.0f);
  324. auto& gle = *this->context()->extraFunctions();
  325. gl.glGenTextures(1, &tileTexture);
  326. gl.glBindTexture(GL_TEXTURE_2D, tileTexture);
  327. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  328. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  329. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  330. gl.glBindTexture(GL_TEXTURE_2D, 0);
  331. gl.glGenFramebuffers(1, &tileFramebuffer);
  332. gl.glBindFramebuffer(GL_FRAMEBUFFER, tileFramebuffer);
  333. gl.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tileTexture, 0);
  334. GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
  335. gle.glDrawBuffers(1, drawBuffers);
  336. if(gl.glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  337. printf("error intitializing framebuffer\n");
  338. }
  339. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  340. unsigned char pix[] = { 255, 0, 0, 0, 255, 0, 0, 0, 255 };
  341. updateGradient();
  342. gl.glDisable(GL_DEPTH_TEST);
  343. //gl3.glBindSampler(0, id);
  344. }
  345. void EscapeTimeVisualWidget::resizeGL(int w, int h)
  346. {
  347. auto& gl = *this->context()->functions();
  348. float pixelRatio = this->devicePixelRatioF();
  349. //pixelRatio = 1.0 / 32;
  350. float newW = w * pixelRatio;
  351. float newH = h * pixelRatio;
  352. setResolutionX(newW);
  353. setResolutionY(newH);
  354. gl.glViewport(0, 0, newW, newH);
  355. QMatrix4x4 pmvMatrix;
  356. pmvMatrix.ortho(QRectF{ 0, 0, newW, newH });
  357. int matrixLocation = program->uniformLocation("matrix");
  358. int rtMatrixLocation = renderTextures->uniformLocation("matrix");
  359. int jMatrixLocation = juliaPreviewer->uniformLocation("matrix");
  360. program->setUniformValue(matrixLocation, pmvMatrix);
  361. renderTextures->setUniformValue(rtMatrixLocation, pmvMatrix);
  362. juliaPreviewer->setUniformValue(jMatrixLocation, pmvMatrix);
  363. }
  364. void EscapeTimeVisualWidget::paintGL(void)
  365. {
  366. if (gradientNeedsUpdate)
  367. updateGradient();
  368. /*ETVImage etvi{ *this };
  369. auto& gl = *this->context()->functions();
  370. gl.glClearColor(0.0, 0.2, 0.0, 1.0);
  371. gl.glClear(GL_COLOR_BUFFER_BIT);
  372. etvi.draw(100, 100, 700, 700);*/
  373. }
  374. void EscapeTimeVisualWidget::drawJulia(float jx, float jy, QRectF area, bool drawSmooth)
  375. {
  376. juliaPreviewer->bind();
  377. int gradLoc = juliaPreviewer->uniformLocation("gradient");
  378. int gradientScaler = juliaPreviewer->uniformLocation("gradientScaler");
  379. int juliaX = juliaPreviewer->uniformLocation("juliaX");
  380. int juliaY = juliaPreviewer->uniformLocation("juliaY");
  381. int vertexLoc = juliaPreviewer->attributeLocation("vertex");
  382. int texCoordsLoc = juliaPreviewer->attributeLocation("texCoord");
  383. int maxIterLoc = juliaPreviewer->attributeLocation("maxIterations");
  384. int smooth = juliaPreviewer->uniformLocation("smoothB");
  385. int matrixLocation = juliaPreviewer->uniformLocation("matrix");
  386. QMatrix4x4 pmvMatrix;
  387. pmvMatrix.ortho(QRect{ 0, 0, getResolutionX(), getResolutionY() });
  388. juliaPreviewer->setUniformValue(matrixLocation, pmvMatrix);
  389. const float x = area.x();
  390. const float y = area.y();
  391. const float w = area.width();
  392. const float h = area.height();
  393. GLfloat const vertices[] = {
  394. x, y, 0.0f,
  395. x, y + h, 0.0f,
  396. x + w, y, 0.0f,
  397. x + w, y + h, 0.0f,
  398. };
  399. GLfloat const texCoords[] = {
  400. 0, 0,
  401. 0, 1,
  402. 1, 0,
  403. 1, 1,
  404. };
  405. auto& gl = *this->context()->functions();
  406. gl.glEnable(GL_TEXTURE_2D);
  407. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  408. juliaPreviewer->setAttributeArray(vertexLoc, vertices, 3);
  409. juliaPreviewer->setAttributeArray(texCoordsLoc, texCoords, 2);
  410. juliaPreviewer->enableAttributeArray(vertexLoc);
  411. juliaPreviewer->enableAttributeArray(texCoordsLoc);
  412. juliaPreviewer->setUniformValue(gradientScaler, 1.0f / float(gradientTextureMax));
  413. juliaPreviewer->setUniformValue(maxIterLoc, float(250));
  414. juliaPreviewer->setUniformValue(juliaX, float(jx));
  415. juliaPreviewer->setUniformValue(juliaY, float(jy));
  416. juliaPreviewer->setUniformValue(smooth, int(drawSmooth ? 1 : 0));
  417. gl.glUniform1i(gradLoc, 0);
  418. gl.glActiveTexture(GL_TEXTURE0);
  419. gl.glBindTexture(GL_TEXTURE_2D, gradientTextureId);
  420. gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  421. juliaPreviewer->disableAttributeArray(vertexLoc);
  422. juliaPreviewer->disableAttributeArray(texCoordsLoc);
  423. //juliaPreviewer->release();
  424. //program->bind();
  425. }
  426. void EscapeTimeVisualWidget::setMaxIterationCutoff(float maxIter)
  427. {
  428. this->maxIterations = maxIter;
  429. }
  430. void EscapeTimeVisualWidget::updateGradient(void)
  431. {
  432. auto& gl = *this->context()->functions();
  433. int len = 512;
  434. if (gradient.getPoints().size() > 25) {
  435. len = 2048;
  436. }
  437. else if (gradient.getPoints().size() > 7) {
  438. len = 1024;
  439. }
  440. std::unique_ptr<uint8_t[]> pixels = std::make_unique<uint8_t[]>(len * 3);
  441. for (int i = 0; i < len; i++) {
  442. RGBColor c = gradient.get(gradient.getMax() * i / len);
  443. pixels[i * 3] = c.r;
  444. pixels[i * 3 + 1] = c.g;
  445. pixels[i * 3 + 2] = c.b;
  446. }
  447. gl.glEnable(GL_TEXTURE_2D);
  448. if (gradientTextureId == 0)
  449. gl.glGenTextures(1, &gradientTextureId);
  450. gl.glBindTexture(GL_TEXTURE_2D, gradientTextureId);
  451. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, len, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<unsigned char*> (pixels.get()));
  452. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  453. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  454. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  455. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  456. gl.glBindTexture(GL_TEXTURE_2D, 0);
  457. this->gradientTextureMax = gradient.getMax();
  458. gradientNeedsUpdate = false;
  459. }
  460. void EscapeTimeVisualWidget::setResolutionX(int w)
  461. {
  462. resolutionX = w;
  463. }
  464. void EscapeTimeVisualWidget::setResolutionY(int h)
  465. {
  466. resolutionY = h;
  467. }
  468. int EscapeTimeVisualWidget::getResolutionX(void) const
  469. {
  470. return resolutionX;
  471. }
  472. int EscapeTimeVisualWidget::getResolutionY(void) const
  473. {
  474. return resolutionY;
  475. }