-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMisc.cpp
More file actions
499 lines (428 loc) · 15.1 KB
/
Misc.cpp
File metadata and controls
499 lines (428 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//=================================================================================================================================
//
// Author: Maurice Ribble
// 3D Application Research Group
// ATI Research, Inc.
//
// Various helpful functions that are project specific.
//
//=================================================================================================================================
// $Id: //depot/3darg/Tools/Handheld/esTestApps/esTriangle/src/Misc.cpp#1 $
//
// Last check-in: $DateTime: 2008/01/23 11:45:40 $
// Last edited by: $Author: dginsbur $
//=================================================================================================================================
// (C) ATI Research, Inc. 2006 All rights reserved.
//=================================================================================================================================
#include "SimpleImageLoader.h"
#include "Misc.h"
#include "assert.h"
#include "comm1.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GLES2/gl2.h> //#include <GLES/gl.h>
// Global holding all the global egl data
//EglStruct g_egl;
//=================================================================================================================================
///
/// Builds the egl attrib list
///
/// \param attribList - The egl attrib list
///
/// \return void
//=================================================================================================================================
#define BUFSIZ 256
//=================================================================================================================================
///
/// Sets up the textures including compression (located here for convienience.
///
/// \param filename - The file name of the texture to be loaded
/// \param texUnit - The texture unit we want to use for this texture
///
/// \return - The ES texture bind number
//=================================================================================================================================
GLuint LoadCompressedTexture( const char8 *filename, const char8 *sType, GLuint texUnit )
{
assert( 0 );
return 0;
}
/*!***************************************************************************
@Function Name PVRTMatrixTranslationF
@Output mOut Translation matrix
@Input fX X component of the translation
@Input fY Y component of the translation
@Input fZ Z component of the translation
@Description Build a transaltion matrix mOut using fX, fY and fZ.
*****************************************************************************/
void myMatrixTranslationF(
float *mOut,
const float fX,
const float fY,
const float fZ)
{
mOut[ 0]=1.0f; mOut[ 4] =0.0f; mOut[8]=0.0f; mOut[12]=fX;
mOut[ 1]=0.0f; mOut[ 5] =1.0f; mOut[9]=0.0f; mOut[13]=fY;
mOut[ 2]=0.0f; mOut[ 6] =0.0f; mOut[10]=1.0f; mOut[14]=fZ;
mOut[ 3]=0.0f; mOut[ 7] =0.0f; mOut[11]=0.0f; mOut[15]=1.0f;
}
/*!***************************************************************************
@Function Name PVRTMatrixRotationYF
@Output mOut Rotation matrix
@Input fAngle Angle of the rotation
@Description Create an Y rotation matrix mOut.
*****************************************************************************/
void myMatrixRotationYF(
float* mOut,
const float fAngle)
{
float fCosine, fSine;
/* Precompute cos and sin */
#if defined(BUILD_DX9) || defined(BUILD_D3DM) || defined(BUILD_DX10)
fCosine = (float)PVRTFCOS(-fAngle);
fSine = (float)PVRTFSIN(-fAngle);
#else
fCosine = (float)cos(fAngle);
fSine = (float)sin(fAngle);
#endif
/* Create the trigonometric matrix corresponding to Y Rotation */
mOut[ 0]=fCosine; mOut[4] =0.0f; mOut[8]=-fSine; mOut[12] =0.0f;
mOut[ 1]=0.0f; mOut[5] =1.0f; mOut[9]=0.0f; mOut[13] =0.0f;
mOut[ 2]=fSine; mOut[6] =0.0f; mOut[10]=fCosine; mOut[14] =0.0f;
mOut[ 3]=0.0f; mOut[7] =0.0f; mOut[11]=0.0f; mOut[15] =1.0f;
}
/*!***************************************************************************
@Function Name PVRTMatrixRotationZF
@Output mOut Rotation matrix
@Input fAngle Angle of the rotation
@Description Create an Z rotation matrix mOut.
*****************************************************************************/
void myMatrixRotationZF(
float * mOut,
const float fAngle)
{
float fCosine, fSine;
/* Precompute cos and sin */
#if defined(BUILD_DX9) || defined(BUILD_D3DM) || defined(BUILD_DX10)
fCosine = (float)PVRTFCOS(-fAngle);
fSine = (float)PVRTFSIN(-fAngle);
#else
fCosine = (float)cos(fAngle);
fSine = (float)sin(fAngle);
#endif
/* Create the trigonometric matrix corresponding to Z Rotation */
mOut[0] = fCosine; mOut[4] = fSine; mOut[8] = 0.0f; mOut[12] = 0.0f;
mOut[1] = -fSine; mOut[5] = fCosine; mOut[9] = 0.0f; mOut[13] = 0.0f;
mOut[2] = 0.0f; mOut[6] = 0.0f; mOut[10]= 1.0f; mOut[14] = 0.0f;
mOut[3] = 0.0f; mOut[7] = 0.0f; mOut[11]= 0.0f; mOut[15] = 1.0f;
}
/*!***************************************************************************
@Function PVRTMatrixMultiplyF
@Output mOut Result of mA x mB
@Input mA First operand
@Input mB Second operand
@Description Multiply mA by mB and assign the result to mOut
(mOut = p1 * p2). A copy of the result matrix is done in
the function because mOut can be a parameter mA or mB.
*****************************************************************************/
void myMatrixMultiplyF(
float * mOut,
float * mA,
float * mB)
{
float *mRet; mRet=mOut;
/* Perform calculation on a dummy matrix (mRet) */
mRet[0] = mA[0]*mB[0] + mA[1]*mB[4] + mA[2]*mB[8] + mA[3]*mB[12];
mRet[1] = mA[0]*mB[1] + mA[1]*mB[5] + mA[2]*mB[9] + mA[3]*mB[13];
mRet[2] = mA[0]*mB[2] + mA[1]*mB[6] + mA[2]*mB[10] + mA[3]*mB[14];
mRet[3] = mA[0]*mB[3] + mA[1]*mB[7] + mA[2]*mB[11] + mA[3]*mB[15];
mRet[4] = mA[4]*mB[0] + mA[5]*mB[4] + mA[6]*mB[4] + mA[7]*mB[12];
mRet[5] = mA[4]*mB[1] + mA[5]*mB[5] + mA[6]*mB[9] + mA[7]*mB[13];
mRet[6] = mA[4]*mB[2] + mA[5]*mB[6] + mA[6]*mB[10] + mA[7]*mB[14];
mRet[7] = mA[4]*mB[3] + mA[5]*mB[7] + mA[6]*mB[11] + mA[7]*mB[15];
mRet[8] = mA[8]*mB[0] + mA[9]*mB[4] + mA[10]*mB[8] + mA[11]*mB[12];
mRet[9] = mA[8]*mB[1] + mA[9]*mB[5] + mA[10]*mB[9] + mA[11]*mB[13];
mRet[10] = mA[8]*mB[2] + mA[9]*mB[6] + mA[10]*mB[10] + mA[11]*mB[14];
mRet[11] = mA[8]*mB[3] + mA[9]*mB[7] + mA[10]*mB[11] + mA[11]*mB[15];
mRet[12] = mA[12]*mB[0] + mA[13]*mB[4] + mA[14]*mB[8] + mA[15]*mB[12];
mRet[13] = mA[12]*mB[1] +mA[13]*mB[5] + mA[14]*mB[9] + mA[15]*mB[13];
mRet[14] = mA[12]*mB[2] + mA[13]*mB[6] + mA[14]*mB[10] + mA[15]*mB[14];
mRet[15] = mA[12]*mB[3] + mA[13]*mB[7] + mA[14]*mB[11] + mA[15]*mB[15];
/* Copy result in pResultMatrix */
//mOut = mRet;
}
/*!***************************************************************************
@Function PVRTMatrixPerspectiveFovLHF
@Output mOut Perspective matrix
@Input fFOVy Field of view
@Input fAspect Aspect ratio
@Input fNear Near clipping distance
@Input fFar Far clipping distance
@Input bRotate Should we rotate it ? (for upright screens)
@Description Create a perspective matrix.
*****************************************************************************/
void myMatrixPerspectiveFovLHF(
float * mOut,
const float fFOVy,
const float fAspect,
const float fNear,
const float fFar,
const bool bRotate)
{
float f, n, fRealAspect;
if (bRotate)
fRealAspect = 1.0f / fAspect;
else
fRealAspect = fAspect;
// cotangent(a) == 1.0f / tan(a);
f = 1.0f / (float)tan(fFOVy * 0.5f);
n = 1.0f / (fFar - fNear);
mOut[ 0] = f / fRealAspect;
mOut[ 1] = 0;
mOut[ 2] = 0;
mOut[ 3] = 0;
mOut[ 4] = 0;
mOut[ 5] = f;
mOut[ 6] = 0;
mOut[ 7] = 0;
mOut[ 8] = 0;
mOut[ 9] = 0;
mOut[10] = fFar * n;
mOut[11] = 1;
mOut[12] = 0;
mOut[13] = 0;
mOut[14] = -fFar * fNear * n;
mOut[15] = 0;
if (bRotate)
{
float * mRotation, *mTemp = mOut;
mRotation=(float*)calloc(16,sizeof(float));
myMatrixRotationZF(mRotation, 90.0f*PVRT_PIf/180.0f);
myMatrixMultiplyF(mOut, mTemp, mRotation);
if(mRotation) {free(mRotation); mRotation=NULL;}
}
}
/*!***************************************************************************
@Function PVRTMatrixPerspectiveFovRHF
@Output mOut Perspective matrix
@Input fFOVy Field of view
@Input fAspect Aspect ratio
@Input fNear Near clipping distance
@Input fFar Far clipping distance
@Input bRotate Should we rotate it ? (for upright screens)
@Description Create a perspective matrix.
*****************************************************************************/
void myMatrixPerspectiveFovRHF(
float *mOut,
const float fFOVy,
const float fAspect,
const float fNear,
const float fFar,
const bool bRotate)
{
float f, n, fRealAspect;
if (bRotate)
fRealAspect = 1.0f / fAspect;
else
fRealAspect = fAspect;
// cotangent(a) == 1.0f / tan(a);
f = 1.0f / (float)tan(fFOVy * 0.5f);
n = 1.0f / (fNear - fFar);
mOut[0]=f / fRealAspect;
mOut[1] = 0;
mOut[2] = 0;
mOut[3] = 0;
mOut[4] = 0;
mOut[5] = f;
mOut[6] = 0;
mOut[7] = 0;
mOut[8] = 0;
mOut[9] = 0;
mOut[10] = (fFar + fNear) * n;
mOut[11] = -1;
mOut[12] = 0;
mOut[13] = 0;
mOut[14] = (2 * fFar * fNear) * n;
mOut[15] = 0;
if (bRotate)
{
float *mRotation, *mTemp; mTemp = mOut;
mRotation=(float*)calloc(16,sizeof(float));
myMatrixRotationZF(mRotation, -90.0f*PVRT_PIf/180.0f);
myMatrixMultiplyF(mOut, mTemp, mRotation);
if(mRotation) {free(mRotation); mRotation=NULL;}
}
}
/*!***************************************************************************
@Function PVRTMatrixVec3NormalizeF
@Output vOut Normalized vector
@Input vIn Vector to normalize
@Description Normalizes the supplied vector.
*****************************************************************************/
void myMatrixVec3NormalizeF(
PVRTVECTOR3f &vOut,
const PVRTVECTOR3f &vIn)
{
float f;
double temp;
temp = (double)(vIn.x * vIn.x + vIn.y * vIn.y + vIn.z * vIn.z);
temp = 1.0 / sqrt(temp);
f = (float)temp;
vOut.x = vIn.x * f;
vOut.y = vIn.y * f;
vOut.z = vIn.z * f;
}
/*!***************************************************************************
@Function PVRTMatrixVec3CrossProductF
@Output vOut Cross product of the two vectors
@Input v1 First vector
@Input v2 Second vector
@Description This function performs the cross product of the two
supplied vectors.
*****************************************************************************/
void myMatrixVec3CrossProductF(
PVRTVECTOR3f &vOut,
const PVRTVECTOR3f &v1,
const PVRTVECTOR3f &v2)
{
PVRTVECTOR3f result;
/* Perform calculation on a dummy VECTOR (result) */
result.x = v1.y * v2.z - v1.z * v2.y;
result.y = v1.z * v2.x - v1.x * v2.z;
result.z = v1.x * v2.y - v1.y * v2.x;
/* Copy result in pOut */
vOut = result;
}
/*!***************************************************************************
@Function myMatrixLookAtRHF
@Output mOut Look-at view matrix
@Input vEye Position of the camera
@Input vAt Point the camera is looking at
@Input vUp Up direction for the camera
@Description Create a look-at view matrix.
*****************************************************************************/
void myMatrixLookAtRHF(
float * mOut,
const PVRTVECTOR3f &vEye,
const PVRTVECTOR3f &vAt,
const PVRTVECTOR3f &vUp)
{
PVRTVECTOR3f f, //from camera to target as new revised z axis direction
vUpActual, //normalized camera up direction
s, //normalized new x axis
u; //normalized new y axis
float * t;
f.x = vAt.x - vEye.x;
f.y = vAt.y - vEye.y;
f.z = vAt.z - vEye.z;
myMatrixVec3NormalizeF(f, f);
myMatrixVec3NormalizeF(vUpActual, vUp);
myMatrixVec3CrossProductF(s, f, vUpActual);// new x axis
myMatrixVec3CrossProductF(u, s, f);
mOut[0] = s.x;
mOut[1] = u.x;
mOut[2] = -f.x;
mOut[3] = 0;
mOut[4] = s.y;
mOut[5] = u.y;
mOut[6] = -f.y;
mOut[7] = 0;
mOut[8]= s.z;
mOut[9]= u.z;
mOut[10]= -f.z;
mOut[11]= 0;
mOut[12]= 0;
mOut[13]= 0;
mOut[14]= 0;
mOut[15]= 1;
t=(float*)calloc(16,sizeof(float));
myMatrixTranslationF(t, -vEye.x, -vEye.y, -vEye.z); //translate the Orginal to camera persition
myMatrixMultiplyF(mOut, t, mOut);
if(t) {free(t); t=NULL;}
}
/*!***************************************************************************
@Function PVRTMatrixLookAtLHX
@Output mOut Look-at view matrix
@Input vEye Position of the camera
@Input vAt Point the camera is looking at
@Input vUp Up direction for the camera
@Description Create a look-at view matrix.
*****************************************************************************/
void myMatrixLookAtLHF(
float* &mOut,
const PVRTVECTOR3f &vEye,
const PVRTVECTOR3f &vAt,
const PVRTVECTOR3f &vUp)
{
PVRTVECTOR3f f, vUpActual, s, u;
float* t;
f.x = vEye.x - vAt.x;
f.y = vEye.y - vAt.y;
f.z = vEye.z - vAt.z;
myMatrixVec3NormalizeF(f, f);
myMatrixVec3NormalizeF(vUpActual, vUp);
myMatrixVec3CrossProductF(s, f, vUpActual);
myMatrixVec3CrossProductF(u, s, f);
mOut[ 0] = s.x;
mOut[ 1] = u.x;
mOut[ 2] = -f.x;
mOut[ 3] = 0.0f;
mOut[ 4] = s.y;
mOut[ 5] = u.y;
mOut[ 6] = -f.y;
mOut[ 7] = 0.0f;
mOut[ 8] = s.z;
mOut[ 9] = u.z;
mOut[10] = -f.z;
mOut[11] = 0.0f;
mOut[12] = 0.0f;
mOut[13] = 0.0f;
mOut[14] = 0.0f;
mOut[15] = 1.0f;
t=(float*)calloc(16,sizeof(float));
myMatrixTranslationF(t, -vEye.x, -vEye.y, -vEye.z);
myMatrixMultiplyF(mOut, t, mOut);
if(t) {free(t); t=NULL;}
}
/*!***************************************************************************
@Function PVRTVec3
@Input fX X component of vector
@Input fY Y component of vector
@Input fZ Z component of vector
@Description Simple constructor from 3 values.
*****************************************************************************/
PVRTVec3 myPVRTVec3(VERTTYPE fX, VERTTYPE fY, VERTTYPE fZ)
{
PVRTVec3 tv3;
tv3.x = fX; tv3.y = fY; tv3.z = fZ;
return tv3;
}
/*!***************************************************************************
@Function multiple value constructor
@Input fX value of x component
@Input fY value of y component
@Input fZ value of z component
@Input fW value of w component
@Description Constructs a PVRTVec4 from 4 separate values
****************************************************************************/
PVRTVec4 myPVRTVec4(VERTTYPE fX, VERTTYPE fY, VERTTYPE fZ, VERTTYPE fW)
{
PVRTVec4 tv4;
tv4.x = fX; tv4.y = fY; tv4.z = fZ; tv4.w = fW;
return tv4;;
}
///
// esLogMessage()
//
// Log an error message to the debug output for the platform
//
//void ESUTIL_API eLogMessage ( const char *formatStr, ... )
void eLogMessage ( const char *formatStr, ... )
{
va_list params;
char buf[BUFSIZ];
va_start ( params, formatStr );
vsprintf_s ( buf, sizeof(buf), formatStr, params );
printf ( "%s", buf );
va_end ( params );
}