Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: glutKeyboardFunc takes 5 frames to update state

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    7

    glutKeyboardFunc takes 5 frames to update state

    Hi!
    I have the keyboard class that initialize the glutKeyboards functions. When I release a key, the program takes five frames to detect. I have another class that when I push the key 's', it add a vec3(1.0,1.0,1.0) to the variable position. The problem is that in each frame from that I press key 's' until it detects that I released it adds to the variable position the vector vec3(1.0,1.0,1.0). The update() function is called before the Draw () function in the render method

    KeyboardDevice.cpp

    Code :
    void KeyboardDevice::Initialize()
    {
        setVisible(false);
        setService(true,2);
        // Initialize glut keyboard functions
        glutKeyboardFunc(keyPressed);
        glutKeyboardUpFunc(keyUp);
        glutSpecialFunc(keySpecial); // Tell GLUT to use the method "keySpecial" for special key presses
        glutSpecialUpFunc(keySpecialUp); // Tell GLUT to use the method "keySpecialUp" for special up key events
    }
    void KeyboardDevice::Load()
    {
     
    }
     
    void KeyboardDevice::Update(){}
    void KeyboardDevice::Draw(){}
     
    void KeyboardDevice::keyPressed (unsigned char key, int x, int y)
    {
        keyStates[key] = true;  // Set the state of the current key to pressed
    }
    void KeyboardDevice::keyUp (unsigned char key, int x, int y)  // it takes 5 frames to detect the release after i press the key.
    {
        keyStates[key] = false;  // Set the state of the current key to not pressed
        timeReleased = clock();
        keyReleaseStates[key] = true; // Set the state of the current key recently pressed
    }
    void KeyboardDevice::keySpecial(int key, int x, int y)
    {
        keySpecialStates[key] = true;
    }
    void KeyboardDevice::keySpecialUp(int key, int x, int y)
    {
        keySpecialStates[key] = false;
        timeReleased = clock();
        keySpecialReleaseStates[key] = true; // Set the state of the current special key recently pressed
    }
     
    bool KeyboardDevice::IsKeyPress(unsigned char key)
    {
        return keyStates[key];
    }
    bool KeyboardDevice::IsKeyUp(unsigned char key)
    {
        double now = clock()/(double)CLOCKS_PER_SEC;
        double last = timeReleased /(double)CLOCKS_PER_SEC;
        if(now-last < 1 && keyReleaseStates[key] == true)
        {
            keyReleaseStates[key] = false;
            return true;
        }
        return false;
    }

    Actor.cpp

    Code :
    void Cube::Update(void)
    {
        if(kb->IsKeyPress('s'))
            position += glm::vec3(1.0 * getParent()->getEngine()->deltaTime);
    }

    Any idea to resolve this?

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,264
    Why don't you perform the calculation as soon as the keyboard button is pressed?
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •