Showing the changes to interface/src/renderer/GeometryCache.cpp (1 of 5 files). Show all
Unified · Split
| @@ -0,0 +1,101 @@ | |||
| 1 | // | ||
| 2 | // GeometryCache.cpp | ||
| 3 | // interface | ||
| 4 | // | ||
| 5 | // Created by Andrzej Kapolka on 6/21/13. | ||
| 6 | // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. | ||
| 7 | |||
| 8 | #include <cmath> | ||
| 9 | |||
| 10 | #include "GeometryCache.h" | ||
| 11 | #include "world.h" | ||
| 12 | |||
| 13 | GeometryCache::~GeometryCache() { | ||
| 14 | foreach (const VerticesIndices& vbo, _hemisphereVBOs) { | ||
| 15 | glDeleteBuffers(1, &vbo.first); | ||
| 16 | glDeleteBuffers(1, &vbo.second); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | void GeometryCache::renderHemisphere(int slices, int stacks) { | ||
| 21 | VerticesIndices& vbo = _hemisphereVBOs[SlicesStacks(slices, stacks)]; | ||
| 22 | int vertices = slices * (stacks - 1) + 1; | ||
| 23 | int indices = slices * 2 * 3 * (stacks - 2) + slices * 3; | ||
| 24 | if (vbo.first == 0) { | ||
| 25 | GLfloat* vertexData = new GLfloat[vertices * 3]; | ||
| 26 | GLfloat* vertex = vertexData; | ||
| 27 | for (int i = 0; i < stacks - 1; i++) { | ||
| 28 | float phi = PIf * 0.5f * i / (stacks - 1), z, radius; | ||
| 29 | sincosf(phi, &z, &radius); | ||
| 30 | |||
| 31 | for (int j = 0; j < slices; j++) { | ||
| 32 | float theta = PIf * 2.0f * j / slices; | ||
| 33 | float x, y; | ||
| 34 | sincosf(theta, &x, &y); | ||
| 35 | x *= radius; | ||
| 36 | y *= radius; | ||
| 37 | |||
| 38 | *(vertex++) = x; | ||
| 39 | *(vertex++) = y; | ||
| 40 | *(vertex++) = z; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | *(vertex++) = 0.0f; | ||
| 44 | *(vertex++) = 0.0f; | ||
| 45 | *(vertex++) = 1.0f; | ||
| 46 | |||
| 47 | glGenBuffers(1, &vbo.first); | ||
| 48 | glBindBuffer(GL_ARRAY_BUFFER, vbo.first); | ||
| 49 | const int BYTES_PER_VERTEX = 3 * sizeof(GLfloat); | ||
| 50 | glBufferData(GL_ARRAY_BUFFER, vertices * BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); | ||
| 51 | delete[] vertexData; | ||
| 52 | |||
| 53 | GLushort* indexData = new GLushort[indices]; | ||
| 54 | GLushort* index = indexData; | ||
| 55 | for (int i = 0; i < stacks - 2; i++) { | ||
| 56 | GLushort bottom = i * slices; | ||
| 57 | GLushort top = bottom + slices; | ||
| 58 | for (int j = 0; j < slices; j++) { | ||
| 59 | int next = (j + 1) % slices; | ||
| 60 | |||
| 61 | *(index++) = bottom + j; | ||
| 62 | *(index++) = top + next; | ||
| 63 | *(index++) = top + j; | ||
| 64 | |||
| 65 | *(index++) = bottom + j; | ||
| 66 | *(index++) = bottom + next; | ||
| 67 | *(index++) = top + next; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | GLushort bottom = (stacks - 2) * slices; | ||
| 71 | GLushort top = bottom + slices; | ||
| 72 | for (int i = 0; i < slices; i++) { | ||
| 73 | *(index++) = bottom + i; | ||
| 74 | *(index++) = bottom + (i + 1) % slices; | ||
| 75 | *(index++) = top; | ||
| 76 | } | ||
| 77 | |||
| 78 | glGenBuffers(1, &vbo.second); | ||
| 79 | glBindBuffer(GL_ARRAY_BUFFER, vbo.second); | ||
| 80 | const int BYTES_PER_INDEX = sizeof(GLushort); | ||
| 81 | glBufferData(GL_ARRAY_BUFFER, indices * BYTES_PER_INDEX, indexData, GL_STATIC_DRAW); | ||
| 82 | delete[] indexData; | ||
| 83 | |||
| 84 | } else { | ||
| 85 | glBindBuffer(GL_ARRAY_BUFFER, vbo.first); | ||
| 86 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); | ||
| 87 | } | ||
| 88 | glEnableClientState(GL_VERTEX_ARRAY); | ||
| 89 | glEnableClientState(GL_NORMAL_ARRAY); | ||
| 90 | |||
| 91 | glVertexPointer(3, GL_FLOAT, 0, 0); | ||
| 92 | glNormalPointer(GL_FLOAT, 0, 0); | ||
| 93 | |||
| 94 | glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); | ||
| 95 | |||
| 96 | glDisableClientState(GL_VERTEX_ARRAY); | ||
| 97 | glDisableClientState(GL_NORMAL_ARRAY); | ||
| 98 | |||
| 99 | glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
| 100 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | ||
| 101 | } | ||