2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
21 #ifndef MODEL_SHARED_H
22 #define MODEL_SHARED_H
24 typedef enum {ST_SYNC=0, ST_RAND } synctype_t;
28 d*_t structures are on-disk representations
29 m*_t structures are in-memory
33 typedef enum {mod_invalid, mod_brush, mod_sprite, mod_alias, mod_brushq2, mod_brushq3} modtype_t;
35 typedef struct animscene_s
37 char name[32]; // for viewthing support
40 int loop; // true or false
45 typedef struct skinframe_s
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 *detail; // detail texture (silly bumps for non-dot3)
55 rtexture_t *glow; // glow only (fullbrights)
56 rtexture_t *fog; // alpha of the base texture (if not opaque)
62 typedef struct overridetagname_s
68 // a replacement set of tag names, per skin
69 typedef struct overridetagnameset_s
71 int num_overridetagnames;
72 overridetagname_t *data_overridetagnames;
76 #define SHADOWMESHVERTEXHASH 1024
77 typedef struct shadowmeshvertexhash_s
79 struct shadowmeshvertexhash_s *next;
81 shadowmeshvertexhash_t;
83 typedef struct shadowmesh_s
86 struct shadowmesh_s *next;
87 // used for light mesh (NULL on shadow mesh)
88 rtexture_t *map_diffuse;
89 rtexture_t *map_specular;
90 rtexture_t *map_normal;
92 int numverts, maxverts;
93 int numtriangles, maxtriangles;
96 // used for light mesh (NULL on shadow mesh)
103 // used for shadow mesh (NULL on light mesh)
105 // these are NULL after Mod_ShadowMesh_Finish is performed, only used
106 // while building meshes
107 shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
112 #include "matrixlib.h"
114 #include "model_brush.h"
115 #include "model_sprite.h"
116 #include "model_alias.h"
118 typedef struct model_alias_s
120 // mdl/md2/md3/zym model formats are treated the same after loading
122 // the shader meshes comprising this model
124 aliasmesh_t *aliasdata_meshes;
128 int aliasnum_tagframes;
129 aliastag_t *aliasdata_tags;
131 // for skeletal models
133 aliasbone_t *aliasdata_bones;
135 float *aliasdata_poses;
139 typedef struct model_sprite_s
142 mspriteframe_t *sprdata_frames;
148 typedef struct model_brush_s
150 // true if this model is a HalfLife .bsp file
152 // string of entity definitions (.map format)
154 // number of submodels in this map (just used by server to know how many
155 // submodels to load)
160 int num_pvsclusterbytes;
161 unsigned char *data_pvsclusters;
163 //pvschain = model->brush.data_pvsclusters + mycluster * model->brush.num_pvsclusterbytes;
164 //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
166 // a mesh containing all shadow casting geometry for the whole model (including submodels), portions of this are referenced by each surface's num_firstshadowmeshtriangle
167 shadowmesh_t *shadowmesh;
170 int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
171 int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
172 qbyte *(*GetPVS)(struct model_s *model, const vec3_t p);
173 int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength);
174 int (*BoxTouchingPVS)(struct model_s *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs);
175 void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
176 void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
177 // these are actually only found on brushq1, but NULL is handled gracefully
178 void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, qbyte *out, int outsize);
179 void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
185 typedef struct model_brushq1_s
187 // lightmap format, set to r_lightmaprgba when model is loaded
195 // number of actual leafs (including 0 which is solid)
197 // visible leafs, not counting 0 (solid)
214 msurface_t *surfaces;
215 int *surfacevisframes;
216 int *surfacepvsframes;
217 msurface_t *surfacepvsnext;
218 surfmesh_t *entiremesh;
224 dclipnode_t *clipnodes;
229 hull_t hulls[MAX_MAP_HULLS];
234 int num_compressedpvs;
235 qbyte *data_compressedpvs;
244 mvertex_t *portalpoints;
249 // pvs visibility marking
250 mleaf_t *pvsviewleaf;
251 int pvsviewleafnovis;
253 mleaf_t *pvsleafchain;
255 int pvssurflistlength;
256 // these get rebuilt as the player moves around if this is the world,
257 // otherwise they are left alone (no pvs for bmodels)
258 msurface_t ***pvstexturechains;
259 msurface_t **pvstexturechainsbuffer;
260 int *pvstexturechainslength;
262 // lightmap update chains for light styles
265 int *light_stylevalue;
266 msurface_t ***light_styleupdatechains;
267 msurface_t **light_styleupdatechainsbuffer;
269 mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
270 void (*BuildPVSTextureChains)(struct model_s *model);
274 /* MSVC can't compile empty structs, so this is commented out for now
275 typedef struct model_brushq2_s
281 // surfaceflags from bsp
282 #define Q3SURFACEFLAG_NODAMAGE 1
283 #define Q3SURFACEFLAG_SLICK 2
284 #define Q3SURFACEFLAG_SKY 4
285 #define Q3SURFACEFLAG_LADDER 8
286 #define Q3SURFACEFLAG_NOIMPACT 16
287 #define Q3SURFACEFLAG_NOMARKS 32
288 #define Q3SURFACEFLAG_FLESH 64
289 #define Q3SURFACEFLAG_NODRAW 128
290 #define Q3SURFACEFLAG_HINT 256
291 #define Q3SURFACEFLAG_SKIP 512
292 #define Q3SURFACEFLAG_NOLIGHTMAP 1024
293 #define Q3SURFACEFLAG_POINTLIGHT 2048
294 #define Q3SURFACEFLAG_METALSTEPS 4096
295 #define Q3SURFACEFLAG_NOSTEPS 8192
296 #define Q3SURFACEFLAG_NONSOLID 16384
297 #define Q3SURFACEFLAG_LIGHTFILTER 32768
298 #define Q3SURFACEFLAG_ALPHASHADOW 65536
299 #define Q3SURFACEFLAG_NODLIGHT 131072
300 #define Q3SURFACEFLAG_DUST 262144
302 // surfaceparms from shaders
303 #define Q3SURFACEPARM_ALPHASHADOW 1
304 #define Q3SURFACEPARM_AREAPORTAL 2
305 #define Q3SURFACEPARM_CLUSTERPORTAL 4
306 #define Q3SURFACEPARM_DETAIL 8
307 #define Q3SURFACEPARM_DONOTENTER 16
308 #define Q3SURFACEPARM_FOG 32
309 #define Q3SURFACEPARM_LAVA 64
310 #define Q3SURFACEPARM_LIGHTFILTER 128
311 #define Q3SURFACEPARM_METALSTEPS 256
312 #define Q3SURFACEPARM_NODAMAGE 512
313 #define Q3SURFACEPARM_NODLIGHT 1024
314 #define Q3SURFACEPARM_NODRAW 2048
315 #define Q3SURFACEPARM_NODROP 4096
316 #define Q3SURFACEPARM_NOIMPACT 8192
317 #define Q3SURFACEPARM_NOLIGHTMAP 16384
318 #define Q3SURFACEPARM_NOMARKS 32768
319 #define Q3SURFACEPARM_NOMIPMAPS 65536
320 #define Q3SURFACEPARM_NONSOLID 131072
321 #define Q3SURFACEPARM_ORIGIN 262144
322 #define Q3SURFACEPARM_PLAYERCLIP 524288
323 #define Q3SURFACEPARM_SKY 1048576
324 #define Q3SURFACEPARM_SLICK 2197152
325 #define Q3SURFACEPARM_SLIME 4194304
326 #define Q3SURFACEPARM_STRUCTURAL 8388608
327 #define Q3SURFACEPARM_TRANS 16777216
328 #define Q3SURFACEPARM_WATER 33554432
330 typedef struct q3mtexture_s
332 char name[Q3PATHLENGTH];
343 typedef struct q3mnode_s
345 //this part shared between node and leaf
346 struct mplane_s *plane; // != NULL
347 struct q3mnode_s *parent;
350 // this part unique to nodes
351 struct q3mnode_s *children[2];
355 typedef struct q3mleaf_s
357 //this part shared between node and leaf
358 struct mplane_s *plane; // == NULL
359 struct q3mnode_s *parent;
362 // this part unique to leafs
366 struct q3mface_s **firstleafface;
368 struct q3mbrush_s **firstleafbrush;
372 typedef struct q3mmodel_s
377 struct q3mface_s *firstface;
379 struct q3mbrush_s *firstbrush;
383 typedef struct q3mbrush_s
385 struct colbrushf_s *colbrushf;
387 struct q3mbrushside_s *firstbrushside;
388 struct q3mtexture_s *texture;
392 typedef struct q3mbrushside_s
394 struct mplane_s *plane;
395 struct q3mtexture_s *texture;
399 typedef struct q3meffect_s
401 char shadername[Q3PATHLENGTH];
402 struct q3mbrush_s *brush;
403 int unknown; // 5 or -1
407 typedef struct q3mface_s
409 struct q3mtexture_s *texture;
410 struct q3meffect_s *effect;
411 rtexture_t *lightmaptexture;
412 int collisions; // performs per triangle collisions on this surface
413 int collisionmarkframe; // don't collide twice in one trace
418 // used for processing
420 // (world only) visframe == r_framecount means it is visible this frame
422 // bounding box for culling
428 float *data_vertex3f;
429 float *data_texcoordtexture2f;
430 float *data_texcoordlightmap2f;
431 float *data_svector3f;
432 float *data_tvector3f;
433 float *data_normal3f;
436 int *data_neighbor3i;
438 int num_collisionvertices;
439 int num_collisiontriangles;
440 float *data_collisionvertex3f;
441 int *data_collisionelement3i;
443 // index into model->brush.shadowmesh
444 int num_firstshadowmeshtriangle;
446 // used for shadow volume generation
449 // temporary use by light processing
450 int lighttemp_castshadow;
454 typedef struct model_brushq3_s
456 // if non-zero this is a submodel
457 // (this is the number of the submodel, an index into data_models)
461 q3mtexture_t *data_textures;
464 mplane_t *data_planes;
467 q3mnode_t *data_nodes;
470 q3mleaf_t *data_leafs;
473 q3mbrush_t **data_leafbrushes;
476 q3msurface_t **data_leaffaces;
479 q3mmodel_t *data_models;
480 // each submodel gets its own model struct so this is different for each.
481 q3mmodel_t *data_thismodel;
484 q3mbrush_t *data_brushes;
487 q3mbrushside_t *data_brushsides;
490 float *data_vertex3f;
491 float *data_texcoordtexture2f;
492 float *data_texcoordlightmap2f;
493 float *data_svector3f;
494 float *data_tvector3f;
495 float *data_normal3f;
500 int *data_neighbor3i;
503 q3meffect_t *data_effects;
506 q3msurface_t *data_faces;
510 rtexture_t **data_lightmaps;
512 // voxel light data with directional shading
514 q3dlightgrid_t *data_lightgrid;
515 // size of each cell (may vary by map, typically 64 64 128)
516 float num_lightgrid_cellsize[3];
517 // 1.0 / num_lightgrid_cellsize
518 float num_lightgrid_scale[3];
519 // dimensions of the world model in lightgrid cells
520 int num_lightgrid_imins[3];
521 int num_lightgrid_imaxs[3];
522 int num_lightgrid_isize[3];
524 int num_lightgrid_dimensions[3];
525 // transform modelspace coordinates to lightgrid index
526 matrix4x4_t num_lightgrid_indexfromworld;
530 typedef struct model_s
532 // name and path of model, for example "progs/player.mdl"
533 char name[MAX_QPATH];
534 // model needs to be loaded if this is true
536 // set if the model is used in current map, models which are not, are purged
538 // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
539 qboolean isworldmodel;
540 // CRC of the file this model was loaded from, to reload if changed
542 // mod_brush, mod_alias, mod_sprite
544 // memory pool for allocations
546 // all models use textures...
547 rtexturepool_t *texturepool;
548 // flags from the model file
550 // engine calculated flags, ones that can not be set in the file
552 // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
554 // number of QC accessible frame(group)s in the model
556 // number of QC accessible skin(group)s in the model
558 // whether to randomize animated framegroups
560 // bounding box at angles '0 0 0'
561 vec3_t normalmins, normalmaxs;
562 // bounding box if yaw angle is not 0, but pitch and roll are
563 vec3_t yawmins, yawmaxs;
564 // bounding box if pitch or roll are used
565 vec3_t rotatedmins, rotatedmaxs;
566 // sphere radius, usable at any angles
568 // squared sphere radius for easier comparisons
570 // skin animation info
571 animscene_t *skinscenes; // [numskins]
572 // skin animation info
573 animscene_t *animscenes; // [numframes]
574 // range of surface numbers in this (sub)model
575 int firstmodelsurface;
576 int nummodelsurfaces;
577 // list of surface numbers in this (sub)model
579 // draw the model's sky polygons (only used by brush models)
580 void(*DrawSky)(struct entity_render_s *ent);
581 // draw the model using lightmap/dlight shading
582 void(*Draw)(struct entity_render_s *ent);
583 // gathers info on which clusters and surfaces are lit by light, as well as calculating a bounding box
584 void(*GetLightInfo)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer);
585 // draw a shadow volume for the model based on light source
586 void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist);
587 // draw the lighting on a model (through stencil)
588 void(*DrawLight)(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, int numsurfaces, const int *surfacelist);
589 // trace a box against this model
590 void (*TraceBox)(struct model_s *model, int frame, struct trace_s *trace, const vec3_t boxstartmins, const vec3_t boxstartmaxs, const vec3_t boxendmins, const vec3_t boxendmaxs, int hitsupercontentsmask);
591 // fields belonging to each type of model
593 model_sprite_t sprite;
595 model_brushq1_t brushq1;
596 /* MSVC can't handle an empty struct, so this is commented out for now
597 model_brushq2_t brushq2;
599 model_brushq3_t brushq3;
600 // skin files can have different tags for each skin
601 overridetagnameset_t *data_overridetagnamesforskin;
602 // flags this model for offseting sounds to the model center (used by brush models)
607 //============================================================================
609 // this can be used for anything without a valid texture
610 extern rtexture_t *r_notexture;
611 #define NUM_DETAILTEXTURES 1
612 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
613 // every texture must be in a pool...
614 extern rtexturepool_t *mod_shared_texturepool;
616 extern rtexture_t *mod_shared_distorttexture[64];
619 extern model_t *loadmodel;
620 extern qbyte *mod_base;
621 // sky/water subdivision
622 //extern cvar_t gl_subdivide_size;
623 // texture fullbrights
624 extern cvar_t r_fullbrights;
626 void Mod_Init (void);
627 void Mod_CheckLoaded (model_t *mod);
628 void Mod_ClearAll (void);
629 model_t *Mod_FindName (const char *name);
630 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
631 void Mod_TouchModel (const char *name);
632 void Mod_UnloadModel (model_t *mod);
634 void Mod_ClearUsed(void);
635 void Mod_PurgeUnused(void);
636 void Mod_LoadModels(void);
638 extern model_t *loadmodel;
639 extern char loadname[32]; // for hunk tags
641 int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements, int numvertices, int *remapvertices);
642 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
643 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
644 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
646 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);
647 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light, int neighbors);
648 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f);
649 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex14f);
650 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);
651 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);
652 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, int light, int neighbors);
653 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
654 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
656 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
657 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
659 // used for talking to the QuakeC mainly
660 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
661 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
663 extern cvar_t r_mipskins;
665 typedef struct skinfileitem_s
667 struct skinfileitem_s *next;
668 char name[MAX_QPATH];
669 char replacement[MAX_QPATH];
673 typedef struct skinfile_s
675 struct skinfile_s *next;
676 skinfileitem_t *items;
680 skinfile_t *Mod_LoadSkinFiles(void);
681 void Mod_FreeSkinFiles(skinfile_t *skinfile);
682 int Mod_CountSkinFiles(skinfile_t *skinfile);
684 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);
686 #endif // MODEL_SHARED_H