To compile a shader, use the following steps:
- Create the shader object:
GLuint vertShader = glCreateShader( GL_VERTEX_SHADER ); if( 0 == vertShader ) { std::cerr << "Error creating vertex shader." << std::endl; exit(EXIT_FAILURE); }
- Copy the source code into the shader object:
std::string shaderCode = loadShaderAsString("basic.vert.glsl"); const GLchar * codeArray[] = { shaderCode.c_str() }; glShaderSource( vertShader, 1, codeArray, NULL );
- Compile the shader:
glCompileShader( vertShader );
- Verify the compilation status:
GLint result; glGetShaderiv( vertShader, GL_COMPILE_STATUS, &result ); if( GL_FALSE == result ) { std::cerr << "Vertex shader compilation failed!" << std::endl;
// Get and print the info log GLint logLen; glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &logLen); if( logLen > 0 ) { std::string log(logLen, ' '), GLsizei written; glGetShaderInfoLog(vertShader, logLen, &written, &log[0]); std::cerr << "Shader log: " << std::endl << log; } }