1 /***************************************************************************
2 *
3 * Copyright 2010,2011 BMW Car IT GmbH
4 *
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ****************************************************************************/
19 #include "Camera.h"
20 #include "vec.h"
21
22 #include <math.h>
23
24 #include <GLES2/gl2.h>
25 #include <GLES2/gl2ext.h>
26
Camera(vec3f position,vec3f target,float viewportWidth,float viewportHeight)27 Camera::Camera(vec3f position, vec3f target, float viewportWidth, float viewportHeight)
28 : m_position(position)
29 , m_target(target)
30 {
31 calculateTranslationMatrix();
32
33 m_fov = M_PI / 4; // 45°
34 m_near = 0.1f;
35 m_far = 1000.0f;
36 m_aspectRatio = viewportWidth/viewportHeight;
37 calculateProjectionMatrix();
38 calculateViewProjectionMatrix();
39 }
40
~Camera()41 Camera::~Camera()
42 {
43 }
44
calculateProjectionMatrix()45 void Camera::calculateProjectionMatrix()
46 {
47 // Precompute borders for projection
48 float range = m_near * tan(m_fov / 2.0);
49 float right = range * m_aspectRatio;
50 float top = range;
51
52 // Column 1
53 m_projectionMatrix[0] = m_near / right;
54 m_projectionMatrix[1] = 0.0;
55 m_projectionMatrix[2] = 0.0;
56 m_projectionMatrix[3] = 0.0;
57
58 // Column 2
59 m_projectionMatrix[4] = 0.0;
60 m_projectionMatrix[5] = m_near / top;
61 m_projectionMatrix[6] = 0.0;
62 m_projectionMatrix[7] = 0.0;
63
64 // Column 3
65 m_projectionMatrix[8] = 0.0;
66 m_projectionMatrix[9] = 0.0;
67 m_projectionMatrix[10] = -(m_far + m_near) / (m_far - m_near);
68 m_projectionMatrix[11] = -1;
69
70 // Column 4
71 m_projectionMatrix[12] = 0.0;
72 m_projectionMatrix[13] = 0.0;
73 m_projectionMatrix[14] = -(2 * m_far * m_near) / (m_far - m_near);
74 m_projectionMatrix[15] = 0.0;
75 }
76
calculateTranslationMatrix()77 void Camera::calculateTranslationMatrix()
78 {
79 m_translationMatrix[0] = 1.0f;
80 m_translationMatrix[1] = 0.0f;
81 m_translationMatrix[2] = 0.0f;
82 m_translationMatrix[3] = 0.0f;
83
84 m_translationMatrix[4] = 0.0f;
85 m_translationMatrix[5] = 1.0f;
86 m_translationMatrix[6] = 0.0f;
87 m_translationMatrix[7] = 0.0f;
88
89 m_translationMatrix[8] = 0.0f;
90 m_translationMatrix[9] = 0.0f;
91 m_translationMatrix[10] = 1.0f;
92 m_translationMatrix[11] = 0.0f;
93
94 m_translationMatrix[12] = m_position.x;
95 m_translationMatrix[13] = m_position.y;
96 m_translationMatrix[14] = m_position.z;
97 m_translationMatrix[15] = 1.0f;
98 }
99
calculateViewProjectionMatrix()100 void Camera::calculateViewProjectionMatrix()
101 {
102
103 m_viewProjectionMatrix[0] = m_translationMatrix[0] * m_projectionMatrix[0];
104 m_viewProjectionMatrix[1] = 0.0f;
105 m_viewProjectionMatrix[2] = 0.0f;
106 m_viewProjectionMatrix[3] = 0.0f;
107
108 m_viewProjectionMatrix[4] = 0.0f;
109 m_viewProjectionMatrix[5] = m_translationMatrix[5] * m_projectionMatrix[5];
110 m_viewProjectionMatrix[6] = 0.0f;
111 m_viewProjectionMatrix[7] = 0.0f;
112
113 m_viewProjectionMatrix[8] = 0.0f;
114 m_viewProjectionMatrix[9] = 0.0f;
115 m_viewProjectionMatrix[10] = m_translationMatrix[10] * m_projectionMatrix[10];
116 m_viewProjectionMatrix[11] = m_translationMatrix[10] * m_projectionMatrix[11];
117
118 m_viewProjectionMatrix[12] = m_translationMatrix[12] * m_projectionMatrix[0];
119 m_viewProjectionMatrix[13] = m_translationMatrix[13] * m_projectionMatrix[5];
120 m_viewProjectionMatrix[14] = m_translationMatrix[14] * m_projectionMatrix[10]
121 + m_translationMatrix[15] * m_projectionMatrix[14];
122 m_viewProjectionMatrix[15] = m_translationMatrix[14] * m_projectionMatrix[11];
123 }
124
getViewProjectionMatrix()125 float* Camera::getViewProjectionMatrix()
126 {
127 return m_viewProjectionMatrix;
128 }
129
update(int currentTimeInMs,int lastFrameTime)130 void Camera::update(int currentTimeInMs, int lastFrameTime)
131 {
132 (void)currentTimeInMs; // prevent warning
133 (void)lastFrameTime; // prevent warning
134 }
135