Reputation: 123
I have the following snippet:
float z, yrot = 0, xrot = 0;
float x = 0.0;
float y = 0.0;
void display() /* 2. */
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* 3. */
glMatrixMode(GL_MODELVIEW_MATRIX);
glLoadIdentity();
glMatrixMode (GL_MODELVIEW);
glTranslatef(x, y, -0.5);
glRotatef(rotation, 0, 0, -1);
glColor3f(1.0,1.0,1.0);
glutWireTeapot(0.2);
glutSwapBuffers(); /* 4. */
}
void keyPressed(unsigned char key, int x, int y)
{
float yrotrad = (yrot / (180 * 3.141592654f));
float xrotrad = (xrot / (180 * 3.141592654f));
switch (key)
{
case 'w':
x += (float)(sin(yrotrad));
z -= (float)(cos(yrotrad)) ;
y -= (float)(sin(xrotrad));
glutPostRedisplay();
break;
}
}
int main(int argc, char **argv) /* 5. */
{
glutDisplayFunc(display);
glutKeyboardFunc(keyPressed);
glutMainLoop();
}
What is supposed to happen when I press the w key is that it's supposed to tilt the camera. But when I press w, nothing happens. What am I doing wrong?
Upvotes: 0
Views: 1168
Reputation: 182093
Your x
and y
parameters are shadowing the globals:
void keyPressed(unsigned char key, int x, int y)
So the increment of those is not doing what you think it does. Turn up your compiler warnings -- you'll probably see some about losing precision.
Ironically, it works correctly for z
but you don't seem to be using it anywhere in your rendering code.
(Aside: You may also want to look into GLFW. It's quite similar to GLUT and as easy to get started with, but better in every respect.)
Upvotes: 2