PDA

View Full Version : keyboard events



lara
09-20-2002, 05:44 AM
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...

nexusone
09-20-2002, 06:06 AM
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

http://www.opengl.org/discussion_boards/ubb/smile.gif


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...

lara
09-20-2002, 06:43 AM
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...

nexusone
09-20-2002, 07:15 AM
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).]

lara
09-20-2002, 07:41 AM
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;
}

nexusone
09-20-2002, 07:55 AM
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;
}

lara
09-20-2002, 09:21 AM
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.

nexusone
09-20-2002, 10:57 AM
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....

http://www.opengl.org/discussion_boards/ubb/smile.gif


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.

Deiussum
09-20-2002, 11:08 AM
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).]