Rishabh
Rishabh

Reputation: 62

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

i ve written a bunch of functions to load the collada (.dae) document, but the problem is the opengl glut (console) window responds slowly to keyboard reponses, i've used only string.h, stdlib.h, and fstream.h, and ofcourse gl/glut.h my program's main functions are:

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();
}

this application search for tags without loading the whole file contents into memory, LoadModel() function will be called by void display(), so whenever i try to use the keyboard function of glut, it reloads the vertex data from the file, this is ok for small .dae files but the large .dae files make my program respond slow, its because my program draws vertex by loading the file() each second, is this the right way loading models??

Upvotes: 0

Views: 283

Answers (2)

ratchet freak
ratchet freak

Reputation: 48216

You are reading in the file each end every time you render the mesh; don't do that.

Instead read the file once and keep the model in memory (perhaps preprocessed a bit to ease rendering).

The VBO method of loading the mesh based on your example is:

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

Then you can bind the relative buffer and draw with glDrawArrays

Upvotes: 2

stonemetal
stonemetal

Reputation: 6208

Disk IO is relatively slow, and is more than likely the slowness you are seeing. You should try to remove any unnecessary work from your draw function. Only load your file once at start up then keep the data in memory. If you load different files based on key press either load all of them up front or once on demand.

Upvotes: 2

Related Questions