从.dae加载顶点会使我的程序变慢,为什么
Loading vertices from .dae makes my program slow, why?
我写了很多函数来加载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相对较慢,很可能是您所看到的速度较慢。您应该尝试从绘图函数中删除任何不必要的工作。启动时只加载一次文件,然后将数据保存在内存中。如果您根据按键加载不同的文件,可以预先加载所有文件,也可以按需加载一次。
相关文章:
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 为什么异常不退出程序?
- 为什么我的 IExtractIcon 处理程序没有被调用?
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- WIN32:C++,为什么在WM_CLOSE上调用Messagebox函数程序正在冻结
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 为什么这个程序的结果是3 "born"?和 4 死
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 尽管我的功能和cout语句的放置错误,但我的程序为什么会起作用
- C++简单的程序 - 为什么我的"res"不进入我的向量?创建一个接受输入 (4+44-67+235) 并输出结果的程序
- 额外的反斜杠字符不会影响我的程序.为什么
- linux C/C++套接字程序为什么没有出现ECONNRESET错误
- C++程序.为什么这段代码有效
- C++迭代程序-为什么我必须添加.begin()才能获得二进制搜索的中点
- 迭代程序.为什么要使用它们