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
110110 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
111111 }
112112
113+const int NUM_VERTICES_PER_TRIANGLE = 3;
114+const int NUM_TRIANGLES_PER_QUAD = 2;
115+const int NUM_VERTICES_PER_TRIANGULATED_QUAD = NUM_VERTICES_PER_TRIANGLE * NUM_TRIANGLES_PER_QUAD;
116+const int NUM_COORDS_PER_VERTEX = 3;
117+const int NUM_BYTES_PER_VERTEX = NUM_COORDS_PER_VERTEX * sizeof(GLfloat);
118+const int NUM_BYTES_PER_INDEX = sizeof(GLushort);
113119
114120 void GeometryCache::renderSphere(float radius, int slices, int stacks) {
115121 VerticesIndices& vbo = _sphereVBOs[IntPair(slices, stacks)];
116− int vertices = slices * (stacks - 1) + 2;
117− const int NUM_VERTICES_PER_TRIANGLE = 3;
118− const int NUM_TRIANGLES_PER_QUAD = 2;
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+ int vertices = slices * (stacks - 1) + 2;
123+ int indices = slices * stacks * NUM_VERTICES_PER_TRIANGULATED_QUAD;
124+ if (vbo.first == 0) {
122125 GLfloat* vertexData = new GLfloat[vertices * NUM_COORDS_PER_VERTEX];
123126 GLfloat* vertex = vertexData;
124127
@@ -148,8 +151,7 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks) { · 23 unmodified lines
148151
149152 glGenBuffers(1, &vbo.first);
150153 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
151− const int BYTES_PER_VERTEX = NUM_COORDS_PER_VERTEX * sizeof(GLfloat);
152− glBufferData(GL_ARRAY_BUFFER, vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
154+ glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
153155 delete[] vertexData;
154156
155157 GLushort* indexData = new GLushort[indices];
@@ -192,8 +194,7 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks) { · 36 unmodified lines
192194
193195 glGenBuffers(1, &vbo.second);
194196 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
195− const int BYTES_PER_INDEX = sizeof(GLushort);
196− glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
197+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
197198 delete[] indexData;
198199
199200 } else {
@@ -239,8 +240,7 @@ void GeometryCache::renderSquare(int xDivisions, int yDivisions) { · 39 unmodified lines
239240
240241 glGenBuffers(1, &vbo.first);
241242 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
242− const int BYTES_PER_VERTEX = 3 * sizeof(GLfloat);
243− glBufferData(GL_ARRAY_BUFFER, vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
243+ glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
244244 delete[] vertexData;
245245
246246 GLushort* indexData = new GLushort[indices];
@@ -263,8 +263,7 @@ void GeometryCache::renderSquare(int xDivisions, int yDivisions) { · 16 unmodified lines
263263
264264 glGenBuffers(1, &vbo.second);
265265 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
266− const int BYTES_PER_INDEX = sizeof(GLushort);
267− glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
266+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
268267 delete[] indexData;
269268
270269 } else {
@@ -313,8 +312,7 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 42 unmodified lines
313312
314313 glGenBuffers(1, &vbo.first);
315314 glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
316− const int BYTES_PER_VERTEX = 3 * sizeof(GLfloat);
317− glBufferData(GL_ARRAY_BUFFER, 2 * vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
315+ glBufferData(GL_ARRAY_BUFFER, 2 * vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW);
318316 delete[] vertexData;
319317
320318 GLushort* indexData = new GLushort[indices];
@@ -337,8 +335,7 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 16 unmodified lines
337335
338336 glGenBuffers(1, &vbo.second);
339337 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
340− const int BYTES_PER_INDEX = sizeof(GLushort);
341− glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
338+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW);
342339 delete[] indexData;
343340
344341 } else {
@@ -360,6 +357,106 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 15 unmodified lines
360357 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
361358 }
362359
360+void 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+
363460 void GeometryCache::renderGrid(int xDivisions, int yDivisions) {
364461 QOpenGLBuffer& buffer = _gridBuffers[IntPair(xDivisions, yDivisions)];
365462 int vertices = (xDivisions + 1 + yDivisions + 1) * 2;