【问题标题】:My OpenGL program's shaders don't affect the geometry or color the polygons我的 OpenGL 程序的着色器不会影响几何图形或为多边形着色
【发布时间】:2017-05-03 17:42:40
【问题描述】:

我有一个顶点和片段着色器不久前还可以正常工作,但我尝试实现骨骼动画,结果在某处损坏了一些东西。我什至不再使用我的自定义函数,而只是设置了一个简单的“绘制三角形并为其着色”代码块,但它不起作用。

绘图代码:

GLfloat vertices[] = {
    0.5f,  0.5f, 0.0f,  // Top Right
    0.5f, -0.5f, 0.0f,  // Bottom Right
    -0.5f, -0.5f, 0.0f,  // Bottom Left
    -0.5f,  0.5f, 0.0f   // Top Left 
};
GLuint indices[] = {  // Note that we start from 0!
    0, 1, 3,  // First Triangle
    1, 2, 3   // Second Triangle
};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

glBindVertexArray(0); 

while (!win.windowShouldClose) {

    win.clearScreen(glm::vec4(1.0f,1.0f,1.0f,1.0f));
    newShader.Use();
    GLint mvpLoc = glGetUniformLocation(newShader.Program, "MVP");
    glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(camera.proj * camera.view));
    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    win.display();

}

顶点着色器:

#version 430
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;

out vec2 TexCoords;
out vec3 ourColor;

uniform mat4 MVP;

void main()
{
    gl_Position =  MVP * vec4(position,1.0f);
    TexCoords = texCoords;
    ourColor = vec3(1.0f, 0.0f, 1.0f);
}

片段着色器:

#version 430

in vec2 TexCoords;
in vec3 ourColor;
out vec4 color;
uniform sampler2D texture_diffuse1;
void main() {
  color = vec4(ourColor,1.0f);
}

我已经花了几天的时间来解决这个问题,但我还没有找到解决方案。我认为这是我发送到着色器的数据的问题,但即使将片段的输出设置为如图所示的常量,三角形仍然是黑色的,根本不受顶点着色器的影响,就好像它们正在运行一样直接从 NDC 到屏幕空间。

【问题讨论】:

    标签: c++ opengl glsl shader


    【解决方案1】:

    由于您没有显示 camera 属性,我怀疑您的投影和视图矩阵有任何问题(即 MVP 矩阵可能会将对象转换出屏幕)。我刚刚在我的电脑上用glfw 库测试了你的程序,我的测试结果显示你的代码是好的。也就是说,“我认为是我发送给着色器的数据有问题”没有问题

    尝试移除顶点着色器的MVP矩阵:

    #version 430
    layout (location = 0) in vec3 position;
    layout (location = 1) in vec3 normal;
    layout (location = 2) in vec2 texCoords;
    
    out vec2 TexCoords;
    out vec3 ourColor;
    
    uniform mat4 MVP;
    
    void main()
    {
        // Remove MVP matrix
        gl_Position = vec4(position,1.0f);
        TexCoords   = texCoords;
        ourColor    = vec3(1.0f, 0.0f, 1.0f);
    }
    

    【讨论】:

    • 我尝试删除矩阵,但我仍然只是得到一个黑色四边形。矩阵会影响颜色吗?
    • @BDL:感谢您找出答案的未来价值。但是,我确实认为这里没有提供他的问题,这些代码在我的程序中运行良好。
    • @BDL:感谢您的建议。
    • @David Hartman:根据您的测试,我确信您附加的代码没有问题。你的着色器程序有问题吗:glCompileShader,glLinkProgram... 尝试在newShader.Use()之后添加代码行_ASSERTE(glGetError() == GL_NO_ERROR)。如果断言通过与否,请在您的问题后添加着色器程序。
    • 我按照你的要求做了,但断言失败,这对我意味着什么?
    猜你喜欢
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多