]> git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.h
build: minor adjustments
[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 msurface_t;
409
410 #include "bih.h"
411
412 #include "model_brush.h"
413 #include "model_q1bsp.h"
414 #include "model_q2bsp.h"
415 #include "model_q3bsp.h"
416 #include "model_vbsp.h"
417 #include "model_sprite.h"
418 #include "model_alias.h"
419
420 struct trace_s;
421
422 struct frameblend_s;
423 struct skeleton_s;
424
425 typedef struct model_s
426 {
427         // name and path of model, for example "progs/player.mdl"
428         char                    name[MAX_QPATH];
429         // model needs to be loaded if this is false
430         qbool           loaded;
431         // set if the model is used in current map, models which are not, are purged
432         qbool           used;
433         // CRC of the file this model was loaded from, to reload if changed
434         unsigned int    crc;
435         // mod_brush, mod_alias, mod_sprite
436         modtype_t               type;
437         // memory pool for allocations
438         struct mempool_s                *mempool;
439         // all models use textures...
440         rtexturepool_t  *texturepool;
441         // EF_* flags (translated from the model file's different flags layout)
442         int                             effects;
443         // number of QC accessible frame(group)s in the model
444         int                             numframes;
445         // number of QC accessible skin(group)s in the model
446         int                             numskins;
447         // whether to randomize animated framegroups
448         synctype_t              synctype;
449         // bounding box at angles '0 0 0'
450         vec3_t                  normalmins, normalmaxs;
451         // bounding box if yaw angle is not 0, but pitch and roll are
452         vec3_t                  yawmins, yawmaxs;
453         // bounding box if pitch or roll are used
454         vec3_t                  rotatedmins, rotatedmaxs;
455         // sphere radius, usable at any angles
456         float                   radius;
457         // squared sphere radius for easier comparisons
458         float                   radius2;
459         // skin animation info
460         animscene_t             *skinscenes; // [numskins]
461         // skin animation info
462         animscene_t             *animscenes; // [numframes]
463         // range of surface numbers in this model
464         int                             submodelsurfaces_start;
465         int                             submodelsurfaces_end;
466         /// surface indices of model in an optimal draw order (submodelindex -> texture -> lightmap -> index)
467         int                             *modelsurfaces_sorted; // same size as num_surfaces
468         // range of collision brush numbers in this (sub)model
469         int                             firstmodelbrush;
470         int                             nummodelbrushes;
471         // BIH (Bounding Interval Hierarchy) for this (sub)model
472         bih_t                   collision_bih;
473         bih_t                   render_bih; // if not set, use collision_bih instead for rendering purposes too
474         // for md3 models
475         int                             num_tags;
476         int                             num_tagframes;
477         aliastag_t              *data_tags;
478         // for skeletal models
479         int                             num_bones;
480         aliasbone_t             *data_bones;
481         float                   num_posescale; // scaling factor from origin in poses7s format (includes divide by 32767)
482         float                   num_poseinvscale; // scaling factor to origin in poses7s format (includes multiply by 32767)
483         int                             num_poses;
484         short                   *data_poses7s; // origin xyz, quat xyzw, unit length, values normalized to +/-32767 range
485         float                   *data_baseboneposeinverse;
486         // textures of this model
487         int                             num_textures;
488         int                             max_textures; // preallocated for expansion (Mod_Mesh_*)
489         int                             num_texturesperskin;
490         texture_t               *data_textures;
491         qbool           wantnormals;
492         qbool           wanttangents;
493         // surfaces of this model
494         int                             num_surfaces;
495         int                             max_surfaces; // preallocated for expansion (Mod_Mesh_*)
496         msurface_t              *data_surfaces;
497         // optional lightmapinfo data for surface lightmap updates
498         msurface_lightmapinfo_t *data_surfaces_lightmapinfo;
499         // all surfaces belong to this mesh
500         surfmesh_t              surfmesh;
501         // data type of model
502         const char              *modeldatatypestring;
503         // generates vertex data for a given frameblend
504         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);
505         // draw the model's sky polygons
506         void(*DrawSky)(struct entity_render_s *ent);
507         // draw refraction/reflection textures for the model's water polygons
508         void(*DrawAddWaterPlanes)(struct entity_render_s *ent);
509         // draw the model using lightmap/dlight shading
510         void(*Draw)(struct entity_render_s *ent);
511         // draw the model to the depth buffer (no color rendering at all)
512         void(*DrawDepth)(struct entity_render_s *ent);
513         // draw any enabled debugging effects on this model (such as showing triangles, normals, collision brushes...)
514         void(*DrawDebug)(struct entity_render_s *ent);
515         // draw geometry textures for deferred rendering
516         void(*DrawPrepass)(struct entity_render_s *ent);
517     // compile an optimized shadowmap mesh for the model based on light source
518         void(*CompileShadowMap)(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist);
519         // draw depth into a shadowmap
520         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);
521         // gathers info on which clusters and surfaces are lit by light, as well as calculating a bounding box
522         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);
523         // draw the lighting on a model (through stencil)
524         void(*DrawLight)(struct entity_render_s *ent, int numsurfaces, const int *surfacelist, const unsigned char *trispvs);
525         // trace a box against this model
526         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);
527         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);
528         // trace a box against this model
529         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);
530         // trace a point against this model (like PointSuperContents)
531         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);
532         // find the supercontents value at a point in this model
533         int (*PointSuperContents)(struct model_s *model, int frame, const vec3_t point);
534         // trace a line against geometry in this model and report correct texture (used by r_shadow_bouncegrid)
535         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);
536         // fields belonging to some types of model
537         model_sprite_t  sprite;
538         model_brush_t   brush;
539         model_brushq1_t brushq1;
540         model_brushq3_t brushq3;
541         // flags this model for offseting sounds to the model center (used by brush models)
542         int soundfromcenter;
543
544         // if set, the model contains light information (lightmap, or vertexlight)
545         qbool lit;
546         float lightmapscale;
547
548         qbool nolerp;
549 }
550 model_t;
551
552 //============================================================================
553
554 // model loading
555 extern model_t *loadmodel;
556 extern unsigned char *mod_base;
557
558 typedef struct modloader_s
559 {
560         const char *extension;
561         const char *header;
562         size_t headersize; // The header might not be NULL terminated
563         void (*Load)(model_t *, void *, void *);
564 } modloader_t;
565
566 // sky/water subdivision
567 //extern struct cvar_s gl_subdivide_size;
568 // texture fullbrights
569 extern struct cvar_s r_fullbrights;
570
571 extern struct cvar_s mod_noshader_default_offsetmapping;
572 extern struct cvar_s mod_q3shader_default_offsetmapping;
573 extern struct cvar_s mod_q3shader_default_offsetmapping_scale;
574 extern struct cvar_s mod_q3shader_default_offsetmapping_bias;
575 extern struct cvar_s mod_q3shader_default_polygonoffset;
576 extern struct cvar_s mod_q3shader_default_polygonfactor;
577 extern struct cvar_s mod_q3shader_default_refractive_index;
578 extern struct cvar_s mod_q3shader_force_addalpha;
579 extern struct cvar_s mod_q3shader_force_terrain_alphaflag;
580 extern struct cvar_s mod_q3bsp_lightgrid_texture;
581 extern struct cvar_s mod_q3bsp_lightgrid_world_surfaces;
582 extern struct cvar_s mod_q3bsp_lightgrid_bsp_surfaces;
583
584 void Mod_Init (void);
585 void Mod_Reload (void);
586 model_t *Mod_LoadModel(model_t *mod, qbool crash, qbool checkdisk);
587 model_t *Mod_FindName (const char *name, const char *parentname);
588 model_t *Mod_ForName (const char *name, qbool crash, qbool checkdisk, const char *parentname);
589 void Mod_UnloadModel (model_t *mod);
590
591 void Mod_ClearUsed(void);
592 void Mod_PurgeUnused(void);
593 void Mod_RemoveStaleWorldModels(model_t *skip); // only used during loading!
594
595 extern model_t *loadmodel;
596 extern char loadname[32];       // for hunk tags
597
598 int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements, int numvertices, int *remapvertices);
599 void Mod_BuildNormals(int firstvertex, int numvertices, int numtriangles, const float *vertex3f, const int *elements, float *normal3f, qbool areaweighting);
600 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);
601
602 qbool Mod_ValidateElements(int *element3i, unsigned short *element3s, int numtriangles, int firstvertex, int numvertices, const char *filename, int fileline);
603 void Mod_AllocSurfMesh(struct mempool_s *mempool, int numvertices, int numtriangles, qbool lightmapoffsets, qbool vertexcolors);
604 void Mod_MakeSortedSurfaces(model_t *mod);
605
606 // called specially by brush model loaders before generating submodels
607 // automatically called after model loader returns
608 void Mod_BuildVBOs(void);
609
610 /// Sets the mod->DrawSky and mod->DrawAddWaterPlanes pointers conditionally based on whether surfaces in this submodel use these features
611 /// called specifically by brush model loaders when generating submodels
612 /// automatically called after model loader returns
613 void Mod_SetDrawSkyAndWater(model_t* mod);
614
615 shadowmesh_t *Mod_ShadowMesh_Alloc(struct mempool_s *mempool, int maxverts, int maxtriangles);
616 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, const float *vertex3f);
617 void Mod_ShadowMesh_AddMesh(shadowmesh_t *mesh, const float *vertex3f, int numtris, const int *element3i);
618 shadowmesh_t *Mod_ShadowMesh_Begin(struct mempool_s *mempool, int maxverts, int maxtriangles);
619 shadowmesh_t *Mod_ShadowMesh_Finish(shadowmesh_t *firstmesh, qbool createvbo);
620 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
621 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
622
623 void Mod_CreateCollisionMesh(model_t *mod);
624
625 void Mod_FreeQ3Shaders(void);
626 void Mod_LoadQ3Shaders(void);
627 shader_t *Mod_LookupQ3Shader(const char *name);
628 qbool Mod_LoadTextureFromQ3Shader(struct mempool_s *mempool, const char *modelname, texture_t *texture, const char *name, qbool warnmissing, qbool fallback, int defaulttexflags, int defaultmaterialflags);
629 texture_shaderpass_t *Mod_CreateShaderPass(struct mempool_s *mempool, struct skinframe_s *skinframe);
630 texture_shaderpass_t *Mod_CreateShaderPassFromQ3ShaderLayer(struct mempool_s *mempool, const char *modelname, q3shaderinfo_layer_t *layer, int layerindex, int texflags, const char *texturename);
631 /// Sets up a material to render the provided skinframe.  See also R_SkinFrame_LoadInternalBGRA.
632 void Mod_LoadCustomMaterial(struct mempool_s *mempool, texture_t *texture, const char *name, int supercontents, int materialflags, struct skinframe_s *skinframe);
633 /// Removes all shaderpasses from material, and optionally deletes the textures in the skinframes.
634 void Mod_UnloadCustomMaterial(texture_t *texture, qbool purgeskins);
635
636 extern struct cvar_s r_mipskins;
637 extern struct cvar_s r_mipnormalmaps;
638
639 typedef struct skinfileitem_s
640 {
641         struct skinfileitem_s *next;
642         char name[MAX_QPATH];
643         char replacement[MAX_QPATH];
644 }
645 skinfileitem_t;
646
647 typedef struct skinfile_s
648 {
649         struct skinfile_s *next;
650         skinfileitem_t *items;
651 }
652 skinfile_t;
653
654 skinfile_t *Mod_LoadSkinFiles(void);
655 void Mod_FreeSkinFiles(skinfile_t *skinfile);
656 int Mod_CountSkinFiles(skinfile_t *skinfile);
657 void Mod_BuildAliasSkinsFromSkinFiles(texture_t *skin, skinfile_t *skinfile, const char *meshname, const char *shadername);
658
659 void Mod_SnapVertices(int numcomponents, int numvertices, float *vertices, float snap);
660 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);
661 void Mod_VertexRangeFromElements(int numelements, const int *elements, int *firstvertexpointer, int *lastvertexpointer);
662
663 typedef struct mod_alloclightmap_row_s
664 {
665         int rowY;
666         int currentX;
667 }
668 mod_alloclightmap_row_t;
669
670 typedef struct mod_alloclightmap_state_s
671 {
672         int width;
673         int height;
674         int currentY;
675         mod_alloclightmap_row_t *rows;
676 }
677 mod_alloclightmap_state_t;
678
679 void Mod_AllocLightmap_Init(mod_alloclightmap_state_t *state, struct mempool_s *mempool, int width, int height);
680 void Mod_AllocLightmap_Free(mod_alloclightmap_state_t *state);
681 void Mod_AllocLightmap_Reset(mod_alloclightmap_state_t *state);
682 qbool Mod_AllocLightmap_Block(mod_alloclightmap_state_t *state, int blockwidth, int blockheight, int *outx, int *outy);
683
684 // bsp models
685 void Mod_BrushInit(void);
686 // used for talking to the QuakeC mainly
687 int Mod_Q1BSP_NativeContentsFromSuperContents(int supercontents);
688 int Mod_Q1BSP_SuperContentsFromNativeContents(int nativecontents);
689 // used for loading wal files in Mod_LoadTextureFromQ3Shader
690 int Mod_Q2BSP_SuperContentsFromNativeContents(int nativecontents);
691 int Mod_Q2BSP_NativeContentsFromSuperContents(int supercontents);
692
693 // a lot of model formats use the Q1BSP code, so here are the prototypes...
694 struct entity_render_s;
695 void R_Mod_DrawAddWaterPlanes(struct entity_render_s *ent);
696 void R_Mod_DrawSky(struct entity_render_s *ent);
697 void R_Mod_Draw(struct entity_render_s *ent);
698 void R_Mod_DrawDepth(struct entity_render_s *ent);
699 void R_Mod_DrawDebug(struct entity_render_s *ent);
700 void R_Mod_DrawPrepass(struct entity_render_s *ent);
701 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);
702 void R_Mod_CompileShadowMap(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist);
703 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);
704 void R_Mod_DrawLight(struct entity_render_s *ent, int numsurfaces, const int *surfacelist, const unsigned char *trispvs);
705
706 // dynamic mesh building (every frame) for debugging and other uses
707 void Mod_Mesh_Create(model_t *mod, const char *name);
708 void Mod_Mesh_Destroy(model_t *mod);
709 void Mod_Mesh_Reset(model_t *mod);
710 texture_t *Mod_Mesh_GetTexture(model_t *mod, const char *name, int defaultdrawflags, int defaulttexflags, int defaultmaterialflags);
711 msurface_t *Mod_Mesh_AddSurface(model_t *mod, texture_t *tex, qbool batchwithprevioussurface);
712 void Mod_Mesh_CheckResize_Vertex(model_t *mod, msurface_t *surf);
713 int Mod_Mesh_AddVertex(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);
714 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);
715 void Mod_Mesh_AddTriangle(model_t *mod, msurface_t *surf, int e0, int e1, int e2);
716 void Mod_Mesh_Validate(model_t *mod);
717 void Mod_Mesh_Finalize(model_t *mod);
718
719 // Collision optimization using Bounding Interval Hierarchy
720 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);
721 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);
722 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);
723 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);
724 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);
725 qbool Mod_CollisionBIH_TraceLineOfSight(struct model_s *model, const vec3_t start, const vec3_t end, const vec3_t acceptmins, const vec3_t acceptmaxs);
726 int Mod_CollisionBIH_PointSuperContents(struct model_s *model, int frame, const vec3_t point);
727 int Mod_CollisionBIH_PointSuperContents_Mesh(struct model_s *model, int frame, const vec3_t point);
728 bih_t *Mod_MakeCollisionBIH(model_t *model, qbool userendersurfaces, bih_t *out);
729
730 // alias models
731 struct frameblend_s;
732 struct skeleton_s;
733 void Mod_AliasInit(void);
734 int Mod_Alias_GetTagMatrix(const model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, int tagindex, matrix4x4_t *outmatrix);
735 int Mod_Alias_GetTagIndexForName(const model_t *model, unsigned int skin, const char *tagname);
736 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);
737
738 void Mod_Skeletal_FreeBuffers(void);
739
740 // sprite models
741 void Mod_SpriteInit(void);
742
743 // loaders
744 void Mod_2PSB_Load(model_t *mod, void *buffer, void *bufferend);
745 void Mod_BSP2_Load(model_t *mod, void *buffer, void *bufferend);
746 void Mod_HLBSP_Load(model_t *mod, void *buffer, void *bufferend);
747 void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend);
748 void Mod_IBSP_Load(model_t *mod, void *buffer, void *bufferend);
749 void Mod_VBSP_Load(model_t *mod, void *buffer, void *bufferend);
750 void Mod_MAP_Load(model_t *mod, void *buffer, void *bufferend);
751 void Mod_OBJ_Load(model_t *mod, void *buffer, void *bufferend);
752 void Mod_IDP0_Load(model_t *mod, void *buffer, void *bufferend);
753 void Mod_IDP2_Load(model_t *mod, void *buffer, void *bufferend);
754 void Mod_IDP3_Load(model_t *mod, void *buffer, void *bufferend);
755 void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer, void *bufferend);
756 void Mod_DARKPLACESMODEL_Load(model_t *mod, void *buffer, void *bufferend);
757 void Mod_PSKMODEL_Load(model_t *mod, void *buffer, void *bufferend);
758 void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend);
759 void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend);
760 void Mod_INTERQUAKEMODEL_Load(model_t *mod, void *buffer, void *bufferend);
761
762 #endif  // MODEL_SHARED_H
763