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