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 9 of 9

Thread: keyboard events

  1. #1
    Intern Contributor
    Join Date
    Sep 2002
    Posts
    54

    keyboard events

    if you have an object rotating all the time..and you want to stop rotation on pressing a keyboard key..how would you do that...

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: keyboard events

    What are you using to setup your openGL screen, glut or win32?

    But here is a generic answer.

    You need to have control variable.

    object_motion = 0=stopped, 1= rotating.

    My_keyboard_routine(key_input)
    {

    if (key_input == "S") object_motion = 0; //stop
    if (key_input == "R") object_motion = 1; // rotate
    }

    My_object_control()
    {

    if (object_motion == 1) object_rotation++;
    if (object_rotation > 360) object_rotation = 0;

    }

    My_display_routine()
    {

    glRotatef( object_rotation, 0.0, 0.0, 1.0); rotate on Z axis.

    draw_object();

    }

    Hope this give you an idea



    Originally posted by lara:
    if you have an object rotating all the time..and you want to stop rotation on pressing a keyboard key..how would you do that...

  3. #3
    Intern Contributor
    Join Date
    Sep 2002
    Posts
    54

    Re: keyboard events

    I am using glut

    but the generic example helps.
    Thank you

    would u also know why when I include the reshape function in my main program, it only displays for a second and then the window is all blank. otherwise, if i comment out that function, the program works fine...

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: keyboard events

    You could post your code, I could point out what is wrong.
    but I am guessing that when the redisplay routine is called it is changing the viewing area. Thus your object is no longer displayed.

    Here is some of my glut keyboard/ control.

    // put in main function
    glutKeyboardFunc(keyboard);

    // standard keyboard func for glut
    void keyboard (unsigned char key, int x, int y)
    {
    switch (key) {
    case "S":
    object_motion = 0;
    break;
    case "R":
    object_motion = 1;
    break;
    case 27:
    exit(0);
    break;
    default:
    break;
    }
    }

    // add to main function
    glutIdleFunc( My_control );


    void My_control( void )
    {

    if (object_motion == 1) object_rotation++;
    if (object_rotation > 360) obect_rotation = 0;

    }

    glutIdle/keyboard must be placed before glutMainLoop

    Originally posted by lara:
    I am using glut

    but the generic example helps.
    Thank you

    would u also know why when I include the reshape function in my main program, it only displays for a second and then the window is all blank. otherwise, if i comment out that function, the program works fine...


    [This message has been edited by nexusone (edited 09-20-2002).]

  5. #5
    Intern Contributor
    Join Date
    Sep 2002
    Posts
    54

    Re: keyboard events

    this is basically it...it is just the line that is rotating... but the reshape does not seem to work.


    static float rotAngle =2;
    int object_motion = 1;

    void display(void)
    {
    glClear (GL_COLOR_BUFFER_BIT);
    if (object_motion == 1)
    rotAngle-=1;
    glPushMatrix();
    glRotatef(rotAngle, 0.0, 0.0, 0.1);
    glBegin (GL_LINES);
    glVertex2f (0.0, 0.0);
    glVertex2f (0.8, 0.0);
    glEnd ();
    glPopMatrix();
    glFlush();
    }

    void reshape (int w, int h)
    {
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
    }

    void init (void)
    {
    glClearColor (0.0, 0.0, 0.0, 0.0);
    }

    int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (300,300);
    glutInitWindowPosition (200, 200);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(display);
    glutMainLoop();
    return 1;
    }

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: keyboard events

    I see that you are not changing to glMatrixMode(GL_MODELVIEW), when drawing your objects.

    add the following, should fix the problem, next thing you will want to do is use double buffers which will stop the flicker that you maybe having in single mode:

    display()
    {
    glClear (GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW); // We are now working with the model matrix not the projection.
    glLoadIdentity(); Reset the matrix for drawing our scene.

    // rest of your code does here.

    Originally posted by lara:
    this is basically it...it is just the line that is rotating... but the reshape does not seem to work.


    static float rotAngle =2;
    int object_motion = 1;

    void display(void)
    {
    glClear (GL_COLOR_BUFFER_BIT);
    if (object_motion == 1)
    rotAngle-=1;
    glPushMatrix();
    glRotatef(rotAngle, 0.0, 0.0, 0.1);
    glBegin (GL_LINES);
    glVertex2f (0.0, 0.0);
    glVertex2f (0.8, 0.0);
    glEnd ();
    glPopMatrix();
    glFlush();
    }

    void reshape (int w, int h)
    {
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
    }

    void init (void)
    {
    glClearColor (0.0, 0.0, 0.0, 0.0);
    }

    int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (300,300);
    glutInitWindowPosition (200, 200);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(display);
    glutMainLoop();
    return 1;
    }


  7. #7
    Intern Contributor
    Join Date
    Sep 2002
    Posts
    54

    Re: keyboard events

    just trying to play with code, and when i try to change my line such as:
    glPushMatrix();
    glRotatef(rotAngle, 0.2, 0.0, 0.1);
    glBegin (GL_LINES);
    glVertex2f (0.2, 0.0);
    glVertex2f (0.8, 0.0);
    glEnd ();
    glPopMatrix();
    and then run the program, the line rotates differently than going round and round like a needle...
    why would it do so and how can one have any line segment moving like a needle fixed at one end.

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: keyboard events

    First don't use fractions for glRotate (degrees of rotation, 1 or 0 for axis to rotate) 1 = rotate, 0 = no rotation

    Second, you are rotating around the origin, so the must start at (0,0) , else your rotating off origin, so both ends are moving.


    glVertex2f (0.0, 0.0); // origin
    glVertex2f (0.8, 0.0);

    now should go round like a needle on a clock....



    Originally posted by lara:
    just trying to play with code, and when i try to change my line such as:
    glPushMatrix();
    glRotatef(rotAngle, 0.2, 0.0, 0.1);
    glBegin (GL_LINES);
    glVertex2f (0.2, 0.0);
    glVertex2f (0.8, 0.0);
    glEnd ();
    glPopMatrix();
    and then run the program, the line rotates differently than going round and round like a needle...
    why would it do so and how can one have any line segment moving like a needle fixed at one end.

  9. #9
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,797

    Re: keyboard events

    It's ok to use fractions for glRotate if you understand what you're doing. In your case it's rotating around a vector (0.2, 0.0, 0.1) which is similar to rotating around an x-axis that is twisted partly around the y-axis.

    [This message has been edited by Deiussum (edited 09-20-2002).]
    Deiussum
    Software Engineer and OpenGL enthusiast

Posting Permissions

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