Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //watch the video for explonation
- //ray-sphere and ray-plane collision detection.
- //if you find a bug, let me know
- #include "functions.h"
- #include "objloader.h"
- float angle=0.0;
- int cube;
- objloader obj;
- bool mousein=false;
- coordinate spherecenter(0.0,0.0,0.0);
- coordinate raystart(0.0,0.0,0.0);
- void init()
- {
- glClearColor(0.5,0.5,0.5,1.0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45,640.0/480.0,1.0,500.0);
- glMatrixMode(GL_MODELVIEW);
- glEnable(GL_DEPTH_TEST);
- cube=obj.load("test10.obj");
- // cube=loadObject("test.obj");
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- float col[]={1.0,1.0,1.0,1.0};
- glLightfv(GL_LIGHT0,GL_DIFFUSE,col);
- initskybox();
- }
- coordinate p1(-5.0,5.0,-5.0);
- coordinate p2(5.0,5.0,-5.0);
- coordinate p3(5.0,-5.0,-5.0);
- coordinate p4(-5.0,-5.0,-5.0);
- /*
- */
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- Control(0.2,0.2,mousein);
- drawSkybox(50.0);
- UpdateCamera();
- float pos[]={-1.0,1.0,-2.0,1.0};
- glLightfv(GL_LIGHT0,GL_POSITION,pos);
- // glTranslatef(0.0,0.0,-4.0);
- // glRotatef(angle,1,1,1);
- glDisable(GL_LIGHTING);
- // if(raysphere(spherecenter.x,spherecenter.y,spherecenter.z,0.0,0.0,1.0,raystart.x, raystart.y,raystart.z,1.0))
- if(rayplane(0.0,0.0,1.0,raystart.x,raystart.y,raystart.z,0.0,0.0,-1.0,p1,p2,p3,p4))
- glColor3f(1.0,0.0,0.0);
- else
- glColor3f(1.0,1.0,1.0);
- //xs+t*xd
- glBegin(GL_LINES);
- glVertex3f(raystart.x,raystart.y,raystart.z);
- glVertex3f(raystart.x+100*0,raystart.y+100*0,raystart.z+100*-1);
- glEnd();
- glBegin(GL_QUADS);
- glVertex3f(-5.0,5.0,-5.0);
- glVertex3f(5.0,5.0,-5.0);
- glVertex3f(5.0,-5.0,-5.0);
- glVertex3f(-5.0,-5.0,-5.0);
- glEnd();
- glEnable(GL_LIGHTING);
- glCallList(cube);
- glColor3f(1.0,1.0,1.0);
- }
- int main()
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_OPENGL);
- bool running=true;
- Uint32 start;
- SDL_Event event;
- init();
- bool b[4]={0,0,0,0};
- while(running)
- {
- start=SDL_GetTicks();
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- case SDL_MOUSEBUTTONDOWN:
- mousein=true;
- SDL_ShowCursor(SDL_DISABLE);
- break;
- case SDL_KEYDOWN:
- if(event.key.keysym.sym==SDLK_p)
- {
- mousein=false;
- SDL_ShowCursor(SDL_ENABLE);
- break;
- }
- if(event.key.keysym.sym==SDLK_ESCAPE)
- {
- running=false;
- break;
- }
- switch(event.key.keysym.sym)
- {
- case SDLK_UP:
- b[0]=1;
- break;
- case SDLK_LEFT:
- b[1]=1;
- break;
- case SDLK_DOWN:
- b[2]=1;
- break;
- case SDLK_RIGHT:
- b[3]=1;
- break;
- }
- break;
- case SDL_KEYUP:
- switch(event.key.keysym.sym)
- {
- case SDLK_UP:
- b[0]=0;
- break;
- case SDLK_LEFT:
- b[1]=0;
- break;
- case SDLK_DOWN:
- b[2]=0;
- break;
- case SDLK_RIGHT:
- b[3]=0;
- break;
- }
- break;
- }
- }
- if(b[0])
- raystart.y+=0.3;
- if(b[1])
- raystart.x-=0.3;
- if(b[2])
- raystart.y-=0.3;
- if(b[3])
- raystart.x+=0.3;
- display();
- SDL_GL_SwapBuffers();
- angle+=0.5;
- if(angle>360)
- angle-=360;
- if(1000/30>(SDL_GetTicks()-start))
- SDL_Delay(1000/30-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- killskybox();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement