Showing the changes to libraries/render-utils/src/GeometryCache.cpp (1 of 2 files). Show all
Unified · Split
| @@ -780,7 +780,8 @@ void GeometryCache::renderSolidCube(float size, const glm::vec4& color) { · 779 unmodified lines | |||
| 780 | } | 780 | } |
| 781 | 781 | ||
| 782 | void GeometryCache::renderWireCube(float size, const glm::vec4& color) { | 782 | void GeometryCache::renderWireCube(float size, const glm::vec4& color) { |
| 783 | VerticesIndices& vbo = _wireCubeVBOs[size]; | 783 | |
| 784 | Vec2Pair colorKey(glm::vec2(color.x, color.y),glm::vec2(color.z, color.y)); | ||
| 784 | const int FLOATS_PER_VERTEX = 3; | 785 | const int FLOATS_PER_VERTEX = 3; |
| 785 | const int VERTICES_PER_EDGE = 2; | 786 | const int VERTICES_PER_EDGE = 2; |
| 786 | const int TOP_EDGES = 4; | 787 | const int TOP_EDGES = 4; |
| @@ -788,7 +789,11 @@ void GeometryCache::renderWireCube(float size, const glm::vec4& color) { · 1 unmodified line | |||
| 788 | const int SIDE_EDGES = 4; | 789 | const int SIDE_EDGES = 4; |
| 789 | const int vertices = 8; | 790 | const int vertices = 8; |
| 790 | const int indices = (TOP_EDGES + BOTTOM_EDGES + SIDE_EDGES) * VERTICES_PER_EDGE; | 791 | const int indices = (TOP_EDGES + BOTTOM_EDGES + SIDE_EDGES) * VERTICES_PER_EDGE; |
| 791 | if (vbo.first == 0) { | 792 | |
| 793 | if (!_cubeVerticies.contains(size)) { | ||
| 794 | gpu::BufferPointer verticesBuffer(new gpu::Buffer()); | ||
| 795 | _cubeVerticies[size] = verticesBuffer; | ||
| 796 | |||
| 792 | int vertexPoints = vertices * FLOATS_PER_VERTEX; | 797 | int vertexPoints = vertices * FLOATS_PER_VERTEX; |
| 793 | GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a wire cube | 798 | GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a wire cube |
| 794 | GLfloat* vertex = vertexData; | 799 | GLfloat* vertex = vertexData; |
| @@ -799,42 +804,65 @@ void GeometryCache::renderWireCube(float size, const glm::vec4& color) { · 4 unmodified lines | |||
| 799 | 1,-1, 1, 1,-1,-1, -1,-1,-1, -1,-1, 1 // v4, v5, v6, v7 (bottom) | 804 | 1,-1, 1, 1,-1,-1, -1,-1,-1, -1,-1, 1 // v4, v5, v6, v7 (bottom) |
| 800 | }; | 805 | }; |
| 801 | 806 | ||
| 802 | // index array of vertex array for glDrawRangeElement() as a GL_LINES for each edge | 807 | for (int i = 0; i < vertexPoints; i++) { |
| 808 | vertex[i] = cannonicalVertices[i] * halfSize; | ||
| 809 | } | ||
| 810 | |||
| 811 | verticesBuffer->append(sizeof(GLfloat) * vertexPoints, (gpu::Buffer::Byte*) vertexData); // I'm skeptical that this is right | ||
| 812 | } | ||
| 813 | |||
| 814 | if (!_wireCubeIndexBuffer) { | ||
| 803 | static GLubyte cannonicalIndices[indices] = { | 815 | static GLubyte cannonicalIndices[indices] = { |
| 804 | 0, 1, 1, 2, 2, 3, 3, 0, // (top) | 816 | 0, 1, 1, 2, 2, 3, 3, 0, // (top) |
| 805 | 4, 5, 5, 6, 6, 7, 7, 4, // (bottom) | 817 | 4, 5, 5, 6, 6, 7, 7, 4, // (bottom) |
| 806 | 0, 4, 1, 5, 2, 6, 3, 7, // (side edges) | 818 | 0, 4, 1, 5, 2, 6, 3, 7, // (side edges) |
| 807 | }; | 819 | }; |
| 808 | 820 | ||
| 809 | for (int i = 0; i < vertexPoints; i++) { | 821 | gpu::BufferPointer indexBuffer(new gpu::Buffer()); |
| 810 | vertex[i] = cannonicalVertices[i] * halfSize; | 822 | _wireCubeIndexBuffer = indexBuffer; |
| 811 | } | ||
| 812 | |||
| 813 | glGenBuffers(1, &vbo.first); | ||
| 814 | glBindBuffer(GL_ARRAY_BUFFER, vbo.first); | ||
| 815 | glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); | ||
| 816 | delete[] vertexData; | ||
| 817 | |||
| 818 | GLushort* indexData = new GLushort[indices]; | ||
| 819 | GLushort* index = indexData; | ||
| 820 | for (int i = 0; i < indices; i++) { | ||
| 821 | index[i] = cannonicalIndices[i]; | ||
| 822 | } | ||
| 823 | |||
| 824 | glGenBuffers(1, &vbo.second); | ||
| 825 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); | ||
| 826 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW); | ||
| 827 | delete[] indexData; | ||
| 828 | 823 | ||
| 829 | } else { | 824 | _wireCubeIndexBuffer->append(sizeof(cannonicalIndices), (gpu::Buffer::Byte*) cannonicalIndices); |
| 830 | glBindBuffer(GL_ARRAY_BUFFER, vbo.first); | ||
| 831 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); | ||
| 832 | } | 825 | } |
| 833 | glEnableClientState(GL_VERTEX_ARRAY); | 826 | |
| 834 | glVertexPointer(FLOATS_PER_VERTEX, GL_FLOAT, FLOATS_PER_VERTEX * sizeof(float), 0); | 827 | if (!_cubeColors.contains(colorKey)) { |
| 835 | glDrawRangeElementsEXT(GL_LINES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); | 828 | gpu::BufferPointer colorBuffer(new gpu::Buffer()); |
| 836 | glDisableClientState(GL_VERTEX_ARRAY); | 829 | _cubeColors[colorKey] = colorBuffer; |
| 830 | |||
| 831 | const int NUM_COLOR_SCALARS_PER_CUBE = 8; | ||
| 832 | int compactColor = ((int(color.x * 255.0f) & 0xFF)) | | ||
| 833 | ((int(color.y * 255.0f) & 0xFF) << 8) | | ||
| 834 | ((int(color.z * 255.0f) & 0xFF) << 16) | | ||
| 835 | ((int(color.w * 255.0f) & 0xFF) << 24); | ||
| 836 | int colors[NUM_COLOR_SCALARS_PER_CUBE] = { compactColor, compactColor, compactColor, compactColor, | ||
| 837 | compactColor, compactColor, compactColor, compactColor }; | ||
| 838 | |||
| 839 | colorBuffer->append(sizeof(colors), (gpu::Buffer::Byte*) colors); | ||
| 840 | } | ||
| 841 | gpu::BufferPointer verticesBuffer = _cubeVerticies[size]; | ||
| 842 | gpu::BufferPointer colorBuffer = _cubeColors[colorKey]; | ||
| 843 | |||
| 844 | const int VERTICES_SLOT = 0; | ||
| 845 | const int COLOR_SLOT = 1; | ||
| 846 | gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone | ||
| 847 | streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::POS_XYZ), 0); | ||
| 848 | streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); | ||
| 849 | |||
| 850 | gpu::BufferView verticesView(verticesBuffer, streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); | ||
| 851 | gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); | ||
| 837 | 852 | ||
| 853 | gpu::Batch batch; | ||
| 854 | |||
| 855 | batch.setInputFormat(streamFormat); | ||
| 856 | batch.setInputBuffer(VERTICES_SLOT, verticesView); | ||
| 857 | batch.setInputBuffer(COLOR_SLOT, colorView); | ||
| 858 | batch.setIndexBuffer(gpu::UINT8, _wireCubeIndexBuffer, 0); | ||
| 859 | batch.drawIndexed(gpu::LINES, indices); | ||
| 860 | |||
| 861 | gpu::GLBackend::renderBatch(batch); | ||
| 862 | |||
| 863 | glDisableClientState(GL_VERTEX_ARRAY); | ||
| 864 | glDisableClientState(GL_COLOR_ARRAY); | ||
| 865 | |||
| 838 | glBindBuffer(GL_ARRAY_BUFFER, 0); | 866 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 839 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | 867 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 840 | } | 868 | } |