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