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
|
#ifndef _MATH3D_H_
#define _MATH3D_H_
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define M3D_PI (3.14159265358979323846)
#define M3D_PI_DIV_180 (0.017453292519943296)
#define M3D_INV_PI_DIV_180 (57.2957795130823229)
#define m3dDegToRad(x) ((x)*M3D_PI_DIV_180)
#define m3dRadToDeg(x) ((x)*M3D_INV_PI_DIV_180)
typedef float M3DMatrix44f[16]; /* 4x4 matrix */
typedef float M3DVector3f[3]; /* vector of 3 floats */
typedef float M3DVector2f[2]; /* vector of 2 floats */
/* this is the targa header, pragmas are needed to do the voodoo magic */
/* http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/ */
#pragma pack(1)
typedef struct
{
GLbyte identsize; /* Size of id field that follows header (0) */
GLbyte imageType; /* 0 = none, 1 = indexed, 2 = rgb, 3 = grey, +8 = rle */
GLbyte colorMapType; /* 0 = none, 1 = paletted */
unsigned short colorMapStart; /* first colour map entry */
unsigned short colorMapLength; /* number of colors */
unsigned char colorMapBits; /* bits per palette entry */
unsigned short xstart; /* image x origin */
unsigned short ystart; /* image y origin */
unsigned short width; /* width in pixels */
unsigned short height; /* height in pixels */
GLbyte bits; /* bits per pixel (8 16, 24, 32) */
GLbyte descriptor; /* image descriptor */
} TGAHEADER;
#pragma pack(1)
/* math function prototypes */
void m3dFindNormal(M3DVector3f, const M3DVector3f, const M3DVector3f,
const M3DVector3f);
void m3dLoadIdentity44(M3DMatrix44f);
void m3dRotationMatrix44(M3DMatrix44f, float, float, float, float);
void gltDrawTorus(GLfloat, GLfloat, GLint, GLint);
void DrawJet(int);
void gltDrawUnitAxes(void);
void m3dMatrixMultiply44(M3DMatrix44f, const M3DMatrix44f, const M3DMatrix44f);
/* other function prototypes */
GLint gltWriteTGA(const char *);
GLbyte *gltLoadTGA(const char *, GLint *, GLint *, GLint *, GLenum *);
static inline void m3dCrossProduct(M3DVector3f result, const M3DVector3f u,
const M3DVector3f v)
{
result[0] = u[1] * v[2] - v[1] * u[2];
result[1] = -u[0] * v[2] + v[0] * u[2];
result[2] = u[0] * v[1] - v[0] * u[1];
}
static inline void m3dTransformVector3(M3DVector3f vOut, const M3DVector3f v,
const M3DMatrix44f m)
{
vOut[0] = m[0] * v[0] + m[4] * v[1] + m[8] * v[2] + m[12]; /* * v[3]; */
vOut[1] = m[1] * v[0] + m[5] * v[1] + m[9] * v[2] + m[13]; /* * v[3]; */
vOut[2] = m[2] * v[0] + m[6] * v[1] + m[10] * v[2] + m[14]; /* * v[3]; */
/* vOut[3] = m[3] * v[0] + m[7] * v[1] + m[11] * v[2] + m[15] * v[3]; */
}
static inline void m3dScaleVector3(M3DVector3f v, float scale)
{
v[0] *= scale;
v[1] *= scale;
v[2] *= scale;
}
static inline float m3dGetVectorLengthSquared(const M3DVector3f u)
{
return (u[0] * u[0]) + (u[1] * u[1]) + (u[2] * u[2]);
}
static inline float m3dGetVectorLength(const M3DVector3f u)
{
return (float) (sqrt((double) (m3dGetVectorLengthSquared(u))));
}
static inline void m3dNormalizeVector(M3DVector3f u)
{
m3dScaleVector3(u, 1.0f / m3dGetVectorLength(u));
}
static inline void m3dTranslateMatrix44(M3DMatrix44f m, float x, float y,
float z)
{
m[12] += x;
m[13] += y;
m[14] += z;
}
static inline void m3dScaleMatrix44(M3DMatrix44f m, float x, float y, float z)
{
m[0] *= x;
m[5] *= y;
m[10] *= z;
}
/* transpose / invert, only 4x4 matricies supported */
#define TRANSPOSE44(dst, src) \
{ \
int i, j; \
for (j = 0; j < 4; j++) \
{ \
for (i = 0; i < 4; i++) \
{ \
dst[(j*4)+i] = src[(i*4)+j]; \
} \
} \
}
static inline void m3dTransposeMatrix44(M3DMatrix44f dst,
const M3DMatrix44f src)
{
TRANSPOSE44(dst, src);
}
#endif
|