EscapeTimeVisualWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. int vertexLoc = program->attributeLocation("vertex");
  62. int texCoordsLoc = program->attributeLocation("texCoord");
  63. int colorLocation = program->uniformLocation("color");
  64. int texLoc = program->uniformLocation("tex");
  65. int gradLoc = program->uniformLocation("gradient");
  66. int gradientScaler = program->uniformLocation("gradientScaler");
  67. int maxIterations = program->uniformLocation("maxIterations");
  68. program->setAttributeArray(vertexLoc, vertices, 3);
  69. program->setAttributeArray(texCoordsLoc, texCoords, 2);
  70. program->enableAttributeArray(vertexLoc);
  71. program->enableAttributeArray(texCoordsLoc);
  72. program->setUniformValue(colorLocation, color);
  73. program->setUniformValue(gradientScaler, 1.0f / float(owner.gradientTextureMax));
  74. program->setUniformValue(maxIterations, float(owner.maxIterations));
  75. gl.glEnable(GL_TEXTURE_2D);
  76. owner.program->bind();
  77. //GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
  78. //gle.glDrawBuffers(1, drawBuffers);
  79. gl.glBindFramebuffer(GL_FRAMEBUFFER, owner.tileFramebuffer);
  80. gl.glDisable(GL_DEPTH_TEST);
  81. if(gl.glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  82. printf("error intitializing framebuffer\n");
  83. }
  84. gl.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, owner.tileTexture, 0);
  85. //gl.glViewport(0, 0, 256, 256);
  86. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  87. gl.glUniform1i(texLoc, 0);
  88. gl.glUniform1i(gradLoc, 2);
  89. gl.glActiveTexture(GL_TEXTURE0);
  90. gl.glBindTexture(GL_TEXTURE_2D, textureId);
  91. gl.glActiveTexture(GL_TEXTURE2);
  92. gl.glBindTexture(GL_TEXTURE_2D, owner.gradientTextureId);
  93. gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  94. program->disableAttributeArray(vertexLoc);
  95. program->disableAttributeArray(texCoordsLoc);
  96. /*owner.renderTextures->bind();
  97. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  98. //gl.glViewport(0, 0, owner.getResolutionX(), owner.getResolutionY());
  99. int rtVertexLoc = owner.renderTextures->attributeLocation("vertex");
  100. int rtTexCoordsLoc = owner.renderTextures->attributeLocation("texCoord");
  101. int rtTexLoc = owner.renderTextures->attributeLocation("tex");
  102. gl.glActiveTexture(GL_TEXTURE0);
  103. gl.glUniform1i(rtTexLoc, 0);
  104. owner.renderTextures->setAttributeArray(rtVertexLoc, vertices, 3);
  105. owner.renderTextures->setAttributeArray(rtTexCoordsLoc, fullTexCoords, 2);
  106. owner.renderTextures->enableAttributeArray(rtVertexLoc);
  107. owner.renderTextures->enableAttributeArray(rtTexCoordsLoc);
  108. gl.glBindTexture(GL_TEXTURE_2D, owner.tileTexture);
  109. //if (rand() % 2)
  110. //gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  111. owner.renderTextures->disableAttributeArray(rtVertexLoc);
  112. owner.renderTextures->disableAttributeArray(rtTexCoordsLoc);
  113. */
  114. gl.glActiveTexture(GL_TEXTURE0);
  115. }
  116. EscapeTimeVisualWidget::EscapeTimeVisualWidget(QWidget* parent) :
  117. QOpenGLWidget{ parent },
  118. gradientTextureId{ 0 },
  119. gradientTextureMax{ 1.0f },
  120. gradientNeedsUpdate{ false }
  121. {
  122. }
  123. void EscapeTimeVisualWidget::setGradient(Gradient newGradient)
  124. {
  125. this->gradient = newGradient;
  126. gradientNeedsUpdate = true;
  127. update();
  128. }
  129. const Gradient& EscapeTimeVisualWidget::getGradient(void)
  130. {
  131. return gradient;
  132. }
  133. void EscapeTimeVisualWidget::initializeGL(void)
  134. {
  135. auto& gl = *this->context()->functions();
  136. gl.glClearColor(0, 0, 0, 0);
  137. gl.glDisable(GL_DEPTH_TEST);
  138. // looks not even better
  139. //gl.glEnable(GL_FRAMEBUFFER_SRGB);
  140. //glShadeModel(GL_SMOOTH);
  141. renderTextures = new QOpenGLShaderProgram{ this->context() };
  142. renderTextures->addShaderFromSourceCode(QOpenGLShader::Vertex,
  143. "attribute highp vec4 vertex;\n"
  144. "attribute highp vec2 texCoord;\n"
  145. "uniform highp mat4 matrix;\n"
  146. "varying highp vec2 texc;\n"
  147. "void main(void)\n"
  148. "{\n"
  149. " gl_Position = matrix * vertex;\n"
  150. " texc = texCoord;\n"
  151. "}");
  152. renderTextures->addShaderFromSourceCode(QOpenGLShader::Fragment,
  153. "#version 110\n"
  154. "uniform sampler2D tex;\n"
  155. "varying highp vec2 texc;\n"
  156. "void main(void)\n"
  157. "{\n"
  158. " gl_FragColor = texture2D(tex, texc);\n"
  159. "}");
  160. renderTextures->link();
  161. program = new QOpenGLShaderProgram{ this->context() };
  162. bool vert = program->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. // TODO rewrite this monster
  173. if (context()->versionFunctions<QOpenGLFunctions_4_0_Core>() != nullptr) {
  174. bool frag = program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  175. "#version 400\n"
  176. "uniform sampler2D gradient;\n"
  177. "uniform sampler2D tex;\n"
  178. "uniform mediump vec4 color;\n"
  179. "uniform highp float gradientScaler;\n"
  180. "uniform highp float maxIterations;\n"
  181. "varying highp vec2 texc;\n"
  182. "vec4 colorize(float pos) {\n"
  183. " if (pos >= maxIterations) {\n"
  184. " return vec4(0.0, 0.0, 0.0, 1.0);\n"
  185. " } else {\n"
  186. " return texture2D(gradient, vec2(pos * gradientScaler, 0.0));\n"
  187. " }\n"
  188. "}\n"
  189. "void main(void)\n"
  190. "{\n"
  191. " vec2 size = textureSize(tex, 0);\n"
  192. " size = vec2(256.0, 256.0);\n"
  193. " vec2 accPoint = texc * size;\n"
  194. " vec2 ip = floor(accPoint);\n"
  195. " vec2 fp = fract(accPoint);\n"
  196. " vec4 inter = textureGather(tex, ip / size, 0);\n"
  197. " vec4 col1 = colorize(inter.x);\n"
  198. " vec4 col2 = colorize(inter.y);\n"
  199. " vec4 col3 = colorize(inter.z);\n"
  200. " vec4 col4 = colorize(inter.w);\n"
  201. " vec4 col = mix(mix(col4, col3, fp.x), mix(col1, col2, fp.x), fp.y);\n"
  202. " gl_FragColor = col;\n"
  203. // " gl_FragColor = gl_FragColor * texture2D(tex, texc);\n"
  204. // " float v = texture2D(tex, texc).r;\n"
  205. // " gl_FragColor = vec4(v, 1.0 - v, v*v, 1);\n"
  206. // " gl_FragColor.g = 0.3;\n"
  207. "}");
  208. }
  209. else {
  210. bool frag = program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  211. "#version 110\n"
  212. "uniform sampler2D gradient;\n"
  213. "uniform sampler2D tex;\n"
  214. "uniform mediump vec4 color;\n"
  215. "uniform highp float gradientScaler;\n"
  216. "uniform highp float maxIterations;\n"
  217. "varying highp vec2 texc;\n"
  218. "void main(void)\n"
  219. "{\n"
  220. " float v = texture2D(tex, texc).r;\n"
  221. " if (v >= maxIterations) {\n"
  222. " gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
  223. " } else {\n"
  224. " gl_FragColor = texture2D(gradient, vec2(v * gradientScaler, 0.0));\n"
  225. " }\n"
  226. // " gl_FragColor = gl_FragColor * texture2D(tex, texc);\n"
  227. // " float v = texture2D(tex, texc).r;\n"
  228. // " gl_FragColor = vec4(v, 1.0 - v, v*v, 1);\n"
  229. // " gl_FragColor.g = 0.3;\n"
  230. "}");
  231. }
  232. //program.link();
  233. bool bound = program->bind();
  234. bound = renderTextures->bind();
  235. int vertexLoc = program->attributeLocation("vertex");
  236. int texCoordsLoc = program->attributeLocation("texCoord");
  237. int colorLocation = program->uniformLocation("color");
  238. int texLoc = program->uniformLocation("tex");
  239. int gradLoc = program->uniformLocation("gradient");
  240. int gradientScaler = program->uniformLocation("gradientScaler");
  241. int maxIterations = program->uniformLocation("maxIterations");
  242. program->setUniformValue(gradientScaler, 0.005f);
  243. program->setUniformValue(maxIterations, 250.0f);
  244. auto& gle = *this->context()->extraFunctions();
  245. gl.glGenTextures(1, &tileTexture);
  246. gl.glBindTexture(GL_TEXTURE_2D, tileTexture);
  247. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  248. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  249. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  250. gl.glBindTexture(GL_TEXTURE_2D, 0);
  251. gl.glGenFramebuffers(1, &tileFramebuffer);
  252. gl.glBindFramebuffer(GL_FRAMEBUFFER, tileFramebuffer);
  253. gl.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tileTexture, 0);
  254. GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
  255. gle.glDrawBuffers(1, drawBuffers);
  256. if(gl.glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
  257. printf("error intitializing framebuffer\n");
  258. }
  259. gl.glBindFramebuffer(GL_FRAMEBUFFER, 0);
  260. unsigned char pix[] = { 255, 0, 0, 0, 255, 0, 0, 0, 255 };
  261. GLuint id;
  262. gl.glEnable(GL_TEXTURE_2D);
  263. gl.glGenTextures(1, &id);
  264. gl.glBindTexture(GL_TEXTURE_2D, id);
  265. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 3, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<char*> (pix));
  266. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  267. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  268. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  269. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  270. gl.glBindTexture(GL_TEXTURE_2D, 0);
  271. gradientTextureId = id;
  272. gl.glDisable(GL_DEPTH_TEST);
  273. //gl3.glBindSampler(0, id);
  274. }
  275. void EscapeTimeVisualWidget::resizeGL(int w, int h)
  276. {
  277. auto& gl = *this->context()->functions();
  278. float pixelRatio = this->devicePixelRatioF();
  279. //pixelRatio = 1.0 / 32;
  280. float newW = w * pixelRatio;
  281. float newH = h * pixelRatio;
  282. setResolutionX(newW);
  283. setResolutionY(newH);
  284. gl.glViewport(0, 0, newW, newH);
  285. QMatrix4x4 pmvMatrix;
  286. pmvMatrix.ortho(QRectF{ 0, 0, newW, newH });
  287. int matrixLocation = program->uniformLocation("matrix");
  288. int rtMatrixLocation = renderTextures->uniformLocation("matrix");
  289. program->setUniformValue(matrixLocation, pmvMatrix);
  290. renderTextures->setUniformValue(rtMatrixLocation, pmvMatrix);
  291. }
  292. void EscapeTimeVisualWidget::paintGL(void)
  293. {
  294. if (gradientNeedsUpdate)
  295. updateGradient();
  296. /*ETVImage etvi{ *this };
  297. auto& gl = *this->context()->functions();
  298. gl.glClearColor(0.0, 0.2, 0.0, 1.0);
  299. gl.glClear(GL_COLOR_BUFFER_BIT);
  300. etvi.draw(100, 100, 700, 700);*/
  301. }
  302. void EscapeTimeVisualWidget::setMaxIterationCutoff(float maxIter)
  303. {
  304. this->maxIterations = maxIter;
  305. }
  306. void EscapeTimeVisualWidget::updateGradient(void)
  307. {
  308. auto& gl = *this->context()->functions();
  309. const int len = 1024;
  310. std::unique_ptr<uint8_t[]> pixels = std::make_unique<uint8_t[]>(len * 3);
  311. for (int i = 0; i < len; i++) {
  312. RGBColor c = gradient.get(gradient.getMax() * i / len);
  313. pixels[i * 3] = c.r;
  314. pixels[i * 3 + 1] = c.g;
  315. pixels[i * 3 + 2] = c.b;
  316. }
  317. gl.glEnable(GL_TEXTURE_2D);
  318. gl.glBindTexture(GL_TEXTURE_2D, gradientTextureId);
  319. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, len, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<unsigned char*> (pixels.get()));
  320. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  321. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  322. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  323. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  324. gl.glBindTexture(GL_TEXTURE_2D, 0);
  325. this->gradientTextureMax = gradient.getMax();
  326. gradientNeedsUpdate = false;
  327. }
  328. void EscapeTimeVisualWidget::setResolutionX(int w)
  329. {
  330. resolutionX = w;
  331. }
  332. void EscapeTimeVisualWidget::setResolutionY(int h)
  333. {
  334. resolutionY = h;
  335. }
  336. int EscapeTimeVisualWidget::getResolutionX(void) const
  337. {
  338. return resolutionX;
  339. }
  340. int EscapeTimeVisualWidget::getResolutionY(void) const
  341. {
  342. return resolutionY;
  343. }