]> icculus.org git repositories - divverent/darkplaces.git/blob - model_shared.h
now supports skyboxes in q3bsp (note: must use the "sky" key in worldspawn to set...
[divverent/darkplaces.git] / model_shared.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #ifndef __MODEL__
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         // true if this model is a HalfLife .bsp file
148         qboolean ishlbsp;
149         // string of entity definitions (.map format)
150         char *entities;
151         // number of submodels in this map (just used by server to know how many
152         // submodels to load)
153         int numsubmodels;
154         // common functions
155         int (*SuperContentsFromNativeContents)(struct model_s *model, int nativecontents);
156         int (*NativeContentsFromSuperContents)(struct model_s *model, int supercontents);
157         qbyte *(*GetPVS)(struct model_s *model, const vec3_t p);
158         int (*FatPVS)(struct model_s *model, const vec3_t org, vec_t radius, qbyte *pvsbuffer, int pvsbufferlength);
159         int (*BoxTouchingPVS)(struct model_s *model, const qbyte *pvs, const vec3_t mins, const vec3_t maxs);
160         void (*LightPoint)(struct model_s *model, const vec3_t p, vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffusenormal);
161         void (*FindNonSolidLocation)(struct model_s *model, const vec3_t in, vec3_t out, vec_t radius);
162         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, int hitsupercontentsmask);
163         // these are actually only found on brushq1, but NULL is handled gracefully
164         void (*AmbientSoundLevelsForPoint)(struct model_s *model, const vec3_t p, qbyte *out, int outsize);
165         void (*RoundUpToHullSize)(struct model_s *cmodel, const vec3_t inmins, const vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs);
166 }
167 model_brush_t;
168
169 typedef struct model_brushq1_s
170 {
171         int                             firstmodelsurface, nummodelsurfaces;
172
173         // lightmap format, set to r_lightmaprgba when model is loaded
174         int                             lightmaprgba;
175
176         dmodel_t                *submodels;
177
178         int                             numplanes;
179         mplane_t                *planes;
180
181         // number of actual leafs (including 0 which is solid)
182         int                             numleafs;
183         // visible leafs, not counting 0 (solid)
184         int                             visleafs;
185         mleaf_t                 *leafs;
186
187         int                             numvertexes;
188         mvertex_t               *vertexes;
189
190         int                             numedges;
191         medge_t                 *edges;
192
193         int                             numnodes;
194         mnode_t                 *nodes;
195
196         int                             numtexinfo;
197         mtexinfo_t              *texinfo;
198
199         int                             numsurfaces;
200         msurface_t              *surfaces;
201         int                             *surfacevisframes;
202         int                             *surfacepvsframes;
203         msurface_t              *surfacepvsnext;
204         surfmesh_t              *entiremesh;
205         surfmesh_t              *surfmeshes;
206
207         int                             numsurfedges;
208         int                             *surfedges;
209
210         int                             numclipnodes;
211         dclipnode_t             *clipnodes;
212
213         int                             nummarksurfaces;
214         int                             *marksurfaces;
215
216         hull_t                  hulls[MAX_MAP_HULLS];
217
218         int                             numtextures;
219         texture_t               *textures;
220
221         int                             num_compressedpvs;
222         qbyte                   *data_compressedpvs;
223         qbyte                   *data_decompressedpvs;
224
225         int                             num_lightdata;
226         qbyte                   *lightdata;
227
228         int                             numportals;
229         mportal_t               *portals;
230
231         int                             numportalpoints;
232         mvertex_t               *portalpoints;
233
234         int                             numlights;
235         mlight_t                *lights;
236
237         // pvs visibility marking
238         mleaf_t                 *pvsviewleaf;
239         int                             pvsviewleafnovis;
240         int                             pvsframecount;
241         mleaf_t                 *pvsleafchain;
242         int                             *pvssurflist;
243         int                             pvssurflistlength;
244         // these get rebuilt as the player moves around if this is the world,
245         // otherwise they are left alone (no pvs for bmodels)
246         msurface_t              ***pvstexturechains;
247         msurface_t              **pvstexturechainsbuffer;
248         int                             *pvstexturechainslength;
249
250         // lightmap update chains for light styles
251         int                             light_styles;
252         qbyte                   *light_style;
253         int                             *light_stylevalue;
254         msurface_t              ***light_styleupdatechains;
255         msurface_t              **light_styleupdatechainsbuffer;
256         int                             light_scalebit;
257         float                   light_ambient;
258
259         mleaf_t *(*PointInLeaf)(struct model_s *model, const float *p);
260         void (*BuildPVSTextureChains)(struct model_s *model);
261 }
262 model_brushq1_t;
263
264 /* MSVC can't compile empty structs, so this is commented out for now
265 typedef struct model_brushq2_s
266 {
267 }
268 model_brushq2_t;
269 */
270
271 #define Q3MTEXTURERENDERFLAGS_NODRAW 1
272 #define Q3MTEXTURERENDERFLAGS_SKY 2
273
274 typedef struct q3mtexture_s
275 {
276         char name[Q3PATHLENGTH];
277         int surfaceflags;
278         int nativecontents;
279         int supercontents;
280         int renderflags;
281
282         int number;
283         skinframe_t skin;
284 }
285 q3mtexture_t;
286
287 typedef struct q3mnode_s
288 {
289         //this part shared between node and leaf
290         int isnode; // true
291         struct q3mnode_s *parent;
292         vec3_t mins;
293         vec3_t maxs;
294         // this part unique to nodes
295         struct mplane_s *plane;
296         struct q3mnode_s *children[2];
297 }
298 q3mnode_t;
299
300 typedef struct q3mleaf_s
301 {
302         //this part shared between node and leaf
303         int isnode; // false
304         struct q3mnode_s *parent;
305         vec3_t mins;
306         vec3_t maxs;
307         // this part unique to leafs
308         int clusterindex;
309         int areaindex;
310         int numleaffaces;
311         struct q3mface_s **firstleafface;
312         int numleafbrushes;
313         struct q3mbrush_s **firstleafbrush;
314 }
315 q3mleaf_t;
316
317 typedef struct q3mmodel_s
318 {
319         vec3_t mins;
320         vec3_t maxs;
321         int numfaces;
322         struct q3mface_s *firstface;
323         int numbrushes;
324         struct q3mbrush_s *firstbrush;
325 }
326 q3mmodel_t;
327
328 typedef struct q3mbrush_s
329 {
330         struct colbrushf_s *colbrushf;
331         int numbrushsides;
332         struct q3mbrushside_s *firstbrushside;
333         struct q3mtexture_s *texture;
334 }
335 q3mbrush_t;
336
337 typedef struct q3mbrushside_s
338 {
339         struct mplane_s *plane;
340         struct q3mtexture_s *texture;
341 }
342 q3mbrushside_t;
343
344 typedef struct q3meffect_s
345 {
346         char shadername[Q3PATHLENGTH];
347         struct q3mbrush_s *brush;
348         int unknown; // 5 or -1
349 }
350 q3meffect_t;
351
352 typedef struct q3mface_s
353 {
354         struct q3mtexture_s *texture;
355         struct q3meffect_s *effect;
356         rtexture_t *lightmaptexture;
357         int collisions; // performs per triangle collisions on this surface
358         int collisionmarkframe; // don't collide twice in one trace
359         int type;
360         int firstvertex;
361         int numvertices;
362         int firstelement;
363         int numelements;
364         int patchsize[2];
365         // used for processing
366         int markframe;
367         // (world only) visframe == r_framecount means it is visible this frame
368         int visframe;
369         // bounding box for culling
370         float mins[3];
371         float maxs[3];
372
373         float *data_vertex3f;
374         float *data_texcoordtexture2f;
375         float *data_texcoordlightmap2f;
376         float *data_svector3f;
377         float *data_tvector3f;
378         float *data_normal3f;
379         float *data_color4f;
380         int numtriangles;
381         int *data_element3i;
382         int *data_neighbor3i;
383 }
384 q3mface_t;
385
386 typedef struct model_brushq3_s
387 {
388         // if non-zero this is a submodel
389         // (this is the number of the submodel, an index into data_models)
390         int submodel;
391
392         int num_textures;
393         q3mtexture_t *data_textures;
394
395         int num_planes;
396         mplane_t *data_planes;
397
398         int num_nodes;
399         q3mnode_t *data_nodes;
400
401         int num_leafs;
402         q3mleaf_t *data_leafs;
403
404         int num_leafbrushes;
405         q3mbrush_t **data_leafbrushes;
406
407         int num_leaffaces;
408         q3mface_t **data_leaffaces;
409
410         int num_models;
411         q3mmodel_t *data_models;
412         // each submodel gets its own model struct so this is different for each.
413         q3mmodel_t *data_thismodel;
414
415         int num_brushes;
416         q3mbrush_t *data_brushes;
417
418         int num_brushsides;
419         q3mbrushside_t *data_brushsides;
420
421         int num_vertices;
422         float *data_vertex3f;
423         float *data_texcoordtexture2f;
424         float *data_texcoordlightmap2f;
425         float *data_svector3f;
426         float *data_tvector3f;
427         float *data_normal3f;
428         float *data_color4f;
429
430         int num_triangles;
431         int *data_element3i;
432         int *data_neighbor3i;
433
434         int num_effects;
435         q3meffect_t *data_effects;
436
437         int num_faces;
438         q3mface_t *data_faces;
439
440         // lightmap textures
441         int num_lightmaps;
442         rtexture_t **data_lightmaps;
443
444         // voxel light data with directional shading
445         int num_lightgrid;
446         q3dlightgrid_t *data_lightgrid;
447         // size of each cell (may vary by map, typically 64 64 128)
448         float num_lightgrid_cellsize[3];
449         // 1.0 / num_lightgrid_cellsize
450         float num_lightgrid_scale[3];
451         // dimensions of the world model in lightgrid cells
452         int num_lightgrid_imins[3];
453         int num_lightgrid_imaxs[3];
454         int num_lightgrid_isize[3];
455         // indexing/clamping
456         int num_lightgrid_dimensions[3];
457         // transform modelspace coordinates to lightgrid index
458         matrix4x4_t num_lightgrid_indexfromworld;
459
460         // pvs
461         int num_pvsclusters;
462         int num_pvschainlength;
463         unsigned char *data_pvschains;
464         // example
465         //pvschain = model->brushq3.data_pvschains + mycluster * model->brushq3.num_pvschainlength;
466         //if (pvschain[thatcluster >> 3] & (1 << (thatcluster & 7)))
467 }
468 model_brushq3_t;
469
470 typedef struct model_s
471 {
472         // name and path of model, for example "progs/player.mdl"
473         char                    name[MAX_QPATH];
474         // model needs to be loaded if this is true
475         qboolean                needload;
476         // set if the model is used in current map, models which are not, are purged
477         qboolean                used;
478         // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
479         qboolean                isworldmodel;
480         // CRC of the file this model was loaded from, to reload if changed
481         unsigned int    crc;
482         // mod_brush, mod_alias, mod_sprite
483         modtype_t               type;
484         // memory pool for allocations
485         mempool_t               *mempool;
486         // all models use textures...
487         rtexturepool_t  *texturepool;
488         // flags from the model file
489         int                             flags;
490         // engine calculated flags, ones that can not be set in the file
491         int                             flags2;
492         // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
493         int                             fullbright;
494         // number of QC accessible frame(group)s in the model
495         int                             numframes;
496         // number of QC accessible skin(group)s in the model
497         int                             numskins;
498         // whether to randomize animated framegroups
499         synctype_t              synctype;
500         // bounding box at angles '0 0 0'
501         vec3_t                  normalmins, normalmaxs;
502         // bounding box if yaw angle is not 0, but pitch and roll are
503         vec3_t                  yawmins, yawmaxs;
504         // bounding box if pitch or roll are used
505         vec3_t                  rotatedmins, rotatedmaxs;
506         // sphere radius, usable at any angles
507         float                   radius;
508         // squared sphere radius for easier comparisons
509         float                   radius2;
510         // skin animation info
511         animscene_t             *skinscenes; // [numskins]
512         // skin animation info
513         animscene_t             *animscenes; // [numframes]
514         // draw the model's sky polygons (only used by brush models)
515         void(*DrawSky)(struct entity_render_s *ent);
516         // draw the model using lightmap/dlight shading
517         void(*Draw)(struct entity_render_s *ent);
518         // draw a fake shadow for the model
519         void(*DrawFakeShadow)(struct entity_render_s *ent);
520         // draw a shadow volume for the model based on light source
521         void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
522         // draw the lighting on a model (through stencil)
523         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);
524         // fields belonging to each type of model
525         model_alias_t   alias;
526         model_sprite_t  sprite;
527         model_brush_t   brush;
528         model_brushq1_t brushq1;
529         /* MSVC can't handle an empty struct, so this is commented out for now
530         model_brushq2_t brushq2;
531         */
532         model_brushq3_t brushq3;
533         // skin files can have different tags for each skin
534         overridetagnameset_t    *data_overridetagnamesforskin;
535 }
536 model_t;
537
538 //============================================================================
539
540 // this can be used for anything without a valid texture
541 extern rtexture_t *r_notexture;
542 #define NUM_DETAILTEXTURES 1
543 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
544 // every texture must be in a pool...
545 extern rtexturepool_t *mod_shared_texturepool;
546
547 extern rtexture_t *mod_shared_distorttexture;
548
549 // model loading
550 extern model_t *loadmodel;
551 extern qbyte *mod_base;
552 // sky/water subdivision
553 //extern cvar_t gl_subdivide_size;
554 // texture fullbrights
555 extern cvar_t r_fullbrights;
556
557 void Mod_Init (void);
558 void Mod_CheckLoaded (model_t *mod);
559 void Mod_ClearAll (void);
560 model_t *Mod_FindName (const char *name);
561 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
562 void Mod_TouchModel (const char *name);
563 void Mod_UnloadModel (model_t *mod);
564
565 void Mod_ClearUsed(void);
566 void Mod_PurgeUnused(void);
567 void Mod_LoadModels(void);
568
569 extern model_t *loadmodel;
570 extern char loadname[32];       // for hunk tags
571
572 int Mod_FindTriangleWithEdge(const int *elements, int numtriangles, int start, int end, int ignore);
573 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
574 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
575 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
576
577 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts);
578 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh);
579 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *v);
580 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, float *vert0, float *vert1, float *vert2);
581 void Mod_ShadowMesh_AddPolygon(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts);
582 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, float *verts, int numtris, int *elements);
583 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int initialnumtriangles);
584 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh);
585 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
586 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
587
588 int Mod_LoadSkinFrame(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
589 int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
590
591 // used for talking to the QuakeC mainly
592 int Mod_Q1BSP_NativeContentsFromSuperContents(struct model_s *model, int supercontents);
593 int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativecontents);
594
595 extern cvar_t r_mipskins;
596
597 typedef struct skinfileitem_s
598 {
599         struct skinfileitem_s *next;
600         char name[MAX_QPATH];
601         char replacement[MAX_QPATH];
602 }
603 skinfileitem_t;
604
605 typedef struct skinfile_s
606 {
607         struct skinfile_s *next;
608         skinfileitem_t *items;
609 }
610 skinfile_t;
611
612 skinfile_t *Mod_LoadSkinFiles(void);
613 void Mod_FreeSkinFiles(skinfile_t *skinfile);
614 int Mod_CountSkinFiles(skinfile_t *skinfile);
615
616 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f);
617
618 #endif  // __MODEL__
619