Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vector4 Renderer::traceColor(Ray ray, Scene* scene, unsigned int recDepth) {
- Vector4 color;
- IntersectionData* iData = scene->intersect(ray);
- if (iData) { // sucessful intersection test
- //shade color
- color = m_shader->shade(iData, scene);
- if (recDepth < m_recursionDepth) {
- color = color.clamp01() * (1-iData->reflectionPercentage - iData->refractionPercentage);
- //normal direction
- Vector3 n = iData->normal;
- //view direction
- Vector3 w = -ray.direction;
- //origin of refraction/reflection ray
- Vector3 o_r = iData->position;
- //any reflection?
- if (iData->reflectionPercentage) {
- //direction of reflection ray
- Vector3 d_refl = (n*(w.dot(n))*2-w);
- Ray reflRay = Ray(o_r,d_refl);
- reflRay.min_t = reflRay.epsilon_t;
- color += traceColor(reflRay,scene,recDepth+1) * iData->reflectionPercentage;
- }
- //any refraction?
- if (iData->refractionPercentage) {
- //just one object for now
- double refrFrac = scene->getRefractionIndex() / iData->refractionIndex;
- if (!iData->rayEntersObject)
- refrFrac = 1/refrFrac;
- //angle between view and normal
- double angle = w.dot(n);
- //refraction direction
- Vector3 d_refr;
- if (angle >= asin(1/refrFrac)) { //case of total reflection
- d_refr = (n*(w.dot(n))*2-w);
- } else {
- d_refr = (n*w.dot(n)-w)*(refrFrac) - n*sqrt(1-refrFrac*refrFrac*(1-w.dot(n)*w.dot(n)));
- }
- double checkangle = d_refr.normalize().dot(ray.direction);
- Ray refrRay = Ray(o_r,d_refr);
- refrRay.min_t = refrRay.epsilon_t;
- color += traceColor(refrRay,scene,recDepth+1) * iData->refractionPercentage;
- }
- } else {
- color = color.clamp01();
- }
- }
- else {
- color = scene->getBackground(); // background color
- }
- // clean
- if (iData!=NULL){
- delete(iData);
- }
- return color.clamp01();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement