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