我正在使用WaveFront .obj Loader,我的第一个目标是从下面的.obj文件中加载所有顶点和索引(显示带有弯曲边缘的立方体)。
# Blender v2.62 (sub 0) OBJ File: 'Cube.blend'
# www.blender.org
o Cube
v 0.900000 -1.000000 -0.900000
v 0.900000 -1.000000 0.900000
v -0.900000 -1.000000 0.900000
v -0.900000 -1.000000 -0.900000
v 0.900000 1.000000 -0.900000
v 0.899999 1.000000 0.900001
v -0.900000 1.000000 0.900000
v -0.900000 1.000000 -0.900000
v 1.000000 -0.900000 -1.000000
v 1.000000 -0.900000 1.000000
v -1.000000 -0.900000 1.000000
v -1.000000 -0.900000 -1.000000
v 1.000000 0.905000 -0.999999
v 0.999999 0.905000 1.000001
v -1.000000 0.905000 1.000000
v -1.000000 0.905000 -1.000000
f 1//1 2//1 3//1
f 1//1 3//1 4//1
f 13//2 9//2 12//2
f 13//2 12//2 16//2
f 5//3 13//3 16//3
f 5//3 16//3 8//3
f 15//4 7//4 8//4
f 15//5 8//5 16//5
f 11//6 15//6 16//6
f 11//6 16//6 12//6
f 14//7 6//7 7//7
f 14//7 7//7 15//7
f 10//8 14//8 11//8
f 14//8 15//8 11//8
f 13//9 5//9 6//9
f 13//9 6//9 14//9
f 9//10 13//10 10//10
f 13//11 14//11 10//11
f 9//12 1//12 4//12
f 9//12 4//12 12//12
f 3//13 11//13 12//13
f 3//14 12//14 4//14
f 2//15 10//15 11//15
f 2//15 11//15 3//15
f 1//16 9//16 10//16
f 1//16 10//16 2//16
f 5//17 8//17 7//17
f 5//17 7//17 6//17
我已将所有顶点和索引加载到名为verticesVec和indicesVec的两个向量中,并且我已经检查了它们两者(它们的输入顺序正确,verticesVec [0] = 0.9且indicesVec [0] = 1)。我将顶点加载到VertexBufferObject中,并将VerticArrayObject中的索引与着色器字段设置一起加载。
然而,当我渲染立方体时,我得到以下图像,这是不正确的。我已经重新检查了我的所有代码,但我找不到我出错的部分。它与向量和向量的字节大小有关吗?
wrong cube http://imageshack.us/a/img850/6249/wrongcube.png
以下代码来自我的Draw / Render方法,它执行所有渲染。
glUseProgram(theProgram);
GLuint modelToCameraMatrixUnif = glGetUniformLocation(theProgram, "modelToCameraMatrix");
GLuint perspectiveMatrixUnif = glGetUniformLocation(theProgram, "cameraToClipMatrix");
glUniformMatrix4fv(perspectiveMatrixUnif, 1, GL_FALSE, glm::value_ptr(cameraToClipMatrix));
glm::mat4 identity(1.0f);
glutil::MatrixStack stack(identity); // Load identity Matrix
// Push for Camera Stuff
glutil::PushStack camera(stack);
stack.Translate(glm::vec3(camX, camY, camZ));
//Push one step further to fix object 1
glutil::PushStack push(stack);
stack.Translate(glm::vec3(0.0f, 0.0f, 3.0f));
// Draw Blender object
glBindVertexArray(this->load.vertexArrayOBject);
glUniformMatrix4fv(modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(stack.Top()));
glDrawElements(GL_TRIANGLES, this->load.indicesVec.size(), GL_UNSIGNED_SHORT, 0);
// Pop camera (call destructor to de-initialize all dynamic memory)
camera.~PushStack();
// Now reset all buffers/programs
glBindVertexArray(0);
glUseProgram(0);
我的.obj加载器中缓冲区对象的初始化:
void Loader::generateVertexBufferObjects()
{
// Fils the vertex buffer object with data
glGenBuffers(1, &vertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, verticesVec.size() * 4, &verticesVec[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Fills the index buffer object with its data
glGenBuffers(1, &indexBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size() * 4, &indicesVec[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void Loader::generateVertexArrayObjects()
{
glGenVertexArrays(1, &vertexArrayOBject);
glBindVertexArray(vertexArrayOBject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
glBindVertexArray(0);
}
答案 0 :(得分:3)
您将索引创建为无符号整数,但是您传递的是OpenGL,它们的类型为GL_UNSIGNED_SHORT。 glDrawElements的type参数必须与上传到索引缓冲区的数据类型相匹配。