//FILE: rt.cpp //T.S. Garrett //April 1, 2003 //Crude Ray Tracing program. //Recognize spheres that are defined by // - center // - radius // - color #include #include #include //provides tan class GLdoublePoint { public: GLdouble x,y,z; }; class GLdoubleVector { public: GLdouble x,y,z; }; class Color3 { public: GLfloat red, green, blue; }; class Sphere { public: GLdoublePoint center; GLdouble radius; GLdouble hit_time; Color3 color; }; void display(); void init(); void findWallHitTimes(); void rayTracer(); void defineSpheres(); void findHitTimeSpheres(); void pickWinner(); void plotPoint(GLint x, GLint y); void keyboard(unsigned char key, int x, int y); //GLOBAL VARIABLES: GLdoublePoint s; //position of the ray; GLdoubleVector dir; //direction of the ray; double t_hit; //hit time GLdoublePoint P; //hit point Color3 color; //color of pixel GLint numCols; //number of columns GLint numRows; //number of rows GLint numSpheres; //number of spheres GLint WinW = 640, WinH = 480; //window width and height GLdouble H, W, N, theta, aspect; //scene dimensions Sphere sphere[5]; //maximum of 5 spheres in the scene int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (WinW, WinH); glutInitWindowPosition (10, 10); glutCreateWindow ("Ray Traced Scene"); glutDisplayFunc(display); glutKeyboardFunc(keyboard); init(); glutMainLoop(); return 0; } //////////////////////////////////////////////////////////////////////////////////// void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glOrtho(0, WinW-1, 0, WinH-1, -1.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //Set up the camera: theta = 50; theta = theta*3.14159/180.0; aspect = (double)WinW/(double)WinH; N = 5.0; H = N * tan(theta/2.0); W = H * aspect; s.x = 0.0; s.y = 0.0; s.z = N; //Eye Position: numCols = WinW; numRows = WinH; numSpheres = 1; } /////////////////////////////////////////////////////////////////////////////////////////// void display() { glClear(GL_COLOR_BUFFER_BIT); defineSpheres(); rayTracer(); glFlush(); } /////////////////////////////////////////////////////////////////////////////////////////// void shadePixel(GLint x, GLint y) { glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); glFlush(); } /////////////////////////////////////////////////////////////////////////////////////////// void defineSpheres() { sphere[0].center.x = 0.0; sphere[0].center.y = 0.0; sphere[0].center.z = 0.0; sphere[0].radius = 1.0; sphere[0].color.red = 1.0; sphere[0].color.green = 0.0; sphere[0].color.blue = 0.0; } /////////////////////////////////////////////////////////////////////////////////////////// void findHitTimeSpheres() { int i; double scale, A, B, C, disc, time1, time2, length; GLdoubleVector local_dir; GLdoublePoint local_s; for(i=0; i0) && (time2>0)) //both positive hits { if (time10) && (time1<0)) sphere[i].hit_time = time2; else if ((time1>0) && (time2<0)) sphere[i].hit_time = time1; else //both negative; sphere[i].hit_time = -2.0; } } } } /////////////////////////////////////////////////////////////////////////////////////////// void pickWinner() { t_hit = 10000; color.red = 0.0; color.green = 0.2; color.blue = 0.0; int i; for (i=0; i0.0) && (sphere[i].hit_time