我正在尝试让一些基本的着色器在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页:
这意味着,如果编译器和链接器确定属性变量“未使用”,则在执行可执行代码时,该属性将处于非 Activity 状态。
非 Activity 属性不是 Activity 的程序资源,因此在RenderDoc中不可见。
此外,着色器阶段的输出变量通过名称链接到下一个着色器阶段的输入变量。texCoord
不是 Activity 的程序资源,因为它已分配给输出变量outUV
。片段着色器没有输入变量outUV
。
顶点着色器:
片段着色器:
参见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/