0

I want to write a program with two buffers and have a frame rate of 30 frames per second showing in the console. I'm using Open GL - C++

    Display()
    {
      glutSwapBuffers();
    }

Timer for fps:

     void mytimer(int fps)
     {
       glutTimerFunc(1000/30 , mytimer, 0);
       glutPostRedisplay();
     }

This code draws a line but I want to draw a line at a frame rate 30 fps.

      void drawScene (void){
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_LINES);
        glVertex2f(0.0,0.0);
        glVertex2f(120.0,120.0);
        glEnd();
        glFlush();
      }

     int main (int argc ,char** argv){
       glutInit(&argc,argv);
       glutInitDisplayMode(GLUT_RGB);
       glutInitWindowSize(360,360);
       glutCreateWindow("mohammad");
       initRendering();
       mytimer(fps);
       //glutDisplayFunc(drawScene);
       glutDisplayFunc(Display);
       glutMainLoop();
       return (0);
     }
genpfault
  • 51,148
  • 11
  • 85
  • 139
Mahdiyar Mzm
  • 9
  • 1
  • 5

2 Answers2

0

glutTimerFunc(1000/30 , mytimer, 0); tells GLUT "from now on, call mytimer every 1000/30th milliseconds". You probably want to call it from main. Otherwise GLUT will have no idea that you've written a timer function.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

First step.

Do you have this line in your code?

glutIdleFunc(idle);

If yes, go to idle() method and comment everything inside. You need to force glutIdleFunc() to do nothing. Normally you have this:

    // executes every time if there is nothing to do basically.
void idle()
{
    glutPostRedisplay(); //draw a frame
}
// callback
glutIdleFunc(idle);

Since the callback (glutIdleFunc) calls idle() every time if opengl has nothing better to do, then you can't control the fps, you need to disable this. Then, your timer should work perfectly.

Here is my piece of code to limit to 60 fps. Works great.

    void frame_limit(int value)
{
    glutPostRedisplay();
    glutTimerFunc(1000 / 60, frame_limit, 0);
}

void idle()
{
    //glutPostRedisplay();
}



void setupCallbacks()
{
    glutCloseFunc(cleanup);
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    glutTimerFunc(1000 / 60, frame_limit, 0);
}

void init(int argc, char* argv[])
{
    createShaderProgram();
    createBufferObjects();
    setupCallbacks();
}

int main(int argc, char* argv[])
{
    init(argc, argv);
    glutMainLoop();
    exit(EXIT_SUCCESS);
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Volodymyr Balytskyy
  • 577
  • 1
  • 7
  • 19