e287efb6fd18dd717d9278b3f8dc9ef2cbb09e93
[neverball] / share / solid.h
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #ifndef SOL_H
16 #define SOL_H
17
18 #include "glext.h"
19 #include "base_config.h"
20
21 /*
22  * Some might  be taken  aback at  the terseness of  the names  of the
23  * structure  members and  the variables  used by  the  functions that
24  * access them.  Yes, yes, I know:  readability.  I  contend that once
25  * the naming  convention is embraced, the names  become more readable
26  * than any  verbose alternative, and their brevity  and uniformity do
27  * more to augment readability than longVariableNames ever could.
28  *
29  * Members  and variables  are named  XY.   X determines  the type  of
30  * structure to which the variable  refers.  Y determines the usage of
31  * the variable.
32  *
33  * The Xs are as documented by struct s_file:
34  *
35  *     f  File          (struct s_file)
36  *     m  Material      (struct s_mtrl)
37  *     v  Vertex        (struct s_vert)
38  *     e  Edge          (struct s_edge)
39  *     s  Side          (struct s_side)
40  *     t  Texture coord (struct s_texc)
41  *     g  Geometry      (struct s_geom)
42  *     l  Lump          (struct s_lump)
43  *     n  Node          (struct s_node)
44  *     p  Path          (struct s_path)
45  *     b  Body          (struct s_body)
46  *     h  Item          (struct s_item)
47  *     z  Goal          (struct s_goal)
48  *     j  Jump          (struct s_jump)
49  *     x  Switch        (struct s_swch)
50  *     r  Billboard     (struct s_bill)
51  *     u  User          (struct s_ball)
52  *     w  Viewpoint     (struct s_view)
53  *     d  Dictionary    (struct s_dict)
54  *     i  Index         (int)
55  *     a  Text          (char)
56  *
57  * The Ys are as follows:
58  *
59  *     c  Counter
60  *     p  Pointer
61  *     v  Vector (array)
62  *     0  Index of the first
63  *     i  Index
64  *     j  Subindex
65  *     k  Subsubindex
66  *
67  * Thus "up" is a pointer to  a user structure.  "lc" is the number of
68  * lumps.  "ei" and "ej" are  edge indices into some "ev" edge vector.
69  * An edge is  defined by two vertices, so  an edge structure consists
70  * of "vi" and "vj".  And so on.
71  *
72  * Those members that do not conform to this convention are explicitly
73  * documented with a comment.
74  *
75  * These prefixes are still available: c k o q y.
76  */
77
78 /*---------------------------------------------------------------------------*/
79
80 /* Material type flags */
81
82 #define M_OPAQUE       1
83 #define M_TRANSPARENT  2
84 #define M_REFLECTIVE   4
85 #define M_ENVIRONMENT  8
86 #define M_ADDITIVE    16
87 #define M_CLAMPED     32
88 #define M_DECAL       64
89 #define M_TWO_SIDED  128
90
91 /* Billboard types. */
92
93 #define B_EDGE     1
94 #define B_FLAT     2
95 #define B_ADDITIVE 4
96 #define B_NOFACE   8
97
98 /* Lump flags. */
99
100 #define L_DETAIL   1
101
102 /* Item types. */
103
104 #define ITEM_NONE       0
105 #define ITEM_COIN       1
106 #define ITEM_GROW       2
107 #define ITEM_SHRINK     3
108
109 /*---------------------------------------------------------------------------*/
110
111 struct s_mtrl
112 {
113     float d[4];                                /* diffuse color              */
114     float a[4];                                /* ambient color              */
115     float s[4];                                /* specular color             */
116     float e[4];                                /* emission color             */
117     float h[1];                                /* specular exponent          */
118     float angle;
119
120     int fl;                                    /* material flags             */
121
122     GLuint o;                                  /* OpenGL texture object      */
123     char   f[PATHMAX];                         /* texture file name          */
124 };
125
126 struct s_vert
127 {
128     float p[3];                                /* vertex position            */
129 };
130
131 struct s_edge
132 {
133     int vi;
134     int vj;
135 };
136
137 struct s_side
138 {
139     float n[3];                                /* plane normal vector        */
140     float d;                                   /* distance from origin       */
141 };
142
143 struct s_texc
144 {
145     float u[2];                                /* texture coordinate         */
146 };
147
148 struct s_geom
149 {
150     int mi;
151     int ti, si, vi;
152     int tj, sj, vj;
153     int tk, sk, vk;
154 };
155
156 struct s_lump
157 {
158     int fl;                                    /* lump flags                 */
159     int v0, vc;
160     int e0, ec;
161     int g0, gc;
162     int s0, sc;
163 };
164
165 struct s_node
166 {
167     int si;
168     int ni;
169     int nj;
170     int l0;
171     int lc;
172 };
173
174 struct s_path
175 {
176     float p[3];                                /* starting position          */
177     float t;                                   /* travel time                */
178
179     int pi;
180     int f;                                     /* enable flag                */
181     int s;                                     /* smooth flag                */
182 };
183
184 struct s_body
185 {
186     float t;                                   /* time on current path       */
187
188     GLuint ol;                                 /* opaque geometry list       */
189     GLuint tl;                                 /* transparent geometry list  */
190     GLuint rl;                                 /* reflective geometry list   */
191     GLuint sl;                                 /* shadowed geometry list     */
192
193     int pi;
194     int ni;
195     int l0;
196     int lc;
197     int g0;
198     int gc;
199 };
200
201 struct s_item
202 {
203     float p[3];                                /* position                   */
204     int   t;                                   /* type                       */
205     int   n;                                   /* value                      */
206 };
207
208 struct s_goal
209 {
210     float p[3];                                /* position                   */
211     float r;                                   /* radius                     */
212 };
213
214 struct s_swch
215 {
216     float p[3];                                /* position                   */
217     float r;                                   /* radius                     */
218     int  pi;                                   /* the linked path            */
219
220     float t0;                                  /* default timer              */
221     float t;                                   /* current timer              */
222     int   f0;                                  /* default state              */
223     int   f;                                   /* current state              */
224     int   i;                                   /* is invisible?              */
225     int   e;                                   /* is a ball inside it?       */
226     int   b;                                   /* which ball?                */
227 };
228
229 struct s_bill
230 {
231     int  fl;
232     int  mi;
233     float t;                                   /* repeat time interval       */
234     float d;                                   /* distance                   */
235
236     float w[3];                                /* width coefficients         */
237     float h[3];                                /* height coefficients        */
238
239     float rx[3];                               /* X rotation coefficients    */
240     float ry[3];                               /* Y rotation coefficients    */
241     float rz[3];                               /* Z rotation coefficients    */
242
243     float p[3];
244 };
245
246 struct s_jump
247 {
248     float p[3];                                /* position                   */
249     float q[3];                                /* target position            */
250     float r;                                   /* radius                     */
251 };
252
253 struct s_ball
254 {
255     float e[3][3];                             /* basis of orientation       */
256     float p[3];                                /* position vector            */
257     float v[3];                                /* velocity vector            */
258     float w[3];                                /* angular velocity vector    */
259     float E[3][3];                             /* basis of pendulum          */
260     float W[3];                                /* angular pendulum velocity  */
261     float r;                                   /* radius                     */
262     int   P;                                   /* play state                 */
263     int   m;                                   /* is ball mobile?            */
264     float O[3];                                /* original position          */
265 };
266
267 struct s_view
268 {
269     float p[3];
270     float q[3];
271 };
272
273 struct s_dict
274 {
275     int ai;
276     int aj;
277 };
278
279 struct s_file
280 {
281     int ac;
282     int mc;
283     int vc;
284     int ec;
285     int sc;
286     int tc;
287     int gc;
288     int lc;
289     int nc;
290     int pc;
291     int bc;
292     int hc;
293     int zc;
294     int jc;
295     int xc;
296     int rc;
297     int uc;
298     int wc;
299     int dc;
300     int ic;
301
302     char          *av;
303     struct s_mtrl *mv;
304     struct s_vert *vv;
305     struct s_edge *ev;
306     struct s_side *sv;
307     struct s_texc *tv;
308     struct s_geom *gv;
309     struct s_lump *lv;
310     struct s_node *nv;
311     struct s_path *pv;
312     struct s_body *bv;
313     struct s_item *hv;
314     struct s_goal *zv;
315     struct s_jump *jv;
316     struct s_swch *xv;
317     struct s_bill *rv;
318     struct s_ball *uv;
319     struct s_view *wv;
320     struct s_dict *dv;
321     int           *iv;
322 };
323
324 /*---------------------------------------------------------------------------*/
325
326 void sol_body_p(float p[3], const struct s_file *, const struct s_body *);
327
328 /*---------------------------------------------------------------------------*/
329
330 int   sol_load_only_file(struct s_file *, const char *);
331 int   sol_load_only_head(struct s_file *, const char *);
332 int   sol_stor(struct s_file *, const char *);
333 void  sol_free(struct s_file *);
334
335 float sol_step(struct s_file *, const float *, float, int, int *);
336
337 int   sol_jump_test(struct s_file *, float *, int);
338 int   sol_swch_test(struct s_file *);
339 int   sol_goal_test(struct s_file *, float *, int);
340
341 struct s_item *sol_item_test(struct s_file *, float *, float);
342
343 /*---------------------------------------------------------------------------*/
344
345 void put_file_state(FILE *, struct s_file *);
346 void get_file_state(FILE *, struct s_file *);
347
348 /*---------------------------------------------------------------------------*/
349
350 #endif