【问题标题】:Loading vertices from .dae makes my program slow, why?从 .dae 加载顶点会使我的程序变慢,为什么?
【发布时间】:2015-01-03 05:16:31
【问题描述】:

我已经编写了一堆函数来加载 collada (.dae) 文档,但问题是 opengl glut (console) 窗口对键盘响应的响应很慢,我只使用了 string.h、stdlib.h、和 fstream.h,当然还有 gl/glut.h 我的程序的主要功能是:

Void LoadModel()
{
COLLADA ca;
double digits[3];
ca.OpenFile(char fname);
ca.EnterLibGeo();// get the position of <library_geometries>
ca.GetFloats();// search for the <float_array> from start to end, and saves thier position in the file
ca.GetAtrributes("count", char Attrib); //same as collada dom's function but its mine
Int run=atoi(Attrib); // to convert the attributes of count which is string in the file to integer
glBegin(GL_TRIANGLES);
for (int i=0;i<=run;i++)
{
MakeFloats(digits); // will convert string digits to floating point values, this function uses the starting position and ending position which GetFloats() stored in variables
glVertex3f(digits[0], digits[1], digitd[2]);
}
glEnd();
glFlush();
}

此应用程序在不将整个文件内容加载到内存的情况下搜索标签,LoadModel() 函数将由 void display() 调用,因此每当我尝试使用 glut 的键盘功能时,它都会从文件中重新加载顶点数据,这对于小的 .dae 文件是可以的,但是大的 .dae 文件使我的程序响应缓慢,这是因为我的程序通过每秒加载 file() 来绘制顶点,这是加载模型的正确方法吗??

【问题讨论】:

    标签: c++ parsing opengl collada


    【解决方案1】:

    每次渲染网格时,您都在读取文件; 不要这样做

    改为读取文件一次并将模型保存在内存中(可能进行了一些预处理以简化渲染)。

    根据你的例子加载网格的VBO方法是:

    COLLADA ca;
    double digits[3];
    ca.OpenFile(char fname);
    ca.EnterLibGeo();// get the position of <library_geometries>
    ca.GetFloats();// search for the <float_array> from start to end, and saves thier position in the file
    ca.GetAtrributes("count", char Attrib); //same as collada dom's function but its mine
    Int run=atoi(Attrib); // to convert the attributes of count which is string in the file to integer
    
    int vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, run*3*sizeof(float), 0, GL_STATIC_DRAW);
    do{
        void* ptr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
        for (int i=0;i<=run;i++)
        {
            MakeFloats(digits); // will convert string digits to floating point values, this function uses the starting position and ending position which GetFloats() stored in variables
            memcpy(ptr+i*3*sizeof(float), digits, 3*sizeof(float));
        }
    }while(!glUnmapBuffer(GL_ARRAY_BUFFER));//if buffer got corrupted then remap and do again
    

    然后就可以绑定相对缓冲区并用glDrawArrays进行绘制

    【讨论】:

    • 谢谢棘轮,stonemetal,我想这应该对我有帮助,我知道但不知道如何防止它,谢谢!!
    【解决方案2】:

    磁盘 IO 相对较慢,很可能是您所看到的缓慢。您应该尝试从绘图功能中删除任何不必要的工作。仅在启动时加载一次文件,然后将数据保存在内存中。如果您根据按键加载不同的文件,请预先加载所有文件或按需加载一次。

    【讨论】:

    • 我该怎么做?我试过这样:我使用了一个全局 int 变量来计算函数的运行次数,并放置了一个 if 函数,但这会导致模型无法旋转,有什么解决方案吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    相关资源
    最近更新 更多