Skip to content

Commit

Permalink
wasm: Fix openglwindow example
Browse files Browse the repository at this point in the history
Use QOpenGLBuffer to satisfy WebGL

Change-Id: Id40a85feb329a85844637c5f5a7a8ae534969086
Reviewed-by: Laszlo Agocs <[email protected]>
Reviewed-by: Qt CI Bot <[email protected]>
  • Loading branch information
EvenOAndersen committed Mar 22, 2024
1 parent f3b7364 commit 6f68cb9
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions examples/opengl/openglwindow/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <QGuiApplication>
#include <QMatrix4x4>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QScreen>
#include <QtMath>


//! [1]
class TriangleWindow : public OpenGLWindow
{
Expand All @@ -20,10 +20,8 @@ class TriangleWindow : public OpenGLWindow
void render() override;

private:
GLint m_posAttr = 0;
GLint m_colAttr = 0;
GLint m_matrixUniform = 0;

QOpenGLBuffer m_vbo;
QOpenGLShaderProgram *m_program = nullptr;
int m_frame = 0;
};
Expand All @@ -48,36 +46,48 @@ int main(int argc, char **argv)
}
//! [2]


//! [3]
static const char *vertexShaderSource =
"attribute highp vec4 posAttr;\n"
"attribute lowp vec4 colAttr;\n"
"varying lowp vec4 col;\n"
"uniform highp mat4 matrix;\n"
"void main() {\n"
" col = colAttr;\n"
" gl_Position = matrix * posAttr;\n"
"}\n";

static const char *fragmentShaderSource =
"varying lowp vec4 col;\n"
"void main() {\n"
" gl_FragColor = col;\n"
"}\n";
static const char *vertexShaderSource = "attribute highp vec4 posAttr;\n"
"attribute lowp vec4 colAttr;\n"
"varying lowp vec4 col;\n"
"uniform highp mat4 matrix;\n"
"void main() {\n"
" col = colAttr;\n"
" gl_Position = matrix * posAttr;\n"
"}\n";

static const char *fragmentShaderSource = "varying lowp vec4 col;\n"
"void main() {\n"
" gl_FragColor = col;\n"
"}\n";
//! [3]

//! [4]
void TriangleWindow::initialize()
{
static const GLfloat vertices_colors[] = { +0.0f, +0.707f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.500f, 0.0f, 1.0f, 0.0f,
+0.5f, -0.500f, 0.0f, 0.0f, 1.0f };

m_vbo.create();
m_vbo.bind();
m_vbo.allocate(vertices_colors, sizeof(vertices_colors));

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), nullptr);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
reinterpret_cast<void *>(2 * sizeof(GLfloat)));

m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->bindAttributeLocation("posAttr", 0);
m_program->bindAttributeLocation("colAttr", 1);
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
Q_ASSERT(m_posAttr != -1);
m_colAttr = m_program->attributeLocation("colAttr");
Q_ASSERT(m_colAttr != -1);
m_program->bind();

m_matrixUniform = m_program->uniformLocation("matrix");
Q_ASSERT(m_matrixUniform != -1);
}
Expand All @@ -100,28 +110,13 @@ void TriangleWindow::render()

m_program->setUniformValue(m_matrixUniform, matrix);

static const GLfloat vertices[] = {
0.0f, 0.707f,
-0.5f, -0.5f,
0.5f, -0.5f
};

static const GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};

glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);

glEnableVertexAttribArray(m_posAttr);
glEnableVertexAttribArray(m_colAttr);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(m_colAttr);
glDisableVertexAttribArray(m_posAttr);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

m_program->release();

Expand Down

0 comments on commit 6f68cb9

Please sign in to comment.