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

问题描述 投票:0回答:2

我写了一堆函数来加载collada(.dae)文件,但问题是opengl过剩(控制台)窗口对键盘响应缓慢响应,我只使用了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文件使我的程序响应缓慢,因为我的程序通过每秒加载文件()来绘制顶点,这是加载模型的正确方法吗?

c++ parsing opengl collada
2个回答
2
投票

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

而是读取文件一次并将模型保留在内存中(可能需要预处理一些以简化渲染)。

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


2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.