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.
4
5/** @file matrix.cc
6 * Implements simple matrix manipulation functions.
7 */
8
9//-----------------------------------------------------------------------------
10#include <stdlib.h>
11#include <string.h>
12#include "matrix.h"
13#define deg_to_rad(x) (x * (M_PI / 180.0f))
14
15void 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;
43}
44
45void 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);
54}
55
56void 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;
62}
63
64void 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;
68
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];
73
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];
78
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];
83
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];
88
89  memcpy(mat, out, sizeof(Matrix_t));
90}
91
92void 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];
98}
99
100void 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];
106}
107
108void 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];
114}
115
116void 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);
120
121  Matrix_t x_matrix;
122  Matrix_t y_matrix;
123  Matrix_t z_matrix;
124
125  rotate_x_matrix(x_rad, x_matrix);
126  rotate_y_matrix(y_rad, y_matrix);
127  rotate_z_matrix(z_rad, z_matrix);
128
129  Matrix_t xy_matrix;
130  multiply_matrix(y_matrix, x_matrix, xy_matrix);
131  multiply_matrix(z_matrix, xy_matrix, mat);
132}
133
134void 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;
139}
140