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 SOLID_BASE_H
16 #define SOLID_BASE_H
17 
18 #include "base_config.h"
19 
20 /*
21  * Some might  be taken  aback at  the terseness of  the names  of the
22  * structure  members and  the variables  used by  the  functions that
23  * access them.  Yes, yes, I know:  readability.  I  contend that once
24  * the naming  convention is embraced, the names  become more readable
25  * than any  verbose alternative, and their brevity  and uniformity do
26  * more to augment readability than longVariableNames ever could.
27  *
28  * Members  and variables  are named  XY.   X determines  the type  of
29  * structure to which the variable  refers.  Y determines the usage of
30  * the variable.
31  *
32  * The Xs are as documented by struct s_base:
33  *
34  *     f  File          (struct s_base)
35  *     m  Material      (struct b_mtrl)
36  *     v  Vertex        (struct b_vert)
37  *     e  Edge          (struct b_edge)
38  *     s  Side          (struct b_side)
39  *     t  Texture coord (struct b_texc)
40  *     g  Geometry      (struct b_geom)
41  *     o  Offset        (struct b_offs)
42  *     l  Lump          (struct b_lump)
43  *     n  Node          (struct b_node)
44  *     p  Path          (struct b_path)
45  *     b  Body          (struct b_body)
46  *     h  Item          (struct b_item)
47  *     z  Goal          (struct b_goal)
48  *     j  Jump          (struct b_jump)
49  *     x  Switch        (struct b_swch)
50  *     r  Billboard     (struct b_bill)
51  *     u  User          (struct b_ball)
52  *     w  Viewpoint     (struct b_view)
53  *     d  Dictionary    (struct b_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 q y.
76  */
77 
78 /*
79  * Additionally, solid data is split into three main parts: static
80  * data (base), simulation data (vary), and rendering data (draw).
81  */
82 
83 /*---------------------------------------------------------------------------*/
84 
85 /* Material type flags */
86 
87 #define M_PARTICLE    (1 << 10)
88 #define M_ALPHA_TEST  (1 <<  9)
89 #define M_REFLECTIVE  (1 <<  8)
90 #define M_TRANSPARENT (1 <<  7)
91 #define M_SHADOWED    (1 <<  6)
92 #define M_DECAL       (1 <<  5)
93 #define M_ENVIRONMENT (1 <<  4)
94 #define M_TWO_SIDED   (1 <<  3)
95 #define M_ADDITIVE    (1 <<  2)
96 #define M_CLAMP_S     (1 <<  1)
97 #define M_CLAMP_T     (1 <<  0)
98 
99 /* Billboard types. */
100 
101 #define B_EDGE     1
102 #define B_FLAT     2
103 #define B_NOFACE   4
104 
105 /* Lump flags. */
106 
107 #define L_DETAIL   1
108 
109 /* Item types. */
110 
111 #define ITEM_NONE       0
112 #define ITEM_COIN       1
113 #define ITEM_GROW       2
114 #define ITEM_SHRINK     3
115 
116 /* Path flags. */
117 
118 #define P_ORIENTED 1
119 
120 /*---------------------------------------------------------------------------*/
121 
122 struct b_mtrl
123 {
124     float d[4];                                /* diffuse color              */
125     float a[4];                                /* ambient color              */
126     float s[4];                                /* specular color             */
127     float e[4];                                /* emission color             */
128     float h[1];                                /* specular exponent          */
129     float angle;
130 
131     int fl;                                    /* material flags             */
132 
133     char   f[PATHMAX];                         /* texture file name          */
134 
135     /* M_ALPHA_TEST */
136     int   alpha_func;                          /* comparison function        */
137     float alpha_ref;                           /* reference value            */
138 };
139 
140 struct b_vert
141 {
142     float p[3];                                /* vertex position            */
143 };
144 
145 struct b_edge
146 {
147     int vi;
148     int vj;
149 };
150 
151 struct b_side
152 {
153     float n[3];                                /* plane normal vector        */
154     float d;                                   /* distance from origin       */
155 };
156 
157 struct b_texc
158 {
159     float u[2];                                /* texture coordinate         */
160 };
161 
162 struct b_offs
163 {
164     int ti, si, vi;
165 };
166 
167 struct b_geom
168 {
169     int mi;
170     int oi, oj, ok;
171 };
172 
173 struct b_lump
174 {
175     int fl;                                    /* lump flags                 */
176     int v0, vc;
177     int e0, ec;
178     int g0, gc;
179     int s0, sc;
180 };
181 
182 struct b_node
183 {
184     int si;
185     int ni;
186     int nj;
187     int l0;
188     int lc;
189 };
190 
191 struct b_path
192 {
193     float p[3];                                /* starting position          */
194     float e[4];                                /* orientation (quaternion)   */
195     float t;                                   /* travel time                */
196     int   tm;                                  /* milliseconds               */
197 
198     int pi;
199     int f;                                     /* enable flag                */
200     int s;                                     /* smooth flag                */
201 
202     int fl;                                    /* flags                      */
203 
204     /* TODO: merge enable and smooth into flags. */
205 };
206 
207 struct b_body
208 {
209     int pi;
210     int pj;
211 
212     int ni;
213     int l0;
214     int lc;
215     int g0;
216     int gc;
217 };
218 
219 struct b_item
220 {
221     float p[3];                                /* position                   */
222     int   t;                                   /* type                       */
223     int   n;                                   /* value                      */
224 };
225 
226 struct b_goal
227 {
228     float p[3];                                /* position                   */
229     float r;                                   /* radius                     */
230 };
231 
232 struct b_swch
233 {
234     float p[3];                                /* position                   */
235     float r;                                   /* radius                     */
236     int  pi;                                   /* the linked path            */
237 
238     float t;                                   /* default timer              */
239     int   tm;                                  /* milliseconds               */
240     int   f;                                   /* default state              */
241     int   i;                                   /* is invisible?              */
242 };
243 
244 struct b_bill
245 {
246     int  fl;
247     int  mi;
248     float t;                                   /* repeat time interval       */
249     float d;                                   /* distance                   */
250 
251     float w[3];                                /* width coefficients         */
252     float h[3];                                /* height coefficients        */
253 
254     float rx[3];                               /* X rotation coefficients    */
255     float ry[3];                               /* Y rotation coefficients    */
256     float rz[3];                               /* Z rotation coefficients    */
257 
258     float p[3];
259 };
260 
261 struct b_jump
262 {
263     float p[3];                                /* position                   */
264     float q[3];                                /* target position            */
265     float r;                                   /* radius                     */
266 };
267 
268 struct b_ball
269 {
270     float p[3];                                /* position vector            */
271     float r;                                   /* radius                     */
272 };
273 
274 struct b_view
275 {
276     float p[3];
277     float q[3];
278 };
279 
280 struct b_dict
281 {
282     int ai;
283     int aj;
284 };
285 
286 struct s_base
287 {
288     int ac;
289     int mc;
290     int vc;
291     int ec;
292     int sc;
293     int tc;
294     int oc;
295     int gc;
296     int lc;
297     int nc;
298     int pc;
299     int bc;
300     int hc;
301     int zc;
302     int jc;
303     int xc;
304     int rc;
305     int uc;
306     int wc;
307     int dc;
308     int ic;
309 
310     char          *av;
311     struct b_mtrl *mv;
312     struct b_vert *vv;
313     struct b_edge *ev;
314     struct b_side *sv;
315     struct b_texc *tv;
316     struct b_offs *ov;
317     struct b_geom *gv;
318     struct b_lump *lv;
319     struct b_node *nv;
320     struct b_path *pv;
321     struct b_body *bv;
322     struct b_item *hv;
323     struct b_goal *zv;
324     struct b_jump *jv;
325     struct b_swch *xv;
326     struct b_bill *rv;
327     struct b_ball *uv;
328     struct b_view *wv;
329     struct b_dict *dv;
330     int           *iv;
331 
332     /*
333      * A mapping from internal to cached material indices.
334      */
335     int *mtrls;
336 };
337 
338 /*---------------------------------------------------------------------------*/
339 
340 int  sol_load_base(struct s_base *, const char *);
341 int  sol_load_meta(struct s_base *, const char *);
342 void sol_free_base(struct s_base *);
343 int  sol_stor_base(struct s_base *, const char *);
344 
345 /*---------------------------------------------------------------------------*/
346 
347 struct path
348 {
349     char prefix[16];
350     char suffix[8];
351 };
352 
353 #define CONCAT_PATH(dst, path, name) do { \
354     SAFECPY((dst), (path)->prefix);       \
355     SAFECAT((dst), (name));               \
356     SAFECAT((dst), (path)->suffix);       \
357 } while (0)
358 
359 extern const struct path tex_paths[4];
360 extern const struct path mtrl_paths[2];
361 
362 /*---------------------------------------------------------------------------*/
363 
364 int mtrl_read(struct b_mtrl *, const char *);
365 
366 /*---------------------------------------------------------------------------*/
367 
368 #endif
369