Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
camera.h
1 #pragma once
2 
3 #include "stage_node.h"
4 #include "../generic/identifiable.h"
5 #include "../generic/optional.h"
6 #include "../generic/manual_object.h"
7 #include "../math/aabb.h"
8 #include "../frustum.h"
9 #include "../sound.h"
10 
11 namespace smlt {
12 
13 class RenderTarget;
14 
15 class Camera:
16  public TypedDestroyableObject<Camera, Stage>,
17  public ContainerNode,
18  public generic::Identifiable<CameraID>,
19  public ChainNameable<Camera>,
20  public AudioSource,
21  public RefCounted<Camera> {
22 
23 public:
24  using ContainerNode::_get_renderables;
25 
26  Camera(Stage* stage, SoundDriver* sound_driver);
27  virtual ~Camera();
28 
29  /* Camera Proxies have no mass/body so their AABB is just 0,0,0, or their position */
30  const AABB& aabb() const override {
31  return bounds_;
32  }
33 
34  const AABB transformed_aabb() const override {
35  return AABB(position(), position());
36  }
37 
38  void clean_up() override {
39  StageNode::clean_up();
40  }
41 
42  void update(float dt) override;
43 
44  // Converts an OpenGL unit to window space
45  smlt::optional<Vec3> project_point(const RenderTarget& target, const Viewport& viewport, const Vec3& point) const;
46 
47  // Converts a pixel to OpenGL units (z-input should be read from the depth buffer)
48  smlt::optional<Vec3> unproject_point(const RenderTarget& target, const Viewport& viewport, const Vec3& win_point);
49 
50  const Mat4& view_matrix() const { return view_matrix_; }
51  const Mat4& projection_matrix() const { return projection_matrix_; }
52 
53  Frustum& frustum() { return frustum_; }
54 
55  void set_perspective_projection(const Degrees &fov, float aspect, float near=1.0f, float far=1000.0f);
56  void set_orthographic_projection(float left, float right, float bottom, float top, float near=-1.0, float far=1.0);
57  float set_orthographic_projection_from_height(float desired_height_in_units, float ratio);
58 
59 private:
60  AABB bounds_;
61  Frustum frustum_;
62 
63  smlt::Mat4 transform_;
64  Mat4 view_matrix_;
65  Mat4 projection_matrix_;
66 
67  void update_frustum();
68 
69  void update_transformation_from_parent() override;
70 };
71 
72 }
smlt::Frustum
Definition: frustum.h:43
smlt::Mat4
Definition: mat4.h:25
smlt::Vec3
Definition: vec3.h:23
smlt::AudioSource
Definition: sound.h:103
smlt::Stage
Definition: stage.h:92
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::ContainerNode
Definition: stage_node.h:285
smlt::TypedDestroyableObject
Definition: manual_object.h:33
smlt::Camera
Definition: camera.h:21
smlt::Degrees
Definition: degrees.h:7
smlt::RenderTarget
Definition: interfaces.h:27
smlt::optional
Definition: optional.h:13
smlt::ChainNameable
Definition: nameable.h:34
smlt::AABB
Definition: aabb.h:22
smlt::SoundDriver
Definition: sound_driver.h:74
smlt::Viewport
Definition: viewport.h:44