Example/GLSL Full Compile Linking

From OpenGL.org
Jump to: navigation, search

Full compile/link of a Vertex and Fragment Shader.

//Read our shaders into the appropriate buffers
std::string fragSource = //Get source code for fragment shader.
 
//Create an empty vertex shader handle
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
 
//Send the vertex shader source code to GL
//Note that std::string's .c_str is NULL character terminated.
std::string vertexSource = //Get source code for vertex shader.
const GLchar *source = (const GLchar *)vertexSource.c_str();
glShaderSource(vertexShader, 1, &source, 0);
 
//Compile the vertex shader
glCompileShader(vertexShader);
 
GLint isCompiled = 0;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
        GLint maxLength = 0;
        glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
 
        //The maxLength includes the NULL character
        std::vector<GLchar> infoLog(maxLength);
        glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &infoLog[0]);
 
        //We don't need the shader anymore.
        glDeleteShader(vertexShader);
 
        //Use the infoLog as you see fit.
 
        //In this simple program, we'll just leave
        return;
}
 
//Create an empty fragment shader handle
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
 
//Send the fragment shader source code to GL
//Note that std::string's .c_str is NULL character terminated.
std::string fragmentSource = //Get source code for fragment shader.
source = (const GLchar *)fragmentSource.c_str();
glShaderSource(fragmentShader, 1, &source, 0);
 
//Compile the fragment shader
glCompileShader(fragmentShader);
 
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
        GLint maxLength = 0;
        glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);
 
        //The maxLength includes the NULL character
        std::vector<GLchar> infoLog(maxLength);
        glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, &infoLog[0]);
 
        //We don't need the shader anymore.
        glDeleteShader(fragmentShader);
        //Either of them. Don't leak shaders.
        glDeleteShader(vertexShader);
 
        //Use the infoLog as you see fit.
 
        //In this simple program, we'll just leave
        return;
}
 
//Vertex and fragment shaders are successfully compiled.
//Now time to link them together into a program.
//Get a program object.
GLuint program = glCreateProgram();
 
//Attach our shaders to our program
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
 
//Link our program
glLinkProgram(program);
 
//Note the different functions here: glGetProgram* instead of glGetShader*.
GLint isLinked = 0;
glGetProgramiv(shaderprogram, GL_LINK_STATUS, (int *)&isLinked);
if(isLinked == GL_FALSE)
{
        GLint maxLength = 0;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
 
        //The maxLength includes the NULL character
        std::vector<GLchar> infoLog(maxLength);
        glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
 
        //We don't need the program anymore.
        glDeleteProgram(program);
        //Don't leak shaders either.
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
 
        //Use the infoLog as you see fit.
 
        //In this simple program, we'll just leave
        return;
}
 
//Always detach shaders after a successful link.
glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader);