Improving performance of opengl sphere generator -
i trying @ least 120fps application, sits around 30fps. if remove sphere creating method, fps goes 120.
my sphere method checks every sphere see if out of bounds. using struct did not improve performance.
xc = x coord yc = y coord zc = y coord xd = x direction yd = y direction zd = z direction is there way drastically improve efficiency?
this code called each frame.
void createspheres() { for(int = 0; < spheres.size(); i+=6) { xc = spheres[i]; yc = spheres[i+1]; zc = spheres[i+2]; xd = spheres[i+3]; yd = spheres[i+4]; zd = spheres[i+5]; if((xc+xd)>= 45 || (xc+xd)<= -45) { xd = 0-xd; } if((yc+yd)>= 9.5 || (yc+yd)<= -9.5) { yd = 0-yd; } if((zc+zd)>= 45 || (zc+zd)<= -45) { zd = 0-zd; } glenable(gl_texture_2d); glbindtexture ( gl_texture_2d, texture_id[6] ); glpushmatrix(); gltranslatef( xc+(xd/10), yc+(yd/10), zc+(zd/10)); glrotatef( -80,1,0,0); glscalef( 0.10f, 0.10f, 0.10f); gluquadrictexture(quadric,1); glusphere(quadric,10.0,72,72); glpopmatrix(); gldisable(gl_texture_2d); spheres[i] = xc+(xd/10); spheres[i+1] = yc+(yd/10); spheres[i+2] = zc+(zd/10); spheres[i+3] = xd; spheres[i+4] = yd; spheres[i+5] = zd; } }
don't use old fixed-function pipeline, create vaos , vbos(http://www.opengl.org/wiki/vertex_specification) vertex attributes of each sphere.
edit: (added more basics vbos , vaos) when rendering without vbos, geometry data send each time render frame. using vbos allows send model data graphics card , render without cpu-gpu bottleneck.
glgenbuffers(1, &vbohandle); // create vbo glbindbuffer(target, vbohandle); // bind vbo glbufferdata(target, size, data, usage); // send vertex data (position, normals, ..) then have establish connection between vbo data , shader attributes
// location of in-attribute of shader program vertexlocation = glgetattriblocation(programhandle,"vertex"); // activate desired vbo glbindbuffer(gl_array_buffer, vbohandle); // set attribute-pointer glvertexattribpointer(vertexlocation, 4, gl_float, gl_false, 0, 0); // enable attribute-array glenablevertexattribarray(vertexlocation); vaos used organize vbos - end following problem:
enable vertex attrib1 enable vertex attrib2 enable vertex attrib3 rendermodel disable vertex attrib1 disable vertex attrib2 disable vertex attrib3 with vaos reduce code effort to
enable vao rendermodel disable vao so combined code fragment may like:
// create , bind vao glgenvertexarrays(1, &vaoid); glbindvertexarray(vaoid); // create , bind vbo glgenbuffers(1, &vboid); glbindbuffer(gl_array_buffer, vboid); glbufferdata(gl_array_buffer, size, data, gl_static_draw); glint loc = glgetattriblocation(programhandle, "attrib1"); glenablevertexattribarray(loc); glvertexattribpointer(loc, 3, gl_float, gl_false, 0, 0); // other vbos, attrib pointers ... // unbindvao glbindvertexarray(0); glbindbuffer(gl_array_buffer, 0); that's minimal amount of information you'll need, recommend read chapters 8 , 12 of opengl superbible 5th edition.
Comments
Post a Comment