The fastest way to load vertex arrays and face indices in OpenGL-ES?

I am trying to download .obj files that I formatted:

vertexX vertexY vertexZ normalX normalY normalZ 

and

 index1 index2 index3 

into vector and vector arrays, which I then directly visualize in Opengl-ES. My problem is that when I try to load a model into arrays, it takes about 40 seconds to load them. I'm not sure why this is happening so slowly, I saw that other codes load the same model in just a few seconds. Any suggestions? My code for downloading 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 of my thoughts was to serialize the arrays to a binary, and then just deserialize them directly into my program. We have not yet figured out how to do this, but perhaps something along these lines may be the solution.

+3
objective-c iphone opengl-es objective-c ++
source share
2 answers

The best practice in the gaming industry is to save all these model data in binary format, so you can very quickly load entire unmovable blocks of memory that can be represented as vertices, normals, or something else. All you need for this is to make a small command line tool to convert .obj text files to your own binary.

also:

  • Have you tried loading text using the stdio library, not stl ifstream?
  • Maybe try to read all the text data once and fill the arrays from memory, and not from the file system?
  • how many parts do you have in the file? Each resizing of std :: vector leads to a new distribution and copying. Try to reserve a place in std :: vector if you know the desired volume before.
+4
source share

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

0
source share

All Articles