问题三十六:ray tracing中的Inverse Mapping(3)——圆盘Inverse Mapping

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

36.3 圆盘Inverse Mapping

36.3.1 数学推导

我们只考虑圆盘垂直于ZOX平面的情况。



36.3.2 看C++代码实现

----------------------------------------------elliptic_plane.cpp ------------------------------------------

elliptic_plane.cpp

#include "elliptic_plane.h"

#include <iostream>
using namespace std;

bool elliptic_plane::hit(const ray& r, float t_min, float t_max, hit_record& rec) const {
#if ELLIPTIC_PLANE_LOG == 1
        std::cout << "-------------elliptic_plane::hit----------------" << endl;
#endif // ELLIPTIC_PLANE_LOG
        if(dot(normal, r.direction()) == 0) {
            return false;
        }
        else {
            if (dot(normal, r.direction()) > 0) {
                rec.normal = - normal;
            }
            float temp = (dot(normal, center) - dot(normal, r.origin())) / dot(normal, r.direction());
            if (temp < t_max && temp > t_min) {
                rec.t = temp;
                rec.p = r.point_at_parameter(rec.t);
                if (((((rec.p.x()-center.x())*(rec.p.x()-center.x())) / (intercept_x*intercept_x))
                   + (((rec.p.y()-center.y())*(rec.p.y()-center.y())) / (intercept_y*intercept_y))
                   + (((rec.p.z()-center.z())*(rec.p.z()-center.z())) / (intercept_z*intercept_z))) <= 1) {
                    rec.mat_ptr = ma;
                    if ((intercept_x == intercept_y) && (intercept_y == intercept_z)) {
                        rec.v = sqrt(((rec.p.x()-center.x())*(rec.p.x()-center.x()) + (rec.p.y()-center.y())*(rec.p.y()-center.y())) / (intercept_x*intercept_x));
                        vec3 pc1 = vec3(rec.p.x()-center.x(), rec.p.y()-center.y(), 0);
                        vec3 vx = vec3(1, 0, 0);
                        float u = acos(dot(pc1, vx) / (pc1.length()*vx.length())) / (2*M_PI);
/*                        float u = acos((rec.p.x()-center.x()) / 
sqrt((rec.p.x()-center.x())*(rec.p.x()-center.x()) 
+ (rec.p.y()-center.y())*(rec.p.y()-center.y()))) / (2*M_PI);
*/
                        if ((rec.p.y()-center.y()) < 0) {
                            rec.u = 1 - u;
                        }
                        else {
                            rec.u = u;
                        }
                    }
                    else {
                        rec.u = -1.0;
                        rec.v = -1.0;
                    }
                    return true;
                }
            }
            return false;
        }
}

 

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

main.cpp

 

        hitable *list[2];
        list[0] = new sphere(vec3(0.0,-100,0), 100, new lambertian(vec3(0.8, 0.8, 0.0)));
        list[1] = new elliptic_plane(vec3(0.0, 2.5, 0), vec3(0, 0, 1), 2.5, 2.5, 2.5, new lambertian(vec3(0.8, 0.8, 0.0)));
        hitable *world = new hitable_list(list,2);

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

 

输出图片:



uv原图:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值