1
0

EscapeTimeVisualWidget.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "EscapeTimeVisualWidget.h"
  2. #include "Bitmap.h"
  3. #include <QOpenGLShaderProgram>
  4. #include <QOpenGLContext>
  5. #include <QOpenGLFunctions>
  6. ETVImage::ETVImage(EscapeTimeVisualWidget& owner) :
  7. owner{ owner }
  8. {
  9. auto& gl = *owner.context()->functions();
  10. gl.glGenTextures(1, &textureId);
  11. gl.glActiveTexture(GL_TEXTURE0);
  12. gl.glBindTexture(GL_TEXTURE_2D, textureId);
  13. Bitmap<float> img{512, 512};
  14. for (int i = 0; i < img.width; i++) {
  15. for (int j = 0; j < img.height; j++) {
  16. img.get(i, j) = (i ^ j);
  17. }
  18. }
  19. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, int(img.width), int(img.height), 0, GL_RED, GL_FLOAT, img.pixels.get());
  20. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  21. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  22. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  23. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  24. gl.glBindTexture(GL_TEXTURE_2D, 0);
  25. }
  26. ETVImage::~ETVImage(void)
  27. {
  28. auto& gl = *owner.context()->functions();
  29. gl.glDeleteTextures(1, &textureId);
  30. }
  31. void ETVImage::draw(float x, float y, float w, float h,
  32. float tx, float ty, float tw, float th)
  33. {
  34. auto& gl = *owner.context()->functions();
  35. GLfloat const vertices[] = {
  36. x, y, 0.0f,
  37. x, y + h, 0.0f,
  38. x + w, y, 0.0f,
  39. x + w, y + h, 0.0f,
  40. };
  41. GLfloat const texCoords[] = {
  42. tx, ty,
  43. tx, ty + th,
  44. tx + tw, ty,
  45. tx + tw, ty + th,
  46. };
  47. QColor color{ 255, 255, 255 };
  48. auto& program = owner.program;
  49. int vertexLoc = program->attributeLocation("vertex");
  50. int texCoordsLoc = program->attributeLocation("texCoord");
  51. int colorLocation = program->uniformLocation("color");
  52. int texLoc = program->uniformLocation("tex");
  53. int gradLoc = program->uniformLocation("gradient");
  54. program->setAttributeArray(vertexLoc, vertices, 3);
  55. program->setAttributeArray(texCoordsLoc, texCoords, 2);
  56. program->enableAttributeArray(vertexLoc);
  57. program->enableAttributeArray(texCoordsLoc);
  58. program->setUniformValue(colorLocation, color);
  59. gl.glEnable(GL_TEXTURE_2D);
  60. gl.glUniform1i(texLoc, 0);
  61. gl.glUniform1i(gradLoc, 2);
  62. gl.glActiveTexture(GL_TEXTURE0);
  63. gl.glBindTexture(GL_TEXTURE_2D, textureId);
  64. gl.glActiveTexture(GL_TEXTURE2);
  65. gl.glBindTexture(GL_TEXTURE_2D, owner.gradientTextureId);
  66. gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  67. program->disableAttributeArray(vertexLoc);
  68. program->disableAttributeArray(texCoordsLoc);
  69. gl.glActiveTexture(GL_TEXTURE0);
  70. }
  71. EscapeTimeVisualWidget::EscapeTimeVisualWidget(QWidget* parent) :
  72. QOpenGLWidget{ parent }
  73. {
  74. }
  75. void EscapeTimeVisualWidget::initializeGL(void)
  76. {
  77. auto& gl = *this->context()->functions();
  78. gl.glClearColor(0, 0, 0, 0);
  79. gl.glDisable(GL_DEPTH_TEST);
  80. // looks not even better
  81. //gl.glEnable(GL_FRAMEBUFFER_SRGB);
  82. //glShadeModel(GL_SMOOTH);
  83. program = new QOpenGLShaderProgram{ this->context() };
  84. bool vert = program->addShaderFromSourceCode(QOpenGLShader::Vertex,
  85. "attribute highp vec4 vertex;\n"
  86. "attribute highp vec2 texCoord;\n"
  87. "uniform highp mat4 matrix;\n"
  88. "varying highp vec2 texc;\n"
  89. "void main(void)\n"
  90. "{\n"
  91. " gl_Position = matrix * vertex;\n"
  92. " texc = texCoord;\n"
  93. "}");
  94. bool frag = program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  95. "uniform sampler2D gradient;\n"
  96. "uniform sampler2D tex;\n"
  97. "uniform mediump vec4 color;\n"
  98. "varying highp vec2 texc;\n"
  99. "void main(void)\n"
  100. "{\n"
  101. " float v = texture2D(tex, texc).r;\n"
  102. " gl_FragColor = texture2D(gradient, vec2(v*0.005, 0.0));\n"
  103. // " gl_FragColor = gl_FragColor * texture2D(tex, texc);\n"
  104. // " float v = texture2D(tex, texc).r;\n"
  105. // " gl_FragColor = vec4(v, 1.0 - v, v*v, 1);\n"
  106. // " gl_FragColor.g = 0.3;\n"
  107. "}");
  108. //program.link();
  109. bool bound = program->bind();
  110. unsigned char pix[] = { 255, 0, 0, 0, 255, 0, 0, 0, 255 };
  111. GLuint id;
  112. gl.glEnable(GL_TEXTURE_2D);
  113. gl.glGenTextures(1, &id);
  114. gl.glBindTexture(GL_TEXTURE_2D, id);
  115. gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 3, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<char*> (pix));
  116. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  117. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  118. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  119. gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  120. gl.glBindTexture(GL_TEXTURE_2D, 0);
  121. gradientTextureId = id;
  122. //gl3.glBindSampler(0, id);
  123. }
  124. void EscapeTimeVisualWidget::resizeGL(int w, int h)
  125. {
  126. auto& gl = *this->context()->functions();
  127. float pixelRatio = this->devicePixelRatioF();
  128. gl.glViewport(0, 0, w * pixelRatio, h * pixelRatio);
  129. QMatrix4x4 pmvMatrix;
  130. pmvMatrix.ortho(QRectF{ 0, 0, w * pixelRatio, h * pixelRatio });
  131. int matrixLocation = program->uniformLocation("matrix");
  132. program->setUniformValue(matrixLocation, pmvMatrix);
  133. }
  134. void EscapeTimeVisualWidget::paintGL(void)
  135. {
  136. ETVImage etvi{ *this };
  137. auto& gl = *this->context()->functions();
  138. gl.glClearColor(0.0, 0.2, 0.0, 1.0);
  139. gl.glClear(GL_COLOR_BUFFER_BIT);
  140. etvi.draw(100, 100, 700, 700);
  141. }