Line data Source code
1 : //////////////////////////////////////////////////////////////////
2 : // Description : Functions to handle matrix multiplication for //
3 : // object transformations. //
4 : //////////////////////////////////////////////////////////////////
5 : #include "Matrix.h"
6 : #include "constants.h"
7 :
8 : //------------------------------------------------------------------------------
9 262 : Matrix::Matrix() {
10 131 : loadIdentity();
11 262 : }
12 :
13 : //------------------------------------------------------------------------------
14 4 : Matrix::Matrix( const Matrix &t ) {
15 34 : for ( int i = 0; i < NUM_CELLS; ++i )
16 32 : data[i] = t.data[i];
17 4 : }
18 :
19 : //------------------------------------------------------------------------------
20 2 : Matrix::Matrix(
21 : const float &c11, const float &c21, const float &c31, const float &c41,
22 : const float &c12, const float &c22, const float &c32, const float &c42,
23 : const float &c13, const float &c23, const float &c33, const float &c43,
24 : const float &c14, const float &c24, const float &c34, const float &c44
25 1 : ) {
26 1 : data[0] = c11; data[1] = c12; data[2] = c13; data[3] = c14;
27 1 : data[4] = c21; data[5] = c22; data[6] = c23; data[7] = c24;
28 1 : data[8] = c31; data[9] = c32; data[10] = c33; data[11] = c34;
29 1 : data[12] = c41; data[13] = c42; data[14] = c43; data[15] = c44;
30 2 : }
31 :
32 : //------------------------------------------------------------------------------
33 152 : void Matrix::loadIdentity() {
34 2584 : for ( int i = 0; i < NUM_CELLS; ++i )
35 2432 : data[i] = 0;
36 :
37 152 : data[0] = 1;
38 152 : data[5] = 1;
39 152 : data[10] = 1;
40 152 : data[15] = 1;
41 152 : }
42 :
43 : //------------------------------------------------------------------------------
44 1 : void Matrix::loadZero() {
45 17 : for ( int i = 0; i < NUM_CELLS; ++i )
46 16 : data[i] = 0;
47 1 : }
48 :
49 : //------------------------------------------------------------------------------
50 13 : void Matrix::operator =(const Matrix &t) {
51 221 : for ( int i = 0; i < NUM_CELLS; ++i )
52 208 : data[i] = t.data[i];
53 13 : }
54 :
55 : //------------------------------------------------------------------------------
56 12 : Matrix Matrix::operator *(const Matrix &p) {
57 12 : Matrix t; int c, r;
58 :
59 204 : for ( int i = 0; i < NUM_CELLS; ++i ) {
60 192 : t.data[i] = 0.0f;
61 :
62 192 : c = (int) i / 4; r = i % 4;
63 960 : for ( int j = 0; j < 4; j++ )
64 768 : t.data[i] += ( p.data[(c*4)+j] * data[ (j*4) + r ] );
65 192 : }
66 :
67 12 : return (t);
68 : }
69 :
70 : //------------------------------------------------------------------------------
71 9 : void Matrix::setTranslation(const float &x, const float &y, const float &z) {
72 9 : loadIdentity();
73 :
74 9 : data[12] = x;
75 9 : data[13] = y;
76 9 : data[14] = z;
77 9 : }
78 :
79 : //------------------------------------------------------------------------------
80 2 : void Matrix::setScale(const float &x, const float &y, const float &z) {
81 2 : loadIdentity();
82 :
83 2 : data[0] = x;
84 2 : data[5] = y;
85 2 : data[10] = z;
86 2 : }
87 :
88 : //------------------------------------------------------------------------------
89 0 : void Matrix::transpose(Matrix &m) {
90 0 : for ( int j = 0; j < 3; j++ ) {
91 0 : for (int i = 0; i < (4-j); ++i) {
92 0 : m.data[(i*4)+j] = data[15-(j*4)-i];
93 0 : m.data[15-(j*4)-i] = data[(i*4)+j];
94 0 : }
95 0 : }
96 0 : }
97 :
98 : //------------------------------------------------------------------------------
99 2 : void Matrix::transformVertex(float *v, float *newVertex) {
100 : float h, x, y, z;
101 :
102 2 : x = ( v[0] * data[0] ) + ( v[1] * data[4] ) + ( v[2] * data[8] ) + data[12];
103 2 : y = ( v[0] * data[1] ) + ( v[1] * data[5] ) + ( v[2] * data[9] ) + data[13];
104 2 : z = ( v[0] * data[2] ) + ( v[1] * data[6] ) + ( v[2] * data[10] ) + data[14];
105 2 : h = ( v[0] * data[3] ) + ( v[1] * data[7] ) + ( v[2] * data[11] ) + data[15];
106 :
107 2 : newVertex[0] = x; newVertex[1] = y; newVertex[2] = z;
108 :
109 2 : newVertex[0] /= h;
110 2 : newVertex[1] /= h;
111 2 : newVertex[2] /= h;
112 2 : }
113 :
114 : //------------------------------------------------------------------------------
115 0 : void Matrix::setShadow(float lightPos[4], float plane[4]) {
116 0 : float temp = plane[0] * lightPos[0] +
117 0 : plane[1] * lightPos[1] +
118 0 : plane[1] * lightPos[2] +
119 0 : plane[3] * lightPos[3];
120 :
121 0 : data[0] = temp - lightPos[0] * plane[0];
122 0 : data[4] = 0.0f - lightPos[0] * plane[1];
123 0 : data[8] = 0.0f - lightPos[0] * plane[2];
124 0 : data[12] = 0.0f - lightPos[0] * plane[3];
125 :
126 0 : data[1] = 0.0f - lightPos[1] * plane[0];
127 0 : data[5] = temp - lightPos[1] * plane[1];
128 0 : data[9] = 0.0f - lightPos[1] * plane[2];
129 0 : data[13] = 0.0f - lightPos[1] * plane[3];
130 :
131 0 : data[2] = 0.0f - lightPos[2] * plane[0];
132 0 : data[6] = 0.0f - lightPos[2] * plane[1];
133 0 : data[10] = temp - lightPos[2] * plane[2];
134 0 : data[14] = 0.0f - lightPos[2] * plane[3];
135 :
136 0 : data[3] = 0.0f - lightPos[3] * plane[0];
137 0 : data[7] = 0.0f - lightPos[3] * plane[1];
138 0 : data[11] = 0.0f - lightPos[3] * plane[2];
139 0 : data[15] = temp - lightPos[3] * plane[3];
140 0 : }
141 :
142 : //------------------------------------------------------------------------------
143 0 : void Matrix::display() {
144 : #if DEBUG
145 : PRINT_DEBUG("Matrix dump:\n");
146 : for ( int i = 0; i < NUM_CELLS; ++i ) {
147 : PRINT_DEBUG("%.3f\t", data[i] );
148 : if ( (i + 1) % 4 == 0 )
149 : PRINT_DEBUG( "\n" );
150 : }
151 : #endif
152 0 : }
|