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

From OpenGL.org
Jump to: navigation, search
m
m
Line 2: Line 2:
  
 
<source lang="cpp">
 
<source lang="cpp">
//Read our shaders into the appropriate buffers
+
// Read our shaders into the appropriate buffers
std::string vertexSource = //Get source code for vertex shader.
+
std::string vertexSource = // Get source code for vertex shader.
std::string fragmentSource = //Get source code for fragment shader.
+
std::string fragmentSource = // Get source code for fragment shader.
  
//Create an empty vertex shader handle
+
// Create an empty vertex shader handle
 
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
 
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  
//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.
 
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);
  
//Compile the vertex shader
+
// Compile the vertex shader
 
glCompileShader(vertexShader);
 
glCompileShader(vertexShader);
  
GLint isCompiled = 0;
+
GLint isCompiled;
 
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
 
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
 
if(isCompiled == GL_FALSE)
 
if(isCompiled == GL_FALSE)
 
{
 
{
GLint maxLength = 0;
+
GLint maxLength;
 
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
 
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
  
//The maxLength includes the NULL character
+
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
+
GLchar infoLog[maxLength];
glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &infoLog[0]);
+
glGetShaderInfoLog(vertexShader, maxLength, &maxLength, infoLog);
 +
 
 +
// Use infoLog as you see fit.
 
 
//We don't need the shader anymore.
+
// We don't need the shader anymore.
 
glDeleteShader(vertexShader);
 
glDeleteShader(vertexShader);
  
//Use the infoLog as you see fit.
+
// In this simple program, we'll just leave
 
//In this simple program, we'll just leave
 
 
return;
 
return;
 
}
 
}
  
//Create an empty fragment shader handle
+
// Create an empty fragment shader handle
 
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
 
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  
//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.
 
source = (const GLchar *)fragmentSource.c_str();
 
source = (const GLchar *)fragmentSource.c_str();
 
glShaderSource(fragmentShader, 1, &source, 0);
 
glShaderSource(fragmentShader, 1, &source, 0);
  
//Compile the fragment shader
+
// Compile the fragment shader
 
glCompileShader(fragmentShader);
 
glCompileShader(fragmentShader);
  
Line 51: Line 51:
 
if(isCompiled == GL_FALSE)
 
if(isCompiled == GL_FALSE)
 
{
 
{
GLint maxLength = 0;
+
GLint maxLength;
 
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);
 
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);
  
//The maxLength includes the NULL character
+
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
+
GLchar infoLog[maxLength];
glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, &infoLog[0]);
+
glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, infoLog);
 
 
//We don't need the shader anymore.
+
// Use infoLog as you see fit.
 +
 
 +
// We don't need the shader anymore.
 
glDeleteShader(fragmentShader);
 
glDeleteShader(fragmentShader);
//Either of them. Don't leak shaders.
+
// Either of them. Don't leak shaders.
 
glDeleteShader(vertexShader);
 
glDeleteShader(vertexShader);
 
//Use the infoLog as you see fit.
 
 
 
//In this simple program, we'll just leave
+
// In this simple program, we'll just leave
 
return;
 
return;
 
}
 
}
  
//Vertex and fragment shaders are successfully compiled.
+
// Vertex and fragment shaders are successfully compiled.
//Now time to link them together into a program.
+
// Now time to link them together into a program.
//Get a program object.
+
// Get a program object.
 
GLuint program = glCreateProgram();
 
GLuint program = glCreateProgram();
  
//Attach our shaders to our program
+
// Attach our shaders to our program
 
glAttachShader(program, vertexShader);
 
glAttachShader(program, vertexShader);
 
glAttachShader(program, fragmentShader);
 
glAttachShader(program, fragmentShader);
  
//Link our program
+
// Link our program
 
glLinkProgram(program);
 
glLinkProgram(program);
  
//Note the different functions here: glGetProgram* instead of glGetShader*.
+
// Note the different functions here: glGetProgram* instead of glGetShader*.
GLint isLinked = 0;
+
GLint isLinked;
 
glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
 
glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
 
if(isLinked == GL_FALSE)
 
if(isLinked == GL_FALSE)
 
{
 
{
GLint maxLength = 0;
+
GLint maxLength;
 
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
 
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
  
//The maxLength includes the NULL character
+
// The maxLength includes the NULL character
std::vector<GLchar> infoLog(maxLength);
+
GLchar infoLog[maxLength];
glGetProgramInfoLog(program, maxLength, &maxLength, &infoLog[0]);
+
glGetProgramInfoLog(program, maxLength, &maxLength, infoLog);
+
 
//We don't need the program anymore.
+
// Use the infoLog as you see fit.
 +
 
 +
// We don't need the program anymore.
 
glDeleteProgram(program);
 
glDeleteProgram(program);
//Don't leak shaders either.
+
// Don't leak shaders either.
 
glDeleteShader(vertexShader);
 
glDeleteShader(vertexShader);
 
glDeleteShader(fragmentShader);
 
glDeleteShader(fragmentShader);
  
//Use the infoLog as you see fit.
+
// In this a simple program, we'll just leave
 
//In this a simple program, we'll just leave
 
 
return;
 
return;
 
}
 
}
  
//Always detach 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><noinclude>[[Category:Example Code]]</noinclude>
 
</source><noinclude>[[Category:Example Code]]</noinclude>

Revision as of 18:10, 4 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;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
	GLint maxLength;
	glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);

	// The maxLength includes the NULL character
	GLchar infoLog[maxLength];
	glGetShaderInfoLog(vertexShader, maxLength, &maxLength, infoLog);

	// Use infoLog as you see fit.
	
	// We don't need the shader anymore.
	glDeleteShader(vertexShader);

	// 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;
	glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);

	// The maxLength includes the NULL character
	GLchar infoLog[maxLength];
	glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, infoLog);
	
	// Use infoLog as you see fit.

	// We don't need the shader anymore.
	glDeleteShader(fragmentShader);
	// Either of them. Don't leak shaders.
	glDeleteShader(vertexShader);
	
	// 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;
glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
if(isLinked == GL_FALSE)
{
	GLint maxLength;
	glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);

	// The maxLength includes the NULL character
	GLchar infoLog[maxLength];
	glGetProgramInfoLog(program, maxLength, &maxLength, infoLog);

	// Use the infoLog as you see fit.

	// We don't need the program anymore.
	glDeleteProgram(program);
	// Don't leak shaders either.
	glDeleteShader(vertexShader);
	glDeleteShader(fragmentShader);

	// In this a simple program, we'll just leave
	return;
}

// Always detach shaders after a successful link.
glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader);