simple shadow implementation and miscellaneous conversion changes

This commit is contained in:
arvid schröder 2022-11-17 20:28:23 +01:00
parent b2788a2291
commit 43f44fb418
5 changed files with 19 additions and 6 deletions

View file

@ -18,7 +18,7 @@
int main() { int main() {
// Let's create a simple cornell box scene... // Let's create a simple cornell box scene...
SimpleScene scene; SimpleScene scene;
scene.setEnvironmentMap(std::make_shared<Texture>("data/lion_env.png")); scene.setEnvironmentMap(std::make_shared<Texture>("../data/lion_env.png"));
auto mirror = std::make_shared<MirrorShader>(); auto mirror = std::make_shared<MirrorShader>();
auto glass = std::make_shared<RefractionShader>(1.31f, 1.0f); auto glass = std::make_shared<RefractionShader>(1.31f, 1.0f);

View file

@ -6,18 +6,26 @@ PointLight::PointLight(Vector3d const &position, float intensity, Color const &c
Light::Illumination PointLight::illuminate(Scene const &scene, Ray const &ray) const { Light::Illumination PointLight::illuminate(Scene const &scene, Ray const &ray) const {
// IMPLEMENT ME // IMPLEMENT ME
// Get the point on the surface // Get the point on the surface
Vector3d intersectionPoint = ray.origin + ray.length * ray.direction;
// Create an instance of the Illumination object, fill in the direction (from // Create an instance of the Illumination object, fill in the direction (from
// surface point to light source) // surface point to light source)
Light::Illumination illum; Light::Illumination illum;
illum.direction = normalized(this->position - intersectionPoint);
// Define a secondary ray from the surface point to the light source. // Define a secondary ray from the surface point to the light source.
Ray lightRay = Ray(intersectionPoint, this->position - intersectionPoint);
// If the target is not in shadow... (use scene.findOcclusion()) // If the target is not in shadow... (use scene.findOcclusion())
bool inShadow = scene.findOcclusion(lightRay);
// Compute the brightness-color of this light using inverse squared distance // Compute the brightness-color of this light using inverse squared distance
// to light source (i.e. 1/(d^2)), the color of this light source and the // to light source (i.e. 1/(d^2)), the color of this light source and the
// intensity // intensity
if (!inShadow) {
illum.color = (this->intensity / pow(length(this->position - intersectionPoint), 2)) * this->color;
}
return illum; return illum;
} }

View file

@ -77,7 +77,7 @@ bool Triangle::intersect(Ray &ray) const {
// Make sure the ray is not parallel to the triangle // Make sure the ray is not parallel to the triangle
float const det = dotProduct(edge1, pVec); float const det = dotProduct(edge1, pVec);
if (fabs(det) < EPSILON) if (std::abs(det) < EPSILON)
return false; return false;
float const inv_det = 1.0f / det; float const inv_det = 1.0f / det;

View file

@ -11,11 +11,10 @@ Color RefractionShader::shade(Scene const &scene, Ray const &ray) const {
// Send out a new refracted ray into the scene; recursively call traceRay() // Send out a new refracted ray into the scene; recursively call traceRay()
if (ray.getRemainingBounces() < 1) { if (ray.getRemainingBounces() < 1) {
return Color(0,0,0); return {0,0,0};
} }
Vector3d n = ray.normal; Vector3d n = ray.normal;
double root = sqrt(1 - (indexOutside*indexOutside*(1 - dotProduct(ray.direction, n) float root = std::sqrt(1 - (indexOutside*indexOutside*(1 - dotProduct(ray.direction, n)
*dotProduct(ray.direction, n)))/(indexInside*indexInside)); *dotProduct(ray.direction, n)))/(indexInside*indexInside));
Vector3d t = (indexOutside/indexInside)*(ray.direction - dotProduct(ray.direction, n) * n)- n * root; Vector3d t = (indexOutside/indexInside)*(ray.direction - dotProduct(ray.direction, n) * n)- n * root;

View file

@ -8,5 +8,11 @@ Color SimpleShadowShader::shade(Scene const &scene, Ray const &ray) const {
// IMPLEMENT ME // IMPLEMENT ME
// loop over all light sources to check for visibility and multiply "light // loop over all light sources to check for visibility and multiply "light
// strength" with this objects albedo (color) // strength" with this objects albedo (color)
return this->objectColor;
Color color = Color(0, 0, 0);
for (auto light : scene.lights()) {
color += this->objectColor * light->illuminate(scene, ray).color;
}
return color;
} }