gpt4 book ai didi

c++ - 我的 OpenGL 程序的着色器不影响几何图形或为多边形着色

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:21 24 4
gpt4 key购买 nike

我有一个顶点和片段着色器,不久前还运行良好,但我试图实现骨骼动画,但沿线的某个地方出了问题。我什至不再使用我的自定义函数,只是设置了一个简单的“绘制三角形并为其着色”代码块,但它不起作用。

绘图代码:

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 到屏幕空间。

最佳答案

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

enter image description here

尝试移除顶点着色器的 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);
}

关于c++ - 我的 OpenGL 程序的着色器不影响几何图形或为多边形着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43751412/

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