Re: The Secret that is OpenGL - Getting Started/Resources
Posted: Wed Nov 06, 2013 12:30 pm
You compile and link a shader only once. This is some code I wrote for loading some time ago. Hope it helps:
Code: Select all
void DumpShaderInfo(GLuint shader){
GLint length;
GLsizei slength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH , &length);
if(length >= 1){
std::string txt;
txt.resize(length);
glGetShaderInfoLog(shader, length, &slength, (GLchar*)txt.c_str());
std::cout << txt << std::endl;
}
}
void DumpShaderProgramInfo(GLuint shaderProgram){
GLint length;
GLsizei slength;
int linked;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linked);
if(!linked){
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH , &length);
if(length >= 1){
std::string txt;
txt.resize(length);
glGetProgramInfoLog(shaderProgram, length, &slength, (GLchar*)txt.c_str());
std::cout << txt << std::endl;
}
}
}
GLuint LoadShader(const std::string& vertex, const std::string& fragment){
GLuint program = glCreateProgram();
// Vertex
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
const char* vertex_c_str = vertex.c_str();
int vertex_length = int(vertex.length());
glShaderSource(vertexShader, 1, &vertex_c_str, &vertex_length);
glCompileShader(vertexShader);
DumpShaderInfo(vertexShader);
glAttachShader(program, vertexShader);
glDeleteShader(vertexShader);
// Fragment
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const char* fragment_c_str = fragment.c_str();
int fragment_length = int(fragment.length());
glShaderSource(fragmentShader, 1, &fragment_c_str, &fragment_length);
glCompileShader(fragmentShader);
DumpShaderInfo(fragmentShader);
glAttachShader(program, fragmentShader);
glDeleteShader(fragmentShader);
// Linkage
glLinkProgram(program);
DumpShaderProgramInfo(program);
glutReportErrors();
return program;
}