/** * Trace a ray and return the color of the given ray. * @param ray [description] * @return */ glm::vec3 trace(Ray ray, float distanceTraveled, int maxDepth) { glm::vec3 color = glm::vec3(0.0f); Intersection inters = getClosestIntersection(ray, false); if(inters.didHit()) { //if we hit something figure out the color. color += inters.object->material.aColor; // Ambient lighting. if(inters.object->material.opacity < 1.0f && maxDepth > 0) { // Transmitted light float rRatio; float cosTheta = glm::dot(inters.incident.direction, inters.normal); if(inters.inside) { rRatio = inters.object->material.refractiveIndex; cosTheta *= -1.0f; } else { rRatio = 1.0f/inters.object->material.refractiveIndex; } float antiCos = sqrtf(1.0f - (1.0f-cosTheta*cosTheta)*rRatio*rRatio); // printf("refraction with ratio: %f, cos: %f, antiCos: %f\n", rRatio, cosTheta, antiCos); glm::vec3 refractedDir = rRatio*inters.incident.direction + (cosTheta*rRatio + antiCos)*inters.normal; color += trace(Ray(inters.point, refractedDir), inters.distanceTraveled+distanceTraveled, maxDepth-1)*(1.0f-inters.object->material.opacity); } if(inters.object->material.reflectivity > 0.0f && maxDepth > 0) { // Reflected light glm::vec3 projOntoNorm = -glm::dot(inters.incident.direction, inters.normal)*inters.normal; glm::vec3 reflectDir = inters.incident.direction + projOntoNorm*2.0f; color += trace(Ray(inters.point, reflectDir), inters.distanceTraveled+distanceTraveled, maxDepth-1)*inters.object->material.rColor*inters.object->material.reflectivity; } if(!inters.inside) { // GLORIOUS IMPROVEMENTS, Diffuse light for(std::vector<Light>::iterator lightIter = scene.lights.begin(); lightIter != scene.lights.end(); ++lightIter) { glm::vec3 lightDir = lightIter->location - inters.point; float distanceToLight = glm::length(lightDir); lightDir = glm::normalize(lightDir); Ray shadowRay = Ray(inters.point, lightDir); Intersection shadowIntersection = getClosestIntersection(shadowRay, true); if((!shadowIntersection.didHit() || distanceToLight < shadowIntersection.distanceTraveled )) {// We hit something behind the light float totalDistanceTraveled = distanceToLight + inters.distanceTraveled + distanceTraveled; // Distance from light to intersection + inters to eye + recursion(reflected/refracted) float difIntensity = glm::dot(lightDir, inters.normal)*lightIter->power; glm::vec3 halfAngle = glm::normalize(lightDir - inters.incident.direction); // incident is in the direction from eye, so negate float NdotH = std::max(0.0f, glm::dot(inters.normal, halfAngle)); float specIntensity = powf(NdotH, inters.object->material.specHardness)*lightIter->power; // Spectral hardness of the material color += (lightIter->color * inters.object->material.sColor)*specIntensity/powf(totalDistanceTraveled, 2); color += (lightIter->color * inters.object->material.dColor)*difIntensity/powf(totalDistanceTraveled, 2); } } } } return color; }
Vector3d traceRay(Ray* ray, vector<SceneObject*>* objects, vector<SceneObject*>* lights, int remainingDepth) { Vector3d backgroundColour(0, 0, 0); Vector3d ambientLight(25, 25, 25); /*Vector3d RED(255, 0, 0); Vector3d GREEN(0, 255, 0); Vector3d BLUE(255, 0, 255); Vector3d YELLOW(255, 255, 0); Vector3d CYAN(0, 255, 255); Vector3d MAJENTA(255, 0, 255);*/ if (remainingDepth <= 0) return backgroundColour; vector<Intersection*>* intersections = getIntersections(objects, ray, NULL, false, (remainingDepth < 2)); Intersection* closestIntersection = getClosestIntersection(ray->origin, intersections); Vector3d closestOrigin; Vector3d closestDirection; SceneObject* closestObject; if (closestIntersection != NULL) { closestOrigin = *(closestIntersection->origin); closestDirection = *(closestIntersection->direction); closestObject = closestIntersection->object; } freeIntersections(intersections); intersections->clear(); delete intersections; if (closestIntersection != NULL) { Vector3d fullLightColour = ambientLight; Vector3d surfaceColour = *(closestIntersection->object->colour); //for each light, add it to the full light on this point (if not blocked) for (unsigned int lightNum = 0; lightNum < lights->size(); lightNum++) { SceneObject* light = (*lights)[lightNum]; Vector3d toLight = *(light->position) - closestOrigin; Vector3d toLightNormalized = toLight.normalized(); double dot = toLightNormalized.dot(closestDirection); if (dot > 0) { intersections = getIntersections(objects, new Ray(&closestOrigin, &toLightNormalized), closestObject, true, false); bool inLight = false; if (intersections->size() == 0) { inLight = true; } else { Intersection* intersection = (*intersections)[0]; Vector3d intersectionOrigin = Vector3d(*(intersection->origin)); Vector3d intersectionDirection = Vector3d(*(intersection->direction)); SceneObject* obj = intersection->object; if ((intersectionOrigin - closestOrigin).norm() > toLight.norm()) { inLight = true; } } if (inLight) { fullLightColour += *(light->colour) * dot; } freeIntersections(intersections); intersections->clear(); delete intersections; } } double reflectivity = closestObject->reflectivity; if (reflectivity > 0) { Vector3d rayDirection = *(ray->direction); Vector3d normal = closestDirection; Vector3d reflectedDirection = rayDirection - ((2 * (normal.dot(rayDirection))) * normal); Ray* reflectedRay = new Ray(&closestOrigin, &reflectedDirection); Vector3d reflectionColour = traceRay(reflectedRay, objects, lights, remainingDepth - 1); delete reflectedRay; surfaceColour *= 1 - reflectivity; surfaceColour += reflectionColour * reflectivity; } Vector3d endColour = surfaceColour; endColour[0] *= fullLightColour[0] / 255; endColour[1] *= fullLightColour[1] / 255; endColour[2] *= fullLightColour[2] / 255; return endColour; } else { return backgroundColour; } }