我遇到了以下问题:
我有这个顶点着色器:
#version 410 core
in vec3 vertex_position;
out vec3 color;
void main()
{
color = vec3(0.0, 0.0, 0.0);
gl_Position = vec4(vertex_position, 1.0);
}
以下片段着色器:
#version 410 core
in vec3 color;
out vec4 frag_color;
void main()
{
frag_color = vec4(color, 1.0);
}
我想将颜色矢量从顶点着色器传递到fragent着色器,但是,屏幕上没有显示任何内容。当我在片段着色器中设置frag_color时:frag_color = vec4(0.0,0.0,0.0,1.0);有用。因此,在两个阶段之间传递价值的东西必定是错误的。
这是我的着色器代码:
void Shader::CreateShaders(std::string filePathVert, std::string filePathFrag)
{
// setup shaders
std::string vertex_shader_source = LoadSourceFile(filePathVert);
std::string fragment_shader_source = LoadSourceFile(filePathFrag);
const GLchar* vs_source = vertex_shader_source.c_str();
const GLchar* fs_source = fragment_shader_source.c_str();
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vs_source, NULL);
glCompileShader(vs);
CheckShaderError(vs, GL_COMPILE_STATUS, false, "Error: Shader compilation failed: ");
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fs_source, NULL);
glCompileShader(fs);
CheckShaderError(fs, GL_COMPILE_STATUS, false, "Error: Shader compilation failed: ");
this->shader_program = glCreateProgram();
glAttachShader(this->shader_program, fs);
glAttachShader(this->shader_program, vs);
glLinkProgram(this->shader_program);
CheckValidationError(this->shader_program);
glDeleteShader(vs);
glDeleteShader(fs);
}
void Shader::Bind()
{
glUseProgram(this->shader_program);
}
void Shader::Destroy()
{
glDeleteProgram(this->shader_program);
}
static std::string LoadSourceFile(std::string filePath)
{
std::ifstream ifs(filePath);
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
if(content.empty())
{
std::cerr << "source file was empty or not correctly loaded\n";
}
return content;
}
我的绘图代码:
GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_positions);
glVertexAttribPointer(glGetAttribLocation(shader.GetProgramID(), "vertex_position"), 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, vbo_colors);
glVertexAttribPointer(glGetAttribLocation(shader.GetProgramID(), "vertex_color"), 3, GL_FLOAT, GL_FALSE, 0, NULL);
while(!glfwWindowShouldClose(display.GetWindowID())) {
display.UpdateFPSCounter();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.Bind();
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
update();
glfwSwapBuffers(display.GetWindowID());
display.CheckWindowShouldClose();
}
有谁看到我做错了什么?
提前致谢!
答案 0 :(得分:1)
vertex_color
与顶点着色器中的变量不匹配。
答案 1 :(得分:0)
问题在于:
glEnableVertexAttribArray(0);
glVertexAttribPointer(glGetAttribLocation(shader.GetProgramID(), "vertex_position"),
3, GL_FLOAT, GL_FALSE, 0, NULL);
这两个调用都将属性位置作为第一个参数。虽然该位置的位置可能恰好为0,但这不能保证,您应该将相同的参数传递给这两个调用:
GLuint posLoc = glGetAttribLocation(shader.GetProgramID(), "vertex_position");
glEnableVertexAttribArray(posLoc);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);