I'm trying to get the basics of 3d with opengl 2.0(I know, I know, it's deprecated etc, etc. It's just for the sake of prototyping). I'm trying to get a cube on the screen which you can move around. I have rendered the cube, and I can navigate the "world"(black background with the colored cube). However, when I try to rotate the camera, and then move, the camera moves independently of the camera's direction. For example, I rotate 90 degrees to the left, then I press "w" and it looks like I'm strafing to the right. I use sdl2 to get keyboard input, and when the user presses a key, I increase deltaX, deltaY, deltaZ, rotationX and rotationY accordingly(that's right, no rotation around the z axis).
And the rendering occurs as follows:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Apply transformations
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glTranslatef(dx, dy, dz);
// Draw each face with a different color
glColor3f(1.0, 1.0, 0.0);
glDrawArrays(GL_QUADS, 0, 4);
glColor3f(1.0, 0.3, 0.4);
glDrawArrays(GL_QUADS, 4, 4);
glColor3f(0.0, 1.0, 0.5);
glDrawArrays(GL_QUADS, 8, 4);
glColor3f(1.0, 1.0, 1.0);
glDrawArrays(GL_QUADS, 12, 4);
glColor3f(0.3, 0.8, 0.0);
glDrawArrays(GL_QUADS, 16, 4);
glColor3f(0.0, 0.0, 1.0);
glDrawArrays(GL_QUADS, 20, 4);
How can I do this?
EDIT: forgot to add, I would like to do this without glu. I don't like linking against a lot of libraries, especially when it's only to get a few functions.
EDIT2: I got forward movement to work using trigonometry like this:
case SDLK_w:
posZ += (float)cos(rotY * M_PI / 180); // I use rotY * PI/180 to convert rotY to radians
posX -= (float)sin(rotY * M_PI / 180);
break;
case SDLK_s:
posZ -= (float)cos(rotY * M_PI / 180);
posX += (float)sin(rotY * M_PI / 180);
And now the only thing to add is strafing, which I thought would be something like this
case SDLK_a:
posX += (float)sin(rotY * M_PI / 180) + (float)cos(rotY * M_PI / 180);
posZ += (float)sin(rotY * M_PI / 180);
break;
case SDLK_d:
posX -= (float)sin(rotY * M_PI / 180) + (float)cos(rotY * M_PI / 180);
posZ -= (float)sin(rotY * M_PI /180);
break;
But it doesn't work, if you strafe when looking down the x axis, you move backwards in the z axis(wtf?)
Thanks!
EDIT3: Never mind, solved it by doing
case SDLK_a:
posX += (float)cos(rotY * M_PI / 180);
posZ += (float)sin(rotY * M_PI / 180);
break;
case SDLK_d:
posX -= (float)cos(rotY * M_PI / 180);
posZ -= (float)sin(rotY * M_PI /180);
break;
I don't know if this is the best way to do it, but it works. Thanks everyone for helping me!
gluLookAt()function works. \$\endgroup\$gluLookAt()function in it. I was suggesting you can look at the implementation to get an idea of how to do what you're trying to do, if it would help. \$\endgroup\$