c++graphicsraytracing

Raytracer Reflection: object is dimmed, not reflective


i'm implementing a simple raytracer and now I'm trying to implement reflections. but the object are just dimmed, there is no reflection. that's how it looks: my scene Here is my code for the reflection in my trace method:

Color Renderer::trace(Ray const& ray, float depth) {
    // Maximaler Rekursionstiefe für Reflexionen
    if (depth > max_depth) {
        return background_color;
    }

    float closest_distance = std::numeric_limits<float>::max();
    std::shared_ptr<Shape> closest_shape = nullptr;
    Ray transformed_ray = ray;
    HitPoint hit;
    HitPoint closest_object_hitpoint;
    closest_object_hitpoint.distance = std::numeric_limits<float>::max();

    for (auto const& shape : scene_.shape_container) {
        transformed_ray = transform_ray(shape->get_world_transformation_inv(), ray); //ray wird transformiert

        hit = shape->intersect(transformed_ray); //hit mit transformed ray

        if (hit.intersection) {
            float distance = glm::length(hit.intersection_point - scene_.camera_container[0]->origin);
            if (hit.distance < closest_object_hitpoint.distance) {
//                closest_distance = distance;
                closest_shape = shape;
                closest_object_hitpoint = hit;
            }
        }
    }

    if (closest_object_hitpoint.intersection) {
        Color local_color = shade(closest_object_hitpoint, closest_shape, closest_distance);

        // Reflexion
        if (closest_shape->get_Material()->reflectivity > 0.0f) {
            float reflection_factor = closest_shape->get_Material()->reflectivity; // Reflexionskoeffizient

            // Berechnung des reflektierten Rays
            glm::vec3 reflected_direction = glm::reflect(glm::normalize(ray.direction), glm::normalize(closest_object_hitpoint.normale));
            glm::vec3 reflected_location = closest_object_hitpoint.intersection_point;
            reflected_location = reflected_location + 0.01f * reflected_direction;

            Ray reflected_ray = {reflected_location, reflected_direction};

            Color reflected_color = trace(reflected_ray, depth + 1);

            //lokale Farbe wird mit reflektierter Farbe kombiniert
            local_color.r = (1.0f - reflection_factor) * local_color.r + reflection_factor * reflected_color.r;
            local_color.g = (1.0f - reflection_factor) * local_color.g + reflection_factor * reflected_color.g;
            local_color.b = (1.0f - reflection_factor) * local_color.b + reflection_factor * reflected_color.b;
        }

        return local_color;
        // return shade(closest_object_hitpoint, closest_shape, closest_distance);
    } else {
        return background_color;
    }
}

the normal-computation, intersection-methods and transformation-matrices are correct. where is the mistake?

i expect the reflection on the checkerboard pattern to reflect the sphere seamlessly. i once worked before i fixed the closest_shape stuff in the trace-method. but after that works correctly, the reflection looks wrong.


Solution

  • The problem is that I used the untransformed ray for the reflection, not the transformed one