MaterialManager.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #pragma once
  35. #include <map>
  36. #include "AssetHelper.h"
  37. namespace AssimpView {
  38. //-------------------------------------------------------------------------------
  39. /* Helper class to create, access and destroy materials
  40. */
  41. //-------------------------------------------------------------------------------
  42. class CMaterialManager {
  43. friend class CDisplay;
  44. public:
  45. //------------------------------------------------------------------
  46. // Singleton accessors
  47. inline static CMaterialManager &Instance() {
  48. return s_cInstance;
  49. }
  50. //------------------------------------------------------------------
  51. // Delete all resources of a given material
  52. //
  53. // Must be called before CreateMaterial() to prevent memory leaking
  54. void DeleteMaterial(AssetHelper::MeshHelper *pcIn);
  55. /// @brief Create the material for a mesh.
  56. ///
  57. /// The function checks whether an identical shader is already in use.
  58. /// A shader is considered to be identical if it has the same input
  59. /// signature and takes the same number of texture channels.
  60. int CreateMaterial(AssetHelper::MeshHelper *pcMesh, const aiMesh *pcSource);
  61. /// @brief Setup the material for a given mesh.
  62. /// @param pcMesh Mesh to be rendered
  63. /// @param pcProj Projection matrix
  64. /// @param aiMe Current world matrix
  65. /// @param pcCam Camera matrix
  66. /// @param vPos Position of the camera
  67. /// @return 0 if successful.
  68. int SetupMaterial(AssetHelper::MeshHelper *pcMesh,
  69. const aiMatrix4x4 &pcProj,
  70. const aiMatrix4x4 &aiMe,
  71. const aiMatrix4x4 &pcCam,
  72. const aiVector3D &vPos);
  73. //------------------------------------------------------------------
  74. // End the material for a given mesh
  75. // Called after mesh rendering is complete
  76. // pcMesh Mesh object
  77. int EndMaterial(AssetHelper::MeshHelper *pcMesh);
  78. //------------------------------------------------------------------
  79. // Recreate all specular materials depending on the current
  80. // specularity settings
  81. //
  82. // Diffuse-only materials are ignored.
  83. // Must be called after specular highlights have been toggled
  84. int UpdateSpecularMaterials();
  85. //------------------------------------------------------------------
  86. // find a valid path to a texture file
  87. //
  88. // Handle 8.3 syntax correctly, search the environment of the
  89. // executable and the asset for a texture with a name very similar
  90. // to a given one
  91. int FindValidPath(aiString *p_szString);
  92. //------------------------------------------------------------------
  93. // Load a texture into memory and create a native D3D texture resource
  94. //
  95. // The function tries to find a valid path for a texture
  96. int LoadTexture(IDirect3DTexture9 **p_ppiOut, aiString *szPath);
  97. //------------------------------------------------------------------
  98. // Getter for m_iShaderCount
  99. //
  100. inline unsigned int GetShaderCount() {
  101. return this->m_iShaderCount;
  102. }
  103. //------------------------------------------------------------------
  104. // Reset the state of the class
  105. // Called whenever a new asset is loaded
  106. inline void Reset() {
  107. m_iShaderCount = 0;
  108. for (auto & sCachedTexture : sCachedTextures) {
  109. sCachedTexture.second->Release();
  110. }
  111. sCachedTextures.clear();
  112. }
  113. private:
  114. // The default constructor
  115. CMaterialManager() :
  116. m_iShaderCount(0),
  117. sDefaultTexture() {
  118. // empty
  119. }
  120. // Destructor, private.
  121. ~CMaterialManager() {
  122. if (sDefaultTexture) {
  123. sDefaultTexture->Release();
  124. }
  125. Reset();
  126. }
  127. //------------------------------------------------------------------
  128. // find a valid path to a texture file
  129. //
  130. // Handle 8.3 syntax correctly, search the environment of the
  131. // executable and the asset for a texture with a name very similar
  132. // to a given one
  133. bool TryLongerPath(char *szTemp, aiString *p_szString);
  134. //------------------------------------------------------------------
  135. // Setup the default texture for a texture channel
  136. //
  137. // Generates a default checker pattern for a texture
  138. int SetDefaultTexture(IDirect3DTexture9 **p_ppiOut);
  139. //------------------------------------------------------------------
  140. // Convert a height map to a normal map if necessary
  141. //
  142. // The function tries to detect the type of a texture automatically.
  143. // However, this won't work in every case.
  144. void HMtoNMIfNecessary(IDirect3DTexture9 *piTexture,
  145. IDirect3DTexture9 **piTextureOut,
  146. bool bWasOriginallyHM = true);
  147. //------------------------------------------------------------------
  148. // Search for non-opaque pixels in a texture
  149. //
  150. // A pixel is considered to be non-opaque if its alpha value is
  151. // less than 255
  152. //------------------------------------------------------------------
  153. bool HasAlphaPixels(IDirect3DTexture9 *piTexture);
  154. private:
  155. static CMaterialManager s_cInstance;
  156. // Specifies the number of different shaders generated for
  157. // the current asset. This number is incremented by CreateMaterial()
  158. // each time a shader isn't found in cache and needs to be created
  159. unsigned int m_iShaderCount;
  160. IDirect3DTexture9 *sDefaultTexture;
  161. using TextureCache = std::map<std::string, IDirect3DTexture9 *>;
  162. TextureCache sCachedTextures;
  163. };
  164. } // namespace AssimpView