]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.h
34f511e4baddd7fb3194cf4bcd12e164d8d3e12b
[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_SHARED_H
22 #define MODEL_SHARED_H
23
24 typedef enum {ST_SYNC=0, ST_RAND } synctype_t;
25
26 /*
27
28 d*_t structures are on-disk representations
29 m*_t structures are in-memory
30
31 */
32
33 typedef enum {mod_invalid, mod_brushq1, mod_sprite, mod_alias, mod_brushq2, mod_brushq3} modtype_t;
34
35 typedef struct animscene_s
36 {
37         char name[32]; // for viewthing support
38         int firstframe;
39         int framecount;
40         int loop; // true or false
41         float framerate;
42 }
43 animscene_t;
44
45 typedef struct skinframe_s
46 {
47         rtexture_t *stain; // inverse modulate with background (used for decals and such)
48         rtexture_t *merged; // original texture without glow
49         rtexture_t *base; // original texture without pants/shirt/glow
50         rtexture_t *pants; // pants only (in greyscale)
51         rtexture_t *shirt; // shirt only (in greyscale)
52         rtexture_t *nmap; // normalmap (bumpmap for dot3)
53         rtexture_t *gloss; // glossmap (for dot3)
54         rtexture_t *detail; // detail texture (silly bumps for non-dot3)
55         rtexture_t *glow; // glow only (fullbrights)
56         rtexture_t *fog; // alpha of the base texture (if not opaque)
57 }
58 skinframe_t;
59
60 #define MAX_SKINS 256
61
62 typedef struct overridetagname_s
63 {
64         char name[MAX_QPATH];
65 }
66 overridetagname_t;
67
68 // a replacement set of tag names, per skin
69 typedef struct overridetagnameset_s
70 {
71         int num_overridetagnames;
72         overridetagname_t *data_overridetagnames;
73 }
74 overridetagnameset_t;
75
76 // LordHavoc: replaces glpoly, triangle mesh
77 typedef struct surfmesh_s
78 {
79         int num_vertices; // number of vertices in the mesh
80         int num_triangles; // number of triangles in the mesh
81         float *data_vertex3f; // float[verts*3] vertex locations
82         float *data_svector3f; // float[verts*3] direction of 'S' (right) texture axis for each vertex
83         float *data_tvector3f; // float[verts*3] direction of 'T' (down) texture axis for each vertex
84         float *data_normal3f; // float[verts*3] direction of 'R' (out) texture axis for each vertex
85         int *data_lightmapoffsets; // index into surface's lightmap samples for vertex lighting
86         float *data_texcoordtexture2f; // float[verts*2] texcoords for surface texture
87         float *data_texcoordlightmap2f; // float[verts*2] texcoords for lightmap texture
88         float *data_texcoorddetail2f; // float[verts*2] texcoords for detail texture
89         float *data_lightmapcolor4f;
90         int *data_element3i; // int[tris*3] triangles of the mesh, 3 indices into vertex arrays for each
91         int *data_neighbor3i; // int[tris*3] neighboring triangle on each edge (-1 if none)
92
93         int num_collisionvertices;
94         int num_collisiontriangles;
95         float *data_collisionvertex3f;
96         int *data_collisionelement3i;
97 }
98 surfmesh_t;
99
100 #define SHADOWMESHVERTEXHASH 1024
101 typedef struct shadowmeshvertexhash_s
102 {
103         struct shadowmeshvertexhash_s *next;
104 }
105 shadowmeshvertexhash_t;
106
107 typedef struct shadowmesh_s
108 {
109         // next mesh in chain
110         struct shadowmesh_s *next;
111         // used for light mesh (NULL on shadow mesh)
112         rtexture_t *map_diffuse;
113         rtexture_t *map_specular;
114         rtexture_t *map_normal;
115         // buffer sizes
116         int numverts, maxverts;
117         int numtriangles, maxtriangles;
118         // used always
119         float *vertex3f;
120         // used for light mesh (NULL on shadow mesh)
121         float *svector3f;
122         float *tvector3f;
123         float *normal3f;
124         float *texcoord2f;
125         // used always
126         int *element3i;
127         // used for shadow mesh (NULL on light mesh)
128         int *neighbor3i;
129         // these are NULL after Mod_ShadowMesh_Finish is performed, only used
130         // while building meshes
131         shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
132 }
133 shadowmesh_t;
134
135
136 #include "matrixlib.h"
137
138 #include "model_brush.h"
139 #include "model_sprite.h"
140 #include "model_alias.h"
141
142 typedef struct model_alias_s
143 {
144         // mdl/md2/md3/zym model formats are treated the same after loading
145
146         // the shader meshes comprising this model
147         int                             aliasnum_meshes;
148         aliasmesh_t             *aliasdata_meshes;
149
150         // for md3 models
151         int                             aliasnum_tags;
152         int                             aliasnum_tagframes;
153         aliastag_t              *aliasdata_tags;
154
155         // for skeletal models
156         int                             aliasnum_bones;
157         aliasbone_t             *aliasdata_bones;
158         int                             aliasnum_poses;
159         float                   *aliasdata_poses;
160 }
161 model_alias_t;
162
163 typedef struct model_sprite_s
164 {
165         int                             sprnum_type;
166         mspriteframe_t  *sprdata_frames;
167 }
168 model_sprite_t;
169
170 struct trace_s;
171
172 typedef struct model_brush_s
173 {
174         // true if this model is a HalfLife .bsp file
175         qboolean ishlbsp;
176         // string of entity definitions (.map format)
177         char *entities;
178         // number of submodels in this map (just used by server to know how many
179         // submodels to load)
180         int numsubmodels;
181         // pointers to each of the submodels if .isworldmodel is true
182         struct model_s **submodels;
183
184         int num_planes;
185         mplane_t *data_planes;
186
187         int num_nodes;
188         mnode_t *data_nodes;
189
190         // visible leafs, not counting 0 (solid)
191         int num_visleafs;
192         // number of actual leafs (including 0 which is solid)
193         int num_leafs;
194         mleaf_t *data_leafs;
195
196         int num_leafbrushes;
197         int *data_leafbrushes;
198
199         int num_leaffaces;
200         int *data_leaffaces;
201
202         int num_portals;
203         mportal_t *data_portals;
204
205         int num_portalpoints;
206         mvertex_t *data_portalpoints;
207
208         // pvs
209         int num_pvsclusters;
210         int num_pvsclusterbytes;
211         unsigned char *data_pvsclusters;
212         // example
213         //pvschain = model->brush.data_pvsclusters + mycluster * model->brush.num_pvsclusterbytes;
214         //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
215
216         // a mesh containing all shadow casting geometry for the whole model (including submodels), portions of this are referenced by each surface's num_firstshadowmeshtriangle
217         shadowmesh_t *shadowmesh;
218
219         // common functions
220         int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
221         int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
222         qbyte *(*GetPVS)(struct model_s *model, const vec3_t p);
223         int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength);
224         int (*BoxTouchingPVS)(struct model_s *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs);
225         void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
226         void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
227         // these are actually only found on brushq1, but NULL is handled gracefully
228         void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, qbyte *out, int outsize);
229         void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
230
231         char skybox[64];
232
233         rtexture_t *solidskytexture;
234         rtexture_t *alphaskytexture;
235 }
236 model_brush_t;
237
238 typedef struct model_brushq1_s
239 {
240         // lightmap format, set to r_lightmaprgba when model is loaded
241         int                             lightmaprgba;
242
243         dmodel_t                *submodels;
244
245         int                             numvertexes;
246         mvertex_t               *vertexes;
247
248         int                             numedges;
249         medge_t                 *edges;
250
251         int                             numtexinfo;
252         mtexinfo_t              *texinfo;
253
254         int                             numsurfaces;
255         msurface_t              *surfaces;
256         msurface_t              *surfacepvsnext;
257
258         int                             numsurfedges;
259         int                             *surfedges;
260
261         int                             numclipnodes;
262         dclipnode_t             *clipnodes;
263
264         hull_t                  hulls[MAX_MAP_HULLS];
265
266         int                             numtextures;
267         texture_t               *textures;
268
269         int                             num_compressedpvs;
270         qbyte                   *data_compressedpvs;
271
272         int                             num_lightdata;
273         qbyte                   *lightdata;
274
275         int                             numlights;
276         mlight_t                *lights;
277
278         // lightmap update chains for light styles
279         int                             light_styles;
280         qbyte                   *light_style;
281         int                             *light_stylevalue;
282         msurface_t              ***light_styleupdatechains;
283         msurface_t              **light_styleupdatechainsbuffer;
284
285         mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
286 }
287 model_brushq1_t;
288
289 /* MSVC can't compile empty structs, so this is commented out for now
290 typedef struct model_brushq2_s
291 {
292 }
293 model_brushq2_t;
294 */
295
296 typedef struct model_brushq3_s
297 {
298         // if non-zero this is a submodel
299         // (this is the number of the submodel, an index into data_models)
300         int submodel;
301
302         int num_textures;
303         q3mtexture_t *data_textures;
304
305         int num_models;
306         q3mmodel_t *data_models;
307         // each submodel gets its own model struct so this is different for each.
308         q3mmodel_t *data_thismodel;
309
310         int num_brushes;
311         q3mbrush_t *data_brushes;
312
313         int num_brushsides;
314         q3mbrushside_t *data_brushsides;
315
316         // freed after loading!
317         int num_vertices;
318         float *data_vertex3f;
319         float *data_texcoordtexture2f;
320         float *data_texcoordlightmap2f;
321         float *data_color4f;
322
323         // freed after loading!
324         int num_triangles;
325         int *data_element3i;
326
327         int num_effects;
328         q3meffect_t *data_effects;
329
330         int num_faces;
331         q3msurface_t *data_faces;
332
333         // lightmap textures
334         int num_lightmaps;
335         rtexture_t **data_lightmaps;
336
337         // voxel light data with directional shading
338         int num_lightgrid;
339         q3dlightgrid_t *data_lightgrid;
340         // size of each cell (may vary by map, typically 64 64 128)
341         float num_lightgrid_cellsize[3];
342         // 1.0 / num_lightgrid_cellsize
343         float num_lightgrid_scale[3];
344         // dimensions of the world model in lightgrid cells
345         int num_lightgrid_imins[3];
346         int num_lightgrid_imaxs[3];
347         int num_lightgrid_isize[3];
348         // indexing/clamping
349         int num_lightgrid_dimensions[3];
350         // transform modelspace coordinates to lightgrid index
351         matrix4x4_t num_lightgrid_indexfromworld;
352 }
353 model_brushq3_t;
354
355 typedef struct model_s
356 {
357         // name and path of model, for example "progs/player.mdl"
358         char                    name[MAX_QPATH];
359         // model needs to be loaded if this is false
360         qboolean                loaded;
361         // set if the model is used in current map, models which are not, are purged
362         qboolean                used;
363         // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
364         qboolean                isworldmodel;
365         // CRC of the file this model was loaded from, to reload if changed
366         unsigned int    crc;
367         // mod_brush, mod_alias, mod_sprite
368         modtype_t               type;
369         // memory pool for allocations
370         mempool_t               *mempool;
371         // all models use textures...
372         rtexturepool_t  *texturepool;
373         // flags from the model file
374         int                             flags;
375         // engine calculated flags, ones that can not be set in the file
376         int                             flags2;
377         // number of QC accessible frame(group)s in the model
378         int                             numframes;
379         // number of QC accessible skin(group)s in the model
380         int                             numskins;
381         // whether to randomize animated framegroups
382         synctype_t              synctype;
383         // bounding box at angles '0 0 0'
384         vec3_t                  normalmins, normalmaxs;
385         // bounding box if yaw angle is not 0, but pitch and roll are
386         vec3_t                  yawmins, yawmaxs;
387         // bounding box if pitch or roll are used
388         vec3_t                  rotatedmins, rotatedmaxs;
389         // sphere radius, usable at any angles
390         float                   radius;
391         // squared sphere radius for easier comparisons
392         float                   radius2;
393         // skin animation info
394         animscene_t             *skinscenes; // [numskins]
395         // skin animation info
396         animscene_t             *animscenes; // [numframes]
397         // range of surface numbers in this (sub)model
398         int                             firstmodelsurface;
399         int                             nummodelsurfaces;
400         // list of surface numbers in this (sub)model
401         int                             *surfacelist;
402         // surface meshes are merged to a smaller set of meshes to allow reduced
403         // vertex array switching, the meshes are limited to 65536 vertices each
404         // to play nice with Geforce1 hardware
405         int                             nummeshes;
406         surfmesh_t              **meshlist;
407         // draw the model's sky polygons (only used by brush models)
408         void(*DrawSky)(struct entity_render_s *ent);
409         // draw the model using lightmap/dlight shading
410         void(*Draw)(struct entity_render_s *ent);
411         // gathers info on which clusters and surfaces are lit by light, as well as calculating a bounding box
412         void(*GetLightInfo)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer);
413         // draw a shadow volume for the model based on light source
414         void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist);
415         // draw the lighting on a model (through stencil)
416         void(*DrawLight)(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist);
417         // trace a box against this model
418         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);
419         // fields belonging to each type of model
420         model_alias_t   alias;
421         model_sprite_t  sprite;
422         model_brush_t   brush;
423         model_brushq1_t brushq1;
424         /* MSVC can't handle an empty struct, so this is commented out for now
425         model_brushq2_t brushq2;
426         */
427         model_brushq3_t brushq3;
428         // skin files can have different tags for each skin
429         overridetagnameset_t    *data_overridetagnamesforskin;
430         // flags this model for offseting sounds to the model center (used by brush models)
431         int soundfromcenter;
432 }
433 model_t;
434
435 //============================================================================
436
437 // this can be used for anything without a valid texture
438 extern rtexture_t *r_notexture;
439 #define NUM_DETAILTEXTURES 1
440 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
441 // every texture must be in a pool...
442 extern rtexturepool_t *mod_shared_texturepool;
443
444 extern rtexture_t *mod_shared_distorttexture[64];
445
446 // model loading
447 extern model_t *loadmodel;
448 extern qbyte *mod_base;
449 // sky/water subdivision
450 //extern cvar_t gl_subdivide_size;
451 // texture fullbrights
452 extern cvar_t r_fullbrights;
453
454 void Mod_Init (void);
455 void Mod_CheckLoaded (model_t *mod);
456 void Mod_ClearAll (void);
457 model_t *Mod_FindName (const char *name);
458 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
459 void Mod_UnloadModel (model_t *mod);
460
461 void Mod_ClearUsed(void);
462 void Mod_PurgeUnused(void);
463 void Mod_LoadModels(void);
464
465 extern model_t *loadmodel;
466 extern char loadname[32];       // for hunk tags
467
468 int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements, int numvertices, int *remapvertices);
469 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
470 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
471 void Mod_BuildNormals(int numverts, int numtriangles, const float *vertex3f, const int *elements, float *normal3f);
472 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
473
474 surfmesh_t *Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, int numcollisionvertices, int numcollisiontriangles, qboolean detailtexcoords, qboolean lightmapoffsets, qboolean vertexcolors);
475
476 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);
477 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh, int light, int neighbors);
478 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *vertex14f);
479 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, float *vertex14f);
480 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *texcoord2f, int numtris, const int *element3i);
481 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);
482 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh, int light, int neighbors);
483 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
484 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
485
486 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
487 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
488
489 // used for talking to the QuakeC mainly
490 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
491 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
492
493 extern cvar_t r_mipskins;
494
495 typedef struct skinfileitem_s
496 {
497         struct skinfileitem_s *next;
498         char name[MAX_QPATH];
499         char replacement[MAX_QPATH];
500 }
501 skinfileitem_t;
502
503 typedef struct skinfile_s
504 {
505         struct skinfile_s *next;
506         skinfileitem_t *items;
507 }
508 skinfile_t;
509
510 skinfile_t *Mod_LoadSkinFiles(void);
511 void Mod_FreeSkinFiles(skinfile_t *skinfile);
512 int Mod_CountSkinFiles(skinfile_t *skinfile);
513
514 void Mod_SnapVertices(int numcomponents, int numvertices, float *vertices, float snap);
515 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);
516
517 #endif  // MODEL_SHARED_H
518