Skip to content

Commit 3000166

Browse files
committed
hotfix
1 parent f8cf407 commit 3000166

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/renderer/raytracer/raytracer.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ namespace cg::renderer
148148
inline void raytracer<VB, RT>::clear_render_target(
149149
const RT& in_clear_value)
150150
{
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++) {
152152
render_target->item(i) = in_clear_value;
153153
history->item(i) = float3{0.f, 0.f, 0.f};
154154
}
@@ -192,24 +192,28 @@ namespace cg::renderer
192192
float3 position, float3 direction,
193193
float3 right, float3 up, size_t depth, size_t accumulation_num)
194194
{
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";
197200
float2 jitter = get_jitter(frame_id);
198201
#pragma omp parallel for
199202
for (int x = 0; x < width; x++) {
200203
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;
203206
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);
205209

206-
ray primary_ray(position, ray_direction);
207-
payload payload = trace_ray(primary_ray, depth);
210+
payload payload = trace_ray(ray, depth);
208211

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);
213217
}
214218
}
215219
}
@@ -243,10 +247,12 @@ namespace cg::renderer
243247
}
244248
}
245249

246-
if (closest_hit_payload.t < max_t) {
250+
if (closest_triangle != nullptr && closest_hit_payload.t < max_t) {
247251
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);
249254
}
255+
250256
return miss_shader(ray);
251257
}
252258

0 commit comments

Comments
 (0)