weird framerate

This is a discussion on weird framerate within the Game Programming forums, part of the General Programming Boards category; Here are the things I noticed. They could be way off base but it may help you. At first glance ...

  1. #16
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here are the things I noticed. They could be way off base but it may help you.

    At first glance your fps calculations seem fine so I will just skip over that.

    Main Program Loop:

    Do you calculate the projection matrix every frame? For your program there is no reason to do this. Calculate it once in the beginning and set it before you start your main rendering loop.

    Also, do you create your skybox every frame too? When not just put the skybox very far away? Or if this doesn't work, you could just translate the skybox everyframe to coincide with your camera's position. I would not create it every frame from scratch though, quite unnecessary.

    Rendering Loop:

    File I/O is painfully slow. Why do you open the file every time you draw the terrain? You know that data is never going to change. You need to initialize your own vertex data from the heightmap at initialization time. That why you can just render strips and not have to deal with the file. This will be a big speed up.

    Try to remove as many redundant state changes as you can. For instance in your loop you have glEnable(GL_TEXTURE_2D); Well, you only need to enable that one time outside of the loop. The calls are costing you.

    Just think about it like this. Just walk through a few iterations by hand. Note what gets set and what doesn't. If you notice that one thing is constantly getting set to the same value (or same function getting called, whatever) then take it outside the loop.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  2. #17
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Just to mention something. If your terrain is not going to change, you might want to use a display list for it. Will all those calls it should give a decent increase in speed.

    I haven't tested this, but this should work and give a decent increase in performance.

    Code:
    void DrawTerrain(int DrawWater)
    {
    	glActiveTextureARB		= (PFNGLACTIVETEXTUREARBPROC)		wglGetProcAddress("glActiveTextureARB");
    	glMultiTexCoord2fARB	= (PFNGLMULTITEXCOORD2FARBPROC)		wglGetProcAddress("glMultiTexCoord2fARB");
            static GLuint terrainDisplayList;
            if (terrainDisplayList == 0)
              terrainDisplayList = glGenLists(1);
    
            static char fileLoadedOnce;	
            if (fileLoadedOnce == 0) {
    	  FILE *pFile = fopen(filename, "rb");
    	
      	  fread(&heightmap, TERRAIN_SIZE * TERRAIN_SIZE, 1, pFile);
      	  fclose(pFile);
              fileLoadedOnce++;
            }
    	
                             static char terrainRenderedOnce;
    		         if (terrainRenderedOnce == 0) {
                                    glNewList(terrainDisplayList, GL_COMPILE);
    				for (int z=0;z < TERRAIN_SIZE - 1.; ++z)
    				{
    					glActiveTextureARB(GL_TEXTURE0_ARB);
    					glEnable(GL_TEXTURE_2D);
    					glBindTexture(GL_TEXTURE_2D,  g_Texture[terraintex1id]);
    					glActiveTextureARB(GL_TEXTURE1_ARB);
    					glEnable(GL_TEXTURE_2D);
    					glBindTexture(GL_TEXTURE_2D,  g_Texture[terraintex2id]);
    					//glActiveTextureARB(GL_TEXTURE2_ARB);
    					//glEnable(GL_TEXTURE_2D);
    					//glBindTexture(GL_TEXTURE_2D,  g_Texture[0]);
    					glBegin(GL_TRIANGLE_STRIP);
    					
    					for (int x=0;x < TERRAIN_SIZE; ++x)
    					{
    						// render two vertices of the strip at once
    						float scaledHeight = heightmap[z * TERRAIN_SIZE + x] / SCALE_FACTOR;
    						float nextScaledHeight = heightmap[(z + 1)* TERRAIN_SIZE + x] / SCALE_FACTOR;
    						float color = 0.5f + 0.5f * scaledHeight / MAX_HEIGHT;
    						float nextColor = 0.5f + 0.5f * nextScaledHeight / MAX_HEIGHT;
    
    						
    						glColor3f(color, color, color);
    						glMultiTexCoord2fARB(GL_TEXTURE0_ARB, (GLfloat)x/TERRAIN_SIZE, (GLfloat)z/TERRAIN_SIZE);
    						glMultiTexCoord2fARB(GL_TEXTURE1_ARB, (GLfloat)x/TERRAIN_SIZE, (GLfloat)z/TERRAIN_SIZE);
    						glVertex3f(static_cast<GLfloat>(x - TERRAIN_SIZE/2), scaledHeight, static_cast<GLfloat>(z - TERRAIN_SIZE/2));
    						
    						glColor3f(nextColor, nextColor, nextColor);
    						glMultiTexCoord2fARB(GL_TEXTURE0_ARB, (GLfloat)x/TERRAIN_SIZE, (GLfloat)(z+1)/TERRAIN_SIZE);
    						glMultiTexCoord2fARB(GL_TEXTURE1_ARB, (GLfloat)x/TERRAIN_SIZE, (GLfloat)(z+1)/TERRAIN_SIZE);
    						glVertex3f(static_cast<GLfloat>(x - TERRAIN_SIZE/2), nextScaledHeight, static_cast<GLfloat>(z + 1 - TERRAIN_SIZE/2));	
    					}
    					glEnd();
    				}
                                    glEndList();
                               }
    
                                if (terrainDisplayList != 0)
                                  glCallList(terrainDisplayList);
    				
    				if(DrawWater == 1)
    				{
    					glColor4f(0.9f, 0.9f, 0.9f, 0.15f);
    					glEnable(GL_BLEND);	
    					glBlendFunc(GL_ONE, GL_ONE);
    					glActiveTextureARB(GL_TEXTURE0_ARB);
    					glEnable(GL_TEXTURE_2D);
    					glBindTexture(GL_TEXTURE_2D,  g_Texture[16]);
    					//glActiveTextureARB(GL_TEXTURE1_ARB);
    					//glEnable(GL_TEXTURE_2D);
    					//glBindTexture(GL_TEXTURE_2D,  g_Texture[0]);
    					//glActiveTextureARB(GL_TEXTURE2_ARB);
    					//glEnable(GL_TEXTURE_2D);
    					//glBindTexture(GL_TEXTURE_2D,  g_Texture[0]);
    					
    					glBegin(GL_QUADS);
    					glTexCoord2f(-40.0, 0.0);glVertex3f(-SEA_SIZE / 2, SEA_LEVEL, SEA_SIZE / 2);
    					glTexCoord2f(-40.0, 40.0);glVertex3f(-SEA_SIZE / 2, SEA_LEVEL, -SEA_SIZE / 2);
    					glTexCoord2f(40.0, 40.0);glVertex3f(SEA_SIZE / 2, SEA_LEVEL, -SEA_SIZE / 2);
    					glTexCoord2f(40.0, 0.0);glVertex3f(SEA_SIZE / 2, SEA_LEVEL, SEA_SIZE / 2);
    					glEnd();
    					glDisable(GL_BLEND);
    				}
    				
    				if(DrawWater == 1)
    				{
    					if(g_Camera.m_vPosition.y < OverlayElevation)
    					{
    						WaterOverlay(g_Camera.m_vView.x, g_Camera.m_vView.y, g_Camera.m_vView.z);
    					}
    				}
    				
    }

  3. #18
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    File I/O is painfully slow. Why do you open the file every time you draw the terrain? You know that data is never going to change. You need to initialize your own vertex data from the heightmap at initialization time. That why you can just render strips and not have to deal with the file. This will be a big speed up.
    Yeah, that's exactly what the problem is. All of the other inefficiencies mentioned by Wizard are pertinent and important, but will be relatively small compared to reading the data for the terrain from hard disk every frame. It's actually really, really impressive that your systems managed interactive frame rates period (and that your hard drive didn't melt )

    so go fix that aspect of the code before worrying about other stuff
    See you in 13

  4. #19
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    hmmm....havn't had a chance to test yet....but you mentioned the thing about the file call everyframe......now iv'e been accessing the exe and the files on the AMD computer (the one getting bad FPS) from my other computer over the network....if it has to pull that data over the network every frame, at only 100Mbps over a 50ft cable..thats gonna be pretty slow isn't it....like i said, I havn't tested it yet, but it seems logical.

    -psychopath
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  2. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  3. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  4. framerate vs refreshrate
    By hannibar in forum Game Programming
    Replies: 2
    Last Post: 03-15-2005, 03:40 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21