Showing the changes to interface/src/renderer/GeometryCache.cpp (1 of 4 files). Show all
Unified · Split
| @@ -110,15 +110,18 @@ void GeometryCache::renderHemisphere(int slices, int stacks) { · 109 unmodified lines | ||
| 110 | 110 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 111 | 111 | } |
| 112 | 112 | |
| 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); | |
| 113 | 119 | |
| 114 | 120 | void GeometryCache::renderSphere(float radius, int slices, int stacks) { |
| 115 | 121 | 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) { | |
| 122 | 125 | GLfloat* vertexData = new GLfloat[vertices * NUM_COORDS_PER_VERTEX]; |
| 123 | 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 | 152 | glGenBuffers(1, &vbo.first); |
| 150 | 153 | 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); | |
| 153 | 155 | delete[] vertexData; |
| 154 | 156 | |
| 155 | 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 | 195 | glGenBuffers(1, &vbo.second); |
| 194 | 196 | 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); | |
| 197 | 198 | delete[] indexData; |
| 198 | 199 | |
| 199 | 200 | } else { |
| @@ -239,8 +240,7 @@ void GeometryCache::renderSquare(int xDivisions, int yDivisions) { · 39 unmodified lines | ||
| 239 | 240 | |
| 240 | 241 | glGenBuffers(1, &vbo.first); |
| 241 | 242 | 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); | |
| 244 | 244 | delete[] vertexData; |
| 245 | 245 | |
| 246 | 246 | GLushort* indexData = new GLushort[indices]; |
| @@ -263,8 +263,7 @@ void GeometryCache::renderSquare(int xDivisions, int yDivisions) { · 16 unmodified lines | ||
| 263 | 263 | |
| 264 | 264 | glGenBuffers(1, &vbo.second); |
| 265 | 265 | 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); | |
| 268 | 267 | delete[] indexData; |
| 269 | 268 | |
| 270 | 269 | } else { |
| @@ -313,8 +312,7 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 42 unmodified lines | ||
| 313 | 312 | |
| 314 | 313 | glGenBuffers(1, &vbo.first); |
| 315 | 314 | 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); | |
| 318 | 316 | delete[] vertexData; |
| 319 | 317 | |
| 320 | 318 | GLushort* indexData = new GLushort[indices]; |
| @@ -337,8 +335,7 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 16 unmodified lines | ||
| 337 | 335 | |
| 338 | 336 | glGenBuffers(1, &vbo.second); |
| 339 | 337 | 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); | |
| 342 | 339 | delete[] indexData; |
| 343 | 340 | |
| 344 | 341 | } else { |
| @@ -360,6 +357,106 @@ void GeometryCache::renderHalfCylinder(int slices, int stacks) { · 15 unmodified lines | ||
| 360 | 357 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 361 | 358 | } |
| 362 | 359 | |
| 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 | + | |
| 363 | 460 | void GeometryCache::renderGrid(int xDivisions, int yDivisions) { |
| 364 | 461 | QOpenGLBuffer& buffer = _gridBuffers[IntPair(xDivisions, yDivisions)]; |
| 365 | 462 | int vertices = (xDivisions + 1 + yDivisions + 1) * 2; |