Euphoria
camera3.cc
Go to the documentation of this file.
1 #include "core/camera3.h"
2 
3 #include "assert/assert.h"
4 
5 namespace eu::core
6 {
8  : view(v)
9  , projection(p)
10  , combined(v * p)
11  , combined_inverted((p * v).get_inverted())
12  {
13  }
14 
15  vec3f
17  {
18  const vec4f v = combined * vec4f {in_world, 1};
19 
20  // divide by w to get back to the 1.0f coordinate space
21  return vec3f {v.x, v.y, v.z} / v.w;
22  }
23 
24  vec3f
26  {
27  const vec4f v = combined_inverted * vec4f {in_clip, 1};
28 
29  // divide by w to get back to the 1.0f coordinate space
30  return vec3f {v.x, v.y, v.z} / v.w;
31  }
32 
33  Ray3f
35  {
36  const auto from = from_clip_to_world(vec3f {p, -1.0f});
37  const auto to = from_clip_to_world(vec3f {p, 1.0f});
38  return Ray3f::from_to(from, to);
39  }
40 
42  : position(zero3f)
43  , rotation(q_identity)
44  , fov(Angle::from_degrees(45.0f))
45  , near(0.1f)
46  , far(100.0f)
47  {}
48 
49  namespace
50  {
51  mat4f
52  calculate_projection_matrix(const Camera3& camera, float aspect)
53  {
54  const mat4f projection_matrix = mat4f::create_perspective
55  (
56  camera.fov,
57  aspect,
58  camera.near,
59  camera.far
60  );
61  return projection_matrix;
62  }
63 
64  mat4f
65  calculate_view_matrix(const Camera3& camera)
66  {
67  const auto rotation = camera.rotation.get_conjugate().to_mat4();
68  const auto translation = mat4f::from_translation(-static_cast<vec3f>(camera.position));
69  return rotation * translation;
70  }
71  }
72 
73  CompiledCamera3
74  Camera3::compile(float aspect) const
75  {
76  return CompiledCamera3 {calculate_view_matrix(*this), calculate_projection_matrix(*this, aspect)};
77  }
78 
79 }
constexpr quatf q_identity
Definition: quat.h:71
constexpr vec3f zero3f
Definition: vec3.h:95
Definition: ray.h:37
static Ray3f from_to(const vec3f &from, const vec3f &to)
Definition: ray.cc:84
CompiledCamera3 compile(float aspect) const
Definition: camera3.cc:74
CompiledCamera3(const mat4f &v, const mat4f &p)
Definition: camera3.cc:7
Ray3f from_clip_to_world_ray(const vec2f &p) const
Definition: camera3.cc:34
vec3f from_clip_to_world(const vec3f &in_clip) const
Definition: camera3.cc:25
vec3f from_world_to_clip(const vec3f &in_world) const
Definition: camera3.cc:16
Definition: mat4.h:14
static mat4f create_perspective(const Angle &fov, float a, float near, float far)
Definition: mat4.cc:406
static mat4f from_translation(const vec3f &v)
Definition: mat4.cc:51
Definition: vec2.h:33
Definition: vec3.h:48
Definition: vec4.h:14