【Ray Tracing in One Weekend】(ch5)法向量的可视化与多个球的出现

Chapter 5: Surface normals and multiple objects.

这章耽搁了好几天,前面的内容都快忘了,首先先来回忆一下前几章的内容吧。

  • 首先,在最一开始,我们使用c++输出了第一张ppm格式的图片,直接用枚举暴力的写出了每一像素的颜色。
  • 接着在ch2中,我们写出了向量类 Vec3.h,可以将ch1中的RGB值替换为一个颜色向量。
  • 在ch3中,我们写出了射线类 Ray.h,将射线在三维空间中表示了出来。我们模仿一个摄像头或者说眼睛看到颜色的过程,从一点出发,发出射线,让这些射线去“撞”空间中的其他物体,并求得撞击点的颜色,便是该点应显示的颜色。即共两步,一是求撞击点,二是求该点颜色。我们用一个Color方法模拟求颜色这一过程,射线的方向不同,Color返回的颜色值也不同,实际上颜色值RGB是根据射线方向向量的XYZ映射的,这样的结果就好像在摄像机前摆了一块颜色均匀变化的幕布。
  • 在ch4中,我们演算了球体的方程,并假设在幕布前有着一个球体,通过求根公式,可以求出射线是否与球体相交,若相交的话,Color方法将返回红色,代表着射线“撞”到了球体。
  • 在ch5中,也就是在本章中,我们不想让球体仅仅是红色了,我们也想让它可以渐变,那么就可以用到它的法向量,让法向量的XYZ值映射到RGB值即可!同时,我们也想多加几个球体出来,只有一个太孤单啦!

表面法向,是一种向量,垂直于表面,且按照惯例,指向外部。

对于一个球体来说,法向便是 hitpoint p减去球体中心C

这里写图片描述

修改 Main.cpp 中部分代码如下:

//注意与ch4不同,此时该函数的返回值以从bool变为float
float hit_sphere(const Vec3& center, float radius, const Ray& r)
{
    Vec3 oc = r.origin() - center;
    float a = dot(r.direction(), r.direction());
    float b = 2.0f*dot(oc, r.direction());
    float c = dot(oc, oc) - radius*radius;
    float discrimiant = b*b - 4.0f*a*c;
    if (discrimiant < 0.0f) 
    {
        return -1.0f;
    }
    else
    {
        return (-b - sqrt(discrimiant)) / (2.0f*a);
    }
}

Vec3 Color(const Ray& r)
{
    float t = hit_sphere(Vec3(0.0f, 0.0f, -1.0f), 0.5, r);
    if (t > 0.0f)
    {
        //法向量
        Vec3 N = unit_vector(r.point_at_parameter(t) - Vec3(0.0f, 0.0f, -1.0f));
        return 0.5f*Vec3(N.x() + 1.0f, N.y() + 1.0f, N.z() + 1.0f);
    }
    //绘制背景
    Vec3 unit_direction = unit_vector(r.direction());
    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);
}

运行结果如下:

这里写图片描述

如此一来,就把球体变为一个渐变色的球了,同时也将法向量可视化了出来。

现在创建一个名为“Hitable”的 abstract class,它是一切可让射线“撞”的物体的父类。

#include "Ray.h"

//撞击点处信息
struct hit_record
{
    //射线参数t
    float t;
    //撞击点位置向量p
    Vec3 p;
    //撞击点处法向量N
    Vec3 normal;
};

//所有能被射线撞击的物体的父类
class Hitable
{
public:
    //hit()在此被声明为虚函数,则hitable为抽象类。抽象类的子类中必须实现其虚函数
    virtual bool hit(const Ray& r, float t_min, float t_max, hit_record& rec) const = 0;
};

接着根据这个父类,写出子类 Sphere.h

#pragma once  
#include "Hitable.h"

class Sphere : public Hitable {
public:
    Sphere() {}
    //此处为使用初始化列表的构造函数来初始化成员变量
    Sphere(Vec3 cen, float r) : center(cen), radius(r) {};
    virtual bool hit(const Ray& r, float tmin, float tmax, hit_record& rec) const;
    Vec3 center;
    float radius;
};

bool Sphere::hit(const Ray& r, float t_min, float t_max, hit_record& rec) const {
    Vec3 oc = r.origin() - center;
    float a = dot(r.direction(), r.direction());
    float b = 2.0f * dot(oc, r.direction());
    float c = dot(oc, oc) - radius*radius;
    float discriminant = (b*b - 4.0f*a*c);
    if (discriminant > 0) {
        float temp = (-b - sqrt(discriminant)) / (2.0f*a);
        if (temp < t_max && temp > t_min) {
            rec.t = temp;
            rec.p = r.point_at_parameter(rec.t);
            rec.normal = (rec.p - center) / radius;
            return true;
        }
        temp = (-b + sqrt(discriminant)) / (2.0f*a);
        if (temp < t_max && temp > t_min) {
            rec.t = temp;
            rec.p = r.point_at_parameter(rec.t);
            rec.normal = (rec.p - center) / radius;
            return true;
        }
    }
    return false;
}

接着是另一个子类,可撞击物体的列表 HitableList.h

#pragma once
#include "Hitable.h"

/*依次判断列表中所有物体是否被光线撞到,每次判断一个。
若有被撞到,则将撞点信息保存在hit_record结构体中。
我们可以看到rec是可能被写多次的,最终保存的值是后一次的值,
也就是真正有效的值是后一次的值,也就是离观测点最近的物体的有效撞点
(“有效撞点”:对于单个物体,会筛选出一个局部有效撞点;对于多个物体,从所有单个物体各自的局部有效撞点筛选出最终一个整体有效撞点)。
因为不管这条光线依次撞击了多少个物体产生多少个撞点,我们能看到的只是离我们最近的撞点
如果当前撞点在范围内,则将当前撞点的距离设置为范围的最大值。也就是后面只考虑比该撞点更近的撞点。
趋势是:找到的撞点是越来越近的,最终找到最近的撞点。*/
class HitableList : public Hitable
{
public:
    HitableList(){}
    HitableList(Hitable **l, int n) { list = l; list_size = n; }
    virtual bool hit(const Ray& r, float tmin, float tmax, hit_record& rec) const;
    Hitable **list;
    int list_size;
};

bool HitableList::hit(const Ray& r, float t_min, float t_max, hit_record& rec)const
{
    hit_record temp_rec;
    bool hit_anything = false;
    double closest_so_far = t_max;
    for (int i = 0; i < list_size; i++)
    {
        if (list[i]->hit(r, t_min, closest_so_far, temp_rec))
        {
            hit_anything = true;
            closest_so_far = temp_rec.t;
            rec = temp_rec;
        }
    }
    return hit_anything;
}

Main.cpp 如下:

#include <iostream>
#include <fstream>
#include "Sphere.h"
#include "HitableList.h"

using namespace std;

Vec3 Color(const Ray& r, Hitable *world)
{
    hit_record rec;
    if (world->hit(r, 0.0, FLT_MAX, rec))
    {
        return 0.5f*Vec3(rec.normal.x() + 1.0f, rec.normal.y() + 1.0f, rec.normal.z() + 1.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("ch5_2Image.ppm");

    int nx = 200;
    int ny = 100;
    outfile << "P3\n" << nx << " " << ny << "\n255\n";

    Vec3 lower_left_corner(-2.0f, -1.0f, -1.0f);
    Vec3 horizontal(4.0f, 0.0f, 0.0f);
    Vec3 vertical(0.0f, 2.0f, 0.0f);
    Vec3 origin(0.0f, 0.0f, 0.0f);

    Hitable *list[2];
    list[0] = new Sphere(Vec3(0.0f, 0.0f, -1.0f), 0.5f);
    list[1] = new Sphere(Vec3(0.0f, -100.5f, -1.0f), 100.0f);
    Hitable *world = new HitableList(list, 2);

    for (int j = ny - 1; j >= 0; j--)
    {
        for (int i = 0; i < nx; i++)
        {
            float u = float(i) / float(nx);
            float v = float(j) / float(ny);
            Ray r(origin, lower_left_corner + u*horizontal + v*vertical);

            Vec3 p = r.point_at_parameter(2.0);
            Vec3 col = Color(r,world);

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

结果如下图:

这里写图片描述

绿色的是第二个球哦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值