-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshrenderer.cpp
188 lines (136 loc) · 5.98 KB
/
meshrenderer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "meshrenderer.h"
#include <iostream>
MeshRenderer::MeshRenderer()
{
meshIBOSize = 0;
}
MeshRenderer::~MeshRenderer() {
gl->glDeleteVertexArrays(1, &vao);
gl->glDeleteVertexArrays(1, &limitVao);
gl->glDeleteBuffers(1, &meshLimitCoordsBO);
gl->glDeleteBuffers(1, &meshLimitNormalsBO);
gl->glDeleteBuffers(1, &meshCoordsBO);
gl->glDeleteBuffers(1, &meshNormalsBO);
gl->glDeleteBuffers(1, &meshIndexBO);
}
void MeshRenderer::init(QOpenGLFunctions_4_1_Core* f, Settings* s) {
gl = f;
settings = s;
initShaders();
initBuffers();
}
void MeshRenderer::initShaders() {
shaderProg.create();
shaderProg.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vertshader.glsl");
shaderProg.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fragshader.glsl");
shaderProg.link();
uniModelViewMatrix = gl->glGetUniformLocation(shaderProg.programId(), "modelviewmatrix");
uniProjectionMatrix = gl->glGetUniformLocation(shaderProg.programId(), "projectionmatrix");
uniNormalMatrix = gl->glGetUniformLocation(shaderProg.programId(), "normalmatrix");
}
void MeshRenderer::initBuffers() {
// Regular drawing
gl->glGenVertexArrays(1, &vao);
gl->glBindVertexArray(vao);
gl->glGenBuffers(1, &meshCoordsBO);
gl->glBindBuffer(GL_ARRAY_BUFFER, meshCoordsBO);
gl->glEnableVertexAttribArray(0);
gl->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
gl->glGenBuffers(1, &meshNormalsBO);
gl->glBindBuffer(GL_ARRAY_BUFFER, meshNormalsBO);
gl->glEnableVertexAttribArray(1);
gl->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
gl->glGenBuffers(1, &meshIndexBO);
gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIndexBO);
gl->glBindVertexArray(0);
// Limit surface variant
gl->glGenVertexArrays(1, &limitVao);
gl->glBindVertexArray(limitVao);
gl->glGenBuffers(1, &meshLimitCoordsBO);
gl->glBindBuffer(GL_ARRAY_BUFFER, meshLimitCoordsBO);
gl->glEnableVertexAttribArray(0);
gl->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
gl->glGenBuffers(1, &meshLimitNormalsBO);
gl->glBindBuffer(GL_ARRAY_BUFFER, meshLimitNormalsBO);
gl->glEnableVertexAttribArray(1);
gl->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIndexBO);
gl->glBindVertexArray(0);
}
void MeshRenderer::updateBuffers(Mesh& currentMesh) {
qDebug() << ".. updateBuffers";
//gather attributes for current mesh
currentMesh.extractAttributes();
QVector<QVector3D>& vertexCoords = currentMesh.getVertexCoords();
QVector<QVector3D>& vertexNormals = currentMesh.getVertexNorms();
QVector<unsigned int>& polyIndices = currentMesh.getPolyIndices();
QVector<QVector3D>& vertexLimitCoords = currentMesh.getLimitCoords();
QVector<QVector3D>& vertexLimitNormals = currentMesh.getLimitNorms();
gl->glBindBuffer(GL_ARRAY_BUFFER, meshCoordsBO);
gl->glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*vertexCoords.size(), vertexCoords.data(), GL_DYNAMIC_DRAW);
qDebug() << " → Updated meshCoordsBO";
gl->glBindBuffer(GL_ARRAY_BUFFER, meshNormalsBO);
gl->glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*vertexNormals.size(), vertexNormals.data(), GL_DYNAMIC_DRAW);
qDebug() << " → Updated meshNormalsBO";
gl->glBindBuffer(GL_ARRAY_BUFFER, meshLimitCoordsBO);
gl->glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*vertexLimitCoords.size(), vertexLimitCoords.data(), GL_DYNAMIC_DRAW);
qDebug() << " → Updated meshLimitCoordsBO";
gl->glBindBuffer(GL_ARRAY_BUFFER, meshLimitNormalsBO);
gl->glBufferData(GL_ARRAY_BUFFER, sizeof(QVector3D)*vertexLimitNormals.size(), vertexLimitNormals.data(), GL_DYNAMIC_DRAW);
qDebug() << " → Updated meshLimitNormalsBO";
gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshIndexBO);
gl->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*polyIndices.size(), polyIndices.data(), GL_DYNAMIC_DRAW);
qDebug() << " → Updated meshIndexBO";
meshIBOSize = polyIndices.size();
}
void MeshRenderer::updateUniforms() {
shaderProg.bind();
gl->glUniformMatrix4fv(uniModelViewMatrix, 1, false, settings->modelViewMatrix.data());
gl->glUniformMatrix4fv(uniProjectionMatrix, 1, false, settings->projectionMatrix.data());
gl->glUniformMatrix3fv(uniNormalMatrix, 1, false, settings->normalMatrix.data());
shaderProg.release();
}
void MeshRenderer::draw(bool limit, bool wireFrame) {
GLuint drawVao;
if (limit) {
drawVao = limitVao;
} else {
drawVao = vao;
}
if (settings->uniformUpdateRequired) {
updateUniforms();
settings->uniformUpdateRequired = false;
}
shaderProg.bind();
//enable primitive restart
gl->glEnable(GL_PRIMITIVE_RESTART);
gl->glPrimitiveRestartIndex(INT_MAX);
bool wireFrameMode;
// Draw surface.
if (wireFrame) {
shaderProg.setUniformValue("materialColour", 0.0, 0.0, 1.0);
wireFrameMode = true;
} else {
shaderProg.setUniformValue("materialColour", 0.5, 0.5, 0.5);
wireFrameMode = settings->wireframeMode;
}
// Check wireframe mode.
if(wireFrameMode) {
shaderProg.setUniformValue("approxFlatShading", false);
shaderProg.setUniformValue("disableLighting", true);
gl->glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
gl->glBindVertexArray(drawVao);
gl->glDrawElements(GL_LINE_LOOP, meshIBOSize, GL_UNSIGNED_INT, 0);
gl->glBindVertexArray(0);
} else {
shaderProg.setUniformValue("disableLighting", false);
shaderProg.setUniformValue("approxFlatShading", settings->approxFlatShading);
gl->glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
gl->glBindVertexArray(drawVao);
gl->glDrawElements(GL_TRIANGLE_FAN, meshIBOSize, GL_UNSIGNED_INT, 0);
gl->glBindVertexArray(0);
}
shaderProg.release();
//disable it again as you might want to draw something else at some point
gl->glDisable(GL_PRIMITIVE_RESTART);
}