Changeset 9818 in orxonox.OLD for branches/new_class_id/src/lib
- Timestamp:
- Sep 25, 2006, 12:10:07 PM (18 years ago)
- Location:
- branches/new_class_id/src/lib/graphics
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/new_class_id/src/lib/graphics/Makefile.am
r9806 r9818 9 9 light.cc \ 10 10 shader.cc \ 11 shader_data.cc \ 11 12 \ 12 13 render2D/render_2d.cc \ -
branches/new_class_id/src/lib/graphics/shader.cc
r9806 r9818 74 74 75 75 76 76 Shader::Shader() 77 : data(new ShaderData) 78 { 79 80 } 77 81 78 82 /** … … 80 84 */ 81 85 Shader::Shader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile) 86 : data(new ShaderData) 82 87 { 83 this->registerObject(this, Shader::_objectList);84 this->shaderProgram = 0;85 this->vertexShader = 0;86 this->fragmentShader = 0;88 if (!Shader::checkShaderAbility()) 89 PRINTF(2)("Your system does not support shaders\n"); 90 else 91 this->load(vertexShaderFile, fragmentShaderFile); 87 92 88 if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100)89 {90 this->shaderProgram = glCreateProgramObjectARB();91 92 if (!vertexShaderFile.empty())93 this->loadShaderProgramm(Shader::Vertex, vertexShaderFile);94 if (!fragmentShaderFile.empty())95 this->loadShaderProgramm(Shader::Fragment, fragmentShaderFile);96 97 this->linkShaderProgram();98 99 }100 else101 {102 PRINTF(2)("Shaders are not supported on your hardware\n");103 }104 93 } 105 94 95 const Shader* Shader::storedShader = NULL; 106 96 107 /** 108 * standard deconstructor 109 */ 110 Shader::~Shader () 97 void Shader::activateShader() const 111 98 { 112 if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB)) 113 Shader::deactivateShader(); 114 115 // delete what has to be deleted here 116 this->deleteProgram(Shader::Vertex); 117 this->deleteProgram(Shader::Fragment); 118 119 if (this->fragmentShader != 0) 99 if (likely (this->getProgram() != 0)) 120 100 { 121 glDetachObjectARB(this->shaderProgram, this->fragmentShader); 122 glDeleteObjectARB(this->fragmentShader); 123 } 124 if (this->vertexShader != 0) 125 { 126 glDetachObjectARB(this->shaderProgram, this->vertexShader); 127 glDeleteObjectARB(this->vertexShader); 128 } 129 if (this->shaderProgram != 0) 130 { 131 GLint status = 0; 132 //glLinkProgramARB(this->shaderProgram); 133 glDeleteObjectARB(this->shaderProgram); 134 // link error checking 135 glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_DELETE_STATUS_ARB, &status); 136 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) 137 this->printError(this->shaderProgram); 138 } 139 } 140 141 Shader* Shader::storedShader = NULL; 142 143 144 bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName) 145 { 146 GLhandleARB shader = 0; 147 148 if (type != Shader::Vertex && type != Shader::Fragment) 149 return false; 150 this->deleteProgram(type); 151 152 153 std::string program; 154 if (!readShader(fileName, program)) 155 return false; 156 157 if (type == Shader::Vertex && GLEW_ARB_vertex_shader) 158 { 159 this->vertexShaderFile = fileName; 160 161 shader = this->vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); 162 } 163 164 if (type == Shader::Fragment && GLEW_ARB_fragment_shader) 165 { 166 this->fragmentShaderFile = fileName; 167 168 shader = this->fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); 169 } 170 171 if (shader != 0) 172 { 173 GLint status = 0; 174 const char* prog = program.c_str(); 175 176 glShaderSourceARB(shader, 1, &prog, NULL); 177 glCompileShaderARB(shader); 178 // checking on error. 179 glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status); 180 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) 181 this->printError(shader); 182 else 183 glAttachObjectARB(this->shaderProgram, shader); 184 } 185 return true; 186 } 187 188 189 void Shader::linkShaderProgram() 190 { 191 GLint status = 0; 192 193 glLinkProgramARB(this->shaderProgram); 194 // link error checking 195 glGetObjectParameterivARB(this->shaderProgram, GL_OBJECT_LINK_STATUS_ARB, &status); 196 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) 197 this->printError(this->shaderProgram); 198 } 199 200 201 bool Shader::readShader(const std::string& fileName, std::string& output) 202 { 203 char lineBuffer[PARSELINELENGTH]; 204 205 std::ifstream shader; 206 shader.open(fileName.c_str()); 207 if (!shader.is_open()) 208 return false; 209 210 211 while (!shader.eof()) 212 { 213 shader.getline(lineBuffer, PARSELINELENGTH); 214 output += lineBuffer; 215 output += "\n"; 216 } 217 218 219 shader.close(); 220 return true; 221 } 222 223 224 225 void Shader::activateShader() 226 { 227 if (likely (this->shaderProgram != 0)) 228 { 229 glUseProgramObjectARB(this->shaderProgram); 101 glUseProgramObjectARB(this->getProgram()); 230 102 Shader::storedShader = this; 231 103 } 232 104 } 233 234 void Shader::bindShader(const char* name, const float* value, size_t size)235 {236 if (likely (this->shaderProgram != 0))237 {238 glUseProgramObjectARB(this->shaderProgram);239 240 unsigned int location = glGetUniformLocationARB(this->shaderProgram, name);241 /* This is EXPENSIVE and should be avoided. */242 243 if (size == 1) glUniform1fvARB(location, 1, value);244 else if (size == 2) glUniform2fvARB(location, 1, value);245 else if (size == 3) glUniform3fvARB(location, 1, value);246 else if (size == 4) glUniform4fvARB(location, 1, value);247 else if (size == 9) glUniformMatrix3fvARB(location, 1, false, value);248 else if (size == 16) glUniformMatrix4fvARB(location, 1, false, value);249 250 }251 }252 253 105 void Shader::deactivateShader() 254 106 { … … 256 108 glUseProgramObjectARB(0); 257 109 Shader::storedShader = NULL; 258 }259 260 261 void Shader::deleteProgram(Shader::Type type)262 {263 GLint status = 0;264 if (type == Shader::Vertex && this->vertexShader != 0)265 {266 this->vertexShaderFile = "";267 glDetachObjectARB(this->shaderProgram, this->vertexShader);268 glDeleteObjectARB(this->vertexShader);269 glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status);270 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)271 Shader::printError(this->vertexShader);272 this->vertexShader = 0;273 }274 else if (type == Shader::Fragment && this->fragmentShader != 0)275 {276 this->fragmentShaderFile = "";277 glDetachObjectARB(this->shaderProgram, this->fragmentShader);278 glDeleteObjectARB(this->fragmentShader);279 glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status);280 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION)281 Shader::printError(this->fragmentShader);282 this->fragmentShader = 0;283 }284 else285 return;286 }287 288 289 void Shader::printError(GLhandleARB program)290 {291 if (program == 0)292 return;293 294 int infologLength = 0;295 int charsWritten = 0;296 char *infoLog;297 298 glGetObjectParameterivARB(program, GL_OBJECT_INFO_LOG_LENGTH_ARB,299 &infologLength);300 301 if (infologLength > 0)302 {303 infoLog = new char[infologLength+1];304 glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);305 printf("%s\n", infoLog);306 delete[] infoLog;307 }308 110 } 309 111 … … 319 121 } 320 122 321 void Shader::debug() const322 {323 PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram);324 if (this->vertexShader != 0)325 {326 /* PRINT(3)("VertexShaderProgramm: number=%d, file='%s'\n", this->vertexShader, this->vertexShaderFile);327 if (this->vertexShaderSource != NULL)328 for (unsigned int i = 0; i < this->vertexShaderSource->getCount(); i++)329 PRINT(3)("%d: %s\n", i, this->vertexShaderSource->getEntry(i));330 }331 if (this->fragmentShader != 0)332 {333 PRINT(3)("FragmentShaderProgramm: number=%d, file='%s'\n", this->fragmentShader, this->fragmentShaderFile);334 if (this->fragmentShaderSource != NULL)335 for (unsigned int i = 0; i < this->fragmentShaderSource->getCount(); i++)336 PRINT(3)("%d: %s\n", i, this->fragmentShaderSource->getEntry(i));*/337 }338 }339 123 -
branches/new_class_id/src/lib/graphics/shader.h
r9806 r9818 8 8 9 9 #include "base_object.h" 10 #include "glincl.h" 11 #include <vector> 12 10 #include "shader_data.h" 13 11 14 12 // FORWARD DECLARATION … … 24 22 { 25 23 public: 26 Uniform(const Shader* shader, const std::string& location) { this->uniform = glGetUniformLocationARB(shader->getProgram(), location.c_str()) ; }27 24 Uniform(const Shader& shader, const std::string& location) { this->uniform = glGetUniformLocation(shader.getProgram(), location.c_str()) ; }; 28 25 Uniform(GLhandleARB shaderProgram, const std::string& location) { this->uniform = glGetUniformLocation(shaderProgram, location.c_str()) ; }; … … 45 42 }; 46 43 47 //! The Type of Shader.48 typedef enum49 {50 None = 0, //!< No Type at all51 Fragment = 1, //!< Fragment Shader.52 Vertex = 2, //!< Vertex Shader.53 Program = 4, //!< Compiled Shader Programm.54 } Type;55 44 56 45 public: 57 Shader(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = ""); 58 virtual ~Shader(); 46 Shader(); 47 Shader(const std::string& vertexShaderFile, const std::string& fragmentShaderFile = ""); 48 49 Shader& operator=(const Shader& shader) { this->data = shader.data; return *this; }; 50 const ShaderData::Pointer& dataPointer() const { return data; }; 51 void acquireData(const ShaderData::Pointer& pointer) { this->data = pointer; }; 52 53 bool load(const std::string& vertexShaderFile, const std::string& fragmentShaderFile = "") 54 { return this->data->load(vertexShaderFile, fragmentShaderFile); } 59 55 60 56 61 Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform( this, location); }57 Shader::Uniform getUniform(const std::string& location) { return Shader::Uniform(*this, location); } 62 58 63 GLhandleARB getProgram() const { return this->shaderProgram; } 64 GLhandleARB getVertexS() const { return this->vertexShader; } 65 GLhandleARB getFragmentS() const { return this->fragmentShader; } 59 GLhandleARB getProgram() const { return this->data->getProgram(); } 60 GLhandleARB getVertexS() const { return this->data->getVertexS(); } 61 GLhandleARB getFragmentS() const { return this->data->getFragmentS(); } 62 63 void activateShader() const; 64 static void deactivateShader(); 65 66 void debug() const { this->data->debug(); }; 67 66 68 67 69 static bool checkShaderAbility(); 68 void activateShader();69 static void deactivateShader();70 71 void debug() const;72 70 73 71 inline static bool isShaderActive() { return (Shader::storedShader != NULL) ? true : false; }; 74 inline static Shader* getActiveShader() { return Shader::storedShader; }; 75 inline static void suspendShader() { Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} }; 72 inline static const Shader* getActiveShader() { return Shader::storedShader; }; 73 74 inline static void suspendShader() { const Shader* currShader = storedShader; if (storedShader!= NULL) { Shader::deactivateShader(); Shader::storedShader = currShader;} }; 76 75 inline static void restoreShader() { if (storedShader != NULL) storedShader->activateShader(); storedShader = NULL; }; 77 76 78 static void printError(GLhandleARB program); 77 private: 78 ShaderData::Pointer data; 79 79 80 private: 81 void bindShader(const char* name, const float* value, size_t size); 82 void linkShaderProgram(); 83 bool readShader(const std::string& fileName, std::string& output); 84 bool loadShaderProgramm(Shader::Type type, const std::string& fileName); 85 void deleteProgram(Shader::Type type); 86 87 private: 88 std::string fragmentShaderFile; 89 std::string vertexShaderFile; 90 91 GLhandleARB shaderProgram; 92 93 GLhandleARB vertexShader; 94 GLhandleARB fragmentShader; 95 96 97 static Shader* storedShader; 80 static const Shader* storedShader; 98 81 }; 99 82 -
branches/new_class_id/src/lib/graphics/shader_data.cc
r9806 r9818 38 38 * standard constructor 39 39 */ 40 Shader ::Shader (const std::string& vertexShaderFile, const std::string& fragmentShaderFile)41 { 42 this->registerObject(this, Shader ::_objectList);40 ShaderData::ShaderData () 41 { 42 this->registerObject(this, ShaderData::_objectList); 43 43 this->shaderProgram = 0; 44 44 this->vertexShader = 0; 45 45 this->fragmentShader = 0; 46 46 } 47 48 49 /// TODO fix that shaders are unloaded first. then loaded 50 bool ShaderData::load(const std::string& vertexShaderFile, const std::string& fragmentShaderFile) 51 { 47 52 if (GLEW_ARB_shader_objects && GLEW_ARB_shading_language_100) 48 53 { … … 50 55 51 56 if (!vertexShaderFile.empty()) 52 this->loadShaderProgramm(Shader ::Vertex, vertexShaderFile);57 this->loadShaderProgramm(ShaderData::Vertex, vertexShaderFile); 53 58 if (!fragmentShaderFile.empty()) 54 this->loadShaderProgramm(Shader ::Fragment, fragmentShaderFile);59 this->loadShaderProgramm(ShaderData::Fragment, fragmentShaderFile); 55 60 56 61 this->linkShaderProgram(); … … 62 67 } 63 68 } 64 65 69 66 70 /** 67 71 * standard deconstructor 68 72 */ 69 Shader ::~Shader()73 ShaderData::~ShaderData () 70 74 { 71 75 if (this->shaderProgram == glGetHandleARB(GL_PROGRAM_OBJECT_ARB)) 72 Shader::deactivateShader();76 glUseProgramObjectARB(0); 73 77 74 78 // delete what has to be deleted here 75 this->deleteProgram(Shader ::Vertex);76 this->deleteProgram(Shader ::Fragment);79 this->deleteProgram(ShaderData::Vertex); 80 this->deleteProgram(ShaderData::Fragment); 77 81 78 82 if (this->fragmentShader != 0) … … 98 102 } 99 103 100 Shader* Shader::storedShader = NULL; 101 102 103 bool Shader::loadShaderProgramm(Shader::Type type, const std::string& fileName) 104 bool ShaderData::loadShaderProgramm(ShaderData::Type type, const std::string& fileName) 104 105 { 105 106 GLhandleARB shader = 0; 106 107 107 if (type != Shader ::Vertex && type != Shader::Fragment)108 if (type != ShaderData::Vertex && type != ShaderData::Fragment) 108 109 return false; 109 110 this->deleteProgram(type); … … 114 115 return false; 115 116 116 if (type == Shader ::Vertex && GLEW_ARB_vertex_shader)117 if (type == ShaderData::Vertex && GLEW_ARB_vertex_shader) 117 118 { 118 119 this->vertexShaderFile = fileName; … … 121 122 } 122 123 123 if (type == Shader ::Fragment && GLEW_ARB_fragment_shader)124 if (type == ShaderData::Fragment && GLEW_ARB_fragment_shader) 124 125 { 125 126 this->fragmentShaderFile = fileName; … … 146 147 147 148 148 void Shader ::linkShaderProgram()149 void ShaderData::linkShaderProgram() 149 150 { 150 151 GLint status = 0; … … 158 159 159 160 160 bool Shader ::readShader(const std::string& fileName, std::string& output)161 bool ShaderData::readShader(const std::string& fileName, std::string& output) 161 162 { 162 163 char lineBuffer[PARSELINELENGTH]; … … 180 181 } 181 182 182 183 184 void Shader::activateShader() 185 { 186 if (likely (this->shaderProgram != 0)) 187 { 188 glUseProgramObjectARB(this->shaderProgram); 189 Shader::storedShader = this; 190 } 191 } 192 193 void Shader::bindShader(const char* name, const float* value, size_t size) 183 void ShaderData::bindShader(const char* name, const float* value, size_t size) 194 184 { 195 185 if (likely (this->shaderProgram != 0)) … … 210 200 } 211 201 212 void Shader::deactivateShader() 213 { 214 if (storedShader != NULL) 215 glUseProgramObjectARB(0); 216 Shader::storedShader = NULL; 217 } 218 219 220 void Shader::deleteProgram(Shader::Type type) 202 void ShaderData::deleteProgram(ShaderData::Type type) 221 203 { 222 204 GLint status = 0; 223 if (type == Shader ::Vertex && this->vertexShader != 0)205 if (type == ShaderData::Vertex && this->vertexShader != 0) 224 206 { 225 207 this->vertexShaderFile = ""; … … 228 210 glGetObjectParameterivARB(this->vertexShader, GL_OBJECT_DELETE_STATUS_ARB, &status); 229 211 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) 230 Shader ::printError(this->vertexShader);212 ShaderData::printError(this->vertexShader); 231 213 this->vertexShader = 0; 232 214 } 233 else if (type == Shader ::Fragment && this->fragmentShader != 0)215 else if (type == ShaderData::Fragment && this->fragmentShader != 0) 234 216 { 235 217 this->fragmentShaderFile = ""; … … 238 220 glGetObjectParameterivARB(this->fragmentShader, GL_OBJECT_DELETE_STATUS_ARB, &status); 239 221 if (status == GL_INVALID_VALUE || status == GL_INVALID_OPERATION) 240 Shader ::printError(this->fragmentShader);222 ShaderData::printError(this->fragmentShader); 241 223 this->fragmentShader = 0; 242 224 } … … 246 228 247 229 248 void Shader ::printError(GLhandleARB program)230 void ShaderData::printError(GLhandleARB program) 249 231 { 250 232 if (program == 0) … … 267 249 } 268 250 269 bool Shader::checkShaderAbility() 270 { 271 if (GLEW_ARB_shader_objects && 272 GLEW_ARB_shading_language_100 && 273 GLEW_ARB_vertex_shader && 274 GLEW_ARB_fragment_shader) 275 return true; 276 else 277 return false; 278 } 279 280 void Shader::debug() const 281 { 282 PRINT(3)("Shader info: (SHADER: %d)\n", this->shaderProgram); 251 void ShaderData::debug() const 252 { 253 PRINT(3)("ShaderData info: (SHADER: %d)\n", this->shaderProgram); 283 254 if (this->vertexShader != 0) 284 255 { -
branches/new_class_id/src/lib/graphics/shader_data.h
r9806 r9818 9 9 #include "base_object.h" 10 10 11 #include " shader_types.h"11 #include "glincl.h" 12 12 #include <vector> 13 13 #include "count_pointer.h" 14 14 15 15 // FORWARD DECLARATION … … 21 21 { 22 22 ObjectListDeclaration(ShaderData); 23 //! The Type of Shader. 24 typedef enum { 25 None = 0, //!< No Type at all 26 Fragment = 1, //!< Fragment Shader. 27 Vertex = 2, //!< Vertex Shader. 28 Program = 4, //!< Compiled Shader Programm. 29 } Type; 30 31 public: 32 typedef CountPointer<ShaderData> Pointer; 23 33 public: 24 ShaderData( const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = "");34 ShaderData(); 25 35 virtual ~ShaderData(); 26 36 27 Shaders::Uniform getUniform(const std::string& location) { return Shader::Uniform(this, location); } 37 bool load(const std::string& vertexShaderFile = "", const std::string& fragmentShaderFile = ""); 38 28 39 29 40 GLhandleARB getProgram() const { return this->shaderProgram; } … … 38 49 void linkShaderProgram(); 39 50 bool readShader(const std::string& fileName, std::string& output); 40 bool loadShaderProgramm(Shader::Type type, const std::string& fileName); 41 void deleteProgram(Shader::Type type); 51 bool loadShaderProgramm(ShaderData::Type type, const std::string& fileName); 52 void deleteProgram(ShaderData::Type type); 53 54 static void printError(GLhandleARB program); 42 55 43 56 private: 57 44 58 std::string fragmentShaderFile; 45 59 std::string vertexShaderFile;
Note: See TracChangeset
for help on using the changeset viewer.