Difference between revisions of "Example/GLSL Full Compile Linking"

From OpenGL.org
Jump to: navigation, search
(Example page for full shader/program compile/link.)
 
(Using bad coding style (ie: not initializing variables) is NOT simplifying.)
 
(5 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
<source lang="cpp">
 
<source lang="cpp">
 
//Read our shaders into the appropriate buffers
 
//Read our shaders into the appropriate buffers
std::string fragSource = //Get source code for fragment shader.
+
std::string vertexSource = //Get source code for vertex shader.
 +
std::string fragmentSource = //Get source code for fragment shader.
  
 
//Create an empty vertex shader handle
 
//Create an empty vertex shader handle
Line 10: Line 11:
 
//Send the vertex shader source code to GL
 
//Send the vertex shader source code to GL
 
//Note that std::string's .c_str is NULL character terminated.
 
//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();
 
const GLchar *source = (const GLchar *)vertexSource.c_str();
 
glShaderSource(vertexShader, 1, &source, 0);
 
glShaderSource(vertexShader, 1, &source, 0);
Line 42: Line 42:
 
//Send the fragment shader source code to GL
 
//Send the fragment shader source code to GL
 
//Note that std::string's .c_str is NULL character terminated.
 
//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();
 
source = (const GLchar *)fragmentSource.c_str();
 
glShaderSource(fragmentShader, 1, &source, 0);
 
glShaderSource(fragmentShader, 1, &source, 0);
Line 84: Line 83:
 
//Note the different functions here: glGetProgram* instead of glGetShader*.
 
//Note the different functions here: glGetProgram* instead of glGetShader*.
 
GLint isLinked = 0;
 
GLint isLinked = 0;
glGetProgramiv(shaderprogram, GL_LINK_STATUS, (int *)&isLinked);
+
glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
 
if(isLinked == GL_FALSE)
 
if(isLinked == GL_FALSE)
 
{
 
{
Line 106: Line 105:
 
}
 
}
  
//Always detatch shaders after a successful link.
+
//Always detach shaders after a successful link.
 
glDetachShader(program, vertexShader);
 
glDetachShader(program, vertexShader);
 
glDetachShader(program, fragmentShader);
 
glDetachShader(program, fragmentShader);
</source>
+
</source><noinclude>[[Category:Example Code]]</noinclude>

Latest revision as of 09:25, 6 December 2014

Full compile/link of a Vertex and Fragment Shader.

//Read our shaders into the appropriate buffers
std::string vertexSource = //Get source code for vertex shader.
std::string fragmentSource = //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.
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.
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(program, 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);