我正在尝试让一些基本着色器在 OpenGL 中工作,但我似乎在第一个障碍处遇到了障碍。我正在尝试启用一些顶点属性,但得到了奇怪的结果。我已经在 RenderDoc 中提出了绘制调用,并且仅启用了顶点属性 0。这是我的 VAO 制作代码和渲染循环。我可能忽略了一些非常明显的事情。谢谢!
std::vector<float> positions;
std::vector<float> normals;
std::vector<float> texCoords;
for (auto x : model->positions)
{
positions.push_back(x.x);
positions.push_back(x.y);
positions.push_back(x.z);
}
for (auto x : model->normals)
{
normals.push_back(x.x);
normals.push_back(x.y);
normals.push_back(x.z);
}
for (auto x : model->texCoords)
{
texCoords.push_back(x.x);
texCoords.push_back(x.y);
}
GLuint indicesVBO = 0;
GLuint texCoordsVBO = 0;
GLuint vertsVBO = 0;
GLuint normsVBO = 0;
glGenVertexArrays(1, &model->vao);
glBindVertexArray(model->vao);
glGenBuffers(1, &vertsVBO);
glBindBuffer(GL_ARRAY_BUFFER, vertsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * positions.size(), positions.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &normsVBO);
glBindBuffer(GL_ARRAY_BUFFER, normsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * normals.size(), normals.data(), GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &texCoordsVBO);
glBindBuffer(GL_ARRAY_BUFFER, texCoordsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * texCoords.size(), texCoords.data(), GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
glEnableVertexAttribArray(2);
glGenBuffers(1, &indicesVBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesVBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, model->indices.size() * sizeof(uint32_t), model->indices.data(), GL_STATIC_DRAW);
glBindVertexArray(0);
我的渲染循环是这样的:
//I'm aware this isn't usually needed but I'm just trying to make sure
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
for (GamePiece * x : gamePieces)
{
glUseProgram(x->program->programID);
glBindVertexArray(x->model->vao);
glBindTexture(GL_TEXTURE_2D, x->texture->texID);
glDrawElements(GL_TRIANGLES, x->model->indices.size(), GL_UNSIGNED_INT,(void*)0);
}
还有我的顶点着色器:
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 texCoord;
out vec2 outUV;
out vec3 outNormal;
void main()
{
outUV = texCoord;
outNormal = normal;
gl_Position = vec4(position, 1.0f);
}
#version 330
in vec2 inUV;
in vec3 normal;
out vec4 outFragcolor;
uniform sampler2D colourTexture;
void main()
{
outFragcolor = texture2D(colourTexture, inUV);
}
最佳答案
参见OpenGL 4.5 Core Profile Specification - 7.3.1 Program Interfaces ,第 96 页:
[...] When a program is linked, the GL builds a list of active resources for each interface. [...] For example, variables might be considered inactive if they are declared but not used in executable code, [...] The set of active resources for any interface is implementation-dependent because it depends on various analysis and optimizations performed by the compiler and linker
这意味着,如果编译器和链接器确定某个属性变量“未使用”,则在执行可执行代码时,该属性将处于非事件状态。
非事件属性不是事件程序资源,因此在 RenderDoc 中不可见。
此外,着色器阶段的输出变量通过其名称链接到下一个着色器阶段的输入变量。
texCoord
不是事件程序资源,因为它被分配给输出变量 outUV
。片段着色器没有输入变量outUV
。
顶点着色器:
out vec2 outUV; out vec3 outNormal;
片段着色器:
in vec2 inUV; in vec3 normal;
参见Program separation linkage :
对顶点着色器的输出和片段着色器的输入使用相同的名称,或者使用布局位置链接接口(interface)变量:
顶点着色器:
layout(location = 0) out vec2 outUV;
layout(location = 1) out vec3 outNormal;
片段着色器:
layout(location = 0) in vec2 inUV;
layout(location = 1) in vec3 normal;
关于c++ - OpenGL 顶点属性未启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59777258/