Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //Thanks for the typed in code to Tapit85
- #include "functions.h"
- float camX = 0., camY = 0., camZ = 5.;
- float camYaw = 0.;
- float camPitch = 0.;
- #ifndef M_PI
- #define M_PI 3.1415926535
- #endif
- void lockCamera()
- {
- if(camPitch > 90)
- camPitch = 90;
- if(camPitch < -90)
- camPitch = -90;
- if(camYaw < 0.)
- camYaw += 360.;
- if(camYaw > 360.)
- camYaw -= 360;
- }
- void moveCamera(float dist, float dir)
- {
- float rad = (camYaw+dir)*M_PI/180.;
- camX -= sin(rad)*dist;
- camZ -= cos(rad)*dist;
- }
- void moveCameraUp(float dist, float dir)
- {
- float rad = (camPitch+dir)*M_PI/180.;
- camY += sin(rad)*dist;
- }
- void Control(float movevel, float mousevel, bool mi)
- {
- if(mi)
- {
- int MidX = 320;
- int MidY = 240;
- SDL_ShowCursor(SDL_DISABLE);
- int tmpx, tmpy;
- SDL_GetMouseState(&tmpx, &tmpy);
- camYaw += mousevel*(MidX-tmpx);
- camPitch += mousevel*(MidY-tmpy);
- lockCamera();
- SDL_WarpMouse(MidX, MidY);
- Uint8* state = SDL_GetKeyState(NULL);
- if(state[SDLK_w])
- {
- // if(camPitch!=90 && camPitch!=-90) // comment in if you want to be able to go up and down
- moveCamera(movevel,0.);
- // moveCameraUp(movevel,0.); // comment in if you want to be able to go up and down
- }
- else if(state[SDLK_s])
- {
- // if(camPitch!=90 && camPitch!=-90) // comment in if you want to be able to go up and down
- moveCamera(movevel,180.);
- // moveCameraUp(movevel,180.); // comment in if you want to be able to go up and down
- }
- if(state[SDLK_a])
- moveCamera(movevel,90.);
- else if(state[SDLK_d])
- moveCamera(movevel,270.);
- }
- glRotatef(-camPitch,1.,0.,0.);
- glRotatef(-camYaw,0.,1.,0.);
- }
- void UpdateCamera()
- {
- glTranslatef(-camX,-camY,-camZ);
- }
- void drawCube(float size)
- {
- glBegin(GL_QUADS);
- // front face
- glColor3f(1.0,0.0,0.0);
- glVertex3f(size/2,size/2,size/2);
- glVertex3f(-size/2,size/2,size/2);
- glVertex3f(-size/2,-size/2,size/2);
- glVertex3f(size/2,-size/2,size/2);
- // left face
- glColor3f(0.0,1.0,0.0);
- glVertex3f(-size/2,size/2,size/2);
- glVertex3f(-size/2,-size/2,size/2);
- glVertex3f(-size/2,-size/2,-size/2);
- glVertex3f(-size/2,size/2,-size/2);
- // back face
- glColor3f(0.0,0.0,1.0);
- glVertex3f(size/2,size/2,-size/2);
- glVertex3f(-size/2,size/2,-size/2);
- glVertex3f(-size/2,-size/2,-size/2);
- glVertex3f(size/2,-size/2,-size/2);
- // right face
- glColor3f(1.0,1.0,0.0);
- glVertex3f(size/2,size/2,size/2);
- glVertex3f(size/2,-size/2,size/2);
- glVertex3f(size/2,-size/2,-size/2);
- glVertex3f(size/2,size/2,-size/2);
- // top face
- glColor3f(1.0,0.0,1.0);
- glVertex3f(size/2,size/2,size/2);
- glVertex3f(-size/2,size/2,size/2);
- glVertex3f(-size/2,size/2,-size/2);
- glVertex3f(size/2,size/2,-size/2);
- // bottom face
- glColor3f(0.0,1.0,1.0);
- glVertex3f(size/2,-size/2,size/2);
- glVertex3f(-size/2,-size/2,size/2);
- glVertex3f(-size/2,-size/2,-size/2);
- glVertex3f(size/2,-size/2,-size/2);
- glEnd();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement