ColourRGB RayTracer::ray_trace(Ray ray, World* world, long depth) { Intersection intersection; ColourRGB colour; if (depth > 5) { return black; } bool hit = world->nearest_intersection(ray, &intersection); if (hit) { Material *m = intersection.shape->get_material(); colour = m->direct_illumination(intersection, world); if (m->is_reflective()) { Vector3D v = ray.direction; Vector3D n = intersection.normal; Point3D p = intersection.hit_point; Vector3D r = v - (2 * n * n.dot(v)); Ray reflected(p, r); colour += m->reflectance() * this->ray_trace(reflected, world, depth + 1); } } else { colour = world->get_background(); } return colour + world->get_ambient(); }
void polyvertex::reflect() { std::vector<point3D> reflected(vertices.size()); std::transform(vertices.begin(), vertices.end(), reflected.begin(), std::negate<point3D>()); vertices.resize(vertices.size() + vertices.size()); std::copy_backward(reflected.begin(), reflected.end(), vertices.end()); }
Color Scene::lanceRayon(const Rayon& ray, int iteration) const { this->addObjectsTabToBinder(); Color result(0.0,0.0,0.0); float minDist(1000.0) ; float dist (0.0) ; Hit hit ; if (binder->intersect(ray,0.0,100.0,hit)) { //result = hit.getObject()->getOptic()->getColor(hit.getU(),hit.getV()); //result = hit.getObject()->getOptic()->getColor(0.5,0.5); Color coulObj(hit.getObject()->getOptic()->getColor(hit.getU(),hit.getV())); for ( std::vector<std::shared_ptr<LightSource>>::const_iterator it = lightsTab.begin(); it != lightsTab.end() ; ++it) // pour chaque source { //d = calcul distance point intersection source Vector directi(it->get()->getOrigin()-hit.getImpactPoint()); float distInterSource = directi.getNorm() ; directi.normalize(); //initialiser Ray : point intersect, direction(point intersect, source), couleur = on s'en fout Color c(0.0,0.0,0.0); Color resultNorm(0.0,0.0,0.0); Rayon ray(hit.getImpactPoint(),directi,c); if (! binder->intersect(ray, 0, distInterSource)) { Color diff(it->get()->getColor()*coulObj*(dotProduct(hit.getNormal(),ray.getDirect()))); Vector moinsV(-directi.getX(),-directi.getY(),-directi.getZ()); Vector miroirV(moinsV + hit.getNormal()*(2*(dotProduct(directi,hit.getNormal())))); //Vmir = V symétrique par rapport à N //spec = coulspec(obj)* (tronquerAZero(RayS.Vmir))^n * coul(source) Color spec(it->get()->getColor()*coulObj*dotProduct(ray.getDirect(),miroirV)); resultNorm = diff + spec ; if ( iteration < 2) { //Res2 = influence rayon réfléchi Rayon reflected(hit.getImpactPoint(),miroirV,c); Color reflectedColor(0.0,0.0,0.0); reflectedColor = this->lanceRayon(reflected,iteration+1); //return pourcent1*Res + ourcent2*Res2 result = resultNorm*0.8 + reflectedColor*0.2 ; } else { result = resultNorm ; } } } } return result; }
Color RayTracer::TraceSpecularReflect(const Scene *scene, const Ray& ray, const LocalGeo& geom, int depth, const CameraSample& samplebuf){ Vec3 wo = -ray.dir, wi; float pdf; Color color; Color f = geom.bsdf->SampleDirect(wo, geom, Sample(), &wi, &pdf, BSDFType(BSDF_REFLECTION | BSDF_SPECULAR)); float absdot_wi_n = Math::AbsDot(wi, geom.normal); if (pdf > 0.f && f != Color::BLACK && absdot_wi_n != 0.f){ Ray reflected(geom.point, wi); // TODO differential color = f * Li(scene, reflected, depth, samplebuf) *absdot_wi_n / pdf; } return color; }
int Velasquez::ReflectHoverPoint::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { _id = HoverPoint::qt_metacall(_c, _id, _a); if (_id < 0) return _id; if (_c == QMetaObject::InvokeMetaMethod) { switch (_id) { case 0: reflected((*reinterpret_cast< bool(*)>(_a[1]))); break; default: ; } _id -= 1; } return _id; }
RGBVec World::traceRay(const Ray &ray, double t_min, int depth) { IntersectionDatum idat = testIntersection(ray, t_min, scenery); if (!idat.intersected) return bg_colour; RGBVec result_vec; ShadableObject *obj = static_cast<ShadableObject *>(idat.intersectedObj); double t = idat.coefficient; // compute lighting/shading for (std::vector<Light>::iterator lptr = lighting.begin(); lptr != lighting.end(); lptr++) { vec3 p = ray.intersectionPoint(t); vec3 n = obj->surfaceNormal(p); vec3 v = (ray.origin - lptr->pos).normalised(); vec3 l = (lptr->pos - p).normalised(); Ray shadowRay(p,l); // if we're not in shadow w.r.t this light if (!shadows_enabled || !traceShadowRay(shadowRay, scenery)) { result_vec += obj->material.shade(*lptr, n, v, l); } if (reflections_enabled && obj->material.reflective && depth < MAX_TRACE_DEPTH) { // recursively trace reflection rays: vec3 d = ray.direction; Ray reflected(p, d - n.scaled(2 * d.dot(n))); RGBVec reflectedColour = traceRay(reflected, REFLECTION_EPS, depth + 1); if (reflectedColour.r() == 0.0 && reflectedColour.g() == 0.0 && reflectedColour.b() == 0.0) continue; RGBVec k_m = obj->material.specular_colour; result_vec += reflectedColour.multiplyColour(k_m); } } return result_vec; }
glm::vec4 BRDF::sampleColor(FastStack<Ray> & rays, const Ray ray, const Intersection & result, Scene * scene) { glm::vec4 position = result.surface->worldTransform * result.surface->samplePosition(result.index, result.coord); glm::vec4 normal = glm::normalize( result.surface->worldTransformIT * result.surface->sampleNormal(result.index, result.coord)); float p = SampleUniform(); glm::vec4 totalColor(0.f, 0.f, 0.f, 0.f); if (p < emissiveProbability) { totalColor = emissiveIntensity * emissiveColor / emissiveProbability; } else if (ray.depth < maxDepth) { glm::vec4 strength = ray.strength * diffuseColor; glm::vec4 outgoing = SampleHemi(normal); Ray reflected(ray.depth + 1, position, outgoing, strength, PAC_EPSILON); rays.push(reflected); } else { totalColor.a = 1.f; } return totalColor * ray.strength; }
glm::vec3 Phong::areaLightShade(const ShadeRec& sr)const { glm::vec3 incoming(-sr.ray.getDirection()); glm::vec3 ambient = sr.world.getAmbient()->L(sr); glm::vec3 color = ambientBrdf->rho(sr,incoming); color.x *= ambient.x; color.y *= ambient.y; color.z *= ambient.z; int numLights = sr.world.lights.size(); for(int i=0;i<numLights;i++) { glm::vec3 reflected(sr.world.lights[i]->getDirection(sr)); float nDotI = glm::dot(sr.normal,reflected); if(nDotI > 0.0) { bool inShadow = false; if(hasShadows && sr.world.lights[i]->castsShadows()) { Ray shadowRay(sr.hitPoint,reflected); inShadow = sr.world.lights[i]->inShadow(shadowRay,sr); } if(!inShadow) { glm::vec3 gainedColor(diffuseBrdf->f(sr,incoming,reflected) + specularBrdf->f(sr,incoming,reflected)); glm::vec3 lightColor = sr.world.lights[i]->L(sr) * nDotI; lightColor.x *= gainedColor.x; lightColor.y *= gainedColor.y; lightColor.z *= gainedColor.z; color += lightColor; } } } return color; }