Reputation: 21
I created this code to draw an object and move it in the direction of x,y or z according to keyboard buttons.
It works perfect and doesn't output any error but the object just appears without moving.
Here is the code:
#include <GL/glut.h>
static GLint rotate = 0;
static GLint axis = 0;
static int xDegrees = 0;
static int yDegrees = 0;
static int zDegrees = 0;
static int direction = 0;
static int stop = 0;
void specialKeyboard( int axis, int x, int y )
{
switch( axis )
{
case GLUT_KEY_RIGHT:
direction = 1;
break;
case GLUT_KEY_LEFT:
direction = -1;
break;
}
}
void change( int* degrees )
{
*degrees = ( ( *degrees ) + direction ) % 360;
}
void rotate1()
{
switch( axis )
{
case '0':
change( &xDegrees );
break;
case '1':
change( &yDegrees );
break;
case '2':
change( &zDegrees );
break;
}
glutPostRedisplay();
}
void keyboard( unsigned char key, int x, int y )
{
switch( key )
{
case 'x':
axis = 0;
glutIdleFunc( rotate1 );
stop = 0;
break;
case 'y':
axis = 1;
glutIdleFunc( rotate1 );
stop = 0;
break;
case 'z':
axis = 2;
glutIdleFunc( rotate1 );
stop = 0;
break;
case 's':
stop = !stop;
if( stop )
glutIdleFunc( NULL );
else
glutIdleFunc( rotate1 );
break;
}
}
void resize( int w, int h )
{
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 50., w / (double)h, 1., 10. );
glMatrixMode( GL_MODELVIEW );
}
void render()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
// Rotate the teapot
glRotatef( xDegrees, 1, 0, 0 );
glRotatef( yDegrees, 0, 1, 0 );
glRotatef( zDegrees, 0, 0, 1 );
glColor3f( 1.0, 1.0, 1.0 );
glutWireTeapot( 1.5 );
glutSwapBuffers();
}
int main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "IVI - Sesion 2" );
glEnable( GL_DEPTH_TEST );
glutDisplayFunc( render );
glutReshapeFunc( resize );
glutKeyboardFunc( keyboard );
glutSpecialFunc( specialKeyboard );
glutMainLoop();
return 0;
}
Upvotes: 0
Views: 56
Reputation: 41017
The problem is because of those
case '0':
...
case '1':
...
axis 0
and axis '0'
(ASCII 48) are not the same, switch to
case 0:
...
case 1:
...
and it will work pressing x, y, z ...
Upvotes: 1