OSDN Git Service

97976742ab705d6b518fd6b24be2ffdeda2f74cf
[dtxmania/dtxmania.git] / SlimDXc_Jun2010(VC++2008) / source / direct3d9 / UVAtlas.cpp
1 #include "stdafx.h"\r
2 /*\r
3 * Copyright (c) 2007-2010 SlimDX Group\r
4\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
11\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
14\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
21 * THE SOFTWARE.\r
22 */\r
23 #include <d3d9.h>\r
24 #include <d3dx9.h>\r
25 \r
26 #include "../ComObject.h"\r
27 #include "../DataStream.h"\r
28 \r
29 #include "Direct3D9Exception.h"\r
30 \r
31 #include "Device.h"\r
32 #include "Mesh.h"\r
33 #include "UVAtlas.h"\r
34 \r
35 using namespace System;\r
36 using namespace System::Runtime::InteropServices;\r
37 \r
38 namespace SlimDX\r
39 {\r
40 namespace Direct3D9\r
41 {\r
42         HRESULT WINAPI NativeAtlasCallback( FLOAT fPercentDone, LPVOID lpUserContext )\r
43         {\r
44                 GCHandle handle = GCHandle::FromIntPtr( IntPtr( lpUserContext ) );\r
45                 Predicate<float>^ callback = safe_cast<Predicate<float>^>( handle.Target );\r
46                 HRESULT result;\r
47 \r
48                 try\r
49                 {\r
50                         result = callback( fPercentDone ) ? S_OK : E_FAIL;\r
51                 }\r
52                 catch( Exception^ )\r
53                 {\r
54                         result = E_FAIL;\r
55                 }\r
56 \r
57                 return result;\r
58         }\r
59 \r
60         UVAtlasOutput^ UVAtlas::Create( Mesh^ mesh, int maxChartCount, float maxStretch, int width, int height, float gutter, int textureIndex,\r
61                                 array<int>^ falseEdges, array<float>^ integratedMetricTensors, UVAtlasQuality quality )\r
62         {\r
63                 return Create( mesh, maxChartCount, maxStretch, width, height, gutter, textureIndex, falseEdges, integratedMetricTensors, quality, nullptr, 0.0001f );\r
64         }\r
65 \r
66         UVAtlasOutput^ UVAtlas::Create( Mesh^ mesh, int maxChartCount, float maxStretch, int width, int height, float gutter, int textureIndex,\r
67                                 array<int>^ falseEdges, array<float>^ integratedMetricTensors, UVAtlasQuality quality, System::Predicate<float>^ callback, float callbackFrequency )\r
68         {\r
69                 ID3DXMesh *meshOut;\r
70                 ID3DXBuffer *facePartitioning;\r
71                 ID3DXBuffer *vertexRemap;\r
72                 FLOAT maxStretchOut;\r
73                 UINT numChartsOut;\r
74                 HRESULT hr;\r
75 \r
76                 pin_ptr<int> pinnedAdj;\r
77                 pin_ptr<int> pinnedFalseEdges;\r
78                 pin_ptr<float> pinnedIMTs;\r
79 \r
80                 DWORD *inputAdjacencyPtr = NULL;\r
81                 DWORD *falseEdgesPtr = NULL;\r
82                 FLOAT *imtPtr = NULL;\r
83 \r
84                 array<int>^ adj = mesh->GetAdjacency();\r
85 \r
86                 if( adj != nullptr )\r
87                 {\r
88                         pinnedAdj = &adj[0];\r
89                         inputAdjacencyPtr = reinterpret_cast<DWORD*>( pinnedAdj );\r
90                 }\r
91 \r
92                 if( falseEdges != nullptr )\r
93                 {\r
94                         pinnedFalseEdges = &falseEdges[0];\r
95                         falseEdgesPtr = reinterpret_cast<DWORD*>( pinnedFalseEdges );\r
96                 }\r
97 \r
98                 if( integratedMetricTensors != nullptr )\r
99                 {\r
100                         pinnedIMTs = &integratedMetricTensors[0];\r
101                         imtPtr = pinnedIMTs;\r
102                 }\r
103 \r
104                 if( callback != nullptr )\r
105                 {\r
106                         GCHandle handle = GCHandle::Alloc( callback, GCHandleType::Pinned );\r
107 \r
108                         hr = D3DXUVAtlasCreate( mesh->InternalPointer, maxChartCount, maxStretch, width, height, gutter, textureIndex,\r
109                                 inputAdjacencyPtr, falseEdgesPtr, imtPtr, NativeAtlasCallback, callbackFrequency, safe_cast<IntPtr>( handle ).ToPointer(), \r
110                                 static_cast<DWORD>( quality ), &meshOut, &facePartitioning, &vertexRemap, &maxStretchOut, &numChartsOut );\r
111 \r
112                         if( handle.IsAllocated )\r
113                                 handle.Free();\r
114                 }\r
115                 else\r
116                 {\r
117                         hr = D3DXUVAtlasCreate( mesh->InternalPointer, maxChartCount, maxStretch, width, height, gutter, textureIndex,\r
118                                 inputAdjacencyPtr, falseEdgesPtr, imtPtr, NULL, callbackFrequency, NULL, static_cast<DWORD>( quality ), &meshOut, \r
119                                 &facePartitioning, &vertexRemap, &maxStretchOut, &numChartsOut );\r
120                 }\r
121 \r
122                 if( RECORD_D3D9( hr ).IsFailure )\r
123                         return nullptr;\r
124 \r
125                 return gcnew UVAtlasOutput( Mesh::FromPointer( meshOut ), gcnew DataStream( facePartitioning ), gcnew DataStream( vertexRemap ), \r
126                         maxStretchOut, numChartsOut );\r
127         }\r
128 \r
129         UVAtlasOutput^ UVAtlas::Partition( Mesh^ mesh, int maxChartCount, float maxStretch, int textureIndex,\r
130                                 array<int>^ falseEdges, array<float>^ integratedMetricTensors, UVAtlasQuality quality, [Out] DataStream^% partitionAdjacency )\r
131         {\r
132                 return Partition( mesh, maxChartCount, maxStretch, textureIndex, falseEdges, integratedMetricTensors, quality, \r
133                         partitionAdjacency, nullptr, 0.0001f );\r
134         }\r
135 \r
136         UVAtlasOutput^ UVAtlas::Partition( Mesh^ mesh, int maxChartCount, float maxStretch, int textureIndex,\r
137                                 array<int>^ falseEdges, array<float>^ integratedMetricTensors, UVAtlasQuality quality, [Out] DataStream^% partitionAdjacency, \r
138                                 System::Predicate<float>^ callback, float callbackFrequency )\r
139         {\r
140                 ID3DXMesh *meshOut;\r
141                 ID3DXBuffer *facePartitioning;\r
142                 ID3DXBuffer *vertexRemap;\r
143                 ID3DXBuffer *partitionResult;\r
144                 FLOAT maxStretchOut;\r
145                 UINT numChartsOut;\r
146                 HRESULT hr;\r
147 \r
148                 pin_ptr<int> pinnedAdj;\r
149                 pin_ptr<int> pinnedFalseEdges;\r
150                 pin_ptr<float> pinnedIMTs;\r
151 \r
152                 DWORD *inputAdjacencyPtr = NULL;\r
153                 DWORD *falseEdgesPtr = NULL;\r
154                 FLOAT *imtPtr = NULL;\r
155 \r
156                 array<int>^ adj = mesh->GetAdjacency();\r
157 \r
158                 if( adj != nullptr )\r
159                 {\r
160                         pinnedAdj = &adj[0];\r
161                         inputAdjacencyPtr = reinterpret_cast<DWORD*>( pinnedAdj );\r
162                 }\r
163 \r
164                 if( falseEdges != nullptr )\r
165                 {\r
166                         pinnedFalseEdges = &falseEdges[0];\r
167                         falseEdgesPtr = reinterpret_cast<DWORD*>( pinnedFalseEdges );\r
168                 }\r
169 \r
170                 if( integratedMetricTensors != nullptr )\r
171                 {\r
172                         pinnedIMTs = &integratedMetricTensors[0];\r
173                         imtPtr = pinnedIMTs;\r
174                 }\r
175 \r
176                 if( callback != nullptr )\r
177                 {\r
178                         GCHandle handle = GCHandle::Alloc( callback, GCHandleType::Pinned );\r
179 \r
180                         hr = D3DXUVAtlasPartition( mesh->InternalPointer, maxChartCount, maxStretch, textureIndex,\r
181                                 inputAdjacencyPtr, falseEdgesPtr, imtPtr, NativeAtlasCallback, callbackFrequency, safe_cast<IntPtr>( handle ).ToPointer(), \r
182                                 static_cast<DWORD>( quality ), &meshOut, &facePartitioning, &vertexRemap, &partitionResult, &maxStretchOut, &numChartsOut );\r
183 \r
184                         if( handle.IsAllocated )\r
185                                 handle.Free();\r
186                 }\r
187                 else\r
188                 {\r
189                         hr = D3DXUVAtlasPartition( mesh->InternalPointer, maxChartCount, maxStretch, textureIndex,\r
190                                 inputAdjacencyPtr, falseEdgesPtr, imtPtr, NULL, callbackFrequency, NULL, static_cast<DWORD>( quality ), &meshOut, \r
191                                 &facePartitioning, &vertexRemap, &partitionResult, &maxStretchOut, &numChartsOut );\r
192                 }\r
193 \r
194                 if( RECORD_D3D9( hr ).IsFailure )\r
195                 {\r
196                         partitionAdjacency = nullptr;\r
197                         return nullptr;\r
198                 }\r
199 \r
200                 partitionAdjacency = gcnew DataStream( partitionResult );\r
201 \r
202                 return gcnew UVAtlasOutput( Mesh::FromPointer( meshOut ), gcnew DataStream( facePartitioning ), gcnew DataStream( vertexRemap ), \r
203                         maxStretchOut, numChartsOut );\r
204         }\r
205 \r
206         Result UVAtlas::Pack( Mesh^ mesh, int width, int height, float gutter, int textureIndex, array<int>^ partitionAdjacency, UVAtlasQuality quality, DataStream^ facePartitioning )\r
207         {\r
208                 return Pack( mesh, width, height, gutter, textureIndex, partitionAdjacency, quality, facePartitioning, nullptr, 0.0001f );\r
209         }\r
210 \r
211         Result UVAtlas::Pack( Mesh^ mesh, int width, int height, float gutter, int textureIndex, array<int>^ partitionAdjacency, \r
212                 UVAtlasQuality quality, DataStream^ facePartitioning, System::Predicate<float>^ callback, float callbackFrequency )\r
213         {\r
214                 HRESULT hr;\r
215                 pin_ptr<int> pinnedAdj = &partitionAdjacency[0];\r
216 \r
217                 if( callback != nullptr )\r
218                 {\r
219                         GCHandle handle = GCHandle::Alloc( callback, GCHandleType::Pinned );\r
220 \r
221                         hr = D3DXUVAtlasPack( mesh->InternalPointer, width, height, gutter, textureIndex,\r
222                                 reinterpret_cast<DWORD*>( pinnedAdj ), NativeAtlasCallback, callbackFrequency, safe_cast<IntPtr>( handle ).ToPointer(), \r
223                                 static_cast<DWORD>( quality ), facePartitioning->GetD3DBuffer() );\r
224 \r
225                         if( handle.IsAllocated )\r
226                                 handle.Free();\r
227                 }\r
228                 else\r
229                 {\r
230                         hr = D3DXUVAtlasPack( mesh->InternalPointer, width, height, gutter, textureIndex,\r
231                                 reinterpret_cast<DWORD*>( pinnedAdj ), NULL, callbackFrequency, NULL, \r
232                                 static_cast<DWORD>( quality ), facePartitioning->GetD3DBuffer() );\r
233                 }\r
234 \r
235                 return RECORD_D3D9( hr );\r
236         }\r
237 }\r
238 }