EscapeTimeVisualWidget.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. "uniform int smooth;\n"
  195. "const highp float left = -1.5;\n"
  196. "const highp float right = 1.5;\n"
  197. "const highp float top = -1.5;\n"
  198. "const highp float bottom = 1.5;\n"
  199. "highp float map(highp float a, highp float b, highp float v) {\n"
  200. " return (1.0 - v) * a + b * v;\n"
  201. "}\n"
  202. "highp float iterate(highp float x, highp float y, highp float ca, highp float cb) {\n"
  203. " int k = 0;\n"
  204. " highp float a = x;\n"
  205. " highp float b = y;\n"
  206. " while(k <= int(maxIterations)) {\n"
  207. " highp float aa = a * a;\n"
  208. " highp float bb = b * b;\n"
  209. " highp float abab = 2.0 * a * b;\n"
  210. " a = aa - bb + ca;\n"
  211. " b = abab + cb;\n"
  212. " if (aa + bb >= 16.0) break;\n"
  213. " k = k + 1;\n"
  214. " }\n"
  215. // " if (smooth != 0) {\n"
  216. " return float(k) + 1.0 - log2(log(a * a + b * b) * 0.5);\n"
  217. // " } else {\n"
  218. // " return float(k);\n"
  219. // " }\n"
  220. "}\n"
  221. "void main(void)\n"
  222. "{\n"
  223. " highp float x = map(left, right, texc.x);\n"
  224. " highp float y = map(top, bottom, texc.y);\n"
  225. " highp float v = iterate(x, y, juliaX, juliaY);\n"
  226. " if (v >= maxIterations) {\n"
  227. " gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
  228. " } else {\n"
  229. " highp float vnorm = v * gradientScaler;\n"
  230. " gl_FragColor = texture2D(gradient, vec2(vnorm, 0.0));\n"
  231. " }\n"
  232. " gl_FragColor.g = 0.3;\n"
  233. "}");
  234. juliaPreviewer->link();
  235. program = new QOpenGLShaderProgram{ this->context() };
  236. bool vert = program->addShaderFromSourceCode(QOpenGLShader::Vertex,
  237. "attribute highp vec4 vertex;\n"
  238. "attribute highp vec2 texCoord;\n"
  239. "uniform highp mat4 matrix;\n"
  240. "varying highp vec2 texc;\n"
  241. "void main(void)\n"
  242. "{\n"
  243. " gl_Position = matrix * vertex;\n"
  244. " texc = texCoord;\n"
  245. "}");
  246. // TODO rewrite this monster
  247. if (!context()->isOpenGLES() &&
  248. context()->versionFunctions<QOpenGLFunctions_4_0_Core>() != nullptr) {
  249. bool frag = program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  250. "#version 400\n"
  251. "uniform sampler2D gradient;\n"
  252. "uniform sampler2D tex;\n"
  253. "uniform mediump vec4 color;\n"
  254. "uniform highp float gradientScaler;\n"
  255. "uniform highp float maxIterations;\n"
  256. "varying highp vec2 texc;\n"
  257. "vec4 colorize(float pos) {\n"
  258. " if (pos >= maxIterations) {\n"
  259. " return vec4(0.0, 0.0, 0.0, 1.0);\n"
  260. " } else {\n"
  261. " return texture2D(gradient, vec2(pos * gradientScaler, 0.0));\n"
  262. " }\n"
  263. "}\n"
  264. "void main(void)\n"
  265. "{\n"
  266. " vec2 size = textureSize(tex, 0);\n"
  267. " size = vec2(256.0, 256.0);\n"
  268. " vec2 accPoint = texc * size;\n"
  269. " vec2 ip = floor(accPoint);\n"
  270. " vec2 fp = fract(accPoint);\n"
  271. " vec4 inter = textureGather(tex, ip / size, 0);\n"
  272. " vec4 col1 = colorize(inter.x);\n"
  273. " vec4 col2 = colorize(inter.y);\n"
  274. " vec4 col3 = colorize(inter.z);\n"
  275. " vec4 col4 = colorize(inter.w);\n"
  276. " vec4 col = mix(mix(col4, col3, fp.x), mix(col1, col2, fp.x), fp.y);\n"
  277. " gl_FragColor = col;\n"
  278. // " gl_FragColor = gl_FragColor * texture2D(tex, texc);\n"
  279. // " float v = texture2D(tex, texc).r;\n"
  280. // " gl_FragColor = vec4(v, 1.0 - v, v*v, 1);\n"
  281. // " gl_FragColor.g = 0.3;\n"
  282. "}");
  283. }
  284. else {
  285. bool frag = program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  286. // "#version 110\n"
  287. "uniform sampler2D gradient;\n"
  288. "uniform sampler2D tex;\n"
  289. "uniform mediump vec4 color;\n"
  290. "uniform highp float gradientScaler;\n"
  291. "uniform highp float maxIterations;\n"
  292. "varying highp vec2 texc;\n"
  293. "void main(void)\n"
  294. "{\n"
  295. " highp float v = texture2D(tex, texc).r;\n"
  296. " if (v >= maxIterations) {\n"
  297. " gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
  298. " } else {\n"
  299. " gl_FragColor = texture2D(gradient, vec2(v * gradientScaler, 0.0));\n"
  300. " }\n"
  301. // " gl_FragColor = gl_FragColor * texture2D(tex, texc);\n"
  302. // " float v = texture2D(tex, texc).r;\n"
  303. // " gl_FragColor = vec4(v, 1.0 - v, v*v, 1);\n"
  304. // " gl_FragColor.g = 0.3;\n"
  305. "}");
  306. }
  307. //program.link();
  308. bool bound = program->bind();
  309. bound = renderTextures->bind();
  310. int vertexLoc = program->attributeLocation("vertex");
  311. int texCoordsLoc = program->attributeLocation("texCoord");
  312. int colorLocation = program->uniformLocation("color");
  313. int texLoc = program->uniformLocation("tex");
  314. int gradLoc = program->uniformLocation("gradient");
  315. int gradientScaler = program->uniformLocation("gradientScaler");
  316. int maxIterations = program->uniformLocation("maxIterations");
  317. program->setUniformValue(gradientScaler, 0.005f);
  318. program->setUniformValue(maxIterations, 250.0f);
  319. auto& gle = *this->context()->extraFunctions();
  320. gl.glGenTextures(1, &tileTexture);
  321. gl.glBindTexture(GL_TEXTURE_2D, tileTexture);
  322. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  323. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  324. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  325. gl.glBindTexture(GL_TEXTURE_2D, 0);
  326. gl.glGenFramebuffers(1, &tileFramebuffer);
  327. gl.glBindFramebuffer(GL_FRAMEBUFFER, tileFramebuffer);
  328. gl.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tileTexture, 0);
  329. GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
  330. gle.glDrawBuffers(1, drawBuffers);
  331. if(gl.glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  332. printf("error intitializing framebuffer\n");
  333. }
  334. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  335. unsigned char pix[] = { 255, 0, 0, 0, 255, 0, 0, 0, 255 };
  336. GLuint id;
  337. gl.glEnable(GL_TEXTURE_2D);
  338. gl.glGenTextures(1, &id);
  339. gl.glBindTexture(GL_TEXTURE_2D, id);
  340. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 3, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<char*> (pix));
  341. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  342. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  343. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  344. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  345. gl.glBindTexture(GL_TEXTURE_2D, 0);
  346. gradientTextureId = id;
  347. gl.glDisable(GL_DEPTH_TEST);
  348. //gl3.glBindSampler(0, id);
  349. }
  350. void EscapeTimeVisualWidget::resizeGL(int w, int h)
  351. {
  352. auto& gl = *this->context()->functions();
  353. float pixelRatio = this->devicePixelRatioF();
  354. //pixelRatio = 1.0 / 32;
  355. float newW = w * pixelRatio;
  356. float newH = h * pixelRatio;
  357. setResolutionX(newW);
  358. setResolutionY(newH);
  359. gl.glViewport(0, 0, newW, newH);
  360. QMatrix4x4 pmvMatrix;
  361. pmvMatrix.ortho(QRectF{ 0, 0, newW, newH });
  362. int matrixLocation = program->uniformLocation("matrix");
  363. int rtMatrixLocation = renderTextures->uniformLocation("matrix");
  364. int jMatrixLocation = juliaPreviewer->uniformLocation("matrix");
  365. program->setUniformValue(matrixLocation, pmvMatrix);
  366. renderTextures->setUniformValue(rtMatrixLocation, pmvMatrix);
  367. juliaPreviewer->setUniformValue(jMatrixLocation, pmvMatrix);
  368. }
  369. void EscapeTimeVisualWidget::paintGL(void)
  370. {
  371. if (gradientNeedsUpdate)
  372. updateGradient();
  373. /*ETVImage etvi{ *this };
  374. auto& gl = *this->context()->functions();
  375. gl.glClearColor(0.0, 0.2, 0.0, 1.0);
  376. gl.glClear(GL_COLOR_BUFFER_BIT);
  377. etvi.draw(100, 100, 700, 700);*/
  378. }
  379. void EscapeTimeVisualWidget::drawJulia(float jx, float jy, QRectF area, bool drawSmooth)
  380. {
  381. juliaPreviewer->bind();
  382. int gradLoc = juliaPreviewer->uniformLocation("gradient");
  383. int gradientScaler = juliaPreviewer->uniformLocation("gradientScaler");
  384. int juliaX = juliaPreviewer->uniformLocation("juliaX");
  385. int juliaY = juliaPreviewer->uniformLocation("juliaY");
  386. int vertexLoc = juliaPreviewer->attributeLocation("vertex");
  387. int texCoordsLoc = juliaPreviewer->attributeLocation("texCoord");
  388. int maxIterLoc = juliaPreviewer->attributeLocation("maxIterations");
  389. int smooth = juliaPreviewer->uniformLocation("smooth");
  390. int matrixLocation = juliaPreviewer->uniformLocation("matrix");
  391. QMatrix4x4 pmvMatrix;
  392. pmvMatrix.ortho(QRect{ 0, 0, getResolutionX(), getResolutionY() });
  393. juliaPreviewer->setUniformValue(matrixLocation, pmvMatrix);
  394. const float x = area.x();
  395. const float y = area.y();
  396. const float w = area.width();
  397. const float h = area.height();
  398. GLfloat const vertices[] = {
  399. x, y, 0.0f,
  400. x, y + h, 0.0f,
  401. x + w, y, 0.0f,
  402. x + w, y + h, 0.0f,
  403. };
  404. GLfloat const texCoords[] = {
  405. 0, 0,
  406. 0, 1,
  407. 1, 0,
  408. 1, 1,
  409. };
  410. auto& gl = *this->context()->functions();
  411. gl.glEnable(GL_TEXTURE_2D);
  412. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  413. juliaPreviewer->setAttributeArray(vertexLoc, vertices, 3);
  414. juliaPreviewer->setAttributeArray(texCoordsLoc, texCoords, 2);
  415. juliaPreviewer->enableAttributeArray(vertexLoc);
  416. juliaPreviewer->enableAttributeArray(texCoordsLoc);
  417. juliaPreviewer->setUniformValue(gradientScaler, 1.0f / float(gradientTextureMax));
  418. juliaPreviewer->setUniformValue(maxIterLoc, float(250));
  419. juliaPreviewer->setUniformValue(juliaX, float(jx));
  420. juliaPreviewer->setUniformValue(juliaY, float(jy));
  421. juliaPreviewer->setUniformValue(smooth, int(drawSmooth ? 1 : 0));
  422. gl.glUniform1i(gradLoc, 0);
  423. gl.glActiveTexture(GL_TEXTURE0);
  424. gl.glBindTexture(GL_TEXTURE_2D, gradientTextureId);
  425. gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  426. juliaPreviewer->disableAttributeArray(vertexLoc);
  427. juliaPreviewer->disableAttributeArray(texCoordsLoc);
  428. //juliaPreviewer->release();
  429. //program->bind();
  430. }
  431. void EscapeTimeVisualWidget::setMaxIterationCutoff(float maxIter)
  432. {
  433. this->maxIterations = maxIter;
  434. }
  435. void EscapeTimeVisualWidget::updateGradient(void)
  436. {
  437. auto& gl = *this->context()->functions();
  438. int len = 512;
  439. if (gradient.getPoints().size() > 25) {
  440. len = 2048;
  441. }
  442. else if (gradient.getPoints().size() > 7) {
  443. len = 1024;
  444. }
  445. std::unique_ptr<uint8_t[]> pixels = std::make_unique<uint8_t[]>(len * 3);
  446. for (int i = 0; i < len; i++) {
  447. RGBColor c = gradient.get(gradient.getMax() * i / len);
  448. pixels[i * 3] = c.r;
  449. pixels[i * 3 + 1] = c.g;
  450. pixels[i * 3 + 2] = c.b;
  451. }
  452. gl.glEnable(GL_TEXTURE_2D);
  453. gl.glBindTexture(GL_TEXTURE_2D, gradientTextureId);
  454. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, len, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<unsigned char*> (pixels.get()));
  455. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  456. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  457. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  458. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  459. gl.glBindTexture(GL_TEXTURE_2D, 0);
  460. this->gradientTextureMax = gradient.getMax();
  461. gradientNeedsUpdate = false;
  462. }
  463. void EscapeTimeVisualWidget::setResolutionX(int w)
  464. {
  465. resolutionX = w;
  466. }
  467. void EscapeTimeVisualWidget::setResolutionY(int h)
  468. {
  469. resolutionY = h;
  470. }
  471. int EscapeTimeVisualWidget::getResolutionX(void) const
  472. {
  473. return resolutionX;
  474. }
  475. int EscapeTimeVisualWidget::getResolutionY(void) const
  476. {
  477. return resolutionY;
  478. }