bool ofxShader::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; return true; }
bool ofxShader::linkProgram() { if(shaders.empty()) { ofLog(OF_LOG_ERROR, "Trying to link GLSL program, but no shaders created yet"); } else { checkAndCreateProgram(); for(map<GLenum, GLuint>::const_iterator it = shaders.begin(); it != shaders.end(); ++it){ GLuint shader = it->second; if(shader) { ofLog(OF_LOG_VERBOSE, "Attaching shader of type " + nameForType(it->first)); glAttachShader(program, shader); } } glLinkProgram(program); for(map<GLenum, GLuint>::const_iterator it = shaders.begin(); it != shaders.end(); ++it){ GLuint shader = it->second; if(shader) { checkShaderLinkStatus(shader, it->first); } } checkProgramInfoLog(program); bLoaded = 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; }
//-------------------------------------------------------------- void ofShader::checkAndCreateProgram() { #ifndef TARGET_OPENGLES if(GL_ARB_shader_objects) { #else if(ofIsGLProgrammableRenderer()){ #endif if(program == 0) { ofLogVerbose("ofShader") << "checkAndCreateProgram(): creating GLSL program"; program = glCreateProgram(); retainProgram(program); } } else { ofLogError("ofShader") << "sorry, it looks like you can't run 'ARB_shader_objects'"; ofLogError("ofShader") << "please check the capabilites of your graphics card: http://www.ozone3d.net/gpu_caps_viewer"; } } //-------------------------------------------------------------- bool ofShader::linkProgram() { if(shaders.empty()) { ofLogError("ofShader") << "linkProgram(): trying to link GLSL program, but no shaders created yet"; } else { checkAndCreateProgram(); for(unordered_map<GLenum, GLuint>::const_iterator it = shaders.begin(); it != shaders.end(); ++it){ GLuint shader = it->second; if(shader) { ofLogVerbose("ofShader") << "linkProgram(): attaching " << nameForType(it->first) << " shader to program " << program; glAttachShader(program, shader); } } glLinkProgram(program); checkProgramLinkStatus(program); // bLoaded means we have loaded shaders onto the graphics card; // it doesn't necessarily mean that these shaders have compiled and linked successfully. bLoaded = true; } return bLoaded; } void ofShader::bindAttribute(GLuint location, const string & name) const{ glBindAttribLocation(program,location,name.c_str()); }
//-------------------------------------------------------------- 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); GLuint err = glGetError(); if (err != GL_NO_ERROR){ ofLog( OF_LOG_ERROR, "OpenGL generated error " + ofToString(err) + " trying to get the compile status for " + nameForType(type) + " shader. Does your video card support this?" ); return false; } 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::linkProgram() { if(shaders.empty()) { ofLog(OF_LOG_ERROR, "Trying to link GLSL program, but no shaders created yet"); } else { checkAndCreateProgram(); for(map<GLenum, GLuint>::const_iterator it = shaders.begin(); it != shaders.end(); ++it){ GLuint shader = it->second; if(shader) { ofLog(OF_LOG_VERBOSE, "Attaching shader of type " + nameForType(it->first)); glAttachShader(program, shader); } } glLinkProgram(program); checkProgramLinkStatus(program); // bLoaded means we have loaded shaders onto the graphics card; // it doesn't necessarily mean that these shaders have compiled and linked successfully. bLoaded = true; } return bLoaded; }
//-------------------------------------------------------------- void ofShader::setGeometryOutputCount(int count) { #ifndef TARGET_OPENGLES checkAndCreateProgram(); glProgramParameteri(program, GL_GEOMETRY_VERTICES_OUT_EXT, count); #endif }
//-------------------------------------------------------------- void ofShader::setGeometryOutputType(GLenum type) { #ifndef TARGET_OPENGLES checkAndCreateProgram(); glProgramParameteri(program, GL_GEOMETRY_OUTPUT_TYPE_EXT, type); #endif }
void ofxShader::setGeometryOutputCount(int count) { checkAndCreateProgram(); glProgramParameteriEXT(program, GL_GEOMETRY_VERTICES_OUT_EXT, count); }
void ofxShader::setGeometryOutputType(GLenum type) { checkAndCreateProgram(); glProgramParameteriEXT(program, GL_GEOMETRY_OUTPUT_TYPE_EXT, type); }
void ofShader::setGeometryInputType(GLenum type) { checkAndCreateProgram(); #ifndef TARGET_LINUX glProgramParameteriEXT(program, GL_GEOMETRY_INPUT_TYPE_EXT, type); #endif }