]> git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.h
COPYING: Remove formfeed characters that don't serve any purpose but make vscode...
[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 #include <stddef.h>
25 #include "qdefs.h"
26 #include "bspfile.h"
27 #include "r_qshader.h"
28 #include "matrixlib.h"
29 struct rtexture_s;
30 struct mempool_s;
31 struct skeleton_s;
32 struct skinframe_s;
33
34 typedef enum synctype_e {ST_SYNC=0, ST_RAND } synctype_t;
35
36 /*
37
38 d*_t structures are on-disk representations
39 m*_t structures are in-memory
40
41 */
42
43 typedef enum modtype_e {mod_invalid, mod_brushq1, mod_sprite, mod_alias, mod_brushq2, mod_brushq3, mod_brushhl2, mod_obj, mod_null} modtype_t;
44
45 typedef struct animscene_s
46 {
47         char name[32]; // for viewthing support
48         int firstframe;
49         int framecount;
50         int loop; // true or false
51         float framerate;
52 }
53 animscene_t;
54
55 struct md3vertex_s;
56 struct trivertx_s;
57 typedef struct texvecvertex_s
58 {
59         signed char svec[3];
60         signed char tvec[3];
61 }
62 texvecvertex_t;
63
64 typedef struct blendweights_s
65 {
66         unsigned char index[4];
67         unsigned char influence[4];
68 }
69 blendweights_t;
70
71 typedef struct r_meshbuffer_s
72 {
73         int bufferobject; // OpenGL
74         void *devicebuffer; // Direct3D
75         size_t size;
76         qbool isindexbuffer;
77         qbool isuniformbuffer;
78         qbool isdynamic;
79         qbool isindex16;
80         char name[MAX_QPATH];
81 }
82 r_meshbuffer_t;
83
84 // used for mesh lists in q1bsp/q3bsp map models
85 // (the surfaces reference portions of these meshes)
86 typedef struct surfmesh_s
87 {
88         // triangle data in system memory
89         int num_triangles; // number of triangles in the mesh
90         int *data_element3i; // int[tris*3] triangles of the mesh, 3 indices into vertex arrays for each
91
92         // vertex data in system memory
93         int num_vertices; // number of vertices in the mesh
94         float *data_vertex3f; // float[verts*3] vertex locations
95         float *data_svector3f; // float[verts*3] direction of 'S' (right) texture axis for each vertex
96         float *data_tvector3f; // float[verts*3] direction of 'T' (down) texture axis for each vertex
97         float *data_normal3f; // float[verts*3] direction of 'R' (out) texture axis for each vertex
98         float *data_texcoordtexture2f; // float[verts*2] texcoords for surface texture
99         float *data_texcoordlightmap2f; // float[verts*2] texcoords for lightmap texture
100         float *data_lightmapcolor4f;
101         unsigned char *data_skeletalindex4ub;
102         unsigned char *data_skeletalweight4ub;
103         int *data_lightmapoffsets; // index into surface's lightmap samples for vertex lighting
104         // index buffer - only one of these will be non-NULL
105         r_meshbuffer_t *data_element3i_indexbuffer;
106         int data_element3i_bufferoffset;
107         unsigned short *data_element3s; // unsigned short[tris*3] triangles of the mesh in unsigned short format (NULL if num_vertices > 65536)
108         r_meshbuffer_t *data_element3s_indexbuffer;
109         int data_element3s_bufferoffset;
110         // vertex buffers
111         r_meshbuffer_t *data_vertex3f_vertexbuffer;
112         int data_vertex3f_bufferoffset;
113         r_meshbuffer_t *data_svector3f_vertexbuffer;
114         int data_svector3f_bufferoffset;
115         r_meshbuffer_t *data_tvector3f_vertexbuffer;
116         int data_tvector3f_bufferoffset;
117         r_meshbuffer_t *data_normal3f_vertexbuffer;
118         int data_normal3f_bufferoffset;
119         r_meshbuffer_t *data_texcoordtexture2f_vertexbuffer;
120         int data_texcoordtexture2f_bufferoffset;
121         r_meshbuffer_t *data_texcoordlightmap2f_vertexbuffer;
122         int data_texcoordlightmap2f_bufferoffset;
123         r_meshbuffer_t *data_lightmapcolor4f_vertexbuffer;
124         int data_lightmapcolor4f_bufferoffset;
125         r_meshbuffer_t *data_skeletalindex4ub_vertexbuffer;
126         int data_skeletalindex4ub_bufferoffset;
127         r_meshbuffer_t *data_skeletalweight4ub_vertexbuffer;
128         int data_skeletalweight4ub_bufferoffset;
129         // morph blending, these are zero if model is skeletal or static
130         int num_morphframes;
131         struct md3vertex_s *data_morphmd3vertex;
132         struct trivertx_s *data_morphmdlvertex;
133         struct texvecvertex_s *data_morphtexvecvertex;
134         float *data_morphmd2framesize6f;
135         float num_morphmdlframescale[3];
136         float num_morphmdlframetranslate[3];
137         // skeletal blending, these are NULL if model is morph or static
138         struct blendweights_s *data_blendweights;
139         int num_blends;
140         unsigned short *blends;
141         // set if there is some kind of animation on this model
142         qbool isanimated;
143
144         // dynamic mesh building support (Mod_Mesh_*)
145         int num_vertexhashsize; // always pow2 for simple masking
146         int *data_vertexhash; // hash table - wrapping buffer for storing index of similar vertex with -1 as terminator
147         int max_vertices; // preallocated size of data_vertex3f and friends (always >= num_vertices)
148         int max_triangles; // preallocated size of data_element3i
149 }
150 surfmesh_t;
151
152 #define SHADOWMESHVERTEXHASH 1024
153 typedef struct shadowmeshvertexhash_s
154 {
155         struct shadowmeshvertexhash_s *next;
156 }
157 shadowmeshvertexhash_t;
158
159 typedef struct shadowmesh_s
160 {
161         struct mempool_s *mempool;
162
163         int numverts;
164         int maxverts;
165         float *vertex3f;
166         r_meshbuffer_t *vbo_vertexbuffer;
167         int vbooffset_vertex3f;
168
169         int numtriangles;
170         int maxtriangles;
171         int *element3i;
172         r_meshbuffer_t *element3i_indexbuffer;
173         int element3i_bufferoffset;
174         unsigned short *element3s;
175         r_meshbuffer_t *element3s_indexbuffer;
176         int element3s_bufferoffset;
177
178         // used for shadow mapping cubemap side partitioning
179         int sideoffsets[6], sidetotals[6];
180
181         // these are NULL after Mod_ShadowMesh_Finish is performed, only used
182         // while building meshes
183         shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
184 }
185 shadowmesh_t;
186
187 typedef struct texture_s
188 {
189         // name
190         char name[64];
191
192         // q1bsp
193         // size
194         unsigned int width, height;
195         // SURF_ flags
196         //unsigned int flags;
197
198         // base material flags
199         int basematerialflags;
200         // current material flags (updated each bmodel render)
201         int currentmaterialflags;
202         // base material alpha (used for Q2 materials)
203         float basealpha;
204
205         // PolygonOffset values for rendering this material
206         // (these are added to the r_refdef values and submodel values)
207         float biaspolygonfactor;
208         float biaspolygonoffset;
209
210         // textures to use when rendering this material (derived from materialshaderpass)
211         struct skinframe_s *currentskinframe;
212         // textures to use for terrain texture blending (derived from backgroundshaderpass)
213         struct skinframe_s *backgroundcurrentskinframe;
214
215         // total frames in sequence and alternate sequence
216         int anim_total[2];
217         // direct pointers to each of the frames in the sequences
218         // (indexed as [alternate][frame])
219         struct texture_s *anim_frames[2][10];
220         // 1 = q1bsp animation with anim_total[0] >= 2 (animated) or anim_total[1] >= 1 (alternate frame set)
221         // 2 = q2bsp animation with anim_total[0] >= 2 (uses self.frame)
222         int animated;
223
224         // renderer checks if this texture needs updating...
225         int update_lastrenderframe;
226         void *update_lastrenderentity;
227         // the current alpha of this texture (may be affected by r_wateralpha, also basealpha, and ent->alpha)
228         float currentalpha;
229         // current value of blendfunc - one of:
230         // {GL_SRC_ALPHA, GL_ONE}
231         // {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA}
232         // {customblendfunc[0], customblendfunc[1]}
233         // {GL_ONE, GL_ZERO}
234         int currentblendfunc[2];
235         // the current texture frame in animation
236         struct texture_s *currentframe;
237         // current texture transform matrix (used for water scrolling)
238         matrix4x4_t currenttexmatrix;
239         matrix4x4_t currentbackgroundtexmatrix;
240
241         // various q3 shader features
242         q3shaderinfo_deform_t deforms[Q3MAXDEFORMS];
243         texture_shaderpass_t *shaderpasses[Q3SHADER_MAXLAYERS]; // all shader passes in one array
244         texture_shaderpass_t *materialshaderpass; // equal to one of shaderpasses[] or NULL
245         texture_shaderpass_t *backgroundshaderpass; // equal to one of shaderpasses[] or NULL
246         unsigned char startpreshaderpass; // range within shaderpasses[]
247         unsigned char endpreshaderpass; // number of preshaderpasses
248         unsigned char startpostshaderpass; // range within shaderpasses[]
249         unsigned char endpostshaderpass; // number of postshaderpasses
250
251         qbool colormapping;
252         struct rtexture_s *basetexture; // original texture without pants/shirt/glow
253         struct rtexture_s *pantstexture; // pants only (in greyscale)
254         struct rtexture_s *shirttexture; // shirt only (in greyscale)
255         struct rtexture_s *nmaptexture; // normalmap (bumpmap for dot3)
256         struct rtexture_s *glosstexture; // glossmap (for dot3)
257         struct rtexture_s *glowtexture; // glow only (fullbrights)
258         struct rtexture_s *fogtexture; // alpha of the base texture (if not opaque)
259         struct rtexture_s *reflectmasktexture; // mask for fake reflections
260         struct rtexture_s *reflectcubetexture; // fake reflections cubemap
261         struct rtexture_s *backgroundbasetexture; // original texture without pants/shirt/glow
262         struct rtexture_s *backgroundnmaptexture; // normalmap (bumpmap for dot3)
263         struct rtexture_s *backgroundglosstexture; // glossmap (for dot3)
264         struct rtexture_s *backgroundglowtexture; // glow only (fullbrights)
265         float specularpower;
266
267         // rendering parameters - updated by R_GetCurrentTexture using rsurface.render_* fields
268         // (almost) all map textures are lightmap (no MATERIALFLAG_MODELLIGHT set),
269         // (almost) all model textures are MATERIALFLAG_MODELLIGHT,
270         // MATERIALFLAG_FULLBRIGHT is rendered as a forced MATERIALFLAG_MODELLIGHT with rtlights disabled
271         float render_glowmod[3];
272         // MATERIALFLAG_MODELLIGHT uses these parameters
273         float render_modellight_ambient[3];
274         float render_modellight_diffuse[3];
275         float render_modellight_lightdir_world[3];
276         float render_modellight_lightdir_local[3];
277         float render_modellight_specular[3];
278         // lightmap rendering (not MATERIALFLAG_MODELLIGHT)
279         float render_lightmap_ambient[3];
280         float render_lightmap_diffuse[3];
281         float render_lightmap_specular[3];
282         // rtlights use these colors for the materials on this entity
283         float render_rtlight_diffuse[3];
284         float render_rtlight_specular[3];
285         // tint applied on top of render_*_diffuse for pants layer
286         float render_colormap_pants[3];
287         // tint applied on top of render_*_diffuse for shirt layer
288         float render_colormap_shirt[3];
289
290         // from q3 shaders
291         int customblendfunc[2];
292
293         // q3bsp
294         int surfaceflags;
295         int supercontents;
296
297         // q2bsp
298         // we have to load the texture multiple times when Q2SURF_ flags differ,
299         // though it still shares the skinframe
300         int q2flags;
301         int q2value;
302         int q2contents;
303
304         // q1qsp
305         /// this points to a variant of the sky texture that has MATERIALFLAG_NOSHADOW, for the e1m5 logo shadow trick.
306         struct texture_s *skynoshadowtexture;
307
308         // reflection
309         float reflectmin; // when refraction is used, minimum amount of reflection (when looking straight down)
310         float reflectmax; // when refraction is used, maximum amount of reflection (when looking parallel to water)
311         float refractfactor; // amount of refraction distort (1.0 = like the cvar specifies)
312         vec4_t refractcolor4f; // color tint of refraction (including alpha factor)
313         float reflectfactor; // amount of reflection distort (1.0 = like the cvar specifies)
314         vec4_t reflectcolor4f; // color tint of reflection (including alpha factor)
315         float r_water_wateralpha; // additional wateralpha to apply when r_water is active
316         float r_water_waterscroll[2]; // scale and speed
317         float refractive_index; // used by r_shadow_bouncegrid for bending photons for refracted light
318         int camera_entity; // entity number for use by cameras
319
320         // offsetmapping
321         dpoffsetmapping_technique_t offsetmapping;
322         float offsetscale;
323         float offsetbias;
324
325         // transparent sort category
326         dptransparentsortcategory_t transparentsort;
327
328         // gloss
329         float specularscalemod;
330         float specularpowermod;
331
332         // diffuse and ambient
333         float rtlightambient;
334
335         // used by Mod_Mesh_GetTexture for drawflag and materialflag overrides, to disambiguate the same texture with different hints
336         int mesh_drawflag;
337         int mesh_defaulttexflags;
338         int mesh_defaultmaterialflags;
339 }
340  texture_t;
341
342 typedef struct mtexinfo_s
343 {
344         float           vecs[2][4];             // [s/t][xyz offset]
345         int                     textureindex;
346         int                     q1flags;
347         int                     q2flags;                        // miptex flags + overrides
348         int                     q2value;                        // light emission, etc
349         char            q2texture[32];  // texture name (textures/*.wal)
350         int                     q2nexttexinfo;  // for animations, -1 = end of chain
351 }
352 mtexinfo_t;
353
354 typedef struct msurface_lightmapinfo_s
355 {
356         // texture mapping properties used by this surface
357         mtexinfo_t *texinfo; // q1bsp
358         // index into r_refdef.scene.lightstylevalue array, 255 means not used (black)
359         unsigned char styles[MAXLIGHTMAPS]; // q1bsp
360         // RGB lighting data [numstyles][height][width][3]
361         unsigned char *samples; // q1bsp
362         // RGB normalmap data [numstyles][height][width][3]
363         unsigned char *nmapsamples; // q1bsp
364         // stain to apply on lightmap (soot/dirt/blood/whatever)
365         unsigned char *stainsamples; // q1bsp
366         int texturemins[2]; // q1bsp
367         int extents[2]; // q1bsp
368         int lightmaporigin[2]; // q1bsp
369 }
370 msurface_lightmapinfo_t;
371
372 struct q3deffect_s;
373
374 /// <summary>
375 ///  describes the textures to use on a range of triangles in the model, and mins/maxs (AABB) for culling.
376 /// </summary>
377 typedef struct msurface_s
378 {
379         /// range of triangles and vertices in model->surfmesh
380         int num_triangles; // triangles
381         int num_firsttriangle; // first element is this *3
382         int num_vertices; // length of the range referenced by elements
383         int num_firstvertex; // min vertex referenced by elements
384
385         /// the texture to use on the surface
386         texture_t *texture;
387         /// the lightmap texture fragment to use on the rendering mesh
388         struct rtexture_s *lightmaptexture;
389         /// the lighting direction texture fragment to use on the rendering mesh
390         struct rtexture_s *deluxemaptexture;
391
392         // the following fields are used situationally and are not part of rendering in typical usage
393
394         /// bounding box for onscreen checks
395         vec3_t mins;
396         vec3_t maxs;
397
398         /// lightmaptexture rebuild information not used in q3bsp
399         msurface_lightmapinfo_t* lightmapinfo; // q1bsp
400         /// fog volume info in q3bsp
401         struct q3deffect_s* effect; // q3bsp
402
403         /// mesh information for collisions (only used by q3bsp curves)
404         int num_firstcollisiontriangle; // q3bsp only
405         int num_collisiontriangles; // number of triangles (if surface has collisions enabled)
406         int num_collisionvertices; // number of vertices referenced by collision triangles (if surface has collisions enabled)
407
408         // used by Mod_Mesh_Finalize when building sortedmodelsurfaces
409         qbool included;
410 }
411 msurface_t;
412
413 #include "bih.h"
414
415 #include "model_brush.h"
416 #include "model_q1bsp.h"
417 #include "model_q2bsp.h"
418 #include "model_q3bsp.h"
419 #include "model_vbsp.h"
420 #include "model_sprite.h"
421 #include "model_alias.h"
422
423 struct trace_s;
424
425 struct frameblend_s;
426 struct skeleton_s;
427
428 typedef struct model_s
429 {
430         // name and path of model, for example "progs/player.mdl"
431         char                    name[MAX_QPATH];
432         // model needs to be loaded if this is false
433         qbool           loaded;
434         // set if the model is used in current map, models which are not, are purged
435         qbool           used;
436         // CRC of the file this model was loaded from, to reload if changed
437         unsigned int    crc;
438         // mod_brush, mod_alias, mod_sprite
439         modtype_t               type;
440         // memory pool for allocations
441         struct mempool_s                *mempool;
442         // all models use textures...
443         rtexturepool_t  *texturepool;
444         // EF_* flags (translated from the model file's different flags layout)
445         int                             effects;
446         // number of QC accessible frame(group)s in the model
447         int                             numframes;
448         // number of QC accessible skin(group)s in the model
449         int                             numskins;
450         // whether to randomize animated framegroups
451         synctype_t              synctype;
452         // bounding box at angles '0 0 0'
453         vec3_t                  normalmins, normalmaxs;
454         // bounding box if yaw angle is not 0, but pitch and roll are
455         vec3_t                  yawmins, yawmaxs;
456         // bounding box if pitch or roll are used
457         vec3_t                  rotatedmins, rotatedmaxs;
458         // sphere radius, usable at any angles
459         float                   radius;
460         // squared sphere radius for easier comparisons
461         float                   radius2;
462         // skin animation info
463         animscene_t             *skinscenes; // [numskins]
464         // skin animation info
465         animscene_t             *animscenes; // [numframes]
466         // range of surface numbers in this model
467         int                             submodelsurfaces_start;
468         int                             submodelsurfaces_end;
469         /// surface indices of model in an optimal draw order (submodelindex -> texture -> lightmap -> index)
470         int                             *modelsurfaces_sorted; // same size as num_surfaces
471         // range of collision brush numbers in this (sub)model
472         int                             firstmodelbrush;
473         int                             nummodelbrushes;
474         // BIH (Bounding Interval Hierarchy) for this (sub)model
475         bih_t                   collision_bih;
476         bih_t                   render_bih; // if not set, use collision_bih instead for rendering purposes too
477         // for md3 models
478         int                             num_tags;
479         int                             num_tagframes;
480         aliastag_t              *data_tags;
481         // for skeletal models
482         int                             num_bones;
483         aliasbone_t             *data_bones;
484         float                   num_posescale; // scaling factor from origin in poses7s format (includes divide by 32767)
485         float                   num_poseinvscale; // scaling factor to origin in poses7s format (includes multiply by 32767)
486         int                             num_poses;
487         short                   *data_poses7s; // origin xyz, quat xyzw, unit length, values normalized to +/-32767 range
488         float                   *data_baseboneposeinverse;
489         // textures of this model
490         int                             num_textures;
491         int                             max_textures; // preallocated for expansion (Mod_Mesh_*)
492         int                             num_texturesperskin;
493         texture_t               *data_textures;
494         qbool           wantnormals;
495         qbool           wanttangents;
496         // surfaces of this model
497         int                             num_surfaces;
498         int                             max_surfaces; // preallocated for expansion (Mod_Mesh_*)
499         msurface_t              *data_surfaces;
500         // optional lightmapinfo data for surface lightmap updates
501         msurface_lightmapinfo_t *data_surfaces_lightmapinfo;
502         // all surfaces belong to this mesh
503         surfmesh_t              surfmesh;
504         // data type of model
505         const char              *modeldatatypestring;
506         // generates vertex data for a given frameblend
507         void(*AnimateVertices)(const struct model_s * RESTRICT model, const struct frameblend_s * RESTRICT frameblend, const struct skeleton_s *skeleton, float * RESTRICT vertex3f, float * RESTRICT normal3f, float * RESTRICT svector3f, float * RESTRICT tvector3f);
508         // draw the model's sky polygons
509         void(*DrawSky)(struct entity_render_s *ent);
510         // draw refraction/reflection textures for the model's water polygons
511         void(*DrawAddWaterPlanes)(struct entity_render_s *ent);
512         // draw the model using lightmap/dlight shading
513         void(*Draw)(struct entity_render_s *ent);
514         // draw the model to the depth buffer (no color rendering at all)
515         void(*DrawDepth)(struct entity_render_s *ent);
516         // draw any enabled debugging effects on this model (such as showing triangles, normals, collision brushes...)
517         void(*DrawDebug)(struct entity_render_s *ent);
518         // draw geometry textures for deferred rendering
519         void(*DrawPrepass)(struct entity_render_s *ent);
520     // compile an optimized shadowmap mesh for the model based on light source
521         void(*CompileShadowMap)(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist);
522         // draw depth into a shadowmap
523         void(*DrawShadowMap)(int side, struct entity_render_s *ent, const vec3_t relativelightorigin, const vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist, const unsigned char *surfacesides, const vec3_t lightmins, const vec3_t lightmaxs);
524         // gathers info on which clusters and surfaces are lit by light, as well as calculating a bounding box
525         void(*GetLightInfo)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outleaflist, unsigned char *outleafpvs, int *outnumleafspointer, int *outsurfacelist, unsigned char *outsurfacepvs, int *outnumsurfacespointer, unsigned char *outshadowtrispvs, unsigned char *outlighttrispvs, unsigned char *visitingleafpvs, int numfrustumplanes, const mplane_t *frustumplanes, qbool noocclusion);
526         // draw the lighting on a model (through stencil)
527         void(*DrawLight)(struct entity_render_s *ent, int numsurfaces, const int *surfacelist, const unsigned char *trispvs);
528         // trace a box against this model
529         void (*TraceBox)(struct model_s *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
530         void (*TraceBrush)(struct model_s *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, struct colbrushf_s *start, struct colbrushf_s *end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
531         // trace a box against this model
532         void (*TraceLine)(struct model_s *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
533         // trace a point against this model (like PointSuperContents)
534         void (*TracePoint)(struct model_s *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
535         // find the supercontents value at a point in this model
536         int (*PointSuperContents)(struct model_s *model, int frame, const vec3_t point);
537         // trace a line against geometry in this model and report correct texture (used by r_shadow_bouncegrid)
538         void (*TraceLineAgainstSurfaces)(struct model_s *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
539         // fields belonging to some types of model
540         model_sprite_t  sprite;
541         model_brush_t   brush;
542         model_brushq1_t brushq1;
543         model_brushq3_t brushq3;
544         // flags this model for offseting sounds to the model center (used by brush models)
545         int soundfromcenter;
546
547         // if set, the model contains light information (lightmap, or vertexlight)
548         qbool lit;
549         float lightmapscale;
550
551         qbool nolerp;
552 }
553 model_t;
554
555 //============================================================================
556
557 // model loading
558 extern model_t *loadmodel;
559 extern unsigned char *mod_base;
560
561 typedef struct modloader_s
562 {
563         const char *extension;
564         const char *header;
565         size_t headersize; // The header might not be NULL terminated
566         void (*Load)(model_t *, void *, void *);
567 } modloader_t;
568
569 // sky/water subdivision
570 //extern struct cvar_s gl_subdivide_size;
571 // texture fullbrights
572 extern struct cvar_s r_fullbrights;
573
574 extern struct cvar_s mod_noshader_default_offsetmapping;
575 extern struct cvar_s mod_q3shader_default_offsetmapping;
576 extern struct cvar_s mod_q3shader_default_offsetmapping_scale;
577 extern struct cvar_s mod_q3shader_default_offsetmapping_bias;
578 extern struct cvar_s mod_q3shader_default_polygonoffset;
579 extern struct cvar_s mod_q3shader_default_polygonfactor;
580 extern struct cvar_s mod_q3shader_default_refractive_index;
581 extern struct cvar_s mod_q3shader_force_addalpha;
582 extern struct cvar_s mod_q3shader_force_terrain_alphaflag;
583 extern struct cvar_s mod_q3bsp_lightgrid_texture;
584 extern struct cvar_s mod_q3bsp_lightgrid_world_surfaces;
585 extern struct cvar_s mod_q3bsp_lightgrid_bsp_surfaces;
586
587 void Mod_Init (void);
588 void Mod_Reload (void);
589 model_t *Mod_LoadModel(model_t *mod, qbool crash, qbool checkdisk);
590 model_t *Mod_FindName (const char *name, const char *parentname);
591 model_t *Mod_ForName (const char *name, qbool crash, qbool checkdisk, const char *parentname);
592 void Mod_UnloadModel (model_t *mod);
593
594 void Mod_ClearUsed(void);
595 void Mod_PurgeUnused(void);
596 void Mod_RemoveStaleWorldModels(model_t *skip); // only used during loading!
597
598 extern model_t *loadmodel;
599 extern char loadname[32];       // for hunk tags
600
601 int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements, int numvertices, int *remapvertices);
602 void Mod_BuildNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const int *elements, float *normal3f, qbool areaweighting);
603 void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const float *texcoord2f, const float *normal3f, const int *elements, float *svector3f, float *tvector3f, qbool areaweighting);
604
605 qbool Mod_ValidateElements(int *element3i, unsigned short *element3s, int numtriangles, int firstvertex, int numvertices, const char *filename, int fileline);
606 void Mod_AllocSurfMesh(struct mempool_s *mempool, int numvertices, int numtriangles, qbool lightmapoffsets, qbool vertexcolors);
607 void Mod_MakeSortedSurfaces(model_t *mod);
608
609 // called specially by brush model loaders before generating submodels
610 // automatically called after model loader returns
611 void Mod_BuildVBOs(void);
612
613 /// Sets the mod->DrawSky and mod->DrawAddWaterPlanes pointers conditionally based on whether surfaces in this submodel use these features
614 /// called specifically by brush model loaders when generating submodels
615 /// automatically called after model loader returns
616 void Mod_SetDrawSkyAndWater(model_t* mod);
617
618 shadowmesh_t *Mod_ShadowMesh_Alloc(struct mempool_s *mempool, int maxverts, int maxtriangles);
619 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, const float *vertex3f);
620 void Mod_ShadowMesh_AddMesh(shadowmesh_t *mesh, const float *vertex3f, int numtris, const int *element3i);
621 shadowmesh_t *Mod_ShadowMesh_Begin(struct mempool_s *mempool, int maxverts, int maxtriangles);
622 shadowmesh_t *Mod_ShadowMesh_Finish(shadowmesh_t *firstmesh, qbool createvbo);
623 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
624 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
625
626 void Mod_CreateCollisionMesh(model_t *mod);
627
628 void Mod_FreeQ3Shaders(void);
629 void Mod_LoadQ3Shaders(void);
630 shader_t *Mod_LookupQ3Shader(const char *name);
631 qbool Mod_LoadTextureFromQ3Shader(struct mempool_s *mempool, const char *modelname, texture_t *texture, const char *name, qbool warnmissing, qbool fallback, int defaulttexflags, int defaultmaterialflags);
632 texture_shaderpass_t *Mod_CreateShaderPass(struct mempool_s *mempool, struct skinframe_s *skinframe);
633 texture_shaderpass_t *Mod_CreateShaderPassFromQ3ShaderLayer(struct mempool_s *mempool, const char *modelname, q3shaderinfo_layer_t *layer, int layerindex, int texflags, const char *texturename);
634 /// Sets up a material to render the provided skinframe.  See also R_SkinFrame_LoadInternalBGRA.
635 void Mod_LoadCustomMaterial(struct mempool_s *mempool, texture_t *texture, const char *name, int supercontents, int materialflags, struct skinframe_s *skinframe);
636 /// Removes all shaderpasses from material, and optionally deletes the textures in the skinframes.
637 void Mod_UnloadCustomMaterial(texture_t *texture, qbool purgeskins);
638
639 extern struct cvar_s r_mipskins;
640 extern struct cvar_s r_mipnormalmaps;
641
642 typedef struct skinfileitem_s
643 {
644         struct skinfileitem_s *next;
645         char name[MAX_QPATH];
646         char replacement[MAX_QPATH];
647 }
648 skinfileitem_t;
649
650 typedef struct skinfile_s
651 {
652         struct skinfile_s *next;
653         skinfileitem_t *items;
654 }
655 skinfile_t;
656
657 skinfile_t *Mod_LoadSkinFiles(void);
658 void Mod_FreeSkinFiles(skinfile_t *skinfile);
659 int Mod_CountSkinFiles(skinfile_t *skinfile);
660 void Mod_BuildAliasSkinsFromSkinFiles(texture_t *skin, skinfile_t *skinfile, const char *meshname, const char *shadername);
661
662 void Mod_SnapVertices(int numcomponents, int numvertices, float *vertices, float snap);
663 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);
664 void Mod_VertexRangeFromElements(int numelements, const int *elements, int *firstvertexpointer, int *lastvertexpointer);
665
666 typedef struct mod_alloclightmap_row_s
667 {
668         int rowY;
669         int currentX;
670 }
671 mod_alloclightmap_row_t;
672
673 typedef struct mod_alloclightmap_state_s
674 {
675         int width;
676         int height;
677         int currentY;
678         mod_alloclightmap_row_t *rows;
679 }
680 mod_alloclightmap_state_t;
681
682 void Mod_AllocLightmap_Init(mod_alloclightmap_state_t *state, struct mempool_s *mempool, int width, int height);
683 void Mod_AllocLightmap_Free(mod_alloclightmap_state_t *state);
684 void Mod_AllocLightmap_Reset(mod_alloclightmap_state_t *state);
685 qbool Mod_AllocLightmap_Block(mod_alloclightmap_state_t *state, int blockwidth, int blockheight, int *outx, int *outy);
686
687 // bsp models
688 void Mod_BrushInit(void);
689 // used for talking to the QuakeC mainly
690 int Mod_Q1BSP_NativeContentsFromSuperContents(int supercontents);
691 int Mod_Q1BSP_SuperContentsFromNativeContents(int nativecontents);
692 // used for loading wal files in Mod_LoadTextureFromQ3Shader
693 int Mod_Q2BSP_SuperContentsFromNativeContents(int nativecontents);
694 int Mod_Q2BSP_NativeContentsFromSuperContents(int supercontents);
695
696 // a lot of model formats use the Q1BSP code, so here are the prototypes...
697 struct entity_render_s;
698 void R_Mod_DrawAddWaterPlanes(struct entity_render_s *ent);
699 void R_Mod_DrawSky(struct entity_render_s *ent);
700 void R_Mod_Draw(struct entity_render_s *ent);
701 void R_Mod_DrawDepth(struct entity_render_s *ent);
702 void R_Mod_DrawDebug(struct entity_render_s *ent);
703 void R_Mod_DrawPrepass(struct entity_render_s *ent);
704 void R_Mod_GetLightInfo(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outleaflist, unsigned char *outleafpvs, int *outnumleafspointer, int *outsurfacelist, unsigned char *outsurfacepvs, int *outnumsurfacespointer, unsigned char *outshadowtrispvs, unsigned char *outlighttrispvs, unsigned char *visitingleafpvs, int numfrustumplanes, const mplane_t *frustumplanes, qbool noocclusion);
705 void R_Mod_CompileShadowMap(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist);
706 void R_Mod_DrawShadowMap(int side, struct entity_render_s *ent, const vec3_t relativelightorigin, const vec3_t relativelightdirection, float lightradius, int modelnumsurfaces, const int *modelsurfacelist, const unsigned char *surfacesides, const vec3_t lightmins, const vec3_t lightmaxs);
707 void R_Mod_DrawLight(struct entity_render_s *ent, int numsurfaces, const int *surfacelist, const unsigned char *trispvs);
708
709 // dynamic mesh building (every frame) for debugging and other uses
710 void Mod_Mesh_Create(model_t *mod, const char *name);
711 void Mod_Mesh_Destroy(model_t *mod);
712 void Mod_Mesh_Reset(model_t *mod);
713 texture_t *Mod_Mesh_GetTexture(model_t *mod, const char *name, int defaultdrawflags, int defaulttexflags, int defaultmaterialflags);
714 msurface_t *Mod_Mesh_AddSurface(model_t *mod, texture_t *tex, qbool batchwithprevioussurface);
715 int Mod_Mesh_IndexForVertex(model_t *mod, msurface_t *surf, float x, float y, float z, float nx, float ny, float nz, float s, float t, float u, float v, float r, float g, float b, float a);
716 void Mod_Mesh_AddTriangle(model_t *mod, msurface_t *surf, int e0, int e1, int e2);
717 void Mod_Mesh_Validate(model_t *mod);
718 void Mod_Mesh_Finalize(model_t *mod);
719
720 // Collision optimization using Bounding Interval Hierarchy
721 void Mod_CollisionBIH_TracePoint(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
722 void Mod_CollisionBIH_TraceLine(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
723 void Mod_CollisionBIH_TraceBox(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
724 void Mod_CollisionBIH_TraceBrush(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, struct colbrushf_s *start, struct colbrushf_s *end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
725 void Mod_CollisionBIH_TracePoint_Mesh(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
726 qbool Mod_CollisionBIH_TraceLineOfSight(struct model_s *model, const vec3_t start, const vec3_t end, const vec3_t acceptmins, const vec3_t acceptmaxs);
727 int Mod_CollisionBIH_PointSuperContents(struct model_s *model, int frame, const vec3_t point);
728 int Mod_CollisionBIH_PointSuperContents_Mesh(struct model_s *model, int frame, const vec3_t point);
729 bih_t *Mod_MakeCollisionBIH(model_t *model, qbool userendersurfaces, bih_t *out);
730
731 // alias models
732 struct frameblend_s;
733 struct skeleton_s;
734 void Mod_AliasInit(void);
735 int Mod_Alias_GetTagMatrix(const model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, int tagindex, matrix4x4_t *outmatrix);
736 int Mod_Alias_GetTagIndexForName(const model_t *model, unsigned int skin, const char *tagname);
737 int Mod_Alias_GetExtendedTagInfoForIndex(const model_t *model, unsigned int skin, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, int tagindex, int *parentindex, const char **tagname, matrix4x4_t *tag_localmatrix);
738
739 void Mod_Skeletal_FreeBuffers(void);
740
741 // sprite models
742 void Mod_SpriteInit(void);
743
744 // loaders
745 void Mod_2PSB_Load(model_t *mod, void *buffer, void *bufferend);
746 void Mod_BSP2_Load(model_t *mod, void *buffer, void *bufferend);
747 void Mod_HLBSP_Load(model_t *mod, void *buffer, void *bufferend);
748 void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend);
749 void Mod_IBSP_Load(model_t *mod, void *buffer, void *bufferend);
750 void Mod_VBSP_Load(model_t *mod, void *buffer, void *bufferend);
751 void Mod_MAP_Load(model_t *mod, void *buffer, void *bufferend);
752 void Mod_OBJ_Load(model_t *mod, void *buffer, void *bufferend);
753 void Mod_IDP0_Load(model_t *mod, void *buffer, void *bufferend);
754 void Mod_IDP2_Load(model_t *mod, void *buffer, void *bufferend);
755 void Mod_IDP3_Load(model_t *mod, void *buffer, void *bufferend);
756 void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer, void *bufferend);
757 void Mod_DARKPLACESMODEL_Load(model_t *mod, void *buffer, void *bufferend);
758 void Mod_PSKMODEL_Load(model_t *mod, void *buffer, void *bufferend);
759 void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend);
760 void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend);
761 void Mod_INTERQUAKEMODEL_Load(model_t *mod, void *buffer, void *bufferend);
762
763 #endif  // MODEL_SHARED_H
764