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