From b0c1b97ff361f6cdf09f5581df1d5ae2f068b76e Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 17 Jul 2013 10:21:29 +0200 Subject: [PATCH] Fixed stroked circles width issue with OpenGL shaders. --- common/gal/opengl/opengl_gal.cpp | 21 ++++++++++++--------- common/gal/opengl/shader.frag | 12 ++++++------ common/gal/opengl/shader.vert | 13 ++++++------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index ad190861a2..a82fa33d30 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -795,7 +795,7 @@ void OPENGL_GAL::DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) aCenterPoint.y - aRadius, layerDepth ); // v0 setShader( SHADER_FILLED_CIRCLE, 2.0 ); - vertex3( aCenterPoint.x + aRadius * sqrt( 3.0f ), + vertex3( aCenterPoint.x + aRadius* sqrt( 3.0f ), aCenterPoint.y - aRadius, layerDepth ); // v1 setShader( SHADER_FILLED_CIRCLE, 3.0 ); @@ -816,16 +816,17 @@ void OPENGL_GAL::DrawCircle( const VECTOR2D& aCenterPoint, double aRadius ) //\\ v0 /_\/_\ v1 */ + double outerRadius = aRadius + ( lineWidth / 2 ); setShader( SHADER_STROKED_CIRCLE, 1.0, aRadius, lineWidth ); - vertex3( aCenterPoint.x - aRadius * sqrt( 3.0f ), - aCenterPoint.y - aRadius, layerDepth ); // v0 + vertex3( aCenterPoint.x - outerRadius * sqrt( 3.0f ), + aCenterPoint.y - outerRadius, layerDepth ); // v0 setShader( SHADER_STROKED_CIRCLE, 2.0, aRadius, lineWidth ); - vertex3( aCenterPoint.x + aRadius * sqrt( 3.0f ), - aCenterPoint.y - aRadius, layerDepth ); // v1 + vertex3( aCenterPoint.x + outerRadius * sqrt( 3.0f ), + aCenterPoint.y - outerRadius, layerDepth ); // v1 setShader( SHADER_STROKED_CIRCLE, 3.0, aRadius, lineWidth ); - vertex3( aCenterPoint.x, aCenterPoint.y + aRadius * 2.0f, layerDepth ); // v2 + vertex3( aCenterPoint.x, aCenterPoint.y + outerRadius * 2.0f, layerDepth ); // v2 } return; @@ -964,6 +965,8 @@ void OPENGL_GAL::drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRa { if( isUseShader ) { + double outerRadius = aRadius + ( lineWidth / 2 ); + Save(); Translate( aCenterPoint ); Rotate( aAngle ); @@ -979,13 +982,13 @@ void OPENGL_GAL::drawStrokedSemiCircle( const VECTOR2D& aCenterPoint, double aRa v0 //__\\ v1 */ setShader( SHADER_STROKED_CIRCLE, 4.0f, aRadius, lineWidth ); - vertex3( -aRadius * 3.0f / sqrt( 3.0f ), 0.0f, layerDepth ); // v0 + vertex3( -outerRadius * 3.0f / sqrt( 3.0f ), 0.0f, layerDepth ); // v0 setShader( SHADER_STROKED_CIRCLE, 5.0f, aRadius, lineWidth ); - vertex3( aRadius * 3.0f / sqrt( 3.0f ), 0.0f, layerDepth ); // v1 + vertex3( outerRadius * 3.0f / sqrt( 3.0f ), 0.0f, layerDepth ); // v1 setShader( SHADER_STROKED_CIRCLE, 6.0f, aRadius, lineWidth ); - vertex3( 0.0f, aRadius * 2.0f, layerDepth ); // v2 + vertex3( 0.0f, outerRadius * 2.0f, layerDepth ); // v2 Restore(); } diff --git a/common/gal/opengl/shader.frag b/common/gal/opengl/shader.frag index f658fcdae5..e53c33674e 100644 --- a/common/gal/opengl/shader.frag +++ b/common/gal/opengl/shader.frag @@ -27,9 +27,9 @@ #version 120 // Shader types -const float SHADER_LINE = 1.0; -const float SHADER_FILLED_CIRCLE = 2.0; -const float SHADER_STROKED_CIRCLE = 3.0; +const float SHADER_LINE = 1.0f; +const float SHADER_FILLED_CIRCLE = 2.0f; +const float SHADER_STROKED_CIRCLE = 3.0f; varying vec4 shaderParams; varying vec2 circleCoords; @@ -45,11 +45,11 @@ void filledCircle( vec2 aCoord ) void strokedCircle( vec2 aCoord, float aRadius, float aWidth ) { - float outerRadius = aRadius; - float innerRadius = aRadius - aWidth; + float outerRadius = aRadius + ( aWidth / 2 ); + float innerRadius = aRadius - ( aWidth / 2 ); float relWidth = innerRadius / outerRadius; - if( ( dot( aCoord, aCoord ) < 1.0 ) && + if( ( dot( aCoord, aCoord ) < 1.0f ) && ( dot( aCoord, aCoord ) > relWidth * relWidth ) ) gl_FragColor = gl_Color; else diff --git a/common/gal/opengl/shader.vert b/common/gal/opengl/shader.vert index 24b12afe45..86f468b3c6 100644 --- a/common/gal/opengl/shader.vert +++ b/common/gal/opengl/shader.vert @@ -27,12 +27,12 @@ #version 120 // Shader types -const float SHADER_LINE = 1.0; -const float SHADER_FILLED_CIRCLE = 2.0; -const float SHADER_STROKED_CIRCLE = 3.0; +const float SHADER_LINE = 1.0f; +const float SHADER_FILLED_CIRCLE = 2.0f; +const float SHADER_STROKED_CIRCLE = 3.0f; // Minimum line width -const float MIN_WIDTH = 1.0; +const float MIN_WIDTH = 1.0f; attribute vec4 attrShaderParams; varying vec4 shaderParams; @@ -51,9 +51,9 @@ void main() // Make lines appear to be at least 1 pixel wide if( worldScale * lineWidth < MIN_WIDTH ) - scale = 1.0 / ( worldScale * lineWidth ); + scale = 1.0f / ( worldScale * lineWidth ); else - scale = 1.0; + scale = 1.0f; gl_Position = gl_ModelViewProjectionMatrix * ( gl_Vertex + vec4( shaderParams.yz * scale, 0.0, 0.0 ) ); @@ -81,7 +81,6 @@ void main() // Make the line appear to be at least 1 pixel wide float lineWidth = shaderParams[3]; float worldScale = gl_ModelViewMatrix[0][0]; - float scale; // Make lines appear to be at least 1 pixel width if( worldScale * lineWidth < MIN_WIDTH )