Model.h 13 KB

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