Model.h 13 KB

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