renamed source files back to .c
[cave9.git] / src / vec.h
blob4108c93d774972371c963b392363642b867f649b
1 /*
2 This file is part of cave9.
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.
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.
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/>.
18 #ifndef math_h_included
19 #define math_h_included
21 #include <SDL_opengl.h>
22 #include <GL/gl.h>
24 #ifndef __cplusplus
25 #ifndef bool
26 typedef unsigned char bool;
27 #define false 0
28 #define true 1
29 #endif
30 #endif
32 #define EPSILON 0.000001
34 typedef GLfloat Vec3[3];
36 #define MAX(x,y) ((x)>(y)?(x):(y))
37 #define MIN(x,y) ((x)<(y)?(x):(y))
38 #define CLAMP(x,y,z) ((x)<(y)?(y):((x)>(z)?(z):(x)))
40 // FIXME parenthize
42 #define RAND (rand()/(float)RAND_MAX)
44 #define COPY(a,b) \
45 a[0]=b[0]; \
46 a[1]=b[1]; \
47 a[2]=b[2];
49 #define CROSS(a,b,c) \
50 a[0]=b[1]*c[2]-b[2]*c[1]; \
51 a[1]=b[2]*c[0]-b[0]*c[2]; \
52 a[2]=b[0]*c[1]-b[1]*c[0];
54 #define DOT(a,b) \
55 (a[0]*b[0]+a[1]*b[1]+a[2]*b[2])
57 #define ADD2(a,b,c) \
58 a[0]=b[0]+c[0]; \
59 a[1]=b[1]+c[1]; \
60 a[2]=b[2]+c[2];
62 #define ADD(a,b) \
63 a[0]+=b[0]; \
64 a[1]+=b[1]; \
65 a[2]+=b[2];
67 #define ADDSCALE(a,b,x) \
68 a[0]+=b[0]*x; \
69 a[1]+=b[1]*x; \
70 a[2]+=b[2]*x;
72 #define SUB2(a,b,c) \
73 a[0]=b[0]-c[0]; \
74 a[1]=b[1]-c[1]; \
75 a[2]=b[2]-c[2];
77 #define SCALE(a,k) \
78 a[0]*=k; \
79 a[1]*=k; \
80 a[2]*=k;
82 #define SCALE2(a,b,k) \
83 a[0]=b[0]*k; \
84 a[1]=b[1]*k; \
85 a[2]=b[2]*k;
87 #define LEN(a) \
88 sqrt(DOT(a,a))
90 #define SET(a,x,y,z) \
91 a[0]=x; \
92 a[1]=y; \
93 a[2]=z;
95 #if 1
96 #define NORM(a) { \
97 float invlen = 1/LEN(a); \
98 SCALE(a,invlen); }
99 #else
100 #define NORM(a) { \
101 float x = DOT(a,a);
102 float xhalf = 0.5f*x;
103 int i = *(int*)&x; // get bits for floating value
104 i = 0x5f3759df - (i>>1); // gives initial guess y0
105 x = *(float*)&i; // convert bits back to float
106 x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
107 SCALE(a,x); }
108 #endif
110 #endif
112 // vim600:fdm=syntax:fdn=1: