]> icculus.org git repositories - divverent/darkplaces.git/blob - model_shared.h
unified model skinframe loaders used for mdl, md2, md3, and bsp textures (both extern...
[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} 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 *verts;
77         int *elements;
78         int *neighbors;
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 typedef struct model_s
91 {
92         char                    name[MAX_QPATH];
93         // model needs to be loaded if this is true
94         qboolean                needload;
95         // set if the model is used in current map, models which are not, are purged
96         qboolean                used;
97         // CRC of the file this model was loaded from, to reload if changed
98         qboolean                crc;
99         // true if this is the world model (I.E. defines what sky to use, and may contain submodels)
100         qboolean                isworldmodel;
101         // true if this model is a HalfLife .bsp file
102         qboolean                ishlbsp;
103         // true if this model was not successfully loaded and should be purged
104         qboolean                error;
105
106         // mod_brush, mod_alias, mod_sprite
107         modtype_t               type;
108         // LordHavoc: Q2/ZYM model support
109         int                             aliastype;
110         // LordHavoc: if true (normally only for sprites) the model/sprite/bmodel is always rendered fullbright
111         int                             fullbright;
112         // number of QC accessible frame(group)s in the model
113         int                             numframes;
114         // number of QC accessible skin(group)s in the model
115         int                             numskins;
116         // whether to randomize animated framegroups
117         synctype_t              synctype;
118
119
120         // flags from the model file
121         int                             flags;
122         // engine calculated flags, ones that can not be set in the file
123         int                             flags2;
124
125         // all models use textures...
126         rtexturepool_t  *texturepool;
127
128         // volume occupied by the model
129         // bounding box at angles '0 0 0'
130         vec3_t                  normalmins, normalmaxs;
131         // bounding box if yaw angle is not 0, but pitch and roll are
132         vec3_t                  yawmins, yawmaxs;
133         // bounding box if pitch or roll are used
134         vec3_t                  rotatedmins, rotatedmaxs;
135         // sphere radius, usable at any angles
136         float                   radius;
137         // squared sphere radius for easier comparisons
138         float                   radius2;
139
140         // brush model specific
141         int                             firstmodelsurface, nummodelsurfaces;
142
143         // lightmap format, set to r_lightmaprgba when model is loaded
144         int                             lightmaprgba;
145
146         int                             numsubmodels;
147         dmodel_t                *submodels;
148
149         int                             numplanes;
150         mplane_t                *planes;
151
152         // number of visible leafs, not counting 0 (solid)
153         int                             numleafs;
154         mleaf_t                 *leafs;
155
156         int                             numvertexes;
157         mvertex_t               *vertexes;
158
159         int                             numedges;
160         medge_t                 *edges;
161
162         int                             numnodes;
163         mnode_t                 *nodes;
164
165         int                             numtexinfo;
166         mtexinfo_t              *texinfo;
167
168         int                             numsurfaces;
169         msurface_t              *surfaces;
170         int                             *surfacevisframes;
171         int                             *surfacepvsframes;
172         msurface_t              *surfacepvsnext;
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         char                    *entities;
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         // skin animation info
224         animscene_t             *skinscenes; // [numskins]
225         // skin frame info
226         skinframe_t             *skinframes;
227
228         animscene_t             *animscenes; // [numframes]
229
230         // mdl/md2/md3 models are the same after loading
231         int                             aliasnum_meshes;
232         aliasmesh_t             *aliasdata_meshes;
233
234         // for Zymotic models
235         int                             zymnum_verts;
236         int                             zymnum_tris;
237         int                             zymnum_shaders;
238         int                             zymnum_bones;
239         int                             zymnum_scenes;
240         float                   *zymdata_texcoords;
241         rtexture_t              **zymdata_textures;
242         qbyte                   *zymdata_trizone;
243         zymbone_t               *zymdata_bones;
244         unsigned int    *zymdata_vertbonecounts;
245         zymvertex_t             *zymdata_verts;
246         unsigned int    *zymdata_renderlist;
247         float                   *zymdata_poses;
248
249         int                             sprnum_type;
250         mspriteframe_t  *sprdata_frames;
251
252
253         // functions used in both rendering modes
254         // draw the model's sky polygons (only used by brush models)
255         void(*DrawSky)(struct entity_render_s *ent);
256
257         // functions used only in normal rendering mode
258         // draw the model
259         void(*Draw)(struct entity_render_s *ent);
260         // draw a fake shadow for the model
261         void(*DrawFakeShadow)(struct entity_render_s *ent);
262
263         // functions used only in shadow volume rendering mode
264         // draw a shadow volume for the model based on light source
265         void(*DrawShadowVolume)(struct entity_render_s *ent, vec3_t relativelightorigin, float lightradius);
266         // draw the lighting on a model (through stencil)
267         void(*DrawLight)(struct entity_render_s *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor);
268
269         // memory pool for allocations
270         mempool_t               *mempool;
271 }
272 model_t;
273
274 //============================================================================
275
276 // this can be used for anything without a valid texture
277 extern rtexture_t *r_notexture;
278 #define NUM_DETAILTEXTURES 1
279 extern rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
280 // every texture must be in a pool...
281 extern rtexturepool_t *mod_shared_texturepool;
282
283 // model loading
284 extern model_t *loadmodel;
285 extern qbyte *mod_base;
286 // sky/water subdivision
287 //extern cvar_t gl_subdivide_size;
288 // texture fullbrights
289 extern cvar_t r_fullbrights;
290
291 void Mod_Init (void);
292 void Mod_CheckLoaded (model_t *mod);
293 void Mod_ClearAll (void);
294 void Mod_ClearErrorModels (void);
295 model_t *Mod_FindName (const char *name);
296 model_t *Mod_ForName (const char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel);
297 void Mod_TouchModel (const char *name);
298 void Mod_UnloadModel (model_t *mod);
299
300 void Mod_ClearUsed(void);
301 void Mod_PurgeUnused(void);
302 void Mod_LoadModels(void);
303
304 extern model_t *loadmodel;
305 extern char loadname[32];       // for hunk tags
306
307 int Mod_FindTriangleWithEdge(const int *elements, int numtriangles, int start, int end, int ignore);
308 void Mod_BuildTriangleNeighbors(int *neighbors, const int *elements, int numtriangles);
309 void Mod_ValidateElements(const int *elements, int numtriangles, int numverts, const char *filename, int fileline);
310 void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const float *vertex, const float *texcoord, const int *elements, float *svectors, float *tvectors, float *normals);
311
312 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts);
313 shadowmesh_t *Mod_ShadowMesh_ReAlloc(mempool_t *mempool, shadowmesh_t *oldmesh);
314 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, float *v);
315 void Mod_ShadowMesh_AddTriangle(mempool_t *mempool, shadowmesh_t *mesh, float *vert0, float *vert1, float *vert2);
316 void Mod_ShadowMesh_AddPolygon(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts);
317 void Mod_ShadowMesh_AddMesh(mempool_t *mempool, shadowmesh_t *mesh, int numverts, float *verts, int numtris, int *elements);
318 shadowmesh_t *Mod_ShadowMesh_Begin(mempool_t *mempool, int initialnumtriangles);
319 shadowmesh_t *Mod_ShadowMesh_Finish(mempool_t *mempool, shadowmesh_t *firstmesh);
320 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
321 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
322
323 int Mod_LoadSkinFrame (skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture);
324 int Mod_LoadSkinFrame_Internal (skinframe_t *skinframe, char *basename, int textureflags, int loadpantsandshirt, int usedetailtexture, int loadglowtexture, qbyte *skindata, int width, int height);
325
326 #endif  // __MODEL__
327