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.
26 typedef enum {ST_SYNC=0, ST_RAND } synctype_t;
31 d*_t structures are on-disk representations
32 m*_t structures are in-memory
36 typedef enum {mod_invalid, mod_brush, mod_sprite, mod_alias, mod_brushq2, mod_brushq3} modtype_t;
38 typedef struct animscene_s
40 char name[32]; // for viewthing support
43 int loop; // true or false
48 typedef struct skinframe_s
50 rtexture_t *stain; // inverse modulate with background (used for decals and such)
51 rtexture_t *merged; // original texture without glow
52 rtexture_t *base; // original texture without pants/shirt/glow
53 rtexture_t *pants; // pants only (in greyscale)
54 rtexture_t *shirt; // shirt only (in greyscale)
55 rtexture_t *nmap; // normalmap (bumpmap for dot3)
56 rtexture_t *gloss; // glossmap (for dot3)
57 rtexture_t *detail; // detail texture (silly bumps for non-dot3)
58 rtexture_t *glow; // glow only (fullbrights)
59 rtexture_t *fog; // alpha of the base texture (if not opaque)
65 typedef struct overridetagname_s
71 // a replacement set of tag names, per skin
72 typedef struct overridetagnameset_s
74 int num_overridetagnames;
75 overridetagname_t *data_overridetagnames;
79 #define SHADOWMESHVERTEXHASH 1024
80 typedef struct shadowmeshvertexhash_s
82 struct shadowmeshvertexhash_s *next;
84 shadowmeshvertexhash_t;
86 typedef struct shadowmesh_s
89 struct shadowmesh_s *next;
90 // used for light mesh (NULL on shadow mesh)
91 rtexture_t *map_diffuse;
92 rtexture_t *map_specular;
93 rtexture_t *map_normal;
95 int numverts, maxverts;
96 int numtriangles, maxtriangles;
99 // used for light mesh (NULL on shadow mesh)
106 // used for shadow mesh (NULL on light mesh)
108 // these are NULL after Mod_ShadowMesh_Finish is performed, only used
109 // while building meshes
110 shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
115 #include "matrixlib.h"
117 #include "model_brush.h"
118 #include "model_sprite.h"
119 #include "model_alias.h"
121 typedef struct model_alias_s
123 // LordHavoc: Q2/ZYM model support
126 // mdl/md2/md3 models are the same after loading
128 aliasmesh_t *aliasdata_meshes;
131 int aliasnum_tagframes;
132 aliastag_t *aliasdata_tags;
134 // for Zymotic models
140 float *zymdata_texcoords;
141 rtexture_t **zymdata_textures;
142 qbyte *zymdata_trizone;
143 zymbone_t *zymdata_bones;
144 unsigned int *zymdata_vertbonecounts;
145 zymvertex_t *zymdata_verts;
146 unsigned int *zymdata_renderlist;
147 float *zymdata_poses;
151 typedef struct model_sprite_s
154 mspriteframe_t *sprdata_frames;
160 typedef struct model_brush_s
162 // true if this model is a HalfLife .bsp file
164 // string of entity definitions (.map format)
166 // number of submodels in this map (just used by server to know how many
167 // submodels to load)
172 int num_pvsclusterbytes;
173 unsigned char *data_pvsclusters;
175 //pvschain = model->brush.data_pvsclusters + mycluster * model->brush.num_pvsclusterbytes;
176 //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
178 // a mesh containing all shadow casting geometry for the whole model (including submodels), portions of this are referenced by each surface's num_firstshadowmeshtriangle
179 shadowmesh_t *shadowmesh;
182 int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
183 int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
184 qbyte *(*GetPVS)(struct model_s *model, const vec3_t p);
185 int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength);
186 int (*BoxTouchingPVS)(struct model_s *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs);
187 void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
188 void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
189 // these are actually only found on brushq1, but NULL is handled gracefully
190 void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, qbyte *out, int outsize);
191 void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
197 typedef struct model_brushq1_s
199 int firstmodelsurface, nummodelsurfaces;
201 // lightmap format, set to r_lightmaprgba when model is loaded
209 // number of actual leafs (including 0 which is solid)
211 // visible leafs, not counting 0 (solid)
228 msurface_t *surfaces;
229 int *surfacevisframes;
230 int *surfacepvsframes;
231 msurface_t *surfacepvsnext;
232 surfmesh_t *entiremesh;
238 dclipnode_t *clipnodes;
243 hull_t hulls[MAX_MAP_HULLS];
248 int num_compressedpvs;
249 qbyte *data_compressedpvs;
258 mvertex_t *portalpoints;
263 // pvs visibility marking
264 mleaf_t *pvsviewleaf;
265 int pvsviewleafnovis;
267 mleaf_t *pvsleafchain;
269 int pvssurflistlength;
270 // these get rebuilt as the player moves around if this is the world,
271 // otherwise they are left alone (no pvs for bmodels)
272 msurface_t ***pvstexturechains;
273 msurface_t **pvstexturechainsbuffer;
274 int *pvstexturechainslength;
276 // lightmap update chains for light styles
279 int *light_stylevalue;
280 msurface_t ***light_styleupdatechains;
281 msurface_t **light_styleupdatechainsbuffer;
284 mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
285 void (*BuildPVSTextureChains)(struct model_s *model);
289 /* MSVC can't compile empty structs, so this is commented out for now
290 typedef struct model_brushq2_s
296 // surfaceflags from bsp
297 #define Q3SURFACEFLAG_NODAMAGE 1
298 #define Q3SURFACEFLAG_SLICK 2
299 #define Q3SURFACEFLAG_SKY 4
300 #define Q3SURFACEFLAG_LADDER 8
301 #define Q3SURFACEFLAG_NOIMPACT 16
302 #define Q3SURFACEFLAG_NOMARKS 32
303 #define Q3SURFACEFLAG_FLESH 64
304 #define Q3SURFACEFLAG_NODRAW 128
305 #define Q3SURFACEFLAG_HINT 256
306 #define Q3SURFACEFLAG_SKIP 512
307 #define Q3SURFACEFLAG_NOLIGHTMAP 1024
308 #define Q3SURFACEFLAG_POINTLIGHT 2048
309 #define Q3SURFACEFLAG_METALSTEPS 4096
310 #define Q3SURFACEFLAG_NOSTEPS 8192
311 #define Q3SURFACEFLAG_NONSOLID 16384
312 #define Q3SURFACEFLAG_LIGHTFILTER 32768
313 #define Q3SURFACEFLAG_ALPHASHADOW 65536
314 #define Q3SURFACEFLAG_NODLIGHT 131072
315 #define Q3SURFACEFLAG_DUST 262144
317 // surfaceparms from shaders
318 #define Q3SURFACEPARM_ALPHASHADOW 1
319 #define Q3SURFACEPARM_AREAPORTAL 2
320 #define Q3SURFACEPARM_CLUSTERPORTAL 4
321 #define Q3SURFACEPARM_DETAIL 8
322 #define Q3SURFACEPARM_DONOTENTER 16
323 #define Q3SURFACEPARM_FOG 32
324 #define Q3SURFACEPARM_LAVA 64
325 #define Q3SURFACEPARM_LIGHTFILTER 128
326 #define Q3SURFACEPARM_METALSTEPS 256
327 #define Q3SURFACEPARM_NODAMAGE 512
328 #define Q3SURFACEPARM_NODLIGHT 1024
329 #define Q3SURFACEPARM_NODRAW 2048
330 #define Q3SURFACEPARM_NODROP 4096
331 #define Q3SURFACEPARM_NOIMPACT 8192
332 #define Q3SURFACEPARM_NOLIGHTMAP 16384
333 #define Q3SURFACEPARM_NOMARKS 32768
334 #define Q3SURFACEPARM_NOMIPMAPS 65536
335 #define Q3SURFACEPARM_NONSOLID 131072
336 #define Q3SURFACEPARM_ORIGIN 262144
337 #define Q3SURFACEPARM_PLAYERCLIP 524288
338 #define Q3SURFACEPARM_SKY 1048576
339 #define Q3SURFACEPARM_SLICK 2197152
340 #define Q3SURFACEPARM_SLIME 4194304
341 #define Q3SURFACEPARM_STRUCTURAL 8388608
342 #define Q3SURFACEPARM_TRANS 16777216
343 #define Q3SURFACEPARM_WATER 33554432
345 typedef struct q3mtexture_s
347 char name[Q3PATHLENGTH];
358 typedef struct q3mnode_s
360 //this part shared between node and leaf
361 struct mplane_s *plane; // != NULL
362 struct q3mnode_s *parent;
365 // this part unique to nodes
366 struct q3mnode_s *children[2];
370 typedef struct q3mleaf_s
372 //this part shared between node and leaf
373 struct mplane_s *plane; // == NULL
374 struct q3mnode_s *parent;
377 // this part unique to leafs
381 struct q3mface_s **firstleafface;
383 struct q3mbrush_s **firstleafbrush;
387 typedef struct q3mmodel_s
392 struct q3mface_s *firstface;
394 struct q3mbrush_s *firstbrush;
398 typedef struct q3mbrush_s
400 struct colbrushf_s *colbrushf;
402 struct q3mbrushside_s *firstbrushside;
403 struct q3mtexture_s *texture;
407 typedef struct q3mbrushside_s
409 struct mplane_s *plane;
410 struct q3mtexture_s *texture;
414 typedef struct q3meffect_s
416 char shadername[Q3PATHLENGTH];
417 struct q3mbrush_s *brush;
418 int unknown; // 5 or -1
422 typedef struct q3mface_s
424 struct q3mtexture_s *texture;
425 struct q3meffect_s *effect;
426 rtexture_t *lightmaptexture;
427 int collisions; // performs per triangle collisions on this surface
428 int collisionmarkframe; // don't collide twice in one trace
433 // used for processing
435 // (world only) visframe == r_framecount means it is visible this frame
437 // bounding box for culling
443 float *data_vertex3f;
444 float *data_texcoordtexture2f;
445 float *data_texcoordlightmap2f;
446 float *data_svector3f;
447 float *data_tvector3f;
448 float *data_normal3f;
451 int *data_neighbor3i;
453 int num_collisionvertices;
454 int num_collisiontriangles;
455 float *data_collisionvertex3f;
456 int *data_collisionelement3i;
458 // index into model->brush.shadowmesh
459 int num_firstshadowmeshtriangle;
461 // used for shadow volume generation
464 // temporary use by light processing
465 int lighttemp_castshadow;
469 typedef struct model_brushq3_s
471 // if non-zero this is a submodel
472 // (this is the number of the submodel, an index into data_models)
476 q3mtexture_t *data_textures;
479 mplane_t *data_planes;
482 q3mnode_t *data_nodes;
485 q3mleaf_t *data_leafs;
488 q3mbrush_t **data_leafbrushes;
491 q3mface_t **data_leaffaces;
494 q3mmodel_t *data_models;
495 // each submodel gets its own model struct so this is different for each.
496 q3mmodel_t *data_thismodel;
499 q3mbrush_t *data_brushes;
502 q3mbrushside_t *data_brushsides;
505 float *data_vertex3f;
506 float *data_texcoordtexture2f;
507 float *data_texcoordlightmap2f;
508 float *data_svector3f;
509 float *data_tvector3f;
510 float *data_normal3f;
515 int *data_neighbor3i;
518 q3meffect_t *data_effects;
521 q3mface_t *data_faces;
525 rtexture_t **data_lightmaps;
527 // voxel light data with directional shading
529 q3dlightgrid_t *data_lightgrid;
530 // size of each cell (may vary by map, typically 64 64 128)
531 float num_lightgrid_cellsize[3];
532 // 1.0 / num_lightgrid_cellsize
533 float num_lightgrid_scale[3];
534 // dimensions of the world model in lightgrid cells
535 int num_lightgrid_imins[3];
536 int num_lightgrid_imaxs[3];
537 int num_lightgrid_isize[3];
539 int num_lightgrid_dimensions[3];
540 // transform modelspace coordinates to lightgrid index
541 matrix4x4_t num_lightgrid_indexfromworld;
545 typedef struct model_s
547 // name and path of model, for example "progs/player.mdl"
548 char name[MAX_QPATH];
549 // model needs to be loaded if this is true
551 // set if the model is used in current map, models which are not, are purged
553 // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
554 qboolean isworldmodel;
555 // CRC of the file this model was loaded from, to reload if changed
557 // mod_brush, mod_alias, mod_sprite
559 // memory pool for allocations
561 // all models use textures...
562 rtexturepool_t *texturepool;
563 // flags from the model file
565 // engine calculated flags, ones that can not be set in the file
567 // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
569 // number of QC accessible frame(group)s in the model
571 // number of QC accessible skin(group)s in the model
573 // whether to randomize animated framegroups
575 // bounding box at angles '0 0 0'
576 vec3_t normalmins, normalmaxs;
577 // bounding box if yaw angle is not 0, but pitch and roll are
578 vec3_t yawmins, yawmaxs;
579 // bounding box if pitch or roll are used
580 vec3_t rotatedmins, rotatedmaxs;
581 // sphere radius, usable at any angles
583 // squared sphere radius for easier comparisons
585 // skin animation info
586 animscene_t *skinscenes; // [numskins]
587 // skin animation info
588 animscene_t *animscenes; // [numframes]
589 // draw the model's sky polygons (only used by brush models)
590 void(*DrawSky)(struct entity_render_s *ent);
591 // draw the model using lightmap/dlight shading
592 void(*Draw)(struct entity_render_s *ent);
593 // draw a shadow volume for the model based on light source
594 void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
595 // draw the lighting on a model (through stencil)
596 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);
597 // trace a box against this model
598 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);
599 // fields belonging to each type of model
601 model_sprite_t sprite;
603 model_brushq1_t brushq1;
604 /* MSVC can't handle an empty struct, so this is commented out for now
605 model_brushq2_t brushq2;
607 model_brushq3_t brushq3;
608 // skin files can have different tags for each skin
609 overridetagnameset_t *data_overridetagnamesforskin;
610 // flags this model for offseting sounds to the model center (used by brush models)
615 //============================================================================
617 // this can be used for anything without a valid texture
618 extern rtexture_t *r_notexture;
619 #define NUM_DETAILTEXTURES 1
620 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
621 // every texture must be in a pool...
622 extern rtexturepool_t *mod_shared_texturepool;
624 extern rtexture_t *mod_shared_distorttexture[64];
627 extern model_t *loadmodel;
628 extern qbyte *mod_base;
629 // sky/water subdivision
630 //extern cvar_t gl_subdivide_size;
631 // texture fullbrights
632 extern cvar_t r_fullbrights;
634 void Mod_Init (void);
635 void Mod_CheckLoaded (model_t *mod);
636 void Mod_ClearAll (void);
637 model_t *Mod_FindName (const char *name);
638 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
639 void Mod_TouchModel (const char *name);
640 void Mod_UnloadModel (model_t *mod);
642 void Mod_ClearUsed(void);
643 void Mod_PurgeUnused(void);
644 void Mod_LoadModels(void);
646 extern model_t *loadmodel;
647 extern char loadname[32]; // for hunk tags
649 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
650 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
651 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
653 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);
654 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light, int neighbors);
655 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f);
656 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex14f);
657 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);
658 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);
659 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, int light, int neighbors);
660 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
661 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
663 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
664 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
666 // used for talking to the QuakeC mainly
667 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
668 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
670 extern cvar_t r_mipskins;
672 typedef struct skinfileitem_s
674 struct skinfileitem_s *next;
675 char name[MAX_QPATH];
676 char replacement[MAX_QPATH];
680 typedef struct skinfile_s
682 struct skinfile_s *next;
683 skinfileitem_t *items;
687 skinfile_t *Mod_LoadSkinFiles(void);
688 void Mod_FreeSkinFiles(skinfile_t *skinfile);
689 int Mod_CountSkinFiles(skinfile_t *skinfile);
691 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);