]> icculus.org git repositories - divverent/darkplaces.git/blob - model_brush.h
modified q3msurface_t to use a surfmesh (although not cleanly)
[divverent/darkplaces.git] / model_brush.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program 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.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #ifndef MODEL_BRUSH_H
22 #define MODEL_BRUSH_H
23
24 /*
25 ==============================================================================
26
27 BRUSH MODELS
28
29 ==============================================================================
30 */
31
32
33 //
34 // in memory representation
35 //
36 typedef struct
37 {
38         vec3_t position;
39 }
40 mvertex_t;
41
42 #define SIDE_FRONT 0
43 #define SIDE_BACK 1
44 #define SIDE_ON 2
45
46
47 // plane_t structure
48 typedef struct mplane_s
49 {
50         vec3_t normal;
51         float dist;
52         // for texture axis selection and fast side tests
53         int type;
54         int signbits;
55 }
56 mplane_t;
57
58 #define SHADERSTAGE_SKY 0
59 #define SHADERSTAGE_NORMAL 1
60 #define SHADERSTAGE_COUNT 2
61
62 #define SURF_PLANEBACK 2
63 #define SURF_DRAWSKY 4
64 #define SURF_DRAWTURB 0x10
65 #define SURF_LIGHTMAP 0x20
66 #define SURF_DRAWNOALPHA 0x100
67 #define SURF_DRAWFULLBRIGHT 0x200
68 #define SURF_LIGHTBOTHSIDES 0x400
69 #define SURF_WATERALPHA 0x4000 // this polygon's alpha is modulated by r_wateralpha
70 #define SURF_SOLIDCLIP 0x8000 // this polygon blocks movement
71
72 #define SURFRENDER_OPAQUE 0
73 #define SURFRENDER_ALPHA 1
74 #define SURFRENDER_ADD 2
75
76 struct entity_render_s;
77 struct texture_s;
78 struct msurface_s;
79
80 typedef struct texture_s
81 {
82         // name
83         char name[16];
84         // size
85         unsigned int width, height;
86         // SURF_ flags
87         unsigned int flags;
88
89         // position in the model's textures array
90         int number;
91
92         // type of rendering (SURFRENDER_ value)
93         int rendertype;
94
95         // loaded the same as model skins
96         skinframe_t skin;
97
98         // total frames in sequence and alternate sequence
99         int anim_total[2];
100         // direct pointers to each of the frames in the sequences
101         // (indexed as [alternate][frame])
102         struct texture_s *anim_frames[2][10];
103         // set if animated or there is an alternate frame set
104         // (this is an optimization in the renderer)
105         int animated;
106         // the current texture frame in animation
107         struct texture_s *currentframe;
108         // current alpha of the texture
109         float currentalpha;
110 }
111 texture_t;
112
113 typedef struct
114 {
115         unsigned short v[2];
116 }
117 medge_t;
118
119 typedef struct
120 {
121         float vecs[2][4];
122         texture_t *texture;
123         int flags;
124 }
125 mtexinfo_t;
126
127 typedef struct msurface_s
128 {
129         // bounding box for onscreen checks
130         vec3_t poly_mins;
131         vec3_t poly_maxs;
132
133         // the node plane this is on, backwards if SURF_PLANEBACK flag set
134         mplane_t *plane;
135         // SURF_ flags
136         int flags;
137         // texture mapping properties used by this surface
138         mtexinfo_t *texinfo;
139
140         // the lightmap texture fragment to use on the rendering mesh
141         rtexture_t *lightmaptexture;
142         // mesh for rendering
143         surfmesh_t mesh;
144         // if lightmap settings changed, this forces update
145         int cached_dlight;
146
147         // should be drawn if visframe == r_framecount (set by PrepareSurfaces)
148         int visframe;
149         // should be drawn if onscreen and not a backface (used for setting visframe)
150         //int pvsframe;
151         // chain of surfaces marked visible by pvs
152         //struct msurface_s *pvschain;
153
154         // surface number, to avoid having to do a divide to find the number of a surface from it's address
155         int number;
156
157         // center for sorting transparent meshes
158         vec3_t poly_center;
159
160         // index into d_lightstylevalue array, 255 means not used (black)
161         qbyte styles[MAXLIGHTMAPS];
162         // RGB lighting data [numstyles][height][width][3]
163         qbyte *samples;
164         // stain to apply on lightmap (soot/dirt/blood/whatever)
165         qbyte *stainsamples;
166         // the stride when building lightmaps to comply with fragment update
167         int lightmaptexturestride;
168         int texturemins[2];
169         int extents[2];
170
171         // if this == r_framecount there are dynamic lights on the surface
172         int dlightframe;
173         // which dynamic lights are touching this surface
174         // (only access this if dlightframe is current)
175         int dlightbits[8];
176         // avoid redundent addition of dlights
177         int lightframe;
178
179         // avoid multiple collision traces with a surface polygon
180         int colframe;
181
182         // these are just 3D points defining the outline of the polygon,
183         // no texcoord info (that can be generated from these)
184         int poly_numverts;
185         float *poly_verts;
186
187         // index into model->brush.shadowmesh
188         int num_firstshadowmeshtriangle;
189
190         // neighboring surfaces (one per poly_numverts)
191         //struct msurface_s **neighborsurfaces;
192         // currently used only for generating static shadow volumes
193         int lighttemp_castshadow;
194
195         // avoid redundent surface shadows
196         int shadowmark;
197 }
198 msurface_t;
199
200 typedef struct mnode_s
201 {
202 // common with leaf
203         // always 0 in nodes
204         int contents;
205
206         struct mnode_s *parent;
207         struct mportal_s *portals;
208
209         // for bounding box culling
210         vec3_t mins;
211         vec3_t maxs;
212
213         mplane_t *plane; // != NULL
214 // node specific
215         struct mnode_s *children[2];
216
217         unsigned short firstsurface;
218         unsigned short numsurfaces;
219 }
220 mnode_t;
221
222 typedef struct mleaf_s
223 {
224 // common with node
225         // always negative in leafs
226         int contents;
227
228         struct mnode_s *parent;
229         struct mportal_s *portals;
230
231         // for bounding box culling
232         vec3_t mins;
233         vec3_t maxs;
234
235         mplane_t *plane; // == NULL
236 // leaf specific
237         // next leaf in pvschain
238         struct mleaf_s *pvschain;
239         // potentially visible if current (model->pvsframecount)
240         int pvsframe;
241         // visible if marked current (r_framecount)
242         int visframe;
243         // used by certain worldnode variants to avoid processing the same leaf twice in a frame
244         int worldnodeframe;
245         // used by polygon-through-portals visibility checker
246         int portalmarkid;
247
248         // -1 is not in pvs, >= 0 is pvs bit number
249         int clusterindex;
250
251         int *firstmarksurface;
252         int nummarksurfaces;
253         qbyte ambient_sound_level[NUM_AMBIENTS];
254 }
255 mleaf_t;
256
257 typedef struct
258 {
259         dclipnode_t *clipnodes;
260         mplane_t *planes;
261         int firstclipnode;
262         int lastclipnode;
263         vec3_t clip_mins;
264         vec3_t clip_maxs;
265         vec3_t clip_size;
266 }
267 hull_t;
268
269 typedef struct mportal_s
270 {
271         struct mportal_s *next; // the next portal on this leaf
272         mleaf_t *here; // the leaf this portal is on
273         mleaf_t *past; // the leaf through this portal (infront)
274         mvertex_t *points;
275         int numpoints;
276         mplane_t plane;
277         int visframe; // is this portal visible this frame?
278 }
279 mportal_t;
280
281 typedef struct svbspmesh_s
282 {
283         struct svbspmesh_s *next;
284         int numverts, maxverts;
285         int numtriangles, maxtriangles;
286         float *verts;
287         int *elements;
288 }
289 svbspmesh_t;
290
291 typedef struct mlight_s
292 {
293         // location of light
294         vec3_t origin;
295         // distance attenuation scale (smaller is a larger light)
296         float falloff;
297         // color and brightness combined
298         vec3_t light;
299         // brightness bias, used for limiting radius without a hard edge
300         float subtract;
301         // spotlight direction
302         vec3_t spotdir;
303         // cosine of spotlight cone angle (or 0 if not a spotlight)
304         float spotcone;
305         // distance bias (larger value is softer and darker)
306         float distbias;
307         // light style controlling this light
308         int style;
309         // maximum extent of the light for shading purposes
310         float lightradius;
311         // maximum extent of the light for culling purposes
312         float cullradius;
313         float cullradius2;
314         /*
315         // surfaces this shines on
316         int numsurfaces;
317         msurface_t **surfaces;
318         // lit area
319         vec3_t mins, maxs;
320         // precomputed shadow volume meshs
321         //svbspmesh_t *shadowvolume;
322         //vec3_t shadowvolumemins, shadowvolumemaxs;
323         shadowmesh_t *shadowvolume;
324         */
325 }
326 mlight_t;
327
328 extern rtexture_t *r_notexture;
329 extern texture_t r_notexture_mip;
330
331 struct model_s;
332 void Mod_Q1BSP_Load(struct model_s *mod, void *buffer);
333 void Mod_IBSP_Load(struct model_s *mod, void *buffer);
334 void Mod_MAP_Load(struct model_s *mod, void *buffer);
335 void Mod_BrushInit(void);
336
337 // Q2 bsp stuff
338
339 #define Q2BSPVERSION    38
340
341 // leaffaces, leafbrushes, planes, and verts are still bounded by
342 // 16 bit short limits
343
344 //=============================================================================
345
346 #define Q2LUMP_ENTITIES         0
347 #define Q2LUMP_PLANES                   1
348 #define Q2LUMP_VERTEXES         2
349 #define Q2LUMP_VISIBILITY               3
350 #define Q2LUMP_NODES                    4
351 #define Q2LUMP_TEXINFO          5
352 #define Q2LUMP_FACES                    6
353 #define Q2LUMP_LIGHTING         7
354 #define Q2LUMP_LEAFS                    8
355 #define Q2LUMP_LEAFFACES                9
356 #define Q2LUMP_LEAFBRUSHES      10
357 #define Q2LUMP_EDGES                    11
358 #define Q2LUMP_SURFEDGES                12
359 #define Q2LUMP_MODELS                   13
360 #define Q2LUMP_BRUSHES          14
361 #define Q2LUMP_BRUSHSIDES               15
362 #define Q2LUMP_POP                      16
363 #define Q2LUMP_AREAS                    17
364 #define Q2LUMP_AREAPORTALS      18
365 #define Q2HEADER_LUMPS          19
366
367 typedef struct
368 {
369         int                     ident;
370         int                     version;
371         lump_t          lumps[HEADER_LUMPS];
372 } q2dheader_t;
373
374 typedef struct
375 {
376         float           mins[3], maxs[3];
377         float           origin[3];              // for sounds or lights
378         int                     headnode;
379         int                     firstface, numfaces;    // submodels just draw faces
380                                                                                 // without walking the bsp tree
381 } q2dmodel_t;
382
383 // planes (x&~1) and (x&~1)+1 are always opposites
384
385 // contents flags are seperate bits
386 // a given brush can contribute multiple content bits
387 // multiple brushes can be in a single leaf
388
389 // these definitions also need to be in q_shared.h!
390
391 // lower bits are stronger, and will eat weaker brushes completely
392 #define Q2CONTENTS_SOLID                        1               // an eye is never valid in a solid
393 #define Q2CONTENTS_WINDOW                       2               // translucent, but not watery
394 #define Q2CONTENTS_AUX                  4
395 #define Q2CONTENTS_LAVA                 8
396 #define Q2CONTENTS_SLIME                        16
397 #define Q2CONTENTS_WATER                        32
398 #define Q2CONTENTS_MIST                 64
399 #define Q2LAST_VISIBLE_CONTENTS 64
400
401 // remaining contents are non-visible, and don't eat brushes
402
403 #define Q2CONTENTS_AREAPORTAL           0x8000
404
405 #define Q2CONTENTS_PLAYERCLIP           0x10000
406 #define Q2CONTENTS_MONSTERCLIP  0x20000
407
408 // currents can be added to any other contents, and may be mixed
409 #define Q2CONTENTS_CURRENT_0            0x40000
410 #define Q2CONTENTS_CURRENT_90           0x80000
411 #define Q2CONTENTS_CURRENT_180  0x100000
412 #define Q2CONTENTS_CURRENT_270  0x200000
413 #define Q2CONTENTS_CURRENT_UP           0x400000
414 #define Q2CONTENTS_CURRENT_DOWN 0x800000
415
416 #define Q2CONTENTS_ORIGIN                       0x1000000       // removed before bsping an entity
417
418 #define Q2CONTENTS_MONSTER              0x2000000       // should never be on a brush, only in game
419 #define Q2CONTENTS_DEADMONSTER  0x4000000
420 #define Q2CONTENTS_DETAIL                       0x8000000       // brushes to be added after vis leafs
421 #define Q2CONTENTS_TRANSLUCENT  0x10000000      // auto set if any surface has trans
422 #define Q2CONTENTS_LADDER                       0x20000000
423
424
425
426 #define Q2SURF_LIGHT            0x1             // value will hold the light strength
427
428 #define Q2SURF_SLICK            0x2             // effects game physics
429
430 #define Q2SURF_SKY              0x4             // don't draw, but add to skybox
431 #define Q2SURF_WARP             0x8             // turbulent water warp
432 #define Q2SURF_TRANS33  0x10
433 #define Q2SURF_TRANS66  0x20
434 #define Q2SURF_FLOWING  0x40    // scroll towards angle
435 #define Q2SURF_NODRAW           0x80    // don't bother referencing the texture
436
437
438
439
440 typedef struct
441 {
442         int                     planenum;
443         int                     children[2];    // negative numbers are -(leafs+1), not nodes
444         short           mins[3];                // for frustom culling
445         short           maxs[3];
446         unsigned short  firstface;
447         unsigned short  numfaces;       // counting both sides
448 } q2dnode_t;
449
450
451 typedef struct
452 {
453         float           vecs[2][4];             // [s/t][xyz offset]
454         int                     flags;                  // miptex flags + overrides
455         int                     value;                  // light emission, etc
456         char            texture[32];    // texture name (textures/*.wal)
457         int                     nexttexinfo;    // for animations, -1 = end of chain
458 } q2texinfo_t;
459
460 typedef struct
461 {
462         int                             contents;                       // OR of all brushes (not needed?)
463
464         short                   cluster;
465         short                   area;
466
467         short                   mins[3];                        // for frustum culling
468         short                   maxs[3];
469
470         unsigned short  firstleafface;
471         unsigned short  numleaffaces;
472
473         unsigned short  firstleafbrush;
474         unsigned short  numleafbrushes;
475 } q2dleaf_t;
476
477 typedef struct
478 {
479         unsigned short  planenum;               // facing out of the leaf
480         short   texinfo;
481 } q2dbrushside_t;
482
483 typedef struct
484 {
485         int                     firstside;
486         int                     numsides;
487         int                     contents;
488 } q2dbrush_t;
489
490
491 // the visibility lump consists of a header with a count, then
492 // byte offsets for the PVS and PHS of each cluster, then the raw
493 // compressed bit vectors
494 #define Q2DVIS_PVS      0
495 #define Q2DVIS_PHS      1
496 typedef struct
497 {
498         int                     numclusters;
499         int                     bitofs[8][2];   // bitofs[numclusters][2]
500 } q2dvis_t;
501
502 // each area has a list of portals that lead into other areas
503 // when portals are closed, other areas may not be visible or
504 // hearable even if the vis info says that it should be
505 typedef struct
506 {
507         int             portalnum;
508         int             otherarea;
509 } q2dareaportal_t;
510
511 typedef struct
512 {
513         int             numareaportals;
514         int             firstareaportal;
515 } q2darea_t;
516
517
518 //Q3 bsp stuff
519
520 #define Q3BSPVERSION    46
521
522 #define Q3LUMP_ENTITIES         0 // entities to spawn (used by server and client)
523 #define Q3LUMP_TEXTURES         1 // textures used (used by faces)
524 #define Q3LUMP_PLANES           2 // planes used (used by bsp nodes)
525 #define Q3LUMP_NODES            3 // bsp nodes (used by bsp nodes, bsp leafs, rendering, collisions)
526 #define Q3LUMP_LEAFS            4 // bsp leafs (used by bsp nodes)
527 #define Q3LUMP_LEAFFACES        5 // array of ints indexing faces (used by leafs)
528 #define Q3LUMP_LEAFBRUSHES      6 // array of ints indexing brushes (used by leafs)
529 #define Q3LUMP_MODELS           7 // models (used by rendering, collisions)
530 #define Q3LUMP_BRUSHES          8 // brushes (used by effects, collisions)
531 #define Q3LUMP_BRUSHSIDES       9 // brush faces (used by brushes)
532 #define Q3LUMP_VERTICES         10 // mesh vertices (used by faces)
533 #define Q3LUMP_TRIANGLES        11 // mesh triangles (used by faces)
534 #define Q3LUMP_EFFECTS          12 // fog (used by faces)
535 #define Q3LUMP_FACES            13 // surfaces (used by leafs)
536 #define Q3LUMP_LIGHTMAPS        14 // lightmap textures (used by faces)
537 #define Q3LUMP_LIGHTGRID        15 // lighting as a voxel grid (used by rendering)
538 #define Q3LUMP_PVS                      16 // potentially visible set; bit[clusters][clusters] (used by rendering)
539 #define Q3HEADER_LUMPS          17
540
541 #define Q3PATHLENGTH 64
542
543 typedef struct
544 {
545         int                     ident;
546         int                     version;
547         lump_t          lumps[HEADER_LUMPS];
548 } q3dheader_t;
549
550 typedef struct
551 {
552         char name[Q3PATHLENGTH];
553         int surfaceflags;
554         int contents;
555 }
556 q3dtexture_t;
557
558 // note: planes are paired, the pair of planes with i and i ^ 1 are opposites.
559 typedef struct
560 {
561         float normal[3];
562         float dist;
563 }
564 q3dplane_t;
565
566 typedef struct
567 {
568         int planeindex;
569         int childrenindex[2];
570         int mins[3];
571         int maxs[3];
572 }
573 q3dnode_t;
574
575 typedef struct
576 {
577         int clusterindex; // pvs index
578         int areaindex; // area index
579         int mins[3];
580         int maxs[3];
581         int firstleafface;
582         int numleaffaces;
583         int firstleafbrush;
584         int numleafbrushes;
585 }
586 q3dleaf_t;
587
588 typedef struct
589 {
590         float mins[3];
591         float maxs[3];
592         int firstface;
593         int numfaces;
594         int firstbrush;
595         int numbrushes;
596 }
597 q3dmodel_t;
598
599 typedef struct
600 {
601         int firstbrushside;
602         int numbrushsides;
603         int textureindex;
604 }
605 q3dbrush_t;
606
607 typedef struct
608 {
609         int planeindex;
610         int textureindex;
611 }
612 q3dbrushside_t;
613
614 typedef struct
615 {
616         float origin3f[3];
617         float texcoord2f[2];
618         float lightmap2f[2];
619         float normal3f[3];
620         unsigned char color4ub[4];
621 }
622 q3dvertex_t;
623
624 typedef struct
625 {
626         int offset; // first vertex index of mesh
627 }
628 q3dmeshvertex_t;
629
630 typedef struct
631 {
632         char shadername[Q3PATHLENGTH];
633         int brushindex;
634         int unknown; // I read this is always 5 except in q3dm8 which has one effect with -1
635 }
636 q3deffect_t;
637
638 #define Q3FACETYPE_POLYGON 1 // common
639 #define Q3FACETYPE_PATCH 2 // common
640 #define Q3FACETYPE_MESH 3 // common
641 #define Q3FACETYPE_FLARE 4 // rare (is this ever used?)
642
643 typedef struct
644 {
645         int textureindex;
646         int effectindex; // -1 if none
647         int type; // Q3FACETYPE
648         int firstvertex;
649         int numvertices;
650         int firstelement;
651         int numelements;
652         int lightmapindex; // -1 if none
653         int lightmap_base[2];
654         int lightmap_size[2];
655         union
656         {
657                 struct
658                 {
659                         // corrupt or don't care
660                         int blah[14];
661                 }
662                 unknown;
663                 struct
664                 {
665                         // Q3FACETYPE_POLYGON
666                         // polygon is simply a convex polygon, renderable as a mesh
667                         float lightmap_origin[3];
668                         float lightmap_vectors[2][3];
669                         float normal[3];
670                         int unused1[2];
671                 }
672                 polygon;
673                 struct
674                 {
675                         // Q3FACETYPE_PATCH
676                         // patch renders as a bezier mesh, with adjustable tesselation
677                         // level (optionally based on LOD using the bbox and polygon
678                         // count to choose a tesselation level)
679                         // note: multiple patches may have the same bbox to cause them to
680                         // be LOD adjusted together as a group
681                         int unused1[3];
682                         float mins[3]; // LOD bbox
683                         float maxs[3]; // LOD bbox
684                         int unused2[3];
685                         int patchsize[2]; // dimensions of vertex grid
686                 }
687                 patch;
688                 struct
689                 {
690                         // Q3FACETYPE_MESH
691                         // mesh renders as simply a triangle mesh
692                         int unused1[3];
693                         float mins[3];
694                         float maxs[3];
695                         int unused2[5];
696                 }
697                 mesh;
698                 struct
699                 {
700                         // Q3FACETYPE_FLARE
701                         // flare renders as a simple sprite at origin, no geometry
702                         // exists, nor does it have a radius, a cvar controls the radius
703                         // and another cvar controls distance fade
704                         // (they were not used in Q3 I'm told)
705                         float origin[3];
706                         int unused1[11];
707                 }
708                 flare;
709         }
710         specific;
711 }
712 q3dface_t;
713
714 typedef struct
715 {
716         unsigned char rgb[128*128*3];
717 }
718 q3dlightmap_t;
719
720 typedef struct
721 {
722         unsigned char ambientrgb[3];
723         unsigned char diffusergb[3];
724         unsigned char diffusepitch;
725         unsigned char diffuseyaw;
726 }
727 q3dlightgrid_t;
728
729 typedef struct
730 {
731         int numclusters;
732         int chainlength;
733         // unsigned char chains[];
734         // containing bits in 0-7 order (not 7-0 order),
735         // pvschains[mycluster * chainlength + (thatcluster >> 3)] & (1 << (thatcluster & 7))
736 }
737 q3dpvs_t;
738
739 #define CHECKPVSBIT(pvs,b) ((b) >= 0 ? ((pvs)[(b) >> 3] & (1 << ((b) & 7))) : false)
740 #define SETPVSBIT(pvs,b) ((b) >= 0 ? ((pvs)[(b) >> 3] |= (1 << ((b) & 7))) : false)
741 #define CLEARPVSBIT(pvs,b) ((b) >= 0 ? ((pvs)[(b) >> 3] &= ~(1 << ((b) & 7))) : false)
742
743 #endif
744