3 * Copyright (c) 2007-2010 SlimDX Group
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
27 #include "../ComObject.h"
\r
28 #include "../DataStream.h"
\r
29 #include "../Utilities.h"
\r
33 #include "MeshData.h"
\r
34 #include "MeshContainer.h"
\r
35 #include "PatchMesh.h"
\r
36 #include "ProgressiveMesh.h"
\r
37 #include "SkinInfo.h"
\r
39 using namespace System;
\r
45 MeshContainerShim::MeshContainerShim( SlimDX::Direct3D9::MeshContainer ^container ) : D3DXMESHCONTAINER( )
\r
47 m_Container = container;
\r
50 MeshContainer::MeshContainer()
\r
52 // Manual Allocation: ugly, but necessary
\r
53 // we clean up in both the destructor and the finalizer
\r
54 Pointer = new MeshContainerShim(this);
\r
55 Pointer->Name = NULL;
\r
56 Pointer->MeshData = D3DXMESHDATA();
\r
57 Pointer->pMaterials = NULL;
\r
58 Pointer->pEffects = NULL;
\r
59 Pointer->NumMaterials = 0;
\r
60 Pointer->pAdjacency = NULL;
\r
61 Pointer->pSkinInfo = NULL;
\r
62 Pointer->pNextMeshContainer = NULL;
\r
64 m_Name = String::Empty;
\r
65 m_NextContainer = nullptr;
\r
66 m_SkinInfo = nullptr;
\r
69 MeshContainer::~MeshContainer()
\r
74 MeshContainer::!MeshContainer()
\r
79 void MeshContainer::Destruct()
\r
81 if( Pointer != NULL )
\r
83 if( Pointer->pMaterials != NULL )
\r
85 for( UINT i = 0; i < Pointer->NumMaterials; i++ )
\r
86 Utilities::FreeNativeString( Pointer->pMaterials[i].pTextureFilename );
\r
88 delete[] Pointer->pMaterials;
\r
89 Pointer->pMaterials = NULL;
\r
92 delete[] Pointer->pAdjacency;
\r
93 Pointer->pAdjacency = NULL;
\r
95 Utilities::FreeNativeString( Pointer->Name );
\r
97 if( Pointer->pEffects != NULL )
\r
99 for( unsigned int i = 0; i < Pointer->NumMaterials; i++ )
\r
101 for( UINT j = 0; j < Pointer->pEffects[i].NumDefaults; j++ )
\r
102 Utilities::FreeNativeString( Pointer->pEffects[i].pDefaults[j].pParamName );
\r
104 delete[] Pointer->pEffects[i].pDefaults;
\r
107 delete[] Pointer->pEffects;
\r
108 Pointer->pEffects = NULL;
\r
111 if(m_MeshData != nullptr )
\r
113 delete m_MeshData->Mesh;
\r
114 delete m_MeshData->PatchMesh;
\r
115 delete m_MeshData->ProgressiveMesh;
\r
116 m_MeshData = nullptr;
\r
126 array<ExtendedMaterial>^ MeshContainer::GetMaterials()
\r
128 array<ExtendedMaterial>^ results = gcnew array<ExtendedMaterial>( Pointer->NumMaterials );
\r
130 for( unsigned int i = 0; i < Pointer->NumMaterials; i++ )
\r
131 results[i] = ExtendedMaterial::FromUnmanaged( Pointer->pMaterials[i] );
\r
136 void MeshContainer::SetMaterials( array<ExtendedMaterial>^ materials )
\r
138 if( Pointer->pMaterials != NULL )
\r
140 for( UINT i = 0; i < Pointer->NumMaterials; i++ )
\r
141 Utilities::FreeNativeString( Pointer->pMaterials[i].pTextureFilename );
\r
142 delete[] Pointer->pMaterials;
\r
145 Pointer->NumMaterials = materials->Length;
\r
146 // Manual Allocation: ugly, but necessary
\r
147 // we clean up whenever a new set of materials is given to us, and in
\r
148 // both the destructor and finalizer
\r
149 Pointer->pMaterials = new D3DXMATERIAL[Pointer->NumMaterials];
\r
151 for( int i = 0; i < materials->Length; i++ )
\r
152 Pointer->pMaterials[i] = ExtendedMaterial::ToUnmanaged( materials[i] );
\r
155 array<EffectInstance>^ MeshContainer::GetEffects()
\r
157 array<EffectInstance>^ results = gcnew array<EffectInstance>( Pointer->NumMaterials );
\r
159 for( unsigned int i = 0; i < Pointer->NumMaterials; i++ )
\r
160 results[i] = EffectInstance::FromUnmanaged( Pointer->pEffects[i] );
\r
165 void MeshContainer::SetEffects( array<EffectInstance>^ effects )
\r
167 if( Pointer->pEffects != NULL )
\r
169 for( UINT i = 0; i < Pointer->NumMaterials; i++ )
\r
171 for( UINT j = 0; j < Pointer->pEffects[i].NumDefaults; j++ )
\r
172 Utilities::FreeNativeString( Pointer->pEffects[i].pDefaults[j].pParamName );
\r
174 delete[] Pointer->pEffects[i].pDefaults;
\r
177 delete[] Pointer->pEffects;
\r
180 Pointer->NumMaterials = effects->Length;
\r
181 // Manual Allocation: ugly, but necessary
\r
182 // we clean up whenever a new set of effects is given, and
\r
183 // in both the destructor and finalizer
\r
184 Pointer->pEffects = new D3DXEFFECTINSTANCE[effects->Length];
\r
186 for( int i = 0; i < effects->Length; i++ )
\r
187 Pointer->pEffects[i] = EffectInstance::ToUnmanaged( effects[i] );
\r
190 array<int>^ MeshContainer::GetAdjacency()
\r
194 if( MeshData->Type == MeshDataType::Mesh )
\r
195 count = MeshData->Mesh->FaceCount;
\r
196 else if( MeshData->Type == MeshDataType::ProgressiveMesh )
\r
197 count = MeshData->ProgressiveMesh->FaceCount;
\r
198 else if( MeshData->Type == MeshDataType::PatchMesh )
\r
199 count = MeshData->PatchMesh->PatchCount;
\r
202 array<int>^ results = gcnew array<int>( count );
\r
204 for( int i = 0; i < count; i++ )
\r
205 results[i] = Pointer->pAdjacency[i];
\r
210 void MeshContainer::SetAdjacency( array<int>^ adjacency )
\r
212 if( Pointer->pAdjacency != NULL )
\r
213 delete[] Pointer->pAdjacency;
\r
215 int count = adjacency->Length;
\r
216 // Manual Allocation: ugly, but necessary
\r
217 // we clean up whenever a new set is given to us, and
\r
218 // in both the destructor and finalizer
\r
219 Pointer->pAdjacency = new DWORD[count];
\r
221 for( int i = 0; i < count; i++ )
\r
222 Pointer->pAdjacency[i] = adjacency[i];
\r
225 String^ MeshContainer::Name::get()
\r
230 void MeshContainer::Name::set( String^ value )
\r
234 Utilities::FreeNativeString( Pointer->Name );
\r
235 Pointer->Name = Utilities::AllocateNativeString( value );
\r
238 MeshData^ MeshContainer::MeshData::get()
\r
243 void MeshContainer::MeshData::set( SlimDX::Direct3D9::MeshData^ value )
\r
245 m_MeshData = value;
\r
247 Pointer->MeshData.Type = static_cast<D3DXMESHDATATYPE>( value->Type );
\r
249 if( Pointer->MeshData.Type == D3DXMESHTYPE_MESH )
\r
250 Pointer->MeshData.pMesh = value->Mesh->InternalPointer;
\r
251 else if( Pointer->MeshData.Type == D3DXMESHTYPE_PMESH )
\r
252 Pointer->MeshData.pPMesh = value->ProgressiveMesh->InternalPointer;
\r
253 else if( Pointer->MeshData.Type == D3DXMESHTYPE_PATCHMESH )
\r
254 Pointer->MeshData.pPatchMesh = value->PatchMesh->InternalPointer;
\r
257 SlimDX::Direct3D9::SkinInfo^ MeshContainer::SkinInfo::get()
\r
262 void MeshContainer::SkinInfo::set( SlimDX::Direct3D9::SkinInfo^ value )
\r
264 m_SkinInfo = value;
\r
266 if( value == nullptr )
\r
267 Pointer->pSkinInfo = NULL;
\r
270 Pointer->pSkinInfo = value->InternalPointer;
\r
274 MeshContainer^ MeshContainer::NextMeshContainer::get()
\r
276 return m_NextContainer;
\r
279 void MeshContainer::NextMeshContainer::set( MeshContainer^ value )
\r
281 m_NextContainer = value;
\r
283 if( value == nullptr )
\r
284 Pointer->pNextMeshContainer = NULL;
\r
286 Pointer->pNextMeshContainer = value->Pointer;
\r