Fastest way to load arrays of vertices and face indices into OpenGL-ES?
Asked Answered
S

2

3

I'm trying to load .obj files that I've formatted into:

vertexX vertexY vertexZ normalX normalY normalZ

and:

index1 index2 index3

format into vector and vector arrays, which I then directly render in Opengl-ES. My problem is, when I try to load the model into the arrays, it takes about 40 seconds to load them in. I'm not sure why it's going so slow, I've seen others code load the same model in just a few seconds. Any suggestions? My code for loading the file is below:

-(void)loadModel:(NSString*)filePath
{
    try {

    ifstream objFile([filePath UTF8String]);
    objFile >> numVertices;
    objFile.ignore(128, '\n');
    vertices.resize(numVertices*6);
    VertexNormal* vertex = (VertexNormal*) &vertices[0];
    svec3* faceDef;

    while (objFile) {
        char c = objFile.get();

        switch (c) {
            case 'v':
            {
                objFile >> vertex->vertices.x >> vertex->vertices.y >> vertex->vertices.z
                >> vertex->normals.x >> vertex->normals.y >> vertex->normals.z;
                vertex++;
                break;
            }
            case 'f':
            {
                objFile >> faceDef->x >> faceDef->y >> faceDef->z;
                faceDef++;
                break;
            }
            case '#':
            {
                part newPart;
                partList.push_back(newPart);
                numObjects++;
                objFile.ignore(128, '\n');
                int numFaces;
                objFile >> numFaces;
                partList[partList.size()-1].faces.resize(numFaces*3);
                partList[partList.size()-1].numFaces = numFaces;
                faceDef = (svec3*) &partList[partList.size()-1].faces[0];
                break;
            }
            default:
                break;
        }
        objFile.ignore(128, '\n');
    }
    objFile.close();
    free(objFile);
    } catch (NSException *ex) {
        NSLog(@"%@", [ex reason]);
    }
}

One line of thought I had was to serialize the arrays into a binary file, then just deserialize them straight into my program. Haven't figured out how to do that yet though, but maybe something along those lines might be a solution.

Sport answered 1/12, 2010 at 19:47 Comment(0)
M
4

The best practice in game industry is to keep all models data in binary format, so you can very fast load whole non-interleaved blocks of memory that can be represented as vertices, normals or anything else. All you need for this - make small command line tool for converting text .obj files into your own binary file.

Also:

  • Did you try to do text loading with stdio library, not stl ifstream?
  • Maybe try to read all the text data once and fill arrays from memory, not from filesystem?
  • how much parts in file do you have? Each resize of std::vector leads to new allocation and copying. Try to reserve space in std::vector, if you know desired volume before.
Mercuri answered 2/12, 2010 at 20:3 Comment(2)
Great suggestion. I ended up writing my own binary format on a PC. Haven't tested on the iPad yet, but load time on the PC went from 33 seconds to 1 second, and file size went from 17MB to 7MB uncompressed, 3MB to 1.8MB compressed. Much better. Thanks.Sport
@Sport can you point me to how to write own binary format? "33 seconds to 1 second" just WOWW.Lui
G
0

I don't know if you've considered this, but if your .obj files won't change in your application, you can format them as objective-C arrays and compile them directly with your code.

Gelinas answered 1/12, 2010 at 22:17 Comment(3)
I've never done that before, do you have a code example on how you would do that? Sounds like it might be what I'm looking for.Sport
Sorry, I forgot to clarify. This may change what you are suggesting, but I'd like the user to be able to open NEW .obj files from their device and view those in my OpenGL viewer. So static files are a no go, unless there is a way to basically have the code for the arrays in a text file and have objective-c compile those arrays directly during runtime.Sport
@Sport Sorry, I don't really have any experience with objective-C. It was just a suggestion.Gelinas

© 2022 - 2024 — McMap. All rights reserved.