Skip to content

Commit 2586cc4

Browse files
committed
Do Lab 2.01
1 parent 93bcc2c commit 2586cc4

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/renderer/raytracer/raytracer.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ namespace cg::renderer
8282
class raytracer
8383
{
8484
public:
85-
raytracer(){};
86-
~raytracer(){};
85+
raytracer() {};
86+
~raytracer() {};
8787

8888
void set_render_target(std::shared_ptr<resource<RT>> in_render_target);
8989
void clear_render_target(const RT& in_clear_value);
@@ -122,22 +122,25 @@ namespace cg::renderer
122122
inline void raytracer<VB, RT>::set_render_target(
123123
std::shared_ptr<resource<RT>> in_render_target)
124124
{
125-
// TODO Lab: 2.01 Implement `set_render_target`, `set_viewport`, and `clear_render_target` methods of `raytracer` class
125+
render_target = in_render_target;
126126
}
127127

128128
template<typename VB, typename RT>
129129
inline void raytracer<VB, RT>::set_viewport(size_t in_width,
130130
size_t in_height)
131131
{
132-
// TODO Lab: 2.01 Implement `set_render_target`, `set_viewport`, and `clear_render_target` methods of `raytracer` class
132+
width = in_width;
133+
height = in_height;
133134
// TODO Lab: 2.06 Add `history` resource in `raytracer` class
134135
}
135136

136137
template<typename VB, typename RT>
137138
inline void raytracer<VB, RT>::clear_render_target(
138139
const RT& in_clear_value)
139140
{
140-
// TODO Lab: 2.01 Implement `set_render_target`, `set_viewport`, and `clear_render_target` methods of `raytracer` class
141+
for (size_t i = 0; i < render_target->get_number_of_elements(); i++) {
142+
render_target->item(i) = in_clear_value;
143+
}
141144
// TODO Lab: 2.06 Add `history` resource in `raytracer` class
142145
}
143146

@@ -165,19 +168,33 @@ namespace cg::renderer
165168
float3 position, float3 direction,
166169
float3 right, float3 up, size_t depth, size_t accumulation_num)
167170
{
168-
// TODO Lab: 2.01 Implement `ray_generation` and `trace_ray` method of `raytracer` class
171+
#pragma omp parallel for
172+
for (int x = 0; x < width; x++) {
173+
for (int y = 0; y < height; y++) {
174+
float u = (2.f * x) / static_cast<float>(width) - 1.f;
175+
float v = (2.f * y) / static_cast<float>(height) - 1.f;
176+
u *= static_cast<float>(width) / static_cast<float>(height);
177+
float3 ray_direction = direction + right * u - up * v;
178+
ray primary_ray(position, ray_direction);
179+
payload payload = trace_ray(primary_ray, depth);
180+
render_target->item(x, y) = RT::from_color(payload.color);
181+
}
182+
}
169183
// TODO Lab: 2.06 Implement TAA in `ray_generation` method of `raytracer` class
170184
}
171185

172186
template<typename VB, typename RT>
173187
inline payload raytracer<VB, RT>::trace_ray(
174188
const ray& ray, size_t depth, float max_t, float min_t) const
175189
{
176-
// TODO Lab: 2.01 Implement `ray_generation` and `trace_ray` method of `raytracer` class
190+
if (depth == 0) {
191+
return miss_shader(ray);
192+
}
193+
depth--;
177194
// TODO Lab: 2.02 Adjust `trace_ray` method of `raytracer` class to traverse geometry and call a closest hit shader
178195
// TODO Lab: 2.04 Adjust `trace_ray` method of `raytracer` to use `any_hit_shader`
179196
// TODO Lab: 2.05 Adjust `trace_ray` method of `raytracer` class to traverse the acceleration structure
180-
return payload{};
197+
return miss_shader(ray);
181198
}
182199

183200
template<typename VB, typename RT>

0 commit comments

Comments
 (0)