From 978b548c64f990dec483426ec89405566a993f66 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Mon, 24 Jun 2013 15:40:31 +0200 Subject: [PATCH] Small improvements to SHADER class. --- common/gal/opengl/shader.cpp | 31 ++++++++++--------------------- include/gal/opengl/shader.h | 34 +++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/common/gal/opengl/shader.cpp b/common/gal/opengl/shader.cpp index 6cbd84850d..83eab3d2a7 100644 --- a/common/gal/opengl/shader.cpp +++ b/common/gal/opengl/shader.cpp @@ -35,13 +35,14 @@ using namespace KiGfx; -SHADER::SHADER() +SHADER::SHADER() : + isProgramCreated( false ), + isShaderLinked( false ), + maximumVertices( 4 ), + active( false ), + geomInputType( GL_LINES ), + geomOutputType( GL_LINES ) { - isProgramCreated = false; - isShaderLinked = false; - maximumVertices = 4; - geomInputType = GL_LINES; - geomOutputType = GL_LINES; } @@ -108,7 +109,7 @@ std::string SHADER::ReadSource( std::string aShaderSourceName ) } -void SHADER::AddSource( std::string aShaderSourceName, ShaderType aShaderType ) +void SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShaderType ) { if( isShaderLinked ) { @@ -195,19 +196,7 @@ bool SHADER::Link() } -void SHADER::Use() -{ - glUseProgram( programNumber ); -} - - -void SHADER::Deactivate() -{ - glUseProgram( 0 ); -} - - -void SHADER::AddParameter( std::string aParameterName ) +void SHADER::AddParameter( const std::string& aParameterName ) { GLint location = glGetUniformLocation( programNumber, aParameterName.c_str() ); @@ -224,7 +213,7 @@ void SHADER::SetParameter( int parameterNumber, float value ) } -int SHADER::GetAttribute( std::string aAttributeName ) +int SHADER::GetAttribute( std::string aAttributeName ) const { return glGetAttribLocation( programNumber, aAttributeName.c_str() ); } diff --git a/include/gal/opengl/shader.h b/include/gal/opengl/shader.h index 71fa03109f..115fa02e49 100644 --- a/include/gal/opengl/shader.h +++ b/include/gal/opengl/shader.h @@ -76,23 +76,42 @@ public: * @param aShaderSourceName is the shader source file name. * @param aShaderType is the type of the shader. */ - void AddSource( std::string aShaderSourceName, ShaderType aShaderType ); + void AddSource( const std::string& aShaderSourceName, ShaderType aShaderType ); /** - * Link the shaders. + * @brief Link the shaders. + * * @return true in case of success, false otherwise. */ bool Link(); /** - * Use the shader. + * @brief Use the shader. */ - void Use(); + inline void Use() + { + glUseProgram( programNumber ); + active = true; + } /** * @brief Deactivate the shader and use the default OpenGL program. */ - void Deactivate(); + inline void Deactivate() + { + glUseProgram( 0 ); + active = false; + } + + /** + * @brief Returns the current state of the shader. + * + * @return True if any of shaders is enabled. + */ + inline bool IsActive() const + { + return active; + } /** * @brief Configure the geometry shader - has to be done before linking! @@ -113,7 +132,7 @@ public: * * @param aParameterName is the name of the parameter. */ - void AddParameter( std::string aParameterName ); + void AddParameter( const std::string& aParameterName ); /** * @brief Set a parameter of the shader. @@ -129,7 +148,7 @@ public: * @param aAttributeName is the name of the attribute. * @return the location. */ - int GetAttribute( std::string aAttributeName ); + int GetAttribute( std::string aAttributeName ) const; private: @@ -152,6 +171,7 @@ private: GLuint programNumber; ///< Shader program number bool isProgramCreated; ///< Flag for program creation bool isShaderLinked; ///< Is the shader linked? + bool active; ///< Is any of shaders used? GLuint maximumVertices; ///< The maximum of vertices to be generated GLuint geomInputType; ///< Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.] GLuint geomOutputType; ///< Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.]