The Industry's Foundation for High Performance Graphics

1. Rotation ?

What I'm trying to do simply is if x is pressed the rotation should be around x-axis and so on with y and z. This is the code
Code :
```

#  include <GL/glut.h>

using namespace std;

static int angle(0);
static float x(0.0), y(0.0), z(0.0);

// Drawing routine.
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);

// Modeling transformations.
glTranslatef(0.0, 0.0, -15.0);
glScalef(1.0, 1.0, 1.0);
glRotatef(angle, x, y, z);

glutWireTeapot(5.0);

glFlush();
}

// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}

// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);

glMatrixMode(GL_MODELVIEW);
}

// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'x': case 'X':
{
x = 1.0; y = 0.0; z = 0.0;
angle += 25;
if ( angle > 360 )
angle = 0;
}
case 'y': case 'Y':
{
x = 0.0; y = 1.0; z = 0.0;
angle += 25;
if ( angle > 360 )
angle = 0;
}
case 'z': case 'Z':
{
x = 0.0; y = 0.0; z = 1.0;
angle += 25;
if ( angle > 360 )
angle = 0;
}
default:
break;
}
glutPostRedisplay();
}

// Main routine.
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("box.cpp");
setup();
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
glutMainLoop();

return 0;
}```

Why it doesn't work as I expected?

2. break it !

hi,

thats simple...
you are missing the breaks in the switch.

Code :
```      case 'x': case 'X':
{
x = 1.0; y = 0.0; z = 0.0;
angle += 25;
if ( angle > 360 )
angle = 0;

break; // <<<<< MISSING !!!
}```

so always the last ( z ) is happening.
But when you change it the behavior for x and y won´t be what you expect ..
why ? because of the order of translate rotate ...

just play arround with the order and values of these lines to get a better feeling for it:
Code :
```   glTranslatef(0.0, 0.0, -15.0);
glScalef(1.0, 1.0, 1.0);
glRotatef(angle, x, y, z);```

cu
uwi

3. I might also suggest the small changes to your code shown below.
You'll need a static angle variable for each axis (xangle, yangle, zangle).
I'm guessing that even after you make these corrections the rotations still
won't work as you hoped. Let us know.

Code :
```case 'x':
case 'X':
{
x = 1.0; y = 0.0; z = 0.0;
xangle = (xangle + 25) % 360;

break;
}```

4. @uwi2k2 & Carmine,
thank you guys.

@Carmine,
What do you mean that I need angle variable for each axis. The code works only if I set for x glRotatef(angle, 1.0, 0.0, 0.0) and y glRotatef(angle, 0.0, 1.0, 0.0) and so is z. I mean without using variables inside glRotatef().
Does this affect the function?

5. Up up up up up

6. I've solved the problem and this is the final code

Code :
```#include <iostream>
#include <GL/glut.h>

static int xangle(0), yangle(0), zangle(0);

// Drawing routine.
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);

// Modeling transformations.
glTranslatef(0.0, 0.0, -15.0);
glScalef(1.0, 1.0, 1.0);
glRotatef(xangle, 1.0f, 0.0f, 0.0f);  // rotate about x
glRotatef(yangle, 0.0f, 1.0f, 0.0f);  // rotate about y
glRotatef(zangle, 0.0f, 0.0f, 1.0f);  // rotate about z
glutWireTeapot(5.0);

glFlush();
}

// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}

// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}

// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
key = toupper(key);
switch(key)
{
case 27:
exit(0);
break;
case 'X':
{
yangle = 0.0; zangle = 0.0;
xangle += 25;
if ( xangle > 360 )
xangle = 0;
break;
}
case 'Y':
{
xangle = 0.0; zangle = 0.0;
yangle += 25;
if ( yangle > 360 )
yangle = 0;
break;
}
case 'Z':
{
xangle = 0.0; yangle = 0.0;
zangle += 25;
if ( zangle > 360 )
zangle = 0;
break;
}
default:
break;
}
glutPostRedisplay();
}

// Main routine.
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("box.cpp");
setup();
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
glutMainLoop();

return 0;
}```

Posting Permissions

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