From 66d257f6207ee37c34e13066cec94b378f66bacd Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Tue, 14 May 2013 10:38:25 +0200 Subject: [PATCH] Drawing tracks using PushVertices, added some comments, fixed formatting. --- common/gal/opengl/opengl_gal.cpp | 90 +++++++++++++++++------------ common/gal/opengl/shader/round.vert | 2 +- common/gal/opengl/vbo_item.cpp | 8 +-- include/gal/opengl/vbo_item.h | 34 +++++++---- 4 files changed, 83 insertions(+), 51 deletions(-) diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index 08d1c03a9f..ce691a06e3 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -68,7 +68,7 @@ OPENGL_GAL::OPENGL_GAL( wxWindow* aParent, wxEvtHandler* aMouseListener, isFrameBufferInitialized = false; isUseShader = isUseShaders; isShaderInitialized = false; - isGrouping = false; + isGrouping = false; shaderPath = "../../common/gal/opengl/shader/"; wxSize parentSize = aParent->GetSize(); @@ -615,20 +615,35 @@ inline void OPENGL_GAL::drawLineQuad( const VECTOR2D& aStartPoint, const VECTOR2 VECTOR2D perpendicularVector( -startEndVector.y * scale, startEndVector.x * scale ); // Compute the edge points of the line - VECTOR2D point1 = aStartPoint + perpendicularVector; - VECTOR2D point2 = aStartPoint - perpendicularVector; - VECTOR2D point3 = aEndPoint + perpendicularVector; - VECTOR2D point4 = aEndPoint - perpendicularVector; + VECTOR2D v0 = aStartPoint + perpendicularVector; + VECTOR2D v1 = aStartPoint - perpendicularVector; + VECTOR2D v2 = aEndPoint + perpendicularVector; + VECTOR2D v3 = aEndPoint - perpendicularVector; - glBegin( GL_TRIANGLES ); - glVertex3d( point1.x, point1.y, layerDepth ); - glVertex3d( point2.x, point2.y, layerDepth ); - glVertex3d( point4.x, point4.y, layerDepth ); + if( isGrouping ) + { + 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, + v3.x, v3.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a, + v0.x, v0.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a, + v3.x, v3.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a, + v2.x, v2.y, layerDepth, strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a + }; + curVboItem->PushVertices( newVertices, 6 ); + } + else + { + glBegin( GL_TRIANGLES ); + glVertex3d( v0.x, v0.y, layerDepth ); + glVertex3d( v1.x, v1.y, layerDepth ); + glVertex3d( v3.x, v3.y, layerDepth ); - glVertex3d( point1.x, point1.y, layerDepth ); - glVertex3d( point4.x, point4.y, layerDepth ); - glVertex3d( point3.x, point3.y, layerDepth ); - glEnd(); + glVertex3d( v0.x, v0.y, layerDepth ); + glVertex3d( v3.x, v3.y, layerDepth ); + glVertex3d( v2.x, v2.y, layerDepth ); + glEnd(); + } } @@ -637,7 +652,7 @@ void OPENGL_GAL::DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndP VECTOR2D startEndVector = aEndPoint - aStartPoint; double lineAngle = atan2( startEndVector.y, startEndVector.x ); - if ( isGrouping ) + /*if ( isGrouping ) { // Angle of a line perpendicular to the segment being drawn double beta = ( M_PI / 2.0 ) - lineAngle; @@ -662,37 +677,40 @@ void OPENGL_GAL::DrawSegment( const VECTOR2D& aStartPoint, const VECTOR2D& aEndP }; curVboItem->PushVertices( newVertices, 6 ); } - - if( isFillEnabled ) + else*/ { - glColor4d( fillColor.r, fillColor.g, fillColor.b, fillColor.a ); + if( isFillEnabled ) + { + if( !isGrouping ) + glColor4d( fillColor.r, fillColor.g, fillColor.b, fillColor.a ); - SetLineWidth( aWidth ); - drawSemiCircle( aStartPoint, aWidth / 2, lineAngle + M_PI / 2, layerDepth ); - drawSemiCircle( aEndPoint, aWidth / 2, lineAngle - M_PI / 2, layerDepth ); - drawLineQuad( aStartPoint, aEndPoint ); - } - else - { - double lineLength = startEndVector.EuclideanNorm(); + SetLineWidth( aWidth ); + drawSemiCircle( aStartPoint, aWidth / 2, lineAngle + M_PI / 2, layerDepth ); + drawSemiCircle( aEndPoint, aWidth / 2, lineAngle - M_PI / 2, layerDepth ); + drawLineQuad( aStartPoint, aEndPoint ); + } + else + { + double lineLength = startEndVector.EuclideanNorm(); - glColor4d( strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a ); + glColor4d( strokeColor.r, strokeColor.g, strokeColor.b, strokeColor.a ); - glPushMatrix(); + glPushMatrix(); - glTranslated( aStartPoint.x, aStartPoint.y, 0.0 ); - glRotated( lineAngle * ( 360 / ( 2 * M_PI ) ), 0, 0, 1 ); + glTranslated( aStartPoint.x, aStartPoint.y, 0.0 ); + glRotated( lineAngle * ( 360 / ( 2 * M_PI ) ), 0, 0, 1 ); - drawLineQuad( VECTOR2D( 0.0, aWidth / 2.0 ), - VECTOR2D( lineLength, aWidth / 2.0 ) ); + drawLineQuad( VECTOR2D( 0.0, aWidth / 2.0 ), + VECTOR2D( lineLength, aWidth / 2.0 ) ); - drawLineQuad( VECTOR2D( 0.0, -aWidth / 2.0 ), - VECTOR2D( lineLength, -aWidth / 2.0 ) ); + drawLineQuad( VECTOR2D( 0.0, -aWidth / 2.0 ), + VECTOR2D( lineLength, -aWidth / 2.0 ) ); - DrawArc( VECTOR2D( 0.0, 0.0 ), aWidth / 2.0, M_PI / 2.0, 3.0 * M_PI / 2.0 ); - DrawArc( VECTOR2D( lineLength, 0.0 ), aWidth / 2.0, M_PI / 2.0, -M_PI / 2.0 ); + DrawArc( VECTOR2D( 0.0, 0.0 ), aWidth / 2.0, M_PI / 2.0, 3.0 * M_PI / 2.0 ); + DrawArc( VECTOR2D( lineLength, 0.0 ), aWidth / 2.0, M_PI / 2.0, -M_PI / 2.0 ); - glPopMatrix(); + glPopMatrix(); + } } } diff --git a/common/gal/opengl/shader/round.vert b/common/gal/opengl/shader/round.vert index 21410a6063..c42f2be8f5 100644 --- a/common/gal/opengl/shader/round.vert +++ b/common/gal/opengl/shader/round.vert @@ -25,7 +25,7 @@ #version 120 -varying float aspect; +varying float aspect; void main() { diff --git a/common/gal/opengl/vbo_item.cpp b/common/gal/opengl/vbo_item.cpp index fc9a8df53d..2ef84ccf5a 100644 --- a/common/gal/opengl/vbo_item.cpp +++ b/common/gal/opengl/vbo_item.cpp @@ -86,7 +86,7 @@ void VBO_ITEM::PushVertex( const GLfloat* aVertex ) } -void VBO_ITEM::PushVertices( const GLfloat* aVertex, GLuint aSize ) +void VBO_ITEM::PushVertices( const GLfloat* aVertices, GLuint aSize ) { int newSize = m_size + aSize; GLfloat* newVertices = new GLfloat[newSize * VertStride]; @@ -101,8 +101,8 @@ void VBO_ITEM::PushVertices( const GLfloat* aVertex, GLuint aSize ) } m_vertices = newVertices; - // Add the new vertex - memcpy( &newVertices[m_size * VertStride], aVertex, aSize * VertSize ); + // Add new vertices + memcpy( &newVertices[m_size * VertStride], aVertices, aSize * VertSize ); // Handle new indices if( m_indices ) @@ -160,7 +160,7 @@ void SetVbo( int aVboId ) } -int GetVbo() +int GetVbo() const { } */ diff --git a/include/gal/opengl/vbo_item.h b/include/gal/opengl/vbo_item.h index 80ed663bd9..ca0f229c99 100644 --- a/include/gal/opengl/vbo_item.h +++ b/include/gal/opengl/vbo_item.h @@ -43,9 +43,23 @@ public: VBO_ITEM(); ~VBO_ITEM(); - // TODO comments + /** + * Function PushVertex() + * Adds a single vertex to the VBO_ITEM. Vertex contains information about coordinates and + * colors and has to follow the specified format {X,Y,Z,R,G,B,A}. + * @param aVertex is a vertex to be added. + */ void PushVertex( const GLfloat* aVertex ); - void PushVertices( const GLfloat* aVertex, GLuint aSize ); + + /** + * Function PushVertices() + * Adds multiple vertices to the VBO_ITEM. This function is recommended over multiple calls to + * PushVertex, as it does less memory reallocations. Vertices contain information about + * coordinates and colors and has to follow the specified format {X,Y,Z,R,G,B,A}. + * @param aVertices are vertices to be added. + * @param aSize is an amount of vertices to be added. + */ + void PushVertices( const GLfloat* aVertices, GLuint aSize ); /** * Function GetVertices() @@ -79,7 +93,7 @@ public: ///< Functions for getting VBO ids. //void SetVbo( int aVboId ); - //int GetVbo(); + //int GetVbo() const; ///< Data organization information for vertices. static const int VertStride = 7; @@ -94,21 +108,21 @@ private: ///< Contains vertices coordinates and colors. ///< Packed by 7 floats for each vertex: {X, Y, Z, R, G, B, A} - GLfloat* m_vertices; + GLfloat* m_vertices; ///< Indices of vertices - GLuint* m_indices; + GLuint* m_indices; ///< Offset and size of data in VBO. - int m_offset; - int m_size; + int m_offset; + int m_size; ///< Shader data used for rendering. - int m_shader; - int m_shaderAttrib; + int m_shader; + int m_shaderAttrib; ///< Flag telling if the item should be recached in VBO or not. - bool m_isDirty; + bool m_isDirty; }; } // namespace KiGfx