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.
6 * Implements simple matrix manipulation functions.
9 //-----------------------------------------------------------------------------
13 #define deg_to_rad(x) (x * (M_PI / 180.0f))
15 void glhFrustumf2(Matrix_t mat
,
22 float temp
, temp2
, temp3
, temp4
;
27 mat
[0] = temp
/ temp2
;
32 mat
[5] = temp
/ temp3
;
35 mat
[8] = (right
+ left
) / temp2
;
36 mat
[9] = (top
+ bottom
) / temp3
;
37 mat
[10] = (-zfar
- znear
) / temp4
;
41 mat
[14] = (-temp
* zfar
) / temp4
;
45 void glhPerspectivef2(Matrix_t mat
,
46 GLfloat fovyInDegrees
,
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
));
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.
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
) {
95 mat
[6] = -sinf(x_rad
);
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
);
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
);
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
);
125 rotate_x_matrix(x_rad
, x_matrix
);
126 rotate_y_matrix(y_rad
, y_matrix
);
127 rotate_z_matrix(z_rad
, z_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
);