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