Constant game speed independent of variable FPS in OpenGL with GLUT?

后端 未结 3 557
一个人的身影
一个人的身影 2021-02-03 12:16

I\'ve been reading Koen Witters detailed article about different game loop solutions but I\'m having some problems implementing the last one with GLUT, which is the recommended

相关标签:
3条回答
  • 2021-02-03 12:42

    You could use glutIdleFunc, which is called continuously whenever possible--similar to the while(game_is_running) loop. That is, whatever logic you would otherwise put into that while loop, you could put into the callback for glutIdleFunc. You can avoid using glutTimerFunc by keeping track of the ticks on your own, as in the article you linked (using glutGet(GLUT_ELAPSED_TIME)).

    0 讨论(0)
  • 2021-02-03 12:44

    Have, as an example, a mouse-driven rotation matrix that updates at a fixed frame-rate, independently of the rendering frame-rate. In my program, space-bar toggles benchmarking mode, and determines the Boolean fxFPS.

    Let go of the mouse button while dragging, and you can 'throw' an object transformed by this matrix.

    If fxFPS is true then the rendering frame-rate is throttled to the animation frame-rate; otherwise identical frames are drawn repeatedly for benchmarking, even though not enough milliseconds will have passed to trigger any animation.

    If you're thinking about slowing down AND speeding up frames, you have to think carefully about whether you mean rendering or animation frames in each case. In this example, render throttling for simple animations is combined with animation acceleration, for any cases when frames might be dropped in a potentially slow animation.

    To accelerate the animation, rotations are performed repeatedly in a loop. Such a loop is not too slow compared with the option of doing trig with an adaptive rotation angle; just be careful what you put inside any loop that actually takes longer to execute, the lower the FPS. This loop takes far less than an extra frame to complete, for each frame-drop that it accounts for, so it's reasonably safe.

    int xSt, ySt, xCr, yCr, msM = 0, msOld = 0;
    bool dragging = false, spin = false, moving = false;
    glm::mat4 mouseRot(1.0f), continRot(1.0f);
    float twoOvHght; // Set in reshape()
    glm::mat4 mouseRotate(bool slow) {
        glm::vec3 axis(twoOvHght * (yCr - ySt), twoOvHght * (xCr - xSt), 0); // Perpendicular to mouse motion
        float len = glm::length(axis);
        if (slow) { // Slow rotation; divide angle by mouse-delay in milliseconds; it is multiplied by frame delay to speed it up later
            int msP = msM - msOld;
            len /= (msP != 0 ? msP : 1);
        }
        if (len != 0) axis = glm::normalize(axis); else axis = glm::vec3(0.0f, 0.0f, 1.0f);
        return rotate(axis, cosf(len), sinf(len));
    }
    void mouseMotion(int x, int y) {
        moving = (xCr != x) | (yCr != y);
        if (dragging & moving) {
            xSt = xCr; xCr = x; ySt = yCr; yCr = y; msOld = msM; msM = glutGet(GLUT_ELAPSED_TIME);
            mouseRot = mouseRotate(false) * mouseRot;
        }
    }
    void mouseButton(int button, int state, int x, int y) {
        if (button == 0) {
            if (state == 0) {
                dragging = true; moving = false; spin = false;
                xCr = x; yCr = y; msM = glutGet(GLUT_ELAPSED_TIME);
                glutPostRedisplay();
            } else {
                dragging = false; spin = moving;
                if (spin) continRot = mouseRotate(true);
            }
        }
    }
    

    And then later...

    bool fxFPS = false;
    int T = 0, ms = 0;
    const int fDel = 20;
    void display() {
        ms = glutGet(GLUT_ELAPSED_TIME);
        if (T <= ms) { T = ms + fDel;
            for (int lp = 0; lp < fDel; lp++) {
                orient = rotY * orient; orientCu = rotX * rotY * orientCu; // Auto-rotate two orientation quaternions
                if (spin) mouseRot = continRot * mouseRot; // Track rotation from thowing action by mouse
            }
            orient1 = glm::mat4_cast(orient); orient2 = glm::mat4_cast(orientCu);
        }
        // Top secret animation code that will make me rich goes here
        glutSwapBuffers();
        if (spin | dragging) { if (fxFPS) while (glutGet(GLUT_ELAPSED_TIME) < T); glutPostRedisplay(); } // Fast, repeated updates of the screen
    }
    

    Enjoy throwing things around an axis; I find that most people do. Notice that the fps affects nothing whatsoever, in the interface or the rendering. I've minimised the use of divisions, so comparisons should be nice and accurate, and any inaccuracy in the clock does not accumulate unnecessarily.

    Syncing of multiplayer games is another 18 conversations, I would judge.

    0 讨论(0)
  • 2021-02-03 13:07

    glut is designed to be the game loop. When you call glutMainLoop(), it executes a 'for loop' with no termination condition except the exit() signal. You can implement your program kind of like you're doing now, but you need some minor changes. First, if you want to know what the FPS is, you should put that tracking into the renderScene() function, not in your update function. Naturally, your update function is being called as fast as specified by the timer and you're treating elapsedTime as a measure of time between frames. In general, that will be true because you're calling glutPostRedisplay rather slowly and glut won't try to update the screen if it doesn't need to (there's no need to redraw if the scene hasn't changed). However, there are other times that renderScene will be called. For example, if you drag something across the window. If you did that, you'd see a higher FPS (if you were properly tracking the FPS in the render function).

    0 讨论(0)
提交回复
热议问题