Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This example program is created by thecplusplusuy for demonstration purposes. It's a simple 3D FPS camera:
- //http://www.youtube.com/user/thecplusplusguy
- //Free source, modify if you want, LGPL licence (I guess), I would be happy, if you would not delete the link
- //so other people can see the tutorial
- //this file is functions.h, it's contain the actual implementation of the 3D camera
- #include "functions.h"
- float camX=0.0,camY=0.0,camZ=5.0; //current position of the camera
- float camYaw=0.0; //camera rotation in Y axis
- float camPitch=0.0; //camera rotation in X axis
- #ifndef M_PI //if the pi is not defined in the cmath header file
- #define M_PI 3.1415926535 //define it
- #endif
- void lockCamera()
- {
- //set campitch between -90 and 90 and set camyaw between 0 and 360 degrees
- if(camPitch>90)
- camPitch=90;
- if(camPitch<-90)
- camPitch=-90;
- if(camYaw<0.0)
- camYaw+=360.0;
- if(camYaw>360.0)
- camYaw-=360;
- }
- void moveCamera(float dist,float dir)
- {
- float rad=(camYaw+dir)*M_PI/180.0; //convert the degrees into radians
- camX-=sin(rad)*dist; //calculate the new coorinate, if you don't understand, draw a right triangle with the datas, you have
- camZ-=cos(rad)*dist; //and try to calculate the new coorinate with trigonometric functions, that should help
- }
- void moveCameraUp(float dist,float dir)
- {
- //the the same, only this time we calculate the y coorinate
- float rad=(camPitch+dir)*M_PI/180.0;
- camY+=sin(rad)*dist;
- }
- void Control(float movevel,float mousevel,bool mi) //move and mouse sensitivity, and is the mouse inside the window?
- {
- if(mi) //if the mouse is in the screen
- {
- int MidX=320; //middle of the screen
- int MidY=240;
- SDL_ShowCursor(SDL_DISABLE); //we don't show the cursor
- int tmpx,tmpy;
- SDL_GetMouseState(&tmpx,&tmpy); //get the current position of the cursor
- camYaw+=mousevel*(MidX-tmpx); //get the rotation, for example, if the mouse current position is 315, than 5*0.2, this is for Y
- camPitch+=mousevel*(MidY-tmpy); //this is for X
- lockCamera();
- SDL_WarpMouse(MidX,MidY); //move back the cursor to the center of the screen
- Uint8* state=SDL_GetKeyState(NULL); //which key is pressed?
- if(state[SDLK_w])
- {
- if(camPitch!=90 && camPitch!=-90) //if we are facing directly up or down, we don't go forward, it will be commented out, when there will be gravity
- moveCamera(movevel,0.0); //move forward
- moveCameraUp(movevel,0.0); //move up/down
- }else if(state[SDLK_s])
- {
- //same, just we use 180 degrees, so we move at the different direction (move back)
- if(camPitch!=90 && camPitch!=-90)
- moveCamera(movevel,180.0);
- moveCameraUp(movevel,180.0);
- }
- if(state[SDLK_a]) //move left
- moveCamera(movevel,90.0);
- else if(state[SDLK_d]) //move right
- moveCamera(movevel,270);
- }
- glRotatef(-camPitch,1.0,0.0,0.0); //rotate the camera (more precisly move everything in the opposit direction)
- glRotatef(-camYaw,0.0,1.0,0.0);
- }
- void UpdateCamera()
- {
- glTranslatef(-camX,-camY,-camZ); //move the camera
- }
- //draw a cube, not related to the 3D camera
- void drawCube(float size)
- {
- float difamb[]={1.0,0.5,0.3,1.0};
- glBegin(GL_QUADS);
- //front face
- glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,difamb);
- glNormal3f(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);
- //left face
- glNormal3f(-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);
- //back face
- glNormal3f(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
- glNormal3f(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);
- //top face
- glNormal3f(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);
- //bottom face
- glNormal3f(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);
- glEnd();
- }
Advertisement
Add Comment
Please, Sign In to add comment