shader.h

00001 /*--License:
00002         Lilith 3D Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2002-2007
00004         www.grinninglizard.com/lilith
00005 
00006         This program is free software; you can redistribute it and/or
00007         modify it under the terms of the GNU General Public License
00008         as published by the Free Software Foundation; either version 2
00009         of the License, or (at your option) any later version.
00010 
00011         This program is distributed in the hope that it will be useful,
00012         but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License
00017         along with this program; if not, write to the Free Software
00018         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020         The full text of the license can be found in license.txt
00021 */
00022 
00023 #ifndef LILITH_SHADER_INCLUDED
00024 #define LILITH_SHADER_INCLUDED
00025 
00026 #include "glew.h"
00027 #include "../grinliz/gltypes.h"
00028 #include "../grinliz/glvector.h"
00029 #include "../grinliz/glcolor.h"
00030 #include "../grinliz/glmatrix.h"
00031 #include <string>
00032 #include <map>
00033 
00034 namespace lilith3d
00035 {
00036 
00037 class Texture;
00038 class ShaderManager;
00039 
00040 struct Shader
00041 {
00042         GLhandleARB vertex;
00043         GLhandleARB fragment;
00044         GLhandleARB program;
00045 };
00046 
00050 class ShaderManager
00051 {
00052   public:
00053         enum {
00054                 TERRAIN_SHADER,
00055                 TERRAIN_SHADOW_SHADER,
00056                 WATER_SHADER,
00057                 FOLIAGE_SHADER,
00058                 DIFFUSE_SHADER,
00059                 DIFFUSE_SHADOW_SHADER,
00060                 TREE_SHADER,
00061                 TREE_BB_SHADER,
00062                 COLORFACET_SHADER,
00063 
00064                 SHADER_COUNT,
00065         };
00066 
00068         ShaderManager();
00070         ~ShaderManager();
00071 
00072         static int MaxTextures();
00074         static ShaderManager* Instance()        { return instance; }
00075 
00077         bool PowerFragments()   { return powerFrag; }
00078 
00080         void    SetImportPath( const std::string& path )        { importPath = path; }
00082         bool    LoadRequiredShaders();
00083 
00085         const Shader* GetShader( int id ) { GLASSERT( id >= 0 && id < SHADER_COUNT );   
00086                                                                                          return &shaderGroup[id]; }
00087 
00089         void    UseShader( const Shader* group );
00090 
00091         const Shader* CurrentShader()   { return currentShader; }
00092 
00096         void    BindTextures( const Shader* group, const Texture* textures[], int numTextures );
00097 
00099         void    UnBindTextures( int startIndex, int count );
00100 
00102         void    SetUniform( const Shader* group, const char* name, float value );
00104         void    SetUniform( const Shader* group, const char* name, const grinliz::Vector3F& value );
00106         void    SetUniform( const Shader* group, const char* name, const grinliz::Vector4F& value );
00108         void    SetUniform( const Shader* group, const char* name, const grinliz::Color3F& value );
00110         void    SetUniform( const Shader* group, const char* name, const grinliz::Color4F& value );
00112         void    SetUniform( const Shader* group, const char* name, const grinliz::Matrix4& value );
00113 
00115         void    ClearShaders();
00116 
00117         void    PrintInfoLog( GLhandleARB obj );
00118         void    CheckError( GLhandleARB object );
00119 
00120   private:
00121         const Shader*   LoadShader( Shader* group,
00122                                                                         const char* filenameVertex, 
00123                                                                         const char* filenameFragment );
00124 
00125         GLhandleARB             LoadShader( GLhandleARB program, const char* filename, int type );
00126         
00127         bool PowerFragmentProgram();
00128         static ShaderManager* instance;
00129 
00130         Shader shaderGroup[ SHADER_COUNT ];
00131 
00132         std::string importPath;
00133         const Shader* currentShader;
00134         bool powerFrag;
00135 
00136         // Constant shaders linked to all loaded shaders:
00137         std::string constantText;
00138         std::string utilityFragment;
00139         std::string utilityVertex;
00140 };
00141 
00142 };      // namespace lilith3d
00143 
00144 #endif

Generated on Fri Mar 23 19:36:22 2007 for Lilith3D by  doxygen 1.5.1-p1