Search code examples
c++openglframe-rate

which one is proper method of writing this gl code


i have been doing some experiments with opengl and handling textures. in my experiment i have a 2d array of (int) which are randomly generated

 int mapskeleton[300][300];

then after that i have my own obj file loader for loading obj with textures

m2d wall,floor;//i initialize and load those files at start

for recording statistics of render times i used

bool Once = 1; 
int secs = 0;

now to the render code here i did my experiment

    //  Code A: Benchmarked on radeon 8670D   
    //  Takes 232(average) millisecs for drawing 300*300 tiles

if(Once)
secs = glutGet(GLUT_ELAPSED_TIME);

for(int i=0;i<mapHeight;i++){
    for(int j=0;j<mapWidth;j++){
        if(mapskeleton[j][i] == skel_Wall){
            glBindTexture(GL_TEXTURE_2D,wall.texture);
            glPushMatrix();
            glTranslatef(j*10,i*10,0);      
            wall.Draw();//Draws 10 textured triangles
            glPopMatrix();
        }
        if(mapskeleton[j][i] == skel_floor){
            glBindTexture(GL_TEXTURE_2D,floor.texture);
            glPushMatrix();
            glTranslatef(j*10,i*10,0);      
            floor.Draw();//Draws 2 textured triangles
            glPopMatrix();
        }           

    }
}
if(Once){
secs = glutGet(GLUT_ELAPSED_TIME)-secs;
printf("time taken for rendering %i msecs",secs)
Once = 0;
}

and other code is

    //  Code B:    Benchmarked on radeon 8670D   
    //  Takes 206(average) millisecs for drawing 300*300 tiles


if(Once)
secs = glutGet(GLUT_ELAPSED_TIME);  

glBindTexture(GL_TEXTURE_2D,floor.texture);

for(int i=0;i<mapHeight;i++){
    for(int j=0;j<mapWidth;j++){
        if(mapskeleton[j][i] == skel_floor){
            glPushMatrix();
            glTranslatef(j*10,i*10,0);      
            floor.Draw();
            glPopMatrix();
        }
    }
}
glBindTexture(GL_TEXTURE_2D,wall.texture);

for(int i=0;i<mapHeight;i++){
    for(int j=0;j<mapWidth;j++){
        if(mapskeleton[j][i] == skel_Wall){
            glPushMatrix();
            glTranslatef(j*10,i*10,0);      
            wall.Draw();
            glPopMatrix();
        }
    }
}   
if(Once){
secs = glutGet(GLUT_ELAPSED_TIME)-secs;
printf("time taken for rendering %i msecs",secs)
Once = 0;
}

for me code A looks good with a point of a person(Beginner) viewing code. but benchmarks say different. my gpu seems to like code B. I don't understand why does code B takes less time to render?


Solution

  • Changes to OpenGL state can generally be expensive - the driver's and/or GPUs data structures and caches can become invalidated. In your case, the change in question is binding a different texture. In code B, you're doing it twice. In code A, you're easily doing it thousands of times.

    When programming OpenGL rendering, you'll generally want to set up the pipeline for settings A, render everything which needs settings A, re-set the pipeline for settings B, render everything which needs settings B, and so on.