【Ray Tracing in One Weekend】(ch8)Metal&Lambertian

本文介绍了如何在光线追踪程序中实现金属材质的镜面反射效果,并通过具体的代码示例展示了如何定义Metal类来实现这一功能。此外,还讨论了如何在主程序中使用不同的材质类型来渲染场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ch8:Metal

再回顾一下上一章中求撞击点颜色的Color()方法:

Vec3 Color(const Ray& r, Hitable *world)
{
    hit_record rec;
    if (world->hit(r, 0.0, FLT_MAX, rec))
    {
        Vec3 target = rec.p + rec.normal + RandomInUnitSphere();
        //递归,每次吸收50%的能量
        return 0.5f*Color(Ray(rec.p, target - rec.p), world);
    }
    else
    {
        //绘制背景
        Vec3 unit_direction = unit_vector(r.direction());
        float t = 0.5f*(unit_direction.y() + 1.0f);
        //(1-t)*白色+t*蓝色,结果是一个蓝白的渐变
        return (1.0f - t)*Vec3(1.0f, 1.0f, 1.0f) + t*Vec3(0.5f, 0.7f, 1.0f);
    }
}

这里我们给球体的是Diffuse材质,反射方向是随机的,反射率是0.5,即每次光线射入吸收该光线颜色(三个通道)的50%,所以球体会有灰色的趋向(灰色即RGB三值近似)。现实中的Diffuse材质的物体,都有其自身的颜色,其反射率等参数各不相同,为了方便,现在我们把材质也抽象为一个类:

#pragma once

#include "Hitable.h"

//通过入射光线,计算反射光线
Vec3 Reflect(const Vec3& v, const Vec3& n)
{
    return v - 2 * dot(v, n)*n;
}

//生成随机方向的标准向量
Vec3 RandomInUnitSphere()
{
    Vec3 p;
    do {
        p = 2.0f * Vec3((rand() % 100 / float(100)), (rand() % 100 / float(100)), (rand() % 100 / float(100))) - Vec3(1.0f, 1.0f, 1.0f);
    } while (dot(p, p) >= 1.0f);

    return p;
}

//抽象出的材质类
class Material {
public:
    virtual bool Scatter(const Ray& r_in, const hit_record& rec, Vec3& attenuation, Ray& scattered) const = 0;
};

//漫反射材质
class Lambertian : public Material {
public:
    Lambertian(const Vec3& a):albedo(a){}
    virtual bool Scatter(const Ray& r_in, const hit_record& rec, Vec3& attenuation, Ray& scattered) const {
        Vec3 target = rec.p + rec.normal + RandomInUnitSphere();
        scattered = Ray(rec.p, target - rec.p);
        attenuation = albedo;
        return true;
    }
    Vec3 albedo;
};

//镜面反射材质
class Metal : public Material {
public:
    Metal(const Vec3& a, float f) : albedo(a) { if (f < 1) fuzz = f; else fuzz = 1; }

    virtual bool Scatter(const Ray& r_in, const hit_record& rec, Vec3& attenuation, Ray& scattered) const
    {
        Vec3 reflected = Reflect(unit_vector(r_in.direction()), rec.normal);
        scattered = Ray(rec.p, reflected + fuzz*RandomInUnitSphere());
        attenuation = albedo;
        return (dot(scattered.direction(), rec.normal) > 0);
    }

    Vec3 albedo;
    float fuzz;
};

将求反射光线的部分放到了材质类的Scatter()方法里,每个材质可以自己定义其反射光线。

同时应注意到我们添加了新的材质类型:Metal,用来表现发生镜面反射的材质。

同时修改Main方法为:

Vec3 Color(const Ray& r, Hitable *world, int depth)
{
    hit_record rec;
    if (world->hit(r, 0.001f, FLT_MAX, rec))
    {
        Ray scattered;
        Vec3 attenuation;
        if (depth < 50 && rec.mat_ptr->Scatter(r, rec, attenuation, scattered)) {
            return attenuation*Color(scattered, world, depth + 1);
        }
        else {
            return Vec3(0.0f, 0.0f, 0.0f);
        }
    }
    else
    {
        //绘制背景
        Vec3 unit_direction = unit_vector(r.direction());
        float t = 0.5f*(unit_direction.y() + 1.0f);
        //(1-t)*白色+t*蓝色,结果是一个蓝白的渐变
        return (1.0f - t)*Vec3(1.0f, 1.0f, 1.0f) + t*Vec3(0.5f, 0.7f, 1.0f);
    }
}

int main()
{
    ofstream outfile;
    outfile.open("ch8Image_fuzz.ppm");

    int nx = 200;
    int ny = 100;
    //采样次数
    int ns = 100;
    outfile << "P3\n" << nx << " " << ny << "\n255\n";

    Hitable *list[4];
    list[0] = new Sphere(Vec3(0.0f, 0.0f, -1.0f), 0.5f, new Lambertian(Vec3(0.8f, 0.3f, 0.3f)));
    list[1] = new Sphere(Vec3(0.0f, -100.5f, -1.0f), 100.0f, new Lambertian(Vec3(0.8f, 0.8f, 0.0f)));
    list[2] = new Sphere(Vec3(1.0f, 0.0f, -1.0f), 0.5f, new Metal(Vec3(0.8f, 0.6f, 0.2f),0.3f));
    list[3] = new Sphere(Vec3(-1.0f, 0.0f, -1.0f), 0.5f, new Metal(Vec3(0.8f, 0.8f, 0.8f),1.0f));
    Hitable *world = new HitableList(list, 4);

    Camera cam;

    //随机数引擎
    default_random_engine reng;
    uniform_real_distribution<float> uni_dist(0.0f, 1.0f);

    for (int j = ny - 1; j >= 0; j--)
    {
        for (int i = 0; i < nx; i++)
        {
            Vec3 col(0.0f, 0.0f, 0.0f);
            //每个区域采样ns次
            for (int s = 0; s < ns; s++)
            {
                float u = float(i + uni_dist(reng)) / float(nx);
                float v = float(j + uni_dist(reng)) / float(ny);
                Ray r = cam.getRay(u,v);
                //Vec3 p = r.point_at_parameter(2.0);
                //将本区域((u,v)到(u+1,v+1))的颜色值累加
                col += Color(r, world, 0);
            }
            //获得区域的颜色均值
            col /= float(ns);
            //gamma矫正
            col = Vec3(sqrt(col[0]), sqrt(col[1]), sqrt(col[2]));
            int ir = int(255.99*col[0]);
            int ig = int(255.99*col[1]);
            int ib = int(255.99*col[2]);
            outfile << ir << " " << ig << " " << ib << "\n";
        }
    }
    outfile.close();
    return 0;
}

渲染结果如图:

这里写图片描述

同样的,我也作死渲了一个高清版:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值