]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.h
moved RecursiveLightPoint code to model_brush.c (model->brush.LightPoint), removing...
[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__
22 #define __MODEL__
23
24 #ifndef SYNCTYPE_T
25 #define SYNCTYPE_T
26 typedef enum {ST_SYNC=0, ST_RAND } synctype_t;
27 #endif
28
29 /*
30
31 d*_t structures are on-disk representations
32 m*_t structures are in-memory
33
34 */
35
36 typedef enum {mod_invalid, mod_brush, mod_sprite, mod_alias, mod_brushq2, mod_brushq3} modtype_t;
37
38 typedef struct animscene_s
39 {
40         char name[32]; // for viewthing support
41         int firstframe;
42         int framecount;
43         int loop; // true or false
44         float framerate;
45 }
46 animscene_t;
47
48 typedef struct skinframe_s
49 {
50         rtexture_t *base; // original texture without pants/shirt/glow
51         rtexture_t *pants; // pants only (in greyscale)
52         rtexture_t *shirt; // shirt only (in greyscale)
53         rtexture_t *glow; // glow only (fullbrights)
54         rtexture_t *merged; // original texture without glow
55         rtexture_t *fog; // alpha of the base texture (if not opaque)
56         rtexture_t *nmap; // normalmap (bumpmap for dot3)
57         rtexture_t *gloss; // glossmap (for dot3)
58         rtexture_t *detail; // detail texture (silly bumps for non-dot3)
59 }
60 skinframe_t;
61
62 #define MAX_SKINS 256
63
64 typedef struct overridetagname_s
65 {
66         char name[MAX_QPATH];
67 }
68 overridetagname_t;
69
70 // a replacement set of tag names, per skin
71 typedef struct overridetagnameset_s
72 {
73         int num_overridetagnames;
74         overridetagname_t *data_overridetagnames;
75 }
76 overridetagnameset_t;
77
78 #define SHADOWMESHVERTEXHASH 1024
79 typedef struct shadowmeshvertexhash_s
80 {
81         struct shadowmeshvertexhash_s *next;
82 }
83 shadowmeshvertexhash_t;
84
85 typedef struct shadowmesh_s
86 {
87         struct shadowmesh_s *next;
88         int numverts, maxverts;
89         int numtriangles, maxtriangles;
90         float *vertex3f;
91         int *element3i;
92         int *neighbor3i;
93         // these are NULL after Mod_ShadowMesh_Finish is performed, only used
94         // while building meshes
95         shadowmeshvertexhash_t **vertexhashtable, *vertexhashentries;
96 }
97 shadowmesh_t;
98
99
100 #include "matrixlib.h"
101
102 #include "model_brush.h"
103 #include "model_sprite.h"
104 #include "model_alias.h"
105
106 typedef struct model_alias_s
107 {
108         // LordHavoc: Q2/ZYM model support
109         int                             aliastype;
110
111         // mdl/md2/md3 models are the same after loading
112         int                             aliasnum_meshes;
113         aliasmesh_t             *aliasdata_meshes;
114
115         int                             aliasnum_tags;
116         int                             aliasnum_tagframes;
117         aliastag_t              *aliasdata_tags;
118
119         // for Zymotic models
120         int                             zymnum_verts;
121         int                             zymnum_tris;
122         int                             zymnum_shaders;
123         int                             zymnum_bones;
124         int                             zymnum_scenes;
125         float                   *zymdata_texcoords;
126         rtexture_t              **zymdata_textures;
127         qbyte                   *zymdata_trizone;
128         zymbone_t               *zymdata_bones;
129         unsigned int    *zymdata_vertbonecounts;
130         zymvertex_t             *zymdata_verts;
131         unsigned int    *zymdata_renderlist;
132         float                   *zymdata_poses;
133 }
134 model_alias_t;
135
136 typedef struct model_sprite_s
137 {
138         int                             sprnum_type;
139         mspriteframe_t  *sprdata_frames;
140 }
141 model_sprite_t;
142
143 struct trace_s;
144
145 typedef struct model_brush_s
146 {
147         char *entities;
148         void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
149         void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
150         void (*TraceBox)(struct model_s *model, struct trace_s *trace, const vec3_t boxstartmins, const vec3_t boxstartmaxs, const vec3_t boxendmins, const vec3_t boxendmaxs);
151 }
152 model_brush_t;
153
154 typedef struct model_brushq1_s
155 {
156         // true if this model is a HalfLife .bsp file
157         qboolean                ishlbsp;
158
159         int                             firstmodelsurface, nummodelsurfaces;
160
161         // lightmap format, set to r_lightmaprgba when model is loaded
162         int                             lightmaprgba;
163
164         int                             numsubmodels;
165         dmodel_t                *submodels;
166
167         int                             numplanes;
168         mplane_t                *planes;
169
170         // number of visible leafs, not counting 0 (solid)
171         int                             numleafs;
172         mleaf_t                 *leafs;
173
174         int                             numvertexes;
175         mvertex_t               *vertexes;
176
177         int                             numedges;
178         medge_t                 *edges;
179
180         int                             numnodes;
181         mnode_t                 *nodes;
182
183         int                             numtexinfo;
184         mtexinfo_t              *texinfo;
185
186         int                             numsurfaces;
187         msurface_t              *surfaces;
188         int                             *surfacevisframes;
189         int                             *surfacepvsframes;
190         msurface_t              *surfacepvsnext;
191         surfmesh_t              *entiremesh;
192         surfmesh_t              *surfmeshes;
193
194         int                             numsurfedges;
195         int                             *surfedges;
196
197         int                             numclipnodes;
198         dclipnode_t             *clipnodes;
199
200         int                             nummarksurfaces;
201         int                             *marksurfaces;
202
203         hull_t                  hulls[MAX_MAP_HULLS];
204
205         int                             numtextures;
206         texture_t               *textures;
207
208         qbyte                   *visdata;
209         qbyte                   *lightdata;
210
211         int                             numportals;
212         mportal_t               *portals;
213
214         int                             numportalpoints;
215         mvertex_t               *portalpoints;
216
217         int                             numlights;
218         mlight_t                *lights;
219
220         // pvs visibility marking
221         mleaf_t                 *pvsviewleaf;
222         int                             pvsviewleafnovis;
223         int                             pvsframecount;
224         mleaf_t                 *pvsleafchain;
225         int                             *pvssurflist;
226         int                             pvssurflistlength;
227         // these get rebuilt as the player moves around if this is the world,
228         // otherwise they are left alone (no pvs for bmodels)
229         msurface_t              ***pvstexturechains;
230         msurface_t              **pvstexturechainsbuffer;
231         int                             *pvstexturechainslength;
232
233         // lightmap update chains for light styles
234         int                             light_styles;
235         qbyte                   *light_style;
236         int                             *light_stylevalue;
237         msurface_t              ***light_styleupdatechains;
238         msurface_t              **light_styleupdatechainsbuffer;
239         int                             light_scalebit;
240         float                   light_ambient;
241
242         mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
243         qbyte *(*LeafPVS)(struct model_s *model, mleaf_t *leaf);
244         void (*BuildPVSTextureChains)(struct model_s *model);
245 }
246 model_brushq1_t;
247
248 /* MSVC can't compile empty structs, so this is commented out for now
249 typedef struct model_brushq2_s
250 {
251 }
252 model_brushq2_t;
253 */
254
255 typedef struct q3mtexture_s
256 {
257         char name[Q3PATHLENGTH];
258         int surfaceflags;
259         int contents;
260
261         int number;
262         skinframe_t skin;
263 }
264 q3mtexture_t;
265
266 typedef struct q3mnode_s
267 {
268         int isnode; // true
269         struct q3mnode_s *parent;
270         struct mplane_s *plane;
271         struct q3mnode_s *children[2];
272 }
273 q3mnode_t;
274
275 typedef struct q3mleaf_s
276 {
277         int isnode; // false
278         struct q3mnode_s *parent;
279         int clusterindex;
280         int areaindex;
281         int numleaffaces;
282         struct q3mface_s **firstleafface;
283         int numleafbrushes;
284         struct q3mbrush_s **firstleafbrush;
285         vec3_t mins;
286         vec3_t maxs;
287 }
288 q3mleaf_t;
289
290 typedef struct q3mmodel_s
291 {
292         vec3_t mins;
293         vec3_t maxs;
294         int numfaces;
295         struct q3mface_s *firstface;
296         int numbrushes;
297         struct q3mbrush_s *firstbrush;
298 }
299 q3mmodel_t;
300
301 typedef struct q3mbrush_s
302 {
303         struct colbrushf_s *colbrushf;
304         int numbrushsides;
305         struct q3mbrushside_s *firstbrushside;
306         struct q3mtexture_s *texture;
307 }
308 q3mbrush_t;
309
310 typedef struct q3mbrushside_s
311 {
312         struct mplane_s *plane;
313         struct q3mtexture_s *texture;
314 }
315 q3mbrushside_t;
316
317 typedef struct q3meffect_s
318 {
319         char shadername[Q3PATHLENGTH];
320         struct q3mbrush_s *brush;
321         int unknown; // 5 or -1
322 }
323 q3meffect_t;
324
325 typedef struct q3mface_s
326 {
327         struct q3mtexture_s *texture;
328         struct q3meffect_s *effect;
329         rtexture_t *lightmaptexture;
330         int type;
331         int firstvertex;
332         int numvertices;
333         int firstelement;
334         int numelements;
335         int patchsize[2];
336
337         float *data_vertex3f;
338         float *data_texturetexcoord2f;
339         float *data_lightmaptexcoord2f;
340         float *data_svector3f;
341         float *data_tvector3f;
342         float *data_normal3f;
343         float *data_color4f;
344         int numtriangles;
345         int *data_element3i;
346         int *data_neighbor3i;
347 }
348 q3mface_t;
349
350 typedef struct model_brushq3_s
351 {
352         int num_textures;
353         q3mtexture_t *data_textures;
354
355         int num_planes;
356         mplane_t *data_planes;
357
358         int num_nodes;
359         q3mnode_t *data_nodes;
360
361         int num_leafs;
362         q3mleaf_t *data_leafs;
363
364         int num_leafbrushes;
365         q3mbrush_t **data_leafbrushes;
366
367         int num_leaffaces;
368         q3mface_t **data_leaffaces;
369
370         int num_models;
371         q3mmodel_t *data_models;
372         // each submodel gets its own model struct so this is different for each.
373         q3mmodel_t data_thismodel;
374
375         int num_brushes;
376         q3mbrush_t *data_brushes;
377
378         int num_brushsides;
379         q3mbrushside_t *data_brushsides;
380
381         int num_vertices;
382         float *data_vertex3f;
383         float *data_texturetexcoord2f;
384         float *data_lightmaptexcoord2f;
385         float *data_svector3f;
386         float *data_tvector3f;
387         float *data_normal3f;
388         float *data_color4f;
389
390         int num_triangles;
391         int *data_element3i;
392         int *data_neighbor3i;
393
394         int num_effects;
395         q3meffect_t *data_effects;
396
397         int num_faces;
398         q3mface_t *data_faces;
399
400         // lightmap textures
401         int num_lightmaps;
402         rtexture_t **data_lightmaps;
403
404         // voxel light data with directional shading
405         int num_lightgrid;
406         q3dlightgrid_t *data_lightgrid;
407         // size of each cell (may vary by map, typically 64 64 128)
408         float num_lightgrid_cellsize[3];
409         // 1.0 / num_lightgrid_cellsize
410         float num_lightgrid_scale[3];
411         // dimensions of the world model in lightgrid cells
412         int num_lightgrid_imins[3];
413         int num_lightgrid_imaxs[3];
414         int num_lightgrid_isize[3];
415         // indexing/clamping
416         int num_lightgrid_dimensions[3];
417         // transform modelspace coordinates to lightgrid index
418         matrix4x4_t num_lightgrid_indexfromworld;
419
420         // pvs
421         int num_pvsclusters;
422         int num_pvschainlength;
423         unsigned char *data_pvschains;
424         // example
425         //pvschain = model->brushq3.data_pvschains + mycluster * model->brushq3.num_pvschainlength;
426         //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
427 }
428 model_brushq3_t;
429
430 typedef struct model_s
431 {
432         // name and path of model, for example "progs/player.mdl"
433         char                    name[MAX_QPATH];
434         // model needs to be loaded if this is true
435         qboolean                needload;
436         // set if the model is used in current map, models which are not, are purged
437         qboolean                used;
438         // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
439         qboolean                isworldmodel;
440         // CRC of the file this model was loaded from, to reload if changed
441         unsigned int    crc;
442         // mod_brush, mod_alias, mod_sprite
443         modtype_t               type;
444         // memory pool for allocations
445         mempool_t               *mempool;
446         // all models use textures...
447         rtexturepool_t  *texturepool;
448         // flags from the model file
449         int                             flags;
450         // engine calculated flags, ones that can not be set in the file
451         int                             flags2;
452         // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
453         int                             fullbright;
454         // number of QC accessible frame(group)s in the model
455         int                             numframes;
456         // number of QC accessible skin(group)s in the model
457         int                             numskins;
458         // whether to randomize animated framegroups
459         synctype_t              synctype;
460         // bounding box at angles '0 0 0'
461         vec3_t                  normalmins, normalmaxs;
462         // bounding box if yaw angle is not 0, but pitch and roll are
463         vec3_t                  yawmins, yawmaxs;
464         // bounding box if pitch or roll are used
465         vec3_t                  rotatedmins, rotatedmaxs;
466         // sphere radius, usable at any angles
467         float                   radius;
468         // squared sphere radius for easier comparisons
469         float                   radius2;
470         // skin animation info
471         animscene_t             *skinscenes; // [numskins]
472         // skin animation info
473         animscene_t             *animscenes; // [numframes]
474         // draw the model's sky polygons (only used by brush models)
475         void(*DrawSky)(struct entity_render_s *ent);
476         // draw the model using lightmap/dlight shading
477         void(*Draw)(struct entity_render_s *ent);
478         // draw a fake shadow for the model
479         void(*DrawFakeShadow)(struct entity_render_s *ent);
480         // draw a shadow volume for the model based on light source
481         void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
482         // draw the lighting on a model (through stencil)
483         void(*DrawLight)(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz);
484         // fields belonging to each type of model
485         model_alias_t   alias;
486         model_sprite_t  sprite;
487         model_brush_t   brush;
488         model_brushq1_t brushq1;
489         /* MSVC can't handle an empty struct, so this is commented out for now
490         model_brushq2_t brushq2;
491         */
492         model_brushq3_t brushq3;
493         // skin files can have different tags for each skin
494         overridetagnameset_t    *data_overridetagnamesforskin;
495 }
496 model_t;
497
498 //============================================================================
499
500 // this can be used for anything without a valid texture
501 extern rtexture_t *r_notexture;
502 #define NUM_DETAILTEXTURES 1
503 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
504 // every texture must be in a pool...
505 extern rtexturepool_t *mod_shared_texturepool;
506
507 // model loading
508 extern model_t *loadmodel;
509 extern qbyte *mod_base;
510 // sky/water subdivision
511 //extern cvar_t gl_subdivide_size;
512 // texture fullbrights
513 extern cvar_t r_fullbrights;
514
515 void Mod_Init (void);
516 void Mod_CheckLoaded (model_t *mod);
517 void Mod_ClearAll (void);
518 model_t *Mod_FindName (const char *name);
519 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
520 void Mod_TouchModel (const char *name);
521 void Mod_UnloadModel (model_t *mod);
522
523 void Mod_ClearUsed(void);
524 void Mod_PurgeUnused(void);
525 void Mod_LoadModels(void);
526
527 extern model_t *loadmodel;
528 extern char loadname[32];       // for hunk tags
529
530 int Mod_FindTriangleWithEdge(const int *elements, int numtriangles, int start, int end, int ignore);
531 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
532 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
533 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
534
535 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts);
536 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh);
537 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *v);
538 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, float *vert0, float *vert1, float *vert2);
539 void Mod_ShadowMesh_AddPolygon(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts);
540 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, float *verts, int numtris, int *elements);
541 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int initialnumtriangles);
542 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh);
543 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
544 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
545
546 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
547 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
548
549 extern cvar_t r_mipskins;
550
551 typedef struct skinfileitem_s
552 {
553         struct skinfileitem_s *next;
554         char name[MAX_QPATH];
555         char replacement[MAX_QPATH];
556 }
557 skinfileitem_t;
558
559 typedef struct skinfile_s
560 {
561         struct skinfile_s *next;
562         skinfileitem_t *items;
563 }
564 skinfile_t;
565
566 skinfile_t *Mod_LoadSkinFiles(void);
567 void Mod_FreeSkinFiles(skinfile_t *skinfile);
568 int Mod_CountSkinFiles(skinfile_t *skinfile);
569
570 #endif  // __MODEL__
571