EscapeTimeVisualWidget.cpp 18 KB

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