使用 "in" 关键字传递整数顶点属性时出现问题



我正在研究骨骼动画。我有一个顶点结构它看起来就像

struct MeshVertex
{
glm::vec3 pos;
glm::vec3 normal;
glm::vec2 tex;
glm::vec3 tangent;
glm::vec3 bitangent;
uint32_t ids[4] = {};
float weights[4] = {};
void print() const;
};

网格是一个基本的立方体与一个骨头。因此ids = {0,0,0,0}weights = {1.0f,0.0f,0.0f,0.0f}对应于每个顶点。在我的网格类,我有一个静态函数Mesh::genFormat()处理属性。vao是一个静态的int在网格类和for_i只是一个方便的宏我用来做循环。注意,我正确地使用了glVertexArrayAttribI格式。

Mesh::Mesh(const std::vector<MeshVertex>& vertices, const std::vector<uint>& indices, const std::vector<Texture>& textures)
{
m_textures = textures;
m_num_indices = indices.size();

// create vertex and index buffers
glCreateBuffers(1, &m_vbo);
glCreateBuffers(1, &m_ibo);
glNamedBufferData(m_vbo, sizeof(MeshVertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
glNamedBufferData(m_ibo, sizeof(uint) * indices.size(), &indices[0], GL_STATIC_DRAW);
}
void Mesh::genFormat()
{
glCreateVertexArrays(1, &vao);
for_i(7) { glEnableVertexArrayAttrib(vao, i); }
glVertexArrayAttribFormat(vao, 0, 3, GL_FLOAT, false, offsetof(MeshVertex, pos)));
glVertexArrayAttribFormat(vao, 1, 3, GL_FLOAT, false, offsetof(MeshVertex, normal));
glVertexArrayAttribFormat(vao, 2, 2, GL_FLOAT, false, offsetof(MeshVertex, tex));
glVertexArrayAttribFormat(vao, 3, 3, GL_FLOAT, false, offsetof(MeshVertex, tangent));
glVertexArrayAttribFormat(vao, 4, 3, GL_FLOAT, false, offsetof(MeshVertex, bitangent));

glVertexArrayAttribIFormat(vao, 5, 4, GL_UNSIGNED_INT, offsetof(MeshVertex, ids)));
glVertexArrayAttribFormat(vao, 6, 4, GL_FLOAT, false, offsetof(MeshVertex, weights)));
for_i(7) { glVertexArrayAttribBinding(vao, i, 0); }
glBindVertexArray(0);
}

下面的GLSL不会渲染任何东西。

#version 460 core
layout(location = 0) in vec3 Pos;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec2 Tex;
layout(location = 3) in vec3 Tan;
layout(location = 4) in vec3 BiTan;
layout(location = 5) in uvec4 BoneIds;
layout(location = 6) in vec4 Weights;
out vec3 normal;
out vec2 tex;
layout(binding = 2, std140) uniform Camera
{
mat4 VP;
vec4 cpos;
};
uniform mat4 node;
uniform mat4 bones_inverse_bind_mesh_parent[50];
void main()
{
tex = Tex;
mat4 W = mat4(0.0f);
if (Weights[0] != 0.0f)
{
for (uint i = 0; i < 4; i++)
W = W + (Weights[i] * bones_inverse_bind_mesh_parent[BoneIds[i]]);

W = node * W;
}
else
W = node;

gl_Position = VP * W * vec4(Pos, 1.0);
}

由于BoneIds[i]总是零,如果我替换

W = W + (Weights[i] * bones_inverse_bind_mesh_parent[BoneIds[i]]);

W = W + (Weights[i] * bones_inverse_bind_mesh_parent[0]);

的结果应该保持不变。我的矩阵变换目前有点偏离(稍后修复),但现在立方体呈现良好。所以BoneIds有问题。在对这个问题绞尽脑汁一段时间后,我把

改成了
layout(location = 5) in uvec4 BoneIds;

layout(location = 5) varying uvec4 BoneIds;

看到一些旧的GLSL在线后,现在一切正常。我不明白的是为什么。我在互联网上看到很多GLSL代码使用in关键字处理整数属性。

更新:

如果我替换glVertexArrayAttribMesh::genFormat()格式

glVertexArrayAttribFormat(vao, 5, 4, GL_UNSIGNED_INT, false, offsetof(MeshVertex, ids));

c++中的和

layout(location = 5) in vec4 BoneIds;

在GLSL中和在GLSL代码中将骨id从float转换为int,代码也可以工作。

好了,我解决了这个问题,尽管我不太明白这是如何解决问题的。我喜欢的图形处理器是自动的,但当我强迫它在我的集成图形上使用NVIDIA处理器时,一切都很好。解决方案图像

更新:

我认为这就像我的英特尔处理器图形支持OpenGL 4.4和glVertexArrayAttribIFormat在OpenGL 4.5中出现一样简单。

最新更新