1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __MODEL_H__
30 #define __MODEL_H__
31 
32 #include "idlib/bv/Bounds.h"
33 #include "renderer/Material.h"
34 
35 /*
36 ===============================================================================
37 
38 	Render Model
39 
40 ===============================================================================
41 */
42 
43 // shared between the renderer, game, and Maya export DLL
44 #define MD5_VERSION_STRING		"MD5Version"
45 #define MD5_MESH_EXT			"md5mesh"
46 #define MD5_ANIM_EXT			"md5anim"
47 #define MD5_CAMERA_EXT			"md5camera"
48 #define MD5_VERSION				10
49 
50 // using shorts for triangle indexes can save a significant amount of traffic, but
51 // to support the large models that renderBump loads, they need to be 32 bits
52 #if 1
53 
54 #define GL_INDEX_TYPE		GL_UNSIGNED_INT
55 typedef int glIndex_t;
56 
57 #else
58 
59 #define GL_INDEX_TYPE		GL_UNSIGNED_SHORT
60 typedef short glIndex_t;
61 
62 #endif
63 
64 
65 typedef struct {
66 	// NOTE: making this a glIndex is dubious, as there can be 2x the faces as verts
67 	glIndex_t					p1, p2;					// planes defining the edge
68 	glIndex_t					v1, v2;					// verts defining the edge
69 } silEdge_t;
70 
71 // this is used for calculating unsmoothed normals and tangents for deformed models
72 typedef struct dominantTri_s {
73 	glIndex_t					v2, v3;
74 	float						normalizationScale[3];
75 } dominantTri_t;
76 
77 typedef struct lightingCache_s {
78 	idVec3						localLightVector;		// this is the statically computed vector to the light
79 														// in texture space for cards without vertex programs
80 } lightingCache_t;
81 
82 typedef struct shadowCache_s {
83 	idVec4						xyz;					// we use homogenous coordinate tricks
84 } shadowCache_t;
85 
86 const int SHADOW_CAP_INFINITE	= 64;
87 
88 // our only drawing geometry type
89 typedef struct srfTriangles_s {
90 	idBounds					bounds;					// for culling
91 
92 	int							ambientViewCount;		// if == tr.viewCount, it is visible this view
93 
94 	bool						generateNormals;		// create normals from geometry, instead of using explicit ones
95 	bool						tangentsCalculated;		// set when the vertex tangents have been calculated
96 	bool						facePlanesCalculated;	// set when the face planes have been calculated
97 	bool						perfectHull;			// true if there aren't any dangling edges
98 	bool						deformedSurface;		// if true, indexes, silIndexes, mirrorVerts, and silEdges are
99 														// pointers into the original surface, and should not be freed
100 
101 	int							numVerts;				// number of vertices
102 	idDrawVert *				verts;					// vertices, allocated with special allocator
103 
104 	int							numIndexes;				// for shadows, this has both front and rear end caps and silhouette planes
105 	glIndex_t *					indexes;				// indexes, allocated with special allocator
106 
107 	glIndex_t *					silIndexes;				// indexes changed to be the first vertex with same XYZ, ignoring normal and texcoords
108 
109 	int							numMirroredVerts;		// this many verts at the end of the vert list are tangent mirrors
110 	int *						mirroredVerts;			// tri->mirroredVerts[0] is the mirror of tri->numVerts - tri->numMirroredVerts + 0
111 
112 	int							numDupVerts;			// number of duplicate vertexes
113 	int *						dupVerts;				// pairs of the number of the first vertex and the number of the duplicate vertex
114 
115 	int							numSilEdges;			// number of silhouette edges
116 	silEdge_t *					silEdges;				// silhouette edges
117 
118 	idPlane *					facePlanes;				// [numIndexes/3] plane equations
119 
120 	dominantTri_t *				dominantTris;			// [numVerts] for deformed surface fast tangent calculation
121 
122 	int							numShadowIndexesNoFrontCaps;	// shadow volumes with front caps omitted
123 	int							numShadowIndexesNoCaps;			// shadow volumes with the front and rear caps omitted
124 
125 	int							shadowCapPlaneBits;		// bits 0-5 are set when that plane of the interacting light has triangles
126 														// projected on it, which means that if the view is on the outside of that
127 														// plane, we need to draw the rear caps of the shadow volume
128 														// turboShadows will have SHADOW_CAP_INFINITE
129 
130 	shadowCache_t *				shadowVertexes;			// these will be copied to shadowCache when it is going to be drawn.
131 														// these are NULL when vertex programs are available
132 
133 	struct srfTriangles_s *		ambientSurface;			// for light interactions, point back at the original surface that generated
134 														// the interaction, which we will get the ambientCache from
135 
136 	struct srfTriangles_s *		nextDeferredFree;		// chain of tris to free next frame
137 
138 	// data in vertex object space, not directly readable by the CPU
139 	struct vertCache_s *		indexCache;				// int
140 	struct vertCache_s *		ambientCache;			// idDrawVert
141 	struct vertCache_s *		lightingCache;			// lightingCache_t
142 	struct vertCache_s *		shadowCache;			// shadowCache_t
143 } srfTriangles_t;
144 
145 typedef idList<srfTriangles_t *> idTriList;
146 
147 typedef struct modelSurface_s {
148 	int							id;
149 	const idMaterial *			shader;
150 	srfTriangles_t *			geometry;
151 } modelSurface_t;
152 
153 typedef enum {
154 	DM_STATIC,		// never creates a dynamic model
155 	DM_CACHED,		// once created, stays constant until the entity is updated (animating characters)
156 	DM_CONTINUOUS	// must be recreated for every single view (time dependent things like particles)
157 } dynamicModel_t;
158 
159 typedef enum {
160 	INVALID_JOINT				= -1
161 } jointHandle_t;
162 
163 class idMD5Joint {
164 public:
idMD5Joint()165 								idMD5Joint() { parent = NULL; }
166 	idStr						name;
167 	const idMD5Joint *			parent;
168 };
169 
170 
171 // the init methods may be called again on an already created model when
172 // a reloadModels is issued
173 
174 class idRenderModel {
175 public:
~idRenderModel()176 	virtual						~idRenderModel() {};
177 
178 	// Loads static models only, dynamic models must be loaded by the modelManager
179 	virtual void				InitFromFile( const char *fileName ) = 0;
180 
181 	// renderBump uses this to load the very high poly count models, skipping the
182 	// shadow and tangent generation, along with some surface cleanup to make it load faster
183 	virtual void				PartialInitFromFile( const char *fileName ) = 0;
184 
185 	// this is used for dynamically created surfaces, which are assumed to not be reloadable.
186 	// It can be called again to clear out the surfaces of a dynamic model for regeneration.
187 	virtual void				InitEmpty( const char *name ) = 0;
188 
189 	// dynamic model instantiations will be created with this
190 	// the geometry data will be owned by the model, and freed when it is freed
191 	// the geoemtry should be raw triangles, with no extra processing
192 	virtual void				AddSurface( modelSurface_t surface ) = 0;
193 
194 	// cleans all the geometry and performs cross-surface processing
195 	// like shadow hulls
196 	// Creates the duplicated back side geometry for two sided, alpha tested, lit materials
197 	// This does not need to be called if none of the surfaces added with AddSurface require
198 	// light interaction, and all the triangles are already well formed.
199 	virtual void				FinishSurfaces() = 0;
200 
201 	// frees all the data, but leaves the class around for dangling references,
202 	// which can regenerate the data with LoadModel()
203 	virtual void				PurgeModel() = 0;
204 
205 	// resets any model information that needs to be reset on a same level load etc..
206 	// currently only implemented for liquids
207 	virtual void				Reset() = 0;
208 
209 	// used for initial loads, reloadModel, and reloading the data of purged models
210 	// Upon exit, the model will absolutely be valid, but possibly as a default model
211 	virtual void				LoadModel() = 0;
212 
213 	// internal use
214 	virtual bool				IsLoaded() const = 0;
215 	virtual void				SetLevelLoadReferenced( bool referenced ) = 0;
216 	virtual bool				IsLevelLoadReferenced() = 0;
217 
218 	// models that are already loaded at level start time
219 	// will still touch their data to make sure they
220 	// are kept loaded
221 	virtual void				TouchData() = 0;
222 
223 	// dump any ambient caches on the model surfaces
224 	virtual void				FreeVertexCache() = 0;
225 
226 	// returns the name of the model
227 	virtual const char	*		Name() const = 0;
228 
229 	// prints a detailed report on the model for printModel
230 	virtual void				Print() const = 0;
231 
232 	// prints a single line report for listModels
233 	virtual void				List() const = 0;
234 
235 	// reports the amount of memory (roughly) consumed by the model
236 	virtual int					Memory() const = 0;
237 
238 	// for reloadModels
239 	virtual ID_TIME_T				Timestamp() const = 0;
240 
241 	// returns the number of surfaces
242 	virtual int					NumSurfaces() const = 0;
243 
244 	// NumBaseSurfaces will not count any overlays added to dynamic models
245 	virtual int					NumBaseSurfaces() const = 0;
246 
247 	// get a pointer to a surface
248 	virtual const modelSurface_t *Surface( int surfaceNum ) const = 0;
249 
250 	// Allocates surface triangles.
251 	// Allocates memory for srfTriangles_t::verts and srfTriangles_t::indexes
252 	// The allocated memory is not initialized.
253 	// srfTriangles_t::numVerts and srfTriangles_t::numIndexes are set to zero.
254 	virtual srfTriangles_t *	AllocSurfaceTriangles( int numVerts, int numIndexes ) const = 0;
255 
256 	// Frees surfaces triangles.
257 	virtual void				FreeSurfaceTriangles( srfTriangles_t *tris ) const = 0;
258 
259 	// created at load time by stitching together all surfaces and sharing
260 	// the maximum number of edges.  This may be incorrect if a skin file
261 	// remaps surfaces between shadow casting and non-shadow casting, or
262 	// if some surfaces are noSelfShadow and others aren't
263 	virtual srfTriangles_t	*	ShadowHull() const = 0;
264 
265 	// models of the form "_area*" may have a prelight shadow model associated with it
266 	virtual bool				IsStaticWorldModel() const = 0;
267 
268 	// models parsed from inside map files or dynamically created cannot be reloaded by
269 	// reloadmodels
270 	virtual bool				IsReloadable() const = 0;
271 
272 	// md3, md5, particles, etc
273 	virtual dynamicModel_t		IsDynamicModel() const = 0;
274 
275 	// if the load failed for any reason, this will return true
276 	virtual bool				IsDefaultModel() const = 0;
277 
278 	// dynamic models should return a fast, conservative approximation
279 	// static models should usually return the exact value
280 	virtual idBounds			Bounds( const struct renderEntity_s *ent = NULL ) const = 0;
281 
282 	// returns value != 0.0f if the model requires the depth hack
283 	virtual float				DepthHack() const = 0;
284 
285 	// returns a static model based on the definition and view
286 	// currently, this will be regenerated for every view, even though
287 	// some models, like character meshes, could be used for multiple (mirror)
288 	// views in a frame, or may stay static for multiple frames (corpses)
289 	// The renderer will delete the returned dynamic model the next view
290 	// This isn't const, because it may need to reload a purged model if it
291 	// wasn't precached correctly.
292 	virtual idRenderModel *		InstantiateDynamicModel( const struct renderEntity_s *ent, const struct viewDef_s *view, idRenderModel *cachedModel ) = 0;
293 
294 	// Returns the number of joints or 0 if the model is not an MD5
295 	virtual int					NumJoints( void ) const = 0;
296 
297 	// Returns the MD5 joints or NULL if the model is not an MD5
298 	virtual const idMD5Joint *	GetJoints( void ) const = 0;
299 
300 	// Returns the handle for the joint with the given name.
301 	virtual jointHandle_t		GetJointHandle( const char *name ) const = 0;
302 
303 	// Returns the name for the joint with the given handle.
304 	virtual const char *		GetJointName( jointHandle_t handle ) const = 0;
305 
306 	// Returns the default animation pose or NULL if the model is not an MD5.
307 	virtual const idJointQuat *	GetDefaultPose( void ) const = 0;
308 
309 	// Returns number of the joint nearest to the given triangle.
310 	virtual int					NearestJoint( int surfaceNum, int a, int c, int b ) const = 0;
311 
312 	// Writing to and reading from a demo file.
313 	virtual void				ReadFromDemoFile( class idDemoFile *f ) = 0;
314 	virtual void				WriteToDemoFile( class idDemoFile *f ) = 0;
315 };
316 
317 #endif /* !__MODEL_H__ */
318