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