Scene Lab
An open source project by FPL.
 All Classes Namespaces Files Functions Pages
basic_camera.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SCENE_LAB_BASIC_CAMERA_H_
16 #define SCENE_LAB_BASIC_CAMERA_H_
17 
18 #include "corgi_component_library/camera_interface.h"
19 #include "mathfu/constants.h"
20 #include "mathfu/glsl_mappings.h"
21 
22 namespace scene_lab {
23 
24 static const mathfu::vec3 kCameraForward = mathfu::kAxisY3f;
25 static const mathfu::vec3 kCameraSide = mathfu::kAxisX3f;
26 static const mathfu::vec3 kCameraUp = mathfu::kAxisZ3f;
27 
28 /// @file
29 /// A simple camera class that implements CameraInterface. If you just need a
30 /// very basic camera to use Scene Lab, you can use this one, which just
31 /// implements the barebones requirements, no stereoscopy or other fancy stuff.
32 ///
33 /// Scene Lab will use this camera by default unless you override by calling
34 /// SceneLab::SetCamera.
35 ///
36 /// By default, this camera uses right-handed coordinates.
37 class BasicCamera : public corgi::CameraInterface {
38  public:
39  BasicCamera();
40  virtual ~BasicCamera() {}
41 
42  /// Returns the View/Projection matrix. `index` must be 0.
43  virtual mathfu::mat4 GetTransformMatrix(int32_t index) const {
44  assert(index == 0);
45  (void)index;
46  return GetTransformMatrix();
47  }
48 
49  /// Returns the View/Projection matrix.
50  virtual mathfu::mat4 GetTransformMatrix() const;
51 
52  /// Returns just View matrix. `index` must be 0.
53  virtual mathfu::mat4 GetViewMatrix(int32_t index) const {
54  assert(index == 0);
55  (void)index;
56  return GetViewMatrix();
57  }
58 
59  /// Returns just the View matrix.
60  virtual mathfu::mat4 GetViewMatrix() const;
61 
62  /// Set the camera's world position. `index` must be 0.
63  virtual void set_position(int32_t index, const mathfu::vec3& position) {
64  assert(index == 0);
65  (void)index;
66  set_position(position);
67  }
68 
69  /// Set the camera's world position.
70  virtual void set_position(const mathfu::vec3& position) {
71  position_ = position;
72  }
73 
74  /// Get the camera's world position. `index` must be 0.
75  virtual mathfu::vec3 position(int32_t index) const {
76  assert(index == 0);
77  (void)index;
78  return position();
79  }
80 
81  /// Get the camera's world position.
82  virtual mathfu::vec3 position() const { return position_; }
83 
84  /// Set the camera's forward direction.
85  virtual void set_facing(const mathfu::vec3& facing) {
86  assert(facing.LengthSquared() != 0);
87  facing_ = facing;
88  }
89 
90  /// Get the camera's forward direction.
91  virtual const mathfu::vec3& facing() const { return facing_; }
92 
93  /// Set the camera's up direction.
94  virtual void set_up(const mathfu::vec3& up) {
95  assert(up.LengthSquared() != 0);
96  up_ = up;
97  }
98  /// Get the camera's up direction.
99  virtual const mathfu::vec3& up() const { return up_; }
100 
101  /// Get the camera's right direction, calculated via cross product of its
102  /// forward and up directions.
103  mathfu::vec3 Right() const {
104  return mathfu::vec3::CrossProduct(facing_, up_);
105  }
106 
107  /// Set the camera's viewport angle, in radians.
109  viewport_angle_ = viewport_angle;
110  }
111  /// Get the camera's viewport angle, in radians.
112  virtual float viewport_angle() const { return viewport_angle_; }
113 
114  /// Set the camera's viewport resolution.
115  virtual void set_viewport_resolution(mathfu::vec2 viewport_resolution) {
116  viewport_resolution_ = viewport_resolution;
117  }
118  /// Get the camera's viewport resolution.
119  virtual mathfu::vec2 viewport_resolution() const {
120  return viewport_resolution_;
121  }
122  /// Set the distance to the near clipping plane.
124  viewport_near_plane_ = viewport_near_plane;
125  }
126  /// Get the distance to the near clipping plane.
127  virtual float viewport_near_plane() const { return viewport_near_plane_; }
128 
129  /// Set the distance to the far clipping plane.
131  viewport_far_plane_ = viewport_far_plane;
132  }
133  /// Get the distance to the far clipping plane.
134  virtual float viewport_far_plane() const { return viewport_far_plane_; }
135 
136  /// Set the camera's viewport.
137  virtual void set_viewport(const mathfu::vec4i& viewport) {
138  viewport_ = viewport;
139  }
140  /// Set the camera's viewport. `index` must be 0.
141  virtual void set_viewport(int32_t index, const mathfu::vec4i& viewport) {
142  assert(index == 0);
143  (void)index;
144  set_viewport(viewport);
145  }
146  /// Get the camera's viewport. `index` must be 0.
147  virtual const mathfu::vec4i& viewport(int32_t index) const {
148  assert(index == 0);
149  (void)index;
150  return viewport();
151  }
152 
153  /// Get the camera's viewport settings.
154  virtual const mathfu::vec4i& viewport() const { return viewport_; }
155 
156  /// Returns false, since this camera is not stereoscopic by design.
157  virtual bool IsStereo() const { return false; }
158  /// Fails if you try to set stereoscopic mode to anything but false.
159  virtual void set_stereo(bool is_stereo) {
160  assert(!is_stereo);
161  (void)is_stereo;
162  }
163 
164  /// Initialize the camera's viewport settings.
167  viewport_angle_ = viewport_angle;
168  viewport_resolution_ = viewport_resolution;
169  viewport_near_plane_ = viewport_near_plane;
170  viewport_far_plane_ = viewport_far_plane;
171  }
172 
173  MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
174 
175  private:
176  mathfu::vec3 position_;
177  mathfu::vec3 facing_;
178  mathfu::vec3 up_;
179  float viewport_angle_;
180  mathfu::vec2 viewport_resolution_;
181  float viewport_near_plane_;
182  float viewport_far_plane_;
183  mathfu::vec4i viewport_;
184 };
185 
186 } // namespace scene_lab
187 
188 #endif // SCENE_LAB_BASIC_CAMERA_H_
virtual float viewport_angle() const
Get the camera's viewport angle, in radians.
Definition: basic_camera.h:112
virtual mathfu::mat4 GetTransformMatrix(int32_t index) const
Returns the View/Projection matrix. index must be 0.
Definition: basic_camera.h:43
virtual mathfu::mat4 GetViewMatrix() const
Returns just the View matrix.
virtual void set_position(int32_t index, const mathfu::vec3 &position)
Set the camera's world position. index must be 0.
Definition: basic_camera.h:63
virtual mathfu::mat4 GetViewMatrix(int32_t index) const
Returns just View matrix. index must be 0.
Definition: basic_camera.h:53
virtual bool IsStereo() const
Returns false, since this camera is not stereoscopic by design.
Definition: basic_camera.h:157
virtual void set_facing(const mathfu::vec3 &facing)
Set the camera's forward direction.
Definition: basic_camera.h:85
virtual void set_viewport(const mathfu::vec4i &viewport)
Set the camera's viewport.
Definition: basic_camera.h:137
void Initialize(float viewport_angle, mathfu::vec2 viewport_resolution, float viewport_near_plane, float viewport_far_plane)
Initialize the camera's viewport settings.
Definition: basic_camera.h:165
virtual mathfu::vec3 position(int32_t index) const
Get the camera's world position. index must be 0.
Definition: basic_camera.h:75
virtual const mathfu::vec3 & facing() const
Get the camera's forward direction.
Definition: basic_camera.h:91
mathfu::vec3 Right() const
Get the camera's right direction, calculated via cross product of its forward and up directions...
Definition: basic_camera.h:103
virtual float viewport_near_plane() const
Get the distance to the near clipping plane.
Definition: basic_camera.h:127
virtual void set_up(const mathfu::vec3 &up)
Set the camera's up direction.
Definition: basic_camera.h:94
virtual const mathfu::vec4i & viewport(int32_t index) const
Get the camera's viewport. index must be 0.
Definition: basic_camera.h:147
virtual const mathfu::vec4i & viewport() const
Get the camera's viewport settings.
Definition: basic_camera.h:154
virtual void set_viewport_near_plane(float viewport_near_plane)
Set the distance to the near clipping plane.
Definition: basic_camera.h:123
virtual void set_viewport(int32_t index, const mathfu::vec4i &viewport)
Set the camera's viewport. index must be 0.
Definition: basic_camera.h:141
virtual void set_stereo(bool is_stereo)
Fails if you try to set stereoscopic mode to anything but false.
Definition: basic_camera.h:159
void set_viewport_angle(float viewport_angle)
Set the camera's viewport angle, in radians.
Definition: basic_camera.h:108
virtual mathfu::vec3 position() const
Get the camera's world position.
Definition: basic_camera.h:82
virtual void set_viewport_resolution(mathfu::vec2 viewport_resolution)
Set the camera's viewport resolution.
Definition: basic_camera.h:115
virtual mathfu::mat4 GetTransformMatrix() const
Returns the View/Projection matrix.
virtual void set_position(const mathfu::vec3 &position)
Set the camera's world position.
Definition: basic_camera.h:70
virtual float viewport_far_plane() const
Get the distance to the far clipping plane.
Definition: basic_camera.h:134
virtual void set_viewport_far_plane(float viewport_far_plane)
Set the distance to the far clipping plane.
Definition: basic_camera.h:130
virtual mathfu::vec2 viewport_resolution() const
Get the camera's viewport resolution.
Definition: basic_camera.h:119
Definition: basic_camera.h:37
virtual const mathfu::vec3 & up() const
Get the camera's up direction.
Definition: basic_camera.h:99