c++ - 为什么法 vector 的顶点属性不起作用?

标签 c++ opengl opengl-3

我正在尝试使用 OpenGL 实现漫射照明。我调试了立方体的片段着色器,发现法 vector 始终为 (0,0,0),即使我在顶点数据中指定了法线并启用了顶点属性。

相关OpenGL代码:

GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
-0.5f,  0.5f, -0.5f,  0.0f,  0.0f, -1.0f,
-0.5f, -0.5f, -0.5f,  0.0f,  0.0f, -1.0f,

-0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
-0.5f,  0.5f,  0.5f,  0.0f,  0.0f,  1.0f,
-0.5f, -0.5f,  0.5f,  0.0f,  0.0f,  1.0f,

-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
-0.5f,  0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
-0.5f, -0.5f, -0.5f, -1.0f,  0.0f,  0.0f,
-0.5f, -0.5f,  0.5f, -1.0f,  0.0f,  0.0f,
-0.5f,  0.5f,  0.5f, -1.0f,  0.0f,  0.0f,

0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
0.5f,  0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
0.5f, -0.5f, -0.5f,  1.0f,  0.0f,  0.0f,
0.5f, -0.5f,  0.5f,  1.0f,  0.0f,  0.0f,
0.5f,  0.5f,  0.5f,  1.0f,  0.0f,  0.0f,

-0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,
0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
-0.5f, -0.5f,  0.5f,  0.0f, -1.0f,  0.0f,
-0.5f, -0.5f, -0.5f,  0.0f, -1.0f,  0.0f,

-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,
0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
-0.5f,  0.5f,  0.5f,  0.0f,  1.0f,  0.0f,
-0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f
};

GLuint VBO, boxVAO;
glGenVertexArrays(1, &boxVAO);
glGenBuffers(1, &VBO);

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

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

顶点着色器:

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
out vec3 FragPos;
out vec3 outNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{
    gl_Position = proj * view * model * vec4(position, 1.0);
    FragPos = vec3(model * vec4(position, 1.0f));
    outNormal = vec3(normal);
}

片段着色器:

#version 330 core
in vec3 FragPos;
in vec3 outNormal;
out vec4 color;

uniform vec3 objectColor;
uniform vec3 lightColor;
uniform vec3 lightPos;

void main()
{
    float ambientIntensity = 0.5f;
    vec3 ambientColor = ambientIntensity * lightColor;

    vec3 norm = normalize(outNormal);
    vec3 lightDir = normalize(lightPos - FragPos);
    float diffuse = max(dot(norm, lightDir), 0.0);
    vec3 diffuseColor = diffuse * lightColor;

    vec3 resultColor = (ambientColor + diffuseColor) * objectColor;
    color = vec4(resultColor, 1.0f);
}

输出:(仅显示环境光,无漫反射)

enter image description here

此外,不确定这是否有帮助,但做一些奇怪的事情,比如将法线设置为片段位置会产生这样的结果: enter image description here

所以看起来法线应该产生漫射光,但不知何故没有正确加载到着色器中。有什么想法吗?

这是我用来调试着色器的代码:

// debug testing
vec3 test = vec3(outNormal.xyz);
bvec3 ln = lessThan(test, vec3(0,0,0));
if (ln[2]){
    color = vec4(1.0, 0.0, 0.0, 1.0);
}else{
    color = vec4(0.0, 1.0, 0.0, 1.0);
}

最佳答案

糟糕!事实证明,当我创建灯光 VAO 时,我使用了相同的顶点,但只使用每个顶点的前三个值作为位置(这对灯光来说是正确的)。但是我不小心使用了那个 VAO 来绘制立方体,而不是上面代码中显示的 VAO。真不敢相信我没听清楚。无论如何,这里的代码是正确的。

和平。

关于c++ - 为什么法 vector 的顶点属性不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36223279/

相关文章:

c++ - 使用#pragma once 有什么危险?

c++ - 如何从 OpenGL 4.5 中的单个立方体贴图纹理创建立方体贴图阵列纹理?

objective-c - 苹果机,OpenGL 4 : drawing on FrameBuffer does not work

OpenGL统一采样器缓冲区: what does it point to?

opengl - 我如何使这个简单的 OpenGL 代码(在 "lenient"3.3 和 4.2 配置文件中工作)在严格的 3.2 和 4.2 核心配置文件中工作?

c++ - 如何使用冒泡排序对链表进行排序?

c# - 共享来自 C#、C++/CLI 和 C++ 的枚举

c++ - 检查构造函数中的许可证

linux - 静态编译glfw

opengl - 如何在 OpenGL 中通过 2D 屏幕鼠标单击大型对象获得准确的 3D 深度?