EscapeTimeVisualWidget.cpp 19 KB

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