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

查看:137
本文介绍了将顶点和面部索引数组加载到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, '\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.

推荐答案

最好的游戏行业的实践是将所有模型数据保持为二进制格式,因此您可以非常快速地加载整个非交错的内存块,可以表示为顶点,法线或其他任何内容。所需的一切 - 制作小型命令行工具,将文本.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天全站免登陆