From 6f68cb9a60c037251927d4fe49cedf575fab4c72 Mon Sep 17 00:00:00 2001 From: Even Oscar Andersen Date: Mon, 18 Mar 2024 14:12:26 +0100 Subject: wasm: Fix openglwindow example Use QOpenGLBuffer to satisfy WebGL Change-Id: Id40a85feb329a85844637c5f5a7a8ae534969086 Reviewed-by: Laszlo Agocs Reviewed-by: Qt CI Bot --- examples/opengl/openglwindow/main.cpp | 81 ++++++++++++++++------------------- 1 file changed, 38 insertions(+), 43 deletions(-) (limited to 'examples/opengl/openglwindow') diff --git a/examples/opengl/openglwindow/main.cpp b/examples/opengl/openglwindow/main.cpp index 6813dc9efd..89933fa379 100644 --- a/examples/opengl/openglwindow/main.cpp +++ b/examples/opengl/openglwindow/main.cpp @@ -6,10 +6,10 @@ #include #include #include +#include #include #include - //! [1] class TriangleWindow : public OpenGLWindow { @@ -20,10 +20,8 @@ public: 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; }; @@ -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(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); } @@ -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(); -- cgit v1.2.3