Reputation: 75
I create a .obj loader that reads from a mesh file that was created in blender 2.66 and loads the data into a std::vector that is passed to Opengl as a VBO. None of the vertices in the mesh file have a position of (0,0,0). So what could be a possible reason that the mesh is getting that extra vertex in the center? As the original mesh is just a triangulated plane with only four vertices.
struct Vertex
{
//Position
GLfloat m_X;
GLfloat m_Y;
GLfloat m_Z;
//Normal
GLfloat m_NX;
GLfloat m_NY;
GLfloat m_NZ;
//TexCoords
GLfloat m_U;
GLfloat m_V;
};
void Mesh::loadMesh(std::string Filename)
{
...
const unsigned int PositionAttribute = 0;
const unsigned int NormalAttribute = 1;
const unsigned int TexCoordAttribute = 2;
//Create a new VBO and use the variable id to store the VBO id
glGenBuffers(1, &MeshVBO);
//make the new VBO active
glBindBuffer(GL_ARRAY_BUFFER, MeshVBO);
//Pass the Mesh's vertex data into the VBO to be transferred to super fast Video RAM
glBufferData(GL_ARRAY_BUFFER, m_Vertices.size() * sizeof(Vertex), &m_Vertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(PositionAttribute);
glEnableVertexAttribArray(NormalAttribute);
glEnableVertexAttribArray(TexCoordAttribute);
//specifies the location and data of an array of vertex
glVertexAttribPointer(PositionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
glVertexAttribPointer(NormalAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(GLfloat) * 3)); //12 byte offset
glVertexAttribPointer(TexCoordAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(GLfloat) * 6)); //24 byte offset
//make the new VBO active
glBindBuffer(GL_ARRAY_BUFFER, MeshVBO);
glGenBuffers(1, &MeshIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, MeshIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (sizeof(GLushort) * m_IndexList.size()), &m_IndexList[0], GL_STATIC_DRAW);
//Cleanup
delete[] m_VertexArray;
delete[] m_Indices;
m_VertexArray = NULL;
m_Indices = NULL;
return Success;
}
void Mesh::drawMesh()
{
glDrawElements(GL_POINTS, (GLsizei)m_IndexList.size() + 1, GL_UNSIGNED_SHORT, 0);
}
Plane.obj
# Blender v2.66 (sub 1) OBJ File: ''
# www.blender.org
mtllib plane.mtl
o Plane
v 0.622129 -0.622129 0.000000
v -0.622129 -0.622129 0.000000
v 0.622129 0.622129 -0.000000
v -0.622129 0.622129 -0.000000
vn 1.000000 0.000000 0.000000
usemtl None
s off
f 2//1 1//1 3//1
f 4//1 2//1 3//1
Upvotes: 0
Views: 469
Reputation: 54592
Your main problem is that indices in OBJ files are 1-based, while OpenGL uses 0-based indices. This means that you will have to subtract 1 from the indices you read from the OBJ file. This is most conveniently done right after you read the indices in your parsing code, because most programming languages and APIs use 0-based indices.
If you want to be able to read a wider selection of OBJ files, you will also have to handle negative index values in the input file. This doesn't seem to be used as commonly, and you may be able to skip this if the files come from a well controlled source (e.g. you always generate them yourself with the same software). If present, negative indices are relative to the end of the vertices read so far. E.g. index -1 is the latest vertex read, -2 the second latest, etc.
In the posted code, you added 1 to the number of available indices in the draw call:
glDrawElements(GL_POINTS, (GLsizei)m_IndexList.size() + 1, GL_UNSIGNED_SHORT, 0);
I suspect that you might have done this to compensate for the wrong index values. Once you have the correct index values, you should remove the + 1
.
For more detailed documentation of the OBJ format, the Wikipedia page (http://en.wikipedia.org/wiki/Wavefront_.obj_file) contains a good overview. The most detailed and thorough definition I ever found is here: http://www.martinreddy.net/gfx/3d/OBJ.spec. The OBJ format can contain fairly advanced features, like NURBS. If you write your own parser and renderer, you will probably want to settle for a subset. Otherwise you'll be busy for quite some time.
If you want more in-depth information about how to render meshes from OBJ files efficiently, here are a few more of my related answers to earlier questions:
Upvotes: 4