光线追踪器反射:对象变暗,不反射

问题描述 投票:0回答:1

我正在实现一个简单的光线追踪器,现在我正在尝试实现反射。但物体只是变暗,没有反射。这就是它的样子: 我的场景 这是我的跟踪方法中的反射代码:

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;
    }
}

法线计算、交集方法和变换矩阵是正确的。 哪里错了?

我希望棋盘图案上的反射能够无缝地反映球体。 在修复跟踪方法中最接近的形状之前,我曾经工作过。但工作正常后,反射看起来是错误的。

c++ graphics raytracing
1个回答
0
投票

问题是我使用未变换的光线进行反射,而不是变换后的光线

© www.soinside.com 2019 - 2024. All rights reserved.