From 32784ea1919799368a6cd5b17ab40e5153c4fbbf Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Mon, 13 May 2013 11:14:35 +0200 Subject: [PATCH] Added possibility of adding multiple vertices to VBO_ITEM at once --- common/gal/opengl/opengl_gal.cpp | 32 +++++++++-------------------- common/gal/opengl/vbo_item.cpp | 35 +++++++++++++++++++++++++++++--- include/gal/opengl/vbo_item.h | 1 + 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index f454b2ccdb..08d1c03a9f 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -651,28 +651,16 @@ void OPENGL_GAL::DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndP VECTOR2D v3( aEndPoint.x - ( aWidth * cos( beta ) / 2.0 ), aEndPoint.y + ( aWidth * sin( beta ) / 2.0 ) ); - // First triangle - GLfloat newVertex1[] = { v0.x, v0.y, layerDepth, - strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a }; - GLfloat newVertex2[] = { v1.x, v1.y, layerDepth, - strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a }; - GLfloat newVertex3[] = { v2.x, v2.y, layerDepth, - strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a }; - - // Second triangle - GLfloat newVertex4[] = { v0.x, v0.y, layerDepth, - strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a }; - GLfloat newVertex5[] = { v2.x, v2.y, layerDepth, - strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a }; - GLfloat newVertex6[] = { v3.x, v3.y, layerDepth, - strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a }; - - curVboItem->PushVertex( newVertex1 ); - curVboItem->PushVertex( newVertex2 ); - curVboItem->PushVertex( newVertex3 ); - curVboItem->PushVertex( newVertex4 ); - curVboItem->PushVertex( newVertex5 ); - curVboItem->PushVertex( newVertex6 ); + // Two triangles + GLfloat newVertices[] = { + v0.x, v0.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a, + v1.x, v1.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a, + v2.x, v2.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a, + v0.x, v0.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a, + v2.x, v2.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a, + v3.x, v3.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a + }; + curVboItem->PushVertices( newVertices, 6 ); } if( isFillEnabled ) diff --git a/common/gal/opengl/vbo_item.cpp b/common/gal/opengl/vbo_item.cpp index ba6a27c147..fc9a8df53d 100644 --- a/common/gal/opengl/vbo_item.cpp +++ b/common/gal/opengl/vbo_item.cpp @@ -61,7 +61,7 @@ void VBO_ITEM::PushVertex( const GLfloat* aVertex ) if( m_vertices ) { // Copy all previous vertices data - memcpy( newVertices, m_vertices, ( m_size ) * VertSize ); + memcpy( newVertices, m_vertices, m_size * VertSize ); delete m_vertices; } m_vertices = newVertices; @@ -73,7 +73,7 @@ void VBO_ITEM::PushVertex( const GLfloat* aVertex ) if( m_indices ) { // Copy all previous vertices data - memcpy( newIndices, m_indices, ( m_size ) * IndSize ); + memcpy( newIndices, m_indices, m_size * IndSize ); delete m_indices; } m_indices = newIndices; @@ -88,7 +88,36 @@ void VBO_ITEM::PushVertex( const GLfloat* aVertex ) void VBO_ITEM::PushVertices( const GLfloat* aVertex, GLuint aSize ) { - // FIXME to be done + int newSize = m_size + aSize; + GLfloat* newVertices = new GLfloat[newSize * VertStride]; + GLuint* newIndices = new GLuint[newSize * IndStride]; + + // Handle new vertices + if( m_vertices ) + { + // Copy all previous vertices data + memcpy( newVertices, m_vertices, ( m_size ) * VertSize ); + delete m_vertices; + } + m_vertices = newVertices; + + // Add the new vertex + memcpy( &newVertices[m_size * VertStride], aVertex, aSize * VertSize ); + + // Handle new indices + if( m_indices ) + { + // Copy all previous vertices data + memcpy( newIndices, m_indices, ( m_size ) * IndSize ); + delete m_indices; + } + m_indices = newIndices; + + // Add the new vertex + for( int i = m_size; i < newSize; ++i ) + m_indices[i] = m_offset + i; + + m_size += aSize; m_isDirty = true; } diff --git a/include/gal/opengl/vbo_item.h b/include/gal/opengl/vbo_item.h index cc6c629d60..80ed663bd9 100644 --- a/include/gal/opengl/vbo_item.h +++ b/include/gal/opengl/vbo_item.h @@ -96,6 +96,7 @@ private: ///< Packed by 7 floats for each vertex: {X, Y, Z, R, G, B, A} GLfloat* m_vertices; + ///< Indices of vertices GLuint* m_indices; ///< Offset and size of data in VBO.