gpt4 book ai didi

opengl - 设置 OpenGL 程序和着色器

转载 作者:行者123 更新时间:2023-12-05 02:24:13 25 4
gpt4 key购买 nike

在使用一个程序和一组顶点/片段着色器后,我需要使用更多程序,所以我没有任何参数的初始设置函数不再足够了。

我将 shader_program 和两个着色器源传递到我的设置函数。当我尝试在我的显示方法中使用该程序时,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");
}

最佳答案

问题是您存储 GLSL 着色器程序名称(从 glCreateShader 返回的值)的变量是按值传递到例程中的。因此,当函数返回时,它的值会丢失。通过引用传递值(作为指针或作为 C++ 引用),然后该值将在函数退出时保留,您将能够在应用程序的其他部分引用着色器。

关于opengl - 设置 OpenGL 程序和着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com