a97219a3f94e3a19895dd6bfae289a648702d477
[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  *
58  * The Ys are as follows:
59  *
60  *     c  Counter
61  *     p  Pointer
62  *     v  Vector (array)
63  *     0  Index of the first
64  *     i  Index
65  *     j  Subindex
66  *     k  Subsubindex
67  *
68  * Thus "up" is a pointer to  a user structure.  "lc" is the number of
69  * lumps.  "ei" and "ej" are  edge indices into some "ev" edge vector.
70  * An edge is  defined by two vertices, so  an edge structure consists
71  * of "vi" and "vj".  And so on.
72  *
73  * Those members that do not conform to this convention are explicitly
74  * documented with a comment.
75  *
76  * These prefixes are still available: c k o q y.
77  */
78
79 /*---------------------------------------------------------------------------*/
80
81 /* Material type flags */
82
83 #define M_OPAQUE       1
84 #define M_TRANSPARENT  2
85 #define M_REFLECTIVE   4
86 #define M_ENVIRONMENT  8
87 #define M_ADDITIVE    16
88 #define M_CLAMPED     32
89 #define M_DECAL       64
90 #define M_TWO_SIDED  128
91
92 /* Billboard types. */
93
94 #define B_EDGE     1
95 #define B_FLAT     2
96 #define B_ADDITIVE 4
97 #define B_NOFACE   8
98
99 /* Lump flags. */
100
101 #define L_DETAIL   1
102
103 /* Item types. */
104
105 #define ITEM_NONE       0
106 #define ITEM_COIN       1
107 #define ITEM_GROW       2
108 #define ITEM_SHRINK     3
109
110 /*---------------------------------------------------------------------------*/
111
112 struct s_mtrl
113 {
114     float d[4];                                /* diffuse color              */
115     float a[4];                                /* ambient color              */
116     float s[4];                                /* specular color             */
117     float e[4];                                /* emission color             */
118     float h[1];                                /* specular exponent          */
119     float angle;
120
121     int fl;                                    /* material flags             */
122
123     GLuint o;                                  /* OpenGL texture object      */
124     char   f[PATHMAX];                         /* texture file name          */
125 };
126
127 struct s_vert
128 {
129     float p[3];                                /* vertex position            */
130 };
131
132 struct s_edge
133 {
134     int vi;
135     int vj;
136 };
137
138 struct s_side
139 {
140     float n[3];                                /* plane normal vector        */
141     float d;                                   /* distance from origin       */
142 };
143
144 struct s_texc
145 {
146     float u[2];                                /* texture coordinate         */
147 };
148
149 struct s_geom
150 {
151     int mi;
152     int ti, si, vi;
153     int tj, sj, vj;
154     int tk, sk, vk;
155 };
156
157 struct s_lump
158 {
159     int fl;                                    /* lump flags                 */
160     int v0, vc;
161     int e0, ec;
162     int g0, gc;
163     int s0, sc;
164 };
165
166 struct s_node
167 {
168     int si;
169     int ni;
170     int nj;
171     int l0;
172     int lc;
173 };
174
175 struct s_path
176 {
177     float p[3];                                /* starting position          */
178     float t;                                   /* travel time                */
179
180     int pi;
181     int f;                                     /* enable flag                */
182     int s;                                     /* smooth flag                */
183 };
184
185 struct s_body
186 {
187     float t;                                   /* time on current path       */
188
189     GLuint ol;                                 /* opaque geometry list       */
190     GLuint tl;                                 /* transparent geometry list  */
191     GLuint rl;                                 /* reflective geometry list   */
192     GLuint sl;                                 /* shadowed geometry list     */
193
194     int pi;
195     int ni;
196     int l0;
197     int lc;
198     int g0;
199     int gc;
200 };
201
202 struct s_item
203 {
204     float p[3];                                /* position                   */
205     int   t;                                   /* type                       */
206     int   n;                                   /* value                      */
207 };
208
209 struct s_goal
210 {
211     float p[3];                                /* position                   */
212     float r;                                   /* radius                     */
213 };
214
215 struct s_swch
216 {
217     float p[3];                                /* position                   */
218     float r;                                   /* radius                     */
219     int  pi;                                   /* the linked path            */
220
221     float t0;                                  /* default timer              */
222     float t;                                   /* current timer              */
223     int   f0;                                  /* default state              */
224     int   f;                                   /* current state              */
225     int   i;                                   /* is invisible?              */
226     int   e;                                   /* is a ball inside it?       */
227     int   b;                                   /* which ball?                */
228 };
229
230 struct s_bill
231 {
232     int  fl;
233     int  mi;
234     float t;                                   /* repeat time interval       */
235     float d;                                   /* distance                   */
236
237     float w[3];                                /* width coefficients         */
238     float h[3];                                /* height coefficients        */
239
240     float rx[3];                               /* X rotation coefficients    */
241     float ry[3];                               /* Y rotation coefficients    */
242     float rz[3];                               /* Z rotation coefficients    */
243
244     float p[3];
245 };
246
247 struct s_jump
248 {
249     float p[3];                                /* position                   */
250     float q[3];                                /* target position            */
251     float r;                                   /* radius                     */
252 };
253
254 struct s_ball
255 {
256     float e[3][3];                             /* basis of orientation       */
257     float p[3];                                /* position vector            */
258     float v[3];                                /* velocity vector            */
259     float w[3];                                /* angular velocity vector    */
260     float E[3][3];                             /* basis of pendulum          */
261     float W[3];                                /* angular pendulum velocity  */
262     float r;                                   /* radius                     */
263     int   P;                                   /* play state                 */
264     int   m;                                   /* is ball mobile?            */
265     float O[3];                                /* original position          */
266 };
267
268 struct s_view
269 {
270     float p[3];
271     float q[3];
272 };
273
274 struct s_dict
275 {
276     int ai;
277     int aj;
278 };
279
280 struct s_file
281 {
282     int ac;
283     int mc;
284     int vc;
285     int ec;
286     int sc;
287     int tc;
288     int gc;
289     int lc;
290     int nc;
291     int pc;
292     int bc;
293     int hc;
294     int zc;
295     int jc;
296     int xc;
297     int rc;
298     int uc;
299     int wc;
300     int dc;
301     int ic;
302
303     char          *av;
304     struct s_mtrl *mv;
305     struct s_vert *vv;
306     struct s_edge *ev;
307     struct s_side *sv;
308     struct s_texc *tv;
309     struct s_geom *gv;
310     struct s_lump *lv;
311     struct s_node *nv;
312     struct s_path *pv;
313     struct s_body *bv;
314     struct s_item *hv;
315     struct s_goal *zv;
316     struct s_jump *jv;
317     struct s_swch *xv;
318     struct s_bill *rv;
319     struct s_ball *uv;
320     struct s_view *wv;
321     struct s_dict *dv;
322     int           *iv;
323 };
324
325 /*---------------------------------------------------------------------------*/
326
327 void sol_body_p(float p[3], const struct s_file *, const struct s_body *);
328
329 /*---------------------------------------------------------------------------*/
330
331 int   sol_load_only_file(struct s_file *, const char *);
332 int   sol_load_only_head(struct s_file *, const char *);
333 int   sol_stor(struct s_file *, const char *);
334 void  sol_free(struct s_file *);
335
336 float sol_step(struct s_file *, const float *, float, int, int *);
337
338 int   sol_jump_test(struct s_file *, float *, int);
339 int   sol_swch_test(struct s_file *);
340 int   sol_goal_test(struct s_file *, float *, int);
341
342 struct s_item *sol_item_test(struct s_file *, float *, float);
343
344 /*---------------------------------------------------------------------------*/
345
346 void put_file_state(FILE *, struct s_file *);
347 void get_file_state(FILE *, struct s_file *);
348
349 /*---------------------------------------------------------------------------*/
350
351 #endif