使用一个程序和一组顶点/片段着色器后,我需要使用更多程序,因此我的初始设置函数不再有任何参数是不够的。
在我的设置函数中,我传递了shader_program和两个shader源。当我尝试在我的显示方法中使用该程序时,GLuint程序句柄为零。我目前不理解的是为什么在我的着色器使用固定源代码时一切都正常工作。可能是C++程序,并且我以错误的方式传递了参数吗?我没有收到任何错误消息,但屏幕上没有渲染任何内容。
这里是我用来调用着色器设置函数和函数本身的代码:
// Variables
GLuint shader_program;
const GLchar *vertex_shader =
"#version 330\n"
""
"layout (location = 0) in vec3 in_position;"
...
"";
// In my main function, I call this to set up the shaders
installShaders(shader_program, vertex_shader, fragment_shader);
// SET-UP-METHOD
void installShaders(GLuint program_handle, const GLchar *vertex_shader_source, const GLchar *fragment_shader_source)
{
// Handles for the shader objects
GLuint vertex_shader_name;
GLuint fragment_shader_name;
// Status values
GLint vertex_shader_compiled;
GLint fragment_shader_compiled;
GLint successfully_linked;
// Generate shader names
vertex_shader_name = glCreateShader(GL_VERTEX_SHADER);
fragment_shader_name = glCreateShader(GL_FRAGMENT_SHADER);
// Specify the sources for the shaders
glShaderSource( vertex_shader_name, 1, (const GLchar**)&vertex_shader_source , NULL);
glShaderSource(fragment_shader_name, 1, (const GLchar**)&fragment_shader_source, NULL);
// Compile Vertex shader and check for errors
glCompileShader(vertex_shader_name);
glGetShaderiv(vertex_shader_name, GL_COMPILE_STATUS, &vertex_shader_compiled);
printShaderInfoLog(vertex_shader_name);
// Compile Fragment shader and check for errors
glCompileShader(fragment_shader_name);
glGetShaderiv(fragment_shader_name, GL_COMPILE_STATUS, &fragment_shader_compiled);
printShaderInfoLog(fragment_shader_name);
// Exit if the shaders couldn't be compiled correctly
if(!vertex_shader_compiled || !fragment_shader_compiled)
{
printf("Shaders were not compiled correctly!\n");
exit(EXIT_FAILURE);
}
// Generate program name
program_handle = glCreateProgram();
// Attach shaders to the program
glAttachShader(program_handle, vertex_shader_name);
glAttachShader(program_handle, fragment_shader_name);
// Link program and check for errors
glLinkProgram(program_handle);
glGetProgramiv(program_handle, GL_LINK_STATUS, &successfully_linked);
printProgramInfoLog(program_handle);
// Exit if the program couldn't be linked correctly
if(!successfully_linked)
{
printf("Program was not linked correctly!\n");
exit(EXIT_FAILURE);
}
// Set up initial values of uniform variables
glUseProgram(program_handle);
location_projectionMatrix = glGetUniformLocation(program_handle, "myProjectionMatrix");
printf("Location of the uniform -->myProjectionMatrix<--- : %i\n", location_projectionMatrix);
projectionMatrix = glm::mat4(1.0f);
glUniformMatrix4fv(location_projectionMatrix, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
location_modelViewMatrix = glGetUniformLocation(program_handle, "myModelViewMatrix");
printf("Location of the uniform -->myModelViewMatrix<--- : %i\n", location_modelViewMatrix);
modelViewMatrix = glm::mat4(1.0f);
glUniformMatrix4fv(location_modelViewMatrix, 1, GL_FALSE, glm::value_ptr(modelViewMatrix));
glUseProgram(0);
// Everything worked
printf("Shaders were set up correctly!\n");
}
program_handle
设为指针或引用(如果您正在使用C ++)。由于您将program_handle
作为值传递到函数中,因此从glCreateProgram
返回的值在函数返回时会丢失。 - radical7glShaderSource
以许多方式处理源代码。如果您将着色器指定为单个、以NULL结尾的字符串,则您正在执行的方式是有效的。 - radical7