This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
basicusagescene.cpp
94 lines (77 loc) · 2.19 KB
/
basicusagescene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "basicusagescene.h"
#include "glassert.h"
#include <QObject>
#include <QOpenGLContext>
BasicUsageScene::BasicUsageScene()
: mShaderProgram(),
mVertexPositionBuffer(QOpenGLBuffer::VertexBuffer),
mVertexColorBuffer(QOpenGLBuffer::VertexBuffer)
{
}
void BasicUsageScene::initialize()
{
prepareShaderProgram();
prepareVertexBuffers();
}
void BasicUsageScene::update(float t)
{
Q_UNUSED(t);
}
void BasicUsageScene::render()
{
glAssert( glClear(GL_COLOR_BUFFER_BIT) );
mShaderProgram.bind();
mVAO.bind();
glAssert( glDrawArrays(GL_TRIANGLES, 0, 3) );
}
void BasicUsageScene::resize(int width, int height)
{
glAssert( glViewport(0, 0, width, height) );
}
void BasicUsageScene::prepareShaderProgram()
{
if (!mShaderProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":shaders/phong.vert"))
{
qCritical() << "error";
}
if (!mShaderProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":shaders/phong.frag"))
{
qCritical() << "error";
}
if (!mShaderProgram.link())
{
qCritical() << "error";
}
glCheckError();
}
void BasicUsageScene::prepareVertexBuffers()
{
float positionData[] = {
-0.8f, -0.8f, 0.0f,
0.8f, -0.8f, 0.0f,
0.0f, 0.8f, 0.0f
};
float colorData[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
mVAO.create();
mVAO.bind();
mVertexPositionBuffer.create();
mVertexPositionBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
mVertexPositionBuffer.bind();
mVertexPositionBuffer.allocate(positionData, 3 * 3 * sizeof(float));
mVertexColorBuffer.create();
mVertexColorBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
mVertexColorBuffer.bind();
mVertexColorBuffer.allocate(colorData, 3 * 3 * sizeof(float));
mShaderProgram.bind();
mVertexPositionBuffer.bind();
mShaderProgram.enableAttributeArray("vertexPosition");
mShaderProgram.setAttributeBuffer("vertexPosition", GL_FLOAT, 0, 3);
mVertexColorBuffer.bind();
mShaderProgram.enableAttributeArray("vertexColor");
mShaderProgram.setAttributeBuffer("vertexColor", GL_FLOAT, 0, 3);
glCheckError();
}