2
0

AssetHelper.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #if (!defined AV_ASSET_HELPER_H_INCLUDED)
  35. #define AV_ASSET_HELPER_H_INCLUDED
  36. #include <d3d9.h>
  37. #include <d3dx9.h>
  38. #include <d3dx9mesh.h>
  39. #include <assimp/scene.h>
  40. namespace AssimpView {
  41. class SceneAnimator;
  42. //-------------------------------------------------------------------------------
  43. /** \brief Class to wrap ASSIMP's asset output structures
  44. */
  45. //-------------------------------------------------------------------------------
  46. class AssetHelper {
  47. public:
  48. enum {
  49. // the original normal set will be used
  50. ORIGINAL = 0x0u,
  51. // a smoothed normal set will be used
  52. SMOOTH = 0x1u,
  53. // a hard normal set will be used
  54. HARD = 0x2u,
  55. };
  56. // default constructor
  57. AssetHelper() :
  58. iNormalSet(ORIGINAL) {
  59. mAnimator = NULL;
  60. apcMeshes = NULL;
  61. pcScene = NULL;
  62. }
  63. // set the normal set to be used
  64. void SetNormalSet(unsigned int iSet);
  65. // flip all normal vectors
  66. void FlipNormals();
  67. void FlipNormalsInt();
  68. //---------------------------------------------------------------
  69. // default vertex data structure
  70. // (even if tangents, bitangents or normals aren't
  71. // required by the shader they will be committed to the GPU)
  72. //---------------------------------------------------------------
  73. struct Vertex {
  74. aiVector3D vPosition;
  75. aiVector3D vNormal;
  76. D3DCOLOR dColorDiffuse;
  77. aiVector3D vTangent;
  78. aiVector3D vBitangent;
  79. aiVector2D vTextureUV;
  80. aiVector2D vTextureUV2;
  81. unsigned char mBoneIndices[4];
  82. unsigned char mBoneWeights[4]; // last Weight not used, calculated inside the vertex shader
  83. /** Returns the vertex declaration elements to create a declaration from. */
  84. static D3DVERTEXELEMENT9 *GetDeclarationElements() {
  85. static D3DVERTEXELEMENT9 decl[] = {
  86. { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
  87. { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
  88. { 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
  89. { 0, 28, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 },
  90. { 0, 40, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0 },
  91. { 0, 52, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
  92. { 0, 60, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
  93. { 0, 68, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0 },
  94. { 0, 72, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0 },
  95. D3DDECL_END()
  96. };
  97. return decl;
  98. }
  99. };
  100. //---------------------------------------------------------------
  101. // FVF vertex structure used for normals
  102. //---------------------------------------------------------------
  103. struct LineVertex {
  104. aiVector3D vPosition;
  105. DWORD dColorDiffuse;
  106. // retrieves the FVF code of the vertex type
  107. static DWORD GetFVF() {
  108. return D3DFVF_DIFFUSE | D3DFVF_XYZ;
  109. }
  110. };
  111. //---------------------------------------------------------------
  112. // Helper class to store GPU related resources created for
  113. // a given aiMesh
  114. //---------------------------------------------------------------
  115. class MeshHelper {
  116. public:
  117. MeshHelper() :
  118. eShadingMode(),
  119. piVB(NULL),
  120. piIB(NULL),
  121. piVBNormals(NULL),
  122. piEffect(NULL),
  123. bSharedFX(false),
  124. piDiffuseTexture(NULL),
  125. piSpecularTexture(NULL),
  126. piAmbientTexture(NULL),
  127. piEmissiveTexture(NULL),
  128. piNormalTexture(NULL),
  129. piOpacityTexture(NULL),
  130. piShininessTexture(NULL),
  131. piLightmapTexture(NULL),
  132. fOpacity(),
  133. fShininess(),
  134. fSpecularStrength(),
  135. twosided(false),
  136. pvOriginalNormals(NULL) {}
  137. ~MeshHelper() {
  138. // NOTE: This is done in DeleteAssetData()
  139. // TODO: Make this a proper d'tor
  140. }
  141. // shading mode to use. Either Lambert or otherwise phong
  142. // will be used in every case
  143. aiShadingMode eShadingMode;
  144. // vertex buffer
  145. IDirect3DVertexBuffer9 *piVB;
  146. // index buffer. For partially transparent meshes
  147. // created with dynamic usage to be able to update
  148. // the buffer contents quickly
  149. IDirect3DIndexBuffer9 *piIB;
  150. // vertex buffer to be used to draw vertex normals
  151. // (vertex normals are generated in every case)
  152. IDirect3DVertexBuffer9 *piVBNormals;
  153. // shader to be used
  154. ID3DXEffect *piEffect;
  155. bool bSharedFX;
  156. // material textures
  157. IDirect3DTexture9 *piDiffuseTexture;
  158. IDirect3DTexture9 *piSpecularTexture;
  159. IDirect3DTexture9 *piAmbientTexture;
  160. IDirect3DTexture9 *piEmissiveTexture;
  161. IDirect3DTexture9 *piNormalTexture;
  162. IDirect3DTexture9 *piOpacityTexture;
  163. IDirect3DTexture9 *piShininessTexture;
  164. IDirect3DTexture9 *piLightmapTexture;
  165. // material colors
  166. D3DXVECTOR4 vDiffuseColor;
  167. D3DXVECTOR4 vSpecularColor;
  168. D3DXVECTOR4 vAmbientColor;
  169. D3DXVECTOR4 vEmissiveColor;
  170. // opacity for the material
  171. float fOpacity;
  172. // shininess for the material
  173. float fShininess;
  174. // strength of the specular highlight
  175. float fSpecularStrength;
  176. // two-sided?
  177. bool twosided;
  178. // Stores a pointer to the original normal set of the asset
  179. aiVector3D *pvOriginalNormals;
  180. };
  181. // One instance per aiMesh in the globally loaded asset
  182. MeshHelper **apcMeshes;
  183. // Scene wrapper instance
  184. aiScene *pcScene;
  185. // Animation player to animate the scene if necessary
  186. SceneAnimator *mAnimator;
  187. // Specifies the normal set to be used
  188. unsigned int iNormalSet;
  189. };
  190. } // namespace AssimpView
  191. #endif // !! IG