Louis Kuang Louis Kuang - 1 year ago 112
C++ Question

OpenGL constant rotation angle

I am doing some openGL programming lately and it invovles basic matrix transformation such as translation, rotation and scaling. I encounter some problems when doing rotation. Here is my question.

Now I am using a variable rotationDegree and a variable rotationStepSize to control the rotation. When the rotation flag is on

//inside paintGL function
if(rotationFlag is on)
rotationDegree += rotationStepSize
if(rotationDegree > 360.0f)
rotationDegree -= 360.0f

Here's the strange thing, Since I define rotationStepSize to be very small, the rotation starts out very slow, but then as time increases it gets faster and faster!

I come up with two explanations for this phenomena:

  • 360f is not the range of values for the degree parameter in glm::rotate

  • The program starts out slow, causing paintGL to be painted to screen less. Then as the program become steady(or other parameter is not changing), the mainLoopEvent is executing faster and faster, causing paintGL to be painted more.

Does anyone know how to solve this problem? I googled about using glutget(GL_TIME_ELAPSED), but on my machine, this function reports "glutget: missing ENUM handle", which indicates that my glut file is not complete, I guess?

So does anyone know how to fix the enum problem or how to get around this to create a scene where I have an object rotating in constant speed?

Thanks a lot!

Answer Source

According to the freeglut_state.c, there is the glutGet function defined.

int FGAPIENTRY glutGet( GLenum eWhat )
    int returnValue ;
    GLboolean boolValue ;

    switch (eWhat)
        case GLUT_INIT_STATE:
        return fgState.Initialised;

        case GLUT_ELAPSED_TIME:
        return fgElapsedTime();

I'm not using freeglut, but look at the docmentation, maybe you should try GLUT_ELAPSED_TIME instead of GL_TIME_ELAPSED?

And calculate the deltatime like this:

int preTime= 0;
while( ... )
     int currentTime= glutGet(GLUT_ELAPSED_TIME);
     int deltaTime = currentTime- preTime;
     preTime = currentTime;

     //... pass the deltaTime to whatever you want...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download