glc_shader.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00024 
00025 #ifndef GLC_SHADER_H_
00026 #define GLC_SHADER_H_
00027 
00028 #include "../glc_global.h"
00029 #include <QGLShader>
00030 #include <QGLShaderProgram>
00031 #include <QStack>
00032 #include <QFile>
00033 #include <QMutex>
00034 #include <QString>
00035 #include <QMap>
00036 
00037 #include "../glc_config.h"
00038 
00041 
00048 
00049 class GLC_LIB_EXPORT GLC_Shader
00050 {
00052 
00054 
00055 public:
00057         GLC_Shader();
00058 
00060         GLC_Shader(QFile&, QFile&);
00061 
00063         GLC_Shader(const GLC_Shader&);
00064 
00066         ~GLC_Shader();
00068 
00070 
00072 
00073 public:
00075         inline GLuint id() const
00076         {return m_ProgramShaderId;}
00077 
00079         inline bool isUsable() const
00080         {return m_ProgramShader.isLinked();}
00081 
00083         bool canBeDeleted() const;
00084 
00086         inline QString name() const
00087         {return m_Name;}
00088 
00090         inline QGLShaderProgram* programShaderHandle()
00091         {return &m_ProgramShader;}
00092 
00094         inline int positionAttributeId() const
00095         {return m_PositionAttributeId;}
00096 
00098         inline int textureAttributeId() const
00099         {return m_TextcoordAttributeId;}
00100 
00102         inline int colorAttributeId() const
00103         {return m_ColorAttributeId;}
00104 
00106         inline int normalAttributeId() const
00107         {return m_NormalAttributeId;}
00108 
00110         static int shaderCount();
00111 
00113         static bool asShader(GLC_uint shadingGroupId);
00114 
00116 
00117         static GLC_Shader* shaderHandle(GLC_uint shadingGroupId);
00118 
00120         static bool hasActiveShader();
00121 
00123 
00124         static GLC_Shader* currentShaderHandle();
00126 
00128 
00130 
00131 public:
00133         void setVertexAndFragmentShader(QFile&, QFile&);
00134 
00136         /* If this shader is usable replacing shader must be usable*/
00137         void replaceShader(const GLC_Shader&);
00138 
00140         inline GLC_Shader& operator=(const GLC_Shader& shader)
00141         {
00142                 replaceShader(shader);
00143                 return *this;
00144         }
00145 
00147         inline void setName(const QString& name)
00148         {m_Name= name;}
00149 
00151         inline int modelViewLocationId() const
00152         {return m_ModelViewLocationId;}
00153 
00155         inline int mvpLocationId() const
00156         {return m_MvpLocationId;}
00157 
00159         inline int invModelViewLocationId() const
00160         {return m_InvModelViewLocationId;}
00161 
00163         inline int enableLightingId() const
00164         {return m_EnableLightingId;}
00165 
00167         inline int lightsEnableStateId() const
00168         {return m_LightsEnableStateId;}
00169 
00171         inline int lightPositionId(GLenum lightId) const
00172         {return m_LightsPositionId.value(lightId);}
00173 
00175         inline int lightAmbientColorId(GLenum lightId) const
00176         {return m_LightsAmbientColorId.value(lightId);}
00177 
00179         inline int lightDiffuseColorId(GLenum lightId) const
00180         {return m_LightsDiffuseColorId.value(lightId);}
00181 
00183         inline int lightSpecularColorId(GLenum lightId) const
00184         {return m_LightsSpecularColorId.value(lightId);}
00185 
00187         inline int lightSpotDirectionId(GLenum lightId) const
00188         {return m_LightsSpotDirectionId.value(lightId);}
00189 
00191         inline int lightAttebuationFactorsId(GLenum lightId) const
00192         {return m_LightsAttenuationFactorsId.value(lightId);}
00193 
00195         inline int lightSpotExponentId(GLenum lightId) const
00196         {return m_LightsSpotExponentId.value(lightId);}
00197 
00199         inline int lightSpotCutoffId(GLenum lightId) const
00200         {return m_LightsSpotCutoffAngleId.value(lightId);}
00201 
00203         inline int lightComputeDistanceAttenuationId(GLenum lightId) const
00204         {return m_LightsComputeDistanceAttenuationId.value(lightId);}
00205 
00207 
00209 
00211 
00212 public:
00214 
00215         void use();
00216 
00218 
00219         static bool use(GLC_uint ShadingGroupId);
00220 
00222         static void unuse();
00223 
00225 
00226         void createAndCompileProgrammShader();
00227 
00229         void deleteShader();
00231 
00233 // private services function
00235 private:
00237         void initLightsUniformId();
00239 // private members
00241 private:
00243         static QStack<GLC_uint> m_ShadingGroupStack;
00244 
00246         static GLC_uint m_CurrentShadingGroupId;
00247 
00249         static QHash<GLC_uint, GLC_Shader*> m_ShaderProgramHash;
00250 
00252         QGLShader m_VertexShader;
00253 
00255         QGLShader m_FragmentShader;
00256 
00258         QGLShaderProgram m_ProgramShader;
00259 
00261         GLC_uint m_ProgramShaderId;
00262 
00264         QString m_Name;
00265 
00267         int m_PositionAttributeId;
00268 
00270         int m_TextcoordAttributeId;
00271 
00273         int m_ColorAttributeId;
00274 
00276         int m_NormalAttributeId;
00277 
00279         int m_ModelViewLocationId;
00280 
00282         int m_MvpLocationId;
00283 
00285         int m_InvModelViewLocationId;
00286 
00288         int m_EnableLightingId;
00289 
00291         int m_LightsEnableStateId;
00292 
00294         QMap<GLenum, int> m_LightsPositionId;
00295 
00297         QMap<GLenum, int> m_LightsAmbientColorId;
00298 
00300         QMap<GLenum, int> m_LightsDiffuseColorId;
00301 
00303         QMap<GLenum, int> m_LightsSpecularColorId;
00304 
00306         QMap<GLenum, int> m_LightsSpotDirectionId;
00307 
00309         QMap<GLenum, int> m_LightsAttenuationFactorsId;
00310 
00312         QMap<GLenum, int> m_LightsSpotExponentId;
00313 
00315         QMap<GLenum, int> m_LightsSpotCutoffAngleId;
00316 
00318         QMap<GLenum, int> m_LightsComputeDistanceAttenuationId;
00319 
00320 };
00321 
00322 #endif /*GLC_SHADER_H_*/

SourceForge.net Logo

©2005-2011 Laurent Ribon