]> icculus.org git repositories - divverent/darkplaces.git/blob - model_shared.h
Q3BSP collisions implemented (although not for patches, which aren't supported anyway...
[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         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         char *entities;
148         void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
149         void (*TraceBox)(struct model_s *model, struct trace_s *trace, const vec3_t boxstartmins, const vec3_t boxstartmaxs, const vec3_t boxendmins, const vec3_t boxendmaxs);
150 }
151 model_brush_t;
152
153 typedef struct model_brushq1_s
154 {
155         // true if this model is a HalfLife .bsp file
156         qboolean                ishlbsp;
157
158         int                             firstmodelsurface, nummodelsurfaces;
159
160         // lightmap format, set to r_lightmaprgba when model is loaded
161         int                             lightmaprgba;
162
163         int                             numsubmodels;
164         dmodel_t                *submodels;
165
166         int                             numplanes;
167         mplane_t                *planes;
168
169         // number of visible leafs, not counting 0 (solid)
170         int                             numleafs;
171         mleaf_t                 *leafs;
172
173         int                             numvertexes;
174         mvertex_t               *vertexes;
175
176         int                             numedges;
177         medge_t                 *edges;
178
179         int                             numnodes;
180         mnode_t                 *nodes;
181
182         int                             numtexinfo;
183         mtexinfo_t              *texinfo;
184
185         int                             numsurfaces;
186         msurface_t              *surfaces;
187         int                             *surfacevisframes;
188         int                             *surfacepvsframes;
189         msurface_t              *surfacepvsnext;
190         surfmesh_t              *entiremesh;
191         surfmesh_t              *surfmeshes;
192
193         int                             numsurfedges;
194         int                             *surfedges;
195
196         int                             numclipnodes;
197         dclipnode_t             *clipnodes;
198
199         int                             nummarksurfaces;
200         int                             *marksurfaces;
201
202         hull_t                  hulls[MAX_MAP_HULLS];
203
204         int                             numtextures;
205         texture_t               *textures;
206
207         qbyte                   *visdata;
208         qbyte                   *lightdata;
209
210         int                             numportals;
211         mportal_t               *portals;
212
213         int                             numportalpoints;
214         mvertex_t               *portalpoints;
215
216         int                             numlights;
217         mlight_t                *lights;
218
219         // pvs visibility marking
220         mleaf_t                 *pvsviewleaf;
221         int                             pvsviewleafnovis;
222         int                             pvsframecount;
223         mleaf_t                 *pvsleafchain;
224         int                             *pvssurflist;
225         int                             pvssurflistlength;
226         // these get rebuilt as the player moves around if this is the world,
227         // otherwise they are left alone (no pvs for bmodels)
228         msurface_t              ***pvstexturechains;
229         msurface_t              **pvstexturechainsbuffer;
230         int                             *pvstexturechainslength;
231
232         // lightmap update chains for light styles
233         int                             light_styles;
234         qbyte                   *light_style;
235         int                             *light_stylevalue;
236         msurface_t              ***light_styleupdatechains;
237         msurface_t              **light_styleupdatechainsbuffer;
238         int                             light_scalebit;
239         float                   light_ambient;
240
241         mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
242         qbyte *(*LeafPVS)(struct model_s *model, mleaf_t *leaf);
243         void (*BuildPVSTextureChains)(struct model_s *model);
244 }
245 model_brushq1_t;
246
247 /* MSVC can't compile empty structs, so this is commented out for now
248 typedef struct model_brushq2_s
249 {
250 }
251 model_brushq2_t;
252 */
253
254 typedef struct q3mtexture_s
255 {
256         char name[Q3PATHLENGTH];
257         int surfaceflags;
258         int contents;
259
260         int number;
261         skinframe_t skin;
262 }
263 q3mtexture_t;
264
265 typedef struct q3mnode_s
266 {
267         int isnode; // true
268         struct q3mnode_s *parent;
269         struct mplane_s *plane;
270         struct q3mnode_s *children[2];
271 }
272 q3mnode_t;
273
274 typedef struct q3mleaf_s
275 {
276         int isnode; // false
277         struct q3mnode_s *parent;
278         int clusterindex;
279         int areaindex;
280         int numleaffaces;
281         struct q3mface_s **firstleafface;
282         int numleafbrushes;
283         struct q3mbrush_s **firstleafbrush;
284         vec3_t mins;
285         vec3_t maxs;
286 }
287 q3mleaf_t;
288
289 typedef struct q3mmodel_s
290 {
291         vec3_t mins;
292         vec3_t maxs;
293         int numfaces;
294         struct q3mface_s *firstface;
295         int numbrushes;
296         struct q3mbrush_s *firstbrush;
297 }
298 q3mmodel_t;
299
300 typedef struct q3mbrush_s
301 {
302         struct colbrushf_s *colbrushf;
303         int numbrushsides;
304         struct q3mbrushside_s *firstbrushside;
305         struct q3mtexture_s *texture;
306 }
307 q3mbrush_t;
308
309 typedef struct q3mbrushside_s
310 {
311         struct mplane_s *plane;
312         struct q3mtexture_s *texture;
313 }
314 q3mbrushside_t;
315
316 typedef struct q3meffect_s
317 {
318         char shadername[Q3PATHLENGTH];
319         struct q3mbrush_s *brush;
320         int unknown; // 5 or -1
321 }
322 q3meffect_t;
323
324 typedef struct q3mface_s
325 {
326         struct q3mtexture_s *texture;
327         struct q3meffect_s *effect;
328         rtexture_t *lightmaptexture;
329         int type;
330         int firstvertex;
331         int numvertices;
332         int firstelement;
333         int numelements;
334         int patchsize[2];
335
336         float *data_vertex3f;
337         float *data_texturetexcoord2f;
338         float *data_lightmaptexcoord2f;
339         float *data_svector3f;
340         float *data_tvector3f;
341         float *data_normal3f;
342         float *data_color4f;
343         int numtriangles;
344         int *data_element3i;
345         int *data_neighbor3i;
346 }
347 q3mface_t;
348
349 typedef struct model_brushq3_s
350 {
351         int num_textures;
352         q3mtexture_t *data_textures;
353
354         int num_planes;
355         mplane_t *data_planes;
356
357         int num_nodes;
358         q3mnode_t *data_nodes;
359
360         int num_leafs;
361         q3mleaf_t *data_leafs;
362
363         int num_leafbrushes;
364         q3mbrush_t **data_leafbrushes;
365
366         int num_leaffaces;
367         q3mface_t **data_leaffaces;
368
369         int num_models;
370         q3mmodel_t *data_models;
371         // each submodel gets its own model struct so this is different for each.
372         q3mmodel_t data_thismodel;
373
374         int num_brushes;
375         q3mbrush_t *data_brushes;
376
377         int num_brushsides;
378         q3mbrushside_t *data_brushsides;
379
380         int num_vertices;
381         float *data_vertex3f;
382         float *data_texturetexcoord2f;
383         float *data_lightmaptexcoord2f;
384         float *data_svector3f;
385         float *data_tvector3f;
386         float *data_normal3f;
387         float *data_color4f;
388
389         int num_triangles;
390         int *data_element3i;
391         int *data_neighbor3i;
392
393         int num_effects;
394         q3meffect_t *data_effects;
395
396         int num_faces;
397         q3mface_t *data_faces;
398
399         // lightmap textures
400         int num_lightmaps;
401         rtexture_t **data_lightmaps;
402
403         // voxel light data with directional shading
404         int num_lightgrid;
405         q3dlightgrid_t *data_lightgrid;
406         // size of each cell (may vary by map, typically 64 64 128)
407         float num_lightgrid_cellsize[3];
408         // 1.0 / num_lightgrid_cellsize
409         float num_lightgrid_scale[3];
410         // dimensions of the world model in lightgrid cells
411         int num_lightgrid_imins[3];
412         int num_lightgrid_imaxs[3];
413         int num_lightgrid_isize[3];
414         // indexing/clamping
415         int num_lightgrid_dimensions[3];
416         // transform modelspace coordinates to lightgrid index
417         matrix4x4_t num_lightgrid_indexfromworld;
418
419         // pvs
420         int num_pvsclusters;
421         int num_pvschainlength;
422         unsigned char *data_pvschains;
423         // example
424         //pvschain = model->brushq3.data_pvschains + mycluster * model->brushq3.num_pvschainlength;
425         //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
426 }
427 model_brushq3_t;
428
429 typedef struct model_s
430 {
431         // name and path of model, for example "progs/player.mdl"
432         char                    name[MAX_QPATH];
433         // model needs to be loaded if this is true
434         qboolean                needload;
435         // set if the model is used in current map, models which are not, are purged
436         qboolean                used;
437         // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
438         qboolean                isworldmodel;
439         // CRC of the file this model was loaded from, to reload if changed
440         unsigned int    crc;
441         // mod_brush, mod_alias, mod_sprite
442         modtype_t               type;
443         // memory pool for allocations
444         mempool_t               *mempool;
445         // all models use textures...
446         rtexturepool_t  *texturepool;
447         // flags from the model file
448         int                             flags;
449         // engine calculated flags, ones that can not be set in the file
450         int                             flags2;
451         // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
452         int                             fullbright;
453         // number of QC accessible frame(group)s in the model
454         int                             numframes;
455         // number of QC accessible skin(group)s in the model
456         int                             numskins;
457         // whether to randomize animated framegroups
458         synctype_t              synctype;
459         // bounding box at angles '0 0 0'
460         vec3_t                  normalmins, normalmaxs;
461         // bounding box if yaw angle is not 0, but pitch and roll are
462         vec3_t                  yawmins, yawmaxs;
463         // bounding box if pitch or roll are used
464         vec3_t                  rotatedmins, rotatedmaxs;
465         // sphere radius, usable at any angles
466         float                   radius;
467         // squared sphere radius for easier comparisons
468         float                   radius2;
469         // skin animation info
470         animscene_t             *skinscenes; // [numskins]
471         // skin animation info
472         animscene_t             *animscenes; // [numframes]
473         // draw the model's sky polygons (only used by brush models)
474         void(*DrawSky)(struct entity_render_s *ent);
475         // draw the model using lightmap/dlight shading
476         void(*Draw)(struct entity_render_s *ent);
477         // draw a fake shadow for the model
478         void(*DrawFakeShadow)(struct entity_render_s *ent);
479         // draw a shadow volume for the model based on light source
480         void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
481         // draw the lighting on a model (through stencil)
482         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);
483         // fields belonging to each type of model
484         model_alias_t   alias;
485         model_sprite_t  sprite;
486         model_brush_t   brush;
487         model_brushq1_t brushq1;
488         /* MSVC can't handle an empty struct, so this is commented out for now
489         model_brushq2_t brushq2;
490         */
491         model_brushq3_t brushq3;
492         // skin files can have different tags for each skin
493         overridetagnameset_t    *data_overridetagnamesforskin;
494 }
495 model_t;
496
497 //============================================================================
498
499 // this can be used for anything without a valid texture
500 extern rtexture_t *r_notexture;
501 #define NUM_DETAILTEXTURES 1
502 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
503 // every texture must be in a pool...
504 extern rtexturepool_t *mod_shared_texturepool;
505
506 // model loading
507 extern model_t *loadmodel;
508 extern qbyte *mod_base;
509 // sky/water subdivision
510 //extern cvar_t gl_subdivide_size;
511 // texture fullbrights
512 extern cvar_t r_fullbrights;
513
514 void Mod_Init (void);
515 void Mod_CheckLoaded (model_t *mod);
516 void Mod_ClearAll (void);
517 model_t *Mod_FindName (const char *name);
518 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
519 void Mod_TouchModel (const char *name);
520 void Mod_UnloadModel (model_t *mod);
521
522 void Mod_ClearUsed(void);
523 void Mod_PurgeUnused(void);
524 void Mod_LoadModels(void);
525
526 extern model_t *loadmodel;
527 extern char loadname[32];       // for hunk tags
528
529 int Mod_FindTriangleWithEdge(const int *elements, int numtriangles, int start, int end, int ignore);
530 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
531 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
532 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
533
534 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts);
535 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh);
536 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *v);
537 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, float *vert0, float *vert1, float *vert2);
538 void Mod_ShadowMesh_AddPolygon(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts);
539 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, float *verts, int numtris, int *elements);
540 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int initialnumtriangles);
541 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh);
542 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
543 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
544
545 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
546 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
547
548 extern cvar_t r_mipskins;
549
550 typedef struct skinfileitem_s
551 {
552         struct skinfileitem_s *next;
553         char name[MAX_QPATH];
554         char replacement[MAX_QPATH];
555 }
556 skinfileitem_t;
557
558 typedef struct skinfile_s
559 {
560         struct skinfile_s *next;
561         skinfileitem_t *items;
562 }
563 skinfile_t;
564
565 skinfile_t *Mod_LoadSkinFiles(void);
566 void Mod_FreeSkinFiles(skinfile_t *skinfile);
567 int Mod_CountSkinFiles(skinfile_t *skinfile);
568
569 #endif  // __MODEL__
570