![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsGLARBVertexBufferObject.hpp 00003 00004 Based on 3DLabs Inc. Ltd. and aGLSL.h by Martin Christen 00005 00006 SUKITTI PUNAK (06/08/2005) 00007 UPDATE (09/10/2010) 00008 ******************************************************************************/ 00009 #ifndef TAPs_GLARBVERTEXBUFFEROBJECT_HPP 00010 #define TAPs_GLARBVERTEXBUFFEROBJECT_HPP 00011 00012 //----------------------------------------------------------------------------- 00013 #ifndef TAPs_USE_GLSL 00014 #define TAPs_USE_GLSL 00015 #endif 00016 //----------------------------------------------------------------------------- 00017 // Operating System Check 00018 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) 00019 #define TAPs_GLSL_WINDOWS 00020 #include <windows.h> 00021 #elif defined(__MACOSX__) 00022 #define TAPs_GLSL_MACOSX 00023 #error "MacOSX is not supported!" 00024 #elif defined(linux) || defined(__linux) || defined(__linux__) 00025 #define TAPs_GLSL_LINUX 00026 #error "LINUX is not supported!" 00027 #else 00028 #error "This OS is not supported!" 00029 #endif 00030 //----------------------------------------------------------------------------- 00031 //#include "TAPs_GL_Ext.hpp" 00032 00033 //#include "../Core/TAPsLib.hpp" 00034 00035 #define GLEW_STATIC 1 00036 #include <GL/glew.h> 00037 #include <GL/wglew.h> 00038 #include <GL/gl.h> 00039 #include <GL/glext.h> 00040 //#include <GL/glut.h> 00041 //#include <GL/glu.h> 00042 //#include <GL/wglext.h> 00043 00044 #include "../Core/TAPsLib.hpp" 00045 00046 #include <iostream> 00047 #include <fstream> 00048 #include <vector> 00049 00050 00051 #ifdef WIN32 /*[*/ 00052 #include <io.h> 00053 #endif /*]*/ 00054 00055 #include <stdlib.h> 00056 #include <stdio.h> 00057 #include <string.h> 00058 #include <fcntl.h> 00059 00060 // For wxWidgets 00061 #ifdef TAPs_USE_WXWIDGETS 00062 #include <wx/wx.h> 00063 #endif 00064 00065 //----------------------------------------------------------------------------- 00066 BEGIN_NAMESPACE_TAPs__OpenGL 00067 //============================================================================= 00068 //----------------------------------------------------------------------------- 00069 // Useful Macros 00070 /* 00071 #ifdef TAPs_GLSL_WINDOWS 00072 #define MacrosLoadExtension( fnType, fnName ) ( ( fnName = (fnType) wglGetProcAddress( #fnName ) ) == NULL ) 00073 #endif 00074 #ifdef TAPs_GLSL_LINUX 00075 #define MacrosLoadExtension( fnType, fnName ) ( ( fnName = (fnType) glXGetProcAddress( #fnName ) ) == NULL ) 00076 #endif 00077 bool GFnTestExtension( const char * extName ); 00078 */ 00079 //============================================================================= 00080 class GLSLShader 00081 { 00082 //---------------------------------------------------------------- 00083 friend class GLSLProgramObject; // i.e. GLSL Program 00084 //---------------------------------------------------------------- 00085 public: 00086 //---------------------------------------------------------------- 00087 GLSLShader (); 00088 ~GLSLShader (); 00089 int Load ( char *fileName ); // read file returns 00090 // 0 if everything is ok, 00091 // -1 if file not found, 00092 // -2 if file is empty 00093 // -3 if no memory 00094 // Load shader from char array, make sure shader is 0 terminated! 00095 void LoadFromMemory ( const char *shader ); 00096 bool Compile (); // compile shader 00097 char * GetCompilerLog (); // get compiler log messages 00098 GLint GetAttribLoc ( char * cpAttribName ); // get localtion of the attribute 00099 //---------------------------------------------------------------- 00100 protected: 00101 //---------------------------------------------------------------- 00102 // Data Members 00103 int m_iShaderType; // 1 = Vertex Shader; 2 = Fragment Shader; else NONE 00104 GLuint m_uiShaderObject; // shader object 00105 GLchar * m_cpShaderSource; // ASCII source code 00106 bool m_bCompileStatus; // true if shader is compiled 00107 GLchar * m_cpCompilerLog; // compiler log message 00108 bool m_bMemAlloc; // true if shader allocated memory 00109 //---------------------------------------------------------------- 00110 }; 00111 //============================================================================= 00112 class GLSLVertexShader : public GLSLShader 00113 { 00114 public: 00115 GLSLVertexShader (); 00116 ~GLSLVertexShader (); 00117 }; 00118 //============================================================================= 00119 class GLSLFragmentShader : public GLSLShader 00120 { 00121 public: 00122 GLSLFragmentShader (); 00123 ~GLSLFragmentShader (); 00124 }; 00125 //============================================================================= 00126 // GLSL Program 00127 class GLSLProgramObject 00128 { 00129 public: 00130 //---------------------------------------------------------------- 00131 GLSLProgramObject (); 00132 ~GLSLProgramObject (); 00133 //---------------------------------------------------------------- 00134 // Add a vertex or fragment shader 00135 void AddShader ( GLSLShader * glslShader ); 00136 //---------------------------------------------------------------- 00137 bool Link (); // link all shaders 00138 char * GetLinkerLog (); // get linker messages 00139 //---------------------------------------------------------------- 00140 // Begin this shader. OpenGL calls will go through shader 00141 void BeginGLSL (); 00142 // Stop using this shader. OpenGL calls will go through regular pipeline. 00143 void EndGLSL (); 00144 //---------------------------------------------------------------- 00145 bool IsGLSLEnabled (); // returns true if GLSL is enabled 00146 //---------------------------------------------------------------- 00147 // Set Uniform Variables to GLSL Shader 00148 bool SetUniform1f ( char * var, GLfloat x ); 00149 bool SetUniform2f ( char * var, GLfloat x, GLfloat y ); 00150 bool SetUniform3f ( char * var, GLfloat x, GLfloat y, GLfloat z ); 00151 bool SetUniform4f ( char * var, GLfloat x, GLfloat y, GLfloat z, GLfloat w ); 00152 //----------------------- 00153 bool SetUniform1fv ( char * var, GLsizei count, GLfloat * val ); 00154 bool SetUniform2fv ( char * var, GLsizei count, GLfloat * val ); 00155 bool SetUniform3fv ( char * var, GLsizei count, GLfloat * val ); 00156 bool SetUniform4fv ( char * var, GLsizei count, GLfloat * val ); 00157 //----------------------- 00158 bool SetUniform1i ( char * var, GLint x ); 00159 bool SetUniform2i ( char * var, GLint x, GLint y ); 00160 bool SetUniform3i ( char * var, GLint x, GLint y, GLint z ); 00161 bool SetUniform4i ( char * var, GLint x, GLint y, GLint z, GLint w ); 00162 //----------------------- 00163 bool SetUniform1iv ( char * var, GLsizei count, GLint * val ); 00164 bool SetUniform2iv ( char * var, GLsizei count, GLint * val ); 00165 bool SetUniform3iv ( char * var, GLsizei count, GLint * val ); 00166 bool SetUniform4iv ( char * var, GLsizei count, GLint * val ); 00167 //----------------------- 00168 bool SetUniformMatrix2fv ( char * var, GLsizei count, GLboolean transpose, GLfloat * val ); 00169 bool SetUniformMatrix3fv ( char * var, GLsizei count, GLboolean transpose, GLfloat * val ); 00170 bool SetUniformMatrix4fv ( char * var, GLsizei count, GLboolean transpose, GLfloat * val ); 00171 //---------------------------------------------------------------- 00172 // Get Uniform Variables from GLSL Shader 00173 void GetUniformfv ( char * var, GLfloat * val ); 00174 void GetUniformiv ( char * var, GLint * val ); 00175 //---------------------------------------------------------------- 00176 // Set Vertex Attributes to GLSL Shader 00177 bool SetVertexAttrib1f ( GLuint index, GLfloat x ); 00178 bool SetVertexAttrib2f ( GLuint index, GLfloat x, GLfloat y ); 00179 bool SetVertexAttrib3f ( GLuint index, GLfloat x, GLfloat y, GLfloat z ); 00180 bool SetVertexAttrib4f ( GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w ); 00181 //---------------------------------------------------------------- 00182 void MemageMemory () { m_bManageMemory = true; } 00183 //---------------------------------------------------------------- 00184 // Turn On/Off All Shaders 00185 static void UseShader ( bool b ) { m_stcbShaderActive = b; } 00186 //---------------------------------------------------------------- 00187 private: 00188 //---------------------------------------------------------------- 00189 // Member Functions 00190 GLint GetUniLoc( const GLchar *name ); // get location of the uniform variable 00191 //---------------------------------------------------------------- 00192 // Data Members 00193 GLuint m_uiProgramObject; // shader object 00194 bool m_bLinkStatus; // true if shader is linked 00195 GLchar * m_cpLinkerLog; // linker log message 00196 std::vector< GLSLShader * > m_vShaderList; // list of all shaders 00197 bool m_bManageMemory; // true if shader manages memory 00198 static bool m_stcbShaderActive; // true if shader is inactive 00199 //---------------------------------------------------------------- 00200 }; 00201 //============================================================================= 00202 // To simplify the process loading/compiling/linking shaders, this 00203 // high level interface to setup a vertex/fragment shader has been created. 00204 class GLSLShaderManager 00205 { 00206 public: 00207 GLSLShaderManager (); 00208 ~GLSLShaderManager (); 00209 //--------------------------------------------------------------- 00210 // Load vertex/fragment shader from file 00211 GLSLProgramObject * LoadFromFile ( char * vertFile, char * fragFile ); 00212 //--------------------------------------------------------------- 00213 // Load vertex/fragment shader from memory 00214 GLSLProgramObject * LoadFromMemory ( const char * vertMem, const char * fragMem ); 00215 //--------------------------------------------------------------- 00216 // Delete a program object 00217 bool Delete ( GLSLProgramObject * obj ); 00218 private: 00219 std::vector< GLSLProgramObject * > m_vProgramObjectList; 00220 //=============================================================== 00221 //--------------------------------------------------------------- 00222 // For Attributes / Uniform Variables 00223 //=========================================== 00224 // Uniform Variables 00225 //------------------------------------------- 00226 // Scalars 00227 std::vector< float > m_f1Uniform; 00228 std::vector< int > m_i1Uniform; 00229 std::vector< bool > m_b1Uniform; 00230 //------------------------------------------- 00231 // Vectors 00232 std::vector< Vector2<float> > m_f2Uniform; 00233 std::vector< Vector2<int> > m_i2Uniform; 00234 std::vector< Vector2<bool> > m_b2Uniform; 00235 std::vector< Vector3<float> > m_f3Uniform; 00236 std::vector< Vector3<int> > m_i3Uniform; 00237 std::vector< Vector3<bool> > m_b3Uniform; 00238 std::vector< Vector4<float> > m_f4Uniform; 00239 std::vector< Vector4<int> > m_i4Uniform; 00240 std::vector< Vector4<bool> > m_b4Uniform; 00241 //------------------------------------------- 00242 // Matrices 00243 std::vector< ColMatrix2x2<float> > m_m2x2Uniform; 00244 std::vector< ColMatrix3x3<float> > m_m3x3Uniform; 00245 std::vector< ColMatrix4x4<float> > m_m4x4Uniform; 00246 //------------------------------------------- 00247 // Samplers 00248 // -------- 00249 // sampler1D Access a 1D texture 00250 // sampler2D Access a 2D texture 00251 // sampler3D Access a 3D texture 00252 // samplerCube Access a cube-map texture 00253 // sampler1DShadow Access a 1D depth texture with comparison 00254 // sampler2DShadow Access a 2D depth texture with comparison 00255 //------------------------------------------- 00256 //=========================================== 00257 // Attributes 00258 //------------------------------------------- 00259 // Scalars 00260 std::vector< short > m_s1VertexAttrib; 00261 std::vector< float > m_f1VertexAttrib; 00262 std::vector< double > m_d1VertexAttrib; 00263 //------------------------------------------- 00264 // 2D Vectors 00265 std::vector< Vector2<short> > m_s2VertexAttrib; 00266 std::vector< Vector2<float> > m_f2VertexAttrib; 00267 std::vector< Vector2<double> > m_d2VertexAttrib; 00268 // 3D Vectors 00269 std::vector< Vector3<short> > m_s3VertexAttrib; 00270 std::vector< Vector3<float> > m_f3VertexAttrib; 00271 std::vector< Vector3<double> > m_d3VertexAttrib; 00272 // 4D Vectors 00273 std::vector< Vector4<short> > m_s4VertexAttrib; 00274 std::vector< Vector4<float> > m_f4VertexAttrib; 00275 std::vector< Vector4<double> > m_d4VertexAttrib; 00276 std::vector< Vector4<char> > m_b4VertexAttrib; 00277 std::vector< Vector4<int> > m_i4VertexAttrib; 00278 std::vector< Vector4<unsigned char> > m_ub4VertexAttrib; 00279 std::vector< Vector4<unsigned short> > m_us4VertexAttrib; 00280 std::vector< Vector4<unsigned int> > m_ui4VertexAttrib; 00281 //--------------------------------------------------------------- 00282 //=============================================================== 00283 //------------------------------------------------------------------------- 00284 }; // End Class GLSLShaderManager 00285 //============================================================================= 00286 // Classless functions to initialize OpenGL Extensions and check for GLSL and 00287 // OpenGL Version 2 00288 bool GFnInitGLSL (); 00289 bool GFnCheckGLSL (); 00290 bool GFnCheckGL2 (); 00291 //============================================================================= 00292 END_NAMESPACE_TAPs__OpenGL 00293 //----------------------------------------------------------------------------- 00294 // Include definition if TAPs_USE_EXPORT is not defined 00295 #if !defined( TAPs_USE_EXPORT ) 00296 #include "TAPsGLSLFns.cpp" 00297 #endif 00298 //----------------------------------------------------------------------------- 00299 #endif 00300 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00301 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8