GLuint createShaderProgram(const char * _strVertex, const char * _strFragment) { GLuint vertex_shader_object = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader_object, 1, &_strVertex, nullptr); glCompileShader(vertex_shader_object); assert(checkShaderCompileStatus(vertex_shader_object)); if (!checkShaderCompileStatus(vertex_shader_object)) logErrorShader(GL_VERTEX_SHADER, _strVertex); GLuint fragment_shader_object = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader_object, 1, &_strFragment, nullptr); glCompileShader(fragment_shader_object); assert(checkShaderCompileStatus(fragment_shader_object)); if (!checkShaderCompileStatus(fragment_shader_object)) logErrorShader(GL_VERTEX_SHADER, _strFragment); GLuint program = glCreateProgram(); glBindAttribLocation(program, SC_POSITION, "aPosition"); glBindAttribLocation(program, SC_TEXCOORD0, "aTexCoord0"); glBindAttribLocation(program, SC_TEXCOORD1, "aTexCoord1"); glAttachShader(program, vertex_shader_object); glAttachShader(program, fragment_shader_object); glLinkProgram(program); glDeleteShader(vertex_shader_object); glDeleteShader(fragment_shader_object); assert(checkProgramLinkStatus(program)); return program; }
unsigned int OpenGL::Helper::createProgramFromMemory(const char *vertex, const char *fragment) { GLuint vs = glCreateShader(GL_VERTEX_SHADER); GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); int vs_len = strlen(vertex); int fs_len = strlen(fragment); glShaderSource(vs, 1, &vertex, &vs_len); glShaderSource(fs, 1, &fragment, &fs_len); glCompileShader(vs); if(!checkShaderCompileStatus(vs)) return -1; glCompileShader(fs); if(!checkShaderCompileStatus(fs)) return -1; GLuint program = glCreateProgram(); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); //glDetachShader(program, vs); //glDetachShader(program, fs); if(!checkProgramLinkStatus(program)) return -1; return program; }
GLuint SetupShaders(void) { GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER); GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(VertexShaderID, 1, &vertex_shader, NULL); glCompileShader(VertexShaderID); checkShaderCompileStatus(VertexShaderID); glShaderSource(FragmentShaderID, 1, &fragment_shader, NULL); glCompileShader(FragmentShaderID); checkShaderCompileStatus(FragmentShaderID); GLuint ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); glLinkProgram(ProgramID); glDetachShader(ProgramID, VertexShaderID); glDetachShader(ProgramID, FragmentShaderID); glDeleteShader(VertexShaderID); glDeleteShader(FragmentShaderID); return ProgramID; }
void kore::Shader::loadShaderCode(const std::string& file, GLenum shadertype) { FILE *code_file = fopen(file.c_str(), "r"); if (code_file == NULL) { kore::Log::getInstance()->write( "[ERROR] Could not open shader '%s'\n", file.c_str()); return; } _handle = glCreateShader(shadertype); _shadertype = shadertype; char f_char; while (fread(&f_char, sizeof(f_char), 1, code_file) != 0) { // ignore 'carriage return' characters if (f_char != '\r') _code += f_char; } fclose(code_file); const GLchar* src = _code.c_str(); glShaderSource(_handle, 1, &src, 0); glCompileShader(_handle); bool bSuccess = checkShaderCompileStatus(_handle, file); if (!bSuccess) { glDeleteShader(_handle); } _name = file.substr(file.find_last_of("/")+1); kore::ResourceManager::getInstance()->addShader(this); }
bool kore::ShaderProgram::loadShader(const std::string& file, GLenum shadertype) { GLuint shaderHandle = ResourceManager::getInstance()->getShaderHandle(file); if (shaderHandle == KORE_GLUINT_HANDLE_INVALID) { // Shader not found in cache. std::string progSrc; shaderHandle = glCreateShader(shadertype); FILE *code_file = fopen(file.c_str(), "r"); if (code_file == NULL) { kore::Log::getInstance()->write( "[ERROR] Could not open shader program '%s'\n", file.c_str()); return false; } char f_char; while (fread(&f_char, sizeof(f_char), 1, code_file) != 0) { if (f_char != '\r') progSrc += f_char; } fclose(code_file); const GLchar* src = progSrc.c_str(); glShaderSource(shaderHandle, 1, &src, 0); glCompileShader(shaderHandle); bool bSuccess = checkShaderCompileStatus(shaderHandle, file); if (!bSuccess) { glDeleteShader(shaderHandle); return false; } ResourceManager::getInstance()->addShaderHandle(file, shaderHandle); } switch (shadertype) { case GL_VERTEX_SHADER: _vertex_prog = shaderHandle; break; case GL_FRAGMENT_SHADER: _fragment_prog = shaderHandle; break; case GL_GEOMETRY_SHADER: _geometry_prog = shaderHandle; break; case GL_TESS_CONTROL_SHADER: _tess_ctrl = shaderHandle; break; case GL_TESS_EVALUATION_SHADER: _tess_eval = shaderHandle; break; } return true; }
void createProgram() { const GLchar* vertexShaderSource = readTextFile("tutorial08.vert"); int vertexShaderSourceLength = strlen(vertexShaderSource); GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShaderId, 1, &vertexShaderSource, &vertexShaderSourceLength); glCompileShader(vertexShaderId); checkShaderCompileStatus(vertexShaderId); const GLchar* fragmentShaderSource = readTextFile("tutorial08.frag"); int fragmentShaderSourceLength = strlen(fragmentShaderSource); GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShaderId, 1, &fragmentShaderSource, &fragmentShaderSourceLength); glCompileShader(fragmentShaderId); checkShaderCompileStatus(fragmentShaderId); programId = glCreateProgram(); glAttachShader(programId, vertexShaderId); glAttachShader(programId, fragmentShaderId); glBindAttribLocation(programId, POSITION_ATTRIBUTE_INDEX, "vPosition"); glBindAttribLocation(programId, NORMAL_ATTRIBUTE_INDEX, "vNormal"); glLinkProgram(programId); checkProgramLinkStatus(programId); }
void Shader::compileShader(const GLuint &shader, const std::string& code) { const char *source; int length; source = code.c_str(); length = code.size(); glShaderSource(shader, 1, &source, &length); glCompileShader(shader); if (!checkShaderCompileStatus(shader)) { exit(0); } }
GLint CShader::createShader( const char* t_shaderFile, GLenum t_shaderType ) { std::string shaderContent; if( !readFileToStr( t_shaderFile, shaderContent ) ) return -1; const GLchar* p = 0; GLuint shaderIndex = glCreateShader( t_shaderType ); p = (const GLchar*)shaderContent.c_str(); glShaderSource( shaderIndex, 1, &p, NULL ); glCompileShader( shaderIndex ); if( !checkShaderCompileStatus( shaderIndex ) ) return -1; return shaderIndex; }
GLuint loadShader(char const *fname, GLenum shaderType, bool *errorFlagPtr) { std::ifstream fileStream(fname); std::stringstream buffer; buffer << fileStream.rdbuf(); std::string shaderSource(buffer.str()); GLuint shaderId = glCreateShader(shaderType); const GLchar * const shaderSourceCStr = shaderSource.c_str(); glShaderSource(shaderId, 1, &shaderSourceCStr, nullptr); glCompileShader(shaderId); *errorFlagPtr = checkShaderCompileStatus(shaderId); if(*errorFlagPtr) { glDeleteShader(shaderId); return 0; } return shaderId; }