从.dae加载顶点会使我的程序变慢,为什么

Loading vertices from .dae makes my program slow, why?

本文关键字:程序 为什么 我的 dae 加载 顶点      更新时间:2023-10-16

我写了很多函数来加载collada(.dae)文档,但问题是opengl glut(控制台)窗口对键盘响应缓慢,我只使用了string.h、stdlib.h和fstream.h,当然还有gl/glust.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文件来说是可以的,但大的.dea文件使我的程序响应缓慢,因为我的程序每秒都通过加载文件()来绘制顶点,这是装载模型的正确方式吗??

每次渲染网格时,都会读取文件的每一端不要那样做

相反,只读取一次文件,并将模型保存在内存中(可能会进行一些预处理以简化渲染)。

基于您的示例加载网格的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 进行绘制

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