c3context: Make sure lights are 'drawn' first
[simavr] / examples / shared / libc3 / src / c3algebra.h
1 /*
2         c3algebra.h
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         Derivative and inspiration from original C++:
7         Paul Rademacher & Jean-Francois DOUEG,
8
9         This file is part of libc3.
10
11         libc3 is free software: you can redistribute it and/or modify
12         it under the terms of the GNU General Public License as published by
13         the Free Software Foundation, either version 3 of the License, or
14         (at your option) any later version.
15
16         libc3 is distributed in the hope that it will be useful,
17         but WITHOUT ANY WARRANTY; without even the implied warranty of
18         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19         GNU General Public License for more details.
20
21         You should have received a copy of the GNU General Public License
22         along with libc3.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #ifndef __C3ALGEBRA_H___
27 #define __C3ALGEBRA_H___
28
29
30 #ifndef M_PI
31 #define M_PI 3.141592654
32 #endif
33 #define PI_OVER_180      0.017453292519943295769236907684886
34 #define PI_OVER_360      0.0087266462599716478846184538424431
35
36 enum {VX, VY, VZ, VW};           // axes
37 enum {PA, PB, PC, PD};           // planes
38 enum {RED, GREEN, BLUE, ALPHA};  // colors
39 enum {KA, KD, KS, ES};           // phong coefficients
40
41 typedef float c3f;
42 typedef c3f (*V_FCT_PTR)(c3f);
43
44 typedef union c3vec2 {
45         struct { c3f x,y; };
46         c3f n[2];
47 } c3vec2;
48
49 typedef union c3vec3 {
50         struct { c3f x,y,z; };
51         c3f n[3];
52 } c3vec3;
53
54 typedef union c3vec4 {
55         struct { c3f x,y,z,w; };
56         c3f n[4];
57 } c3vec4, * c3vec4p;
58
59 typedef union c3mat3 {
60         c3vec3 v[3];
61         c3f n[3*3];
62 } c3mat3, * c3mat3p;
63
64 typedef union c3mat4 {
65         c3vec4 v[4];
66         c3f n[4*4];
67 } c3mat4, * c3mat4p;
68
69 /*
70  * c3vec2 related
71  */
72
73 c3vec2  c3vec2_zero();
74 c3vec2  c3vec2f(c3f x, c3f y);
75
76 c3vec2  c3vec2_add(c3vec2 a, const c3vec2 v);
77 c3vec2  c3vec2_sub(c3vec2 a, const c3vec2 v);
78 c3vec2  c3vec2_mulf(c3vec2 a, c3f d);
79 c3vec2  c3vec2_divf(c3vec2 a, c3f d);
80
81 c3f             c3vec2_length2(const c3vec2 a);
82 c3f             c3vec2_length(const c3vec2 a);
83 c3vec2  c3vec2_normalize(const c3vec2 a); // it is up to caller to avoid divide-by-zero
84 c3vec2  c3vec2_apply(c3vec2 a, V_FCT_PTR fct);
85 c3vec2  c3vec2_minus(const c3vec2 a);
86 c3f             c3vec2_dot(const c3vec2 a, const c3vec2 b);
87 c3vec2  c3vec2_min(const c3vec2 a, const c3vec2 b);
88 c3vec2  c3vec2_max(const c3vec2 a, const c3vec2 b);
89 c3vec2  c3vec2_prod(const c3vec2 a, const c3vec2 b);
90
91 /*
92  * c3vec3 related
93  */
94
95 c3vec3  c3vec3_zero();
96 c3vec3  c3vec3f(c3f x, c3f y, c3f z);
97 c3vec3  c3vec3_vec2f(const c3vec2 v, c3f d);
98 c3vec3  c3vec3_vec2(const c3vec2 v);
99 c3vec3  c3vec3_vec4(const c3vec4 v); // it is up to caller to avoid divide-by-zero
100
101 c3vec3  c3vec3_add(const c3vec3 a, const c3vec3 v);
102 c3vec3  c3vec3_sub(const c3vec3 a, const c3vec3 v);
103 c3vec3  c3vec3_mulf(const c3vec3 a, c3f d);
104 c3vec3  c3vec3_divf(const c3vec3 a, c3f d);
105
106 c3f             c3vec3_length2(const c3vec3 a);
107 c3f             c3vec3_length(const c3vec3 a);
108 c3vec3  c3vec3_normalize(const c3vec3 a); // it is up to caller to avoid divide-by-zero
109 c3vec3  c3vec3_homogenize(c3vec3 a); // it is up to caller to avoid divide-by-zero
110 c3vec3  c3vec3_apply(c3vec3 a, V_FCT_PTR fct);
111 c3vec3  c3vec3_minus(const c3vec3 a);
112 c3f             c3vec3_dot(const c3vec3 a, const c3vec3 b);
113 int             c3vec3_equal(const c3vec3 a, const c3vec3 b);
114 c3vec3  c3vec3_min(const c3vec3 a, const c3vec3 b);
115 c3vec3  c3vec3_max(const c3vec3 a, const c3vec3 b);
116 c3vec3  c3vec3_prod(const c3vec3 a, const c3vec3 b);
117
118 c3vec3  c3vec3_cross(const c3vec3 a, const c3vec3 b);
119 c3vec3  c3vec2_cross(const c3vec2 a, const c3vec2 b);
120 c3vec3  c3vec3_polar(const c3vec3 a); // returns phi, theta, length
121
122 /*
123  * c3vec4 related
124  */
125
126 c3vec4  c3vec4_zero();
127 c3vec4  c3vec4f(c3f x, c3f y, c3f z, c3f w);
128 c3vec4  c3vec4_vec3(const c3vec3 v);
129 c3vec4  c3vec4_vec3f(const c3vec3 v, c3f d);
130
131 c3vec4  c3vec4_add(c3vec4 a, const c3vec4 v);
132 c3vec4  c3vec4_sub(c3vec4 a, const c3vec4 v);
133 c3vec4  c3vec4_mulf(c3vec4 a, c3f d);
134 c3vec4  c3vec4_divf(c3vec4 a, c3f d);
135
136 c3f             c3vec4_length2(const c3vec4 a);
137 c3f             c3vec4_length(const c3vec4 a);
138 c3vec4  c3vec4_normalize(c3vec4 a); // it is up to caller to avoid divide-by-zero
139 c3vec4  c3vec4_homogenize(c3vec4 a); // it is up to caller to avoid divide-by-zero
140 c3vec4  c3vec4_apply(c3vec4 a, V_FCT_PTR fct);
141 c3vec4  c3vec4_minus(const c3vec4 a);
142 int             c3vec4_equal(const c3vec4 a, const c3vec4 b);
143 c3vec4  c3vec4_min(const c3vec4 a, const c3vec4 b);
144 c3vec4  c3vec4_max(const c3vec4 a, const c3vec4 b);
145 c3vec4  c3vec4_prod(const c3vec4 a, const c3vec4 b);
146
147 /*
148  * c3mat3 related
149  */
150
151 c3mat3  c3mat3_identity();
152 c3mat3  c3mat3_vec3(const c3vec3 v0, const c3vec3 v1, const c3vec3 v2);
153 c3mat3p c3mat3_add(const c3mat3p a, const c3mat3p m);
154 c3mat3p c3mat3_sub(const c3mat3p a, const c3mat3p m);
155 c3mat3p c3mat3_mulf(const c3mat3p a, c3f d);
156 c3mat3p c3mat3_divf(const c3mat3p a, c3f d);
157
158 c3mat3  c3mat3_transpose(const c3mat3p a);
159 c3mat3  c3mat3_inverse(const c3mat3p m);   // Gauss-Jordan elimination with partial pivoting
160 c3mat3p c3mat3_apply(c3mat3p a, V_FCT_PTR fct);
161 c3mat3  c3mat3_minus(const c3mat3p a);
162
163 c3mat3  c3mat3_mul(const c3mat3p a, const c3mat3p b);
164 int             c3mat3_equal(const c3mat3p a, const c3mat3p b);
165
166 c3vec2  c3mat3_mulv2(const c3mat3p a, const c3vec2 v);
167 c3vec3  c3mat3_mulv3(const c3mat3p a, const c3vec3 v);
168 c3vec2  c3vec2_mulm3(const c3vec2 v, const c3mat3p a);
169 c3vec3  c3vec3_mulm3(const c3vec3 v, const c3mat3p a);
170
171 c3mat3  identity2D();
172 c3mat3  translation2D(const c3vec2 v);
173 c3mat3  rotation2D(const c3vec2 Center, c3f angleDeg);
174 c3mat3  scaling2D(const c3vec2 scaleVector);
175
176 /*
177  * c3mat4 related
178  */
179
180 c3mat4  c3mat4_identity();
181 c3mat4  c3mat4_vec4(const c3vec4 v0, const c3vec4 v1, const c3vec4 v2, const c3vec4 v3);
182 c3mat4  c3mat4f(
183      c3f a00, c3f a01, c3f a02, c3f a03,
184      c3f a10, c3f a11, c3f a12, c3f a13,
185      c3f a20, c3f a21, c3f a22, c3f a23,
186      c3f a30, c3f a31, c3f a32, c3f a33 );
187
188 c3mat4  c3mat4_minus(const c3mat4p a);
189 c3mat4p c3mat4p_add(c3mat4p a, const c3mat4p m);
190 c3mat4  c3mat4_add(const c3mat4p a, const c3mat4p b);
191 c3mat4p c3mat4p_sub(c3mat4p a, const c3mat4p m);
192 c3mat4  c3mat4_sub(const c3mat4p a, const c3mat4p b);
193 c3mat4p c3mat4p_mulf(c3mat4p a, c3f d);
194 c3mat4  c3mat4_mulf(const c3mat4p a, c3f d);
195 c3mat4  c3mat4_mul(const c3mat4p a, const c3mat4p b);
196 c3mat4p c3mat4p_divf(c3mat4p a, c3f d);
197 c3mat4  c3mat4_divf(const c3mat4p a, c3f d);
198
199 c3mat4  c3mat4_transpose(const c3mat4p a);
200 c3mat4  c3mat4_inverse(const c3mat4p m);       // Gauss-Jordan elimination with partial pivoting
201 c3mat4p c3mat4p_apply(c3mat4p a, V_FCT_PTR fct);
202 c3mat4p c3mat4p_swap_rows(c3mat4p a, int i, int j);
203 c3mat4p c3mat4p_swap_cols(c3mat4p a, int i, int j);
204 int             c3mat4_equal(const c3mat4p a, const c3mat4p b);
205
206 c3vec4  c3vec4_mulm4(const c3vec4 v, const c3mat4p a);
207 c3vec4  c3mat4_mulv4(const c3mat4p a, const c3vec4 v);
208 c3vec3  c3mat4_mulv3(const c3mat4p a, const c3vec3 v);
209
210 c3mat4  identity3D();
211 c3mat4  translation3D(const c3vec3 v);
212 c3mat4  rotation3D(const c3vec3 Axis, c3f angleDeg);
213 c3mat4  rotation3Drad(const c3vec3 Axis, c3f angleRad);
214 c3mat4  scaling3D(const c3vec3 scaleVector);
215 c3mat4  frustum3D(
216                         c3f left, c3f right, c3f bottom, c3f top,
217                         c3f znear, c3f zfar);
218 c3mat4  perspective3D(c3f fov, c3f aspect, c3f znear, c3f zfar);
219 c3mat4  ortho3D(
220                 c3f left, c3f right, c3f bottom, c3f top,
221                 c3f near, c3f far);
222 c3mat4  screen_ortho3D(
223                 c3f left, c3f right, c3f bottom, c3f top,
224                 c3f near, c3f far);
225
226 #endif /* __C3ALGEBRA_H___ */