OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/android/vr_shell/vr_util.h" | |
6 | |
7 #include <array> | |
8 #include <cmath> | |
9 | |
10 #include "third_party/gvr-android-sdk/src/ndk-beta/include/vr/gvr/capi/include/g
vr_types.h" | |
11 | |
12 namespace vr_shell { | |
13 | |
14 // Internal matrix layout: | |
15 // | |
16 // m[0][0], m[0][1], m[0][2], m[0][3], | |
17 // m[1][0], m[1][1], m[1][2], m[1][3], | |
18 // m[2][0], m[2][1], m[2][2], m[2][3], | |
19 // m[3][0], m[3][1], m[3][2], m[3][3], | |
20 // | |
21 // The translation component is in the right column m[i][3]. | |
22 // | |
23 // The bottom row m[3][i] is (0, 0, 0, 1) for non-perspective transforms. | |
24 // | |
25 // These matrices are intended to be used to premultiply column vectors | |
26 // for transforms, so successive transforms need to be left-multiplied. | |
27 | |
28 void SetIdentityM(gvr::Mat4f& mat) { | |
29 float* m = (float*)mat.m; | |
30 for (int i = 0; i < 16; i++) { | |
31 m[i] = 0; | |
32 } | |
33 for (int i = 0; i < 16; i += 5) { | |
34 m[i] = 1.0f; | |
35 } | |
36 } | |
37 | |
38 // Left multiply a translation matrix. | |
39 void TranslateM(gvr::Mat4f& tmat, gvr::Mat4f& mat, float x, float y, float z) { | |
40 if (&tmat != &mat) { | |
41 for (int i = 0; i < 4; ++i) { | |
42 for (int j = 0; j < 4; ++j) { | |
43 tmat.m[i][j] = mat.m[i][j]; | |
44 } | |
45 } | |
46 } | |
47 tmat.m[0][3] += x; | |
48 tmat.m[1][3] += y; | |
49 tmat.m[2][3] += z; | |
50 } | |
51 | |
52 // Right multiply a translation matrix. | |
53 void TranslateMRight(gvr::Mat4f& tmat, | |
54 gvr::Mat4f& mat, | |
55 float x, | |
56 float y, | |
57 float z) { | |
58 if (&tmat != &mat) { | |
59 for (int i = 0; i < 4; ++i) { | |
60 for (int j = 0; j < 3; ++j) { | |
61 tmat.m[i][j] = mat.m[i][j]; | |
62 } | |
63 } | |
64 } | |
65 | |
66 for (int i = 0; i < 4; i++) { | |
67 tmat.m[i][3] = | |
68 mat.m[i][0] * x + mat.m[i][1] * y + mat.m[i][2] * z + mat.m[i][3]; | |
69 } | |
70 } | |
71 | |
72 // Left multiply a scale matrix. | |
73 void ScaleM(gvr::Mat4f& tmat, const gvr::Mat4f& mat, | |
74 float x, float y, float z) { | |
75 if (&tmat != &mat) { | |
76 for (int i = 0; i < 4; ++i) { | |
77 for (int j = 0; j < 3; ++j) { | |
78 tmat.m[i][j] = mat.m[i][j]; | |
79 } | |
80 } | |
81 } | |
82 // Multiply all rows including translation components. | |
83 for (int j = 0; j < 4; ++j) { | |
84 tmat.m[0][j] *= x; | |
85 tmat.m[1][j] *= y; | |
86 tmat.m[2][j] *= z; | |
87 } | |
88 } | |
89 | |
90 // Right multiply a scale matrix. | |
91 void ScaleMRight(gvr::Mat4f& tmat, const gvr::Mat4f& mat, | |
92 float x, float y, float z) { | |
93 if (&tmat != &mat) { | |
94 for (int i = 0; i < 4; ++i) { | |
95 for (int j = 0; j < 3; ++j) { | |
96 tmat.m[i][j] = mat.m[i][j]; | |
97 } | |
98 } | |
99 } | |
100 // Multiply columns, don't change translation components. | |
101 for (int i = 0; i < 3; ++i) { | |
102 tmat.m[i][0] *= x; | |
103 tmat.m[i][1] *= y; | |
104 tmat.m[i][2] *= z; | |
105 } | |
106 } | |
107 | |
108 std::array<float, 16> MatrixToGLArray(const gvr::Mat4f& matrix) { | |
109 // Note that this performs a *transpose* to a column-major matrix array, as | |
110 // expected by GL. The input matrix has translation components at [i][3] for | |
111 // use with row vectors and premultiplied transforms. In the output, the | |
112 // translation elements are at the end at positions 3*4+i. | |
113 std::array<float, 16> result; | |
114 for (int i = 0; i < 4; ++i) { | |
115 for (int j = 0; j < 4; ++j) { | |
116 result[j * 4 + i] = matrix.m[i][j]; | |
117 } | |
118 } | |
119 return result; | |
120 } | |
121 | |
122 gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) { | |
123 gvr::Mat4f result; | |
124 for (int i = 0; i < 4; ++i) { | |
125 for (int k = 0; k < 4; ++k) { | |
126 result.m[i][k] = mat.m[k][i]; | |
127 } | |
128 } | |
129 return result; | |
130 } | |
131 | |
132 std::array<float, 4> MatrixVectorMul(const gvr::Mat4f& matrix, | |
133 const std::array<float, 4>& vec) { | |
134 std::array<float, 4> result; | |
135 for (int i = 0; i < 4; ++i) { | |
136 result[i] = 0; | |
137 for (int k = 0; k < 4; ++k) { | |
138 result[i] += matrix.m[i][k] * vec[k]; | |
139 } | |
140 } | |
141 return result; | |
142 } | |
143 | |
144 std::array<float, 3> MatrixVectorMul(const gvr::Mat4f& matrix, | |
145 const std::array<float, 3>& vec) { | |
146 // Use homogeneous coordinates for the multiplication. | |
147 std::array<float, 4> vec_h = {{vec[0], vec[1], vec[2], 1.0f}}; | |
148 std::array<float, 4> result; | |
149 for (int i = 0; i < 4; ++i) { | |
150 result[i] = 0; | |
151 for (int k = 0; k < 4; ++k) { | |
152 result[i] += matrix.m[i][k] * vec_h[k]; | |
153 } | |
154 } | |
155 // Convert back from homogeneous coordinates. | |
156 float rw = 1.0f / result[3]; | |
157 return {{rw * result[0], rw * result[1], rw * result[2]}}; | |
158 } | |
159 | |
160 gvr::Vec3f MatrixVectorMul(const gvr::Mat4f& m, const gvr::Vec3f& v) { | |
161 gvr::Vec3f res; | |
162 res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3]; | |
163 res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3]; | |
164 res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3]; | |
165 return res; | |
166 } | |
167 | |
168 // Rotation only, ignore translation components. | |
169 gvr::Vec3f MatrixVectorRotate(const gvr::Mat4f& m, const gvr::Vec3f& v) { | |
170 gvr::Vec3f res; | |
171 res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z; | |
172 res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z; | |
173 res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z; | |
174 return res; | |
175 } | |
176 | |
177 gvr::Mat4f MatrixMul(const gvr::Mat4f& matrix1, const gvr::Mat4f& matrix2) { | |
178 gvr::Mat4f result; | |
179 for (int i = 0; i < 4; ++i) { | |
180 for (int j = 0; j < 4; ++j) { | |
181 result.m[i][j] = 0.0f; | |
182 for (int k = 0; k < 4; ++k) { | |
183 result.m[i][j] += matrix1.m[i][k] * matrix2.m[k][j]; | |
184 } | |
185 } | |
186 } | |
187 return result; | |
188 } | |
189 | |
190 gvr::Mat4f PerspectiveMatrixFromView(const gvr::Rectf& fov, | |
191 float z_near, | |
192 float z_far) { | |
193 gvr::Mat4f result; | |
194 const float x_left = -std::tan(fov.left * M_PI / 180.0f) * z_near; | |
195 const float x_right = std::tan(fov.right * M_PI / 180.0f) * z_near; | |
196 const float y_bottom = -std::tan(fov.bottom * M_PI / 180.0f) * z_near; | |
197 const float y_top = std::tan(fov.top * M_PI / 180.0f) * z_near; | |
198 | |
199 assert(x_left < x_right && y_bottom < y_top && z_near < z_far && | |
200 z_near > 0.0f && z_far > 0.0f); | |
201 const float X = (2 * z_near) / (x_right - x_left); | |
202 const float Y = (2 * z_near) / (y_top - y_bottom); | |
203 const float A = (x_right + x_left) / (x_right - x_left); | |
204 const float B = (y_top + y_bottom) / (y_top - y_bottom); | |
205 const float C = (z_near + z_far) / (z_near - z_far); | |
206 const float D = (2 * z_near * z_far) / (z_near - z_far); | |
207 | |
208 for (int i = 0; i < 4; ++i) { | |
209 for (int j = 0; j < 4; ++j) { | |
210 result.m[i][j] = 0.0f; | |
211 } | |
212 } | |
213 result.m[0][0] = X; | |
214 result.m[0][2] = A; | |
215 result.m[1][1] = Y; | |
216 result.m[1][2] = B; | |
217 result.m[2][2] = C; | |
218 result.m[2][3] = D; | |
219 result.m[3][2] = -1; | |
220 | |
221 return result; | |
222 } | |
223 | |
224 gvr::Rectf ModulateRect(const gvr::Rectf& rect, float width, float height) { | |
225 gvr::Rectf result = {rect.left * width, rect.right * width, | |
226 rect.bottom * height, rect.top * height}; | |
227 return result; | |
228 } | |
229 | |
230 gvr::Recti CalculatePixelSpaceRect(const gvr::Sizei& texture_size, | |
231 const gvr::Rectf& texture_rect) { | |
232 float width = static_cast<float>(texture_size.width); | |
233 float height = static_cast<float>(texture_size.height); | |
234 gvr::Rectf rect = ModulateRect(texture_rect, width, height); | |
235 gvr::Recti result = { | |
236 static_cast<int>(rect.left), static_cast<int>(rect.right), | |
237 static_cast<int>(rect.bottom), static_cast<int>(rect.top)}; | |
238 return result; | |
239 } | |
240 | |
241 gvr::Vec3f getForwardVector(const gvr::Mat4f& matrix) { | |
242 gvr::Vec3f forward; | |
243 float* fp = &forward.x; | |
244 // Same as multiplying the inverse of the rotation component of the matrix by | |
245 // (0, 0, -1, 0). | |
246 for (int i = 0; i < 3; ++i) { | |
247 fp[i] = -matrix.m[2][i]; | |
248 } | |
249 return forward; | |
250 } | |
251 | |
252 /** | |
253 * Provides the relative translation of the head as a 3x1 vector. | |
254 * | |
255 */ | |
256 gvr::Vec3f getTranslation(const gvr::Mat4f& matrix) { | |
257 gvr::Vec3f translation; | |
258 float* tp = &translation.x; | |
259 // Same as multiplying the matrix by (0, 0, 0, 1). | |
260 for (int i = 0; i < 3; ++i) { | |
261 tp[i] = matrix.m[i][3]; | |
262 } | |
263 return translation; | |
264 } | |
265 | |
266 GLuint CompileShader(GLenum shader_type, | |
267 const GLchar* shader_source, | |
268 std::string& error) { | |
269 GLuint shader_handle = glCreateShader(shader_type); | |
270 if (shader_handle != 0) { | |
271 // Pass in the shader source. | |
272 int len = strlen(shader_source); | |
273 glShaderSource(shader_handle, 1, &shader_source, &len); | |
274 // Compile the shader. | |
275 glCompileShader(shader_handle); | |
276 // Get the compilation status. | |
277 GLint status; | |
278 glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); | |
279 if (status == GL_FALSE) { | |
280 GLint info_log_length; | |
281 glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); | |
282 GLchar* str_info_log = new GLchar[info_log_length + 1]; | |
283 glGetShaderInfoLog(shader_handle, info_log_length, nullptr, str_info_log); | |
284 error = "Error compiling shader: "; | |
285 error += str_info_log; | |
286 delete[] str_info_log; | |
287 glDeleteShader(shader_handle); | |
288 shader_handle = 0; | |
289 } | |
290 } | |
291 | |
292 return shader_handle; | |
293 } | |
294 | |
295 GLuint CreateAndLinkProgram(GLuint vertext_shader_handle, | |
296 GLuint fragment_shader_handle, | |
297 int num_attributes, | |
298 const GLchar** attributes, | |
299 std::string& error) { | |
300 GLuint program_handle = glCreateProgram(); | |
301 | |
302 if (program_handle != 0) { | |
303 // Bind the vertex shader to the program. | |
304 glAttachShader(program_handle, vertext_shader_handle); | |
305 | |
306 // Bind the fragment shader to the program. | |
307 glAttachShader(program_handle, fragment_shader_handle); | |
308 | |
309 // Bind attributes. This is optional, no need to supply them if | |
310 // using glGetAttribLocation to look them up. Useful for a single | |
311 // vertex array object (VAO) that is used with multiple shaders. | |
312 if (attributes != nullptr) { | |
313 for (int i = 0; i < num_attributes; i++) { | |
314 glBindAttribLocation(program_handle, i, attributes[i]); | |
315 } | |
316 } | |
317 | |
318 // Link the two shaders together into a program. | |
319 glLinkProgram(program_handle); | |
320 | |
321 // Get the link status. | |
322 GLint link_status; | |
323 glGetProgramiv(program_handle, GL_LINK_STATUS, &link_status); | |
324 | |
325 // If the link failed, delete the program. | |
326 if (link_status == GL_FALSE) { | |
327 GLint info_log_length; | |
328 glGetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); | |
329 | |
330 GLchar* str_info_log = new GLchar[info_log_length + 1]; | |
331 glGetProgramInfoLog(program_handle, info_log_length, nullptr, | |
332 str_info_log); | |
333 error = "Error compiling program: "; | |
334 error += str_info_log; | |
335 delete[] str_info_log; | |
336 glDeleteProgram(program_handle); | |
337 program_handle = 0; | |
338 } | |
339 } | |
340 | |
341 return program_handle; | |
342 } | |
343 | |
344 float VectorLength(const gvr::Vec3f& vec) { | |
345 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); | |
346 } | |
347 | |
348 void NormalizeVector(gvr::Vec3f& vec) { | |
349 float len = VectorLength(vec); | |
350 vec.x /= len; | |
351 vec.y /= len; | |
352 vec.z /= len; | |
353 } | |
354 | |
355 float VectorDot(const gvr::Vec3f& a, const gvr::Vec3f& b) { | |
356 return a.x * b.x + a.y * b.y + a.z * b.z; | |
357 } | |
358 | |
359 void NormalizeQuat(gvr::Quatf& quat) { | |
360 float len = sqrt(quat.qx * quat.qx + quat.qy * quat.qy + quat.qz * quat.qz + | |
361 quat.qw * quat.qw); | |
362 quat.qx /= len; | |
363 quat.qy /= len; | |
364 quat.qz /= len; | |
365 quat.qw /= len; | |
366 } | |
367 | |
368 gvr::Quatf QuatFromAxisAngle(float x, float y, float z, float angle) { | |
369 gvr::Quatf res; | |
370 float s = sin(angle / 2); | |
371 res.qx = x * s; | |
372 res.qy = y * s; | |
373 res.qz = z * s; | |
374 res.qw = cos(angle / 2); | |
375 return res; | |
376 } | |
377 | |
378 gvr::Quatf QuatMultiply(const gvr::Quatf& a, const gvr::Quatf& b) { | |
379 gvr::Quatf res; | |
380 res.qw = a.qw * b.qw - a.qx * b.qx - a.qy * b.qy - a.qz * b.qz; | |
381 res.qx = a.qw * b.qx + a.qx * b.qw + a.qy * b.qz - a.qz * b.qy; | |
382 res.qy = a.qw * b.qy - a.qx * b.qz + a.qy * b.qw + a.qz * b.qx; | |
383 res.qz = a.qw * b.qz + a.qx * b.qy - a.qy * b.qx + a.qz * b.qw; | |
384 return res; | |
385 } | |
386 | |
387 gvr::Mat4f QuatToMatrix(const gvr::Quatf& quat) { | |
388 const float x2 = quat.qx * quat.qx; | |
389 const float y2 = quat.qy * quat.qy; | |
390 const float z2 = quat.qz * quat.qz; | |
391 const float xy = quat.qx * quat.qy; | |
392 const float xz = quat.qx * quat.qz; | |
393 const float xw = quat.qx * quat.qw; | |
394 const float yz = quat.qy * quat.qz; | |
395 const float yw = quat.qy * quat.qw; | |
396 const float zw = quat.qz * quat.qw; | |
397 | |
398 const float m11 = 1.0f - 2.0f * y2 - 2.0f * z2; | |
399 const float m12 = 2.0f * (xy - zw); | |
400 const float m13 = 2.0f * (xz + yw); | |
401 const float m21 = 2.0f * (xy + zw); | |
402 const float m22 = 1.0f - 2.0f * x2 - 2.0f * z2; | |
403 const float m23 = 2.0f * (yz - xw); | |
404 const float m31 = 2.0f * (xz - yw); | |
405 const float m32 = 2.0f * (yz + xw); | |
406 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2; | |
407 | |
408 float ret[16] = {m11, m12, m13, 0.0f, m21, m22, m23, 0.0f, | |
409 m31, m32, m33, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}; | |
410 | |
411 return *((gvr::Mat4f*)&ret); | |
412 } | |
413 | |
414 } // namespace vr_shell | |
OLD | NEW |