Thread: weird framerate

  1. #16
    Registered User
    Join Date
    Apr 2002
    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
    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.

    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);
                             static char terrainRenderedOnce;
    		         if (terrainRenderedOnce == 0) {
                                    glNewList(terrainDisplayList, GL_COMPILE);
    				for (int z=0;z < TERRAIN_SIZE - 1.; ++z)
    					glBindTexture(GL_TEXTURE_2D,  g_Texture[terraintex1id]);
    					glBindTexture(GL_TEXTURE_2D,  g_Texture[terraintex2id]);
    					//glBindTexture(GL_TEXTURE_2D,  g_Texture[0]);
    					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));	
                                if (terrainDisplayList != 0)
    				if(DrawWater == 1)
    					glColor4f(0.9f, 0.9f, 0.9f, 0.15f);
    					glBlendFunc(GL_ONE, GL_ONE);
    					glBindTexture(GL_TEXTURE_2D,  g_Texture[16]);
    					//glBindTexture(GL_TEXTURE_2D,  g_Texture[0]);
    					//glBindTexture(GL_TEXTURE_2D,  g_Texture[0]);
    					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);
    				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
    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
    Where circles begin.
    hmmm....havn't had a chance to test yet....but you mentioned the thing about the file call 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 i said, I havn't tested it yet, but it seems logical.

    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

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, 11: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, 04:40 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM