thecplusplusguy

Simple OpenGL (SDL) 3D FPS camera - main.cpp

Aug 31st, 2011
5,492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. //This example program is created by thecplusplusuy for demonstration purposes. It's a simple 3D FPS camera:
  2. //http://www.youtube.com/user/thecplusplusguy
  3. //Free source, modify if you want, LGPL licence (I guess), I would be happy, if you would not delete the link
  4. //so other people can see the tutorial
  5. //this file is main.cpp, the entry point of the program, and we load the model here, and use the 3D camera in here.
  6. //You need the objloader.h and objloader.cpp file for that (earlier I uploaded the source, so I don't again), and you need an object (test10.obj) which you load (obviously you could use just simple primitives (triangle, quad) for that too, but object are looks better)
  7. #include "functions.h"
  8. #include "objloader.h"
  9.  
  10. float angle=0.0;
  11.  
  12. int cube;
  13. objloader obj;
  14. bool mousein=false;
  15. void init()
  16. {
  17.     glClearColor(0.5,0.5,0.5,1.0);
  18.     glMatrixMode(GL_PROJECTION);
  19.     glLoadIdentity();
  20.     gluPerspective(45,640.0/480.0,1.0,500.0);
  21.     glMatrixMode(GL_MODELVIEW);
  22.     glEnable(GL_DEPTH_TEST);
  23.     cube=obj.load("test10.obj");
  24.     glEnable(GL_LIGHTING);
  25.     glEnable(GL_LIGHT0);
  26.     float col[]={1.0,1.0,1.0,1.0};
  27.     glLightfv(GL_LIGHT0,GL_DIFFUSE,col);
  28.     initskybox();
  29. }
  30.  
  31. void display()
  32. {
  33.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  34.     glLoadIdentity();
  35.     Control(0.2,0.2,mousein);   //calculate the position, and rotate the camera
  36.     UpdateCamera(); //move the camera to the new location
  37.     float pos[]={-1.0,1.0,-2.0,1.0};
  38.     glLightfv(GL_LIGHT0,GL_POSITION,pos);
  39.     glCallList(cube);
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45.     SDL_Init(SDL_INIT_EVERYTHING);
  46.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_OPENGL);
  47.     bool running=true;
  48.     Uint32 start;
  49.     SDL_Event event;
  50.     init();
  51.     while(running)
  52.     {
  53.         start=SDL_GetTicks();
  54.         while(SDL_PollEvent(&event))
  55.         {
  56.             switch(event.type)
  57.             {
  58.                 case SDL_QUIT:
  59.                     running=false;
  60.                     break;
  61.                 case SDL_MOUSEBUTTONDOWN:
  62.                     mousein=true;
  63.                     SDL_ShowCursor(SDL_DISABLE);
  64.                     break;
  65.                 case SDL_KEYDOWN:
  66.                     if(event.key.keysym.sym==SDLK_p)
  67.                     {
  68.                         mousein=false;
  69.                         SDL_ShowCursor(SDL_ENABLE);
  70.                         break;
  71.                     }
  72.                     if(event.key.keysym.sym==SDLK_ESCAPE)
  73.                     {
  74.                         running=false;
  75.                         break;
  76.                     }
  77.             }
  78.         }
  79.         display();
  80.         SDL_GL_SwapBuffers();
  81.         angle+=0.5;
  82.         if(angle>360)
  83.             angle-=360;
  84.         if(1000/30>(SDL_GetTicks()-start))
  85.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  86.     }
  87.     SDL_Quit();
  88.     killskybox();
  89.     return 0;  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment