From d7857dd026c434f70cb46e8b13c2205df482e828 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Fri, 2 Aug 2013 10:55:40 +0200 Subject: [PATCH] Fixed the tesselator, so now it works with Windows. --- common/gal/opengl/opengl_gal.cpp | 48 ++++++++++------------------ include/gal/opengl/opengl_gal.h | 55 ++++++++++---------------------- 2 files changed, 33 insertions(+), 70 deletions(-) diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index 640a1b4ae9..358a7add09 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -37,10 +37,6 @@ #include -#ifndef CALLBACK -#define CALLBACK -#endif - using namespace KiGfx; // Prototypes @@ -92,6 +88,10 @@ OPENGL_GAL::OPENGL_GAL( wxWindow* aParent, wxEvtHandler* aMouseListener, // Tesselator initialization tesselator = gluNewTess(); InitTesselatorCallbacks( tesselator ); + if( tesselator == NULL ) + { + wxLogFatalError( wxT( "Could not create the tesselator" ) ); + } gluTessProperty( tesselator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE ); } @@ -233,7 +233,7 @@ void OPENGL_GAL::DrawLine( const VECTOR2D& aStartPoint, const VECTOR2D& aEndPoin // Line caps drawFilledSemiCircle( aStartPoint, lineWidth / 2, lineAngle + M_PI / 2 ); - drawFilledSemiCircle( aEndPoint, lineWidth / 2, lineAngle - M_PI / 2 ); + drawFilledSemiCircle( aEndPoint, lineWidth / 2, lineAngle - M_PI / 2 ); } @@ -471,41 +471,25 @@ void OPENGL_GAL::DrawPolygon( const std::deque& aPointList ) // for this purpose the GLU standard functions are used currentManager->Shader( SHADER_NONE ); - typedef std::vector OGLPOINTS; - - // Do only one heap allocation, can do because we know size in advance. - // std::vector is then fastest - OGLPOINTS vertexList( aPointList.size(), OGLPOINT( "fastest" ) ); - - glNormal3d( 0.0, 0.0, 1.0 ); - currentManager->Color( fillColor.r, fillColor.g, fillColor.b, fillColor.a ); - - glShadeModel( GL_FLAT ); - TessParams params = { currentManager, tessIntersects }; gluTessBeginPolygon( tesselator, ¶ms ); gluTessBeginContour( tesselator ); - // use operator=( const POINTS& ) - copy( aPointList.begin(), aPointList.end(), vertexList.begin() ); - - for( OGLPOINTS::iterator it = vertexList.begin(); it != vertexList.end(); it++ ) + boost::shared_array points( new GLdouble[3 * aPointList.size()] ); + int v = 0; + for( std::deque::const_iterator it = aPointList.begin(); it != aPointList.end(); it++ ) { - it->z = layerDepth; - gluTessVertex( tesselator, &it->x, &it->x ); + points[v] = it->x; + points[v + 1] = it->y; + points[v + 2] = layerDepth; + gluTessVertex( tesselator, &points[v], &points[v] ); + v += 3; } gluTessEndContour( tesselator ); gluTessEndPolygon( tesselator ); // Free allocated intersecting points - std::vector::iterator it, it_end; - - for( it = tessIntersects.begin(), it_end = tessIntersects.end(); it < it_end; ++it ) - { - delete[] *it; - } - tessIntersects.clear(); // vertexList destroyed here @@ -1028,7 +1012,7 @@ void CALLBACK CombineCallback( GLdouble coords[3], OPENGL_GAL::TessParams* param = static_cast( aData ); // Save the pointer so we can delete it later - param->intersectPoints.push_back( vertex ); + param->intersectPoints.push_back( boost::shared_array( vertex ) ); memcpy( vertex, coords, 3 * sizeof(GLdouble) ); @@ -1036,7 +1020,7 @@ void CALLBACK CombineCallback( GLdouble coords[3], } -void CALLBACK EdgeCallback() +void CALLBACK EdgeCallback( GLboolean aEdgeFlag ) { // This callback is needed to force GLU tesselator to use triangles only } @@ -1056,5 +1040,5 @@ void InitTesselatorCallbacks( GLUtesselator* aTesselator ) gluTessCallback( aTesselator, GLU_TESS_VERTEX_DATA, ( void (CALLBACK*)() )VertexCallback ); gluTessCallback( aTesselator, GLU_TESS_COMBINE_DATA, ( void (CALLBACK*)() )CombineCallback ); gluTessCallback( aTesselator, GLU_TESS_EDGE_FLAG, ( void (CALLBACK*)() )EdgeCallback ); - gluTessCallback( aTesselator, GLU_TESS_ERROR_DATA, ( void (CALLBACK*)() )ErrorCallback ); + gluTessCallback( aTesselator, GLU_TESS_ERROR, ( void (CALLBACK*)() )ErrorCallback ); } diff --git a/include/gal/opengl/opengl_gal.h b/include/gal/opengl/opengl_gal.h index 402f8fc133..4291090520 100644 --- a/include/gal/opengl/opengl_gal.h +++ b/include/gal/opengl/opengl_gal.h @@ -47,11 +47,15 @@ #include #include #include +#include #include #include #include +#ifndef CALLBACK +#define CALLBACK +#endif namespace KiGfx { @@ -244,8 +248,10 @@ public: ///< Parameters passed to the GLU tesselator typedef struct { - VERTEX_MANAGER* vboManager; ///< VERTEX_ITEM for storing new vertices - std::vector& intersectPoints; ///< Intersect points, that have to be freed + /// Manager used for storing new vertices + VERTEX_MANAGER* vboManager; + /// Intersect points, that have to be freed after tessellation + std::deque< boost::shared_array >& intersectPoints; } TessParams; protected: @@ -258,11 +264,11 @@ private: static const int CIRCLE_POINTS = 64; ///< The number of points for circle approximation static const int CURVE_POINTS = 32; ///< The number of points for curve approximation - wxClientDC* clientDC; ///< Drawing context - wxGLContext* glContext; ///< OpenGL context of wxWidgets - wxWindow* parentWindow; ///< Parent window - wxEvtHandler* mouseListener; - wxEvtHandler* paintListener; + wxClientDC* clientDC; ///< Drawing context + wxGLContext* glContext; ///< OpenGL context of wxWidgets + wxWindow* parentWindow; ///< Parent window + wxEvtHandler* mouseListener; + wxEvtHandler* paintListener; // Vertex buffer objects related fields typedef std::map< unsigned int, boost::shared_ptr > GroupsMap; @@ -294,37 +300,10 @@ private: bool isGrouping; ///< Was a group started? // Polygon tesselation - GLUtesselator* tesselator; ///< Pointer to the tesselator - std::vector tessIntersects; ///< Storage of intersecting points - - // Structure used for tesselation of polygons - struct OGLPOINT - { - OGLPOINT() : - x( 0.0 ), y( 0.0 ), z( 0.0 ) - {} - - OGLPOINT( const char* fastest ) - { - // do nothing for fastest speed, and keep inline - } - - OGLPOINT( const VECTOR2D& aPoint ) : - x( aPoint.x ), y( aPoint.y ), z( 0.0 ) - {} - - OGLPOINT& operator=( const VECTOR2D& aPoint ) - { - x = aPoint.x; - y = aPoint.y; - z = 0.0; - return *this; - } - - GLdouble x; - GLdouble y; - GLdouble z; - }; + /// The tessellator + GLUtesselator* tesselator; + /// Storage for intersecting points + std::deque< boost::shared_array > tessIntersects; /** * @brief Draw a quad for the line.