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)
171 int num_pvsclusterbytes;
172 unsigned char *data_pvsclusters;
174 //pvschain = model->brush.data_pvsclusters + mycluster * model->brush.num_pvsclusterbytes;
175 //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
178 int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
179 int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
180 qbyte *(*GetPVS)(struct model_s *model, const vec3_t p);
181 int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength);
182 int (*BoxTouchingPVS)(struct model_s *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs);
183 void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
184 void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
185 // these are actually only found on brushq1, but NULL is handled gracefully
186 void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, qbyte *out, int outsize);
187 void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
191 typedef struct model_brushq1_s
193 int firstmodelsurface, nummodelsurfaces;
195 // lightmap format, set to r_lightmaprgba when model is loaded
203 // number of actual leafs (including 0 which is solid)
205 // visible leafs, not counting 0 (solid)
222 msurface_t *surfaces;
223 int *surfacevisframes;
224 int *surfacepvsframes;
225 msurface_t *surfacepvsnext;
226 surfmesh_t *entiremesh;
232 dclipnode_t *clipnodes;
237 hull_t hulls[MAX_MAP_HULLS];
242 int num_compressedpvs;
243 qbyte *data_compressedpvs;
252 mvertex_t *portalpoints;
257 // pvs visibility marking
258 mleaf_t *pvsviewleaf;
259 int pvsviewleafnovis;
261 mleaf_t *pvsleafchain;
263 int pvssurflistlength;
264 // these get rebuilt as the player moves around if this is the world,
265 // otherwise they are left alone (no pvs for bmodels)
266 msurface_t ***pvstexturechains;
267 msurface_t **pvstexturechainsbuffer;
268 int *pvstexturechainslength;
270 // lightmap update chains for light styles
273 int *light_stylevalue;
274 msurface_t ***light_styleupdatechains;
275 msurface_t **light_styleupdatechainsbuffer;
278 mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
279 void (*BuildPVSTextureChains)(struct model_s *model);
283 /* MSVC can't compile empty structs, so this is commented out for now
284 typedef struct model_brushq2_s
290 // surfaceflags from bsp
291 #define Q3SURFACEFLAG_NODAMAGE 1
292 #define Q3SURFACEFLAG_SLICK 2
293 #define Q3SURFACEFLAG_SKY 4
294 #define Q3SURFACEFLAG_LADDER 8
295 #define Q3SURFACEFLAG_NOIMPACT 16
296 #define Q3SURFACEFLAG_NOMARKS 32
297 #define Q3SURFACEFLAG_FLESH 64
298 #define Q3SURFACEFLAG_NODRAW 128
299 #define Q3SURFACEFLAG_HINT 256
300 #define Q3SURFACEFLAG_SKIP 512
301 #define Q3SURFACEFLAG_NOLIGHTMAP 1024
302 #define Q3SURFACEFLAG_POINTLIGHT 2048
303 #define Q3SURFACEFLAG_METALSTEPS 4096
304 #define Q3SURFACEFLAG_NOSTEPS 8192
305 #define Q3SURFACEFLAG_NONSOLID 16384
306 #define Q3SURFACEFLAG_LIGHTFILTER 32768
307 #define Q3SURFACEFLAG_ALPHASHADOW 65536
308 #define Q3SURFACEFLAG_NODLIGHT 131072
309 #define Q3SURFACEFLAG_DUST 262144
311 // surfaceparms from shaders
312 #define Q3SURFACEPARM_ALPHASHADOW 1
313 #define Q3SURFACEPARM_AREAPORTAL 2
314 #define Q3SURFACEPARM_CLUSTERPORTAL 4
315 #define Q3SURFACEPARM_DETAIL 8
316 #define Q3SURFACEPARM_DONOTENTER 16
317 #define Q3SURFACEPARM_FOG 32
318 #define Q3SURFACEPARM_LAVA 64
319 #define Q3SURFACEPARM_LIGHTFILTER 128
320 #define Q3SURFACEPARM_METALSTEPS 256
321 #define Q3SURFACEPARM_NODAMAGE 512
322 #define Q3SURFACEPARM_NODLIGHT 1024
323 #define Q3SURFACEPARM_NODRAW 2048
324 #define Q3SURFACEPARM_NODROP 4096
325 #define Q3SURFACEPARM_NOIMPACT 8192
326 #define Q3SURFACEPARM_NOLIGHTMAP 16384
327 #define Q3SURFACEPARM_NOMARKS 32768
328 #define Q3SURFACEPARM_NOMIPMAPS 65536
329 #define Q3SURFACEPARM_NONSOLID 131072
330 #define Q3SURFACEPARM_ORIGIN 262144
331 #define Q3SURFACEPARM_PLAYERCLIP 524288
332 #define Q3SURFACEPARM_SKY 1048576
333 #define Q3SURFACEPARM_SLICK 2197152
334 #define Q3SURFACEPARM_SLIME 4194304
335 #define Q3SURFACEPARM_STRUCTURAL 8388608
336 #define Q3SURFACEPARM_TRANS 16777216
337 #define Q3SURFACEPARM_WATER 33554432
339 typedef struct q3mtexture_s
341 char name[Q3PATHLENGTH];
352 typedef struct q3mnode_s
354 //this part shared between node and leaf
355 struct mplane_s *plane; // != NULL
356 struct q3mnode_s *parent;
359 // this part unique to nodes
360 struct q3mnode_s *children[2];
364 typedef struct q3mleaf_s
366 //this part shared between node and leaf
367 struct mplane_s *plane; // == NULL
368 struct q3mnode_s *parent;
371 // this part unique to leafs
375 struct q3mface_s **firstleafface;
377 struct q3mbrush_s **firstleafbrush;
381 typedef struct q3mmodel_s
386 struct q3mface_s *firstface;
388 struct q3mbrush_s *firstbrush;
392 typedef struct q3mbrush_s
394 struct colbrushf_s *colbrushf;
396 struct q3mbrushside_s *firstbrushside;
397 struct q3mtexture_s *texture;
401 typedef struct q3mbrushside_s
403 struct mplane_s *plane;
404 struct q3mtexture_s *texture;
408 typedef struct q3meffect_s
410 char shadername[Q3PATHLENGTH];
411 struct q3mbrush_s *brush;
412 int unknown; // 5 or -1
416 typedef struct q3mface_s
418 struct q3mtexture_s *texture;
419 struct q3meffect_s *effect;
420 rtexture_t *lightmaptexture;
421 int collisions; // performs per triangle collisions on this surface
422 int collisionmarkframe; // don't collide twice in one trace
427 // used for processing
429 // (world only) visframe == r_framecount means it is visible this frame
431 // bounding box for culling
437 float *data_vertex3f;
438 float *data_texcoordtexture2f;
439 float *data_texcoordlightmap2f;
440 float *data_svector3f;
441 float *data_tvector3f;
442 float *data_normal3f;
445 int *data_neighbor3i;
447 // temporary use by light processing
448 int lighttemp_castshadow;
452 typedef struct model_brushq3_s
454 // if non-zero this is a submodel
455 // (this is the number of the submodel, an index into data_models)
459 q3mtexture_t *data_textures;
462 mplane_t *data_planes;
465 q3mnode_t *data_nodes;
468 q3mleaf_t *data_leafs;
471 q3mbrush_t **data_leafbrushes;
474 q3mface_t **data_leaffaces;
477 q3mmodel_t *data_models;
478 // each submodel gets its own model struct so this is different for each.
479 q3mmodel_t *data_thismodel;
482 q3mbrush_t *data_brushes;
485 q3mbrushside_t *data_brushsides;
488 float *data_vertex3f;
489 float *data_texcoordtexture2f;
490 float *data_texcoordlightmap2f;
491 float *data_svector3f;
492 float *data_tvector3f;
493 float *data_normal3f;
498 int *data_neighbor3i;
501 q3meffect_t *data_effects;
504 q3mface_t *data_faces;
508 rtexture_t **data_lightmaps;
510 // voxel light data with directional shading
512 q3dlightgrid_t *data_lightgrid;
513 // size of each cell (may vary by map, typically 64 64 128)
514 float num_lightgrid_cellsize[3];
515 // 1.0 / num_lightgrid_cellsize
516 float num_lightgrid_scale[3];
517 // dimensions of the world model in lightgrid cells
518 int num_lightgrid_imins[3];
519 int num_lightgrid_imaxs[3];
520 int num_lightgrid_isize[3];
522 int num_lightgrid_dimensions[3];
523 // transform modelspace coordinates to lightgrid index
524 matrix4x4_t num_lightgrid_indexfromworld;
528 typedef struct model_s
530 // name and path of model, for example "progs/player.mdl"
531 char name[MAX_QPATH];
532 // model needs to be loaded if this is true
534 // set if the model is used in current map, models which are not, are purged
536 // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
537 qboolean isworldmodel;
538 // CRC of the file this model was loaded from, to reload if changed
540 // mod_brush, mod_alias, mod_sprite
542 // memory pool for allocations
544 // all models use textures...
545 rtexturepool_t *texturepool;
546 // flags from the model file
548 // engine calculated flags, ones that can not be set in the file
550 // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
552 // number of QC accessible frame(group)s in the model
554 // number of QC accessible skin(group)s in the model
556 // whether to randomize animated framegroups
558 // bounding box at angles '0 0 0'
559 vec3_t normalmins, normalmaxs;
560 // bounding box if yaw angle is not 0, but pitch and roll are
561 vec3_t yawmins, yawmaxs;
562 // bounding box if pitch or roll are used
563 vec3_t rotatedmins, rotatedmaxs;
564 // sphere radius, usable at any angles
566 // squared sphere radius for easier comparisons
568 // skin animation info
569 animscene_t *skinscenes; // [numskins]
570 // skin animation info
571 animscene_t *animscenes; // [numframes]
572 // draw the model's sky polygons (only used by brush models)
573 void(*DrawSky)(struct entity_render_s *ent);
574 // draw the model using lightmap/dlight shading
575 void(*Draw)(struct entity_render_s *ent);
576 // draw a shadow volume for the model based on light source
577 void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
578 // draw the lighting on a model (through stencil)
579 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);
580 // trace a box against this model
581 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);
582 // fields belonging to each type of model
584 model_sprite_t sprite;
586 model_brushq1_t brushq1;
587 /* MSVC can't handle an empty struct, so this is commented out for now
588 model_brushq2_t brushq2;
590 model_brushq3_t brushq3;
591 // skin files can have different tags for each skin
592 overridetagnameset_t *data_overridetagnamesforskin;
593 // flags this model for offseting sounds to the model center (used by brush models)
598 //============================================================================
600 // this can be used for anything without a valid texture
601 extern rtexture_t *r_notexture;
602 #define NUM_DETAILTEXTURES 1
603 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
604 // every texture must be in a pool...
605 extern rtexturepool_t *mod_shared_texturepool;
607 extern rtexture_t *mod_shared_distorttexture[64];
610 extern model_t *loadmodel;
611 extern qbyte *mod_base;
612 // sky/water subdivision
613 //extern cvar_t gl_subdivide_size;
614 // texture fullbrights
615 extern cvar_t r_fullbrights;
617 void Mod_Init (void);
618 void Mod_CheckLoaded (model_t *mod);
619 void Mod_ClearAll (void);
620 model_t *Mod_FindName (const char *name);
621 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
622 void Mod_TouchModel (const char *name);
623 void Mod_UnloadModel (model_t *mod);
625 void Mod_ClearUsed(void);
626 void Mod_PurgeUnused(void);
627 void Mod_LoadModels(void);
629 extern model_t *loadmodel;
630 extern char loadname[32]; // for hunk tags
632 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
633 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
634 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
636 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);
637 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light, int neighbors);
638 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f);
639 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex14f);
640 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);
641 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);
642 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, int light, int neighbors);
643 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
644 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
646 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
647 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
649 // used for talking to the QuakeC mainly
650 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
651 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
653 extern cvar_t r_mipskins;
655 typedef struct skinfileitem_s
657 struct skinfileitem_s *next;
658 char name[MAX_QPATH];
659 char replacement[MAX_QPATH];
663 typedef struct skinfile_s
665 struct skinfile_s *next;
666 skinfileitem_t *items;
670 skinfile_t *Mod_LoadSkinFiles(void);
671 void Mod_FreeSkinFiles(skinfile_t *skinfile);
672 int Mod_CountSkinFiles(skinfile_t *skinfile);
674 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);