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