]> icculus.org git repositories - divverent/darkplaces.git/blob - model_shared.h
fix crashes when recursing portals on maps with some bogus leafs (where
[divverent/darkplaces.git] / model_shared.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_SHARED_H
22 #define MODEL_SHARED_H
23
24 typedef enum synctype_e {ST_SYNC=0, ST_RAND } synctype_t;
25
26 /*
27
28 d*_t structures are on-disk representations
29 m*_t structures are in-memory
30
31 */
32
33 typedef enum modtype_e {mod_invalid, mod_brushq1, mod_sprite, mod_alias, mod_brushq2, mod_brushq3} modtype_t;
34
35 typedef struct animscene_s
36 {
37         char name[32]; // for viewthing support
38         int firstframe;
39         int framecount;
40         int loop; // true or false
41         float framerate;
42 }
43 animscene_t;
44
45 typedef struct skinframe_s
46 {
47         rtexture_t *stain; // inverse modulate with background (used for decals and such)
48         rtexture_t *merged; // original texture without glow
49         rtexture_t *base; // original texture without pants/shirt/glow
50         rtexture_t *pants; // pants only (in greyscale)
51         rtexture_t *shirt; // shirt only (in greyscale)
52         rtexture_t *nmap; // normalmap (bumpmap for dot3)
53         rtexture_t *gloss; // glossmap (for dot3)
54         rtexture_t *glow; // glow only (fullbrights)
55         rtexture_t *fog; // alpha of the base texture (if not opaque)
56         // accounting data for hash searches:
57         // the compare variables are used to identify internal skins from certain
58         // model formats
59         // (so that two q1bsp maps with the same texture name for different
60         //  textures do not have any conflicts)
61         struct skinframe_s *next; // next on hash chain
62         char basename[MAX_QPATH]; // name of this
63         int textureflags; // texture flags to use
64         int comparewidth;
65         int compareheight;
66         int comparecrc;
67         // mark and sweep garbage collection, this value is updated to a new value
68         // on each level change for the used skinframes, if some are not used they
69         // are freed
70         int loadsequence;
71         // on 32bit systems this makes the struct 128 bytes long
72         int padding;
73 }
74 skinframe_t;
75
76 #define MAX_SKINS 256
77
78 struct md3vertex_s;
79 struct trivertx_s;
80 typedef struct texvecvertex_s
81 {
82         signed char svec[3];
83         signed char tvec[3];
84 }
85 texvecvertex_t;
86
87 // used for mesh lists in q1bsp/q3bsp map models
88 // (the surfaces reference portions of these meshes)
89 typedef struct surfmesh_s
90 {
91         // triangle data in system memory
92         int num_triangles; // number of triangles in the mesh
93         int *data_element3i; // int[tris*3] triangles of the mesh, 3 indices into vertex arrays for each
94         unsigned short *data_element3s; // unsigned short[tris*3] triangles of the mesh in unsigned short format (NULL if num_vertices > 65536)
95         int *data_neighbor3i; // int[tris*3] neighboring triangle on each edge (-1 if none)
96         // element buffer object (stores triangles in video memory)
97         int ebo3i; // unsigned int format (only allocated if num_vertices > 65536)
98         int ebo3s; // unsigned short format (only allocated if num_vertices <= 65536)
99         // vertex data in system memory
100         int num_vertices; // number of vertices in the mesh
101         float *data_vertex3f; // float[verts*3] vertex locations
102         float *data_svector3f; // float[verts*3] direction of 'S' (right) texture axis for each vertex
103         float *data_tvector3f; // float[verts*3] direction of 'T' (down) texture axis for each vertex
104         float *data_normal3f; // float[verts*3] direction of 'R' (out) texture axis for each vertex
105         float *data_texcoordtexture2f; // float[verts*2] texcoords for surface texture
106         float *data_texcoordlightmap2f; // float[verts*2] texcoords for lightmap texture
107         float *data_lightmapcolor4f;
108         int *data_lightmapoffsets; // index into surface's lightmap samples for vertex lighting
109         // vertex buffer object (stores geometry in video memory)
110         int vbo;
111         size_t vbooffset_vertex3f;
112         size_t vbooffset_svector3f;
113         size_t vbooffset_tvector3f;
114         size_t vbooffset_normal3f;
115         size_t vbooffset_texcoordtexture2f;
116         size_t vbooffset_texcoordlightmap2f;
117         size_t vbooffset_lightmapcolor4f;
118         // morph blending, these are zero if model is skeletal or static
119         int num_morphframes;
120         struct md3vertex_s *data_morphmd3vertex;
121         struct trivertx_s *data_morphmdlvertex;
122         struct texvecvertex_s *data_morphtexvecvertex;
123         float *data_morphmd2framesize6f;
124         float num_morphmdlframescale[3];
125         float num_morphmdlframetranslate[3];
126         // skeletal blending, these are NULL if model is morph or static
127         int *data_vertexweightindex4i;
128         float *data_vertexweightinfluence4f;
129         // set if there is some kind of animation on this model
130         qboolean isanimated;
131 }
132 surfmesh_t;
133
134 #define SHADOWMESHVERTEXHASH 1024
135 typedef struct shadowmeshvertexhash_s
136 {
137         struct shadowmeshvertexhash_s *next;
138 }
139 shadowmeshvertexhash_t;
140
141 typedef struct shadowmesh_s
142 {
143         // next mesh in chain
144         struct shadowmesh_s *next;
145         // used for light mesh (NULL on shadow mesh)
146         rtexture_t *map_diffuse;
147         rtexture_t *map_specular;
148         rtexture_t *map_normal;
149         // buffer sizes
150         int numverts, maxverts;
151         int numtriangles, maxtriangles;
152         // used always
153         float *vertex3f;
154         // used for light mesh (NULL on shadow mesh)
155         float *svector3f;
156         float *tvector3f;
157         float *normal3f;
158         float *texcoord2f;
159         // used always
160         int *element3i;
161         unsigned short *element3s;
162         // used for shadow mesh (NULL on light mesh)
163         int *neighbor3i;
164         // these are NULL after Mod_ShadowMesh_Finish is performed, only used
165         // while building meshes
166         shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
167         // element buffer object (stores triangles in video memory)
168         // (created by Mod_ShadowMesh_Finish if possible)
169         int ebo3i;
170         int ebo3s;
171         // vertex buffer object (stores vertices in video memory)
172         // (created by Mod_ShadowMesh_Finish if possible)
173         int vbo;
174         size_t vbooffset_vertex3f;
175         size_t vbooffset_svector3f;
176         size_t vbooffset_tvector3f;
177         size_t vbooffset_normal3f;
178         size_t vbooffset_texcoord2f;
179 }
180 shadowmesh_t;
181
182 // various flags from shaders, used for special effects not otherwise classified
183 // TODO: support these features more directly
184 #define Q3TEXTUREFLAG_TWOSIDED 1
185 #define Q3TEXTUREFLAG_NOPICMIP 16
186 #define Q3TEXTUREFLAG_POLYGONOFFSET 32
187 #define Q3TEXTUREFLAG_REFRACTION 256
188 #define Q3TEXTUREFLAG_REFLECTION 512
189 #define Q3TEXTUREFLAG_WATERSHADER 1024
190
191 #define Q3PATHLENGTH 64
192 #define TEXTURE_MAXFRAMES 64
193 #define Q3WAVEPARMS 4
194 #define Q3DEFORM_MAXPARMS 3
195 #define Q3SHADER_MAXLAYERS 2 // FIXME support more than that (currently only two are used, so why keep more in RAM?)
196 #define Q3RGBGEN_MAXPARMS 3
197 #define Q3ALPHAGEN_MAXPARMS 1
198 #define Q3TCGEN_MAXPARMS 6
199 #define Q3TCMOD_MAXPARMS 6
200 #define Q3MAXTCMODS 8
201 #define Q3MAXDEFORMS 4
202
203 typedef enum q3wavefunc_e
204 {
205         Q3WAVEFUNC_NONE,
206         Q3WAVEFUNC_INVERSESAWTOOTH,
207         Q3WAVEFUNC_NOISE,
208         Q3WAVEFUNC_SAWTOOTH,
209         Q3WAVEFUNC_SIN,
210         Q3WAVEFUNC_SQUARE,
211         Q3WAVEFUNC_TRIANGLE,
212         Q3WAVEFUNC_COUNT
213 }
214 q3wavefunc_t;
215
216 typedef enum q3deform_e
217 {
218         Q3DEFORM_NONE,
219         Q3DEFORM_PROJECTIONSHADOW,
220         Q3DEFORM_AUTOSPRITE,
221         Q3DEFORM_AUTOSPRITE2,
222         Q3DEFORM_TEXT0,
223         Q3DEFORM_TEXT1,
224         Q3DEFORM_TEXT2,
225         Q3DEFORM_TEXT3,
226         Q3DEFORM_TEXT4,
227         Q3DEFORM_TEXT5,
228         Q3DEFORM_TEXT6,
229         Q3DEFORM_TEXT7,
230         Q3DEFORM_BULGE,
231         Q3DEFORM_WAVE,
232         Q3DEFORM_NORMAL,
233         Q3DEFORM_MOVE,
234         Q3DEFORM_COUNT
235 }
236 q3deform_t;
237
238 typedef enum q3rgbgen_e
239 {
240         Q3RGBGEN_IDENTITY,
241         Q3RGBGEN_CONST,
242         Q3RGBGEN_ENTITY,
243         Q3RGBGEN_EXACTVERTEX,
244         Q3RGBGEN_IDENTITYLIGHTING,
245         Q3RGBGEN_LIGHTINGDIFFUSE,
246         Q3RGBGEN_ONEMINUSENTITY,
247         Q3RGBGEN_ONEMINUSVERTEX,
248         Q3RGBGEN_VERTEX,
249         Q3RGBGEN_WAVE,
250         Q3RGBGEN_COUNT
251 }
252 q3rgbgen_t;
253
254 typedef enum q3alphagen_e
255 {
256         Q3ALPHAGEN_IDENTITY,
257         Q3ALPHAGEN_CONST,
258         Q3ALPHAGEN_ENTITY,
259         Q3ALPHAGEN_LIGHTINGSPECULAR,
260         Q3ALPHAGEN_ONEMINUSENTITY,
261         Q3ALPHAGEN_ONEMINUSVERTEX,
262         Q3ALPHAGEN_PORTAL,
263         Q3ALPHAGEN_VERTEX,
264         Q3ALPHAGEN_WAVE,
265         Q3ALPHAGEN_COUNT
266 }
267 q3alphagen_t;
268
269 typedef enum q3tcgen_e
270 {
271         Q3TCGEN_NONE,
272         Q3TCGEN_TEXTURE, // very common
273         Q3TCGEN_ENVIRONMENT, // common
274         Q3TCGEN_LIGHTMAP,
275         Q3TCGEN_VECTOR,
276         Q3TCGEN_COUNT
277 }
278 q3tcgen_t;
279
280 typedef enum q3tcmod_e
281 {
282         Q3TCMOD_NONE,
283         Q3TCMOD_ENTITYTRANSLATE,
284         Q3TCMOD_ROTATE,
285         Q3TCMOD_SCALE,
286         Q3TCMOD_SCROLL,
287         Q3TCMOD_STRETCH,
288         Q3TCMOD_TRANSFORM,
289         Q3TCMOD_TURBULENT,
290         Q3TCMOD_COUNT
291 }
292 q3tcmod_t;
293
294 typedef struct q3shaderinfo_layer_rgbgen_s
295 {
296         q3rgbgen_t rgbgen;
297         float parms[Q3RGBGEN_MAXPARMS];
298         q3wavefunc_t wavefunc;
299         float waveparms[Q3WAVEPARMS];
300 }
301 q3shaderinfo_layer_rgbgen_t;
302
303 typedef struct q3shaderinfo_layer_alphagen_s
304 {
305         q3alphagen_t alphagen;
306         float parms[Q3ALPHAGEN_MAXPARMS];
307         q3wavefunc_t wavefunc;
308         float waveparms[Q3WAVEPARMS];
309 }
310 q3shaderinfo_layer_alphagen_t;
311
312 typedef struct q3shaderinfo_layer_tcgen_s
313 {
314         q3tcgen_t tcgen;
315         float parms[Q3TCGEN_MAXPARMS];
316 }
317 q3shaderinfo_layer_tcgen_t;
318
319 typedef struct q3shaderinfo_layer_tcmod_s
320 {
321         q3tcmod_t tcmod;
322         float parms[Q3TCMOD_MAXPARMS];
323         q3wavefunc_t wavefunc;
324         float waveparms[Q3WAVEPARMS];
325 }
326 q3shaderinfo_layer_tcmod_t;
327
328 typedef struct q3shaderinfo_layer_s
329 {
330         int alphatest;
331         int clampmap;
332         float framerate;
333         int numframes;
334         int texflags;
335         char** texturename;
336         int blendfunc[2];
337         q3shaderinfo_layer_rgbgen_t rgbgen;
338         q3shaderinfo_layer_alphagen_t alphagen;
339         q3shaderinfo_layer_tcgen_t tcgen;
340         q3shaderinfo_layer_tcmod_t tcmods[Q3MAXTCMODS];
341 }
342 q3shaderinfo_layer_t;
343
344 typedef struct q3shaderinfo_deform_s
345 {
346         q3deform_t deform;
347         float parms[Q3DEFORM_MAXPARMS];
348         q3wavefunc_t wavefunc;
349         float waveparms[Q3WAVEPARMS];
350 }
351 q3shaderinfo_deform_t;
352
353 typedef struct q3shaderinfo_s
354 {
355         char name[Q3PATHLENGTH];
356         int surfaceparms;
357         int textureflags;
358         int numlayers;
359         qboolean lighting;
360         qboolean vertexalpha;
361         qboolean textureblendalpha;
362         int primarylayer, backgroundlayer;
363         q3shaderinfo_layer_t layers[Q3SHADER_MAXLAYERS];
364         char skyboxname[Q3PATHLENGTH];
365         q3shaderinfo_deform_t deforms[Q3MAXDEFORMS];
366
367         // reflection
368         float reflectmin; // when refraction is used, minimum amount of reflection (when looking straight down)
369         float reflectmax; // when refraction is used, maximum amount of reflection (when looking parallel to water)
370         float refractfactor; // amount of refraction distort (1.0 = like the cvar specifies)
371         vec4_t refractcolor4f; // color tint of refraction (including alpha factor)
372         float reflectfactor; // amount of reflection distort (1.0 = like the cvar specifies)
373         vec4_t reflectcolor4f; // color tint of reflection (including alpha factor)
374         float r_water_wateralpha; // additional wateralpha to apply when r_water is active
375 }
376 q3shaderinfo_t;
377
378 typedef enum texturelayertype_e
379 {
380         TEXTURELAYERTYPE_INVALID,
381         TEXTURELAYERTYPE_LITTEXTURE,
382         TEXTURELAYERTYPE_TEXTURE,
383         TEXTURELAYERTYPE_FOG,
384 }
385 texturelayertype_t;
386
387 typedef enum texturelayerflag_e
388 {
389         // indicates that the pass should apply fog darkening; used on
390         // transparent surfaces where simply blending an alpha fog as a final
391         // pass would not behave properly, so all the surfaces must be darkened,
392         // and the fog color added as a separate pass
393         TEXTURELAYERFLAG_FOGDARKEN = 1,
394 }
395 texturelayerflag_t;
396
397 typedef struct texturelayer_s
398 {
399         texturelayertype_t type;
400         qboolean depthmask;
401         int blendfunc1;
402         int blendfunc2;
403         rtexture_t *texture;
404         matrix4x4_t texmatrix;
405         vec4_t color;
406         int flags;
407 }
408 texturelayer_t;
409
410 typedef struct texture_s
411 {
412         // q1bsp
413         // name
414         //char name[16];
415         // size
416         unsigned int width, height;
417         // SURF_ flags
418         //unsigned int flags;
419
420         // base material flags
421         int basematerialflags;
422         // current material flags (updated each bmodel render)
423         int currentmaterialflags;
424
425         // PolygonOffset values for rendering this material
426         // (these are added to the r_refdef values and submodel values)
427         float biaspolygonfactor;
428         float biaspolygonoffset;
429
430         // textures to use when rendering this material
431         skinframe_t *currentskinframe;
432         int numskinframes;
433         float skinframerate;
434         skinframe_t *skinframes[TEXTURE_MAXFRAMES];
435         // background layer (for terrain texture blending)
436         skinframe_t *backgroundcurrentskinframe;
437         int backgroundnumskinframes;
438         float backgroundskinframerate;
439         skinframe_t *backgroundskinframes[TEXTURE_MAXFRAMES];
440
441         // total frames in sequence and alternate sequence
442         int anim_total[2];
443         // direct pointers to each of the frames in the sequences
444         // (indexed as [alternate][frame])
445         struct texture_s *anim_frames[2][10];
446         // set if animated or there is an alternate frame set
447         // (this is an optimization in the renderer)
448         int animated;
449
450         // the current alpha of this texture (may be affected by r_wateralpha)
451         float currentalpha;
452         // the current texture frame in animation
453         struct texture_s *currentframe;
454         // current texture transform matrix (used for water scrolling)
455         matrix4x4_t currenttexmatrix;
456
457         // various q3 shader features
458         q3shaderinfo_layer_rgbgen_t rgbgen;
459         q3shaderinfo_layer_alphagen_t alphagen;
460         q3shaderinfo_layer_tcgen_t tcgen;
461         q3shaderinfo_layer_tcmod_t tcmods[Q3MAXTCMODS];
462         q3shaderinfo_deform_t deforms[Q3MAXDEFORMS];
463
464         qboolean colormapping;
465         rtexture_t *basetexture;
466         rtexture_t *glosstexture;
467         rtexture_t *backgroundbasetexture;
468         rtexture_t *backgroundglosstexture;
469         float specularscale;
470         float specularpower;
471         // color tint (colormod * currentalpha) used for rtlighting this material
472         float dlightcolor[3];
473         // color tint (colormod * 2) used for lightmapped lighting on this material
474         // includes alpha as 4th component
475         // replaces role of gl_Color in GLSL shader
476         float lightmapcolor[4];
477
478         // from q3 shaders
479         int customblendfunc[2];
480
481         int currentnumlayers;
482         texturelayer_t currentlayers[16];
483
484         // q3bsp
485         char name[64];
486         int surfaceflags;
487         int supercontents;
488         int surfaceparms;
489         int textureflags;
490
491         // reflection
492         float reflectmin; // when refraction is used, minimum amount of reflection (when looking straight down)
493         float reflectmax; // when refraction is used, maximum amount of reflection (when looking parallel to water)
494         float refractfactor; // amount of refraction distort (1.0 = like the cvar specifies)
495         vec4_t refractcolor4f; // color tint of refraction (including alpha factor)
496         float reflectfactor; // amount of reflection distort (1.0 = like the cvar specifies)
497         vec4_t reflectcolor4f; // color tint of reflection (including alpha factor)
498         float r_water_wateralpha; // additional wateralpha to apply when r_water is active
499 }
500 texture_t;
501
502 typedef struct mtexinfo_s
503 {
504         float vecs[2][4];
505         texture_t *texture;
506         int flags;
507 }
508 mtexinfo_t;
509
510 typedef struct msurface_lightmapinfo_s
511 {
512         // texture mapping properties used by this surface
513         mtexinfo_t *texinfo; // q1bsp
514         // index into r_refdef.scene.lightstylevalue array, 255 means not used (black)
515         unsigned char styles[MAXLIGHTMAPS]; // q1bsp
516         // RGB lighting data [numstyles][height][width][3]
517         unsigned char *samples; // q1bsp
518         // RGB normalmap data [numstyles][height][width][3]
519         unsigned char *nmapsamples; // q1bsp
520         // stain to apply on lightmap (soot/dirt/blood/whatever)
521         unsigned char *stainsamples; // q1bsp
522         int texturemins[2]; // q1bsp
523         int extents[2]; // q1bsp
524         int lightmaporigin[2]; // q1bsp
525 }
526 msurface_lightmapinfo_t;
527
528 struct q3deffect_s;
529 typedef struct msurface_s
530 {
531         // bounding box for onscreen checks
532         vec3_t mins;
533         vec3_t maxs;
534         // the texture to use on the surface
535         texture_t *texture;
536         // the lightmap texture fragment to use on the rendering mesh
537         rtexture_t *lightmaptexture;
538         // the lighting direction texture fragment to use on the rendering mesh
539         rtexture_t *deluxemaptexture;
540
541         // surfaces own ranges of vertices and triangles in the model->surfmesh
542         int num_triangles; // number of triangles
543         int num_firsttriangle; // first triangle
544         int num_vertices; // number of vertices
545         int num_firstvertex; // first vertex
546
547         // shadow volume building information
548         int num_firstshadowmeshtriangle; // index into model->brush.shadowmesh
549
550         // lightmaptexture rebuild information not used in q3bsp
551         msurface_lightmapinfo_t *lightmapinfo; // q1bsp
552
553         // mesh information for collisions (only used by q3bsp curves)
554         int num_collisiontriangles; // q3bsp
555         int *data_collisionelement3i; // q3bsp
556         int num_collisionvertices; // q3bsp
557         float *data_collisionvertex3f; // q3bsp
558         struct q3deffect_s *effect; // q3bsp
559         // FIXME: collisionmarkframe should be kept in a separate array
560         int collisionmarkframe; // q3bsp // don't collide twice in one trace
561 }
562 msurface_t;
563
564 #include "matrixlib.h"
565
566 #include "model_brush.h"
567 #include "model_sprite.h"
568 #include "model_alias.h"
569
570 typedef struct model_sprite_s
571 {
572         int                             sprnum_type;
573         mspriteframe_t  *sprdata_frames;
574 }
575 model_sprite_t;
576
577 struct trace_s;
578
579 typedef struct model_brush_lightstyleinfo_s
580 {
581         int style;
582         int value;
583         int numsurfaces;
584         int *surfacelist;
585 }
586 model_brush_lightstyleinfo_t;
587
588 typedef struct model_brush_s
589 {
590         // true if this model is a HalfLife .bsp file
591         qboolean ishlbsp;
592         // string of entity definitions (.map format)
593         char *entities;
594
595         // if non-zero this is a submodel
596         // (this is the number of the submodel, an index into submodels)
597         int submodel;
598
599         // number of submodels in this map (just used by server to know how many
600         // submodels to load)
601         int numsubmodels;
602         // pointers to each of the submodels if .isworldmodel is true
603         struct model_s **submodels;
604
605         int num_planes;
606         mplane_t *data_planes;
607
608         int num_nodes;
609         mnode_t *data_nodes;
610
611         // visible leafs, not counting 0 (solid)
612         int num_visleafs;
613         // number of actual leafs (including 0 which is solid)
614         int num_leafs;
615         mleaf_t *data_leafs;
616
617         int num_leafbrushes;
618         int *data_leafbrushes;
619
620         int num_leafsurfaces;
621         int *data_leafsurfaces;
622
623         int num_portals;
624         mportal_t *data_portals;
625
626         int num_portalpoints;
627         mvertex_t *data_portalpoints;
628
629         int num_brushes;
630         q3mbrush_t *data_brushes;
631
632         int num_brushsides;
633         q3mbrushside_t *data_brushsides;
634
635         // pvs
636         int num_pvsclusters;
637         int num_pvsclusterbytes;
638         unsigned char *data_pvsclusters;
639         // example
640         //pvschain = model->brush.data_pvsclusters + mycluster * model->brush.num_pvsclusterbytes;
641         //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
642
643         // a mesh containing all shadow casting geometry for the whole model (including submodels), portions of this are referenced by each surface's num_firstshadowmeshtriangle
644         shadowmesh_t *shadowmesh;
645
646         // common functions
647         int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
648         int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
649         unsigned char *(*GetPVS)(struct model_s *model, const vec3_t p);
650         int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, unsigned char *pvsbuffer, int pvsbufferlength, qboolean merge);
651         int (*BoxTouchingPVS)(struct model_s *model, const unsigned char *pvs, const vec3_t mins, const vec3_t maxs);
652         int (*BoxTouchingLeafPVS)(struct model_s *model, const unsigned char *pvs, const vec3_t mins, const vec3_t maxs);
653         int (*BoxTouchingVisibleLeafs)(struct model_s *model, const unsigned char *visibleleafs, const vec3_t mins, const vec3_t maxs);
654         int (*FindBoxClusters)(struct model_s *model, const vec3_t mins, const vec3_t maxs, int maxclusters, int *clusterlist);
655         void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
656         void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
657         mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
658         // these are actually only found on brushq1, but NULL is handled gracefully
659         void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, unsigned char *out, int outsize);
660         void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
661         // trace a line of sight through this model (returns false if the line if sight is definitely blocked)
662         qboolean (*TraceLineOfSight)(struct model_s *model, const vec3_t start, const vec3_t end);
663
664         char skybox[MAX_QPATH];
665
666         rtexture_t *solidskytexture;
667         rtexture_t *alphaskytexture;
668
669         qboolean supportwateralpha;
670
671         // QuakeWorld
672         int qw_md4sum;
673         int qw_md4sum2;
674 }
675 model_brush_t;
676
677 typedef struct model_brushq1_s
678 {
679         dmodel_t                *submodels;
680
681         int                             numvertexes;
682         mvertex_t               *vertexes;
683
684         int                             numedges;
685         medge_t                 *edges;
686
687         int                             numtexinfo;
688         mtexinfo_t              *texinfo;
689
690         int                             numsurfedges;
691         int                             *surfedges;
692
693         int                             numclipnodes;
694         mclipnode_t             *clipnodes;
695
696         hull_t                  hulls[MAX_MAP_HULLS];
697
698         int                             num_compressedpvs;
699         unsigned char                   *data_compressedpvs;
700
701         int                             num_lightdata;
702         unsigned char                   *lightdata;
703         unsigned char                   *nmaplightdata; // deluxemap file
704
705         // lightmap update chains for light styles
706         int                             num_lightstyles;
707         model_brush_lightstyleinfo_t *data_lightstyleinfo;
708
709         // this contains bytes that are 1 if a surface needs its lightmap rebuilt
710         unsigned char *lightmapupdateflags;
711 }
712 model_brushq1_t;
713
714 /* MSVC can't compile empty structs, so this is commented out for now
715 typedef struct model_brushq2_s
716 {
717 }
718 model_brushq2_t;
719 */
720
721 typedef struct model_brushq3_s
722 {
723         int num_models;
724         q3dmodel_t *data_models;
725
726         // used only during loading - freed after loading!
727         int num_vertices;
728         float *data_vertex3f;
729         float *data_normal3f;
730         float *data_texcoordtexture2f;
731         float *data_texcoordlightmap2f;
732         float *data_color4f;
733
734         // freed after loading!
735         int num_triangles;
736         int *data_element3i;
737
738         int num_effects;
739         q3deffect_t *data_effects;
740
741         // lightmap textures
742         int num_originallightmaps;
743         int num_mergedlightmaps;
744         int num_lightmapmergepower;
745         int num_lightmapmerge;
746         rtexture_t **data_lightmaps;
747         rtexture_t **data_deluxemaps;
748
749         // voxel light data with directional shading
750         int num_lightgrid;
751         q3dlightgrid_t *data_lightgrid;
752         // size of each cell (may vary by map, typically 64 64 128)
753         float num_lightgrid_cellsize[3];
754         // 1.0 / num_lightgrid_cellsize
755         float num_lightgrid_scale[3];
756         // dimensions of the world model in lightgrid cells
757         int num_lightgrid_imins[3];
758         int num_lightgrid_imaxs[3];
759         int num_lightgrid_isize[3];
760         // indexing/clamping
761         int num_lightgrid_dimensions[3];
762         // transform modelspace coordinates to lightgrid index
763         matrix4x4_t num_lightgrid_indexfromworld;
764
765         // true if this q3bsp file has been detected as using deluxemapping
766         // (lightmap texture pairs, every odd one is never directly refernced,
767         //  and contains lighting normals, not colors)
768         qboolean deluxemapping;
769         // true if the detected deluxemaps are the modelspace kind, rather than
770         // the faster tangentspace kind
771         qboolean deluxemapping_modelspace;
772 }
773 model_brushq3_t;
774
775 struct frameblend_s;
776
777 typedef struct model_s
778 {
779         // name and path of model, for example "progs/player.mdl"
780         char                    name[MAX_QPATH];
781         // model needs to be loaded if this is false
782         qboolean                loaded;
783         // set if the model is used in current map, models which are not, are purged
784         qboolean                used;
785         // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
786         qboolean                isworldmodel;
787         // CRC of the file this model was loaded from, to reload if changed
788         unsigned int    crc;
789         // mod_brush, mod_alias, mod_sprite
790         modtype_t               type;
791         // memory pool for allocations
792         mempool_t               *mempool;
793         // all models use textures...
794         rtexturepool_t  *texturepool;
795         // EF_* flags (translated from the model file's different flags layout)
796         int                             effects;
797         // number of QC accessible frame(group)s in the model
798         int                             numframes;
799         // number of QC accessible skin(group)s in the model
800         int                             numskins;
801         // whether to randomize animated framegroups
802         synctype_t              synctype;
803         // bounding box at angles '0 0 0'
804         vec3_t                  normalmins, normalmaxs;
805         // bounding box if yaw angle is not 0, but pitch and roll are
806         vec3_t                  yawmins, yawmaxs;
807         // bounding box if pitch or roll are used
808         vec3_t                  rotatedmins, rotatedmaxs;
809         // sphere radius, usable at any angles
810         float                   radius;
811         // squared sphere radius for easier comparisons
812         float                   radius2;
813         // skin animation info
814         animscene_t             *skinscenes; // [numskins]
815         // skin animation info
816         animscene_t             *animscenes; // [numframes]
817         // range of surface numbers in this (sub)model
818         int                             firstmodelsurface;
819         int                             nummodelsurfaces;
820         // range of collision brush numbers in this (sub)model
821         int                             firstmodelbrush;
822         int                             nummodelbrushes;
823         // list of surface numbers in this (sub)model
824         int                             *surfacelist;
825         // for md3 models
826         int                             num_tags;
827         int                             num_tagframes;
828         aliastag_t              *data_tags;
829         // for skeletal models
830         int                             num_bones;
831         aliasbone_t             *data_bones;
832         int                             num_poses;
833         float                   *data_poses;
834         float                   *data_baseboneposeinverse;
835         // textures of this model
836         int                             num_textures;
837         int                             num_texturesperskin;
838         texture_t               *data_textures;
839         // surfaces of this model
840         int                             num_surfaces;
841         msurface_t              *data_surfaces;
842         // optional lightmapinfo data for surface lightmap updates
843         msurface_lightmapinfo_t *data_surfaces_lightmapinfo;
844         // all surfaces belong to this mesh
845         surfmesh_t              surfmesh;
846         // data type of model
847         const char              *modeldatatypestring;
848         // generates vertex data for a given frameblend
849         void(*AnimateVertices)(const struct model_s *model, const struct frameblend_s *frameblend, float *vertex3f, float *normal3f, float *svector3f, float *tvector3f);
850         // draw the model's sky polygons (only used by brush models)
851         void(*DrawSky)(struct entity_render_s *ent);
852         // draw refraction/reflection textures for the model's water polygons (only used by brush models)
853         void(*DrawAddWaterPlanes)(struct entity_render_s *ent);
854         // draw the model using lightmap/dlight shading
855         void(*Draw)(struct entity_render_s *ent);
856         // draw the model to the depth buffer (no color rendering at all)
857         void(*DrawDepth)(struct entity_render_s *ent);
858         // draw any enabled debugging effects on this model (such as showing triangles, normals, collision brushes...)
859         void(*DrawDebug)(struct entity_render_s *ent);
860         // gathers info on which clusters and surfaces are lit by light, as well as calculating a bounding box
861         void(*GetLightInfo)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outleaflist, unsigned char *outleafpvs, int *outnumleafspointer, int *outsurfacelist, unsigned char *outsurfacepvs, int *outnumsurfacespointer, unsigned char *outshadowtrispvs, unsigned char *outlighttrispvs, unsigned char *visitingleafpvs);
862         // compile a shadow volume for the model based on light source
863         void(*CompileShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist);
864         // draw a shadow volume for the model based on light source
865         void(*DrawShadowVolume)(struct entity_render_s *ent, const vec3_t relativelightorigin, const vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist, const vec3_t lightmins, const vec3_t lightmaxs);
866         // draw the lighting on a model (through stencil)
867         void(*DrawLight)(struct entity_render_s *ent, int numsurfaces, const int *surfacelist, const unsigned char *trispvs);
868         // trace a box against this model
869         void (*TraceBox)(struct model_s *model, int frame, struct trace_s *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask);
870         // find the supercontents value at a point in this model
871         int (*PointSuperContents)(struct model_s *model, int frame, const vec3_t point);
872         // fields belonging to some types of model
873         model_sprite_t  sprite;
874         model_brush_t   brush;
875         model_brushq1_t brushq1;
876         /* MSVC can't handle an empty struct, so this is commented out for now
877         model_brushq2_t brushq2;
878         */
879         model_brushq3_t brushq3;
880         // flags this model for offseting sounds to the model center (used by brush models)
881         int soundfromcenter;
882 }
883 dp_model_t;
884
885 //============================================================================
886
887 // model loading
888 extern dp_model_t *loadmodel;
889 extern unsigned char *mod_base;
890 // sky/water subdivision
891 //extern cvar_t gl_subdivide_size;
892 // texture fullbrights
893 extern cvar_t r_fullbrights;
894
895 void Mod_Init (void);
896 void Mod_Reload (void);
897 dp_model_t *Mod_LoadModel(dp_model_t *mod, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
898 dp_model_t *Mod_FindName (const char *name);
899 dp_model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
900 void Mod_UnloadModel (dp_model_t *mod);
901
902 void Mod_ClearUsed(void);
903 void Mod_PurgeUnused(void);
904 void Mod_RemoveStaleWorldModels(dp_model_t *skip); // only used during loading!
905
906 extern dp_model_t *loadmodel;
907 extern char loadname[32];       // for hunk tags
908
909 int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements, int numvertices, int *remapvertices);
910 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
911 void Mod_ValidateElements(int *elements, int numtriangles, int firstvertex, int numverts, const char *filename, int fileline);
912 void Mod_BuildNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const int *elements, float *normal3f, qboolean areaweighting);
913 void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const float *texcoord2f, const float *normal3f, const int *elements, float *svector3f, float *tvector3f, qboolean areaweighting);
914
915 void Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qboolean lightmapoffsets, qboolean vertexcolors, qboolean neighbors);
916
917 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts, int maxtriangles, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, int light, int neighbors, int expandable);
918 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light, int neighbors);
919 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f);
920 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex14f);
921 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *texcoord2f, int numtris, const int *element3i);
922 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int maxverts, int maxtriangles, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, int light, int neighbors, int expandable);
923 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, qboolean light, qboolean neighbors, qboolean createvbo);
924 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
925 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
926
927 void Mod_LoadQ3Shaders(void);
928 q3shaderinfo_t *Mod_LookupQ3Shader(const char *name);
929 qboolean Mod_LoadTextureFromQ3Shader(texture_t *texture, const char *name, qboolean warnmissing, qboolean fallback, int defaulttexflags);
930
931 extern cvar_t r_mipskins;
932
933 typedef struct skinfileitem_s
934 {
935         struct skinfileitem_s *next;
936         char name[MAX_QPATH];
937         char replacement[MAX_QPATH];
938 }
939 skinfileitem_t;
940
941 typedef struct skinfile_s
942 {
943         struct skinfile_s *next;
944         skinfileitem_t *items;
945 }
946 skinfile_t;
947
948 skinfile_t *Mod_LoadSkinFiles(void);
949 void Mod_FreeSkinFiles(skinfile_t *skinfile);
950 int Mod_CountSkinFiles(skinfile_t *skinfile);
951
952 void Mod_SnapVertices(int numcomponents, int numvertices, float *vertices, float snap);
953 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);
954 void Mod_VertexRangeFromElements(int numelements, const int *elements, int *firstvertexpointer, int *lastvertexpointer);
955
956 // bsp models
957 void Mod_BrushInit(void);
958 // used for talking to the QuakeC mainly
959 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
960 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
961
962 // a lot of model formats use the Q1BSP code, so here are the prototypes...
963 struct entity_render_s;
964 void R_Q1BSP_DrawAddWaterPlanes(struct entity_render_s *ent);
965 void R_Q1BSP_DrawSky(struct entity_render_s *ent);
966 void R_Q1BSP_Draw(struct entity_render_s *ent);
967 void R_Q1BSP_DrawDepth(struct entity_render_s *ent);
968 void R_Q1BSP_DrawDebug(struct entity_render_s *ent);
969 void R_Q1BSP_GetLightInfo(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outleaflist, unsigned char *outleafpvs, int *outnumleafspointer, int *outsurfacelist, unsigned char *outsurfacepvs, int *outnumsurfacespointer, unsigned char *outshadowtrispvs, unsigned char *outlighttrispvs, unsigned char *visitingleafpvs);
970 void R_Q1BSP_CompileShadowVolume(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist);
971 void R_Q1BSP_DrawShadowVolume(struct entity_render_s *ent, const vec3_t relativelightorigin, const vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist, const vec3_t lightmins, const vec3_t lightmaxs);
972 void R_Q1BSP_DrawLight(struct entity_render_s *ent, int numsurfaces, const int *surfacelist, const unsigned char *trispvs);
973
974 // alias models
975 struct frameblend_s;
976 void Mod_AliasInit(void);
977 int Mod_Alias_GetTagMatrix(const dp_model_t *model, int poseframe, int tagindex, matrix4x4_t *outmatrix);
978 int Mod_Alias_GetTagIndexForName(const dp_model_t *model, unsigned int skin, const char *tagname);
979
980 // sprite models
981 void Mod_SpriteInit(void);
982
983 // loaders
984 void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend);
985 void Mod_IBSP_Load(dp_model_t *mod, void *buffer, void *bufferend);
986 void Mod_MAP_Load(dp_model_t *mod, void *buffer, void *bufferend);
987 void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend);
988 void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend);
989 void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend);
990 void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend);
991 void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend);
992 void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend);
993 void Mod_IDSP_Load(dp_model_t *mod, void *buffer, void *bufferend);
994 void Mod_IDS2_Load(dp_model_t *mod, void *buffer, void *bufferend);
995
996 // utility
997 qboolean Mod_CanSeeBox_Trace(int numsamples, float t, dp_model_t *model, vec3_t eye, vec3_t minsX, vec3_t maxsX);
998
999 #endif  // MODEL_SHARED_H
1000