问题四十二:怎么用ray tracing画任意圆环片段
42.1 数学推导
引入两个参数theta1、theta2
撞击点P和中心点O连线的向量OP与+X轴的夹角theta在[theta1, theta2]范围内时,才有可能撞击到圆环段。
当然,考虑到theta2可能大于360度,所以,加入另外一种情况:
theta + 2*π <=theta2
theta1的范围[0, 360]
theta2的范围[0,正无穷)
42.2 看C++代码实现
----------------------------------------------tori_part.h ------------------------------------------
tori_part.h
#ifndef TORI_PART_H
#define TORI_PART_H#include "hitable.h"
#include "material.h"
#include "log.h"class tori_part : public hitable
{public:tori_part() {}tori_part(vec3 cen, float ra, float rb, material *m, float t1, float t2) {center = cen;radius_a = ra;radius_b = rb;ma = m;theta1 = t1*M_PI/180;theta2 = t2*M_PI/180;}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;
};#endif // TORI_PART_H
----------------------------------------------tori_part.cpp ------------------------------------------
tori_part.cpp
#include "tori_part.h"#include <iostream>
using namespace std;bool tori_part::hit(const ray& r, float t_min, float t_max, hit_record& rec) const {
#if TORI_PART_LOG == 1std::cout << "-------------tori_part::hit---1-------------" << endl;
#endif // TORI_PART_LOGvec3 oc = r.origin() - center;float A = dot(oc, oc);float B = 2*dot(oc, r.direction());float C = dot(r.direction(), r.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*r.direction().z()*r.direction().z();float a1 = 2*A*B - 2*Rr_square_p*B + 8*R_square*oc.z()*r.direction().z();float a0 = A*A - 2*Rr_square_p*A + 4*R_square*oc.z()*oc.z() + 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 = r.point_at_parameter(rec.t);vec3 pc = rec.p - center;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.y() < center.y()) {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(r.direction(), rec.normal) > 0) {rec.normal = - rec.normal;}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(vec3(0, 3.2,0), 3.2, 0.2, new lambertian(vec3(0.0, 1.0, 0.0)), 30, 60);
hitable *world = newhitable_list(list,1);
vec3 lookfrom(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);
图片输出结果如下:
theta1和theta2分别为30, 60
theta1 和theta2分别为30,150
theta1 和theta2分别为30,210
theta1 和theta2分别为30, 270
theta1 和theta2分别为30, 360
theta1 和theta2分别为30, 380
theta1 和theta2分别为30, 390
搞个组合图:
hitable *list[7];list[0] = new tori(vec3(0, 3.2, 0), 3.2, 0.2, new lambertian(vec3(0.0, 1.0, 0.0)));list[1] = new tori(vec3(0, 4.8, 0), 1.2, 0.2, new lambertian(vec3(1.0, 0.0, 0.0)));list[2] = new quadratic_cylinder_all(vec3(0, 3.2, 0), 0.2, 0.2, 0.20001, 2.9,new lambertian(vec3(1.0, 0.0, 0.0)), vec3(0, 1, 0), 0);list[3] = new quadratic_cylinder_all(vec3(0, 3.2, 0), 0.2, 0.2, 0.20001, 2.9,new lambertian(vec3(1.0, 0.0, 0.0)), vec3(1, 0, 0), 0);list[4] = new quadratic_cylinder_all(vec3(0, 4.8, 0), 0.2, 0.2, 0.20001, 1.3,new lambertian(vec3(1.0, 0.0, 0.0)), vec3(1, 0, 0), 0);list[5] = new tori_part(vec3(-1.6, 3.2, 0), 1.6, 0.2, new lambertian(vec3(1.0, 0.0, 0.0)), 240, 360);list[6] = new tori_part(vec3(1.6, 3.2, 0), 1.6, 0.2, new lambertian(vec3(1.0, 0.0, 0.0)), 180, 300);hitable *world = new hitable_list(list,7);vec3 lookfrom(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);
输出结果如下:(如上位置、长度参数有微调,原参数记不得了)
(by the way, 咱不是开过水果店么,咱店的名字叫做“果园里”,如下图形是本人当时为店名设计的logo)
哦也~看张大图:
等等,图片中心那四段多余的圆弧是什么鬼?
本来高高兴兴,为什么要出现这种事,蓝瘦,香菇
下一章节来解这个问题~~~~~
问题四十二:怎么用ray tracing画任意圆环片段相关推荐
- 问题四十一:怎么用ray tracing画任意圆柱面(generalized cylinder)
我们之前在"35.2"章节中画过椭圆柱面: 我们还在"36.4"章节中画过圆柱面的Inverse Mapping图: 但是,这些柱面都是:底面与ZOX平面平行, ...
- 问题三十四:怎么用ray tracing画任意长方体(generalized box)
34.1 思路分析 这个内容书上没有,但是觉得实际应用中的长方体的位置应该是任意的(表面法向量不一定平行坐标轴). 怎么画? 1,光线撞击到长方体 2,撞击点到光线起点的距离 3,撞击点的法向量 怎么 ...
- 问题五十:怎么用ray tracing画blobs
这一节,画这个: 参考文献: Blinn, J.F., A generalization of algebraic surfacedrawing. ACM Trans. Graph. 1(3) , 2 ...
- 问题四十七:怎么用ray tracing画superellipsoid (2)
网络上有另外一种更为简单的superellipsoid的方程形式:(这种方程,更快些) 数学推导和"问题四十六"类似(应该是更为简单),此处不表. 47.1 看C++代码实现 -- ...
- 问题六十:怎么用ray tracing画回旋体(rotational sweeping / revolution)
60.1 概述 回旋体,大概是长这个样子: 回旋体是指曲线(称为"基本曲线")围绕y轴转一圈得到的图形. (基本曲线是由多段b-spline曲线段连接而成) 这里先强调一下: 上图 ...
- maya批量命名插件_教你玩转MAYA的四十二精华造诣(第一期)
最近在整理文档时发现我收藏了一篇关于MAYA应用技巧的文章,突然有兴趣看了看,结果发现老版本MAYA中的某些内容很多已经无法应用于新版本.我又上网查了一下,结果发现网上好多帖子和我收藏的这篇内容基本一 ...
- OpenCV学习笔记(四十一)——再看基础数据结构core OpenCV学习笔记(四十二)——Mat数据操作之普通青年、文艺青年、暴力青年 OpenCV学习笔记(四十三)——存取像素值操作汇总co
OpenCV学习笔记(四十一)--再看基础数据结构core 记得我在OpenCV学习笔记(四)--新版本的数据结构core里面讲过新版本的数据结构了,可是我再看这部分的时候,我发现我当时实在是看得太马 ...
- 四十二、深入Java中的文件读取操作
@Author:Runsen @Date:2020/6/8 作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件.导致翘课严重,专业排名 ...
- JavaScript学习(四十二)—利用工厂模式创建对象以及工厂模式创建对象的不足
JavaScript学习(四十二)-利用工厂模式创建对象以及工厂模式创建对象的不足 一.利用工厂模式创建对象 工厂模式是JavaScript中的一种设计模式,它的作用是批量创建具有同种属性的对象. 格 ...
最新文章
- NAACL19笔记:自然语言处理应用的实用理解(多图解链接)
- Linux内存管理【转】
- C++读写txt文件方式以及基于opencv的Mat数据类型读写txt文件
- mysql 入门命令
- vba数组下标越界_VBA编程知识点(7)——数组基本知识
- 华大单片机HC32L136J8TA读取DS18B20温度(源码+时钟配置)
- 服务器运行jar包日志怎么清理,docker 启动jar包,并将日志文件进行挂载
- c 使用RTP协议发送视频数据
- backgroundworker 在执行dowork事件时该如何取消_澳航官宣!明年上半年起,机场所有售票处和服务台永久取消,订票改签等只能“自助”...
- spark记录(7)SparkCore的调优之数据倾斜调优
- CMD下的网络安全配置
- element表格多列排序_Python,Numpy,Pandas…数据科学家必备排序技巧
- Coverity 代码静态安全扫描工具 : 认识Coverity
- 在线matlab,亲测好用
- 智慧工厂3D物联网可视化建模管理系统
- VBA 下载图片到word
- 随心所遇,随遇而安。
- Firefox火狐浏览器优化加速about:config配置
- 使用 ROT13 算法加密解密数据
- 阿里云免费个人Docker镜像仓库搭建