DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Model.h (12845B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition 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 BFG Edition 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 /*
     33 ===============================================================================
     34 
     35 	Render Model
     36 
     37 ===============================================================================
     38 */
     39 
     40 // shared between the renderer, game, and Maya export DLL
     41 #define MD5_VERSION_STRING		"MD5Version"
     42 #define MD5_MESH_EXT			"md5mesh"
     43 #define MD5_ANIM_EXT			"md5anim"
     44 #define MD5_CAMERA_EXT			"md5camera"
     45 #define MD5_VERSION				10
     46 
     47 #include "jobs/ShadowShared.h"
     48 #include "jobs/prelightshadowvolume/PreLightShadowVolume.h"
     49 #include "jobs/staticshadowvolume/StaticShadowVolume.h"
     50 #include "jobs/dynamicshadowvolume/DynamicShadowVolume.h"
     51 
     52 // this is used for calculating unsmoothed normals and tangents for deformed models
     53 struct dominantTri_t {
     54 	triIndex_t					v2, v3;
     55 	float						normalizationScale[3];
     56 };
     57 
     58 const int SHADOW_CAP_INFINITE	= 64;
     59 
     60 class idRenderModelStatic;
     61 struct viewDef_t;
     62 
     63 // our only drawing geometry type
     64 struct srfTriangles_t {
     65 	srfTriangles_t() {}
     66 
     67 	idBounds					bounds;					// for culling
     68 
     69 	bool						generateNormals;		// create normals from geometry, instead of using explicit ones
     70 	bool						tangentsCalculated;		// set when the vertex tangents have been calculated
     71 	bool						perfectHull;			// true if there aren't any dangling edges
     72 	bool						referencedVerts;		// if true the 'verts' are referenced and should not be freed
     73 	bool						referencedIndexes;		// if true, indexes, silIndexes, mirrorVerts, and silEdges are
     74 														// pointers into the original surface, and should not be freed
     75 
     76 	int							numVerts;				// number of vertices
     77 	idDrawVert *				verts;					// vertices, allocated with special allocator
     78 
     79 	int							numIndexes;				// for shadows, this has both front and rear end caps and silhouette planes
     80 	triIndex_t *				indexes;				// indexes, allocated with special allocator
     81 
     82 	triIndex_t *				silIndexes;				// indexes changed to be the first vertex with same XYZ, ignoring normal and texcoords
     83 
     84 	int							numMirroredVerts;		// this many verts at the end of the vert list are tangent mirrors
     85 	int *						mirroredVerts;			// tri->mirroredVerts[0] is the mirror of tri->numVerts - tri->numMirroredVerts + 0
     86 
     87 	int							numDupVerts;			// number of duplicate vertexes
     88 	int *						dupVerts;				// pairs of the number of the first vertex and the number of the duplicate vertex
     89 
     90 	int							numSilEdges;			// number of silhouette edges
     91 	silEdge_t *					silEdges;				// silhouette edges
     92 
     93 	dominantTri_t *				dominantTris;			// [numVerts] for deformed surface fast tangent calculation
     94 
     95 	int							numShadowIndexesNoFrontCaps;	// shadow volumes with front caps omitted
     96 	int							numShadowIndexesNoCaps;			// shadow volumes with the front and rear caps omitted
     97 
     98 	int							shadowCapPlaneBits;		// bits 0-5 are set when that plane of the interacting light has triangles
     99 														// projected on it, which means that if the view is on the outside of that
    100 														// plane, we need to draw the rear caps of the shadow volume
    101 														// dynamic shadows will have SHADOW_CAP_INFINITE
    102 
    103 	idShadowVert *				preLightShadowVertexes;	// shadow vertices in CPU memory for pre-light shadow volumes
    104 	idShadowVert *				staticShadowVertexes;	// shadow vertices in CPU memory for static shadow volumes
    105 
    106 	srfTriangles_t *			ambientSurface;			// for light interactions, point back at the original surface that generated
    107 														// the interaction, which we will get the ambientCache from
    108 
    109 	srfTriangles_t *			nextDeferredFree;		// chain of tris to free next frame
    110 
    111 	// for deferred normal / tangent transformations by joints
    112 	// the jointsInverted list / buffer object on md5WithJoints may be
    113 	// shared by multiple srfTriangles_t
    114 	idRenderModelStatic *		staticModelWithJoints;
    115 
    116 	// data in vertex object space, not directly readable by the CPU
    117 	vertCacheHandle_t			indexCache;				// GL_INDEX_TYPE
    118 	vertCacheHandle_t			ambientCache;			// idDrawVert
    119 	vertCacheHandle_t			shadowCache;			// idVec4
    120 
    121 	DISALLOW_COPY_AND_ASSIGN( srfTriangles_t );
    122 };
    123 
    124 typedef idList<srfTriangles_t *, TAG_IDLIB_LIST_TRIANGLES> idTriList;
    125 
    126 struct modelSurface_t {
    127 	int							id;
    128 	const idMaterial *			shader;
    129 	srfTriangles_t *			geometry;
    130 };
    131 
    132 enum dynamicModel_t {
    133 	DM_STATIC,		// never creates a dynamic model
    134 	DM_CACHED,		// once created, stays constant until the entity is updated (animating characters)
    135 	DM_CONTINUOUS	// must be recreated for every single view (time dependent things like particles)
    136 };
    137 
    138 enum jointHandle_t {
    139 	INVALID_JOINT				= -1
    140 };
    141 
    142 class idMD5Joint {
    143 public:
    144 								idMD5Joint() { parent = NULL; }
    145 	idStr						name;
    146 	const idMD5Joint *			parent;
    147 };
    148 
    149 
    150 // the init methods may be called again on an already created model when
    151 // a reloadModels is issued
    152 
    153 class idRenderModel {
    154 public:
    155 	virtual						~idRenderModel() {};
    156 
    157 	// Loads static models only, dynamic models must be loaded by the modelManager
    158 	virtual void				InitFromFile( const char *fileName ) = 0;
    159 
    160 	// Supports reading/writing binary file formats
    161 	virtual bool				LoadBinaryModel( idFile * file, const ID_TIME_T sourceTimeStamp ) = 0;
    162 	virtual void				WriteBinaryModel( idFile * file, ID_TIME_T *_timeStamp = NULL ) const = 0;
    163 	virtual bool				SupportsBinaryModel() = 0;
    164 
    165 	// renderBump uses this to load the very high poly count models, skipping the
    166 	// shadow and tangent generation, along with some surface cleanup to make it load faster
    167 	virtual void				PartialInitFromFile( const char *fileName ) = 0;
    168 
    169 	// this is used for dynamically created surfaces, which are assumed to not be reloadable.
    170 	// It can be called again to clear out the surfaces of a dynamic model for regeneration.
    171 	virtual void				InitEmpty( const char *name ) = 0;
    172 
    173 	// dynamic model instantiations will be created with this
    174 	// the geometry data will be owned by the model, and freed when it is freed
    175 	// the geoemtry should be raw triangles, with no extra processing
    176 	virtual void				AddSurface( modelSurface_t surface ) = 0;
    177 
    178 	// cleans all the geometry and performs cross-surface processing
    179 	// like shadow hulls
    180 	// Creates the duplicated back side geometry for two sided, alpha tested, lit materials
    181 	// This does not need to be called if none of the surfaces added with AddSurface require
    182 	// light interaction, and all the triangles are already well formed.
    183 	virtual void				FinishSurfaces() = 0;
    184 
    185 	// frees all the data, but leaves the class around for dangling references,
    186 	// which can regenerate the data with LoadModel()
    187 	virtual void				PurgeModel() = 0;
    188 
    189 	// resets any model information that needs to be reset on a same level load etc.. 
    190 	// currently only implemented for liquids
    191 	virtual void				Reset() = 0;
    192 
    193 	// used for initial loads, reloadModel, and reloading the data of purged models
    194 	// Upon exit, the model will absolutely be valid, but possibly as a default model
    195 	virtual void				LoadModel() = 0;
    196 
    197 	// internal use
    198 	virtual bool				IsLoaded() = 0;
    199 	virtual void				SetLevelLoadReferenced( bool referenced ) = 0;
    200 	virtual bool				IsLevelLoadReferenced() = 0;
    201 
    202 	// models that are already loaded at level start time
    203 	// will still touch their data to make sure they
    204 	// are kept loaded
    205 	virtual void				TouchData() = 0;
    206 
    207 	// dump any ambient caches on the model surfaces
    208 	virtual void				FreeVertexCache() = 0;
    209 
    210 	// returns the name of the model
    211 	virtual const char	*		Name() const = 0;
    212 
    213 	// prints a detailed report on the model for printModel
    214 	virtual void				Print() const = 0;
    215 
    216 	// prints a single line report for listModels
    217 	virtual void				List() const = 0;
    218 
    219 	// reports the amount of memory (roughly) consumed by the model
    220 	virtual int					Memory() const = 0;
    221 
    222 	// for reloadModels
    223 	virtual ID_TIME_T			Timestamp() const = 0;
    224 
    225 	// returns the number of surfaces
    226 	virtual int					NumSurfaces() const = 0;
    227 
    228 	// NumBaseSurfaces will not count any overlays added to dynamic models
    229 	virtual int					NumBaseSurfaces() const = 0;
    230 
    231 	// get a pointer to a surface
    232 	virtual const modelSurface_t *Surface( int surfaceNum ) const = 0;
    233 
    234 	// Allocates surface triangles.
    235 	// Allocates memory for srfTriangles_t::verts and srfTriangles_t::indexes
    236 	// The allocated memory is not initialized.
    237 	// srfTriangles_t::numVerts and srfTriangles_t::numIndexes are set to zero.
    238 	virtual srfTriangles_t *	AllocSurfaceTriangles( int numVerts, int numIndexes ) const = 0;
    239 
    240 	// Frees surfaces triangles.
    241 	virtual void				FreeSurfaceTriangles( srfTriangles_t *tris ) const = 0;
    242 
    243 	// models of the form "_area*" may have a prelight shadow model associated with it
    244 	virtual bool				IsStaticWorldModel() const = 0;
    245 
    246 	// models parsed from inside map files or dynamically created cannot be reloaded by
    247 	// reloadmodels
    248 	virtual bool				IsReloadable() const = 0;
    249 
    250 	// md3, md5, particles, etc
    251 	virtual dynamicModel_t		IsDynamicModel() const = 0;
    252 
    253 	// if the load failed for any reason, this will return true
    254 	virtual bool				IsDefaultModel() const = 0;
    255 
    256 	// dynamic models should return a fast, conservative approximation
    257 	// static models should usually return the exact value
    258 	virtual idBounds			Bounds( const struct renderEntity_s *ent = NULL ) const = 0;
    259 
    260 	// returns value != 0.0f if the model requires the depth hack
    261 	virtual float				DepthHack() const = 0;
    262 
    263 	// returns a static model based on the definition and view
    264 	// currently, this will be regenerated for every view, even though
    265 	// some models, like character meshes, could be used for multiple (mirror)
    266 	// views in a frame, or may stay static for multiple frames (corpses)
    267 	// The renderer will delete the returned dynamic model the next view
    268 	// This isn't const, because it may need to reload a purged model if it
    269 	// wasn't precached correctly.
    270 	virtual idRenderModel *		InstantiateDynamicModel( const struct renderEntity_s *ent, const viewDef_t *view, idRenderModel *cachedModel ) = 0;
    271 
    272 	// Returns the number of joints or 0 if the model is not an MD5
    273 	virtual int					NumJoints() const = 0;
    274 
    275 	// Returns the MD5 joints or NULL if the model is not an MD5
    276 	virtual const idMD5Joint *	GetJoints() const = 0;
    277 
    278 	// Returns the handle for the joint with the given name.
    279 	virtual jointHandle_t		GetJointHandle( const char *name ) const = 0;
    280 
    281 	// Returns the name for the joint with the given handle.
    282 	virtual const char *		GetJointName( jointHandle_t handle ) const = 0;
    283 
    284 	// Returns the default animation pose or NULL if the model is not an MD5.
    285 	virtual const idJointQuat *	GetDefaultPose() const = 0;
    286 
    287 	// Returns number of the joint nearest to the given triangle.
    288 	virtual int					NearestJoint( int surfaceNum, int a, int c, int b ) const = 0;
    289 
    290 	// Writing to and reading from a demo file.
    291 	virtual void				ReadFromDemoFile( class idDemoFile *f ) = 0;
    292 	virtual void				WriteToDemoFile( class idDemoFile *f ) = 0;
    293 
    294 	// if false, the model doesn't need to be linked into the world, because it
    295 	// can't contribute visually -- triggers, etc
    296 	virtual bool				ModelHasDrawingSurfaces() const { return true; };
    297 
    298 	// if false, the model doesn't generate interactions with lights
    299  	virtual bool				ModelHasInteractingSurfaces() const { return true; };
    300 
    301 	// if false, the model doesn't need to be added to the view unless it is
    302 	// directly visible, because it can't cast shadows into the view
    303 	virtual bool				ModelHasShadowCastingSurfaces() const { return true; };
    304 };
    305 
    306 #endif /* !__MODEL_H__ */