thecplusplusguy

loadAnimation - OpenGL, SDL, C++ - main.cpp

Oct 23rd, 2011
1,982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //load animation main.cpp
  3. #include "functions.h"
  4. #include "objloader.h"
  5. #include "text.h"
  6. float angle=0.0;
  7.  
  8. int cube;
  9. objloader obj;
  10. bool mousein=false;
  11. coordinate spherecenter(0.0,0.0,0.0);
  12. coordinate raystart(0.0,0.0,0.0);
  13. text* tex;
  14. std::vector<unsigned int> frames;
  15. int curframe=0;
  16. void init()
  17. {
  18.     glClearColor(0.5,0.5,0.5,1.0);
  19.     glMatrixMode(GL_PROJECTION);
  20.     glLoadIdentity();
  21.     gluPerspective(45,640.0/480.0,1.0,500.0);
  22.     glMatrixMode(GL_MODELVIEW);
  23.     glEnable(GL_DEPTH_TEST);
  24.     cube=obj.load("test10.obj");
  25.     std::vector<unsigned int> chars;
  26.     char tmp[40];
  27.     for(int i=0;i<26;i++)
  28.     {
  29.         sprintf(tmp,"font/%d.obj",i);
  30.         unsigned int tmp2=obj.load(tmp);
  31.         chars.push_back(tmp2);
  32.     }
  33.     tex=new text(0.9,0.8,chars);
  34. //  cube=loadObject("test.obj");
  35.     glEnable(GL_LIGHTING);
  36.     glEnable(GL_LIGHT0);
  37.     float col[]={1.0,1.0,1.0,1.0};
  38.     glLightfv(GL_LIGHT0,GL_DIFFUSE,col);
  39.     initskybox();
  40.     loadAnimation(frames,"anim/anim",250,obj);
  41. }
  42.  
  43. coordinate p1(-5.0,5.0,-5.0);
  44. coordinate p2(5.0,5.0,-5.0);
  45. coordinate p3(5.0,-5.0,-5.0);
  46. coordinate p4(-5.0,-5.0,-5.0);
  47. /*
  48.  
  49. */
  50.  
  51. void display()
  52. {
  53.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  54.     glLoadIdentity();
  55.     Control(0.2,0.2,mousein);
  56.     drawSkybox(50.0);
  57.     UpdateCamera();
  58.     float pos[]={-1.0,1.0,-2.0,1.0};
  59.     glLightfv(GL_LIGHT0,GL_POSITION,pos);
  60. //  glTranslatef(0.0,0.0,-4.0);
  61. //  glRotatef(angle,1,1,1);
  62.     glDisable(GL_LIGHTING);
  63. //  if(raysphere(spherecenter.x,spherecenter.y,spherecenter.z,0.0,0.0,1.0,raystart.x, raystart.y,raystart.z,1.0))
  64.     if(rayplane(0.0,0.0,1.0,raystart.x,raystart.y,raystart.z,0.0,0.0,-1.0,p1,p2,p3,p4))
  65.         glColor3f(1.0,0.0,0.0);
  66.     else
  67.         glColor3f(1.0,1.0,1.0);
  68.     //xs+t*xd
  69.     glBegin(GL_LINES);
  70.         glVertex3f(raystart.x,raystart.y,raystart.z);
  71.         glVertex3f(raystart.x+100*0,raystart.y+100*0,raystart.z+100*-1);
  72.     glEnd();
  73.     glBegin(GL_QUADS);
  74.         glVertex3f(-5.0,5.0,-5.0);
  75.         glVertex3f(5.0,5.0,-5.0);
  76.         glVertex3f(5.0,-5.0,-5.0);
  77.         glVertex3f(-5.0,-5.0,-5.0);
  78.         glColor3f(0,0,0);
  79.         glVertex3f(-10.0,0.0,-10.0);
  80.         glVertex3f(-10.0,-5.0,10.0);
  81.         glVertex3f(10.0,-5.0,10.0);
  82.         glVertex3f(10.0,0.0,-10.0);
  83.     glEnd();
  84.     glEnable(GL_LIGHTING);
  85.     glCallList(cube);
  86.     tex->drawText(coordinate(10,0,0),coordinate(angle,0,0),"HELLO\nWORLD\nFROM\nOPENGL");
  87.     glTranslatef(-20,0,0);
  88.     glCallList(frames[curframe]);
  89.     curframe++;
  90.     if(curframe>249)
  91.         curframe=0;
  92.     std::cout << curframe << std::endl;
  93.     glColor3f(1.0,1.0,1.0);
  94. }
  95.  
  96.                 coordinate p5(-10.0,0,-10.0);
  97.                 coordinate p6(-10.0,-5.0,10.0);
  98.                 coordinate p7(10.0,-5.0,10.0);
  99.                 coordinate p8(10.0,0,-10.0);
  100.  
  101. int main()
  102. {
  103.     SDL_Init(SDL_INIT_EVERYTHING);
  104.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_OPENGL);
  105.     bool running=true;
  106.     Uint32 start;
  107.     SDL_Event event;
  108.     init();
  109.     bool b[4]={0,0,0,0};
  110.     while(running)
  111.     {
  112.         start=SDL_GetTicks();
  113.         while(SDL_PollEvent(&event))
  114.         {
  115.             switch(event.type)
  116.             {
  117.                 case SDL_QUIT:
  118.                     running=false;
  119.                     break;
  120.                 case SDL_MOUSEBUTTONDOWN:
  121.                     mousein=true;
  122.                     SDL_ShowCursor(SDL_DISABLE);
  123.                     break;
  124.                 case SDL_KEYDOWN:
  125.                     if(event.key.keysym.sym==SDLK_p)
  126.                     {
  127.                         mousein=false;
  128.                         SDL_ShowCursor(SDL_ENABLE);
  129.                         break;
  130.                     }
  131.                     if(event.key.keysym.sym==SDLK_ESCAPE)
  132.                     {
  133.                         running=false;
  134.                         break;
  135.                     }  
  136.                     switch(event.key.keysym.sym)
  137.                     {
  138.                         case SDLK_UP:
  139.                             b[0]=1;
  140.                             break;
  141.  
  142.                         case SDLK_LEFT:
  143.                             b[1]=1;
  144.                             break;
  145.  
  146.                         case SDLK_DOWN:
  147.                             b[2]=1;
  148.                             break;
  149.  
  150.                         case SDLK_RIGHT:
  151.                             b[3]=1;
  152.                             break;
  153.                     }
  154.                     break;             
  155.                 case SDL_KEYUP:
  156.                     switch(event.key.keysym.sym)
  157.                     {
  158.                         case SDLK_UP:
  159.                             b[0]=0;
  160.                             break;
  161.  
  162.                         case SDLK_LEFT:
  163.                             b[1]=0;
  164.                             break;
  165.  
  166.                         case SDLK_DOWN:
  167.                             b[2]=0;
  168.                             break;
  169.  
  170.                         case SDLK_RIGHT:
  171.                             b[3]=0;
  172.                             break;
  173.                     }
  174.                     break;                     
  175.             }
  176.         }
  177.         if(b[0])
  178.             raystart.y+=0.3;
  179.         if(b[1])
  180.             raystart.x-=0.3;
  181.         if(b[2])
  182.             raystart.y-=0.3;
  183.         if(b[3])
  184.             raystart.x+=0.3;
  185.         display();
  186.         SDL_GL_SwapBuffers();
  187.         angle+=0.5;
  188.         if(angle>360)
  189.             angle-=360;
  190.         coordinate cameraPos=camPos();
  191.         if(spheresphere(cameraPos,2.0,coordinate(0,0,0),1.0))
  192.             std::cout << "collision\n";
  193.         sphereplane(cameraPos,coordinate(0,0,1),p1,p2,p3,p4,2.0);
  194.         sphereplane(cameraPos,coordinate(0,0.9701425,0.242535625),p5,p6,p7,p8,2.0);
  195.         moveTo(cameraPos);
  196.         if(1000/30>(SDL_GetTicks()-start))
  197.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  198.     }
  199.     SDL_Quit();
  200.     killskybox();
  201.     delete tex;
  202.     return 0;  
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment