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 *base; // original texture without pants/shirt/glow
51 rtexture_t *pants; // pants only (in greyscale)
52 rtexture_t *shirt; // shirt only (in greyscale)
53 rtexture_t *glow; // glow only (fullbrights)
54 rtexture_t *merged; // original texture without glow
55 rtexture_t *fog; // alpha of the base texture (if not opaque)
56 rtexture_t *nmap; // normalmap (bumpmap for dot3)
57 rtexture_t *gloss; // glossmap (for dot3)
58 rtexture_t *detail; // detail texture (silly bumps for non-dot3)
64 typedef struct overridetagname_s
70 // a replacement set of tag names, per skin
71 typedef struct overridetagnameset_s
73 int num_overridetagnames;
74 overridetagname_t *data_overridetagnames;
78 #define SHADOWMESHVERTEXHASH 1024
79 typedef struct shadowmeshvertexhash_s
81 struct shadowmeshvertexhash_s *next;
83 shadowmeshvertexhash_t;
85 typedef struct shadowmesh_s
88 struct shadowmesh_s *next;
89 // used for light mesh (NULL on shadow mesh)
90 rtexture_t *map_diffuse;
91 rtexture_t *map_specular;
92 rtexture_t *map_normal;
94 int numverts, maxverts;
95 int numtriangles, maxtriangles;
98 // used for light mesh (NULL on shadow mesh)
105 // used for shadow mesh (NULL on light mesh)
107 // these are NULL after Mod_ShadowMesh_Finish is performed, only used
108 // while building meshes
109 shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
114 #include "matrixlib.h"
116 #include "model_brush.h"
117 #include "model_sprite.h"
118 #include "model_alias.h"
120 typedef struct model_alias_s
122 // LordHavoc: Q2/ZYM model support
125 // mdl/md2/md3 models are the same after loading
127 aliasmesh_t *aliasdata_meshes;
130 int aliasnum_tagframes;
131 aliastag_t *aliasdata_tags;
133 // for Zymotic models
139 float *zymdata_texcoords;
140 rtexture_t **zymdata_textures;
141 qbyte *zymdata_trizone;
142 zymbone_t *zymdata_bones;
143 unsigned int *zymdata_vertbonecounts;
144 zymvertex_t *zymdata_verts;
145 unsigned int *zymdata_renderlist;
146 float *zymdata_poses;
150 typedef struct model_sprite_s
153 mspriteframe_t *sprdata_frames;
159 typedef struct model_brush_s
161 // true if this model is a HalfLife .bsp file
163 // string of entity definitions (.map format)
165 // number of submodels in this map (just used by server to know how many
166 // submodels to load)
169 int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
170 int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
171 qbyte *(*GetPVS)(struct model_s *model, const vec3_t p);
172 int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength);
173 int (*BoxTouchingPVS)(struct model_s *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs);
174 void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
175 void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
176 // these are actually only found on brushq1, but NULL is handled gracefully
177 void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, qbyte *out, int outsize);
178 void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
182 typedef struct model_brushq1_s
184 int firstmodelsurface, nummodelsurfaces;
186 // lightmap format, set to r_lightmaprgba when model is loaded
194 // number of actual leafs (including 0 which is solid)
196 // visible leafs, not counting 0 (solid)
213 msurface_t *surfaces;
214 int *surfacevisframes;
215 int *surfacepvsframes;
216 msurface_t *surfacepvsnext;
217 surfmesh_t *entiremesh;
223 dclipnode_t *clipnodes;
228 hull_t hulls[MAX_MAP_HULLS];
233 int num_compressedpvs;
234 qbyte *data_compressedpvs;
235 qbyte *data_decompressedpvs;
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;
270 mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
271 void (*BuildPVSTextureChains)(struct model_s *model);
275 /* MSVC can't compile empty structs, so this is commented out for now
276 typedef struct model_brushq2_s
283 #define SURFACEPARM_ALPHASHADOW 1
284 #define SURFACEPARM_AREAPORTAL 2
285 #define SURFACEPARM_CLUSTERPORTAL 4
286 #define SURFACEPARM_DETAIL 8
287 #define SURFACEPARM_DONOTENTER 16
288 #define SURFACEPARM_FOG 32
289 #define SURFACEPARM_LAVA 64
290 #define SURFACEPARM_LIGHTFILTER 128
291 #define SURFACEPARM_METALSTEPS 256
292 #define SURFACEPARM_NODAMAGE 512
293 #define SURFACEPARM_NODLIGHT 1024
294 #define SURFACEPARM_NODRAW 2048
295 #define SURFACEPARM_NODROP 4096
296 #define SURFACEPARM_NOIMPACT 8192
297 #define SURFACEPARM_NOLIGHTMAP 16384
298 #define SURFACEPARM_NOMARKS 32768
299 #define SURFACEPARM_NOMIPMAPS 65536
300 #define SURFACEPARM_NONSOLID 131072
301 #define SURFACEPARM_ORIGIN 262144
302 #define SURFACEPARM_PLAYERCLIP 524288
303 #define SURFACEPARM_SKY 1048576
304 #define SURFACEPARM_SLICK 2197152
305 #define SURFACEPARM_SLIME 4194304
306 #define SURFACEPARM_STRUCTURAL 8388608
307 #define SURFACEPARM_TRANS 16777216
308 #define SURFACEPARM_WATER 33554432
310 typedef struct q3mtexture_s
312 char name[Q3PATHLENGTH];
323 typedef struct q3mnode_s
325 //this part shared between node and leaf
327 struct q3mnode_s *parent;
330 // this part unique to nodes
331 struct mplane_s *plane;
332 struct q3mnode_s *children[2];
336 typedef struct q3mleaf_s
338 //this part shared between node and leaf
340 struct q3mnode_s *parent;
343 // this part unique to leafs
347 struct q3mface_s **firstleafface;
349 struct q3mbrush_s **firstleafbrush;
353 typedef struct q3mmodel_s
358 struct q3mface_s *firstface;
360 struct q3mbrush_s *firstbrush;
364 typedef struct q3mbrush_s
366 struct colbrushf_s *colbrushf;
368 struct q3mbrushside_s *firstbrushside;
369 struct q3mtexture_s *texture;
373 typedef struct q3mbrushside_s
375 struct mplane_s *plane;
376 struct q3mtexture_s *texture;
380 typedef struct q3meffect_s
382 char shadername[Q3PATHLENGTH];
383 struct q3mbrush_s *brush;
384 int unknown; // 5 or -1
388 typedef struct q3mface_s
390 struct q3mtexture_s *texture;
391 struct q3meffect_s *effect;
392 rtexture_t *lightmaptexture;
393 int collisions; // performs per triangle collisions on this surface
394 int collisionmarkframe; // don't collide twice in one trace
399 // used for processing
401 // (world only) visframe == r_framecount means it is visible this frame
403 // bounding box for culling
409 float *data_vertex3f;
410 float *data_texcoordtexture2f;
411 float *data_texcoordlightmap2f;
412 float *data_svector3f;
413 float *data_tvector3f;
414 float *data_normal3f;
417 int *data_neighbor3i;
419 // temporary use by light processing
420 int lighttemp_castshadow;
424 typedef struct model_brushq3_s
426 // if non-zero this is a submodel
427 // (this is the number of the submodel, an index into data_models)
431 q3mtexture_t *data_textures;
434 mplane_t *data_planes;
437 q3mnode_t *data_nodes;
440 q3mleaf_t *data_leafs;
443 q3mbrush_t **data_leafbrushes;
446 q3mface_t **data_leaffaces;
449 q3mmodel_t *data_models;
450 // each submodel gets its own model struct so this is different for each.
451 q3mmodel_t *data_thismodel;
454 q3mbrush_t *data_brushes;
457 q3mbrushside_t *data_brushsides;
460 float *data_vertex3f;
461 float *data_texcoordtexture2f;
462 float *data_texcoordlightmap2f;
463 float *data_svector3f;
464 float *data_tvector3f;
465 float *data_normal3f;
470 int *data_neighbor3i;
473 q3meffect_t *data_effects;
476 q3mface_t *data_faces;
480 rtexture_t **data_lightmaps;
482 // voxel light data with directional shading
484 q3dlightgrid_t *data_lightgrid;
485 // size of each cell (may vary by map, typically 64 64 128)
486 float num_lightgrid_cellsize[3];
487 // 1.0 / num_lightgrid_cellsize
488 float num_lightgrid_scale[3];
489 // dimensions of the world model in lightgrid cells
490 int num_lightgrid_imins[3];
491 int num_lightgrid_imaxs[3];
492 int num_lightgrid_isize[3];
494 int num_lightgrid_dimensions[3];
495 // transform modelspace coordinates to lightgrid index
496 matrix4x4_t num_lightgrid_indexfromworld;
500 int num_pvschainlength;
501 unsigned char *data_pvschains;
503 //pvschain = model->brushq3.data_pvschains + mycluster * model->brushq3.num_pvschainlength;
504 //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
508 typedef struct model_s
510 // name and path of model, for example "progs/player.mdl"
511 char name[MAX_QPATH];
512 // model needs to be loaded if this is true
514 // set if the model is used in current map, models which are not, are purged
516 // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
517 qboolean isworldmodel;
518 // CRC of the file this model was loaded from, to reload if changed
520 // mod_brush, mod_alias, mod_sprite
522 // memory pool for allocations
524 // all models use textures...
525 rtexturepool_t *texturepool;
526 // flags from the model file
528 // engine calculated flags, ones that can not be set in the file
530 // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
532 // number of QC accessible frame(group)s in the model
534 // number of QC accessible skin(group)s in the model
536 // whether to randomize animated framegroups
538 // bounding box at angles '0 0 0'
539 vec3_t normalmins, normalmaxs;
540 // bounding box if yaw angle is not 0, but pitch and roll are
541 vec3_t yawmins, yawmaxs;
542 // bounding box if pitch or roll are used
543 vec3_t rotatedmins, rotatedmaxs;
544 // sphere radius, usable at any angles
546 // squared sphere radius for easier comparisons
548 // skin animation info
549 animscene_t *skinscenes; // [numskins]
550 // skin animation info
551 animscene_t *animscenes; // [numframes]
552 // draw the model's sky polygons (only used by brush models)
553 void(*DrawSky)(struct entity_render_s *ent);
554 // draw the model using lightmap/dlight shading
555 void(*Draw)(struct entity_render_s *ent);
556 // draw a shadow volume for the model based on light source
557 void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
558 // draw the lighting on a model (through stencil)
559 void(*DrawLight)(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz);
560 // trace a box against this model
561 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);
562 // fields belonging to each type of model
564 model_sprite_t sprite;
566 model_brushq1_t brushq1;
567 /* MSVC can't handle an empty struct, so this is commented out for now
568 model_brushq2_t brushq2;
570 model_brushq3_t brushq3;
571 // skin files can have different tags for each skin
572 overridetagnameset_t *data_overridetagnamesforskin;
573 // flags this model for offseting sounds to the model center (used by brush models)
578 //============================================================================
580 // this can be used for anything without a valid texture
581 extern rtexture_t *r_notexture;
582 #define NUM_DETAILTEXTURES 1
583 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
584 // every texture must be in a pool...
585 extern rtexturepool_t *mod_shared_texturepool;
587 extern rtexture_t *mod_shared_distorttexture[64];
590 extern model_t *loadmodel;
591 extern qbyte *mod_base;
592 // sky/water subdivision
593 //extern cvar_t gl_subdivide_size;
594 // texture fullbrights
595 extern cvar_t r_fullbrights;
597 void Mod_Init (void);
598 void Mod_CheckLoaded (model_t *mod);
599 void Mod_ClearAll (void);
600 model_t *Mod_FindName (const char *name);
601 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
602 void Mod_TouchModel (const char *name);
603 void Mod_UnloadModel (model_t *mod);
605 void Mod_ClearUsed(void);
606 void Mod_PurgeUnused(void);
607 void Mod_LoadModels(void);
609 extern model_t *loadmodel;
610 extern char loadname[32]; // for hunk tags
612 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
613 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
614 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
616 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);
617 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light, int neighbors);
618 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f);
619 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex14f);
620 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex3f, float *svector3f, float *tvector3f, float *normal3f, float *texcoord2f, int numtris, int *element3i);
621 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);
622 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, int light, int neighbors);
623 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
624 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
626 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
627 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
629 // used for talking to the QuakeC mainly
630 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
631 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
633 extern cvar_t r_mipskins;
635 typedef struct skinfileitem_s
637 struct skinfileitem_s *next;
638 char name[MAX_QPATH];
639 char replacement[MAX_QPATH];
643 typedef struct skinfile_s
645 struct skinfile_s *next;
646 skinfileitem_t *items;
650 skinfile_t *Mod_LoadSkinFiles(void);
651 void Mod_FreeSkinFiles(skinfile_t *skinfile);
652 int Mod_CountSkinFiles(skinfile_t *skinfile);
654 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);