wasm: Fix openglwindow example
Use QOpenGLBuffer to satisfy WebGL Change-Id: Id40a85feb329a85844637c5f5a7a8ae534969086 Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
parent
f3b73645a8
commit
6f68cb9a60
@ -6,10 +6,10 @@
|
|||||||
#include <QGuiApplication>
|
#include <QGuiApplication>
|
||||||
#include <QMatrix4x4>
|
#include <QMatrix4x4>
|
||||||
#include <QOpenGLShaderProgram>
|
#include <QOpenGLShaderProgram>
|
||||||
|
#include <QOpenGLBuffer>
|
||||||
#include <QScreen>
|
#include <QScreen>
|
||||||
#include <QtMath>
|
#include <QtMath>
|
||||||
|
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
class TriangleWindow : public OpenGLWindow
|
class TriangleWindow : public OpenGLWindow
|
||||||
{
|
{
|
||||||
@ -20,10 +20,8 @@ public:
|
|||||||
void render() override;
|
void render() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLint m_posAttr = 0;
|
|
||||||
GLint m_colAttr = 0;
|
|
||||||
GLint m_matrixUniform = 0;
|
GLint m_matrixUniform = 0;
|
||||||
|
QOpenGLBuffer m_vbo;
|
||||||
QOpenGLShaderProgram *m_program = nullptr;
|
QOpenGLShaderProgram *m_program = nullptr;
|
||||||
int m_frame = 0;
|
int m_frame = 0;
|
||||||
};
|
};
|
||||||
@ -48,10 +46,8 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
//! [2]
|
//! [2]
|
||||||
|
|
||||||
|
|
||||||
//! [3]
|
//! [3]
|
||||||
static const char *vertexShaderSource =
|
static const char *vertexShaderSource = "attribute highp vec4 posAttr;\n"
|
||||||
"attribute highp vec4 posAttr;\n"
|
|
||||||
"attribute lowp vec4 colAttr;\n"
|
"attribute lowp vec4 colAttr;\n"
|
||||||
"varying lowp vec4 col;\n"
|
"varying lowp vec4 col;\n"
|
||||||
"uniform highp mat4 matrix;\n"
|
"uniform highp mat4 matrix;\n"
|
||||||
@ -60,8 +56,7 @@ static const char *vertexShaderSource =
|
|||||||
" gl_Position = matrix * posAttr;\n"
|
" gl_Position = matrix * posAttr;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
static const char *fragmentShaderSource =
|
static const char *fragmentShaderSource = "varying lowp vec4 col;\n"
|
||||||
"varying lowp vec4 col;\n"
|
|
||||||
"void main() {\n"
|
"void main() {\n"
|
||||||
" gl_FragColor = col;\n"
|
" gl_FragColor = col;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
@ -70,14 +65,29 @@ static const char *fragmentShaderSource =
|
|||||||
//! [4]
|
//! [4]
|
||||||
void TriangleWindow::initialize()
|
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 = new QOpenGLShaderProgram(this);
|
||||||
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
|
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
|
||||||
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
|
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
|
||||||
|
m_program->bindAttributeLocation("posAttr", 0);
|
||||||
|
m_program->bindAttributeLocation("colAttr", 1);
|
||||||
m_program->link();
|
m_program->link();
|
||||||
m_posAttr = m_program->attributeLocation("posAttr");
|
m_program->bind();
|
||||||
Q_ASSERT(m_posAttr != -1);
|
|
||||||
m_colAttr = m_program->attributeLocation("colAttr");
|
|
||||||
Q_ASSERT(m_colAttr != -1);
|
|
||||||
m_matrixUniform = m_program->uniformLocation("matrix");
|
m_matrixUniform = m_program->uniformLocation("matrix");
|
||||||
Q_ASSERT(m_matrixUniform != -1);
|
Q_ASSERT(m_matrixUniform != -1);
|
||||||
}
|
}
|
||||||
@ -100,28 +110,13 @@ void TriangleWindow::render()
|
|||||||
|
|
||||||
m_program->setUniformValue(m_matrixUniform, matrix);
|
m_program->setUniformValue(m_matrixUniform, matrix);
|
||||||
|
|
||||||
static const GLfloat vertices[] = {
|
glEnableVertexAttribArray(0);
|
||||||
0.0f, 0.707f,
|
glEnableVertexAttribArray(1);
|
||||||
-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);
|
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
glDisableVertexAttribArray(m_colAttr);
|
glDisableVertexAttribArray(0);
|
||||||
glDisableVertexAttribArray(m_posAttr);
|
glDisableVertexAttribArray(1);
|
||||||
|
|
||||||
m_program->release();
|
m_program->release();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user