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 == 1
std::cout << "-------------tori_part::hit---1-------------" << endl;
#endif // TORI_PART_LOG
vec3 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)
哦也~看张大图:
等等,图片中心那四段多余的圆弧是什么鬼?
本来高高兴兴,为什么要出现这种事,蓝瘦,香菇
下一章节来解这个问题~~~~~