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_brushq1, 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 // LordHavoc: replaces glpoly, triangle mesh
77 typedef struct surfmesh_s
79 int num_vertices; // number of vertices in the mesh
80 int num_triangles; // number of triangles in the mesh
81 float *data_vertex3f; // float[verts*3] vertex locations
82 float *data_svector3f; // float[verts*3] direction of 'S' (right) texture axis for each vertex
83 float *data_tvector3f; // float[verts*3] direction of 'T' (down) texture axis for each vertex
84 float *data_normal3f; // float[verts*3] direction of 'R' (out) texture axis for each vertex
85 int *data_lightmapoffsets; // index into surface's lightmap samples for vertex lighting
86 float *data_texcoordtexture2f; // float[verts*2] texcoords for surface texture
87 float *data_texcoordlightmap2f; // float[verts*2] texcoords for lightmap texture
88 float *data_texcoorddetail2f; // float[verts*2] texcoords for detail texture
89 float *data_lightmapcolor4f;
90 int *data_element3i; // int[tris*3] triangles of the mesh, 3 indices into vertex arrays for each
91 int *data_neighbor3i; // int[tris*3] neighboring triangle on each edge (-1 if none)
93 int num_collisionvertices;
94 int num_collisiontriangles;
95 float *data_collisionvertex3f;
96 int *data_collisionelement3i;
100 #define SHADOWMESHVERTEXHASH 1024
101 typedef struct shadowmeshvertexhash_s
103 struct shadowmeshvertexhash_s *next;
105 shadowmeshvertexhash_t;
107 typedef struct shadowmesh_s
109 // next mesh in chain
110 struct shadowmesh_s *next;
111 // used for light mesh (NULL on shadow mesh)
112 rtexture_t *map_diffuse;
113 rtexture_t *map_specular;
114 rtexture_t *map_normal;
116 int numverts, maxverts;
117 int numtriangles, maxtriangles;
120 // used for light mesh (NULL on shadow mesh)
127 // used for shadow mesh (NULL on light mesh)
129 // these are NULL after Mod_ShadowMesh_Finish is performed, only used
130 // while building meshes
131 shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
136 #include "matrixlib.h"
138 #include "model_brush.h"
139 #include "model_sprite.h"
140 #include "model_alias.h"
142 typedef struct model_alias_s
144 // mdl/md2/md3/zym model formats are treated the same after loading
146 // the shader meshes comprising this model
148 aliasmesh_t *aliasdata_meshes;
152 int aliasnum_tagframes;
153 aliastag_t *aliasdata_tags;
155 // for skeletal models
157 aliasbone_t *aliasdata_bones;
159 float *aliasdata_poses;
163 typedef struct model_sprite_s
166 mspriteframe_t *sprdata_frames;
172 typedef struct model_brush_s
174 // true if this model is a HalfLife .bsp file
176 // string of entity definitions (.map format)
178 // number of submodels in this map (just used by server to know how many
179 // submodels to load)
181 // pointers to each of the submodels if .isworldmodel is true
182 struct model_s **submodels;
186 int num_pvsclusterbytes;
187 unsigned char *data_pvsclusters;
189 //pvschain = model->brush.data_pvsclusters + mycluster * model->brush.num_pvsclusterbytes;
190 //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
192 // a mesh containing all shadow casting geometry for the whole model (including submodels), portions of this are referenced by each surface's num_firstshadowmeshtriangle
193 shadowmesh_t *shadowmesh;
196 int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
197 int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
198 qbyte *(*GetPVS)(struct model_s *model, const vec3_t p);
199 int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength);
200 int (*BoxTouchingPVS)(struct model_s *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs);
201 void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
202 void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
203 // these are actually only found on brushq1, but NULL is handled gracefully
204 void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, qbyte *out, int outsize);
205 void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
209 rtexture_t *solidskytexture;
210 rtexture_t *alphaskytexture;
214 typedef struct model_brushq1_s
216 // lightmap format, set to r_lightmaprgba when model is loaded
224 // number of actual leafs (including 0 which is solid)
226 // visible leafs, not counting 0 (solid)
243 msurface_t *surfaces;
244 int *surfacevisframes;
245 msurface_t *surfacepvsnext;
251 dclipnode_t *clipnodes;
256 hull_t hulls[MAX_MAP_HULLS];
261 int num_compressedpvs;
262 qbyte *data_compressedpvs;
271 mvertex_t *portalpoints;
276 // lightmap update chains for light styles
279 int *light_stylevalue;
280 msurface_t ***light_styleupdatechains;
281 msurface_t **light_styleupdatechainsbuffer;
283 mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
287 /* MSVC can't compile empty structs, so this is commented out for now
288 typedef struct model_brushq2_s
294 // surfaceflags from bsp
295 #define Q3SURFACEFLAG_NODAMAGE 1
296 #define Q3SURFACEFLAG_SLICK 2
297 #define Q3SURFACEFLAG_SKY 4
298 #define Q3SURFACEFLAG_LADDER 8
299 #define Q3SURFACEFLAG_NOIMPACT 16
300 #define Q3SURFACEFLAG_NOMARKS 32
301 #define Q3SURFACEFLAG_FLESH 64
302 #define Q3SURFACEFLAG_NODRAW 128
303 #define Q3SURFACEFLAG_HINT 256
304 #define Q3SURFACEFLAG_SKIP 512
305 #define Q3SURFACEFLAG_NOLIGHTMAP 1024
306 #define Q3SURFACEFLAG_POINTLIGHT 2048
307 #define Q3SURFACEFLAG_METALSTEPS 4096
308 #define Q3SURFACEFLAG_NOSTEPS 8192
309 #define Q3SURFACEFLAG_NONSOLID 16384
310 #define Q3SURFACEFLAG_LIGHTFILTER 32768
311 #define Q3SURFACEFLAG_ALPHASHADOW 65536
312 #define Q3SURFACEFLAG_NODLIGHT 131072
313 #define Q3SURFACEFLAG_DUST 262144
315 // surfaceparms from shaders
316 #define Q3SURFACEPARM_ALPHASHADOW 1
317 #define Q3SURFACEPARM_AREAPORTAL 2
318 #define Q3SURFACEPARM_CLUSTERPORTAL 4
319 #define Q3SURFACEPARM_DETAIL 8
320 #define Q3SURFACEPARM_DONOTENTER 16
321 #define Q3SURFACEPARM_FOG 32
322 #define Q3SURFACEPARM_LAVA 64
323 #define Q3SURFACEPARM_LIGHTFILTER 128
324 #define Q3SURFACEPARM_METALSTEPS 256
325 #define Q3SURFACEPARM_NODAMAGE 512
326 #define Q3SURFACEPARM_NODLIGHT 1024
327 #define Q3SURFACEPARM_NODRAW 2048
328 #define Q3SURFACEPARM_NODROP 4096
329 #define Q3SURFACEPARM_NOIMPACT 8192
330 #define Q3SURFACEPARM_NOLIGHTMAP 16384
331 #define Q3SURFACEPARM_NOMARKS 32768
332 #define Q3SURFACEPARM_NOMIPMAPS 65536
333 #define Q3SURFACEPARM_NONSOLID 131072
334 #define Q3SURFACEPARM_ORIGIN 262144
335 #define Q3SURFACEPARM_PLAYERCLIP 524288
336 #define Q3SURFACEPARM_SKY 1048576
337 #define Q3SURFACEPARM_SLICK 2197152
338 #define Q3SURFACEPARM_SLIME 4194304
339 #define Q3SURFACEPARM_STRUCTURAL 8388608
340 #define Q3SURFACEPARM_TRANS 16777216
341 #define Q3SURFACEPARM_WATER 33554432
342 #define Q3SURFACEPARM_POINTLIGHT 67108864
344 // various flags from shaders
345 #define Q3TEXTUREFLAG_TWOSIDED 1
346 #define Q3TEXTUREFLAG_ADDITIVE 2
347 #define Q3TEXTUREFLAG_NOMIPMAPS 4
348 #define Q3TEXTUREFLAG_NOPICMIP 8
349 #define Q3TEXTUREFLAG_AUTOSPRITE 16
350 #define Q3TEXTUREFLAG_AUTOSPRITE2 32
351 #define Q3TEXTUREFLAG_ALPHATEST 64
354 typedef struct q3mtexture_s
356 char name[Q3PATHLENGTH];
357 char firstpasstexturename[Q3PATHLENGTH];
368 struct q3msurface_s **facelist;
373 typedef struct q3mnode_s
375 //this part shared between node and leaf
376 struct mplane_s *plane; // != NULL
377 struct q3mnode_s *parent;
380 // this part unique to nodes
381 struct q3mnode_s *children[2];
385 typedef struct q3mleaf_s
387 //this part shared between node and leaf
388 struct mplane_s *plane; // == NULL
389 struct q3mnode_s *parent;
392 // this part unique to leafs
396 struct q3msurface_s **firstleafface;
397 int *firstleaffacenum;
399 struct q3mbrush_s **firstleafbrush;
403 typedef struct q3mmodel_s
408 struct q3msurface_s *firstface;
410 struct q3mbrush_s *firstbrush;
414 typedef struct q3mbrush_s
416 struct colbrushf_s *colbrushf;
418 struct q3mbrushside_s *firstbrushside;
419 struct q3mtexture_s *texture;
423 typedef struct q3mbrushside_s
425 struct mplane_s *plane;
426 struct q3mtexture_s *texture;
430 typedef struct q3meffect_s
432 char shadername[Q3PATHLENGTH];
433 struct q3mbrush_s *brush;
434 int unknown; // 5 or -1
438 typedef struct q3msurface_s
440 // FIXME: collisionmarkframe should be kept in a separate array
441 // FIXME: visframe should be kept in a separate array
442 // FIXME: shadowmark should be kept in a separate array
444 struct q3mtexture_s *texture;
445 struct q3meffect_s *effect;
446 rtexture_t *lightmaptexture;
447 int collisionmarkframe; // don't collide twice in one trace
448 int visframe; // visframe == r_framecount means it is visible this frame
449 // bounding box for culling
455 // index into model->brush.shadowmesh
456 int num_firstshadowmeshtriangle;
458 // used for shadow volume generation
463 typedef struct model_brushq3_s
465 // if non-zero this is a submodel
466 // (this is the number of the submodel, an index into data_models)
470 q3mtexture_t *data_textures;
471 q3msurface_t **data_texturefaces;
472 int *data_texturefacenums;
475 mplane_t *data_planes;
478 q3mnode_t *data_nodes;
481 q3mleaf_t *data_leafs;
484 q3mbrush_t **data_leafbrushes;
487 q3msurface_t **data_leaffaces;
488 int *data_leaffacenums;
491 q3mmodel_t *data_models;
492 // each submodel gets its own model struct so this is different for each.
493 q3mmodel_t *data_thismodel;
496 q3mbrush_t *data_brushes;
499 q3mbrushside_t *data_brushsides;
502 float *data_vertex3f;
503 float *data_texcoordtexture2f;
504 float *data_texcoordlightmap2f;
505 float *data_svector3f;
506 float *data_tvector3f;
507 float *data_normal3f;
512 int *data_neighbor3i;
515 q3meffect_t *data_effects;
518 q3msurface_t *data_faces;
522 rtexture_t **data_lightmaps;
524 // voxel light data with directional shading
526 q3dlightgrid_t *data_lightgrid;
527 // size of each cell (may vary by map, typically 64 64 128)
528 float num_lightgrid_cellsize[3];
529 // 1.0 / num_lightgrid_cellsize
530 float num_lightgrid_scale[3];
531 // dimensions of the world model in lightgrid cells
532 int num_lightgrid_imins[3];
533 int num_lightgrid_imaxs[3];
534 int num_lightgrid_isize[3];
536 int num_lightgrid_dimensions[3];
537 // transform modelspace coordinates to lightgrid index
538 matrix4x4_t num_lightgrid_indexfromworld;
542 typedef struct model_s
544 // name and path of model, for example "progs/player.mdl"
545 char name[MAX_QPATH];
546 // model needs to be loaded if this is false
548 // set if the model is used in current map, models which are not, are purged
550 // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
551 qboolean isworldmodel;
552 // CRC of the file this model was loaded from, to reload if changed
554 // mod_brush, mod_alias, mod_sprite
556 // memory pool for allocations
558 // all models use textures...
559 rtexturepool_t *texturepool;
560 // flags from the model file
562 // engine calculated flags, ones that can not be set in the file
564 // number of QC accessible frame(group)s in the model
566 // number of QC accessible skin(group)s in the model
568 // whether to randomize animated framegroups
570 // bounding box at angles '0 0 0'
571 vec3_t normalmins, normalmaxs;
572 // bounding box if yaw angle is not 0, but pitch and roll are
573 vec3_t yawmins, yawmaxs;
574 // bounding box if pitch or roll are used
575 vec3_t rotatedmins, rotatedmaxs;
576 // sphere radius, usable at any angles
578 // squared sphere radius for easier comparisons
580 // skin animation info
581 animscene_t *skinscenes; // [numskins]
582 // skin animation info
583 animscene_t *animscenes; // [numframes]
584 // range of surface numbers in this (sub)model
585 int firstmodelsurface;
586 int nummodelsurfaces;
587 // list of surface numbers in this (sub)model
589 // surface meshes are merged to a smaller set of meshes to allow reduced
590 // vertex array switching, the meshes are limited to 65536 vertices each
591 // to play nice with Geforce1 hardware
593 surfmesh_t **meshlist;
594 // draw the model's sky polygons (only used by brush models)
595 void(*DrawSky)(struct entity_render_s *ent);
596 // draw the model using lightmap/dlight shading
597 void(*Draw)(struct entity_render_s *ent);
598 // gathers info on which clusters and surfaces are lit by light, as well as calculating a bounding box
599 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);
600 // draw a shadow volume for the model based on light source
601 void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist);
602 // draw the lighting on a model (through stencil)
603 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, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist);
604 // trace a box against this model
605 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);
606 // fields belonging to each type of model
608 model_sprite_t sprite;
610 model_brushq1_t brushq1;
611 /* MSVC can't handle an empty struct, so this is commented out for now
612 model_brushq2_t brushq2;
614 model_brushq3_t brushq3;
615 // skin files can have different tags for each skin
616 overridetagnameset_t *data_overridetagnamesforskin;
617 // flags this model for offseting sounds to the model center (used by brush models)
622 //============================================================================
624 // this can be used for anything without a valid texture
625 extern rtexture_t *r_notexture;
626 #define NUM_DETAILTEXTURES 1
627 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
628 // every texture must be in a pool...
629 extern rtexturepool_t *mod_shared_texturepool;
631 extern rtexture_t *mod_shared_distorttexture[64];
634 extern model_t *loadmodel;
635 extern qbyte *mod_base;
636 // sky/water subdivision
637 //extern cvar_t gl_subdivide_size;
638 // texture fullbrights
639 extern cvar_t r_fullbrights;
641 void Mod_Init (void);
642 void Mod_CheckLoaded (model_t *mod);
643 void Mod_ClearAll (void);
644 model_t *Mod_FindName (const char *name);
645 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
646 void Mod_UnloadModel (model_t *mod);
648 void Mod_ClearUsed(void);
649 void Mod_PurgeUnused(void);
650 void Mod_LoadModels(void);
652 extern model_t *loadmodel;
653 extern char loadname[32]; // for hunk tags
655 int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements, int numvertices, int *remapvertices);
656 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
657 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
658 void Mod_BuildNormals(int numverts, int numtriangles, const float *vertex3f, const int *elements, float *normal3f);
659 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
661 surfmesh_t *Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, int numcollisionvertices, int numcollisiontriangles, qboolean detailtexcoords, qboolean lightmapoffsets, qboolean vertexcolors);
663 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);
664 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light, int neighbors);
665 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f);
666 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex14f);
667 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);
668 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);
669 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, int light, int neighbors);
670 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
671 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
673 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
674 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
676 // used for talking to the QuakeC mainly
677 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
678 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
680 extern cvar_t r_mipskins;
682 typedef struct skinfileitem_s
684 struct skinfileitem_s *next;
685 char name[MAX_QPATH];
686 char replacement[MAX_QPATH];
690 typedef struct skinfile_s
692 struct skinfile_s *next;
693 skinfileitem_t *items;
697 skinfile_t *Mod_LoadSkinFiles(void);
698 void Mod_FreeSkinFiles(skinfile_t *skinfile);
699 int Mod_CountSkinFiles(skinfile_t *skinfile);
701 void Mod_SnapVertices(int numcomponents, int numvertices, float *vertices, float snap);
702 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);
704 #endif // MODEL_SHARED_H