~ubuntu-branches/ubuntu/saucy/mupen64plus-video-rice/saucy

« back to all changes in this revision

Viewing changes to src/VectorMath.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2011-01-22 11:05:28 UTC
  • Revision ID: james.westby@ubuntu.com-20110122110528-k6z84gdespqqd9zp
Tags: upstream-1.99.4
ImportĀ upstreamĀ versionĀ 1.99.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
2
 *   Mupen64plus - VectorMath.cpp                                          *
 
3
 *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
 
4
 *   Copyright (C) 2002 Rice1964                                           *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU General Public License as published by  *
 
8
 *   the Free Software Foundation; either version 2 of the License, or     *
 
9
 *   (at your option) any later version.                                   *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU General Public License     *
 
17
 *   along with this program; if not, write to the                         *
 
18
 *   Free Software Foundation, Inc.,                                       *
 
19
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 
20
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
21
 
 
22
#include <string.h>
 
23
 
 
24
#include "VectorMath.h"
 
25
 
 
26
//---------- XMATRIX
 
27
 
 
28
XMATRIX::XMATRIX()
 
29
{
 
30
}
 
31
 
 
32
XMATRIX::XMATRIX( const float *pIn )
 
33
{
 
34
   memcpy(m, pIn, 16*4);
 
35
}
 
36
 
 
37
XMATRIX::XMATRIX( const MATRIX &pIn )
 
38
{
 
39
   memcpy(m, pIn.m, 16*4);
 
40
}
 
41
 
 
42
XMATRIX::XMATRIX( float _11, float _12, float _13, float _14,
 
43
              float _21, float _22, float _23, float _24,
 
44
              float _31, float _32, float _33, float _34,
 
45
              float _41, float _42, float _43, float _44 )
 
46
{
 
47
   this->_11 = _11;
 
48
   this->_12 = _12;
 
49
   this->_13 = _13;
 
50
   this->_14 = _14;
 
51
   this->_21 = _21;
 
52
   this->_22 = _22;
 
53
   this->_23 = _23;
 
54
   this->_24 = _24;
 
55
   this->_31 = _31;
 
56
   this->_32 = _32;
 
57
   this->_33 = _33;
 
58
   this->_34 = _34;
 
59
   this->_41 = _41;
 
60
   this->_42 = _42;
 
61
   this->_43 = _43;
 
62
   this->_44 = _44;
 
63
}
 
64
 
 
65
float& XMATRIX::operator () ( unsigned int Row, unsigned int Col )
 
66
{
 
67
   return m[Row][Col];
 
68
}
 
69
 
 
70
float  XMATRIX::operator () ( unsigned int Row, unsigned int Col ) const
 
71
{
 
72
   return m[Row][Col];
 
73
}
 
74
 
 
75
XMATRIX::operator float* ()
 
76
{
 
77
   return (float*)m;
 
78
}
 
79
 
 
80
XMATRIX::operator const float* () const
 
81
{
 
82
   return (float*)m;
 
83
}
 
84
 
 
85
XMATRIX& XMATRIX::operator *= ( const XMATRIX &pIn )
 
86
{
 
87
   XMATRIX mTemp(*this);
 
88
   *this = mTemp*pIn;
 
89
   return *this;
 
90
}
 
91
 
 
92
XMATRIX& XMATRIX::operator += ( const XMATRIX &pIn )
 
93
{
 
94
   XMATRIX mTemp(*this);
 
95
   *this = mTemp+pIn;
 
96
   return *this;
 
97
}
 
98
 
 
99
XMATRIX& XMATRIX::operator -= ( const XMATRIX &pIn )
 
100
{
 
101
   XMATRIX mTemp(*this);
 
102
   *this = mTemp-pIn;
 
103
   return *this;
 
104
}
 
105
 
 
106
XMATRIX& XMATRIX::operator *= ( float f)
 
107
{
 
108
   for (int i=0; i<16; i++) ((float*)m)[i] *= f;
 
109
   return *this;
 
110
}
 
111
 
 
112
XMATRIX& XMATRIX::operator /= ( float f)
 
113
{
 
114
   for (int i=0; i<16; i++) ((float*)m)[i] /= f;
 
115
   return *this;
 
116
}
 
117
 
 
118
XMATRIX XMATRIX::operator + () const
 
119
{
 
120
   return *this;
 
121
}
 
122
 
 
123
XMATRIX XMATRIX::operator - () const
 
124
{
 
125
   XMATRIX mTemp;
 
126
   for (int i=0; i<16; i++) ((float*)mTemp.m)[i] = -((float*)m)[i];
 
127
   return mTemp;
 
128
}
 
129
 
 
130
XMATRIX XMATRIX::operator * ( const XMATRIX &pIn ) const
 
131
{
 
132
   XMATRIX mTemp;
 
133
   for (int i=0; i<4; i++)
 
134
     for (int j=0; j<4; j++)
 
135
       mTemp.m[i][j] = m[i][0]*pIn.m[0][j] +
 
136
                       m[i][1]*pIn.m[1][j] +
 
137
                       m[i][2]*pIn.m[2][j] +
 
138
                       m[i][3]*pIn.m[3][j];
 
139
   return mTemp;
 
140
}
 
141
 
 
142
XMATRIX XMATRIX::operator + ( const XMATRIX &pIn ) const
 
143
{
 
144
   XMATRIX mTemp;
 
145
   for (int i=0; i<16; i++)
 
146
     ((float*)mTemp.m)[i] = ((float*)m)[i] + ((float*)pIn.m)[i];
 
147
   return mTemp;
 
148
}
 
149
 
 
150
XMATRIX XMATRIX::operator - ( const XMATRIX &pIn ) const
 
151
{
 
152
   XMATRIX mTemp;
 
153
   for (int i=0; i<16; i++)
 
154
     ((float*)mTemp.m)[i] = ((float*)m)[i] - ((float*)pIn.m)[i];
 
155
   return mTemp;
 
156
}
 
157
 
 
158
/*
 
159
    XMATRIX operator * ( float ) const;
 
160
    XMATRIX operator / ( float ) const;
 
161
    friend XMATRIX operator * ( float, const XMATRIX & );
 
162
    bool operator == ( const XMATRIX & ) const;
 
163
    bool operator != ( const XMATRIX & ) const;
 
164
*/
 
165
 
 
166
//---------- VECTOR3
 
167
 
 
168
XVECTOR3::XVECTOR3()
 
169
{
 
170
}
 
171
 
 
172
XVECTOR3::XVECTOR3( const float *f )
 
173
{
 
174
   x = f[0];
 
175
   y = f[1];
 
176
   z = f[2];
 
177
}
 
178
 
 
179
XVECTOR3::XVECTOR3( const VECTOR3 &v )
 
180
{
 
181
   x = v.x;
 
182
   y = v.y;
 
183
   z = v.z;
 
184
}
 
185
 
 
186
XVECTOR3::XVECTOR3( float _x, float _y, float _z )
 
187
{
 
188
   x = _x;
 
189
   y = _y;
 
190
   z = _z;
 
191
}
 
192
 
 
193
/*
 
194
    // casting
 
195
    inline operator float* ();
 
196
    inline operator const float* () const;
 
197
 
 
198
    // assignment operators
 
199
    inline XVECTOR3& operator += ( const XVECTOR3 &op );
 
200
    inline XVECTOR3& operator -= ( const XVECTOR3 &op );
 
201
    inline XVECTOR3& operator *= ( float op );
 
202
        inline XVECTOR3& operator /= ( float op );
 
203
 
 
204
    // unary operators
 
205
    inline XVECTOR3 operator + () const;
 
206
    inline XVECTOR3 operator - () const;
 
207
 
 
208
    // binary operators
 
209
        inline XVECTOR3 operator + ( const XVECTOR3 &op ) const;
 
210
    inline XVECTOR3 operator - ( const XVECTOR3 &op ) const;
 
211
    inline XVECTOR3 operator * ( float op ) const;
 
212
    inline XVECTOR3 operator / ( float op ) const;
 
213
 
 
214
 
 
215
    friend XVECTOR3 operator * ( float, const XVECTOR3& );
 
216
 
 
217
    inline bool operator == ( const XVECTOR3 &op ) const;
 
218
    inline bool operator != ( const XVECTOR3 &op ) const;
 
219
*/
 
220
 
 
221
//---------- XVECTOR4
 
222
 
 
223
XVECTOR4::XVECTOR4()
 
224
{
 
225
}
 
226
 
 
227
/*
 
228
    XVECTOR4( const float *f );
 
229
    XVECTOR4( const VECTOR4 &v );
 
230
    XVECTOR4( float _x, float _y, float _z, float _w );
 
231
 
 
232
    // casting
 
233
        inline operator float* ();
 
234
    inline operator const float* () const;
 
235
 
 
236
    // assignment operators
 
237
    inline XVECTOR4& operator += ( const XVECTOR4 &op );
 
238
    inline XVECTOR4& operator -= ( const XVECTOR4 &op );
 
239
    inline XVECTOR4& operator *= ( float op );
 
240
    inline XVECTOR4& operator /= ( float op );
 
241
 
 
242
    // unary operators
 
243
    inline XVECTOR4 operator + () const;
 
244
    inline XVECTOR4 operator - () const;
 
245
 
 
246
    // binary operators
 
247
    inline XVECTOR4 operator + ( const XVECTOR4 &op ) const;
 
248
    inline XVECTOR4 operator - ( const XVECTOR4 &op ) const;
 
249
    inline XVECTOR4 operator * ( float op ) const;
 
250
    inline XVECTOR4 operator / ( float op ) const;
 
251
 
 
252
    friend XVECTOR4 operator * ( float, const XVECTOR4& );
 
253
 
 
254
    inline bool operator == ( const XVECTOR4 &op ) const;
 
255
    inline bool operator != ( const XVECTOR4 &op ) const;
 
256
*/
 
257
 
 
258
//---------- OTHER
 
259
 
 
260
XMATRIX* MatrixTranspose(
 
261
                XMATRIX* pOut,
 
262
                const XMATRIX* pM
 
263
                )
 
264
{
 
265
   pOut->_11 = pM->_11;
 
266
   pOut->_12 = pM->_21;
 
267
   pOut->_13 = pM->_31;
 
268
   pOut->_14 = pM->_41;
 
269
   pOut->_21 = pM->_12;
 
270
   pOut->_22 = pM->_22;
 
271
   pOut->_23 = pM->_32;
 
272
   pOut->_24 = pM->_42;
 
273
   pOut->_31 = pM->_13;
 
274
   pOut->_32 = pM->_23;
 
275
   pOut->_33 = pM->_33;
 
276
   pOut->_34 = pM->_43;
 
277
   pOut->_41 = pM->_14;
 
278
   pOut->_42 = pM->_24;
 
279
   pOut->_43 = pM->_34;
 
280
   pOut->_44 = pM->_44;
 
281
   return pOut;
 
282
}
 
283
 
 
284
XVECTOR4 Vec3Transform(XVECTOR4 *pOut, const XVECTOR3 *pV, const XMATRIX *pM)
 
285
{
 
286
   pOut->x = pV->x*pM->_11 + pV->y*pM->_21 + pV->z*pM->_31 + pM->_41;
 
287
   pOut->y = pV->x*pM->_12 + pV->y*pM->_22 + pV->z*pM->_32 + pM->_42;
 
288
   pOut->z = pV->x*pM->_13 + pV->y*pM->_23 + pV->z*pM->_33 + pM->_43;
 
289
   pOut->w = pV->x*pM->_14 + pV->y*pM->_24 + pV->z*pM->_34 + pM->_44;
 
290
   return *pOut;
 
291
}
 
292