You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

203 lines
6.8 KiB

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