Color raytrace( Scene scene, Ray ray ) { Color color; color.r = 0; color.b = 0; color.g = 0; Intersection best; best.hit = false; float bestT = 10000; float t; for( int j = 0; j < scene.numSpheres; j++ ) { t = sphereHitTest( scene.spheres[j], ray ); if( t > 0 ) { if( !best.hit || t < bestT ) { best = sphereIntersection( scene.spheres[j], ray, t ); bestT = t; } } } for( int j = 0; j < scene.numTriangles; j++ ) { t = triangleHitTest( scene.triangles[j], ray ); if( t > 0 ) { if( !best.hit || t < bestT ) { best = triangleIntersection( scene.triangles[j], ray, t ); bestT = t; } } } for( int j = 0; j < scene.numPlanes; j++ ) { t = planeHitTest( scene.planes[j], ray ); if( t > 0 ) { if( !best.hit || t < bestT ) { best = planeIntersection( scene.planes[j], ray, t ); bestT = t; } } } if( best.hit ) { color = plus( color, directIllumination( best, scene ) ); //printf("color: %f, %f, %f\n", color.r, color.g, color.b); } return limitColor( color ); }
IAccDataStruct::IntersectionData Heightmap::createTrianglesAndIntersect(Ray ray, vec3 v0, vec3 v1, vec3 v2, vec3 v3) { vector<vec3*> vertices; vertices.push_back(&v0); vertices.push_back(&v1); vertices.push_back(&v2); vec3 normal = vec3(cross(v1-v0,v2-v0)); vector<vec3*> normals; normals.push_back(&normal); normals.push_back(&normal); normals.push_back(&normal); vector<vec3*> tex_coords; Triangle* triangle1 = new Triangle(vertices,normals,tex_coords,0); vector<vec3*> vertices2; vertices2.push_back(&v2); vertices2.push_back(&v3); vertices2.push_back(&v0); vec3 normal2 = vec3(cross(v2-v3,v2-v0)); vector<vec3*> normals2; normals2.push_back(&normal2); normals2.push_back(&normal2); normals2.push_back(&normal2); vector<vec3*> tex_coords2; Triangle* triangle2 = new Triangle(vertices2,normals2,tex_coords2,0); return triangleIntersection(ray,triangle1,triangle2); }
/** * Triangle Intersection */ float Triangle::getIntersection(Vector3 eyeOrig, Vector3 delOrig, float startInterval, float endInterval, PixelInfo* intersection) { //transform e and d: Vector3 eye=((*inverseTransMat)*getV4(eyeOrig, 1)).xyz(); Vector3 del=((*inverseTransMat)*getV4(delOrig, 0)).xyz(); float alpha, beta, gamma; float t=triangleIntersection(eye, del, startInterval, endInterval, &alpha, &beta, &gamma, vertices[0].position, vertices[1].position, vertices[2].position); if(!t) return 0; if(intersection) { intersection->ambient=(alpha*(vertices[0].material->ambient)) + (beta*(vertices[1].material->ambient)) + (gamma*(vertices[2].material->ambient)); intersection->diffuse=(alpha*(vertices[0].material->diffuse)) + (beta*(vertices[1].material->diffuse)) + (gamma*(vertices[2].material->diffuse)); intersection->specular=(alpha*(vertices[0].material->specular)) + (beta*(vertices[1].material->specular)) + (gamma*(vertices[2].material->specular)); intersection->refractive_index=(alpha*(vertices[0].material->refractive_index)) + (beta*(vertices[1].material->refractive_index)) + (gamma*(vertices[2].material->refractive_index)); intersection->intersectionPoint=eyeOrig+(t*delOrig); intersection->normal=normalize((*normalMat)*(alpha*vertices[0].normal + beta*vertices[1].normal + gamma*vertices[2].normal)); // calculate texture color: Vector2 tex_coord=(alpha*vertices[0].tex_coord) + (beta*vertices[1].tex_coord) + (gamma*vertices[2].tex_coord); intersection->tex_coord=tex_coord; intersection->tex_color=alpha*getTextureColor(tex_coord, vertices[0].material)+ beta*getTextureColor(tex_coord, vertices[1].material)+ gamma*getTextureColor(tex_coord, vertices[2].material);//*/ } return t; }