views:

200

answers:

1

OK. I'm confused.

I'm developing a 3D game using SDL and OpenGL on Ubuntu 9.04 using Eclipse CDT. I've got a class to hold the mesh data in vectors for each type. Such as Vertex, Normal, UVcoord (texture coordinates), as well as a vector of faces. Each face has 3 int vectors which hold indexes to the other data. So far my game has been working quite well at rendering at nice rates. But then again I only had less then one hundred vertexes among two objects for testing purposes.

The loop accessing this data looks like this:

void RenderFace(oFace face)
{
    /*
    * More Stuff
    */

    oVertice gvert;
    oUVcoord tvert;
    oNormal nvert;

    for (unsigned int fvIndex = 0; fvIndex < face.GeoVerts.size(); fvIndex++)
    {
        gvert = obj.TheMesh.GetVertice(face.GeoVerts[fvIndex] - 1);
        tvert = obj.TheMesh.GetUVcoord(face.UV_Verts[fvIndex] - 1);
        nvert = obj.TheMesh.GetNormal(face.NrmVerts[fvIndex] - 1);

        glNormal3f(nvert.X, nvert.Y, nvert.Z);
        glTexCoord2f(tvert.U, tvert.V);
        glVertex3f(scale * gvert.X, scale * gvert.Y, scale * gvert.Z);
    }

    /*
    *  More Stuff
    */
}

There is a loop that calls the renderFace() funtion which includes the above for loop. The minus one is because Wavefront .obj files are 1 indexed (instead of c++ 0 index). Anyway, I discovered that once you have about 30 thousand or so faces, all those calls to glVertex3f() and the like slow the game down to about 10 FPS. That I can't allow. So I learned about vertex arrays, which require pointers to arrays. Following the example of a NeHe tutorial I continued to use my oVertice class and the others. Which just have floats x, y, z, or u, v. So I added the same function above to my OnLoad() function to build the arrays which are just "oVertice*" and similar.

Here is the code:

bool oEntity::OnLoad(std::string FileName)
{
    if (!obj.OnLoad(FileName))
    {
        return false;
    }

    unsigned int flsize = obj.TheMesh.GetFaceListSize();

    obj.TheMesh.VertListPointer = new oVertice[flsize];
    obj.TheMesh.UVlistPointer = new oUVcoord[flsize];
    obj.TheMesh.NormListPointer = new oNormal[flsize];

    oFace face = obj.TheMesh.GetFace(0);
    oVertice gvert;
    oUVcoord tvert;
    oNormal nvert;
    unsigned int counter = 0;
    unsigned int temp = 0;

    for (unsigned int flIndex = 0; flIndex < obj.TheMesh.GetFaceListSize(); flIndex++)
    {
        face = obj.TheMesh.GetFace(flIndex);

        for (unsigned int fvIndex = 0; fvIndex < face.GeoVerts.size(); fvIndex++)
        {
            temp = face.GeoVerts[fvIndex];
            gvert = obj.TheMesh.GetVertice(face.GeoVerts[fvIndex] - 1);

            temp = face.UV_Verts[fvIndex];
            tvert = obj.TheMesh.GetUVcoord(face.UV_Verts[fvIndex] - 1);

            temp = face.NrmVerts[fvIndex];
            nvert = obj.TheMesh.GetNormal(face.NrmVerts[fvIndex] - 1);

            obj.TheMesh.VertListPointer[counter].X = gvert.X;
            obj.TheMesh.VertListPointer[counter].Y = gvert.Y;
            obj.TheMesh.VertListPointer[counter].Z = gvert.Z;

            obj.TheMesh.UVlistPointer[counter].U = tvert.U;
            obj.TheMesh.UVlistPointer[counter].V = tvert.V;

            obj.TheMesh.NormListPointer[counter].X = nvert.X;
            obj.TheMesh.NormListPointer[counter].Y = nvert.Y;
            obj.TheMesh.NormListPointer[counter].Z = nvert.Z;

            counter++;
        }
    }

    return true;
}

The unsigned int temp variable is for debugging purposes. Apparently I don't have a default constructor for oFace that doesn't have something to initialize with. Anyway, as you can see it's pretty much that same exact routine. Only instead of calling a gl function I add the data to three arrays.

Here's the kicker:

I'm loading a typical cube made of triangles.

When I access element 16 (0 indexed) of the UV_Verts vector from the RenderFace() function I get 12.

But when I access element 16 (0 indexed) of the same UV_Verts vector from the OnLoad() function I get something like 3045472189

I am so confused.

Does anyone know what's causing this? And if so how to resolve it?

+3  A: 

One possible reason could be that you're creating arrays with size flsize:

obj.TheMesh.VertListPointer = new oVertice[flsize];
obj.TheMesh.UVlistPointer = new oUVcoord[flsize];
obj.TheMesh.NormListPointer = new oNormal[flsize];

but use the arrays with indices up to flsize * face.GeoVerts.size

for (...; flIndex < obj.TheMesh.GetFaceListSize(); ...) { // flsize = GetFaceListSize
  for (...; fvIndex < face.GeoVerts.size(); ...) {
    ...
    obj.TheMesh.UVlistPointer[counter].U = ...;
    ...
    counter++;
  }
}

so your array creation code should actually be more like

obj.TheMesh.VertListPointer = new oVertice[flsize * face.GeoVerts.size()];
obj.TheMesh.UVlistPointer = new oUVcoord[flsize * face.GeoVerts.size()];
obj.TheMesh.NormListPointer = new oNormal[flsize * face.GeoVerts.size()];
schnaader