Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: native_client_sdk/src/examples/hello_world_instance3d/matrix.cc

Issue 15011003: ppapi_simple (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Copyright Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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,
17 GLfloat left,
18 GLfloat right,
19 GLfloat bottom,
20 GLfloat top,
21 GLfloat znear,
22 GLfloat zfar) {
23 float temp, temp2, temp3, temp4;
24 temp = 2.0f * znear;
25 temp2 = right - left;
26 temp3 = top - bottom;
27 temp4 = zfar - znear;
28 mat[0] = temp / temp2;
29 mat[1] = 0.0f;
30 mat[2] = 0.0f;
31 mat[3] = 0.0f;
32 mat[4] = 0.0f;
33 mat[5] = temp / temp3;
34 mat[6] = 0.0f;
35 mat[7] = 0.0f;
36 mat[8] = (right + left) / temp2;
37 mat[9] = (top + bottom) / temp3;
38 mat[10] = (-zfar - znear) / temp4;
39 mat[11] = -1.0f;
40 mat[12] = 0.0f;
41 mat[13] = 0.0f;
42 mat[14] = (-temp * zfar) / temp4;
43 mat[15] = 0.0f;
44 }
45
46 void glhPerspectivef2(Matrix_t mat,
47 GLfloat fovyInDegrees,
48 GLfloat aspectRatio,
49 GLfloat znear,
50 GLfloat zfar) {
51 float ymax, xmax;
52 ymax = znear * tanf(fovyInDegrees * 3.14f / 360.0f);
53 xmax = ymax * aspectRatio;
54 glhFrustumf2(mat, -xmax, xmax, -ymax, ymax, znear, zfar);
55 }
56
57 void identity_matrix(Matrix_t mat) {
58 memset(mat, 0, sizeof(Matrix_t));
59 mat[0] = 1.0;
60 mat[5] = 1.0;
61 mat[10] = 1.0;
62 mat[15] = 1.0;
63 }
64
65 void multiply_matrix(const Matrix_t a, const Matrix_t b, Matrix_t mat) {
66 // Generate to a temporary first in case the output matrix and input
67 // matrix are the same.
68 Matrix_t out;
69
70 out[0] = a[0] * b[0] + a[4] * b[1] + a[8] * b[2] + a[12] * b[3];
71 out[1] = a[1] * b[0] + a[5] * b[1] + a[9] * b[2] + a[13] * b[3];
72 out[2] = a[2] * b[0] + a[6] * b[1] + a[10] * b[2] + a[14] * b[3];
73 out[3] = a[3] * b[0] + a[7] * b[1] + a[11] * b[2] + a[15] * b[3];
74
75 out[4] = a[0] * b[4] + a[4] * b[5] + a[8] * b[6] + a[12] * b[7];
76 out[5] = a[1] * b[4] + a[5] * b[5] + a[9] * b[6] + a[13] * b[7];
77 out[6] = a[2] * b[4] + a[6] * b[5] + a[10] * b[6] + a[14] * b[7];
78 out[7] = a[3] * b[4] + a[7] * b[5] + a[11] * b[6] + a[15] * b[7];
79
80 out[8] = a[0] * b[8] + a[4] * b[9] + a[8] * b[10] + a[12] * b[11];
81 out[9] = a[1] * b[8] + a[5] * b[9] + a[9] * b[10] + a[13] * b[11];
82 out[10] = a[2] * b[8] + a[6] * b[9] + a[10] * b[10] + a[14] * b[11];
83 out[11] = a[3] * b[8] + a[7] * b[9] + a[11] * b[10] + a[15] * b[11];
84
85 out[12] = a[0] * b[12] + a[4] * b[13] + a[8] * b[14] + a[12] * b[15];
86 out[13] = a[1] * b[12] + a[5] * b[13] + a[9] * b[14] + a[13] * b[15];
87 out[14] = a[2] * b[12] + a[6] * b[13] + a[10] * b[14] + a[14] * b[15];
88 out[15] = a[3] * b[12] + a[7] * b[13] + a[11] * b[14] + a[15] * b[15];
89
90 memcpy(mat, out, sizeof(Matrix_t));
91 }
92
93 void rotate_x_matrix(GLfloat x_rad, Matrix_t mat) {
94 identity_matrix(mat);
95 mat[5] = cosf(x_rad);
96 mat[6] = -sinf(x_rad);
97 mat[9] = -mat[6];
98 mat[10] = mat[5];
99 }
100
101 void rotate_y_matrix(GLfloat y_rad, Matrix_t mat) {
102 identity_matrix(mat);
103 mat[0] = cosf(y_rad);
104 mat[2] = sinf(y_rad);
105 mat[8] = -mat[2];
106 mat[10] = mat[0];
107 }
108
109 void rotate_z_matrix(GLfloat z_rad, Matrix_t mat) {
110 identity_matrix(mat);
111 mat[0] = cosf(z_rad);
112 mat[1] = sinf(z_rad);
113 mat[4] = -mat[1];
114 mat[5] = mat[0];
115 }
116
117 void rotate_matrix(GLfloat x_deg, GLfloat y_deg, GLfloat z_deg, Matrix_t mat) {
118 GLfloat x_rad = (GLfloat) deg_to_rad(x_deg);
119 GLfloat y_rad = (GLfloat) deg_to_rad(y_deg);
120 GLfloat z_rad = (GLfloat) deg_to_rad(z_deg);
121
122 Matrix_t x_matrix;
123 Matrix_t y_matrix;
124 Matrix_t z_matrix;
125
126 rotate_x_matrix(x_rad, x_matrix);
127 rotate_y_matrix(y_rad, y_matrix);
128 rotate_z_matrix(z_rad, z_matrix);
129
130 Matrix_t xy_matrix;
131 multiply_matrix(y_matrix, x_matrix, xy_matrix);
132 multiply_matrix(z_matrix, xy_matrix, mat);
133 }
134
135 void translate_matrix(GLfloat x, GLfloat y, GLfloat z, Matrix_t mat) {
136 identity_matrix(mat);
137 mat[12] += x;
138 mat[13] += y;
139 mat[14] += z;
140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698