00001 /* 00002 --------------------------------------------------------------------------- 00003 Direct3D AssimpRenderer by ponx 00004 --------------------------------------------------------------------------- 00005 00006 This code is heavily based on the "AssimpView" project of the Open Asset Import Library (ASSIMP). 00007 http://assimp.sourceforge.net 00008 00009 /* 00010 --------------------------------------------------------------------------- 00011 Open Asset Import Library (ASSIMP) 00012 --------------------------------------------------------------------------- 00013 00014 Copyright (c) 2006-2008, ASSIMP Development Team 00015 00016 All rights reserved. 00017 00018 Redistribution and use of this software in source and binary forms, 00019 with or without modification, are permitted provided that the following 00020 conditions are met: 00021 00022 * Redistributions of source code must retain the above 00023 copyright notice, this list of conditions and the 00024 following disclaimer. 00025 00026 * Redistributions in binary form must reproduce the above 00027 copyright notice, this list of conditions and the 00028 following disclaimer in the documentation and/or other 00029 materials provided with the distribution. 00030 00031 * Neither the name of the ASSIMP team, nor the names of its 00032 contributors may be used to endorse or promote products 00033 derived from this software without specific prior 00034 written permission of the ASSIMP Development Team. 00035 00036 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00037 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00038 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00039 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00040 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00041 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00042 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00043 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00044 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00045 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00046 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00047 --------------------------------------------------------------------------- 00048 */ 00049 00050 #pragma once 00051 00052 //#include <d3d9.h> 00053 //#include <string> 00054 struct aiMesh; 00055 00056 #include "AssetHelper.h" 00057 //#include "AssimpRenderer.h" 00058 00059 00060 namespace AssimpRendererD3d 00061 { 00062 00063 //------------------------------------------------------------------------------- 00064 /* Helper class to create, access and destroy materials 00065 */ 00066 //------------------------------------------------------------------------------- 00067 class CMaterialManager 00068 { 00069 00070 private: 00071 00072 friend class AssimpRenderer; 00073 friend class CDisplay; 00074 00075 // default constructor 00076 CMaterialManager() 00077 : m_iShaderCount (0) {} 00078 00079 public: 00080 00081 //------------------------------------------------------------------ 00082 // Singleton accessors 00083 static CMaterialManager s_cInstance; 00084 00085 inline static CMaterialManager& Instance () 00086 { 00087 return s_cInstance; 00088 } 00089 00090 00091 // pnx: FindValidPath will search the directory of this path when lookin for texture files. 00092 // Shall be set from outside (e.g. when loading an asset) 00093 aiString currentFilePath; 00094 00095 //------------------------------------------------------------------ 00096 // Delete all resources of a given material 00097 // 00098 // Must be called before CreateMaterial() to prevent memory leaking 00099 void DeleteMaterial(AssetHelper::MeshHelper* pcIn); 00100 00101 //------------------------------------------------------------------ 00102 // Create the material for a mesh. 00103 // 00104 // The function checks whether an identical shader is already in use. 00105 // A shader is considered to be identical if it has the same input 00106 // signature and takes the same number of texture channels. 00107 int CreateMaterial(const AssetHelper* g_pcAsset, AssetHelper::MeshHelper* pcMesh, const aiMesh* pcSource); 00108 00109 //------------------------------------------------------------------ 00110 // Setup the material for a given mesh 00111 // pcMesh Mesh to be rendered 00112 // pcProj Projection matrix 00113 // aiMe Current world matrix 00114 // pcCam Camera matrix 00115 // vPos Position of the camera 00116 // TODO: Extract camera position from matrix ... 00117 // 00118 int SetupMaterial (AssetHelper::MeshHelper* pcMesh, 00119 const aiMatrix4x4& pcProj, 00120 const aiMatrix4x4& aiMe, 00121 const aiMatrix4x4& pcCam, 00122 const aiVector3D& vPos, 00123 const AssetHelper& assetHelper); 00124 00125 //------------------------------------------------------------------ 00126 // End the material for a given mesh 00127 // Called after mesh rendering is complete 00128 // pcMesh Mesh object 00129 int EndMaterial (AssetHelper::MeshHelper* pcMesh); 00130 00131 //------------------------------------------------------------------ 00132 // Recreate all specular materials depending on the current 00133 // specularity settings 00134 // 00135 // Diffuse-only materials are ignored. 00136 // Must be called after specular highlights have been toggled 00137 int UpdateSpecularMaterials(const AssetHelper* g_pcAsset); 00138 00139 //------------------------------------------------------------------ 00140 // find a valid path to a texture file 00141 // 00142 // Handle 8.3 syntax correctly, search the environment of the 00143 // executable and the asset for a texture with a name very similar 00144 // to a given one 00145 int FindValidPath(aiString* p_szString); 00146 00147 //------------------------------------------------------------------ 00148 // Load a texture into memory and create a native D3D texture resource 00149 // 00150 // The function tries to find a valid path for a texture 00151 int LoadTexture(const AssetHelper* g_pcAsset, IDirect3DTexture9** p_ppiOut, aiString* szPath); 00152 00153 00154 // PNX: die hier ist neu, um bein Deserialisieren die Texturen zu laden 00155 int LoadDirect3DTexturesInMeshHelper(const AssetHelper* g_pcAsset, AssetHelper::MeshHelper* pcMesh); 00156 00157 //------------------------------------------------------------------ 00158 // Getter for m_iShaderCount 00159 // 00160 inline unsigned int GetShaderCount() 00161 { 00162 return this->m_iShaderCount; 00163 } 00164 00165 //------------------------------------------------------------------ 00166 // Reset the state of the class 00167 // Called whenever a new asset is loaded 00168 inline void Reset() 00169 { 00170 this->m_iShaderCount = 0; 00171 } 00172 00173 private: 00174 00175 //------------------------------------------------------------------ 00176 // find a valid path to a texture file 00177 // 00178 // Handle 8.3 syntax correctly, search the environment of the 00179 // executable and the asset for a texture with a name very similar 00180 // to a given one 00181 bool TryLongerPath(char* szTemp,aiString* p_szString); 00182 00183 //------------------------------------------------------------------ 00184 // Setup the default texture for a texture channel 00185 // 00186 // Generates a default checker pattern for a texture 00187 int SetDefaultTexture(IDirect3DTexture9** p_ppiOut); 00188 00189 //------------------------------------------------------------------ 00190 // Convert a height map to a normal map if necessary 00191 // 00192 // The function tries to detect the type of a texture automatically. 00193 // However, this wont work in every case. 00194 void HMtoNMIfNecessary(IDirect3DTexture9* piTexture, 00195 IDirect3DTexture9** piTextureOut, 00196 bool bWasOriginallyHM = true); 00197 00198 //------------------------------------------------------------------ 00199 // Search for non-opaque pixels in a texture 00200 // 00201 // A pixel is considered to be non-opaque if its alpha value is 00202 // less than 255 00203 //------------------------------------------------------------------ 00204 bool HasAlphaPixels(IDirect3DTexture9* piTexture); 00205 00206 private: 00207 00208 // 00209 // Specifies the number of different shaders generated for 00210 // the current asset. This number is incremented by CreateMaterial() 00211 // each time a shader isn't found in cache and needs to be created 00212 // 00213 unsigned int m_iShaderCount; 00214 }; 00215 00216 } // namespace 00217