diff --git a/Fragment.glsl b/Fragment.glsl new file mode 100644 index 0000000..22308e6 --- /dev/null +++ b/Fragment.glsl @@ -0,0 +1,6 @@ +#version 330 core +out vec4 pixColor; + +void main(){ + pixColor = vec4(1., 0., 0., 1); +} \ No newline at end of file diff --git a/Vertex.glsl b/Vertex.glsl new file mode 100644 index 0000000..42802cf --- /dev/null +++ b/Vertex.glsl @@ -0,0 +1,6 @@ +#version 330 core +layout (location = 0) in vec2 position; + +void main(){ + gl_Position = vec4(position.x, position.y, 0.0, 1.); +} \ No newline at end of file diff --git a/headers/Mandelbrot.h b/headers/Mandelbrot.h index aed8856..7b1332a 100644 --- a/headers/Mandelbrot.h +++ b/headers/Mandelbrot.h @@ -3,19 +3,17 @@ #include #include +#include class Mandelbrot : public QObject, protected QOpenGLExtraFunctions { Q_OBJECT public: void init(); - void draw(); + void draw(GLuint, int, QOpenGLShaderProgram&); void zoom(double); public slots: void setIterations(int); private: - static float* genVertices(); - GLuint createVAO(); - GLuint vao; int iterations = 0; double scale = 1; QVector2D translation; diff --git a/headers/OutputWidget.h b/headers/OutputWidget.h index e76d66c..16eebf2 100644 --- a/headers/OutputWidget.h +++ b/headers/OutputWidget.h @@ -22,6 +22,13 @@ private: void mouseReleaseEvent(QMouseEvent*) override; void keyPressEvent(QKeyEvent*) override; + void initShader(); + static std::vector genVertices(); + GLuint createVAO(); + GLuint vao; + int vertCount; + QOpenGLShaderProgram shader; + Mandelbrot mandelbrot; }; diff --git a/src/Mandelbrot.cpp b/src/Mandelbrot.cpp index 813d6a9..f005043 100644 --- a/src/Mandelbrot.cpp +++ b/src/Mandelbrot.cpp @@ -3,12 +3,13 @@ void Mandelbrot::init() { initializeOpenGLFunctions(); - - vao = createVAO(); } -void Mandelbrot::draw() { - +void Mandelbrot::draw(GLuint vao, int vertCount, QOpenGLShaderProgram& shader) { + shader.bind(); + glBindVertexArray(vao); + glDrawArrays(GL_QUADS, 0, vertCount); + glBindVertexArray(0); } void Mandelbrot::zoom(double delta) { @@ -19,34 +20,4 @@ void Mandelbrot::setIterations(int value) { iterations = value; } -GLuint Mandelbrot::createVAO() { - GLuint vboId; - glGenBuffers(1, &vboId); - glBindBuffer(GL_ARRAY_BUFFER, vboId); - - float* vertices = genVertices(); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - GLuint vaoId; - glGenVertexArrays(1, &vaoId); - glBindVertexArray(vaoId); - glBindBuffer(GL_ARRAY_BUFFER, vboId); - - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*) nullptr); - glEnableVertexAttribArray(0); - - glBindVertexArray(0); - - return vaoId; -} - -float* Mandelbrot::genVertices() { - return new float[8]{ - -1, -1, - 1, -1, - 1, 1, - -1, 1 - }; -} - diff --git a/src/OutputWidget.cpp b/src/OutputWidget.cpp index b5b7e5b..bc1a300 100644 --- a/src/OutputWidget.cpp +++ b/src/OutputWidget.cpp @@ -1,5 +1,6 @@ #include +#include #include "../headers/OutputWidget.h" using std::cout, std::endl; @@ -8,20 +9,64 @@ void OutputWidget::initializeGL() { cout << "Initialize OpenGL" << endl; initializeOpenGLFunctions(); getMandelbrot()->init(); + initShader(); auto format = this->format(); cout << "OpenGL version: " << format.majorVersion() << "." << format.minorVersion() << endl; + vao = createVAO(); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glClearColor(0.5f, 0.5f, 0.5f, 1.0f); } void OutputWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT); - getMandelbrot()->draw(); + getMandelbrot()->draw(vao, vertCount, shader); } void OutputWidget::resizeGL(int w, int h) { + //glViewport(0, 0, w, h); +} + +GLuint OutputWidget::createVAO() { + GLuint vboId; + glGenBuffers(1, &vboId); + glBindBuffer(GL_ARRAY_BUFFER, vboId); + + auto vertices = genVertices(); + size_t size = vertices.size() * sizeof(QVector2D); + vertCount = int(vertices.size()); + glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)size, &vertices[0], GL_STATIC_DRAW); + + GLuint vaoId; + glGenVertexArrays(1, &vaoId); + glBindVertexArray(vaoId); + glBindBuffer(GL_ARRAY_BUFFER, vboId); + + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(QVector2D), (GLvoid const*) 0); + glEnableVertexAttribArray(0); + + glBindVertexArray(0); + + return vaoId; +} + +std::vector OutputWidget::genVertices() { + return { + {-1, -1}, + {1, -1}, + {1, 1}, + {-1, 1} + }; +} +void OutputWidget::initShader() { + if (!shader.addShaderFromSourceFile(QOpenGLShader::Vertex, "Vertex.glsl")) close(); + if (!shader.addShaderFromSourceFile(QOpenGLShader::Fragment, "Fragment.glsl")) close(); + if (!shader.link()) close(); + if (!shader.bind()) close(); } void OutputWidget::wheelEvent(QWheelEvent *e) {