TDME2  1.9.200
Camera.cpp
Go to the documentation of this file.
1 #include <tdme/engine/Camera.h>
2 
3 #include <memory>
4 
5 #include <tdme/tdme.h>
7 #include <tdme/engine/Frustum.h>
8 #include <tdme/math/Math.h>
9 #include <tdme/math/Matrix4x4.h>
10 #include <tdme/math/Vector3.h>
11 #include <tdme/utilities/Console.h>
12 
13 using std::make_unique;
14 using std::unique_ptr;
15 
19 using tdme::math::Math;
23 
24 Camera::Camera(Renderer* renderer)
25 {
26  this->renderer = renderer;
27  width = 0;
28  height = 0;
29  fovX = 45.0f;
30  zNear = 0.1f;
31  zFar = 150.0f;
35  upVector.set(0.0f, 1.0f, 0.0f);
36  forwardVector.set(0.0f, 0.0f, -1.0f);
37  sideVector.set(1.0f, 0.0f, 0.0f);
38  lookFrom.set(0.0f, 50.0f, 400.0f);
39  lookAt.set(0.0f, 50.0f, 0.0f);
40  frustum = make_unique<Frustum>(renderer);
41 }
42 
44 }
45 
46 Vector3 Camera::defaultUp(0.0f, 1.0f, 0.0f);
47 
48 Vector3 Camera::computeUpVector(const Vector3& lookFrom, const Vector3& lookAt)
49 {
50  Vector3 tmpForward;
51  Vector3 tmpUpVector;
52  tmpForward.set(lookAt).sub(lookFrom).normalize();
53  if (Math::abs(tmpForward.getX()) < Math::EPSILON && Math::abs(tmpForward.getZ()) < Math::EPSILON) {
54  tmpUpVector.set(0.0f, 0.0f, tmpForward.getY()).normalize();
55  return tmpUpVector;
56  }
57  auto tmpSide = Vector3::computeCrossProduct(tmpForward, defaultUp).normalize();
58  tmpUpVector = Vector3::computeCrossProduct(tmpSide, tmpForward).normalize();
59  return tmpUpVector;
60 }
61 
63 {
64  // see: see http://www.songho.ca/opengl/gl_transform.html
65  switch(frustumMode) {
67  {
68  auto leftPlane = (-width / 2.0f) * orthographicFrustumScale;
69  auto rightPlane = (width / 2.0f) * orthographicFrustumScale;
70  auto topPlane = (height / 2.0f) * orthographicFrustumScale;
71  auto bottomPlane = (-height / 2.0f) * orthographicFrustumScale;
72  auto nearPlane = zNear;
73  auto farPlane = zFar;
74  return projectionMatrix.set(
75  2.0f / (rightPlane - leftPlane),
76  0.0f,
77  0.0f,
78  0.0f,
79  0.0f,
80  2.0f / (topPlane - bottomPlane),
81  0.0f,
82  0.0f,
83  0.0f,
84  0.0f,
85  -2.0f / (farPlane - nearPlane),
86  0.0f,
87  -(rightPlane + leftPlane) / (rightPlane - leftPlane),
88  -(topPlane + bottomPlane) / (topPlane - bottomPlane),
89  -(farPlane + nearPlane) / (farPlane - nearPlane),
90  1.0f
91  );
92  }
93  default:
94  {
95  // see: https://github.com/g-truc/glm
96  auto aspect = static_cast<float>(this->height) / static_cast<float>(this->width);
97  auto rad = fovX * 3.1415927f / 180.0f;
98  auto _height = Math::cos(0.5f * rad) / Math::sin(0.5 * rad);
99  auto _width = _height * aspect;
100  return projectionMatrix.set(
101  _width,
102  0.0f,
103  0.0f,
104  0.0f,
105  0.0f,
106  _height,
107  0.0f,
108  0.0f,
109  0.0f,
110  0.0f,
111  -(zFar + zNear) / (zFar - zNear),
112  -1.0f,
113  0.0f,
114  0.0f,
115  -(2.0f * zFar * zNear) / (zFar - zNear),
116  1.0f
117  );
118  }
119  }
120 }
121 
123 {
124  Vector3 tmpUp = upVector;
125  if (cameraMode == CAMERAMODE_LOOKAT) {
127  sideVector = Vector3::computeCrossProduct(forwardVector, upVector).normalize();
128  tmpUp = Vector3::computeCrossProduct(sideVector, forwardVector);
129  }
130  cameraMatrix.
131  identity().
132  setTranslation(
133  lookFrom.clone().scale(-1.0f)
134  ).
135  multiply(
136  Matrix4x4(
137  sideVector[0],
138  tmpUp[0],
139  -forwardVector[0],
140  0.0f,
141  sideVector[1],
142  tmpUp[1],
143  -forwardVector[1],
144  0.0f,
145  sideVector[2],
146  tmpUp[2],
147  -forwardVector[2],
148  0.0f,
149  0.0f,
150  0.0f,
151  0.0f,
152  1.0f
153  )
154  );
155  return cameraMatrix;
156 }
157 
158 void Camera::update(int contextIdx, int32_t width, int32_t height)
159 {
160  auto reshaped = false;
161  auto _width = width;
162  auto _height = height;
163  if (this->width != _width || this->height != _height) {
164  reshaped = true;
165  if (_height <= 0)
166  _height = 1;
167 
168  this->width = _width;
169  this->height = _height;
171  _width / 2.0f,
172  0.0f,
173  0.0f,
174  0.0f,
175  0.0f,
176  _height / 2.0f,
177  0.0f,
178  0.0f,
179  0.0f,
180  0.0f,
181  1.0f,
182  0.0f,
183  0 + (_width / 2.0f),
184  0 + (_height / 2.0f),
185  0.0f,
186  1.0f
187  );
188  }
189 
190  // setup projection and model view matrices and such
193  renderer->onUpdateProjectionMatrix(contextIdx);
195  renderer->onUpdateModelViewMatrix(contextIdx);
197  renderer->onUpdateCameraMatrix(contextIdx);
198 
199  //
202 
203  // viewport
206 }
Matrix4x4 projectionMatrix
Definition: Camera.h:46
Vector3 upVector
Definition: Camera.h:43
FrustumMode frustumMode
Definition: Camera.h:39
Vector3 lookFrom
Definition: Camera.h:41
static STATIC_DLL_IMPEXT Vector3 defaultUp
Definition: Camera.h:31
Vector3 sideVector
Definition: Camera.h:45
Vector3 lookAt
Definition: Camera.h:42
int32_t height
Definition: Camera.h:34
void update(int contextIdx, int32_t width, int32_t height)
Sets up camera while resizing the view port.
Definition: Camera.cpp:158
Matrix4x4 mvpInvertedMatrix
Definition: Camera.h:49
Matrix4x4 & computeModelViewMatrix()
Computes projection matrix for given look from, look at and up vector.
Definition: Camera.cpp:122
unique_ptr< Frustum > frustum
Definition: Camera.h:50
Matrix4x4 mvpMatrix
Definition: Camera.h:48
~Camera()
Destructor.
Definition: Camera.cpp:43
static Vector3 computeUpVector(const Vector3 &lookFrom, const Vector3 &lookAt)
Computes the up vector for given look from and look at vectors.
Definition: Camera.cpp:48
float orthographicFrustumScale
Definition: Camera.h:40
Vector3 forwardVector
Definition: Camera.h:44
@ FRUSTUMMODE_ORTHOGRAPHIC
Definition: Camera.h:27
Matrix4x4 & computeProjectionMatrix()
Computes the projection matrix.
Definition: Camera.cpp:62
Renderer * renderer
Definition: Camera.h:32
CameraMode cameraMode
Definition: Camera.h:38
Matrix4x4 cameraMatrix
Definition: Camera.h:47
Frustum class.
Definition: Frustum.h:29
virtual void onUpdateProjectionMatrix(int contextIdx)=0
Update projection matrix event.
virtual void setViewPort(int32_t width, int32_t height)=0
Set up viewport parameter.
virtual void onUpdateCameraMatrix(int contextIdx)=0
Update camera matrix event.
virtual void updateViewPort()=0
Update viewport.
virtual void onUpdateModelViewMatrix(int contextIdx)=0
Update model view matrix event.
Standard math functions.
Definition: Math.h:19
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 multiply(const Vector3 &vector3) const
Multiplies this matrix with vector3.
Definition: Matrix4x4.h:225
Matrix4x4 & set(float r0c0, float r0c1, float r0c2, float r0c3, float r1c0, float r1c1, float r1c2, float r1c3, float r2c0, float r2c1, float r2c2, float r2c3, float r3c0, float r3c1, float r3c2, float r3c3)
Sets this matrix by its components.
Definition: Matrix4x4.h:108
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
float getY() const
Definition: Vector3.h:117
float getX() const
Definition: Vector3.h:100
float getZ() const
Definition: Vector3.h:134
Vector3 clone() const
Clones this vector3.
Definition: Vector3.h:374
Vector3 & sub(float scalar)
Subtracts a scalar.
Definition: Vector3.h:177
Vector3 & scale(float scalar)
Scales by scalar.
Definition: Vector3.h:201
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Vector3 & normalize()
Normalizes this vector3.
Definition: Vector3.h:239
Console class.
Definition: Console.h:29