-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube.C
304 lines (260 loc) · 10.1 KB
/
Cube.C
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <GL/glew.h>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <time.h>
#include <cmath>
#include <cstring>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_access.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtx/transform2.hpp"
#include "config.h"
#include "Cube.h"
#include "Shader.h"
using namespace std;
Cube::Cube()
: _transformFeedback(0),
_srcXfbBuff(0),
_destXfbBuff(1),
_particleGenVao(0),
_indicesVbo(0),
_vertsVbo(0),
_vertQid(0),
_vertCount(0),
_genProg(),
_surfaceProg(),
_time(0.0f),
_modelViewMatrix(1.0f),
_projectionMatrix(1.0f)
{
// Initialize OpenGL state
glClearColor(0.0, 0.0, 0.0, 1.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
// Generate Cube geometry and store in _indices, _position3.
static const float position[] =
{ -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f
};
static const unsigned index[] =
{ 0, 1, 2,
0, 2, 3,
1, 5, 6,
1, 6, 2,
5, 4, 7,
5, 7, 6,
4, 0, 3,
4, 3, 7,
3, 2, 6,
3, 6, 7,
1, 0, 4,
1, 4, 5
};
_verts3.assign(position, position + sizeof(position) / sizeof(position[0]));
_indices.assign(index, index + sizeof(index) / sizeof(index[0]));
// Load basic shader.
_basicProg.compile("Basic.Vertex", NULL, "Basic.Fragment");
_basicProg.link();
// Load particle generation shader.
const GLchar *particleVaryings[] = {"gWorldPos", "gVelocity", "gAge"};
_genProg.compile("Particle.Vertex", "Particle.Geometry", "Particle.Fragment");
glTransformFeedbackVaryings(_genProg, 3, particleVaryings, GL_SEPARATE_ATTRIBS);
_genProg.link();
// Load particle velocity/decay/rendering shader.
const GLchar *surfaceVaryings[] = {"gWorldPos", "gVelocity", "gAge"};
_surfaceProg.compile("Surface.Vertex", "Surface.Geometry", "Surface.Fragment");
glTransformFeedbackVaryings(_surfaceProg, 3, surfaceVaryings, GL_SEPARATE_ATTRIBS);
_surfaceProg.link();
// Create transform feedback objects.
glGenTransformFeedbacks(1, &_transformFeedback);
// Bind a vertex array object.
// A VAO holds the state of all VBOs associated with it.
glGenVertexArrays(1, &_particleGenVao);
glBindVertexArray(_particleGenVao);
// Create a VBO to hold per-vertex position data. Point the
// "position" varying to this VBO. (Note that _vao is still bound,
// so this VBO will be associated with the enabled VAO)
GLuint posIdx = glGetAttribLocation(_genProg, "position");
glGenBuffers(1, &_vertsVbo);
glBindBuffer(GL_ARRAY_BUFFER, _vertsVbo);
glBufferData(GL_ARRAY_BUFFER, _verts3.size() * sizeof(GLfloat), &_verts3[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(posIdx);
glVertexAttribPointer(posIdx, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Create a VBO to hold vertex index data. This VBO isn't attached
// to a varying, since it simply provides an index into the other
// VBOs at rendering time. See glDrawElements(). (Note that _vao
// is still bound, so this VBO will be associated with the enabled
// VAO)
glGenBuffers(1, &_indicesVbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indicesVbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indices.size() * sizeof(GLuint), &_indices[0], GL_STATIC_DRAW);
// Now that we're done uploading data to the OpenGL server, unbind
// the VAO and VBO.
glBindVertexArray(0);
// Create vertex array and buffers for particle aging shader.
glGenVertexArrays(2, _surfaceVao);
glGenBuffers(2, _worldPosBo);
glGenBuffers(2, _velocityBo);
glGenBuffers(2, _ageBo);
for (int i = 0; i < 2; ++i) {
GLuint posIdx = glGetAttribLocation(_surfaceProg, "worldPos");
GLuint velIdx = glGetAttribLocation(_surfaceProg, "velocity");
GLuint ageIdx = glGetAttribLocation(_surfaceProg, "age");
glBindVertexArray(_surfaceVao[i]);
glEnableVertexAttribArray(posIdx);
glBindBuffer(GL_ARRAY_BUFFER, _worldPosBo[i]);
glBufferData(GL_ARRAY_BUFFER, 1000000 * 4 * sizeof(GLfloat), 0, GL_STREAM_COPY);
glVertexAttribPointer(posIdx, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(velIdx);
glBindBuffer(GL_ARRAY_BUFFER, _velocityBo[i]);
glBufferData(GL_ARRAY_BUFFER, 1000000 * 3 * sizeof(GLfloat), 0, GL_STREAM_COPY);
glVertexAttribPointer(velIdx, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(ageIdx);
glBindBuffer(GL_ARRAY_BUFFER, _ageBo[i]);
glBufferData(GL_ARRAY_BUFFER, 1000000 * 1 * sizeof(GLfloat), 0, GL_STREAM_COPY);
glVertexAttribPointer(ageIdx, 1, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
}
// Generate query object IDs.
glGenQueries(1, &_vertQid);
// Populate the default modelview and projection matrices.
_modelViewMatrix = glm::translate(glm::mat4(1.0), glm::vec3(0.0, 0.0, -40.0));
_projectionMatrix = glm::mat4(1.0f) * glm::perspective
(25.0f, (float)RENDER_WIDTH /(float)RENDER_HEIGHT, 1.0f, 60.0f);
return;
}
Cube::~Cube()
{
// Delete all buffers and vao.
glDeleteBuffers(1, &_vertsVbo);
glDeleteVertexArrays(1, &_particleGenVao);
// TODO delete transform feedback buffers and vao.
//
// Delete transform feedback object.
glDeleteTransformFeedbacks(1, &_transformFeedback);
return;
}
void Cube::render(float secondsElapsed)
{
// Setup projection * modelview matrix.
float degrees = secondsElapsed * 50.0f;
_modelViewMatrix = glm::rotate(_modelViewMatrix, degrees, glm::vec3(0.3f, 1.0f, 0.1f));
glm::mat4 translatedMatrix = glm::translate(_modelViewMatrix, glm::vec3(3.0f, 1.0f, 0.0f));
glm::mat4 mvpMatrix = _projectionMatrix * translatedMatrix;
// Draw the wireframe outline of the cube.
GLuint mvpIdx = glGetUniformLocation(_basicProg, "MVPMatrix");
GLuint colIdx = glGetUniformLocation(_basicProg, "Color");
glUseProgram(_basicProg);
glUniformMatrix4fv(mvpIdx, 1, false, &mvpMatrix[0][0]);
glUniform4f(colIdx, 1.0, 1.0, 1.0, 1.0);
glBindVertexArray(_particleGenVao);
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
// Generate new points for this frame.
GLuint mv2Idx = glGetUniformLocation(_genProg, "MVPMatrix");
GLuint timIdx = glGetUniformLocation(_genProg, "Time");
GLuint elaIdx = glGetUniformLocation(_genProg, "ElapsedSec");
GLuint brtIdx = glGetUniformLocation(_genProg, "BirthFrequency");
glUseProgram(_genProg);
glUniformMatrix4fv(mv2Idx, 1, false, &mvpMatrix[0][0]);
glUniform1f(timIdx, _time);
glUniform1f(elaIdx, secondsElapsed);
glUniform1f(brtIdx, 1000.0f);
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, _vertQid);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, _transformFeedback);
glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0,
_worldPosBo[_srcXfbBuff],
4*_vertCount*sizeof(GLfloat),
4*_vertCount*sizeof(GLfloat) + 80*4*sizeof(GLfloat));
glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 1,
_velocityBo[_srcXfbBuff],
3*_vertCount*sizeof(GLfloat),
3*_vertCount*sizeof(GLfloat) + 80*3*sizeof(GLfloat));
glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 2,
_ageBo[_srcXfbBuff],
1*_vertCount*sizeof(GLfloat),
1*_vertCount*sizeof(GLfloat) + 80*1*sizeof(GLfloat));
glBeginTransformFeedback(GL_POINTS);
glEnable(GL_RASTERIZER_DISCARD);
glBindVertexArray(_particleGenVao);
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
glEndTransformFeedback();
glDisable(GL_RASTERIZER_DISCARD);
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
GLint pointsGenerated = -1;
glGetQueryObjectiv(_vertQid, GL_QUERY_RESULT, &pointsGenerated);
_vertCount += pointsGenerated;
// Print the number of verts collected.
cout << "Vert Count: " << _vertCount << endl;
// Draw the surface with associated poplights.
GLuint el2Idx = glGetUniformLocation(_surfaceProg, "ElapsedSec");
GLuint accIdx = glGetUniformLocation(_surfaceProg, "WorldAccel");
GLuint mxaIdx = glGetUniformLocation(_surfaceProg, "MaxAgeSec");
glUseProgram(_surfaceProg);
glUniform1f(el2Idx, secondsElapsed);
glUniform3f(accIdx, 0.0, -9.8, 0.0);
glUniform1f(mxaIdx, 4.0f);
glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, _vertQid);
glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, _transformFeedback);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, _worldPosBo[_destXfbBuff]);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 1, _velocityBo[_destXfbBuff]);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 2, _ageBo[_destXfbBuff]);
glBeginTransformFeedback(GL_POINTS);
glDisable(GL_DEPTH_TEST);
glBindVertexArray(_surfaceVao[_srcXfbBuff]);
glDrawArrays(GL_POINTS, 0, _vertCount);
glBindVertexArray(0);
glEnable(GL_DEPTH_TEST);
glEndTransformFeedback();
glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
GLint pointsProcessed = -1;
glGetQueryObjectiv(_vertQid, GL_QUERY_RESULT, &pointsProcessed);
_vertCount = pointsProcessed;
cout << "Vert Count: " << _vertCount << endl;
// Swap source and destination buffers.
_srcXfbBuff = 1 - _srcXfbBuff;
_destXfbBuff = 1 - _destXfbBuff;
checkGLError();
// Accumulate time.
_time += secondsElapsed;
}
void Cube::checkGLError() {
GLenum error = GL_NO_ERROR;
do {
error = glGetError();
switch (error) {
case GL_NO_ERROR:
break;
case GL_INVALID_ENUM:
cerr << "GLERROR: INVALID_ENUM" << endl;
break;
case GL_INVALID_VALUE:
cerr << "GLERROR: INVALID_VALUE" << endl;
break;
case GL_INVALID_OPERATION:
cerr << "GLERROR: INVALID_OPERATION" << endl;
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
cerr << "GLERROR: INVALID_FRAMEBUFFER_OPERATION" << endl;
break;
case GL_OUT_OF_MEMORY:
cerr << "GLERROR: OUT_OF_MEMORY" << endl;
break;
default:
cerr << "GLERROR: UNKNOWN ERROR" << endl;
}
} while (error != GL_NO_ERROR);
return;
}