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