3
votes

Im working with a parser that handles Wavefront OBJ 3D object files and im not quite sure if im doing the loading correctly into OpenGL.

So basically what i do is that i read my Wavefront OBJ files and parse all data.

Normally in OpenGL ES 1.1 i do following when loading data:

glBegin(GL_TRIANGLES);
glNormal3f(normals[faces[i].normal[0]].v[0], normals[faces[i].normal[0]].v[1], normals[faces[i].normal[0]].v[2]);
glVertex3f(vertices[faces[i].vertex[0]].v[0], vertices[faces[i].vertex[0]].v[1], vertices[faces[i].vertex[0]].v[2]);
glNormal3f(normals[faces[i].normal[1]].v[0], normals[faces[i].normal[1]].v[1], normals[faces[i].normal[1]].v[2]);
glVertex3f(vertices[faces[i].vertex[1]].v[0], vertices[faces[i].vertex[1]].v[1], vertices[faces[i].vertex[1]].v[2]);
glNormal3f(normals[faces[i].normal[2]].v[0], normals[faces[i].normal[2]].v[1], normals[faces[i].normal[2]].v[2]);
glVertex3f(vertices[faces[i].vertex[2]].v[0], vertices[faces[i].vertex[2]].v[1], vertices[faces[i].vertex[2]].v[2]);
glEnd();

As for OpenGL ES 2.0 i have tried following for the vertices without any luck:

glBufferData(GL_ARRAY_BUFFER, obj.vertices.size()*sizeof(float), &(obj.vertices[0].v), GL_STATIC_DRAW);

My datastructure:

struct vertex {
    std::vector<float> v;
}

The v vector is created for each vertex offcourse, with {x,y,z}.

class waveObj {
    public:
        std::vector<vertex> vertices;
        std::vector<vertex> texcoords;
        std::vector<vertex> normals;
        std::vector<vertex> parameters;
        std::vector<face> faces;
}

struct face {
    std::vector<int> vertex;
    std::vector<int> texture;
    std::vector<int> normal;
};

How can i load my data as i done with OpenGL ES 1.1 in 2.0?

Also is it even possible to load a vector (v), rather than the positions seperate (float x,y,z)?

1

1 Answers

2
votes

There are several options:

  • Create separate VBO for each of you data: one for position, one for normals, etc
  • Create single VBO with interleaved data - but this would require a bit of code changes in your code.

For start I suggest using one buffer for one vertex attrib + index buffer:

one thing with the index buffer:

  • you have separate indices for pos, normal, texture (you take those values directly from OBJ file), but if you ant to draw geometry using IBO (index buffer object) you need to create sinlge index.

here is some of my code to do that:

map<FaceIndex, GLushort, FaceIndexComparator>::iterator 
           cacheIndex = cache.find(fi);

if (cacheIndex != cache.end()) {
    node->mIndices.push_back(cacheIndex->second);   
}
else {
    node->mPositions.push_back(positions[fi.v]);
    node->mNormals.push_back(normals[fi.n]);
    node->mTexCoords.push_back(texCoords[fi.t]);
    node->mIndices.push_back((unsigned int)node->mPositions.size()-1);

    cache[fi] = ((unsigned int)node->mPositions.size()-1);
}

What it does:

  • it has a vector for each pos, nomal and tex cood... but when there is a "f" flag in OBJ file I check if there is a triple in my cache.
  • if there is such triple I put that index in my node's indices
  • if not I need to create new index