1 /*
2 	This file is part of cave9.
3 
4 	cave9 is free software: you can redistribute it and/or modify
5 	it under the terms of the GNU General Public License as published by
6 	the Free Software Foundation, either version 3 of the License, or
7 	(at your option) any later version.
8 
9 	cave9 is distributed in the hope that it will be useful,
10 	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 	GNU General Public License for more details.
13 
14 	You should have received a copy of the GNU General Public License
15 	along with cave9.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef math_h_included
19 #define math_h_included
20 
21 #include <SDL_opengl.h>
22 #include <GL/gl.h>
23 
24 #define EPSILON 0.000001
25 
26 //ifdef _WIN32 // XXX no M_PI on mingw???
27 #ifndef M_PI
28 # define M_PI		3.14159265358979323846	/* pi */
29 #endif
30 #ifndef M_PI_2
31 # define M_PI_2		1.57079632679489661923	/* pi/2 */
32 #endif
33 
34 
35 typedef GLfloat Vec3[3];
36 
37 #define COPY(a,b) \
38 	(a)[0]=(b)[0]; \
39 	(a)[1]=(b)[1]; \
40 	(a)[2]=(b)[2];
41 
42 #define CROSS(a,b,c) \
43 	(a)[0]=(b)[1]*(c)[2]-(b)[2]*(c)[1]; \
44 	(a)[1]=(b)[2]*(c)[0]-(b)[0]*(c)[2]; \
45 	(a)[2]=(b)[0]*(c)[1]-(b)[1]*(c)[0];
46 
47 #define DOT(a,b) \
48 	((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
49 
50 #define ADD2(a,b,c) \
51 	(a)[0]=(b)[0]+(c)[0]; \
52 	(a)[1]=(b)[1]+(c)[1]; \
53 	(a)[2]=(b)[2]+(c)[2];
54 
55 #define ADD(a,b) \
56 	(a)[0]+=(b)[0]; \
57 	(a)[1]+=(b)[1]; \
58 	(a)[2]+=(b)[2];
59 
60 #define ADDSCALE(a,b,x) \
61 	(a)[0]+=(b)[0]*(x); \
62 	(a)[1]+=(b)[1]*(x); \
63 	(a)[2]+=(b)[2]*(x);
64 
65 #define SUB2(a,b,c) \
66 	(a)[0]=(b)[0]-(c)[0]; \
67 	(a)[1]=(b)[1]-(c)[1]; \
68 	(a)[2]=(b)[2]-(c)[2];
69 
70 #define SCALE(a,k) \
71 	(a)[0]*=(k); \
72 	(a)[1]*=(k); \
73 	(a)[2]*=(k);
74 
75 #define SCALE2(a,b,k) \
76 	(a)[0]=(b)[0]*(k); \
77 	(a)[1]=(b)[1]*(k); \
78 	(a)[2]=(b)[2]*(k);
79 
80 #define LEN(a) \
81 	sqrt(DOT((a),(a)))
82 
83 #define SET(a,x,y,z) \
84 	(a)[0]=(x); \
85 	(a)[1]=(y); \
86 	(a)[2]=(z);
87 
88 #if 1
89 #define NORM(a) { \
90 	float invlen = 1/LEN((a)); \
91 	SCALE((a),invlen); }
92 #else
93 #define NORM(a) { \
94 	float x = DOT((a),(a));
95 	float xhalf = 0.5f*x;
96 	int i = *(int*)&x;        // get bits for floating value
97 	i = 0x5f3759df - (i>>1);  // gives initial guess y0
98 	x = *(float*)&i;          // convert bits back to float
99 	x = x*(1.5f-xhalf*x*x);   // Newton step, repeating increases accuracy
100 	SCALE((a),x); }
101 #endif
102 
103 #endif
104 
105 // vim600:fdm=syntax:fdn=1:
106