int main (int argc, char * argv[]){ backBuffer = new ColorFloat[640*480]; // allocate the ray direction lookup table Vector *directionTable = GenerateRayDirectionTable(); const int numPrimitives = 1; Primitive *primitives = new Primitive[numPrimitives]; const int numLightSources = 1; LightSource *lightSources = new LightSource[numLightSources]; const int numVertices = 4; Vector *vertices = new Vector[numVertices]; Scene(primitives, numPrimitives, vertices, numVertices, lightSources, numLightSources); SetupScene(primitives, numPrimitives, vertices, numVertices, lightSources, numLightSources); TraceScene(primitives, numPrimitives, lightSources, numLightSources, backBuffer, directionTable); /*alboroto para opengl*/ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); glutInitWindowSize(width, height); glutInitWindowPosition(initx, inity); window = glutCreateWindow("final"); glutDisplayFunc(&DrawGLScene); glutIdleFunc(&DrawGLScene); glutKeyboardFunc(&keyPressed); InitGL(width, height); /* float * a = new float[3]; glGetFloatv(GL_CURRENT_RASTER_POSITION,a); printf("x=%f y=%f z=%f\n",a[0],a[1],a[2]);*/ glutMainLoop(); /*fin alboroto de opengl*/ // clean up for(int i=0; i<numPrimitives; i++) ClearPrimitiveType(primitives[i]); delete[] lightSources; delete[] vertices; delete[] directionTable; delete[] backBuffer; return 0; }
int main(int c, char *v[]) { int i; FILE *in_fp; fprintf(stderr,"Compile date: %s\n", COMPDATE); fprintf(stderr,"Compiler switches: %s\n", CFLAGS); in_fp=fopen(v[1],"r"); if (!in_fp) { printf("ERROR: Could not open indata file\n"); exit(1); } fscanf(in_fp,"%d",&DISTRIB); fclose(in_fp); /* End of Benchmark stuff */ /* Write PPM header to stdout */ fprintf( stdout, "P6" ); fputc( 10, stdout ); fprintf( stdout, "%d %d", WIDTH, HEIGHT ); fputc( 10, stdout ); fprintf( stdout, "255" ); fputc( 10, stdout ); /***...calculate image...***/ TraceScene(); /***...write image to stdout...***/ for (i=0 ; i<3*WIDTH*HEIGHT ; ) { fputc( memory[i++]&~1, stdout); fputc( memory[i++]&~1, stdout); fputc( memory[i++]&~1, stdout); } return 0; /***...ANSI C wants main to return an int...***/ }
Colour RayTracer::TraceScene(Scene* pScene, Ray& ray, Colour incolour, int tracelevel, bool shadowray) { RayHitResult result; Colour outcolour = incolour; Colour reflectColour; Colour refractColour; std::vector<Light*>* light_list = pScene->GetLightList(); if (tracelevel <= 0) // reach the MAX depth of the recursion. { return outcolour; } result = pScene->IntersectByRay(ray, shadowray); if (result.data) //the ray has hit something { Vector3 start = ray.GetRayStart(); if (!shadowray) { outcolour = CalculateLighting(light_list,&start, &result); } else { return outcolour * 0.3f; } if(m_traceflag & TRACE_REFLECTION) { //Only consider reflection for spheres and boxes if (((Primitive*)result.data)->m_primtype == Primitive::PRIMTYPE_Sphere || ((Primitive*)result.data)->m_primtype == Primitive::PRIMTYPE_Box) { //TODO: Calculate reflection ray based on the current intersection result //Recursively call TraceScene with the reflection ray //Combine the returned colour with the current surface colour // DONE m_isReflecting = true; ray.SetRay(result.point, ray.GetRay().Reflect(result.normal)); outcolour *= TraceScene(pScene, ray, outcolour, --tracelevel, shadowray); } } if (m_traceflag & TRACE_REFRACTION) { //Only consider refraction for spheres and boxes if (((Primitive*)result.data)->m_primtype == Primitive::PRIMTYPE_Sphere || ((Primitive*)result.data)->m_primtype == Primitive::PRIMTYPE_Box) { //TODO: Calculate refraction ray based on the current intersection result //Recursively call TraceScene with the reflection ray //Combine the returned colour with the current surface colour // DONE double refractionIndex = 1; // Better refraction that using a constant if (m_isRefracting) { refractionIndex = ((Primitive*)result.data)->GetMaterial()->GetRefractiveIndex() / AIR_REFRACTIVE_INDEX; } else { refractionIndex = AIR_REFRACTIVE_INDEX / ((Primitive*)result.data)->GetMaterial()->GetRefractiveIndex(); } m_isRefracting = true; ray.SetRay(result.point + (result.normal * -0.0001), ray.GetRay().Refract((result.normal), 1)); outcolour += TraceScene(pScene, ray, outcolour, --tracelevel, shadowray) * ((Primitive*)result.data)->GetMaterial()->GetTransparency(); } } //Check if this is in shadow if ( m_traceflag & TRACE_SHADOW ) { std::vector<Light*>::iterator lit_iter = light_list->begin(); while (lit_iter != light_list->end()) { //TODO: Calculate the shadow ray using the current intersection result and the light position //Recursively call TraceScene with the shadow ray // DONE Vector3 l_normal = ((*lit_iter)->GetLightPosition() - result.point).Normalise(); Vector3 l = result.point + (l_normal * 0.0001); ray.SetRay(l, l_normal); outcolour = TraceScene(pScene, ray, outcolour, --tracelevel, true); lit_iter++; } } } return outcolour; }