Advertisement
thecplusplusguy

Simple obj loader 2 (OpenGL,SDL,C++) - main.cpp

Aug 26th, 2011
10,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. //This example program is created by thecplusplusuy for demonstration purposes. It's a simple 3D model loader (wavefront (.obj)), which is capable to load materials and UV textures:
  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 third.cpp an example, how to use the class
  6. #include "objloader.h"
  7.  
  8. float angle=0.0;
  9.  
  10. int cube;
  11. objloader obj;  //create an instance of the objloader
  12. void init()
  13. {
  14.     glClearColor(0.5,0.5,0.5,1.0);
  15.     glMatrixMode(GL_PROJECTION);
  16.     glLoadIdentity();
  17.     gluPerspective(45,640.0/480.0,1.0,500.0);
  18.     glMatrixMode(GL_MODELVIEW);
  19.     glEnable(GL_DEPTH_TEST);
  20.     cube=obj.load("test.obj");  //load it
  21.     glEnable(GL_LIGHTING);
  22.     glEnable(GL_LIGHT0);
  23.     float col[]={1.0,1.0,1.0,1.0};
  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.     glTranslatef(0.0,-30.0,-100.0);
  34.     glCallList(cube);   //and display it
  35. }
  36.  
  37.  
  38. int main(int argc,char** argv)
  39. {
  40.     SDL_Init(SDL_INIT_EVERYTHING);
  41.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_OPENGL);
  42.     bool running=true;
  43.     Uint32 start;
  44.     SDL_Event event;
  45.     init();
  46.     while(running)
  47.     {
  48.         start=SDL_GetTicks();
  49.         while(SDL_PollEvent(&event))
  50.         {
  51.             switch(event.type)
  52.             {
  53.                 case SDL_QUIT:
  54.                     running=false;
  55.                     break;
  56.             }
  57.         }
  58.         display();
  59.         SDL_GL_SwapBuffers();
  60.         angle+=0.5;
  61.         if(angle>360)
  62.             angle-=360;
  63.         if(1000/30>(SDL_GetTicks()-start))
  64.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  65.     }
  66.     SDL_Quit();
  67.     return 0;  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement