History

Cone render function, some cleanup of GeometryCache methods.

Andrzej Kapolka committed 12 years ago

Commit 3b56f611 parent 6b8476a4 files · +126 −18Browse files

Showing the changes to interface/src/renderer/GeometryCache.cpp (1 of 4 files). Show all

Unified · Split

interface/src/renderer/GeometryCache.cpp+115−18
@@ -110,15 +110,18 @@ void GeometryCache::renderHemisphere(int slices, int stacks) { · 109 unmodified lines
110 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);110 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
111}111}
112 112
113const int NUM_VERTICES_PER_TRIANGLE = 3;
114const int NUM_TRIANGLES_PER_QUAD = 2;
115const int NUM_VERTICES_PER_TRIANGULATED_QUAD = NUM_VERTICES_PER_TRIANGLE * NUM_TRIANGLES_PER_QUAD;
116const int NUM_COORDS_PER_VERTEX = 3;
117const int NUM_BYTES_PER_VERTEX = NUM_COORDS_PER_VERTEX * sizeof(GLfloat);
118const int NUM_BYTES_PER_INDEX = sizeof(GLushort);
113 119
114void GeometryCache::renderSphere(float radius, int slices, int stacks) {120void GeometryCache::renderSphere(float radius, int slices, int stacks) {
115 VerticesIndices& vbo = _sphereVBOs[IntPair(slices, stacks)];121 VerticesIndices& vbo = _sphereVBOs[IntPair(slices, stacks)];
116 int vertices = slices * (stacks - 1) + 2;122 int vertices = slices * (stacks - 1) + 2;
117 const int NUM_VERTICES_PER_TRIANGLE = 3;123 int indices = slices * stacks * NUM_VERTICES_PER_TRIANGULATED_QUAD;
118 const int NUM_TRIANGLES_PER_QUAD = 2;124 if (vbo.first == 0) {
119 int indices = slices * NUM_TRIANGLES_PER_QUAD * NUM_VERTICES_PER_TRIANGLE * (stacks - 1) + slices * NUM_TRIANGLES_PER_QUAD * NUM_VERTICES_PER_TRIANGLE;
120 if (vbo.first == 0) {
121 const int NUM_COORDS_PER_VERTEX = 3;
122 GLfloat* vertexData = new GLfloat[vertices * NUM_COORDS_PER_VERTEX];125 GLfloat* vertexData = new GLfloat[vertices * NUM_COORDS_PER_VERTEX];
123 GLfloat* vertex = vertexData;126 GLfloat* vertex = vertexData;
124 127
@@ -148,8 +151,7 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks) { · 23 unmodified lines
148 151
149 glGenBuffers(1, &vbo.first);152 glGenBuffers(1, &vbo.first);
150 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);153 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
151 const int BYTES_PER_VERTEX = NUM_COORDS_PER_VERTEX * sizeof(GLfloat);154 glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
152 glBufferData(GL_ARRAY_BUFFER, vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
153 delete[] vertexData;155 delete[] vertexData;
154 156
155 GLushort* indexData = new GLushort[indices];157 GLushort* indexData = new GLushort[indices];
@@ -192,8 +194,7 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks) { · 36 unmodified lines
192 194
193 glGenBuffers(1, &vbo.second);195 glGenBuffers(1, &vbo.second);
194 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);196 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
195 const int BYTES_PER_INDEX = sizeof(GLushort);197 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
196 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
197 delete[] indexData;198 delete[] indexData;
198 199
199 } else {200 } else {
@@ -239,8 +240,7 @@ void GeometryCache::renderSquare(int xDivisions, int yDivisions) { · 39 unmodified lines
239 240
240 glGenBuffers(1, &vbo.first);241 glGenBuffers(1, &vbo.first);
241 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);242 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
242 const int BYTES_PER_VERTEX = 3 * sizeof(GLfloat);243 glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
243 glBufferData(GL_ARRAY_BUFFER, vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
244 delete[] vertexData;244 delete[] vertexData;
245 245
246 GLushort* indexData = new GLushort[indices];246 GLushort* indexData = new GLushort[indices];
@@ -263,8 +263,7 @@ void GeometryCache::renderSquare(int xDivisions, int yDivisions) { · 16 unmodified lines
263 263
264 glGenBuffers(1, &vbo.second);264 glGenBuffers(1, &vbo.second);
265 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);265 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
266 const int BYTES_PER_INDEX = sizeof(GLushort);266 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
267 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
268 delete[] indexData;267 delete[] indexData;
269 268
270 } else {269 } else {
@@ -313,8 +312,7 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 42 unmodified lines
313 312
314 glGenBuffers(1, &vbo.first);313 glGenBuffers(1, &vbo.first);
315 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);314 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
316 const int BYTES_PER_VERTEX = 3 * sizeof(GLfloat);315 glBufferData(GL_ARRAY_BUFFER, 2 * vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
317 glBufferData(GL_ARRAY_BUFFER, 2 * vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
318 delete[] vertexData;316 delete[] vertexData;
319 317
320 GLushort* indexData = new GLushort[indices];318 GLushort* indexData = new GLushort[indices];
@@ -337,8 +335,7 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 16 unmodified lines
337 335
338 glGenBuffers(1, &vbo.second);336 glGenBuffers(1, &vbo.second);
339 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);337 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
340 const int BYTES_PER_INDEX = sizeof(GLushort);338 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
341 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
342 delete[] indexData;339 delete[] indexData;
343 340
344 } else {341 } else {
@@ -360,6 +357,106 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 15 unmodified lines
360 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);357 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
361}358}
362 359
360void GeometryCache::renderCone(float base, float height, int slices, int stacks) {
361 VerticesIndices& vbo = _halfCylinderVBOs[IntPair(slices, stacks)];
362 int vertices = (stacks + 2) * slices;
363 int baseTriangles = slices - 2;
364 int indices = NUM_VERTICES_PER_TRIANGULATED_QUAD * slices * stacks + NUM_VERTICES_PER_TRIANGLE * baseTriangles;
365 if (vbo.first == 0) {
366 GLfloat* vertexData = new GLfloat[vertices * NUM_COORDS_PER_VERTEX * 2];
367 GLfloat* vertex = vertexData;
368 // cap
369 for (int i = 0; i < slices; i++) {
370 float theta = TWO_PI * i / slices;
371
372 //normals
373 *(vertex++) = 0.0f;
374 *(vertex++) = 0.0f;
375 *(vertex++) = -1.0f;
376
377 // vertices
378 *(vertex++) = cosf(theta);
379 *(vertex++) = sinf(theta);
380 *(vertex++) = 0.0f;
381 }
382 // body
383 for (int i = 0; i <= stacks; i++) {
384 float z = (float)i / stacks;
385 float radius = 1.0f - z;
386
387 for (int j = 0; j < slices; j++) {
388 float theta = TWO_PI * j / slices;
389
390 //normals
391 *(vertex++) = cosf(theta) / SQUARE_ROOT_OF_2;
392 *(vertex++) = sinf(theta) / SQUARE_ROOT_OF_2;
393 *(vertex++) = 1.0f / SQUARE_ROOT_OF_2;
394
395 // vertices
396 *(vertex++) = radius * cosf(theta);
397 *(vertex++) = radius * sinf(theta);
398 *(vertex++) = z;
399 }
400 }
401
402 glGenBuffers(1, &vbo.first);
403 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
404 glBufferData(GL_ARRAY_BUFFER, 2 * vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
405 delete[] vertexData;
406
407 GLushort* indexData = new GLushort[indices];
408 GLushort* index = indexData;
409 for (int i = 0; i < baseTriangles; i++) {
410 *(index++) = 0;
411 *(index++) = i + 1;
412 *(index++) = i + 2;
413 }
414 for (int i = 1; i <= stacks; i++) {
415 GLushort bottom = i * slices;
416 GLushort top = bottom + slices;
417 for (int j = 0; j < slices; j++) {
418 int next = (j + 1) % slices;
419
420 *(index++) = bottom + j;
421 *(index++) = top + next;
422 *(index++) = top + j;
423
424 *(index++) = bottom + j;
425 *(index++) = bottom + next;
426 *(index++) = top + next;
427 }
428 }
429
430 glGenBuffers(1, &vbo.second);
431 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
432 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
433 delete[] indexData;
434
435 } else {
436 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
437 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
438 }
439 glEnableClientState(GL_VERTEX_ARRAY);
440 glEnableClientState(GL_NORMAL_ARRAY);
441
442 int stride = NUM_VERTICES_PER_TRIANGULATED_QUAD * sizeof(float);
443 glNormalPointer(GL_FLOAT, stride, 0);
444 glVertexPointer(NUM_COORDS_PER_VERTEX, GL_FLOAT, stride, (const void *)(NUM_COORDS_PER_VERTEX * sizeof(float)));
445
446 glPushMatrix();
447 glScalef(base, base, height);
448
449 glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0);
450
451 glPopMatrix();
452
453 glDisableClientState(GL_VERTEX_ARRAY);
454 glDisableClientState(GL_NORMAL_ARRAY);
455
456 glBindBuffer(GL_ARRAY_BUFFER, 0);
457 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
458}
459
363void GeometryCache::renderGrid(int xDivisions, int yDivisions) {460void GeometryCache::renderGrid(int xDivisions, int yDivisions) {
364 QOpenGLBuffer& buffer = _gridBuffers[IntPair(xDivisions, yDivisions)];461 QOpenGLBuffer& buffer = _gridBuffers[IntPair(xDivisions, yDivisions)];
365 int vertices = (xDivisions + 1 + yDivisions + 1) * 2;462 int vertices = (xDivisions + 1 + yDivisions + 1) * 2;