Advertisement
thecplusplusguy

OpenGL (SDL,C++) - ray tracing - third.cpp

Sep 29th, 2011
4,922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //watch the video for explonation
  3. //ray-sphere and ray-plane collision detection.
  4. //if you find a bug, let me know
  5. #include "functions.h"
  6. #include "objloader.h"
  7.  
  8. float angle=0.0;
  9.  
  10. int cube;
  11. objloader obj;
  12. bool mousein=false;
  13. coordinate spherecenter(0.0,0.0,0.0);
  14. coordinate raystart(0.0,0.0,0.0);
  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. //  cube=loadObject("test.obj");
  25.     glEnable(GL_LIGHTING);
  26.     glEnable(GL_LIGHT0);
  27.     float col[]={1.0,1.0,1.0,1.0};
  28.     glLightfv(GL_LIGHT0,GL_DIFFUSE,col);
  29.     initskybox();
  30. }
  31.  
  32. coordinate p1(-5.0,5.0,-5.0);
  33. coordinate p2(5.0,5.0,-5.0);
  34. coordinate p3(5.0,-5.0,-5.0);
  35. coordinate p4(-5.0,-5.0,-5.0);
  36. /*
  37.  
  38. */
  39.  
  40. void display()
  41. {
  42.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  43.     glLoadIdentity();
  44.     Control(0.2,0.2,mousein);
  45.     drawSkybox(50.0);
  46.     UpdateCamera();
  47.     float pos[]={-1.0,1.0,-2.0,1.0};
  48.     glLightfv(GL_LIGHT0,GL_POSITION,pos);
  49. //  glTranslatef(0.0,0.0,-4.0);
  50. //  glRotatef(angle,1,1,1);
  51.     glDisable(GL_LIGHTING);
  52. //  if(raysphere(spherecenter.x,spherecenter.y,spherecenter.z,0.0,0.0,1.0,raystart.x, raystart.y,raystart.z,1.0))
  53.     if(rayplane(0.0,0.0,1.0,raystart.x,raystart.y,raystart.z,0.0,0.0,-1.0,p1,p2,p3,p4))
  54.         glColor3f(1.0,0.0,0.0);
  55.     else
  56.         glColor3f(1.0,1.0,1.0);
  57.     //xs+t*xd
  58.     glBegin(GL_LINES);
  59.         glVertex3f(raystart.x,raystart.y,raystart.z);
  60.         glVertex3f(raystart.x+100*0,raystart.y+100*0,raystart.z+100*-1);
  61.     glEnd();
  62.     glBegin(GL_QUADS);
  63.         glVertex3f(-5.0,5.0,-5.0);
  64.         glVertex3f(5.0,5.0,-5.0);
  65.         glVertex3f(5.0,-5.0,-5.0);
  66.         glVertex3f(-5.0,-5.0,-5.0);
  67.     glEnd();
  68.     glEnable(GL_LIGHTING);
  69.     glCallList(cube);
  70.     glColor3f(1.0,1.0,1.0);
  71. }
  72.  
  73.  
  74. int main()
  75. {
  76.     SDL_Init(SDL_INIT_EVERYTHING);
  77.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_OPENGL);
  78.     bool running=true;
  79.     Uint32 start;
  80.     SDL_Event event;
  81.     init();
  82.     bool b[4]={0,0,0,0};
  83.     while(running)
  84.     {
  85.         start=SDL_GetTicks();
  86.         while(SDL_PollEvent(&event))
  87.         {
  88.             switch(event.type)
  89.             {
  90.                 case SDL_QUIT:
  91.                     running=false;
  92.                     break;
  93.                 case SDL_MOUSEBUTTONDOWN:
  94.                     mousein=true;
  95.                     SDL_ShowCursor(SDL_DISABLE);
  96.                     break;
  97.                 case SDL_KEYDOWN:
  98.                     if(event.key.keysym.sym==SDLK_p)
  99.                     {
  100.                         mousein=false;
  101.                         SDL_ShowCursor(SDL_ENABLE);
  102.                         break;
  103.                     }
  104.                     if(event.key.keysym.sym==SDLK_ESCAPE)
  105.                     {
  106.                         running=false;
  107.                         break;
  108.                     }  
  109.                     switch(event.key.keysym.sym)
  110.                     {
  111.                         case SDLK_UP:
  112.                             b[0]=1;
  113.                             break;
  114.  
  115.                         case SDLK_LEFT:
  116.                             b[1]=1;
  117.                             break;
  118.  
  119.                         case SDLK_DOWN:
  120.                             b[2]=1;
  121.                             break;
  122.  
  123.                         case SDLK_RIGHT:
  124.                             b[3]=1;
  125.                             break;
  126.                     }
  127.                     break;             
  128.                 case SDL_KEYUP:
  129.                     switch(event.key.keysym.sym)
  130.                     {
  131.                         case SDLK_UP:
  132.                             b[0]=0;
  133.                             break;
  134.  
  135.                         case SDLK_LEFT:
  136.                             b[1]=0;
  137.                             break;
  138.  
  139.                         case SDLK_DOWN:
  140.                             b[2]=0;
  141.                             break;
  142.  
  143.                         case SDLK_RIGHT:
  144.                             b[3]=0;
  145.                             break;
  146.                     }
  147.                     break;                     
  148.             }
  149.         }
  150.         if(b[0])
  151.             raystart.y+=0.3;
  152.         if(b[1])
  153.             raystart.x-=0.3;
  154.         if(b[2])
  155.             raystart.y-=0.3;
  156.         if(b[3])
  157.             raystart.x+=0.3;
  158.         display();
  159.         SDL_GL_SwapBuffers();
  160.         angle+=0.5;
  161.         if(angle>360)
  162.             angle-=360;
  163.         if(1000/30>(SDL_GetTicks()-start))
  164.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  165.     }
  166.     SDL_Quit();
  167.     killskybox();
  168.     return 0;  
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement