Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / examples / api / graphics_3d / matrix.cc
blob25778e5fa3a03d12efe0fd75ce8495e722c74875
1 // Copyright (c) 2012 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.
5 /** @file matrix.cc
6 * Implements simple matrix manipulation functions.
7 */
9 //-----------------------------------------------------------------------------
10 #include <stdlib.h>
11 #include <string.h>
12 #include "matrix.h"
13 #define deg_to_rad(x) (x * (M_PI / 180.0f))
15 void glhFrustumf2(Matrix_t mat,
16 GLfloat left,
17 GLfloat right,
18 GLfloat bottom,
19 GLfloat top,
20 GLfloat znear,
21 GLfloat zfar) {
22 float temp, temp2, temp3, temp4;
23 temp = 2.0f * znear;
24 temp2 = right - left;
25 temp3 = top - bottom;
26 temp4 = zfar - znear;
27 mat[0] = temp / temp2;
28 mat[1] = 0.0f;
29 mat[2] = 0.0f;
30 mat[3] = 0.0f;
31 mat[4] = 0.0f;
32 mat[5] = temp / temp3;
33 mat[6] = 0.0f;
34 mat[7] = 0.0f;
35 mat[8] = (right + left) / temp2;
36 mat[9] = (top + bottom) / temp3;
37 mat[10] = (-zfar - znear) / temp4;
38 mat[11] = -1.0f;
39 mat[12] = 0.0f;
40 mat[13] = 0.0f;
41 mat[14] = (-temp * zfar) / temp4;
42 mat[15] = 0.0f;
45 void glhPerspectivef2(Matrix_t mat,
46 GLfloat fovyInDegrees,
47 GLfloat aspectRatio,
48 GLfloat znear,
49 GLfloat zfar) {
50 float ymax, xmax;
51 ymax = znear * tanf(fovyInDegrees * 3.14f / 360.0f);
52 xmax = ymax * aspectRatio;
53 glhFrustumf2(mat, -xmax, xmax, -ymax, ymax, znear, zfar);
56 void identity_matrix(Matrix_t mat) {
57 memset(mat, 0, sizeof(Matrix_t));
58 mat[0] = 1.0;
59 mat[5] = 1.0;
60 mat[10] = 1.0;
61 mat[15] = 1.0;
64 void multiply_matrix(const Matrix_t a, const Matrix_t b, Matrix_t mat) {
65 // Generate to a temporary first in case the output matrix and input
66 // matrix are the same.
67 Matrix_t out;
69 out[0] = a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3];
70 out[1] = a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3];
71 out[2] = a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3];
72 out[3] = a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3];
74 out[4] = a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7];
75 out[5] = a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7];
76 out[6] = a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7];
77 out[7] = a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7];
79 out[8] = a[0] * b[8] + a[4] * b[9] + a[8] * b[10] + a[12] * b[11];
80 out[9] = a[1] * b[8] + a[5] * b[9] + a[9] * b[10] + a[13] * b[11];
81 out[10] = a[2] * b[8] + a[6] * b[9] + a[10] * b[10] + a[14] * b[11];
82 out[11] = a[3] * b[8] + a[7] * b[9] + a[11] * b[10] + a[15] * b[11];
84 out[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14] + a[12] * b[15];
85 out[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14] + a[13] * b[15];
86 out[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
87 out[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];
89 memcpy(mat, out, sizeof(Matrix_t));
92 void rotate_x_matrix(GLfloat x_rad, Matrix_t mat) {
93 identity_matrix(mat);
94 mat[5] = cosf(x_rad);
95 mat[6] = -sinf(x_rad);
96 mat[9] = -mat[6];
97 mat[10] = mat[5];
100 void rotate_y_matrix(GLfloat y_rad, Matrix_t mat) {
101 identity_matrix(mat);
102 mat[0] = cosf(y_rad);
103 mat[2] = sinf(y_rad);
104 mat[8] = -mat[2];
105 mat[10] = mat[0];
108 void rotate_z_matrix(GLfloat z_rad, Matrix_t mat) {
109 identity_matrix(mat);
110 mat[0] = cosf(z_rad);
111 mat[1] = sinf(z_rad);
112 mat[4] = -mat[1];
113 mat[5] = mat[0];
116 void rotate_matrix(GLfloat x_deg, GLfloat y_deg, GLfloat z_deg, Matrix_t mat) {
117 GLfloat x_rad = (GLfloat) deg_to_rad(x_deg);
118 GLfloat y_rad = (GLfloat) deg_to_rad(y_deg);
119 GLfloat z_rad = (GLfloat) deg_to_rad(z_deg);
121 Matrix_t x_matrix;
122 Matrix_t y_matrix;
123 Matrix_t z_matrix;
125 rotate_x_matrix(x_rad, x_matrix);
126 rotate_y_matrix(y_rad, y_matrix);
127 rotate_z_matrix(z_rad, z_matrix);
129 Matrix_t xy_matrix;
130 multiply_matrix(y_matrix, x_matrix, xy_matrix);
131 multiply_matrix(z_matrix, xy_matrix, mat);
134 void translate_matrix(GLfloat x, GLfloat y, GLfloat z, Matrix_t mat) {
135 identity_matrix(mat);
136 mat[12] += x;
137 mat[13] += y;
138 mat[14] += z;