我想要做的是在VBO和VAO的帮助下渲染交错的顶点数组。不幸的是,场景没有显示纹理,只有常规灰色三角形。然而,所有顶点位置似乎都被正确绘制。我几乎倾向于认为问题是一个错误的索引或属性设置,但在阅读了很多关于glVertexAttribPointer的Stride和Pointer参数的问题后,我仔细检查过。为了排除数据结构和其他东西的问题,我试图用glBegin / glTexCoord2f / glVertex3f / glEnd渲染一切,它工作正常(但很慢)。
我的班级成员:
vector<unsigned short> m_indexVec;
vector<PackedVertex> m_vertVec;
GLuint m_textureId;
GLuint m_vboIdVertex;
GLuint m_vboIdIndex;
GLuint m_vao;
我的顶点结构:
struct PackedVertex
{
glm::vec3 position;
glm::vec2 uv;
glm::vec3 normal;
}
VAO / VBO初始值:
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
glGenBuffers(1, &m_vboIdVertex);
glGenBuffers(1, &m_vboIdIndex);
glBindBuffer(GL_ARRAY_BUFFER, m_vboIdVertex);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboIdIndex);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(PackedVertex), (const GLvoid *)(offsetof(PackedVertex,position)) ); // vertices
glEnableVertexAttribArray((GLuint)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(PackedVertex), (const GLvoid *)(offsetof(PackedVertex,uv)) ); // uv
glEnableVertexAttribArray((GLuint)1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(PackedVertex), (const GLvoid *)(offsetof(PackedVertex,normal)) ); // normals
glEnableVertexAttribArray((GLuint)2);
glBufferData(GL_ARRAY_BUFFER, sizeof(PackedVertex) * m_vertVec.size(), &m_vertVec[0], GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * m_indexVec.size(), &m_indexVec[0], GL_STATIC_DRAW);
glBindVertexArray(0);
渲染方法:
glBindTexture( GL_TEXTURE_2D, m_textureId );
glBindVertexArray(m_vao);
glDrawElements(GL_TRIANGLES, m_indexVec.size(), GL_UNSIGNED_SHORT, (const GLvoid*)0);
glBindVertexArray(0);
顶点着色器:
#version 330
in vec4 vVertex;
in vec2 vTexCoords;
uniform mat4 mvp;
smooth out vec2 uv;
void main()
{
gl_Position = mvp * vVertex;
uv = vTexCoords;
}
片段着色器:
#version 330
in vec2 uv;
uniform sampler2D colortexture;
out vec4 vFragColor;
void main()
{
vFragColor = texture(colortexture, uv.st);
}
激活着色器后:
GLint t = glGetUniformLocation(UVShader, "colortexture");
glUniform1i(t, textureUnit);
glm::mat4 glm_PModelViewMatrix = glm_ProjectionMatrix * glm_ViewMatrix;
GLint MVPLocation = glGetUniformLocation(shaderPrograms[ShaderNames::UVShader], "mvp");
gluniformmatrix4fv(MVPLocation, 1, GL_FALSE, glm::value_ptr(glm_PModelViewMatrix) );
现在我将着色器更改为:
顶点着色器:
#version 330
layout (location=0) in vec4 vVertex;
layout (location=1) in vec2 vTexCoords;
uniform mat4 mvp;
smooth out vec2 uv;
void main()
{
gl_Position = mvp * vVertex;
uv = vTexCoords;
}
片段着色器:
#version 330
in vec2 uv;
uniform sampler2D colortexture;
out vec4 vFragColor;
void main()
{
vFragColor = texture(colortexture, uv.st);
}
...但是当我使用“布局”功能时,我看不到任何变化。一切都像以前一样被纹理化,但我的访问冲突的问题仍然存在(仅在发布版本中)。我认为glBufferData复制的数据或glDrawElements访问的数据仍然存在问题。