Showing the changes to libraries/render-utils/src/GeometryCache.cpp (1 of 107 files). Show all
Unified · Split
| @@ -119,7 +119,7 @@ const int NUM_COORDS_PER_VERTEX = 3; · 118 unmodified lines | ||
| 119 | 119 | const int NUM_BYTES_PER_VERTEX = NUM_COORDS_PER_VERTEX * sizeof(GLfloat); |
| 120 | 120 | const int NUM_BYTES_PER_INDEX = sizeof(GLushort); |
| 121 | 121 | |
| 122 | −void GeometryCache::renderSphere(float radius, int slices, int stacks) { | |
| 122 | +void GeometryCache::renderSphere(float radius, int slices, int stacks, bool solid) { | |
| 123 | 123 | VerticesIndices& vbo = _sphereVBOs[IntPair(slices, stacks)]; |
| 124 | 124 | int vertices = slices * (stacks - 1) + 2; |
| 125 | 125 | int indices = slices * stacks * NUM_VERTICES_PER_TRIANGULATED_QUAD; |
| @@ -211,7 +211,11 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks) { · 85 unmodified lines | ||
| 211 | 211 | |
| 212 | 212 | glPushMatrix(); |
| 213 | 213 | glScalef(radius, radius, radius); |
| 214 | − glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); | |
| 214 | + if (solid) { | |
| 215 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); | |
| 216 | + } else { | |
| 217 | + glDrawRangeElementsEXT(GL_LINES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); | |
| 218 | + } | |
| 215 | 219 | glPopMatrix(); |
| 216 | 220 | |
| 217 | 221 | glDisableClientState(GL_VERTEX_ARRAY); |
| @@ -503,6 +507,161 @@ void GeometryCache::renderGrid(int xDivisions, int yDivisions) { · 285 unmodified lines | ||
| 503 | 507 | buffer.release(); |
| 504 | 508 | } |
| 505 | 509 | |
| 510 | +void GeometryCache::renderSolidCube(float size) { | |
| 511 | + VerticesIndices& vbo = _solidCubeVBOs[size]; | |
| 512 | + const int FLOATS_PER_VERTEX = 3; | |
| 513 | + const int VERTICES_PER_FACE = 4; | |
| 514 | + const int NUMBER_OF_FACES = 6; | |
| 515 | + const int TRIANGLES_PER_FACE = 2; | |
| 516 | + const int VERTICES_PER_TRIANGLE = 3; | |
| 517 | + const int vertices = NUMBER_OF_FACES * VERTICES_PER_FACE * FLOATS_PER_VERTEX; | |
| 518 | + const int indices = NUMBER_OF_FACES * TRIANGLES_PER_FACE * VERTICES_PER_TRIANGLE; | |
| 519 | + const int vertexPoints = vertices * FLOATS_PER_VERTEX; | |
| 520 | + if (vbo.first == 0) { | |
| 521 | + GLfloat* vertexData = new GLfloat[vertexPoints * 2]; // vertices and normals | |
| 522 | + GLfloat* vertex = vertexData; | |
| 523 | + float halfSize = size / 2.0f; | |
| 524 | + | |
| 525 | + static GLfloat cannonicalVertices[vertexPoints] = | |
| 526 | + { 1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // v0,v1,v2,v3 (front) | |
| 527 | + 1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // v0,v3,v4,v5 (right) | |
| 528 | + 1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // v0,v5,v6,v1 (top) | |
| 529 | + -1, 1, 1, -1, 1,-1, -1,-1,-1, -1,-1, 1, // v1,v6,v7,v2 (left) | |
| 530 | + -1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1, // v7,v4,v3,v2 (bottom) | |
| 531 | + 1,-1,-1, -1,-1,-1, -1, 1,-1, 1, 1,-1 }; // v4,v7,v6,v5 (back) | |
| 532 | + | |
| 533 | + // normal array | |
| 534 | + static GLfloat cannonicalNormals[vertexPoints] = | |
| 535 | + { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0,v1,v2,v3 (front) | |
| 536 | + 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0,v3,v4,v5 (right) | |
| 537 | + 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0,v5,v6,v1 (top) | |
| 538 | + -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1,v6,v7,v2 (left) | |
| 539 | + 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7,v4,v3,v2 (bottom) | |
| 540 | + 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 }; // v4,v7,v6,v5 (back) | |
| 541 | + | |
| 542 | + // index array of vertex array for glDrawElements() & glDrawRangeElement() | |
| 543 | + static GLubyte cannonicalIndices[indices] = | |
| 544 | + { 0, 1, 2, 2, 3, 0, // front | |
| 545 | + 4, 5, 6, 6, 7, 4, // right | |
| 546 | + 8, 9,10, 10,11, 8, // top | |
| 547 | + 12,13,14, 14,15,12, // left | |
| 548 | + 16,17,18, 18,19,16, // bottom | |
| 549 | + 20,21,22, 22,23,20 }; // back | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + GLfloat* cannonicalVertex = &cannonicalVertices[0]; | |
| 554 | + GLfloat* cannonicalNormal = &cannonicalNormals[0]; | |
| 555 | + | |
| 556 | + for (int i = 0; i < vertices; i++) { | |
| 557 | + //normals | |
| 558 | + *(vertex++) = *cannonicalNormal++; | |
| 559 | + *(vertex++) = *cannonicalNormal++; | |
| 560 | + *(vertex++) = *cannonicalNormal++; | |
| 561 | + | |
| 562 | + // vertices | |
| 563 | + *(vertex++) = halfSize * *cannonicalVertex++; | |
| 564 | + *(vertex++) = halfSize * *cannonicalVertex++; | |
| 565 | + *(vertex++) = halfSize * *cannonicalVertex++; | |
| 566 | + | |
| 567 | + } | |
| 568 | + | |
| 569 | + glGenBuffers(1, &vbo.first); | |
| 570 | + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); | |
| 571 | + glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); | |
| 572 | + delete[] vertexData; | |
| 573 | + | |
| 574 | + GLushort* indexData = new GLushort[indices]; | |
| 575 | + GLushort* index = indexData; | |
| 576 | + for (int i = 0; i < indices; i++) { | |
| 577 | + index[i] = cannonicalIndices[i]; | |
| 578 | + } | |
| 579 | + | |
| 580 | + glGenBuffers(1, &vbo.second); | |
| 581 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); | |
| 582 | + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW); | |
| 583 | + delete[] indexData; | |
| 584 | + | |
| 585 | + } else { | |
| 586 | + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); | |
| 587 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); | |
| 588 | + } | |
| 589 | + glEnableClientState(GL_VERTEX_ARRAY); | |
| 590 | + glEnableClientState(GL_NORMAL_ARRAY); | |
| 591 | + | |
| 592 | + glNormalPointer(GL_FLOAT, 6 * sizeof(float), 0); | |
| 593 | + glVertexPointer(3, GL_FLOAT, (6 * sizeof(float)), (const void *)(3 * sizeof(float))); | |
| 594 | + | |
| 595 | + glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); | |
| 596 | + | |
| 597 | + glDisableClientState(GL_VERTEX_ARRAY); | |
| 598 | + glDisableClientState(GL_NORMAL_ARRAY); | |
| 599 | + | |
| 600 | + glBindBuffer(GL_ARRAY_BUFFER, 0); | |
| 601 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
| 602 | +} | |
| 603 | + | |
| 604 | +void GeometryCache::renderWireCube(float size) { | |
| 605 | + VerticesIndices& vbo = _wireCubeVBOs[size]; | |
| 606 | + const int FLOATS_PER_VERTEX = 3; | |
| 607 | + const int VERTICES_PER_EDGE = 2; | |
| 608 | + const int TOP_EDGES = 4; | |
| 609 | + const int BOTTOM_EDGES = 4; | |
| 610 | + const int SIDE_EDGES = 4; | |
| 611 | + const int vertices = 8; | |
| 612 | + const int indices = (TOP_EDGES + BOTTOM_EDGES + SIDE_EDGES) * VERTICES_PER_EDGE; | |
| 613 | + if (vbo.first == 0) { | |
| 614 | + int vertexPoints = vertices * FLOATS_PER_VERTEX; | |
| 615 | + GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a wire cube | |
| 616 | + GLfloat* vertex = vertexData; | |
| 617 | + float halfSize = size / 2.0f; | |
| 618 | + | |
| 619 | + static GLfloat cannonicalVertices[] = | |
| 620 | + { 1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // v0, v1, v2, v3 (top) | |
| 621 | + 1,-1, 1, 1,-1,-1, -1,-1,-1, -1,-1, 1 // v4, v5, v6, v7 (bottom) | |
| 622 | + }; | |
| 623 | + | |
| 624 | + // index array of vertex array for glDrawRangeElement() as a GL_LINES for each edge | |
| 625 | + static GLubyte cannonicalIndices[indices] = { | |
| 626 | + 0, 1, 1, 2, 2, 3, 3, 0, // (top) | |
| 627 | + 4, 5, 5, 6, 6, 7, 7, 4, // (bottom) | |
| 628 | + 0, 4, 1, 5, 2, 6, 3, 7, // (side edges) | |
| 629 | + }; | |
| 630 | + | |
| 631 | + for (int i = 0; i < vertexPoints; i++) { | |
| 632 | + vertex[i] = cannonicalVertices[i] * halfSize; | |
| 633 | + } | |
| 634 | + | |
| 635 | + glGenBuffers(1, &vbo.first); | |
| 636 | + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); | |
| 637 | + glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); | |
| 638 | + delete[] vertexData; | |
| 639 | + | |
| 640 | + GLushort* indexData = new GLushort[indices]; | |
| 641 | + GLushort* index = indexData; | |
| 642 | + for (int i = 0; i < indices; i++) { | |
| 643 | + index[i] = cannonicalIndices[i]; | |
| 644 | + } | |
| 645 | + | |
| 646 | + glGenBuffers(1, &vbo.second); | |
| 647 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); | |
| 648 | + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW); | |
| 649 | + delete[] indexData; | |
| 650 | + | |
| 651 | + } else { | |
| 652 | + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); | |
| 653 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); | |
| 654 | + } | |
| 655 | + glEnableClientState(GL_VERTEX_ARRAY); | |
| 656 | + glVertexPointer(FLOATS_PER_VERTEX, GL_FLOAT, FLOATS_PER_VERTEX * sizeof(float), 0); | |
| 657 | + glDrawRangeElementsEXT(GL_LINES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); | |
| 658 | + glDisableClientState(GL_VERTEX_ARRAY); | |
| 659 | + | |
| 660 | + glBindBuffer(GL_ARRAY_BUFFER, 0); | |
| 661 | + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | |
| 662 | +} | |
| 663 | + | |
| 664 | + | |
| 506 | 665 | QSharedPointer<NetworkGeometry> GeometryCache::getGeometry(const QUrl& url, const QUrl& fallback, bool delayLoad) { |
| 507 | 666 | return getResource(url, fallback, delayLoad).staticCast<NetworkGeometry>(); |
| 508 | 667 | } |