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