@@ -148,7 +148,7 @@ namespace cg::renderer
148
148
inline void raytracer<VB, RT>::clear_render_target(
149
149
const RT& in_clear_value)
150
150
{
151
- for (size_t i = 0 ; i < render_target->get_number_of_elements (); i++) {
151
+ for (size_t i = 0 ; i < render_target->count (); i++) {
152
152
render_target->item (i) = in_clear_value;
153
153
history->item (i) = float3{0 .f , 0 .f , 0 .f };
154
154
}
@@ -192,24 +192,28 @@ namespace cg::renderer
192
192
float3 position, float3 direction,
193
193
float3 right, float3 up, size_t depth, size_t accumulation_num)
194
194
{
195
- for (size_t frame_id = 0 ; frame_id < accumulation_num; frame_id++) {
196
- std::cout << " Tracing " << frame_id << " /" << accumulation_num << " frame\n " ;
195
+
196
+ float frame_weight = 1 .f / static_cast <float >(accumulation_num);
197
+
198
+ for (int frame_id = 0 ; frame_id < accumulation_num; frame_id++) {
199
+ std::cout << " Tracing frame #" << frame_id + 1 << " \n " ;
197
200
float2 jitter = get_jitter (frame_id);
198
201
#pragma omp parallel for
199
202
for (int x = 0 ; x < width; x++) {
200
203
for (int y = 0 ; y < height; y++) {
201
- float u = (2 .f * x + jitter.x ) / static_cast <float >(width) - 1 .f ;
202
- float v = (2 .f * y + jitter.y ) / static_cast <float >(height) - 1 .f ;
204
+ float u = (2 .f * x + jitter.x ) / static_cast <float >(width - 1 ) - 1 .f ;
205
+ float v = (2 .f * y + jitter.y ) / static_cast <float >(height - 1 ) - 1 .f ;
203
206
u *= static_cast <float >(width) / static_cast <float >(height);
204
- float3 ray_direction = direction + right * u - up * v;
207
+ float3 ray_direction = direction + u * right - v * up;
208
+ ray ray (position, ray_direction);
205
209
206
- ray primary_ray (position, ray_direction);
207
- payload payload = trace_ray (primary_ray, depth);
210
+ payload payload = trace_ray (ray, depth);
208
211
209
- history->item (x, y) += sqrt (payload.color .to_float3 () / accumulation_num);
210
- if (frame_id + 1 == accumulation_num) {
211
- render_target->item (x, y) = RT::from_float3 (history->item (x, y));
212
- }
212
+ auto & history_pixel = history->item (x, y);
213
+ history_pixel += sqrt (payload.color .to_float3 () * frame_weight);
214
+
215
+ if (frame_id == accumulation_num - 1 )
216
+ render_target->item (x, y) = RT::from_float3 (history_pixel);
213
217
}
214
218
}
215
219
}
@@ -243,10 +247,12 @@ namespace cg::renderer
243
247
}
244
248
}
245
249
246
- if (closest_hit_payload.t < max_t ) {
250
+ if (closest_triangle != nullptr && closest_hit_payload.t < max_t ) {
247
251
if (closest_hit_shader)
248
- return closest_hit_shader (ray, closest_hit_payload, *closest_triangle, depth);
252
+ return closest_hit_shader (ray, closest_hit_payload, *closest_triangle,
253
+ depth);
249
254
}
255
+
250
256
return miss_shader (ray);
251
257
}
252
258
0 commit comments