将顶点数组和面索引加载到OpenGL-ES中的最快方法? [英] Fastest way to load arrays of vertices and face indices into OpenGL-ES?

查看:18
本文介绍了将顶点数组和面索引加载到OpenGL-ES中的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载已格式化的 .obj 文件:

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

vertexX vertexY vertexZ normalX normalY normalZ

和:

index1 index2 index3

格式化成向量和向量数组,然后我直接在 Opengl-ES 中渲染.我的问题是,当我尝试将模型加载到数组中时,加载它们大约需要 40 秒.我不知道为什么它会这么慢,我已经看到其他代码只加载了几个相同的模型秒.有什么建议?我加载文件的代码如下:

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, '
');
    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, '
');
                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, '
');
    }
    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.

推荐答案

游戏行业的最佳实践是将所有模型数据保存为二进制格式,这样您就可以非常快速地加载可以表示的整个非交错内存块作为顶点,法线或其他任何东西.您所需要的一切 - 制作用于将文本 .obj 文件转换为您自己的二进制文件的小型命令行工具.

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.

还有:

  • 您是否尝试使用 stdio 库而不是 stl ifstream 进行文本加载?
  • 也许尝试一次读取所有文本数据并从内存中填充数组,而不是从文件系统中?
  • 文件中有多少部分?每次调整 std::vector 的大小都会导致新的分配和复制.如果您之前知道所需的卷,请尝试在 std::vector 中保留空间.

这篇关于将顶点数组和面索引加载到OpenGL-ES中的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆