History

first cut at moving wire cube to support gpu streams

ZappoMan committed 11 years ago

Commit daafa201 parent 149f3652 files · +67 −31Browse files

Showing the changes to libraries/render-utils/src/GeometryCache.cpp (1 of 2 files). Show all

Unified · Split

libraries/render-utils/src/GeometryCache.cpp+58−30
@@ -780,7 +780,8 @@ void GeometryCache::renderSolidCube(float size, const glm::vec4& color) { · 779 unmodified lines
780780 }
781781
782782 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));
784785 const int FLOATS_PER_VERTEX = 3;
785786 const int VERTICES_PER_EDGE = 2;
786787 const int TOP_EDGES = 4;
@@ -788,7 +789,11 @@ void GeometryCache::renderWireCube(float size, const glm::vec4& color) { · 1 unmodified line
788789 const int SIDE_EDGES = 4;
789790 const int vertices = 8;
790791 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+
792797 int vertexPoints = vertices * FLOATS_PER_VERTEX;
793798 GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a wire cube
794799 GLfloat* vertex = vertexData;
@@ -799,42 +804,65 @@ void GeometryCache::renderWireCube(float size, const glm::vec4& color) { · 4 unmodified lines
799804 1,-1, 1, 1,-1,-1, -1,-1,-1, -1,-1, 1 // v4, v5, v6, v7 (bottom)
800805 };
801806
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) {
803815 static GLubyte cannonicalIndices[indices] = {
804816 0, 1, 1, 2, 2, 3, 3, 0, // (top)
805817 4, 5, 5, 6, 6, 7, 7, 4, // (bottom)
806818 0, 4, 1, 5, 2, 6, 3, 7, // (side edges)
807819 };
808−
809− for (int i = 0; i < vertexPoints; i++) {
810− vertex[i] = cannonicalVertices[i] * halfSize;
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;
820+
821+ gpu::BufferPointer indexBuffer(new gpu::Buffer());
822+ _wireCubeIndexBuffer = indexBuffer;
828823
829− } else {
830− glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
831− glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second);
824+ _wireCubeIndexBuffer->append(sizeof(cannonicalIndices), (gpu::Buffer::Byte*) cannonicalIndices);
832825 }
833− glEnableClientState(GL_VERTEX_ARRAY);
834− glVertexPointer(FLOATS_PER_VERTEX, GL_FLOAT, FLOATS_PER_VERTEX * sizeof(float), 0);
835− glDrawRangeElementsEXT(GL_LINES, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0);
836− glDisableClientState(GL_VERTEX_ARRAY);
826+
827+ if (!_cubeColors.contains(colorKey)) {
828+ gpu::BufferPointer colorBuffer(new gpu::Buffer());
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);
837852
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+
838866 glBindBuffer(GL_ARRAY_BUFFER, 0);
839867 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
840868 }