Nicolas Winkler 5 anos atrás
pai
commit
36dc2d4fcd
2 arquivos alterados com 119 adições e 34 exclusões
  1. 69 23
      MandelWidget.cpp
  2. 50 11
      MandelWidget.h

+ 69 - 23
MandelWidget.cpp

@@ -12,7 +12,7 @@ using namespace mnd;
 #include <cstdio>
 
 
-Texture::Texture(QOpenGLFunctions_3_0& gl, const Bitmap<RGBColor>& bitmap, GLint param) :
+Texture::Texture(QOpenGLFunctions& gl, const Bitmap<RGBColor>& bitmap, GLint param) :
     gl{ gl }
 {
     gl.glGenTextures(1, &id);
@@ -59,8 +59,47 @@ void Texture::bind(void) const
 }
 
 
-void Texture::drawRect(float x, float y, float width, float height)
+void Texture::drawRect(QOpenGLShaderProgram* program,
+                       float x, float y, float width, float height,
+                       float tx, float ty, float tw, float th)
 {
+#if 1
+    GLfloat const vertices[] = {
+        x, y,  0.0f,
+        x, y + height, 0.0f,
+        x + width, y + height, 0.0f,
+        x + width, y, 0.0f,
+    };
+
+    GLfloat const texCoords[] = {
+        tx,      ty,
+        tx,      ty + th,
+        tx + tw, ty + th,
+        tx + tw, ty,
+    };
+
+    QColor color(255, 255, 255);
+
+    int vertexLoc = program->attributeLocation("vertex");
+    int texCoordsLoc = program->attributeLocation("texCoord");
+    int colorLocation = program->uniformLocation("color");
+    int texLoc = program->uniformLocation("tex");
+    program->enableAttributeArray(vertexLoc);
+    program->enableAttributeArray(texCoordsLoc);
+    program->setAttributeArray(vertexLoc, vertices, 3);
+    program->setAttributeArray(texCoordsLoc, texCoords, 2);
+    program->setUniformValue(colorLocation, color);
+
+
+    auto& gl3 = *QOpenGLContext::currentContext()->functions();
+    gl3.glUniform1i(texLoc, 0);
+    gl3.glActiveTexture(GL_TEXTURE0 + 0);
+    gl3.glBindTexture(GL_TEXTURE_2D, id);
+    gl3.glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+    program->disableAttributeArray(vertexLoc);
+    program->disableAttributeArray(texCoordsLoc);
+#else
     gl.glColor3ub(255, 255, 255);
     gl.glEnable(GL_TEXTURE_2D);
     bind();
@@ -75,6 +114,7 @@ void Texture::drawRect(float x, float y, float width, float height)
     gl.glVertex2f(x + width, y + height);
     gl.glEnd();
     gl.glDisable(GL_TEXTURE_2D);
+#endif
 }
 
 
@@ -88,7 +128,9 @@ TextureClip::~TextureClip(void)
 {
 }
 
-void TextureClip::drawRect(float x, float y, float width, float height)
+
+void TextureClip::drawRect(QOpenGLShaderProgram* program,
+        float x, float y, float width, float height)
 {
     /*
     auto& gl = texture->gl;
@@ -106,17 +148,8 @@ void TextureClip::drawRect(float x, float y, float width, float height)
     gl.glVertex2f(x + width, y + height);
     gl.glEnd();
     gl.glDisable(GL_TEXTURE_2D);*/
-
-    int vertexLocation = program->attributeLocation("vertex");
-    program->enableAttributeArray(vertexLocation);
-    program->setAttributeArray(vertexLocation, triangleVertices, 3);
-    program->setUniformValue(matrixLocation, pmvMatrix);
-    program->setUniformValue(colorLocation, color);
-
-    auto& gl3 = *QOpenGLContext::currentContext()->functions();
-    gl3.glDrawArrays(GL_TRIANGLES, 0, 3);
-
-    program->disableAttributeArray(vertexLocation);
+    texture->drawRect(program, x, y, width, height,
+                      tx, ty, tw, th);
 }
 
 
@@ -147,11 +180,13 @@ QuadImage::~QuadImage(void)
 }
 
 
-void QuadImage::drawRect(float x, float y, float width, float height)
+void QuadImage::drawRect(QOpenGLShaderProgram* program,
+        float x, float y, float width, float height)
 {
     for (int i = 0; i < 2; i++) {
         for (int j = 0; j < 2; j++) {
-            this->cells[i][j]->drawRect(x + i * 0.5f * width,
+            this->cells[i][j]->drawRect(program,
+                                        x + i * 0.5f * width,
                                         y + j * 0.5f * height,
                                         width * 0.5f,
                                         height * 0.5f);
@@ -349,7 +384,7 @@ MandelView::MandelView(mnd::MandelGenerator* generator, MandelWidget& owner) :
     }*/
     Bitmap<RGBColor> emp(1, 1);
     emp.get(0, 0) = RGBColor{ 0, 0, 0 };
-    auto& gl = *QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_2_0>();
+    auto& gl = *QOpenGLContext::currentContext()->functions();
     empty = std::make_unique<Texture>(gl, emp, GL_NEAREST);
     connect(&calcer, &Calcer::done, this, &MandelView::cellReady);
 }
@@ -503,7 +538,7 @@ GridElement* MandelView::searchUnder(int level, GridIndex i, GridIndex j, int re
 }
 
 
-void MandelView::paint(const mnd::MandelViewport& mvp, QPainter& qp)
+void MandelView::paint(const mnd::MandelViewport& mvp)
 {    
     mnd::Real dpp = mvp.width / width;
     int level = getLevel(dpp) - 1;
@@ -541,7 +576,7 @@ void MandelView::paint(const mnd::MandelViewport& mvp, QPainter& qp)
             }
 
             if (t != nullptr) {
-                t->img->drawRect(float(x), float(y), float(w), float(w));
+                t->img->drawRect(this->owner.program,float(x), float(y), float(w), float(w));
                 /*glBegin(GL_LINE_LOOP);
                 glVertex2f(float(x), float(y));
                 glVertex2f(float(x) + float(w), float(y));
@@ -555,7 +590,8 @@ void MandelView::paint(const mnd::MandelViewport& mvp, QPainter& qp)
             }
             else {
                 calcer.calc(grid, level, i, j, 1000);
-                this->empty->drawRect(float(x), float(y), float(w), float(w));
+                this->empty->drawRect(this->owner.program,
+                            float(x), float(y), float(w), float(w));
             }
         }
     }
@@ -563,7 +599,7 @@ void MandelView::paint(const mnd::MandelViewport& mvp, QPainter& qp)
 
 void MandelView::cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp)
 {
-    auto& gl = *QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_2_0>();
+    auto& gl = *QOpenGLContext::currentContext()->functions();
     this->getGrid(level).setCell(i, j,
         std::make_unique<GridElement>(true, std::make_shared<TextureClip>(std::make_shared<Texture>(gl, *bmp))));
     delete bmp;
@@ -679,20 +715,28 @@ void MandelWidget::initializeGL(void)
     program = new QOpenGLShaderProgram{ this->context() };
     bool vert = program->addShaderFromSourceCode(QOpenGLShader::Vertex,
     "attribute highp vec4 vertex;\n"
+    "attribute highp vec2 texCoord;\n"
     "uniform highp mat4 matrix;\n"
+    "varying highp vec2 texc;\n"
     "void main(void)\n"
     "{\n"
     "   gl_Position = matrix * vertex;\n"
+    "   texc = texCoord;\n"
     "}");
     bool frag = program->addShaderFromSourceCode(QOpenGLShader::Fragment,
     "uniform mediump vec4 color;\n"
+    "varying highp vec2 texc;\n"
+    "uniform sampler2D tex;\n"
     "void main(void)\n"
     "{\n"
-    "   gl_FragColor = color;\n"
+    "   gl_FragColor = color * texture2D(tex, texc);\n"
+//    "   gl_FragColor.g = 0.3;\n"
     "}");
     //program.link();
     bool bound = program->bind();
 
+    //gl3.glBindSampler(0, id);
+
     mandelView = nullptr;
     requestRecalc();
 }
@@ -744,7 +788,6 @@ void MandelWidget::paintGL(void)
 
     gl.glClear(GL_COLOR_BUFFER_BIT);
 
-    updateAnimations();
 
     QPainter painter{ this };
 
@@ -756,6 +799,9 @@ void MandelWidget::paintGL(void)
         drawInfo();
     if (selectingPoint)
         drawPoint();*/
+    //QPainter painter{ this };
+    updateAnimations();
+    mandelView->paint(this->currentViewport);
 
     static GLfloat const triangleVertices[] = {
         0.0,  20,  0.0f,

+ 50 - 11
MandelWidget.h

@@ -36,10 +36,12 @@ class MandelWidget;
 
 class Texture
 {
+protected:
     GLuint id;
+    QOpenGLFunctions& gl;
+    inline Texture(QOpenGLFunctions& gl) : gl{ gl } {}
 public:
-    QOpenGLFunctions_3_0& gl;
-    Texture(QOpenGLFunctions_3_0& gl, const Bitmap<RGBColor>& pict, GLint param = GL_LINEAR);
+    Texture(QOpenGLFunctions& gl, const Bitmap<RGBColor>& pict, GLint param = GL_LINEAR);
     ~Texture(void);
 
     Texture(const Texture& other) = delete;
@@ -53,7 +55,39 @@ private:
 public:
     inline GLuint getId(void) const { return id; }
 
-    void drawRect(float x, float y, float width, float height);
+    void drawRect(QOpenGLShaderProgram* program,
+                  float x, float y, float width, float height,
+                  float tx = 0.0f, float ty = 0.0f,
+                  float tw = 1.0f, float th = 1.0f);
+};
+
+
+///
+/// \brief The FloatTexture class
+///
+class FloatTexture
+{
+    GLuint id;
+    QOpenGLFunctions& gl;
+public:
+    FloatTexture(QOpenGLFunctions& gl, const Bitmap<float>& pict, GLint param = GL_LINEAR);
+    ~Texture(void);
+
+    Texture(const Texture& other) = delete;
+    Texture& operator=(const Texture& other) = delete;
+
+    Texture(Texture&& other);
+    Texture& operator=(Texture&& other) = delete;
+
+private:
+    void bind(void) const;
+public:
+    inline GLuint getId(void) const { return id; }
+
+    void drawRect(QOpenGLShaderProgram* program,
+                  float x, float y, float width, float height,
+                  float tx = 0.0f, float ty = 0.0f,
+                  float tw = 1.0f, float th = 1.0f);
 };
 
 
@@ -65,7 +99,8 @@ public:
     CellImage(const CellImage& b) = delete;
     virtual ~CellImage(void);
 
-    virtual void drawRect(float x, float y, float width, float height) = 0;
+    virtual void drawRect(QOpenGLShaderProgram* program,
+                          float x, float y, float width, float height) = 0;
     virtual std::shared_ptr<CellImage> clip(short i, short j) = 0;
     virtual int getRecalcPriority(void) const = 0;
 };
@@ -91,11 +126,12 @@ public:
 
     virtual ~TextureClip(void);
 
-    void drawRect(float x, float y, float width, float height);
+    void drawRect(QOpenGLShaderProgram* program,
+                  float x, float y, float width, float height) override;
 
     TextureClip clip(float x, float y, float w, float h);
-    std::shared_ptr<CellImage> clip(short i, short j);
-    int getRecalcPriority(void) const;
+    std::shared_ptr<CellImage> clip(short i, short j) override;
+    int getRecalcPriority(void) const override;
 };
 
 
@@ -115,9 +151,10 @@ public:
 
     virtual ~QuadImage(void);
 
-    void drawRect(float x, float y, float width, float height);
-    std::shared_ptr<CellImage> clip(short i, short j);
-    int getRecalcPriority(void) const;
+    void drawRect(QOpenGLShaderProgram* program,
+                  float x, float y, float width, float height) override;
+    std::shared_ptr<CellImage> clip(short i, short j) override;
+    int getRecalcPriority(void) const override;
 };
 
 
@@ -267,7 +304,7 @@ public:
     void garbageCollect(int level, GridIndex i, GridIndex j);
     GridElement* searchAbove(int level, GridIndex i, GridIndex j, int recursionLevel);
     GridElement* searchUnder(int level, GridIndex i, GridIndex j, int recursionLevel);
-    void paint(const mnd::MandelViewport& mvp, QPainter& qp);
+    void paint(const mnd::MandelViewport& mvp);
 public slots:
     void cellReady(int level, GridIndex i, GridIndex j, Bitmap<RGBColor>* bmp);
 signals:
@@ -278,6 +315,8 @@ signals:
 class MandelWidget : public QOpenGLWidget
 {
     Q_OBJECT
+
+    friend class MandelView;
 private:
     mnd::MandelContext& mndContext;
     mnd::MandelGenerator* generator;