问题四十四:怎么用ray tracing画空间任意位置的圆环的任意片段

198 篇文章 12 订阅
195 篇文章 27 订阅

44.1 数学推导

同“41.1”

 

44.2 看C++代码实现

----------------------------------------------tori_part_all.h ------------------------------------------

tori_part_all.h

#ifndef TORI_PART_ALL_H
#define TORI_PART_ALL_H


#include "hitable.h"
#include "material.h"
#include "log.h"

class tori_part_all : public hitable
{
    public:
        tori_part_all() {}
        tori_part_all(vec3 cen, float ra, float rb, material *m, float t1, float t2, vec3 u, float an) {
             center = cen;
             radius_a = ra;
             radius_b = rb;
             ma = m;
             theta1 = t1*M_PI/180;
             theta2 = t2*M_PI/180;

             vector_u = u;

            get_vector_vw(vector_u, an, vector_v, vector_w);

        }
        virtual bool hit(const ray& r, float tmin, float tmax, hit_record& rec) const;
        vec3 center;
        float radius_a;
        float radius_b;
        material *ma;
        float theta1, theta2;

        vec3vector_u;

        vec3vector_v;

        vec3vector_w;

};

#endif // TORI_PART_ALL_H

----------------------------------------------tori_part_all.h ------------------------------------------

tori_part_all.h

#include "tori_part_all.h"

#include <iostream>
using namespace std;

bool tori_part_all::hit(const ray& r, float t_min, float t_max, hit_record& rec) const {
#if TORI_PART_ALL_LOG == 1
        std::cout << "-------------tori_part_all::hit---1-------------" << endl;
#endif // TORI_PART_ALL_LOG

        vec3direction = vector_trans(r.direction(), vector_v, vector_u, vector_w);

        vec3origin = vector_trans(r.origin(), vector_v, vector_u, vector_w);

        vec3center_vuw = vector_trans(center, vector_v, vector_u, vector_w);

        vec3 oc = origin - center_vuw;
        float A = dot(oc, oc);
        float B = 2*dot(oc, direction);
        float C = dot(direction, direction);
        float Rr_square_p = radius_a*radius_a +radius_b*radius_b;
        float Rr_square_s_square = (radius_a*radius_a - radius_b*radius_b) * (radius_a*radius_a - radius_b*radius_b);
        float R_square = radius_a*radius_a;
        float a4 = C*C;
        float a3 = 2*B*C;
        float a2 = B*B + 2*A*C - 2*Rr_square_p*C + 4*R_square*direction.y()*direction.y();
        float a1 = 2*A*B - 2*Rr_square_p*B + 8*R_square*oc.y()*direction.y();
        float a0 = A*A - 2*Rr_square_p*A + 4*R_square*oc.y()*oc.y() + Rr_square_s_square;

        float roots[5];
        roots_quartic_equation2(a4, a3, a2, a1, a0, roots);

        float temp;
        if (roots[0] > 0.0001) {
            for (int i=1; i<int(roots[0]); i++) {
                for (int j=i+1; j<int(roots[0])+1; j++) {
                    if (roots[i] > roots[j]) {
                        temp = roots[i];
                        roots[i] = roots[j];
                        roots[j] = temp;
                    }
                }
            }
            for (int k=1; k<int(roots[0])+1; k++) {
                if (roots[k] < t_max && roots[k] > t_min) {
                    rec.t = roots[k];

                    rec.p = vector_trans(r.point_at_parameter(rec.t), vector_v, vector_u,vector_w); //将撞击点坐标转换到vuw坐标系

                    vec3 pc = rec.p - center_vuw;
                    vec3 x = vec3(1.0, 0.0, 0.0);

                    float cos_theta = dot(pc, x) / (pc.length() * x.length());
                    float theta = acos(cos_theta);
                    if (rec.p.z() > center_vuw.z()) {
                        theta = 2*M_PI - theta;
                    }
                    if (((theta >= theta1) && (theta <= theta2)) || ((theta + 2*M_PI) <= theta2)) {
                        float nx = 4*pc.x()*pc.x()*pc.x() + 4*pc.x()*(pc.y()*pc.y()+pc.z()*pc.z()-(radius_a*radius_a+radius_b*radius_b));
                        float ny = 4*pc.y()*pc.y()*pc.y() + 4*pc.y()*(pc.x()*pc.x()+pc.z()*pc.z()+radius_a*radius_a-radius_b*radius_b);
                        float nz = 4*pc.z()*pc.z()*pc.z() + 4*pc.z()*(pc.x()*pc.x()+pc.y()*pc.y()-(radius_a*radius_a+radius_b*radius_b));
                        rec.normal = unit_vector(vec3(nx, ny, nz));
                        if(dot(direction, rec.normal) > 0) {
                            rec.normal = - rec.normal;
                        }

                        rec.normal =vector_trans_back(rec.normal, vector_v, vector_u, vector_w); //最终的撞击点的法向量需要从vuw坐标系转回到xyz坐标系。

                        rec.mat_ptr = ma;
                        rec.u = -1.0;
                        rec.v = -1.0;
                        delete [] roots;
                        return true;
                    }
                }
            }
        }
        delete [] roots;
        return false;
}

 

----------------------------------------------main.cpp ------------------------------------------

main.cpp

 

        hitable *list[1];

        list[0] = new tori_part_all(vec3(0.0,2.0, 0), 2.0, 0.2, new lambertian(vec3(0.0, 1.0, 0.0)), 90, 360, vec3(1, 1, 1), 0);

 

        hitable *world = newhitable_list(list,1);

 

输出结果如下:

90, 360, vec3(0, 1, 0), 0


90, 360, vec3(1, 0, 0), 0


90, 360, vec3(0, 0, 1), 0


90, 360, vec3(1, 0, 1), 0


90, 360, vec3(1, 1, 1), 0


90, 360, vec3(1, 1, 1), 90


90, 360, vec3(1, 1, 1), 180


90, 360, vec3(1, 1, 1), 270


 

看张组合图:

        hitable *list[4];

        list[0] = new tori_part_all(vec3(0.0, 8.25, -2.121), 3, 0.2, new lambertian(vec3(0.0, 1.0, 0.0)), 30, 360, vec3(0, 1, 1), 0);
        list[1] = new tori_part_all(vec3(0.0, 5.129, 0), 1.4, 0.2, new lambertian(vec3(1.0, 0.0, 0.0)), 30, 360, vec3(1, 0, 1), 0);
        list[2] = new tori_part_all(vec3(0.0, 2.129, 0), 2.2, 0.2, new lambertian(vec3(1.0, 1.0, 0.0)), 30, 360, vec3(1, 0, 0.5), 0);
        list[3] = new quadratic_cylinder_all(vec3(0, 0.329, 0), 0.4, 0.4, 0.40001, 4,
                                             new lambertian(vec3(0.0, 1.0, 1.0)), vec3(1, 0, 0.25), 0);

        hitable *world = new hitable_list(list,4);

        vec3 lookfrom(0.0, 3, 5);
        vec3 lookat(0, 3, 0);
        float dist_to_focus = (lookfrom - lookat).length();
        float aperture = 0.0;
        camera cam(lookfrom, lookat, vec3(0,1,0), 80, float(nx)/float(ny), aperture, 0.7*dist_to_focus);


输出图片如下:



 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值