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