Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: OpenGL Setting FPS

  1. #1
    Member Contributor
    Join Date
    Jun 2009
    Posts
    90

    OpenGL Setting FPS

    I'm trying to load the changing colors of a model in a .txt file. So this file could contain 100 values and I want to be able to have a integer passed in the .txt to set the FPS that these values will be rendered.
    Is this possible? I read mixed things on the topic of Fixed FPS and wanted the advice of the community since I don't know the credibility of the sources I read.

    Thanks

  2. #2
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: OpenGL Setting FPS

    Use "float curGameTime; " instead of "int curFrame;" .

    On consoles (fixed hardware capabilities and power), you can use the "int curFrame" approach if you're certain there won't be frame-drops, and you target only a specific refresh-rate.
    On a PC, some users will have highest-end gaming rigs, others will be using last decade's laptops; some will have force-disabled vsync in the driver panels; and anyway some users run in 60Hz refresh-rate, others at 75Hz, others at 100Hz, etc.

    Your text-file:
    Code :
    // time  R G B
    0.0 :  0.4,0.4,0.4
    5.7 :  1.0,0.3,0.4
    14.3 :  1.0,0.3,0.4
    ...

    Your frame-update code:

    Code :
    float g_CurGameTime;
     
    vec3 Color;
     
    void updateScene(float frameDuration){
    	foreach(ScriptEntry e){ // optimize this...
    		if(g_CurGameTime>=e->time &amp;&amp; e->time<(g_CurGameTime+frameDuration)){
    			Color = e->color;
    		}
    	}
    	...
    	g_CurGameTime+=frameDuration;
    }
    It could be better or useful to interpolate entries via linear or spline-interpolation. There are articles/tutes on it, keywords are: animation, script, spline, lerp, interpolation, timing, events.

  3. #3
    Member Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: OpenGL Setting FPS

    Thanks for the quick response.

    One clarifying question, do I call this function in the display function I use as a GLUT callback? Because if so, wouldn't this only change the variable for the period of time specified rather than setting the actually FPS?

    I might be missing something tho...

  4. #4
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: OpenGL Setting FPS

    Yes, inside the display-function, call this updateScene(). You'll have to measure how much time the previous frame took, and supply it as the "frameDuration" parameter.

    Do not think in terms of "FPS" (frames per second), think in terms of "time passed". For some frames, your code might take 3ms , for others you might take 44ms.

  5. #5
    Member Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: OpenGL Setting FPS

    Illian,

    I was hoping to control the actual render rate of OpenGL. To "throttle down" the rendering, if you will.

    Is this possible?

  6. #6
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: OpenGL Setting FPS

    wglSwapIntervalEXT(0) - no frame-limit
    wglSwapIntervalEXT(1) - vsync (i.e 60Hz, depends on current monitor mode)
    wglSwapIntervalEXT(2) - vsync/2 (if vsync is at 60Hz, you'll update at 30Hz)
    wglSwapIntervalEXT(3) - vsync/3 (if vsync is at 60Hz, you'll update at 20Hz)
    etc....


    Anything else: OS-dependent timers: Sleep, polling-loops, events, etc etc. Generally anything but polling-loops will be imprecise.

  7. #7
    Member Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: OpenGL Setting FPS

    I call that... when? Prior to rendering anything? Any 30Hz doesn't correlate to 30FPS, right? Is there a way to tell what the Hz cooresponds to.

    Thanks!

  8. #8
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: OpenGL Setting FPS

    You call wglSwapIntervalEXT only once. There are specs about it on this site.
    http://en.wikipedia.org/wiki/Hertz

  9. #9
    Senior Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    408

    Re: OpenGL Setting FPS

    Since you are using GLUT, you can use glutGet(GLUT_ELAPSED_TIME) to measure time in a cross-platform way. One thing to be aware of when using any timer function, they are not guaranteed precise beyond 10-100 millisecond. You can get around that by averaging multiple frames over a second (10xprecision of timer).

    If you just want to control the speed of the display loop you can simply put a blocking condition in the display loop. I have attached a piece of minimal code that measures FPS with averaging and a blocking condition in the display loop to throttle the speed of the display (to turn on the throttling loop change in the idle() function "#undef REGULATE_FPS" to "#define REGULATE_FPS"). When REGULATE_FPS is undef'ed then rendering loop will go as fast as hardware allows and FPS will be >>60 (assuming vsync off on my machine I measure FPS ~8000) and when REGULATE_FPS is define'ed then the FPS will be fixed to 1 frame per 35milliseconds = ~29FPS which FPS() will measure. Note that it takes approximately 1 second after startup for the FPS() to accurately begin displaying the measured frames per second in the printf call.

    ps for advanced users: if you want to go non-cross platform to avoid having an imprecise clock measure, please try to avoid QueryPerformanceCounter in windows since the Performance counter value may unexpectedly leap forward. One approach I have found is to use the CPU RDSTC assembly op-code on Intel/AMD processors -- see the wiki at Time Stamp Counter and for working code find cycle.h at the bottom. I like RDTSC opcode since it works in linux as well as windows. But note, in my applications I have been perfectly happy to live with averaging over multiple frames to deal with the imprecise clock functions. end of comment for advanced users.

    Code :
    //this shows one way to get FPS using an inprecise clock
    //or to throttle the drawing rate to a fixed FPS
    //g++ glut_fps_demo.cpp -lGL -lglut
    #include <cstdlib>
    #include <iostream>
     
    //linux openGL headers
    #include <GL/gl.h>
    #include <GL/glut.h>
     
    GLint gFramesPerSecond = 0;
     
    void FPS(void) {
      static GLint Frames = 0;         // frames averaged over 1000mS
      static GLuint Clock;             // [milliSeconds]
      static GLuint PreviousClock = 0; // [milliSeconds]
      static GLuint NextClock = 0;     // [milliSeconds]
     
      ++Frames;
      Clock = glutGet(GLUT_ELAPSED_TIME); //has limited resolution, so average over 1000mS
      if ( Clock < NextClock ) return;
     
      gFramesPerSecond = Frames/1; // store the averaged number of frames per second
     
      PreviousClock = Clock;
      NextClock = Clock+1000; // 1000mS=1S in the future
      Frames=0;
    }
     
    void idle() {
      #define REGULATE_FPS
      #ifdef REGULATE_FPS
      static GLuint PreviousClock=glutGet(GLUT_ELAPSED_TIME);
      static GLuint Clock=glutGet(GLUT_ELAPSED_TIME);
      static GLfloat deltaT;
     
      Clock = glutGet(GLUT_ELAPSED_TIME);
      deltaT=Clock-PreviousClock;
      if (deltaT < 35) {return;} else {PreviousClock=Clock;}
      #endif
     
      //put your specific idle code here
      //... this code will run at FPS
      printf(".");
      //end your specific idle code here
     
      FPS(); //only call once per frame loop 
      glutPostRedisplay();
    }
     
    void display() {
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glClear(GL_COLOR_BUFFER_BIT);
     
      // Set the drawing color (RGB: WHITE)
      printf("FPS %d\n",gFramesPerSecond);
      glColor3f(1.0,1.0,1.0);
     
      glBegin(GL_LINE_STRIP); {
         glVertex3f(0.25,0.25,0.0);
         glVertex3f(0.75,0.25,0.0);
         glVertex3f(0.75,0.75,0.0);
         glVertex3f(0.25,0.75,0.0);
         glVertex3f(0.25,0.25,0.0);
      }
      glEnd(); 
     
      glutSwapBuffers();
    }
     
    void init() {
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0); 
    }
     
    void keyboard(unsigned char key, int x, int y)
    {
       switch (key) {
          case 27:  // escape key
             exit(0);
             break;
          default:
             break;
       }
    }
     
    int main(int argc, char** argv) {
       glutInit(&amp;argc, argv);
       glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
       glutCreateWindow("FPS test");
     
       glutIdleFunc(idle);
       glutDisplayFunc(display);
       glutKeyboardFunc(keyboard);
     
       init();
     
       glutMainLoop();
       return 0;
    }

  10. #10
    Member Contributor
    Join Date
    Jun 2009
    Posts
    90

    Re: OpenGL Setting FPS

    Thanks for the posts! Very helpful, I'll probably go with the non-vsync changing route.

Page 1 of 2 12 LastLast

Similar Threads

  1. Setting up OpenGL in VC++
    By oliver_kenny in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-23-2015, 10:42 PM
  2. Setting up Opengl
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-14-2004, 09:29 AM
  3. Setting up OpenGl under VC7
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-22-2004, 11:38 AM
  4. Setting up OpenGL
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-23-2002, 05:23 PM
  5. help setting up openGL!!
    By in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-28-2002, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean