void AmbientLight::commit() { Light::commit(); color = getParam3f("color", vec3f(1.f)); intensity = getParam1f("intensity", 1.f); vec3f radiance = getRadiance(); ispc::AmbientLight_set(getIE(), (ispc::vec3f&)radiance); }
// different visualization modes glm::vec3 Radiosity::setupHelperForColor(Face *f, int i, int j) { assert (mesh->getFace(i) == f); assert (j >= 0 && j < 4); if (args->render_mode == RENDER_MATERIALS) { return f->getMaterial()->getDiffuseColor(); } else if (args->render_mode == RENDER_RADIANCE && args->interpolate == true) { std::vector<Face*> faces; CollectFacesWithVertex((*f)[j],f,faces); float total = 0; glm::vec3 color = glm::vec3(0,0,0); glm::vec3 normal = f->computeNormal(); for (unsigned int i = 0; i < faces.size(); i++) { glm::vec3 normal2 = faces[i]->computeNormal(); float area = faces[i]->getArea(); if (glm::dot(normal,normal2) < 0.5) continue; assert (area > 0); total += area; color += float(area) * getRadiance(faces[i]->getRadiosityPatchIndex()); } assert (total > 0); color /= total; return color; } else if (args->render_mode == RENDER_LIGHTS) { return f->getMaterial()->getEmittedColor(); } else if (args->render_mode == RENDER_UNDISTRIBUTED) { return getUndistributed(i); } else if (args->render_mode == RENDER_ABSORBED) { return getAbsorbed(i); } else if (args->render_mode == RENDER_RADIANCE) { return getRadiance(i); } else if (args->render_mode == RENDER_FORM_FACTORS) { if (formfactors == NULL) ComputeFormFactors(); float scale = 0.2 * total_area/getArea(i); float factor = scale * getFormFactor(max_undistributed_patch,i); return glm::vec3(factor,factor,factor); } else { assert(0); } exit(0); }
Vec3f Radiosity::whichVisualization(enum RENDER_MODE mode, Face *f, int i) { assert (mesh->getFace(i) == f); assert (i >= 0 && i < num_faces); if (mode == RENDER_LIGHTS) { return f->getMaterial()->getEmittedColor(); } else if (mode == RENDER_UNDISTRIBUTED) { return getUndistributed(i); } else if (mode == RENDER_ABSORBED) { return getAbsorbed(i); } else if (mode == RENDER_RADIANCE) { return getRadiance(i); } else if (mode == RENDER_FORM_FACTORS) { if (formfactors == NULL) ComputeFormFactors(); double scale = 0.2 * total_area/getArea(i); double factor = scale * getFormFactor(max_undistributed_patch,i); return Vec3f(factor,factor,factor); } else { assert(0); } exit(0); }
double Radiosity::Iterate() { if (formfactors == NULL) ComputeFormFactors(); assert (formfactors != NULL); // ========================================== // ASSIGNMENT: IMPLEMENT RADIOSITY ALGORITHM // ========================================== for (int i = 0; i < num_faces; ++i) { Face *f1 = mesh->getFace(i); Material *m = f1->getMaterial(); Vec3f p_i = m->getDiffuseColor(); int j = max_undistributed_patch; if (i == j) { continue; } Vec3f ff = formfactors[i * num_faces + j] * getUndistributed(j); setRadiance(i, getRadiance(i) + ff * p_i); setUndistributed(i, getUndistributed(i) + ff * p_i); setAbsorbed(i, getAbsorbed(i) + (Vec3f(1,1,1)-p_i) * ff); } setUndistributed(max_undistributed_patch, Vec3f(0,0,0)); // return the total light yet undistributed // (so we can decide when the solution has sufficiently converged) findMaxUndistributed(); return total_undistributed; }
float Radiosity::Iterate() { if (formfactors == NULL) ComputeFormFactors(); assert (formfactors != NULL); // ========================================== // ASSIGNMENT: IMPLEMENT RADIOSITY ALGORITHM // ========================================== //Compute the radiosity matrix. I feel like I shouldn't have to do this //every time. Maybe this is what the form factors matrix was meant for //save the radiances std::vector<glm::vec3> radiations, absorbeds; for (int i=0; i<num_faces; i++){ Face* face=mesh->getFace(i); glm::vec3 ro_i=face->getMaterial()->getDiffuseColor(); glm::vec3 ones(1,1,1); glm::vec3 abso_i=ones-ro_i; glm::vec3 radiance=face->getMaterial()->getEmittedColor(); glm::vec3 absorbed_i(0,0,0);//=getAbsorbed(i); for (int j=0; j<num_faces; j++){ //new_row.push_back(-ro_i*getFormFactor(i,j)) //might need to reverse that j,i radiance+=ro_i*getFormFactor(j,i)*getRadiance(j); absorbed_i+=abso_i*getFormFactor(j,i)*getRadiance(j); } //the undistributed light is the stuff we got on this iteration setUndistributed(i, radiance-getRadiance(i)); //std::cout<<glm::length(radiance)<<' '; radiations.push_back(radiance); absorbeds.push_back(absorbed_i); } //std::cout<<std::endl; //set the computed radiances for(int i=0; i<num_faces; i++){ setRadiance(i, radiations[i]); setAbsorbed(i, absorbeds[i]); } //now we must calculate the undistributed light float total_undistributed = 0; glm::vec3 ambient; for (int i=0; i<num_faces;i++){ total_undistributed+=glm::length(getUndistributed(i)); ambient+=getUndistributed(i); } //uncomment this for the ambient lighting term. This implementation, while simple //breaks my visualization for undistributed #if 1 ambient/=float(num_faces); for (int i=0; i<num_faces; i++){ setRadiance(i, getRadiance(i)+ambient); } #endif // return the total light yet undistributed // (so we can decide when the solution has sufficiently converged) std::cout<<total_undistributed<<std::endl; return total_undistributed; }
float Radiosity::Iterate() { if (formfactors == NULL) ComputeFormFactors(); assert (formfactors != NULL); // ========================================== // ASSIGNMENT: IMPLEMENT RADIOSITY ALGORITHM // ========================================== //printf("\n\n"); //take the thing with the most undistributed radiosity findMaxUndistributed(); float totalUnd = 0; for(int j = 0; j<num_faces; j++) { if (j == max_undistributed_patch) { continue; } //use max_undistributed_patch for the thing giving away radience //the j panel absorbs some of the light and reflects the rest /*printf("MaxUndist: (%f %f %f)\tJDiffuseColor: (%f %f %f)\tAbsorbed: (%f %f %f)\tRadiance: (%f %f %f)\tUndistributed: (%f %f %f)\t\n\n", getUndistributed(max_undistributed_patch).x, getUndistributed(max_undistributed_patch).y, getUndistributed(max_undistributed_patch).z, mesh->getFace(j)->getMaterial()->getDiffuseColor().x, mesh->getFace(j)->getMaterial()->getDiffuseColor().y, mesh->getFace(j)->getMaterial()->getDiffuseColor().z, getAbsorbed(j).x, getAbsorbed(j).y, getAbsorbed(j).z, getRadiance(j).x, getRadiance(j).y, getRadiance(j).x, getUndistributed(j).x, getUndistributed(j).y, getUndistributed(j).z);//*/ //give it to everyone else based on form factor setRadiance(j, getUndistributed(max_undistributed_patch)*(getFormFactor(max_undistributed_patch, j)/ totalFormFactori[max_undistributed_patch])*mesh->getFace(j)->getMaterial()->getDiffuseColor() +//- getAbsorbed(j) + *******INSTEAD OF GET ABSORBED THIS SHOULD BE PANEL COLOR) getRadiance(j));//*/ setAbsorbed(j, (getUndistributed(max_undistributed_patch)*(1.0f-mesh->getFace(j)->getMaterial()->getDiffuseColor()))* (getFormFactor(max_undistributed_patch, j)/totalFormFactori[max_undistributed_patch]) + (getAbsorbed(j)));//*/ setUndistributed(j, getUndistributed(max_undistributed_patch)*(getFormFactor(max_undistributed_patch, j)/ totalFormFactori[max_undistributed_patch])*mesh->getFace(j)->getMaterial()->getDiffuseColor() + getUndistributed(j));//*/ totalUnd += glm::length(getUndistributed(j)); /*printf("MaxUndist: (%f %f %f)\tJDiffuseColor: (%f %f %f)\tAbsorbed: (%f %f %f)\tRadiance: (%f %f %f)\tUndistributed: (%f %f %f)\t\n", getUndistributed(max_undistributed_patch).x, getUndistributed(max_undistributed_patch).y, getUndistributed(max_undistributed_patch).z, mesh->getFace(j)->getMaterial()->getDiffuseColor().x, mesh->getFace(j)->getMaterial()->getDiffuseColor().y, mesh->getFace(j)->getMaterial()->getDiffuseColor().z, getAbsorbed(j).x, getAbsorbed(j).y, getAbsorbed(j).z, getRadiance(j).x, getRadiance(j).y, getRadiance(j).x, getUndistributed(j).x, getUndistributed(j).y, getUndistributed(j).z); printf("***************************************************************************************\n");//*/ } //setRadiance(max_undistributed_patch, glm::vec3(0,0,0)); setUndistributed(max_undistributed_patch, glm::vec3(0, 0, 0)); //============================================== // return the total light yet undistributed //============================================== // (so we can decide when the solution has sufficiently converged) //printf("TotalUndistributed: %f", totalUnd); return totalUnd; }
RGBColor RayMarchingIntegrator::getRadiance(const Ray& ray) const { Intersection intersection = world->scene->intersect(ray); if (intersection) { RGBColor color = RGBColor(0, 0, 0); Point local; if (intersection.solid->texMapper == nullptr) { local = WorldMapper(Float4::rep(1)).getCoords(intersection); } else { local = intersection.solid->texMapper->getCoords(intersection); } RGBColor emission; if (intersection.solid->material == nullptr) { float greyScale = fabs(dot(intersection.ray.d, intersection.normalVector)); emission = RGBColor(greyScale, greyScale, greyScale); } else { emission = intersection.solid->material->getEmission(local, intersection.normalVector, -ray.d); } Material::SampleReflectance sample; Ray sampleRay; switch (intersection.solid->material->useSampling()) { case Material::SAMPLING_NOT_NEEDED: for (int i = 0; i < world->light.size(); i++) { LightHit shadowRay = world->light[i]->getLightHit(intersection.hitPoint()); if (dot(intersection.normalVector, shadowRay.direction) > 0) { Ray shadow = Ray(intersection.hitPoint() + EPSILON * shadowRay.direction, shadowRay.direction); //TODO more epsilon? Intersection shadowRayIntersection = world->scene->intersect(shadow, shadowRay.distance); if (!shadowRayIntersection) { RGBColor reflectance; if (intersection.solid->material == nullptr) { reflectance = RGBColor(0.5, 0.5, 0.5); } else { if(intersection.solid->isFracLand){ reflectance = intersection.solid->material->getReflectance( intersection.hitPoint(), intersection.normalVector, -ray.d, shadowRay.direction); } else{ reflectance = intersection.solid->material->getReflectance(local, intersection.normalVector, -ray.d, shadowRay.direction); } // reflectance = intersection.solid->material->getReflectance(local, // intersection.normalVector, -ray.d, shadowRay.direction); } RGBColor intensity = world->light[i]->getIntensity(shadowRay); RGBColor lightSourceColor = (reflectance * intensity); color = color + lightSourceColor; } } } break; case Material::SAMPLING_ALL: sample = intersection.solid->material->getSampleReflectance(intersection.hitPoint(), intersection.normalVector, -intersection.ray.d); // sample = intersection.solid->material->getSampleReflectance(local, intersection.normalVector, // -intersection.ray.d); sampleRay = Ray(intersection.hitPoint() + EPSILON * sample.direction, sample.direction); if (MarchRecursionDepth > MAX_RECURSION_DEPTH) { MarchRecursionDepth = 0.f; } else { MarchRecursionDepth++; color = color + getRadiance(sampleRay) * sample.reflectance; } break; case Material::SAMPLING_SECONDARY: for (int i = 0; i < world->light.size(); i++) { LightHit shadowRay = world->light[i]->getLightHit(intersection.hitPoint()); if (dot(intersection.normalVector, shadowRay.direction) > 0) { Ray shadow = Ray(intersection.hitPoint() + EPSILON * intersection.normal(), shadowRay.direction); Intersection shadowRayIntersection = world->scene->intersect(shadow, shadowRay.distance); if (!shadowRayIntersection) { RGBColor reflectance = intersection.solid->material->getReflectance(local, intersection.normalVector, -ray.d, shadowRay.direction); RGBColor emission = intersection.solid->material->getEmission(local, intersection.normalVector, -ray.d); RGBColor intensity = world->light[i]->getIntensity(shadowRay); RGBColor lightSourceColor = (reflectance * intensity); color = color + lightSourceColor; } } } sample = intersection.solid->material->getSampleReflectance(local, intersection.normalVector, -intersection.ray.d); if (sample.direction != Vector(0, 0, 1) && sample.reflectance != RGBColor(0.f, 0.f, 0.f)) { sampleRay = Ray(intersection.hitPoint() + EPSILON * sample.direction, sample.direction); if (MarchRecursionDepth > MAX_RECURSION_DEPTH) { MarchRecursionDepth = 0; } else { MarchRecursionDepth++; color = color + getRadiance(sampleRay) * sample.reflectance; } } break; default: break; } MarchRecursionDepth = 0; if (world->fogs.size() == 0) { return color + emission; } /* RGBColor fog = world->fog->modulateColor(ray.o, intersection.hitPoint()); float transmittance = world->fog->transmittance(ray.o, intersection.hitPoint()); return (color + emission) * transmittance + (1 - transmittance) * fog;*/ RGBColor fogColor = RGBColor::rep(0); for (int i = 0; i < world->fogs.size(); i++) { Intersection i1 = world->fogs[i]->getPrimitive()->intersect(ray, intersection.distance); if (i1) { float transmittance = 1; float enterDistance = 0; //i1.distance; //float exitDistance = i1.exitDistance == i1.distance ? intersection.distance : i1.exitDistance; float exitDistance = intersection.distance; //std::min(i1.exitDistance, intersection.distance); while (enterDistance < exitDistance) { float stepSize = std::min(STEP_SIZE, exitDistance - enterDistance); Point deltaPoint = ray.o + (enterDistance + stepSize / 2) * ray.d; float deltaTransmittance = exp( -world->fogs[i]->getDensity(deltaPoint) * stepSize); RGBColor deltaColor = RGBColor(0, 0, 0); for (int i = 0; i < world->light.size(); i++) { LightHit shadowRay = world->light[i]->getLightHit( deltaPoint); Ray shadow = Ray( deltaPoint + EPSILON * shadowRay.direction, shadowRay.direction); Intersection shadowRayIntersection = world->scene->intersect(shadow, shadowRay.distance); if (!shadowRayIntersection) { RGBColor intensity = world->light[i]->getIntensity( shadowRay); RGBColor lightSourceColor = (intensity); deltaColor = (deltaColor + lightSourceColor); } } fogColor = fogColor + transmittance * (1 - deltaTransmittance) * deltaColor * world->fogs[i]->getColor(deltaPoint); transmittance *= deltaTransmittance; enterDistance += STEP_SIZE; } //LOG_DEBUG("fogcolor " << fogColor.r << ", " << fogColor.g << ", " << fogColor.b); } } return (color + emission) + fogColor; } else { //if (!world->fog) return RGBColor(0, 0, 0); /*Intersection i1 = world->fog->getPrimitive()->intersect(ray); if (i1) { RGBColor fogColor = RGBColor::rep(0); float transmittance = 1; float enterDistance = i1.distance; //float exitDistance = i1.exitDistance == i1.distance ? intersection.distance : i1.exitDistance; float exitDistance = std::min(i1.exitDistance, intersection.distance); while (enterDistance < exitDistance) { float stepSize = std::min(STEP_SIZE, exitDistance - enterDistance); Point deltaPoint = ray.o + (enterDistance + stepSize / 2) * ray.d; float deltaTransmittance = exp(-world->fog->getDensity(deltaPoint) * stepSize); RGBColor deltaColor = RGBColor(0, 0, 0); for (int i = 0; i < world->light.size(); i++) { LightHit shadowRay = world->light[i]->getLightHit(deltaPoint); if (dot(intersection.normalVector, shadowRay.direction) > 0) { Ray shadow = Ray(deltaPoint + EPSILON * shadowRay.direction, shadowRay.direction); Intersection shadowRayIntersection = world->scene->intersect(shadow, shadowRay.distance); if (!shadowRayIntersection) { RGBColor reflectance = world->fog->getColor(deltaPoint); RGBColor intensity = world->light[i]->getIntensity(shadowRay); RGBColor lightSourceColor = (reflectance * intensity); deltaColor = (deltaColor + lightSourceColor); } } } fogColor = fogColor + transmittance * (1 - deltaTransmittance) * deltaColor; transmittance *= deltaTransmittance; enterDistance += STEP_SIZE; } return fogColor; } RGBColor fog = world->fog->getColor(intersection.hitPoint());*/ //return fog; } }