]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.h
added DP_QC_TRACE_MOVETYPE_HITMODEL extension (and added DP_QC_TRACE_MOVETYPE_WORLDON...
[xonotic/darkplaces.git] / model_shared.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20
21 #ifndef __MODEL__
22 #define __MODEL__
23
24 #ifndef SYNCTYPE_T
25 #define SYNCTYPE_T
26 typedef enum {ST_SYNC=0, ST_RAND } synctype_t;
27 #endif
28
29 /*
30
31 d*_t structures are on-disk representations
32 m*_t structures are in-memory
33
34 */
35
36 typedef enum {mod_invalid, mod_brush, mod_sprite, mod_alias, mod_brushq2, mod_brushq3} modtype_t;
37
38 typedef struct animscene_s
39 {
40         char name[32]; // for viewthing support
41         int firstframe;
42         int framecount;
43         int loop; // true or false
44         float framerate;
45 }
46 animscene_t;
47
48 typedef struct skinframe_s
49 {
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)
59 }
60 skinframe_t;
61
62 #define MAX_SKINS 256
63
64 typedef struct overridetagname_s
65 {
66         char name[MAX_QPATH];
67 }
68 overridetagname_t;
69
70 // a replacement set of tag names, per skin
71 typedef struct overridetagnameset_s
72 {
73         int num_overridetagnames;
74         overridetagname_t *data_overridetagnames;
75 }
76 overridetagnameset_t;
77
78 #define SHADOWMESHVERTEXHASH 1024
79 typedef struct shadowmeshvertexhash_s
80 {
81         struct shadowmeshvertexhash_s *next;
82 }
83 shadowmeshvertexhash_t;
84
85 typedef struct shadowmesh_s
86 {
87         struct shadowmesh_s *next;
88         int numverts, maxverts;
89         int numtriangles, maxtriangles;
90         float *vertex3f;
91         int *element3i;
92         int *neighbor3i;
93         // these are NULL after Mod_ShadowMesh_Finish is performed, only used
94         // while building meshes
95         shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
96 }
97 shadowmesh_t;
98
99
100 #include "matrixlib.h"
101
102 #include "model_brush.h"
103 #include "model_sprite.h"
104 #include "model_alias.h"
105
106 typedef struct model_alias_s
107 {
108         // LordHavoc: Q2/ZYM model support
109         int                             aliastype;
110
111         // mdl/md2/md3 models are the same after loading
112         int                             aliasnum_meshes;
113         aliasmesh_t             *aliasdata_meshes;
114
115         int                             aliasnum_tags;
116         int                             aliasnum_tagframes;
117         aliastag_t              *aliasdata_tags;
118
119         // for Zymotic models
120         int                             zymnum_verts;
121         int                             zymnum_tris;
122         int                             zymnum_shaders;
123         int                             zymnum_bones;
124         int                             zymnum_scenes;
125         float                   *zymdata_texcoords;
126         rtexture_t              **zymdata_textures;
127         qbyte                   *zymdata_trizone;
128         zymbone_t               *zymdata_bones;
129         unsigned int    *zymdata_vertbonecounts;
130         zymvertex_t             *zymdata_verts;
131         unsigned int    *zymdata_renderlist;
132         float                   *zymdata_poses;
133 }
134 model_alias_t;
135
136 typedef struct model_sprite_s
137 {
138         int                             sprnum_type;
139         mspriteframe_t  *sprdata_frames;
140 }
141 model_sprite_t;
142
143 struct trace_s;
144
145 typedef struct model_brush_s
146 {
147         // true if this model is a HalfLife .bsp file
148         qboolean ishlbsp;
149         // string of entity definitions (.map format)
150         char *entities;
151         // number of submodels in this map (just used by server to know how many
152         // submodels to load)
153         int numsubmodels;
154         // common functions
155         int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
156         int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
157         qbyte *(*GetPVS)(struct model_s *model, const vec3_t p);
158         int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength);
159         int (*BoxTouchingPVS)(struct model_s *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs);
160         void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
161         void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
162         // these are actually only found on brushq1, but NULL is handled gracefully
163         void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, qbyte *out, int outsize);
164         void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
165 }
166 model_brush_t;
167
168 typedef struct model_brushq1_s
169 {
170         int                             firstmodelsurface, nummodelsurfaces;
171
172         // lightmap format, set to r_lightmaprgba when model is loaded
173         int                             lightmaprgba;
174
175         dmodel_t                *submodels;
176
177         int                             numplanes;
178         mplane_t                *planes;
179
180         // number of actual leafs (including 0 which is solid)
181         int                             numleafs;
182         // visible leafs, not counting 0 (solid)
183         int                             visleafs;
184         mleaf_t                 *leafs;
185
186         int                             numvertexes;
187         mvertex_t               *vertexes;
188
189         int                             numedges;
190         medge_t                 *edges;
191
192         int                             numnodes;
193         mnode_t                 *nodes;
194
195         int                             numtexinfo;
196         mtexinfo_t              *texinfo;
197
198         int                             numsurfaces;
199         msurface_t              *surfaces;
200         int                             *surfacevisframes;
201         int                             *surfacepvsframes;
202         msurface_t              *surfacepvsnext;
203         surfmesh_t              *entiremesh;
204
205         int                             numsurfedges;
206         int                             *surfedges;
207
208         int                             numclipnodes;
209         dclipnode_t             *clipnodes;
210
211         int                             nummarksurfaces;
212         int                             *marksurfaces;
213
214         hull_t                  hulls[MAX_MAP_HULLS];
215
216         int                             numtextures;
217         texture_t               *textures;
218
219         int                             num_compressedpvs;
220         qbyte                   *data_compressedpvs;
221         qbyte                   *data_decompressedpvs;
222
223         int                             num_lightdata;
224         qbyte                   *lightdata;
225
226         int                             numportals;
227         mportal_t               *portals;
228
229         int                             numportalpoints;
230         mvertex_t               *portalpoints;
231
232         int                             numlights;
233         mlight_t                *lights;
234
235         // pvs visibility marking
236         mleaf_t                 *pvsviewleaf;
237         int                             pvsviewleafnovis;
238         int                             pvsframecount;
239         mleaf_t                 *pvsleafchain;
240         int                             *pvssurflist;
241         int                             pvssurflistlength;
242         // these get rebuilt as the player moves around if this is the world,
243         // otherwise they are left alone (no pvs for bmodels)
244         msurface_t              ***pvstexturechains;
245         msurface_t              **pvstexturechainsbuffer;
246         int                             *pvstexturechainslength;
247
248         // lightmap update chains for light styles
249         int                             light_styles;
250         qbyte                   *light_style;
251         int                             *light_stylevalue;
252         msurface_t              ***light_styleupdatechains;
253         msurface_t              **light_styleupdatechainsbuffer;
254         int                             light_scalebit;
255         float                   light_ambient;
256
257         mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
258         void (*BuildPVSTextureChains)(struct model_s *model);
259 }
260 model_brushq1_t;
261
262 /* MSVC can't compile empty structs, so this is commented out for now
263 typedef struct model_brushq2_s
264 {
265 }
266 model_brushq2_t;
267 */
268
269 #define Q3MTEXTURERENDERFLAGS_NODRAW 1
270 #define Q3MTEXTURERENDERFLAGS_SKY 2
271
272 typedef struct q3mtexture_s
273 {
274         char name[Q3PATHLENGTH];
275         int surfaceflags;
276         int nativecontents;
277         int supercontents;
278         int renderflags;
279
280         int number;
281         skinframe_t skin;
282 }
283 q3mtexture_t;
284
285 typedef struct q3mnode_s
286 {
287         //this part shared between node and leaf
288         int isnode; // true
289         struct q3mnode_s *parent;
290         vec3_t mins;
291         vec3_t maxs;
292         // this part unique to nodes
293         struct mplane_s *plane;
294         struct q3mnode_s *children[2];
295 }
296 q3mnode_t;
297
298 typedef struct q3mleaf_s
299 {
300         //this part shared between node and leaf
301         int isnode; // false
302         struct q3mnode_s *parent;
303         vec3_t mins;
304         vec3_t maxs;
305         // this part unique to leafs
306         int clusterindex;
307         int areaindex;
308         int numleaffaces;
309         struct q3mface_s **firstleafface;
310         int numleafbrushes;
311         struct q3mbrush_s **firstleafbrush;
312 }
313 q3mleaf_t;
314
315 typedef struct q3mmodel_s
316 {
317         vec3_t mins;
318         vec3_t maxs;
319         int numfaces;
320         struct q3mface_s *firstface;
321         int numbrushes;
322         struct q3mbrush_s *firstbrush;
323 }
324 q3mmodel_t;
325
326 typedef struct q3mbrush_s
327 {
328         struct colbrushf_s *colbrushf;
329         int numbrushsides;
330         struct q3mbrushside_s *firstbrushside;
331         struct q3mtexture_s *texture;
332 }
333 q3mbrush_t;
334
335 typedef struct q3mbrushside_s
336 {
337         struct mplane_s *plane;
338         struct q3mtexture_s *texture;
339 }
340 q3mbrushside_t;
341
342 typedef struct q3meffect_s
343 {
344         char shadername[Q3PATHLENGTH];
345         struct q3mbrush_s *brush;
346         int unknown; // 5 or -1
347 }
348 q3meffect_t;
349
350 typedef struct q3mface_s
351 {
352         struct q3mtexture_s *texture;
353         struct q3meffect_s *effect;
354         rtexture_t *lightmaptexture;
355         int collisions; // performs per triangle collisions on this surface
356         int collisionmarkframe; // don't collide twice in one trace
357         int type;
358         int firstvertex;
359         int firstelement;
360         int patchsize[2];
361         // used for processing
362         int markframe;
363         // (world only) visframe == r_framecount means it is visible this frame
364         int visframe;
365         // bounding box for culling
366         float mins[3];
367         float maxs[3];
368
369         int num_vertices;
370         int num_triangles;
371         float *data_vertex3f;
372         float *data_texcoordtexture2f;
373         float *data_texcoordlightmap2f;
374         float *data_svector3f;
375         float *data_tvector3f;
376         float *data_normal3f;
377         float *data_color4f;
378         int *data_element3i;
379         int *data_neighbor3i;
380 }
381 q3mface_t;
382
383 typedef struct model_brushq3_s
384 {
385         // if non-zero this is a submodel
386         // (this is the number of the submodel, an index into data_models)
387         int submodel;
388
389         int num_textures;
390         q3mtexture_t *data_textures;
391
392         int num_planes;
393         mplane_t *data_planes;
394
395         int num_nodes;
396         q3mnode_t *data_nodes;
397
398         int num_leafs;
399         q3mleaf_t *data_leafs;
400
401         int num_leafbrushes;
402         q3mbrush_t **data_leafbrushes;
403
404         int num_leaffaces;
405         q3mface_t **data_leaffaces;
406
407         int num_models;
408         q3mmodel_t *data_models;
409         // each submodel gets its own model struct so this is different for each.
410         q3mmodel_t *data_thismodel;
411
412         int num_brushes;
413         q3mbrush_t *data_brushes;
414
415         int num_brushsides;
416         q3mbrushside_t *data_brushsides;
417
418         int num_vertices;
419         float *data_vertex3f;
420         float *data_texcoordtexture2f;
421         float *data_texcoordlightmap2f;
422         float *data_svector3f;
423         float *data_tvector3f;
424         float *data_normal3f;
425         float *data_color4f;
426
427         int num_triangles;
428         int *data_element3i;
429         int *data_neighbor3i;
430
431         int num_effects;
432         q3meffect_t *data_effects;
433
434         int num_faces;
435         q3mface_t *data_faces;
436
437         // lightmap textures
438         int num_lightmaps;
439         rtexture_t **data_lightmaps;
440
441         // voxel light data with directional shading
442         int num_lightgrid;
443         q3dlightgrid_t *data_lightgrid;
444         // size of each cell (may vary by map, typically 64 64 128)
445         float num_lightgrid_cellsize[3];
446         // 1.0 / num_lightgrid_cellsize
447         float num_lightgrid_scale[3];
448         // dimensions of the world model in lightgrid cells
449         int num_lightgrid_imins[3];
450         int num_lightgrid_imaxs[3];
451         int num_lightgrid_isize[3];
452         // indexing/clamping
453         int num_lightgrid_dimensions[3];
454         // transform modelspace coordinates to lightgrid index
455         matrix4x4_t num_lightgrid_indexfromworld;
456
457         // pvs
458         int num_pvsclusters;
459         int num_pvschainlength;
460         unsigned char *data_pvschains;
461         // example
462         //pvschain = model->brushq3.data_pvschains + mycluster * model->brushq3.num_pvschainlength;
463         //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
464 }
465 model_brushq3_t;
466
467 typedef struct model_s
468 {
469         // name and path of model, for example "progs/player.mdl"
470         char                    name[MAX_QPATH];
471         // model needs to be loaded if this is true
472         qboolean                needload;
473         // set if the model is used in current map, models which are not, are purged
474         qboolean                used;
475         // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
476         qboolean                isworldmodel;
477         // CRC of the file this model was loaded from, to reload if changed
478         unsigned int    crc;
479         // mod_brush, mod_alias, mod_sprite
480         modtype_t               type;
481         // memory pool for allocations
482         mempool_t               *mempool;
483         // all models use textures...
484         rtexturepool_t  *texturepool;
485         // flags from the model file
486         int                             flags;
487         // engine calculated flags, ones that can not be set in the file
488         int                             flags2;
489         // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
490         int                             fullbright;
491         // number of QC accessible frame(group)s in the model
492         int                             numframes;
493         // number of QC accessible skin(group)s in the model
494         int                             numskins;
495         // whether to randomize animated framegroups
496         synctype_t              synctype;
497         // bounding box at angles '0 0 0'
498         vec3_t                  normalmins, normalmaxs;
499         // bounding box if yaw angle is not 0, but pitch and roll are
500         vec3_t                  yawmins, yawmaxs;
501         // bounding box if pitch or roll are used
502         vec3_t                  rotatedmins, rotatedmaxs;
503         // sphere radius, usable at any angles
504         float                   radius;
505         // squared sphere radius for easier comparisons
506         float                   radius2;
507         // skin animation info
508         animscene_t             *skinscenes; // [numskins]
509         // skin animation info
510         animscene_t             *animscenes; // [numframes]
511         // draw the model's sky polygons (only used by brush models)
512         void(*DrawSky)(struct entity_render_s *ent);
513         // draw the model using lightmap/dlight shading
514         void(*Draw)(struct entity_render_s *ent);
515         // draw a shadow volume for the model based on light source
516         void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
517         // draw the lighting on a model (through stencil)
518         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);
519         // trace a box against this model
520         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);
521         // fields belonging to each type of model
522         model_alias_t   alias;
523         model_sprite_t  sprite;
524         model_brush_t   brush;
525         model_brushq1_t brushq1;
526         /* MSVC can't handle an empty struct, so this is commented out for now
527         model_brushq2_t brushq2;
528         */
529         model_brushq3_t brushq3;
530         // skin files can have different tags for each skin
531         overridetagnameset_t    *data_overridetagnamesforskin;
532         // flags this model for offseting sounds to the model center (used by brush models)
533         int soundfromcenter;
534 }
535 model_t;
536
537 //============================================================================
538
539 // this can be used for anything without a valid texture
540 extern rtexture_t *r_notexture;
541 #define NUM_DETAILTEXTURES 1
542 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
543 // every texture must be in a pool...
544 extern rtexturepool_t *mod_shared_texturepool;
545
546 extern rtexture_t *mod_shared_distorttexture[64];
547
548 // model loading
549 extern model_t *loadmodel;
550 extern qbyte *mod_base;
551 // sky/water subdivision
552 //extern cvar_t gl_subdivide_size;
553 // texture fullbrights
554 extern cvar_t r_fullbrights;
555
556 void Mod_Init (void);
557 void Mod_CheckLoaded (model_t *mod);
558 void Mod_ClearAll (void);
559 model_t *Mod_FindName (const char *name);
560 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
561 void Mod_TouchModel (const char *name);
562 void Mod_UnloadModel (model_t *mod);
563
564 void Mod_ClearUsed(void);
565 void Mod_PurgeUnused(void);
566 void Mod_LoadModels(void);
567
568 extern model_t *loadmodel;
569 extern char loadname[32];       // for hunk tags
570
571 int Mod_FindTriangleWithEdge(const int *elements, int numtriangles, int start, int end, int ignore);
572 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
573 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
574 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
575
576 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts);
577 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh);
578 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *v);
579 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, float *vert0, float *vert1, float *vert2);
580 void Mod_ShadowMesh_AddPolygon(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts);
581 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, float *verts, int numtris, int *elements);
582 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int initialnumtriangles);
583 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh);
584 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
585 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
586
587 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
588 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
589
590 // used for talking to the QuakeC mainly
591 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
592 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
593
594 extern cvar_t r_mipskins;
595
596 typedef struct skinfileitem_s
597 {
598         struct skinfileitem_s *next;
599         char name[MAX_QPATH];
600         char replacement[MAX_QPATH];
601 }
602 skinfileitem_t;
603
604 typedef struct skinfile_s
605 {
606         struct skinfile_s *next;
607         skinfileitem_t *items;
608 }
609 skinfile_t;
610
611 skinfile_t *Mod_LoadSkinFiles(void);
612 void Mod_FreeSkinFiles(skinfile_t *skinfile);
613 int Mod_CountSkinFiles(skinfile_t *skinfile);
614
615 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);
616
617 #endif  // __MODEL__
618