Commit 28511cf4 authored by Maciej Suminski's avatar Maciej Suminski
Browse files

Introducing shaders.

Shader's parameters are stored in VBO_ITEM. Changed VBO_ITEM data structure. Added UseShader() function for selecting shader for a given VBO_ITEM.
Added one main vertex & fragment shader program to be used for with all kinds of items (type of shader is selected using attributes that are stored in VBO). Currently available shaders are: at-least-1px-width line, filled circle and stroked circle.
Removed unnecessary param (aDepthOffset) from a few functions (OPENGL_GAL::drawSemiCircle(), OPENGL_GAL::drawLineCap()). Removed function OPENGL_GAL::DrawRoundedSegment(). Changed some asserts to debug info or error log.
parent ad5d10a8
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -58,9 +58,9 @@ EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin
    wxStandardPaths paths;
    wxFileName executableFile( paths.GetExecutablePath() );
    m_galShaderPath = std::string( ( executableFile.GetPath() +
                                   wxT( "/../../common/gal/opengl/shader/" ) ).mb_str() );
                                   wxT( "/../../common/gal/opengl/" ) ).mb_str() );

    SwitchBackend( aGalType, false );
    SwitchBackend( aGalType, true );
    SetBackgroundStyle( wxBG_STYLE_CUSTOM );

    // Initial display settings
+303 −249

File changed.

Preview size limit exceeded, changes collapsed.

+38 −7
Original line number Diff line number Diff line
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 2013 Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 * Copyright (C) 2013 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * Fragment shader
 *
@@ -24,18 +25,48 @@
 */

#version 120
//#pragma debug(on)

varying float aspect;
// Shader types
const float SHADER_LINE                 = 1.0;
const float SHADER_FILLED_CIRCLE        = 2.0;
const float SHADER_STROKED_CIRCLE       = 3.0;

void main()
varying in vec4 shaderParams;

void filledCircle( vec2 aCoord )
{
    vec2 v = abs( gl_TexCoord[0].xy - vec2( 0.5, 0.5 ) ) * 2.0 - vec2( aspect, 0.0 );
    vec2 d = vec2( v.x / ( 1.0 - aspect ), v.y );
    if( dot( aCoord, aCoord ) < 1.0 )
        gl_FragColor = gl_Color;
    else
        discard;
}

    if( v.x <= 0.0 || (dot( d, d ) < 1.0 ) )

void strokedCircle( vec2 aCoord, float aWidth )
{
    if( ( dot( aCoord, aCoord ) < 1.0 ) && 
        ( dot( aCoord, aCoord ) > aWidth * aWidth ) )
        gl_FragColor = gl_Color;
    else
        discard;
}

    // gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);

void main()
{
    if( shaderParams[0] == SHADER_FILLED_CIRCLE )
    {
        filledCircle( vec2( shaderParams[1], shaderParams[2] ) );
    }
    else if( shaderParams[0] == SHADER_STROKED_CIRCLE )
    {
        strokedCircle( vec2( shaderParams[1], shaderParams[2] ), shaderParams[3] );
    }
    else
    {
        // Simple pass-through
        gl_FragColor = gl_Color;
    }
}
+35 −5
Original line number Diff line number Diff line
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
 * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
 * Copyright (C) 2013 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * Vertex shader
 *
@@ -24,12 +24,42 @@
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

// This shader requires GLSL 1.2
#version 120

// Shader types
const float SHADER_LINE                 = 1.0;
const float SHADER_FILLED_CIRCLE        = 2.0;
const float SHADER_STROKED_CIRCLE       = 3.0;

attribute   vec4 attrShaderParams;
varying out vec4 shaderParams;

void main()
{
    // Simple pass-through
    gl_Position   = gl_Vertex;
    // Pass attributes to the fragment shader
    shaderParams = attrShaderParams;
    
    if( shaderParams[0] == SHADER_LINE )
    {
        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 < 1.0 )
            scale = 1.0 / ( worldScale * lineWidth );
        else
            scale = 1.0;
        
        gl_Position = gl_ModelViewProjectionMatrix * 
            ( gl_Vertex + vec4( shaderParams.yz * scale, 0.0, 0.0 ) );
    }
    else
    {
        // Pass through the coordinates like in the fixed pipeline
        gl_Position = ftransform();
    }
    
    gl_FrontColor = gl_Color;
}
+0 −60
Original line number Diff line number Diff line
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 2012 Torsten Hueter, torstenhtr <at> gmx.de
 * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
 *
 * Fragment shader
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

// This shader requires GLSL 1.2
#version 120

// Input variables
flat varying vec4 center_;
flat varying vec2 radius_;
flat varying vec4 colorA_;
flat varying vec4 colorB_;

void main( void )
{
    // Compute the distance from the circle edge
    float   distA   = distance( center_, gl_FragCoord ) - radius_.y;
    float   distB   = radius_.x - distance( center_, gl_FragCoord );

    // Limit the range to [ 0 .. 1 ]
    if( distA < 0 ) distA = 0;
    if( distA > 1 ) distA = 1;
    if( distB < 0 ) distB = 0;
    if( distB > 1 ) distB = 1;

    // Points with a larger distance from the edge are set deeper
    gl_FragDepth = gl_FragCoord.z + distA * 0.001 + distB * 0.001;

    // Compute the color
    vec4 color;
    color.r = colorA_.r;
    color.g = colorA_.g;
    color.b = colorA_.b;
    color.a = colorA_.a * ( 1 - distA ) * ( 1 - distB );

    // Now output the edge fragment color
    gl_FragColor = color;
}
Loading