Advertisement
thecplusplusguy

OpenGL tutorial 11

Jun 23rd, 2012
1,668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //Thanks for the typed in code to Tapit85
  3. //other files are the same
  4. #include "functions.h"
  5.  
  6. float angle = 0.0;
  7.  
  8. int cube;
  9. void init()
  10. {
  11.     glClearColor(.5,.5,.5,1.);  //background color and alpha
  12.     glMatrixMode(GL_PROJECTION);
  13.     glLoadIdentity();
  14.     gluPerspective(45,640.0/480.0,1.0,500.0);
  15.     glMatrixMode(GL_MODELVIEW);
  16.     glEnable(GL_DEPTH_TEST);
  17.     cube = loadObject("test.obj");
  18.     glEnable(GL_LIGHTING);
  19.     glEnable(GL_LIGHT0);
  20.     glEnable(GL_BLEND);
  21.     // (A*S)+(B*D)
  22.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  23.     float col[] = {1.,1.,1.,1.};
  24.     glLightfv(GL_LIGHT0, GL_DIFFUSE, col);
  25. }
  26.  
  27. void display()
  28. {
  29.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  30.     glLoadIdentity();
  31.     float pos[] = {-1.0, 1.0, -2.0, 1.0};
  32.     glLightfv(GL_LIGHT0, GL_POSITION, pos);
  33.     glDisable(GL_LIGHTING);
  34.     glBegin(GL_QUADS);
  35.     // draw transparent objects from far to near!
  36.     // first draw transparent objects then nontransparent objects
  37.         glColor4f(0.,0.,1.,1.); // also possible with materials (for lighting)
  38.         glVertex3f(-2.,2.,-10.);
  39.         glVertex3f(-2.,-2.,-10.);
  40.         glVertex3f(2.,-2.,-10.);
  41.         glVertex3f(2.,2.,-10.);
  42.  
  43.         glColor4f(1.,0.,0.,.5);
  44.         glVertex3f(-1.,3.,-9.);
  45.         glVertex3f(-1.,-3.,-9.);
  46.         glVertex3f(3.,-3.,-9.);
  47.         glVertex3f(3.,3.,-9.);
  48.     glEnd();
  49. }
  50.  
  51. int main(int argc, char** argv)
  52. {
  53.     SDL_Init(SDL_INIT_EVERYTHING);
  54.     SDL_Surface *screen;
  55. //  screen = SDL_SetVideoMode(1024, 768, 32, SDL_SWSURFACE|SDL_OPENGL|SDL_FULLSCREEN);
  56.     screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_OPENGL);
  57.     bool running = true;
  58.     const int FPS = 30;
  59.     Uint32 start;
  60.     SDL_Event event;
  61.     init();
  62.     while(running) {
  63.         start = SDL_GetTicks();
  64.         while(SDL_PollEvent(&event)) {
  65.             switch(event.type) {
  66.                 case SDL_QUIT:
  67.                     running = false;
  68.                     break;
  69.                 case SDL_KEYDOWN:
  70.                     switch(event.key.keysym.sym)
  71.                     {
  72.                         case SDLK_ESCAPE:
  73.                             running = false;
  74.                             break;
  75.                     }
  76.                     break;
  77.             }
  78.         }
  79.  
  80.         display();
  81.         SDL_GL_SwapBuffers();
  82.         angle += 0.5;
  83.         if(angle > 360)
  84.             angle -= 360;
  85.         if(1000/FPS > SDL_GetTicks()-start)
  86.             SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
  87.     }
  88.     SDL_Quit();
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement