xref: /reactos/dll/directx/wine/d3dx9_36/skin.c (revision 0e1975b6)
1 #ifdef __REACTOS__
2 #include "precomp.h"
3 #else
4 /*
5  * Skin Info operations specific to D3DX9.
6  *
7  * Copyright (C) 2011 Dylan Smith
8  * Copyright (C) 2013 Christian Costa
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
26 #include "d3dx9_private.h"
27 #endif /* __REACTOS__ */
28 
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30 
31 struct bone
32 {
33     char *name;
34     D3DXMATRIX transform;
35     DWORD num_influences;
36     DWORD *vertices;
37     FLOAT *weights;
38 };
39 
40 struct d3dx9_skin_info
41 {
42     ID3DXSkinInfo ID3DXSkinInfo_iface;
43     LONG ref;
44 
45     DWORD fvf;
46     D3DVERTEXELEMENT9 vertex_declaration[MAX_FVF_DECL_SIZE];
47     DWORD num_vertices;
48     DWORD num_bones;
49     struct bone *bones;
50 };
51 
impl_from_ID3DXSkinInfo(ID3DXSkinInfo * iface)52 static inline struct d3dx9_skin_info *impl_from_ID3DXSkinInfo(ID3DXSkinInfo *iface)
53 {
54     return CONTAINING_RECORD(iface, struct d3dx9_skin_info, ID3DXSkinInfo_iface);
55 }
56 
d3dx9_skin_info_QueryInterface(ID3DXSkinInfo * iface,REFIID riid,void ** out)57 static HRESULT WINAPI d3dx9_skin_info_QueryInterface(ID3DXSkinInfo *iface, REFIID riid, void **out)
58 {
59     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
60 
61     if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSkinInfo))
62     {
63         IUnknown_AddRef(iface);
64         *out = iface;
65         return D3D_OK;
66     }
67 
68     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
69 
70     return E_NOINTERFACE;
71 }
72 
d3dx9_skin_info_AddRef(ID3DXSkinInfo * iface)73 static ULONG WINAPI d3dx9_skin_info_AddRef(ID3DXSkinInfo *iface)
74 {
75     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
76     ULONG refcount = InterlockedIncrement(&skin->ref);
77 
78     TRACE("%p increasing refcount to %u.\n", skin, refcount);
79 
80     return refcount;
81 }
82 
d3dx9_skin_info_Release(ID3DXSkinInfo * iface)83 static ULONG WINAPI d3dx9_skin_info_Release(ID3DXSkinInfo *iface)
84 {
85     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
86     ULONG refcount = InterlockedDecrement(&skin->ref);
87 
88     TRACE("%p decreasing refcount to %u.\n", skin, refcount);
89 
90     if (!refcount)
91     {
92         DWORD i;
93 
94         for (i = 0; i < skin->num_bones; ++i)
95         {
96             HeapFree(GetProcessHeap(), 0, skin->bones[i].name);
97             HeapFree(GetProcessHeap(), 0, skin->bones[i].vertices);
98             HeapFree(GetProcessHeap(), 0, skin->bones[i].weights);
99         }
100         HeapFree(GetProcessHeap(), 0, skin->bones);
101         HeapFree(GetProcessHeap(), 0, skin);
102     }
103 
104     return refcount;
105 }
106 
d3dx9_skin_info_SetBoneInfluence(ID3DXSkinInfo * iface,DWORD bone_num,DWORD num_influences,const DWORD * vertices,const float * weights)107 static HRESULT WINAPI d3dx9_skin_info_SetBoneInfluence(ID3DXSkinInfo *iface,
108         DWORD bone_num, DWORD num_influences, const DWORD *vertices, const float *weights)
109 {
110     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
111     struct bone *bone;
112     DWORD *new_vertices = NULL;
113     FLOAT *new_weights = NULL;
114 
115     TRACE("iface %p, bone_num %u, num_influences %u, vertices %p, weights %p.\n",
116             iface, bone_num, num_influences, vertices, weights);
117 
118     if (bone_num >= skin->num_bones || !vertices || !weights)
119         return D3DERR_INVALIDCALL;
120 
121     if (num_influences) {
122         new_vertices = HeapAlloc(GetProcessHeap(), 0, num_influences * sizeof(*vertices));
123         if (!new_vertices)
124             return E_OUTOFMEMORY;
125         new_weights = HeapAlloc(GetProcessHeap(), 0, num_influences * sizeof(*weights));
126         if (!new_weights) {
127             HeapFree(GetProcessHeap(), 0, new_vertices);
128             return E_OUTOFMEMORY;
129         }
130         memcpy(new_vertices, vertices, num_influences * sizeof(*vertices));
131         memcpy(new_weights, weights, num_influences * sizeof(*weights));
132     }
133     bone = &skin->bones[bone_num];
134     bone->num_influences = num_influences;
135     HeapFree(GetProcessHeap(), 0, bone->vertices);
136     HeapFree(GetProcessHeap(), 0, bone->weights);
137     bone->vertices = new_vertices;
138     bone->weights = new_weights;
139 
140     return D3D_OK;
141 }
142 
d3dx9_skin_info_SetBoneVertexInfluence(ID3DXSkinInfo * iface,DWORD bone_num,DWORD influence_num,float weight)143 static HRESULT WINAPI d3dx9_skin_info_SetBoneVertexInfluence(ID3DXSkinInfo *iface,
144         DWORD bone_num, DWORD influence_num, float weight)
145 {
146     FIXME("iface %p, bone_num %u, influence_num %u, weight %.8e stub!\n",
147             iface, bone_num, influence_num, weight);
148 
149     return E_NOTIMPL;
150 }
151 
d3dx9_skin_info_GetNumBoneInfluences(ID3DXSkinInfo * iface,DWORD bone_num)152 static DWORD WINAPI d3dx9_skin_info_GetNumBoneInfluences(ID3DXSkinInfo *iface, DWORD bone_num)
153 {
154     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
155 
156     TRACE("iface %p, bone_num %u.\n", iface, bone_num);
157 
158     if (bone_num >= skin->num_bones)
159         return 0;
160 
161     return skin->bones[bone_num].num_influences;
162 }
163 
d3dx9_skin_info_GetBoneInfluence(ID3DXSkinInfo * iface,DWORD bone_num,DWORD * vertices,float * weights)164 static HRESULT WINAPI d3dx9_skin_info_GetBoneInfluence(ID3DXSkinInfo *iface,
165         DWORD bone_num, DWORD *vertices, float *weights)
166 {
167     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
168     struct bone *bone;
169 
170     TRACE("iface %p, bone_num %u, vertices %p, weights %p.\n",
171             iface, bone_num, vertices, weights);
172 
173     if (bone_num >= skin->num_bones || !vertices)
174         return D3DERR_INVALIDCALL;
175 
176     bone = &skin->bones[bone_num];
177     if (!bone->num_influences)
178         return D3D_OK;
179 
180     memcpy(vertices, bone->vertices, bone->num_influences * sizeof(*vertices));
181     if (weights)
182         memcpy(weights, bone->weights, bone->num_influences * sizeof(*weights));
183 
184     return D3D_OK;
185 }
186 
d3dx9_skin_info_GetBoneVertexInfluence(ID3DXSkinInfo * iface,DWORD bone_num,DWORD influence_num,float * weight,DWORD * vertex_num)187 static HRESULT WINAPI d3dx9_skin_info_GetBoneVertexInfluence(ID3DXSkinInfo *iface,
188         DWORD bone_num, DWORD influence_num, float *weight, DWORD *vertex_num)
189 {
190     FIXME("iface %p, bone_num %u, influence_num %u, weight %p, vertex_num %p stub!\n",
191             iface, bone_num, influence_num, weight, vertex_num);
192 
193     return E_NOTIMPL;
194 }
195 
d3dx9_skin_info_GetMaxVertexInfluences(ID3DXSkinInfo * iface,DWORD * max_vertex_influences)196 static HRESULT WINAPI d3dx9_skin_info_GetMaxVertexInfluences(ID3DXSkinInfo *iface, DWORD *max_vertex_influences)
197 {
198     FIXME("iface %p, max_vertex_influences %p stub!\n", iface, max_vertex_influences);
199 
200     return E_NOTIMPL;
201 }
202 
d3dx9_skin_info_GetNumBones(ID3DXSkinInfo * iface)203 static DWORD WINAPI d3dx9_skin_info_GetNumBones(ID3DXSkinInfo *iface)
204 {
205     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
206 
207     TRACE("iface %p.\n", iface);
208 
209     return skin->num_bones;
210 }
211 
d3dx9_skin_info_FindBoneVertexInfluenceIndex(ID3DXSkinInfo * iface,DWORD bone_num,DWORD vertex_num,DWORD * influence_index)212 static HRESULT WINAPI d3dx9_skin_info_FindBoneVertexInfluenceIndex(ID3DXSkinInfo *iface,
213         DWORD bone_num, DWORD vertex_num, DWORD *influence_index)
214 {
215     FIXME("iface %p, bone_num %u, vertex_num %u, influence_index %p stub!\n",
216             iface, bone_num, vertex_num, influence_index);
217 
218     return E_NOTIMPL;
219 }
220 
d3dx9_skin_info_GetMaxFaceInfluences(struct ID3DXSkinInfo * iface,struct IDirect3DIndexBuffer9 * index_buffer,DWORD num_faces,DWORD * max_face_influences)221 static HRESULT WINAPI d3dx9_skin_info_GetMaxFaceInfluences(struct ID3DXSkinInfo *iface,
222         struct IDirect3DIndexBuffer9 *index_buffer, DWORD num_faces, DWORD *max_face_influences)
223 {
224     FIXME("iface %p, index_buffer %p, num_faces %u, max_face_influences %p stub!\n",
225             iface, index_buffer, num_faces, max_face_influences);
226 
227     return E_NOTIMPL;
228 }
229 
d3dx9_skin_info_SetMinBoneInfluence(ID3DXSkinInfo * iface,float min_influence)230 static HRESULT WINAPI d3dx9_skin_info_SetMinBoneInfluence(ID3DXSkinInfo *iface, float min_influence)
231 {
232     FIXME("iface %p, min_influence %.8e stub!\n", iface, min_influence);
233 
234     return E_NOTIMPL;
235 }
236 
d3dx9_skin_info_GetMinBoneInfluence(ID3DXSkinInfo * iface)237 static float WINAPI d3dx9_skin_info_GetMinBoneInfluence(ID3DXSkinInfo *iface)
238 {
239     FIXME("iface %p stub!\n", iface);
240 
241     return 0.0f;
242 }
243 
d3dx9_skin_info_SetBoneName(ID3DXSkinInfo * iface,DWORD bone_idx,const char * name)244 static HRESULT WINAPI d3dx9_skin_info_SetBoneName(ID3DXSkinInfo *iface, DWORD bone_idx, const char *name)
245 {
246     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
247     char *new_name;
248     size_t size;
249 
250     TRACE("iface %p, bone_idx %u, name %s.\n", iface, bone_idx, debugstr_a(name));
251 
252     if (bone_idx >= skin->num_bones || !name)
253         return D3DERR_INVALIDCALL;
254 
255     size = strlen(name) + 1;
256     new_name = HeapAlloc(GetProcessHeap(), 0, size);
257     if (!new_name)
258         return E_OUTOFMEMORY;
259     memcpy(new_name, name, size);
260     HeapFree(GetProcessHeap(), 0, skin->bones[bone_idx].name);
261     skin->bones[bone_idx].name = new_name;
262 
263     return D3D_OK;
264 }
265 
d3dx9_skin_info_GetBoneName(ID3DXSkinInfo * iface,DWORD bone_idx)266 static const char * WINAPI d3dx9_skin_info_GetBoneName(ID3DXSkinInfo *iface, DWORD bone_idx)
267 {
268     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
269 
270     TRACE("iface %p, bone_idx %u.\n", iface, bone_idx);
271 
272     if (bone_idx >= skin->num_bones)
273         return NULL;
274 
275     return skin->bones[bone_idx].name;
276 }
277 
d3dx9_skin_info_SetBoneOffsetMatrix(ID3DXSkinInfo * iface,DWORD bone_num,const D3DXMATRIX * bone_transform)278 static HRESULT WINAPI d3dx9_skin_info_SetBoneOffsetMatrix(ID3DXSkinInfo *iface,
279         DWORD bone_num, const D3DXMATRIX *bone_transform)
280 {
281     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
282 
283     TRACE("iface %p, bone_num %u, bone_transform %p.\n", iface, bone_num, bone_transform);
284 
285     if (bone_num >= skin->num_bones || !bone_transform)
286         return D3DERR_INVALIDCALL;
287 
288     skin->bones[bone_num].transform = *bone_transform;
289     return D3D_OK;
290 }
291 
d3dx9_skin_info_GetBoneOffsetMatrix(ID3DXSkinInfo * iface,DWORD bone_num)292 static D3DXMATRIX * WINAPI d3dx9_skin_info_GetBoneOffsetMatrix(ID3DXSkinInfo *iface, DWORD bone_num)
293 {
294     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
295 
296     TRACE("iface %p, bone_num %u.\n", iface, bone_num);
297 
298     if (bone_num >= skin->num_bones)
299         return NULL;
300 
301     return &skin->bones[bone_num].transform;
302 }
303 
d3dx9_skin_info_Clone(ID3DXSkinInfo * iface,ID3DXSkinInfo ** skin_info)304 static HRESULT WINAPI d3dx9_skin_info_Clone(ID3DXSkinInfo *iface, ID3DXSkinInfo **skin_info)
305 {
306     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
307     unsigned int i;
308     HRESULT hr;
309 
310     TRACE("iface %p, skin_info %p.\n", iface, skin_info);
311 
312     if (FAILED(hr = D3DXCreateSkinInfo(skin->num_vertices, skin->vertex_declaration, skin->num_bones, skin_info)))
313         return hr;
314 
315     for (i = 0; i < skin->num_bones; ++i)
316     {
317         struct bone *current_bone = &skin->bones[i];
318 
319         if (current_bone->name && FAILED(hr = (*skin_info)->lpVtbl->SetBoneName(*skin_info, i, current_bone->name)))
320             break;
321         if (FAILED(hr = (*skin_info)->lpVtbl->SetBoneOffsetMatrix(*skin_info, i, &current_bone->transform)))
322             break;
323         if (current_bone->vertices && current_bone->weights
324                 && FAILED(hr = (*skin_info)->lpVtbl->SetBoneInfluence(*skin_info, i, current_bone->num_influences,
325                 current_bone->vertices, current_bone->weights)))
326             break;
327     }
328 
329     if (FAILED(hr))
330         (*skin_info)->lpVtbl->Release(*skin_info);
331 
332     return hr;
333 }
334 
d3dx9_skin_info_Remap(ID3DXSkinInfo * iface,DWORD num_vertices,DWORD * vertex_remap)335 static HRESULT WINAPI d3dx9_skin_info_Remap(ID3DXSkinInfo *iface, DWORD num_vertices, DWORD *vertex_remap)
336 {
337     FIXME("iface %p, num_vertices %u, vertex_remap %p stub!\n", iface, num_vertices, vertex_remap);
338 
339     return E_NOTIMPL;
340 }
341 
d3dx9_skin_info_SetFVF(ID3DXSkinInfo * iface,DWORD fvf)342 static HRESULT WINAPI d3dx9_skin_info_SetFVF(ID3DXSkinInfo *iface, DWORD fvf)
343 {
344     HRESULT hr;
345     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
346 
347     TRACE("iface %p, fvf %#x.\n", iface, fvf);
348 
349     hr = D3DXDeclaratorFromFVF(fvf, declaration);
350     if (FAILED(hr)) return hr;
351 
352     return iface->lpVtbl->SetDeclaration(iface, declaration);
353 }
354 
d3dx9_skin_info_SetDeclaration(ID3DXSkinInfo * iface,const D3DVERTEXELEMENT9 * declaration)355 static HRESULT WINAPI d3dx9_skin_info_SetDeclaration(ID3DXSkinInfo *iface, const D3DVERTEXELEMENT9 *declaration)
356 {
357     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
358     HRESULT hr;
359     int count;
360 
361     TRACE("iface %p, declaration %p.\n", iface, declaration);
362 
363     if (!declaration)
364         return D3DERR_INVALIDCALL;
365     for (count = 0; declaration[count].Stream != 0xff; count++) {
366         if (declaration[count].Stream != 0) {
367             WARN("Invalid vertex element %u; contains non-zero stream %u\n",
368                  count, declaration[count].Stream);
369             return D3DERR_INVALIDCALL;
370         }
371     }
372     count++;
373 
374     memcpy(skin->vertex_declaration, declaration, count * sizeof(*declaration));
375 
376     if (FAILED(hr = D3DXFVFFromDeclarator(skin->vertex_declaration, &skin->fvf)))
377         skin->fvf = 0;
378 
379     return D3D_OK;
380 }
381 
d3dx9_skin_info_GetFVF(ID3DXSkinInfo * iface)382 static DWORD WINAPI d3dx9_skin_info_GetFVF(ID3DXSkinInfo *iface)
383 {
384     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
385 
386     TRACE("iface %p.\n", iface);
387 
388     return skin->fvf;
389 }
390 
d3dx9_skin_info_GetDeclaration(ID3DXSkinInfo * iface,D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])391 static HRESULT WINAPI d3dx9_skin_info_GetDeclaration(ID3DXSkinInfo *iface,
392         D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
393 {
394     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
395     UINT count = 0;
396 
397     TRACE("iface %p, declaration %p.\n", iface, declaration);
398 
399     while (skin->vertex_declaration[count++].Stream != 0xff);
400     memcpy(declaration, skin->vertex_declaration, count * sizeof(declaration[0]));
401     return D3D_OK;
402 }
403 
d3dx9_skin_info_UpdateSkinnedMesh(ID3DXSkinInfo * iface,const D3DXMATRIX * bone_transforms,const D3DXMATRIX * bone_inv_transpose_transforms,const void * src_vertices,void * dst_vertices)404 static HRESULT WINAPI d3dx9_skin_info_UpdateSkinnedMesh(ID3DXSkinInfo *iface, const D3DXMATRIX *bone_transforms,
405         const D3DXMATRIX *bone_inv_transpose_transforms, const void *src_vertices, void *dst_vertices)
406 {
407     struct d3dx9_skin_info *skin = impl_from_ID3DXSkinInfo(iface);
408     DWORD size = D3DXGetFVFVertexSize(skin->fvf);
409     DWORD i, j;
410 
411     TRACE("iface %p, bone_transforms %p, bone_inv_transpose_transforms %p, src_vertices %p, dst_vertices %p\n",
412             skin, bone_transforms, bone_inv_transpose_transforms, src_vertices, dst_vertices);
413 
414     if (bone_inv_transpose_transforms)
415         FIXME("Skinning vertices with two position elements not supported\n");
416 
417     if ((skin->fvf & D3DFVF_POSITION_MASK) != D3DFVF_XYZ) {
418         FIXME("Vertex type %#x not supported\n", skin->fvf & D3DFVF_POSITION_MASK);
419         return E_FAIL;
420     }
421 
422     /* Reset all positions */
423     for (i = 0; i < skin->num_vertices; i++) {
424         D3DXVECTOR3 *position = (D3DXVECTOR3*)((BYTE*)dst_vertices + size * i);
425         position->x = 0.0f;
426         position->y = 0.0f;
427         position->z = 0.0f;
428     }
429 
430     /* Update positions that are influenced by bones */
431     for (i = 0; i < skin->num_bones; i++) {
432         D3DXMATRIX bone_inverse, matrix;
433 
434         D3DXMatrixInverse(&bone_inverse, NULL, &skin->bones[i].transform);
435         D3DXMatrixMultiply(&matrix, &bone_transforms[i], &bone_inverse);
436         D3DXMatrixMultiply(&matrix, &matrix, &skin->bones[i].transform);
437 
438         for (j = 0; j < skin->bones[i].num_influences; j++) {
439             D3DXVECTOR3 position;
440             D3DXVECTOR3 *position_src = (D3DXVECTOR3*)((BYTE*)src_vertices + size * skin->bones[i].vertices[j]);
441             D3DXVECTOR3 *position_dest = (D3DXVECTOR3*)((BYTE*)dst_vertices + size * skin->bones[i].vertices[j]);
442             FLOAT weight = skin->bones[i].weights[j];
443 
444             D3DXVec3TransformCoord(&position, position_src, &matrix);
445             position_dest->x += weight * position.x;
446             position_dest->y += weight * position.y;
447             position_dest->z += weight * position.z;
448         }
449     }
450 
451     if (skin->fvf & D3DFVF_NORMAL) {
452         /* Reset all normals */
453         for (i = 0; i < skin->num_vertices; i++) {
454             D3DXVECTOR3 *normal = (D3DXVECTOR3*)((BYTE*)dst_vertices + size * i + sizeof(D3DXVECTOR3));
455             normal->x = 0.0f;
456             normal->y = 0.0f;
457             normal->z = 0.0f;
458         }
459 
460         /* Update normals that are influenced by bones */
461         for (i = 0; i < skin->num_bones; i++) {
462             D3DXMATRIX bone_inverse, matrix;
463 
464             D3DXMatrixInverse(&bone_inverse, NULL, &skin->bones[i].transform);
465             D3DXMatrixMultiply(&matrix, &skin->bones[i].transform, &bone_transforms[i]);
466 
467             for (j = 0; j < skin->bones[i].num_influences; j++) {
468                 D3DXVECTOR3 normal;
469                 D3DXVECTOR3 *normal_src = (D3DXVECTOR3*)((BYTE*)src_vertices + size * skin->bones[i].vertices[j] + sizeof(D3DXVECTOR3));
470                 D3DXVECTOR3 *normal_dest = (D3DXVECTOR3*)((BYTE*)dst_vertices + size * skin->bones[i].vertices[j] + sizeof(D3DXVECTOR3));
471                 FLOAT weight = skin->bones[i].weights[j];
472 
473                 D3DXVec3TransformNormal(&normal, normal_src, &bone_inverse);
474                 D3DXVec3TransformNormal(&normal, &normal, &matrix);
475                 normal_dest->x += weight * normal.x;
476                 normal_dest->y += weight * normal.y;
477                 normal_dest->z += weight * normal.z;
478             }
479         }
480 
481         /* Normalize all normals that are influenced by bones*/
482         for (i = 0; i < skin->num_vertices; i++) {
483             D3DXVECTOR3 *normal_dest = (D3DXVECTOR3*)((BYTE*)dst_vertices + (i * size) + sizeof(D3DXVECTOR3));
484             if ((normal_dest->x != 0.0f) && (normal_dest->y != 0.0f) && (normal_dest->z != 0.0f))
485                 D3DXVec3Normalize(normal_dest, normal_dest);
486         }
487     }
488 
489     return D3D_OK;
490 }
491 
d3dx9_skin_info_ConvertToBlendedMesh(ID3DXSkinInfo * iface,ID3DXMesh * mesh_in,DWORD options,const DWORD * adjacency_in,DWORD * adjacency_out,DWORD * face_remap,ID3DXBuffer ** vertex_remap,DWORD * max_face_infl,DWORD * num_bone_combinations,ID3DXBuffer ** bone_combination_table,ID3DXMesh ** mesh_out)492 static HRESULT WINAPI d3dx9_skin_info_ConvertToBlendedMesh(ID3DXSkinInfo *iface, ID3DXMesh *mesh_in,
493         DWORD options, const DWORD *adjacency_in, DWORD *adjacency_out, DWORD *face_remap,
494         ID3DXBuffer **vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
495         ID3DXBuffer **bone_combination_table, ID3DXMesh **mesh_out)
496 {
497     FIXME("iface %p, mesh_in %p, options %#x, adjacency_in %p, adjacency_out %p, face_remap %p, vertex_remap %p, "
498             "max_face_infl %p, num_bone_combinations %p, bone_combination_table %p, mesh_out %p stub!\n",
499             iface, mesh_in, options, adjacency_in, adjacency_out, face_remap, vertex_remap,
500             max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
501 
502     return E_NOTIMPL;
503 }
504 
d3dx9_skin_info_ConvertToIndexedBlendedMesh(ID3DXSkinInfo * iface,ID3DXMesh * mesh_in,DWORD options,DWORD palette_size,const DWORD * adjacency_in,DWORD * adjacency_out,DWORD * face_remap,ID3DXBuffer ** vertex_remap,DWORD * max_face_infl,DWORD * num_bone_combinations,ID3DXBuffer ** bone_combination_table,ID3DXMesh ** mesh_out)505 static HRESULT WINAPI d3dx9_skin_info_ConvertToIndexedBlendedMesh(ID3DXSkinInfo *iface, ID3DXMesh *mesh_in,
506         DWORD options, DWORD palette_size, const DWORD *adjacency_in, DWORD *adjacency_out, DWORD *face_remap,
507         ID3DXBuffer **vertex_remap, DWORD *max_face_infl, DWORD *num_bone_combinations,
508         ID3DXBuffer **bone_combination_table, ID3DXMesh **mesh_out)
509 {
510     FIXME("iface %p, mesh_in %p, options %#x, palette_size %u, adjacency_in %p, adjacency_out %p, face_remap %p, vertex_remap %p, "
511             "max_face_infl %p, num_bone_combinations %p, bone_combination_table %p, mesh_out %p stub!\n",
512             iface, mesh_in, options, palette_size, adjacency_in, adjacency_out, face_remap, vertex_remap,
513             max_face_infl, num_bone_combinations, bone_combination_table, mesh_out);
514 
515     return E_NOTIMPL;
516 }
517 
518 static const struct ID3DXSkinInfoVtbl d3dx9_skin_info_vtbl =
519 {
520     d3dx9_skin_info_QueryInterface,
521     d3dx9_skin_info_AddRef,
522     d3dx9_skin_info_Release,
523     d3dx9_skin_info_SetBoneInfluence,
524     d3dx9_skin_info_SetBoneVertexInfluence,
525     d3dx9_skin_info_GetNumBoneInfluences,
526     d3dx9_skin_info_GetBoneInfluence,
527     d3dx9_skin_info_GetBoneVertexInfluence,
528     d3dx9_skin_info_GetMaxVertexInfluences,
529     d3dx9_skin_info_GetNumBones,
530     d3dx9_skin_info_FindBoneVertexInfluenceIndex,
531     d3dx9_skin_info_GetMaxFaceInfluences,
532     d3dx9_skin_info_SetMinBoneInfluence,
533     d3dx9_skin_info_GetMinBoneInfluence,
534     d3dx9_skin_info_SetBoneName,
535     d3dx9_skin_info_GetBoneName,
536     d3dx9_skin_info_SetBoneOffsetMatrix,
537     d3dx9_skin_info_GetBoneOffsetMatrix,
538     d3dx9_skin_info_Clone,
539     d3dx9_skin_info_Remap,
540     d3dx9_skin_info_SetFVF,
541     d3dx9_skin_info_SetDeclaration,
542     d3dx9_skin_info_GetFVF,
543     d3dx9_skin_info_GetDeclaration,
544     d3dx9_skin_info_UpdateSkinnedMesh,
545     d3dx9_skin_info_ConvertToBlendedMesh,
546     d3dx9_skin_info_ConvertToIndexedBlendedMesh,
547 };
548 
D3DXCreateSkinInfo(DWORD num_vertices,const D3DVERTEXELEMENT9 * declaration,DWORD num_bones,ID3DXSkinInfo ** skin_info)549 HRESULT WINAPI D3DXCreateSkinInfo(DWORD num_vertices, const D3DVERTEXELEMENT9 *declaration,
550         DWORD num_bones, ID3DXSkinInfo **skin_info)
551 {
552     HRESULT hr;
553     static const D3DVERTEXELEMENT9 empty_declaration = D3DDECL_END();
554     struct d3dx9_skin_info *object = NULL;
555 
556     TRACE("num_vertices %u, declaration %p, num_bones %u, skin_info %p.\n",
557             num_vertices, declaration, num_bones, skin_info);
558 
559     if (!skin_info || !declaration)
560         return D3DERR_INVALIDCALL;
561 
562     object = HeapAlloc(GetProcessHeap(), 0, sizeof(*object));
563     if (!object)
564         return E_OUTOFMEMORY;
565 
566     object->ID3DXSkinInfo_iface.lpVtbl = &d3dx9_skin_info_vtbl;
567     object->ref = 1;
568     object->num_vertices = num_vertices;
569     object->num_bones = num_bones;
570     object->vertex_declaration[0] = empty_declaration;
571     object->fvf = 0;
572 
573     object->bones = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_bones * sizeof(*object->bones));
574     if (!object->bones) {
575         hr = E_OUTOFMEMORY;
576         goto error;
577     }
578 
579     if (FAILED(hr = d3dx9_skin_info_SetDeclaration(&object->ID3DXSkinInfo_iface, declaration)))
580         goto error;
581 
582     *skin_info = &object->ID3DXSkinInfo_iface;
583 
584     return D3D_OK;
585 error:
586     HeapFree(GetProcessHeap(), 0, object->bones);
587     HeapFree(GetProcessHeap(), 0, object);
588     return hr;
589 }
590 
D3DXCreateSkinInfoFVF(DWORD num_vertices,DWORD fvf,DWORD num_bones,ID3DXSkinInfo ** skin_info)591 HRESULT WINAPI D3DXCreateSkinInfoFVF(DWORD num_vertices, DWORD fvf, DWORD num_bones, ID3DXSkinInfo **skin_info)
592 {
593     HRESULT hr;
594     D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
595 
596     TRACE("(%u, %x, %u, %p)\n", num_vertices, fvf, num_bones, skin_info);
597 
598     hr = D3DXDeclaratorFromFVF(fvf, declaration);
599     if (FAILED(hr))
600         return hr;
601 
602     return D3DXCreateSkinInfo(num_vertices, declaration, num_bones, skin_info);
603 }
604