Skip to content

Commit 7358edf

Browse files
-Use raw material pointers for speed in hit_record
-Change CSG threshold and ray marching algorithm -Fix elongate bounding box -Remove offseting hit point for dielectrics
1 parent 6c91e6a commit 7358edf

16 files changed

+133
-153
lines changed

src/bvh_node.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ bool bvh_node::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, rand
3131
#endif
3232
}
3333

34-
// bool bvh_node::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, random_gen& rng) {
35-
// if (!box.hit(r, t_min, t_max, rng))
36-
// return false;
37-
//
38-
// bool hit_left = left->hit(r, t_min, t_max, rec, rng);
39-
// bool hit_right = right->hit(r, t_min, hit_left ? rec.t : t_max, rec, rng);
40-
//
41-
// return hit_left || hit_right;
42-
// }
43-
4434
inline bool box_compare(const std::shared_ptr<hitable> a, const std::shared_ptr<hitable> b, int axis) {
4535
aabb box_a;
4636
aabb box_b;

src/cone.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ bool cone::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, random_g
129129
// rec.bump_normal = -rec.bump_normal;
130130
// }
131131

132-
rec.mat_ptr = mat_ptr;
132+
rec.mat_ptr = mat_ptr.get();
133133
return(true);
134134
}
135135
// if((t_cyl < temp2 || !second_is_hit) && t_cyl > t_min && t_cyl < t_max && radHit2 <= radius * radius && base_is_hit) {
@@ -143,7 +143,7 @@ bool cone::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, random_g
143143
rec.p = p;
144144
rec.normal = vec3(0,-1,0);
145145
rec.t = t_cyl;
146-
rec.mat_ptr = mat_ptr;
146+
rec.mat_ptr = mat_ptr.get();
147147
rec.u = u;
148148
rec.v = v;
149149
rec.dpdu = vec3(1, 0, 0);
@@ -187,7 +187,7 @@ bool cone::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, random_g
187187
// rec.normal = -rec.normal;
188188
// rec.bump_normal = -rec.bump_normal;
189189
// }
190-
rec.mat_ptr = mat_ptr;
190+
rec.mat_ptr = mat_ptr.get();
191191
return(true);
192192
}
193193
return(false);

src/constant.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ bool constant_medium::hit(const ray& r, Float t_min, Float t_max, hit_record& re
5050
rec.t = rec1.t + hit_distance / r.direction().length();
5151
rec.p = r.point_at_parameter(rec.t);
5252
rec.normal = vec3(1,0,0);
53-
rec.mat_ptr = phase_function;
53+
rec.mat_ptr = phase_function.get();
5454
return(true);
5555
}
5656
}

src/csg.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#include "csg.h"
22

33
bool csg::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, random_gen& rng) {
4-
Float threshold = 10e-6;
4+
Float threshold = 0.001;
5+
56
Float delta = 10e-5 * max_dist/100;
67
Float t = 0;
78
bool first = true;
89
vec3 dir = unit_vector(r.direction());
910
Float max_t = t_max * r.direction().length();
11+
1012

1113
while (t < max_t) {
1214
Float minDistance = INFINITY;
@@ -16,16 +18,17 @@ bool csg::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, random_ge
1618
float d = std::fabs(shapes->getDistance(from));
1719

1820
//Need to deal with refraction, often initial distance is too close to surface, so we offset
19-
if(first && t < threshold) {
20-
t += 100 * threshold; //Hard coded threshold, not great
21+
if(first && d < threshold) {
22+
t += 0.01; //Hard coded offset, not great
2123
first = false;
2224
continue;
2325
}
2426

2527
if (d < minDistance) {
2628
minDistance = d;
2729
}
28-
if (minDistance <= threshold * t) {
30+
first = false;
31+
if (minDistance <= threshold) {
2932
Float tval = t / r.direction().length();
3033
if(tval > t_min && tval < t_max) {
3134
rec.normal = vec3(
@@ -44,7 +47,7 @@ bool csg::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, random_ge
4447
rec.v = 0.5;
4548
rec.dpdu = 0.5;
4649
rec.dpdv = 0.5;
47-
rec.mat_ptr = mat_ptr;
50+
rec.mat_ptr = mat_ptr.get();
4851
rec.has_bump = false;
4952
rec.bump_normal = rec.normal;
5053
return(true);

src/csg.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,6 @@ class csg_elongate : public ImplicitShape {
354354
}
355355
virtual bool bbox(Float t0, Float t1, aabb& box) const {
356356
shape->bbox(t0,t1,box);
357-
box.bounds[0] += center;
358-
box.bounds[1] += center;
359357
box = Expand(box, elongate);
360358
return(true);
361359
}
@@ -377,8 +375,6 @@ class csg_elongate_robust : public ImplicitShape {
377375
}
378376
virtual bool bbox(Float t0, Float t1, aabb& box) const {
379377
shape->bbox(t0,t1,box);
380-
box.bounds[0] += center;
381-
box.bounds[1] += center;
382378
box = Expand(box, elongate);
383379
return(true);
384380
}

src/curve.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ bool curve::recursiveIntersect(const ray& r, Float tmin, Float tmax, hit_record&
306306

307307
rec.u = u;
308308
rec.v = v;
309-
rec.mat_ptr = mat_ptr;
309+
rec.mat_ptr = mat_ptr.get();
310310
rec.has_bump = false;
311311
rec.p = r.point_at_parameter(rec.t) + rec.normal * hitWidth * 0.5 * offset_scale;
312312
return(true);

src/cylinder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ bool cylinder::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, rand
112112
rec.bump_normal *= dot(temppoint, dir) > 0 ? -1 : 1;
113113

114114
}
115-
rec.mat_ptr = mat_ptr;
115+
rec.mat_ptr = mat_ptr.get();
116116
return(true);
117117
}
118118
Float t_cyl = -(r.origin().y()-length/2) / r.direction().y();
@@ -139,7 +139,7 @@ bool cylinder::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, rand
139139
rec.p = p;
140140
rec.normal = vec3(0,1,0);
141141
rec.t = t_cyl;
142-
rec.mat_ptr = mat_ptr;
142+
rec.mat_ptr = mat_ptr.get();
143143
rec.u = u;
144144
rec.v = v;
145145
rec.dpdu = vec3(1, 0, 0);
@@ -174,7 +174,7 @@ bool cylinder::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, rand
174174
rec.p = p;
175175
rec.normal = vec3(0,-1,0);
176176
rec.t = t_cyl2;
177-
rec.mat_ptr = mat_ptr;
177+
rec.mat_ptr = mat_ptr.get();
178178
rec.u = u;
179179
rec.v = v;
180180
rec.dpdu = vec3(1, 0, 0);
@@ -214,7 +214,7 @@ bool cylinder::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, rand
214214
rec.bump_normal.make_unit_vector();
215215
}
216216

217-
rec.mat_ptr = mat_ptr;
217+
rec.mat_ptr = mat_ptr.get();
218218
return(true);
219219
}
220220
return(false);

src/disk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool disk::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, random_g
5959
rec.p = p;
6060
rec.normal = n;
6161
rec.t = t;
62-
rec.mat_ptr = mat_ptr;
62+
rec.mat_ptr = mat_ptr.get();
6363
rec.u = u;
6464
rec.v = v;
6565

src/ellipsoid.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ class ellipsoid: public hitable {
1414
inv_axes = vec3(1.0f/axes.x(), 1.0f/axes.y(), 1.0f/axes.z());
1515
largest_proj_axis = axes.x() * axes.y() * axes.z() / ffmin(axes.x(), ffmin(axes.y(), axes.z()));
1616
};
17-
~ellipsoid() {
18-
// delete mat_ptr;
19-
// delete alpha_mask;
20-
}
2117
virtual bool hit(const ray& r, Float tmin, Float tmax, hit_record& rec, random_gen& rng);
2218
virtual bool bounding_box(Float t0, Float t1, aabb& box) const;
2319
virtual Float pdf_value(const vec3& o, const vec3& v, random_gen& rng);
@@ -51,7 +47,7 @@ bool ellipsoid::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, ran
5147
Float v;
5248
if(temp1 < t_max && temp1 > t_min) {
5349
vec3 p1 = scaled_ray.point_at_parameter(temp1) ;
54-
p1 *= 1/p1.length() * axes;;
50+
p1 *= 1/p1.length() * axes;
5551
vec3 normal = (p1 - center) * inv_axes;
5652
normal.make_unit_vector();
5753
get_sphere_uv(normal, u, v);
@@ -61,7 +57,7 @@ bool ellipsoid::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, ran
6157
}
6258
if(temp2 < t_max && temp2 > t_min) {
6359
vec3 p2 = scaled_ray.point_at_parameter(temp2) ;
64-
p2 *= 1/p2.length() * axes;;
60+
p2 *= 1/p2.length() * axes;
6561
vec3 normal = (p2 - center) * inv_axes;
6662
normal.make_unit_vector();
6763
get_sphere_uv(normal, u, v);
@@ -77,7 +73,7 @@ bool ellipsoid::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, ran
7773
rec.t = temp1;
7874
rec.p = scaled_ray.point_at_parameter(rec.t) ;
7975
rec.normal = (rec.p - center);
80-
rec.mat_ptr = mat_ptr;
76+
rec.mat_ptr = mat_ptr.get();
8177
vec3 trans_normal = rec.normal * inv_axes;
8278
trans_normal.make_unit_vector();
8379

@@ -109,7 +105,7 @@ bool ellipsoid::hit(const ray& r, Float t_min, Float t_max, hit_record& rec, ran
109105
rec.t = temp2;
110106
rec.p = scaled_ray.point_at_parameter(rec.t) ;
111107
rec.normal = (rec.p - center);
112-
rec.mat_ptr = mat_ptr;
108+
rec.mat_ptr = mat_ptr.get();
113109
vec3 trans_normal = rec.normal * inv_axes;
114110
trans_normal.make_unit_vector();
115111

src/hitable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct hit_record {
2525
vec3 dpdu, dpdv;
2626
vec3 bump_normal;
2727
bool has_bump;
28-
std::shared_ptr<material> mat_ptr;
28+
material* mat_ptr;
2929
};
3030

3131
class hitable {

0 commit comments

Comments
 (0)