@@ -51,20 +51,24 @@ namespace cg::renderer
5151 };
5252
5353 template <typename VB>
54- inline triangle<VB>::triangle(
55- const VB& vertex_a, const VB& vertex_b, const VB& vertex_c)
54+ inline triangle<VB>::triangle(const VB& vertex_a, const VB& vertex_b,
55+ const VB& vertex_c)
5656 {
5757 a = float3{vertex_a.x , vertex_a.y , vertex_a.z };
5858 b = float3{vertex_b.x , vertex_b.y , vertex_b.z };
5959 c = float3{vertex_c.x , vertex_c.y , vertex_c.z };
60+
6061 ba = b - a;
6162 ca = c - a;
63+
6264 na = float3{vertex_a.nx , vertex_a.ny , vertex_a.nz };
6365 nb = float3{vertex_b.nx , vertex_b.ny , vertex_b.nz };
6466 nc = float3{vertex_c.nx , vertex_c.ny , vertex_c.nz };
67+
6568 ambient = float3{vertex_a.ambient_r , vertex_a.ambient_g , vertex_a.ambient_b };
6669 diffuse = float3{vertex_a.diffuse_r , vertex_a.diffuse_g , vertex_a.diffuse_b };
67- emissive = float3{vertex_a.emissive_r , vertex_a.emissive_g , vertex_a.emissive_b };
70+ emissive =
71+ float3{vertex_a.emissive_r , vertex_a.emissive_g , vertex_a.emissive_b };
6872 }
6973
7074 template <typename VB>
@@ -99,21 +103,29 @@ namespace cg::renderer
99103 void clear_render_target (const RT& in_clear_value);
100104 void set_viewport (size_t in_width, size_t in_height);
101105
102- void set_vertex_buffers (std::vector<std::shared_ptr<cg::resource<VB>>> in_vertex_buffers);
103- void set_index_buffers (std::vector<std::shared_ptr<cg::resource<unsigned int >>> in_index_buffers);
106+ void set_vertex_buffers (
107+ std::vector<std::shared_ptr<cg::resource<VB>>> in_vertex_buffers);
108+ void
109+ set_index_buffers (std::vector<std::shared_ptr<cg::resource<unsigned int >>>
110+ in_index_buffers);
104111 void build_acceleration_structure ();
105112 std::vector<aabb<VB>> acceleration_structures;
106113
107- void ray_generation (float3 position, float3 direction, float3 right, float3 up, size_t depth, size_t accumulation_num);
114+ void ray_generation (float3 position, float3 direction, float3 right,
115+ float3 up, size_t depth, size_t accumulation_num);
108116
109- payload trace_ray (const ray& ray, size_t depth, float max_t = 1000 .f, float min_t = 0 .001f ) const ;
110- payload intersection_shader (const triangle<VB>& triangle, const ray& ray) const ;
117+ payload trace_ray (const ray& ray, size_t depth, float max_t = 1000 .f,
118+ float min_t = 0 .001f ) const ;
119+ payload intersection_shader (const triangle<VB>& triangle,
120+ const ray& ray) const ;
111121
112122 std::function<payload(const ray& ray)> miss_shader = nullptr ;
113- std::function<payload(const ray& ray, payload& payload, const triangle<VB>& triangle, size_t depth)>
123+ std::function<payload(const ray& ray, payload& payload,
124+ const triangle<VB>& triangle, size_t depth)>
114125 closest_hit_shader = nullptr ;
115- std::function<payload(const ray& ray, payload& payload, const triangle<VB>& triangle)> any_hit_shader =
116- nullptr ;
126+ std::function<payload(const ray& ray, payload& payload,
127+ const triangle<VB>& triangle)>
128+ any_hit_shader = nullptr ;
117129
118130 float2 get_jitter (int frame_id);
119131
@@ -136,17 +148,16 @@ namespace cg::renderer
136148 }
137149
138150 template <typename VB, typename RT>
139- inline void raytracer<VB, RT>::set_viewport(size_t in_width,
140- size_t in_height)
151+ inline void raytracer<VB, RT>::set_viewport(size_t in_width, size_t in_height)
141152 {
142- width = in_width;
143153 height = in_height;
154+ width = in_width;
155+
144156 history = std::make_shared<cg::resource<float3>>(width, height);
145157 }
146158
147159 template <typename VB, typename RT>
148- inline void raytracer<VB, RT>::clear_render_target(
149- const RT& in_clear_value)
160+ inline void raytracer<VB, RT>::clear_render_target(const RT& in_clear_value)
150161 {
151162 for (size_t i = 0 ; i < render_target->count (); i++) {
152163 render_target->item (i) = in_clear_value;
@@ -155,42 +166,43 @@ namespace cg::renderer
155166 }
156167
157168 template <typename VB, typename RT>
158- inline void raytracer<VB, RT>::set_vertex_buffers(std::vector<std::shared_ptr<cg::resource<VB>>> in_vertex_buffers)
169+ inline void raytracer<VB, RT>::set_vertex_buffers(
170+ std::vector<std::shared_ptr<cg::resource<VB>>> in_vertex_buffers)
159171 {
160172 vertex_buffers = in_vertex_buffers;
161173 }
162174
163175 template <typename VB, typename RT>
164- void raytracer<VB, RT>::set_index_buffers(std::vector<std::shared_ptr<cg::resource<unsigned int >>> in_index_buffers)
176+ void raytracer<VB, RT>::set_index_buffers(
177+ std::vector<std::shared_ptr<cg::resource<unsigned int >>> in_index_buffers)
165178 {
166179 index_buffers = in_index_buffers;
167180 }
168181
169182 template <typename VB, typename RT>
170183 inline void raytracer<VB, RT>::build_acceleration_structure()
171184 {
172- for (size_t i = 0 ; i < index_buffers.size (); i++)
173- {
174- auto & index_buffer = index_buffers[i];
175- auto & vertex_buffer = vertex_buffers[i];
176- size_t index = 0 ;
185+ for (size_t shape_id = 0 ; shape_id < index_buffers.size (); shape_id++) {
186+ auto & index_buffer = index_buffers[shape_id];
187+ auto & vertex_buffer = vertex_buffers[shape_id];
188+ size_t index_id = 0 ;
177189 aabb<VB> aabb;
178- while (index < index_buffer->count ())
179- {
190+ while (index_id < index_buffer->count ()) {
180191 triangle<VB> triangle (
181- vertex_buffer->item (index_buffer->item (index ++)),
182- vertex_buffer->item (index_buffer->item (index ++)),
183- vertex_buffer->item (index_buffer->item (index ++)));
184- aabb.push_back (triangle);
192+ vertex_buffer->item (index_buffer->item (index_id ++)),
193+ vertex_buffer->item (index_buffer->item (index_id ++)),
194+ vertex_buffer->item (index_buffer->item (index_id ++)));
195+ aabb.add_triangle (triangle);
185196 }
197+ acceleration_structures.push_back (aabb);
186198 }
187- acceleration_structures.push_back (aabb);
188199 }
189200
190201 template <typename VB, typename RT>
191- inline void raytracer<VB, RT>::ray_generation(
192- float3 position, float3 direction,
193- float3 right, float3 up, size_t depth, size_t accumulation_num)
202+ inline void raytracer<VB, RT>::ray_generation(float3 position, float3 direction,
203+ float3 right, float3 up,
204+ size_t depth,
205+ size_t accumulation_num)
194206 {
195207
196208 float frame_weight = 1 .f / static_cast <float >(accumulation_num);
@@ -220,25 +232,23 @@ namespace cg::renderer
220232 }
221233
222234 template <typename VB, typename RT>
223- inline payload raytracer<VB, RT>::trace_ray(
224- const ray& ray, size_t depth, float max_t , float min_t ) const
235+ inline payload raytracer<VB, RT>::trace_ray(const ray& ray, size_t depth,
236+ float max_t , float min_t ) const
225237 {
226- if (depth == 0 ) {
238+ if (depth == 0 )
227239 return miss_shader (ray);
228- }
229240 depth--;
241+
230242 payload closest_hit_payload{};
231243 closest_hit_payload.t = max_t ;
232244 const triangle<VB>* closest_triangle = nullptr ;
245+
233246 for (auto & aabb: acceleration_structures) {
234- if (!aabb.aabb_test (ray)) {
247+ if (!aabb.aabb_test (ray))
235248 continue ;
236- }
237- for (auto & triangle: aabb.get_triangles ())
238- {
249+ for (auto & triangle: aabb.get_triangles ()) {
239250 payload payload = intersection_shader (triangle, ray);
240- if (payload.t > min_t && payload.t < closest_hit_payload.t )
241- {
251+ if (payload.t > min_t && payload.t < closest_hit_payload.t ) {
242252 closest_hit_payload = payload;
243253 closest_triangle = ▵
244254 if (any_hit_shader)
@@ -257,26 +267,32 @@ namespace cg::renderer
257267 }
258268
259269 template <typename VB, typename RT>
260- inline payload raytracer<VB, RT>::intersection_shader(
261- const triangle<VB>& triangle, const ray& ray) const
270+ inline payload
271+ raytracer<VB, RT>::intersection_shader(const triangle<VB>& triangle,
272+ const ray& ray) const
262273 {
263274 payload payload{};
264275 payload.t = -1 .f ;
276+
265277 float3 pvec = cross (ray.direction , triangle.ca );
266278 float det = dot (triangle.ba , pvec);
267279 if (det > -1e-8 && det < 1e-8 )
268280 return payload;
281+
269282 float inv_det = 1 .f / det;
270283 float3 tvec = ray.position - triangle.a ;
271284 float u = dot (tvec, pvec) * inv_det;
272285 if (u < 0 .f || u > 1 .f )
273286 return payload;
274- float3 qvec = cross (tvec, triangle.ba );
275- float v = dot (ray.direction , qvec) * inv_det;
287+
288+ float3 gvec = cross (tvec, triangle.ba );
289+ float v = dot (ray.direction , gvec) * inv_det;
276290 if (v < 0 .f || u + v > 1 .f )
277291 return payload;
278- payload.t = dot (triangle.ca , qvec) * inv_det;
292+
293+ payload.t = dot (triangle.ca , gvec) * inv_det;
279294 payload.bary = float3{1 .f - u - v, u, v};
295+
280296 return payload;
281297 }
282298
@@ -287,33 +303,32 @@ namespace cg::renderer
287303
288304 constexpr int base_x = 2 ;
289305 int index = frame_id + 1 ;
290- float inv_base = 1 .f / base_x;
306+ float inv_base = 1 .f / static_cast < float >( base_x) ;
291307 float fraction = inv_base;
292308 while (index > 0 ) {
293- result.x += static_cast < float > (index % base_x) * fraction;
309+ result.x = (index % base_x) * fraction;
294310 index /= base_x;
295311 fraction *= inv_base;
296312 }
297313
298314 constexpr int base_y = 3 ;
299315 index = frame_id + 1 ;
300- inv_base = 1 .f / base_x ;
316+ inv_base = 1 .f / static_cast < float >(base_y) ;
301317 fraction = inv_base;
302318 while (index > 0 ) {
303- result.y += static_cast < float > (index % base_y) * fraction;
319+ result.y = (index % base_y) * fraction;
304320 index /= base_y;
305321 fraction *= inv_base;
306322 }
307323
308324 return result - 0 .5f ;
309325 }
310326
311-
312327 template <typename VB>
313328 inline void aabb<VB>::add_triangle(const triangle<VB> triangle)
314329 {
315330 if (triangles.empty ()) {
316- aabb_min = aabb_max = triangle.a ;
331+ aabb_max = aabb_min = triangle.a ;
317332 }
318333
319334 triangles.push_back (triangle);
@@ -336,12 +351,12 @@ namespace cg::renderer
336351 template <typename VB>
337352 inline bool aabb<VB>::aabb_test(const ray& ray) const
338353 {
339- float3 inv_ray_dir = 1 .f / ray.direction ;
340- float3 t0 = (aabb_max - ray.position ) * inv_ray_dir ;
341- float3 t1 = (aabb_min - ray.position ) * inv_ray_dir ;
342- float3 t_min = min (t0, t1);
343- float3 t_max = max (t0, t1);
344- return maxelem (t_min ) <= maxelem (t_max );
354+ float3 inv_dir = 1 .f / ray.direction ;
355+ float3 t0 = (aabb_max - ray.position ) * inv_dir ;
356+ float3 t1 = (aabb_min - ray.position ) * inv_dir ;
357+ float3 tmin = min (t0, t1);
358+ float3 tmax = max (t0, t1);
359+ return maxelem (tmin ) <= minelem (tmax );
345360 }
346361
347362}// namespace cg::renderer
0 commit comments