0

I am trying to get at least 120fps with an application, but it currently sits around 30fps. If I remove my sphere creating method, the fps goes to 120.

My sphere method checks every sphere to see if it is out of bounds. Using a 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 any way that I could drastically improve efficiency?

This code is called each frame.

void createSpheres()
{
     for(int i = 0; i < 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;  

    }
}
  • 3
    Are you creating spheres on each frame? – Michael IV May 03 '13 at 11:26
  • At first glance, you can set separate variables for xc+(xd/10), yc+(yd/10) and zc+(zd/10) after your if's and pass those to glTranslatef and assign them to spheres[i], spheres[i+1] and spheres[i+2], so you only have to calculate them once. I doubt it will have the impact you are hoping for, but depending on the length of your loop, it can improve performance. – Kevin May 03 '13 at 12:26
  • @MichaelIV It's called each frame yes – Adam Kenworthy May 03 '13 at 13:48
  • 2
    Why not create the sphere geometry once and then transform it as necessary? Alternatively, depending on how you want to use the spheres, you could try using impostors instead of lots of triangles: http://stackoverflow.com/questions/10488086/drawing-a-sphere-in-opengl-es/10506172#10506172 – Brad Larson May 03 '13 at 15:10
  • how about changing gluSphere(quadric,10.0,72,72); – Adam Kenworthy May 04 '13 at 14:31

1 Answers1

1

Don't use the old fixed-function pipeline, create VAOs and VBOs(http://www.opengl.org/wiki/Vertex_Specification) for your vertex attributes of each sphere.

Edit: (added more basics about VBOs and VAOs) When you are rendering without VBOs, the geometry data is send each time you render a frame. Using VBOs allows you to send model data to your graphics card and then to render without the cpu-gpu bottleneck.

glGenBuffers(1, &vboHandle);            // create vbo
glBindBuffer(target, vboHandle);        // bind vbo
glBufferData(target, size, data, usage); // send vertex data (position, normals, ..)

then you have to establish a connection between your VBO data and shader attributes

// get location of the in-attribute of the 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);
// finally enable attribute-array
glEnableVertexAttribArray(vertexLocation);

VAOs are used to organize your VBOs - Usually you end up with the following problem:

enable vertex attrib1
enable vertex attrib2
enable vertex attrib3

renderModel

disable vertex attrib1
disable vertex attrib2
disable vertex attrib3

With VAOs you reduce the code effort to

enable VAO

renderModel

disable VAO

So a combined code fragment may look like:

// Create and bind VAO
glGenVertexArrays(1, &vaoId); 
glBindVertexArray(vaoId);

// create and 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 probably the minimal amount of information you'll need, I recommend to read the chapters 8 and 12 of the opengl superbible 5th edition.

dinony
  • 614
  • 3
  • 13