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