I personaly like to keep all the gltranslate/rotate stuff in my display() routine.
example my mouse routine, only problem is that you only get movement once per click.
void mouse(int button, int state, int x, int y)
{
if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) Worldz++;
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) Worldz–;
}
To have it move until button is released do this:
Note: edit and added rolling function.
void mouse(int button, int state, int x, int y)
{
if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) cube_x = 1; 1 = roll right.
if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_UP)) cube_x = 0; 0 = no motion
// repeat above for left button excecpt cube_x = -1; where -1 = roll left.
}
display()
{
now add rotate and translate and drawing commands
// note I have update a little
glTranslate(cube_position_x, y, z); move right or left.
glRotatef(cube_rotation, 1, 0, 0); Roll cube
Draw cube
}
my_idle_func()
{
cube_position = cube_position + cube_x; // move cube right or left based on cube_x value.
if (cube_position > 10) cube_position = 10; // max movement to the right. Adjust number to match your screen units
if (cube_position < -10) cube_position = -10; // max movement to the left.
// make cube roll
cube_rotation = cube_rotation + cube_x; // rotate roll cube clockwise or counter based on direction of travel.
if (cube_rotation > 360) cube_rotation = 0; // 360 is max of angle.
if (cube_rotation < 0) cube_rotation = 360;
glutRepostDisplay();// last call in idle func.
}
Let me know if there is anything you don’t understand about this code
Note if you reg. you will get an e-mail when someone post a reply to your message.
Originally posted by Claudee:
[b]I trying to roll a cube from left screen to right screen.Here are the coding.I hope somebody can help me as I am unable to call the rollCube function.
Thanks.
void MyMouse(int button,int state,int x,int y){
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
glTranslated(100,0,0);
glRotatef(-90.0,0.0,1.0,0.0);
glTranslated(-100,0,0);
}
else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
glutIdleFunc(NULL);
glFlush();
}[/b]
[This message has been edited by nexusone (edited 07-31-2002).]
[This message has been edited by nexusone (edited 07-31-2002).]