//-------------------------------------------------------------- bool ofShader::setupShaderFromSource(GLenum type, string source) { // create program if it doesn't exist already checkAndCreateProgram(); // create shader GLuint shader = glCreateShader(type); if(shader == 0) { ofLog(OF_LOG_ERROR, "Failed creating shader of type " + nameForType(type)); return false; } // compile shader const char* sptr = source.c_str(); int ssize = source.size(); glShaderSource(shader, 1, &sptr, &ssize); glCompileShader(shader); // check compile status GLint status = GL_FALSE; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if(status == GL_TRUE) ofLog(OF_LOG_VERBOSE, nameForType(type) + " shader compiled."); else if (status == GL_FALSE) { ofLog(OF_LOG_ERROR, nameForType(type) + " shader failed to compile"); checkShaderInfoLog(shader, type); return false; } shaders[type] = shader; retainShader(shader); return true; }
//-------------------------------------------------------------- bool ofShader::setupShaderFromSource(GLenum type, string source, string sourceDirectoryPath) { unload(); // create program if it doesn't exist already checkAndCreateProgram(); GLuint clearErrors = glGetError(); //needed for some users to clear gl errors if( clearErrors != GL_NO_ERROR ) { ofLogVerbose("ofShader") << "setupShaderFromSource(): OpenGL error after checkAndCreateProgram() (probably harmless): error " << clearErrors; } // create shader GLuint shader = glCreateShader(type); if(shader == 0) { ofLogError("ofShader") << "setupShaderFromSource(): failed creating " << nameForType(type) << " shader"; return false; } // parse for includes string src = parseForIncludes( source , sourceDirectoryPath); // store source code (that's the expanded source with all includes copied in) shaderSource[type] = src; // compile shader const char* sptr = src.c_str(); int ssize = src.size(); glShaderSource(shader, 1, &sptr, &ssize); glCompileShader(shader); // check compile status GLint status = GL_FALSE; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); GLuint err = glGetError(); if (err != GL_NO_ERROR) { ofLogError("ofShader") << "setupShaderFromSource(): OpenGL generated error " << err << " trying to get the compile status for a " << nameForType(type) << " shader, does your video card support this?"; return false; } if(status == GL_TRUE) { ofLogVerbose("ofShader") << "setupShaderFromSource(): " << nameForType(type) + " shader compiled"; #ifdef TARGET_EMSCRIPTEN checkShaderInfoLog(shader, type, OF_LOG_VERBOSE); #else checkShaderInfoLog(shader, type, OF_LOG_WARNING); #endif } else if (status == GL_FALSE) { ofLogError("ofShader") << "setupShaderFromSource(): " << nameForType(type) + " shader failed to compile"; checkShaderInfoLog(shader, type, OF_LOG_ERROR); return false; } shaders[type] = shader; retainShader(shader); return true; }
//-------------------------------------------------------------- ofShader::ofShader(const ofShader & mom) : program(mom.program), bLoaded(mom.bLoaded), shaders(mom.shaders){ if(mom.bLoaded){ retainProgram(program); for(unordered_map<GLenum, GLuint>::const_iterator it = shaders.begin(); it != shaders.end(); ++it){ GLuint shader = it->second; retainShader(shader); } } }
//-------------------------------------------------------------- ofShader & ofShader::operator=(const ofShader & mom){ if(this == &mom) { return *this; } if(bLoaded){ unload(); } program = mom.program; bLoaded = mom.bLoaded; shaders = mom.shaders; if(mom.bLoaded){ retainProgram(program); for(unordered_map<GLenum, GLuint>::const_iterator it = shaders.begin(); it != shaders.end(); ++it){ GLuint shader = it->second; retainShader(shader); } } return *this; }