Tatooine
render.h
Go to the documentation of this file.
1#ifndef TATOOINE_RENDERING_RAYTRACING_RENDER_H
2#define TATOOINE_RENDERING_RAYTRACING_RENDER_H
3//==============================================================================
7//==============================================================================
9//==============================================================================
10template <typename Real>
12 vec<Real, 3> const& bg_color = vec<Real, 3>::ones(),
13 std::string const& prop_name = "image") {
14 rectilinear_grid image{cam.plane_width(), cam.plane_height()};
15 auto& rendered_mesh = image.template add_vertex_property<vec3>(prop_name);
16
17 constexpr std::array offsets{vec2{0, 0}, vec2{-0.25, -0.25},
18 vec2{0.25, -0.25}, vec2{-0.25, 0.25},
19 vec2{0.25, 0.25}};
20 Real const shininess = 10;
21 auto const L = normalize(cam.lookat() - cam.eye());
22#pragma omp parallel for collapse(2)
23 for (std::size_t y = 0; y < cam.plane_height(); ++y) {
24 for (std::size_t x = 0; x < cam.plane_width(); ++x) {
25 rendered_mesh(x, y) = vec<Real, 3>::zeros();
26 for (auto const& offset : offsets) {
27 auto const cur_ray = cam.ray(x + offset(0), y + offset(1));
28 auto const intersection = mesh.check_intersection(cur_ray);
29 if (intersection) {
30 auto const N = normalize(intersection->normal);
31 auto const V = normalize(cur_ray.direction());
32
33 auto const diffuse_color = intersection->normal * 0.5 + 0.5;
34 auto luminance = diffuse_color;
35
36 auto const illuminance = std::abs(dot(L, N));
37 //if (illuminance > 0) {
38 // auto brdf = diffuse_color;
39 // auto const half_dir = normalize(V + L);
40 // auto const spec_dot = std::max(dot(half_dir, N), 0.0);
41 // brdf += std::pow(spec_dot, shininess);
42 // luminance += brdf * illuminance;
43 //}
44
45 rendered_mesh(x, y) += luminance;
46 } else {
47 rendered_mesh(x, y) += bg_color;
48 }
49 }
50 rendered_mesh(x, y) /= offsets.size();
51 }
52 }
53 return image;
54}
55//==============================================================================
56} // namespace tatooine::rendering::raytracing
57//==============================================================================
58#endif
Definition: mesh.h:16
Definition: rectilinear_grid.h:38
Definition: camera.h:312
Definition: render.h:8
auto render(camera< Real > const &cam, unstructured_triangular_grid< Real, 3 > const &mesh, vec< Real, 3 > const &bg_color=vec< Real, 3 >::ones(), std::string const &prop_name="image")
Definition: render.h:11
constexpr auto normalize(base_tensor< Tensor, T, N > const &t_in) -> vec< T, N >
Definition: tensor_operations.h:100
constexpr auto dot(base_tensor< Tensor0, T0, N > const &lhs, base_tensor< Tensor1, T1, N > const &rhs)
Definition: tensor_operations.h:120
Definition: intersection.h:13
normal_t normal
normal of intersection point on the primitive
Definition: intersection.h:31
Definition: unstructured_triangular_grid.h:10
Definition: vec.h:12
static auto constexpr zeros()
Definition: vec.h:26