* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / directx / wine / d3dx9_36 / mesh.c
1 /*
2 * Mesh operations specific to D3DX9.
3 *
4 * Copyright (C) 2005 Henri Verbeet
5 * Copyright (C) 2006 Ivan Gyurdiev
6 * Copyright (C) 2009 David Adam
7 * Copyright (C) 2010 Tony Wasserka
8 * Copyright (C) 2011 Dylan Smith
9 * Copyright (C) 2011 Michael Mc Donnell
10 * Copyright (C) 2013 Christian Costa
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27 #include "d3dx9_36_private.h"
28
29 #include <assert.h>
30 #ifdef HAVE_FLOAT_H
31 # include <float.h>
32 #endif
33
34 #undef MAKE_DDHRESULT
35 #include "dxfile.h"
36 #include "rmxfguid.h"
37 #include "rmxftmpl.h"
38 #include "wine/list.h"
39
40 struct d3dx9_mesh
41 {
42 ID3DXMesh ID3DXMesh_iface;
43 LONG ref;
44
45 DWORD numfaces;
46 DWORD numvertices;
47 DWORD options;
48 DWORD fvf;
49 IDirect3DDevice9 *device;
50 D3DVERTEXELEMENT9 cached_declaration[MAX_FVF_DECL_SIZE];
51 IDirect3DVertexDeclaration9 *vertex_declaration;
52 UINT vertex_declaration_size;
53 UINT num_elem;
54 IDirect3DVertexBuffer9 *vertex_buffer;
55 IDirect3DIndexBuffer9 *index_buffer;
56 DWORD *attrib_buffer;
57 int attrib_buffer_lock_count;
58 DWORD attrib_table_size;
59 D3DXATTRIBUTERANGE *attrib_table;
60 };
61
62 const UINT d3dx_decltype_size[] =
63 {
64 /* D3DDECLTYPE_FLOAT1 */ sizeof(FLOAT),
65 /* D3DDECLTYPE_FLOAT2 */ sizeof(D3DXVECTOR2),
66 /* D3DDECLTYPE_FLOAT3 */ sizeof(D3DXVECTOR3),
67 /* D3DDECLTYPE_FLOAT4 */ sizeof(D3DXVECTOR4),
68 /* D3DDECLTYPE_D3DCOLOR */ sizeof(D3DCOLOR),
69 /* D3DDECLTYPE_UBYTE4 */ 4 * sizeof(BYTE),
70 /* D3DDECLTYPE_SHORT2 */ 2 * sizeof(SHORT),
71 /* D3DDECLTYPE_SHORT4 */ 4 * sizeof(SHORT),
72 /* D3DDECLTYPE_UBYTE4N */ 4 * sizeof(BYTE),
73 /* D3DDECLTYPE_SHORT2N */ 2 * sizeof(SHORT),
74 /* D3DDECLTYPE_SHORT4N */ 4 * sizeof(SHORT),
75 /* D3DDECLTYPE_USHORT2N */ 2 * sizeof(USHORT),
76 /* D3DDECLTYPE_USHORT4N */ 4 * sizeof(USHORT),
77 /* D3DDECLTYPE_UDEC3 */ 4, /* 3 * 10 bits + 2 padding */
78 /* D3DDECLTYPE_DEC3N */ 4,
79 /* D3DDECLTYPE_FLOAT16_2 */ 2 * sizeof(D3DXFLOAT16),
80 /* D3DDECLTYPE_FLOAT16_4 */ 4 * sizeof(D3DXFLOAT16),
81 };
82
83 static inline struct d3dx9_mesh *impl_from_ID3DXMesh(ID3DXMesh *iface)
84 {
85 return CONTAINING_RECORD(iface, struct d3dx9_mesh, ID3DXMesh_iface);
86 }
87
88 static HRESULT WINAPI d3dx9_mesh_QueryInterface(ID3DXMesh *iface, REFIID riid, void **out)
89 {
90 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
91
92 if (IsEqualGUID(riid, &IID_IUnknown) ||
93 IsEqualGUID(riid, &IID_ID3DXBaseMesh) ||
94 IsEqualGUID(riid, &IID_ID3DXMesh))
95 {
96 iface->lpVtbl->AddRef(iface);
97 *out = iface;
98 return S_OK;
99 }
100
101 WARN("Interface %s not found.\n", debugstr_guid(riid));
102
103 return E_NOINTERFACE;
104 }
105
106 static ULONG WINAPI d3dx9_mesh_AddRef(ID3DXMesh *iface)
107 {
108 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
109 ULONG refcount = InterlockedIncrement(&mesh->ref);
110
111 TRACE("%p increasing refcount to %u.\n", mesh, refcount);
112
113 return refcount;
114 }
115
116 static ULONG WINAPI d3dx9_mesh_Release(ID3DXMesh *iface)
117 {
118 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
119 ULONG refcount = InterlockedDecrement(&mesh->ref);
120
121 TRACE("%p decreasing refcount to %u.\n", mesh, refcount);
122
123 if (!refcount)
124 {
125 IDirect3DIndexBuffer9_Release(mesh->index_buffer);
126 IDirect3DVertexBuffer9_Release(mesh->vertex_buffer);
127 if (mesh->vertex_declaration)
128 IDirect3DVertexDeclaration9_Release(mesh->vertex_declaration);
129 IDirect3DDevice9_Release(mesh->device);
130 HeapFree(GetProcessHeap(), 0, mesh->attrib_buffer);
131 HeapFree(GetProcessHeap(), 0, mesh->attrib_table);
132 HeapFree(GetProcessHeap(), 0, mesh);
133 }
134
135 return refcount;
136 }
137
138 static HRESULT WINAPI d3dx9_mesh_DrawSubset(ID3DXMesh *iface, DWORD attrib_id)
139 {
140 struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
141 HRESULT hr;
142 DWORD face_start;
143 DWORD face_end = 0;
144 DWORD vertex_size;
145
146 TRACE("iface %p, attrib_id %u.\n", iface, attrib_id);
147
148 if (!This->vertex_declaration)
149 {
150 WARN("Can't draw a mesh with an invalid vertex declaration.\n");
151 return E_FAIL;
152 }
153
154 vertex_size = iface->lpVtbl->GetNumBytesPerVertex(iface);
155
156 hr = IDirect3DDevice9_SetVertexDeclaration(This->device, This->vertex_declaration);
157 if (FAILED(hr)) return hr;
158 hr = IDirect3DDevice9_SetStreamSource(This->device, 0, This->vertex_buffer, 0, vertex_size);
159 if (FAILED(hr)) return hr;
160 hr = IDirect3DDevice9_SetIndices(This->device, This->index_buffer);
161 if (FAILED(hr)) return hr;
162
163 while (face_end < This->numfaces)
164 {
165 for (face_start = face_end; face_start < This->numfaces; face_start++)
166 {
167 if (This->attrib_buffer[face_start] == attrib_id)
168 break;
169 }
170 if (face_start >= This->numfaces)
171 break;
172 for (face_end = face_start + 1; face_end < This->numfaces; face_end++)
173 {
174 if (This->attrib_buffer[face_end] != attrib_id)
175 break;
176 }
177
178 hr = IDirect3DDevice9_DrawIndexedPrimitive(This->device, D3DPT_TRIANGLELIST,
179 0, 0, This->numvertices, face_start * 3, face_end - face_start);
180 if (FAILED(hr)) return hr;
181 }
182
183 return D3D_OK;
184 }
185
186 static DWORD WINAPI d3dx9_mesh_GetNumFaces(ID3DXMesh *iface)
187 {
188 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
189
190 TRACE("iface %p.\n", iface);
191
192 return mesh->numfaces;
193 }
194
195 static DWORD WINAPI d3dx9_mesh_GetNumVertices(ID3DXMesh *iface)
196 {
197 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
198
199 TRACE("iface %p.\n", iface);
200
201 return mesh->numvertices;
202 }
203
204 static DWORD WINAPI d3dx9_mesh_GetFVF(ID3DXMesh *iface)
205 {
206 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
207
208 TRACE("iface %p.\n", iface);
209
210 return mesh->fvf;
211 }
212
213 static void copy_declaration(D3DVERTEXELEMENT9 *dst, const D3DVERTEXELEMENT9 *src, UINT num_elem)
214 {
215 memcpy(dst, src, num_elem * sizeof(*src));
216 }
217
218 static HRESULT WINAPI d3dx9_mesh_GetDeclaration(ID3DXMesh *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
219 {
220 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
221
222 TRACE("iface %p, declaration %p.\n", iface, declaration);
223
224 if (!declaration)
225 return D3DERR_INVALIDCALL;
226
227 copy_declaration(declaration, mesh->cached_declaration, mesh->num_elem);
228
229 return D3D_OK;
230 }
231
232 static DWORD WINAPI d3dx9_mesh_GetNumBytesPerVertex(ID3DXMesh *iface)
233 {
234 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
235
236 TRACE("iface %p.\n", iface);
237
238 return mesh->vertex_declaration_size;
239 }
240
241 static DWORD WINAPI d3dx9_mesh_GetOptions(ID3DXMesh *iface)
242 {
243 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
244
245 TRACE("iface %p.\n", iface);
246
247 return mesh->options;
248 }
249
250 static HRESULT WINAPI d3dx9_mesh_GetDevice(struct ID3DXMesh *iface, struct IDirect3DDevice9 **device)
251 {
252 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
253
254 TRACE("iface %p, device %p.\n", iface, device);
255
256 if (!device)
257 return D3DERR_INVALIDCALL;
258 *device = mesh->device;
259 IDirect3DDevice9_AddRef(mesh->device);
260
261 return D3D_OK;
262 }
263
264 static HRESULT WINAPI d3dx9_mesh_CloneMeshFVF(struct ID3DXMesh *iface, DWORD options, DWORD fvf,
265 struct IDirect3DDevice9 *device, struct ID3DXMesh **clone_mesh)
266 {
267 HRESULT hr;
268 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
269
270 TRACE("iface %p, options %#x, fvf %#x, device %p, clone_mesh %p.\n",
271 iface, options, fvf, device, clone_mesh);
272
273 if (FAILED(hr = D3DXDeclaratorFromFVF(fvf, declaration)))
274 return hr;
275
276 return iface->lpVtbl->CloneMesh(iface, options, declaration, device, clone_mesh);
277 }
278
279 static FLOAT scale_clamp_ubyten(FLOAT value)
280 {
281 value = value * UCHAR_MAX;
282
283 if (value < 0.0f)
284 {
285 return 0.0f;
286 }
287 else
288 {
289 if (value > UCHAR_MAX) /* Clamp at 255 */
290 return UCHAR_MAX;
291 else
292 return value;
293 }
294 }
295
296 static FLOAT scale_clamp_shortn(FLOAT value)
297 {
298 value = value * SHRT_MAX;
299
300 /* The tests show that the range is SHRT_MIN + 1 to SHRT_MAX. */
301 if (value <= SHRT_MIN)
302 {
303 return SHRT_MIN + 1;
304 }
305 else if (value > SHRT_MAX)
306 {
307 return SHRT_MAX;
308 }
309 else
310 {
311 return value;
312 }
313 }
314
315 static FLOAT scale_clamp_ushortn(FLOAT value)
316 {
317 value = value * USHRT_MAX;
318
319 if (value < 0.0f)
320 {
321 return 0.0f;
322 }
323 else
324 {
325 if (value > USHRT_MAX) /* Clamp at 65535 */
326 return USHRT_MAX;
327 else
328 return value;
329 }
330 }
331
332 static INT simple_round(FLOAT value)
333 {
334 int res = (INT)(value + 0.5f);
335
336 return res;
337 }
338
339 static void convert_float4(BYTE *dst, const D3DXVECTOR4 *src, D3DDECLTYPE type_dst)
340 {
341 BOOL fixme_once = FALSE;
342
343 switch (type_dst)
344 {
345 case D3DDECLTYPE_FLOAT1:
346 {
347 FLOAT *dst_ptr = (FLOAT*)dst;
348 *dst_ptr = src->x;
349 break;
350 }
351 case D3DDECLTYPE_FLOAT2:
352 {
353 D3DXVECTOR2 *dst_ptr = (D3DXVECTOR2*)dst;
354 dst_ptr->x = src->x;
355 dst_ptr->y = src->y;
356 break;
357 }
358 case D3DDECLTYPE_FLOAT3:
359 {
360 D3DXVECTOR3 *dst_ptr = (D3DXVECTOR3*)dst;
361 dst_ptr->x = src->x;
362 dst_ptr->y = src->y;
363 dst_ptr->z = src->z;
364 break;
365 }
366 case D3DDECLTYPE_FLOAT4:
367 {
368 D3DXVECTOR4 *dst_ptr = (D3DXVECTOR4*)dst;
369 dst_ptr->x = src->x;
370 dst_ptr->y = src->y;
371 dst_ptr->z = src->z;
372 dst_ptr->w = src->w;
373 break;
374 }
375 case D3DDECLTYPE_D3DCOLOR:
376 {
377 dst[0] = (BYTE)simple_round(scale_clamp_ubyten(src->z));
378 dst[1] = (BYTE)simple_round(scale_clamp_ubyten(src->y));
379 dst[2] = (BYTE)simple_round(scale_clamp_ubyten(src->x));
380 dst[3] = (BYTE)simple_round(scale_clamp_ubyten(src->w));
381 break;
382 }
383 case D3DDECLTYPE_UBYTE4:
384 {
385 dst[0] = src->x < 0.0f ? 0 : (BYTE)simple_round(src->x);
386 dst[1] = src->y < 0.0f ? 0 : (BYTE)simple_round(src->y);
387 dst[2] = src->z < 0.0f ? 0 : (BYTE)simple_round(src->z);
388 dst[3] = src->w < 0.0f ? 0 : (BYTE)simple_round(src->w);
389 break;
390 }
391 case D3DDECLTYPE_SHORT2:
392 {
393 SHORT *dst_ptr = (SHORT*)dst;
394 dst_ptr[0] = (SHORT)simple_round(src->x);
395 dst_ptr[1] = (SHORT)simple_round(src->y);
396 break;
397 }
398 case D3DDECLTYPE_SHORT4:
399 {
400 SHORT *dst_ptr = (SHORT*)dst;
401 dst_ptr[0] = (SHORT)simple_round(src->x);
402 dst_ptr[1] = (SHORT)simple_round(src->y);
403 dst_ptr[2] = (SHORT)simple_round(src->z);
404 dst_ptr[3] = (SHORT)simple_round(src->w);
405 break;
406 }
407 case D3DDECLTYPE_UBYTE4N:
408 {
409 dst[0] = (BYTE)simple_round(scale_clamp_ubyten(src->x));
410 dst[1] = (BYTE)simple_round(scale_clamp_ubyten(src->y));
411 dst[2] = (BYTE)simple_round(scale_clamp_ubyten(src->z));
412 dst[3] = (BYTE)simple_round(scale_clamp_ubyten(src->w));
413 break;
414 }
415 case D3DDECLTYPE_SHORT2N:
416 {
417 SHORT *dst_ptr = (SHORT*)dst;
418 dst_ptr[0] = (SHORT)simple_round(scale_clamp_shortn(src->x));
419 dst_ptr[1] = (SHORT)simple_round(scale_clamp_shortn(src->y));
420 break;
421 }
422 case D3DDECLTYPE_SHORT4N:
423 {
424 SHORT *dst_ptr = (SHORT*)dst;
425 dst_ptr[0] = (SHORT)simple_round(scale_clamp_shortn(src->x));
426 dst_ptr[1] = (SHORT)simple_round(scale_clamp_shortn(src->y));
427 dst_ptr[2] = (SHORT)simple_round(scale_clamp_shortn(src->z));
428 dst_ptr[3] = (SHORT)simple_round(scale_clamp_shortn(src->w));
429 break;
430 }
431 case D3DDECLTYPE_USHORT2N:
432 {
433 USHORT *dst_ptr = (USHORT*)dst;
434 dst_ptr[0] = (USHORT)simple_round(scale_clamp_ushortn(src->x));
435 dst_ptr[1] = (USHORT)simple_round(scale_clamp_ushortn(src->y));
436 break;
437 }
438 case D3DDECLTYPE_USHORT4N:
439 {
440 USHORT *dst_ptr = (USHORT*)dst;
441 dst_ptr[0] = (USHORT)simple_round(scale_clamp_ushortn(src->x));
442 dst_ptr[1] = (USHORT)simple_round(scale_clamp_ushortn(src->y));
443 dst_ptr[2] = (USHORT)simple_round(scale_clamp_ushortn(src->z));
444 dst_ptr[3] = (USHORT)simple_round(scale_clamp_ushortn(src->w));
445 break;
446 }
447 case D3DDECLTYPE_FLOAT16_2:
448 {
449 D3DXFloat32To16Array((D3DXFLOAT16*)dst, (FLOAT*)src, 2);
450 break;
451 }
452 case D3DDECLTYPE_FLOAT16_4:
453 {
454 D3DXFloat32To16Array((D3DXFLOAT16*)dst, (FLOAT*)src, 4);
455 break;
456 }
457 default:
458 if (!fixme_once++)
459 FIXME("Conversion from D3DDECLTYPE_FLOAT4 to %d not implemented.\n", type_dst);
460 break;
461 }
462 }
463
464 static void convert_component(BYTE *dst, BYTE *src, D3DDECLTYPE type_dst, D3DDECLTYPE type_src)
465 {
466 BOOL fixme_once = FALSE;
467
468 switch (type_src)
469 {
470 case D3DDECLTYPE_FLOAT1:
471 {
472 FLOAT *src_ptr = (FLOAT*)src;
473 D3DXVECTOR4 src_float4 = {*src_ptr, 0.0f, 0.0f, 1.0f};
474 convert_float4(dst, &src_float4, type_dst);
475 break;
476 }
477 case D3DDECLTYPE_FLOAT2:
478 {
479 D3DXVECTOR2 *src_ptr = (D3DXVECTOR2*)src;
480 D3DXVECTOR4 src_float4 = {src_ptr->x, src_ptr->y, 0.0f, 1.0f};
481 convert_float4(dst, &src_float4, type_dst);
482 break;
483 }
484 case D3DDECLTYPE_FLOAT3:
485 {
486 D3DXVECTOR3 *src_ptr = (D3DXVECTOR3*)src;
487 D3DXVECTOR4 src_float4 = {src_ptr->x, src_ptr->y, src_ptr->z, 1.0f};
488 convert_float4(dst, &src_float4, type_dst);
489 break;
490 }
491 case D3DDECLTYPE_FLOAT4:
492 {
493 D3DXVECTOR4 *src_ptr = (D3DXVECTOR4*)src;
494 D3DXVECTOR4 src_float4 = {src_ptr->x, src_ptr->y, src_ptr->z, src_ptr->w};
495 convert_float4(dst, &src_float4, type_dst);
496 break;
497 }
498 case D3DDECLTYPE_D3DCOLOR:
499 {
500 D3DXVECTOR4 src_float4 =
501 {
502 (FLOAT)src[2]/UCHAR_MAX,
503 (FLOAT)src[1]/UCHAR_MAX,
504 (FLOAT)src[0]/UCHAR_MAX,
505 (FLOAT)src[3]/UCHAR_MAX
506 };
507 convert_float4(dst, &src_float4, type_dst);
508 break;
509 }
510 case D3DDECLTYPE_UBYTE4:
511 {
512 D3DXVECTOR4 src_float4 = {src[0], src[1], src[2], src[3]};
513 convert_float4(dst, &src_float4, type_dst);
514 break;
515 }
516 case D3DDECLTYPE_SHORT2:
517 {
518 SHORT *src_ptr = (SHORT*)src;
519 D3DXVECTOR4 src_float4 = {src_ptr[0], src_ptr[1], 0.0f, 1.0f};
520 convert_float4(dst, &src_float4, type_dst);
521 break;
522 }
523 case D3DDECLTYPE_SHORT4:
524 {
525 SHORT *src_ptr = (SHORT*)src;
526 D3DXVECTOR4 src_float4 = {src_ptr[0], src_ptr[1], src_ptr[2], src_ptr[3]};
527 convert_float4(dst, &src_float4, type_dst);
528 break;
529 }
530 case D3DDECLTYPE_UBYTE4N:
531 {
532 D3DXVECTOR4 src_float4 =
533 {
534 (FLOAT)src[0]/UCHAR_MAX,
535 (FLOAT)src[1]/UCHAR_MAX,
536 (FLOAT)src[2]/UCHAR_MAX,
537 (FLOAT)src[3]/UCHAR_MAX
538 };
539 convert_float4(dst, &src_float4, type_dst);
540 break;
541 }
542 case D3DDECLTYPE_SHORT2N:
543 {
544 SHORT *src_ptr = (SHORT*)src;
545 D3DXVECTOR4 src_float4 = {(FLOAT)src_ptr[0]/SHRT_MAX, (FLOAT)src_ptr[1]/SHRT_MAX, 0.0f, 1.0f};
546 convert_float4(dst, &src_float4, type_dst);
547 break;
548 }
549 case D3DDECLTYPE_SHORT4N:
550 {
551 SHORT *src_ptr = (SHORT*)src;
552 D3DXVECTOR4 src_float4 =
553 {
554 (FLOAT)src_ptr[0]/SHRT_MAX,
555 (FLOAT)src_ptr[1]/SHRT_MAX,
556 (FLOAT)src_ptr[2]/SHRT_MAX,
557 (FLOAT)src_ptr[3]/SHRT_MAX
558 };
559 convert_float4(dst, &src_float4, type_dst);
560 break;
561 }
562 case D3DDECLTYPE_FLOAT16_2:
563 {
564 D3DXVECTOR4 src_float4 = {0.0f, 0.0f, 0.0f, 1.0f};
565 D3DXFloat16To32Array((FLOAT*)&src_float4, (D3DXFLOAT16*)src, 2);
566 convert_float4(dst, &src_float4, type_dst);
567 break;
568 }
569 case D3DDECLTYPE_FLOAT16_4:
570 {
571 D3DXVECTOR4 src_float4;
572 D3DXFloat16To32Array((FLOAT*)&src_float4, (D3DXFLOAT16*)src, 4);
573 convert_float4(dst, &src_float4, type_dst);
574 break;
575 }
576 default:
577 if (!fixme_once++)
578 FIXME("Conversion of D3DDECLTYPE %d to %d not implemented.\n", type_src, type_dst);
579 break;
580 }
581 }
582
583 static INT get_equivalent_declaration_index(D3DVERTEXELEMENT9 orig_declaration, D3DVERTEXELEMENT9 *declaration)
584 {
585 INT i;
586
587 for (i = 0; declaration[i].Stream != 0xff; i++)
588 {
589 if (orig_declaration.Usage == declaration[i].Usage
590 && orig_declaration.UsageIndex == declaration[i].UsageIndex)
591 {
592 return i;
593 }
594 }
595
596 return -1;
597 }
598
599 static HRESULT convert_vertex_buffer(ID3DXMesh *mesh_dst, ID3DXMesh *mesh_src)
600 {
601 HRESULT hr;
602 D3DVERTEXELEMENT9 orig_declaration[MAX_FVF_DECL_SIZE] = {D3DDECL_END()};
603 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE] = {D3DDECL_END()};
604 BYTE *vb_dst = NULL;
605 BYTE *vb_src = NULL;
606 UINT i;
607 UINT num_vertices = mesh_src->lpVtbl->GetNumVertices(mesh_src);
608 UINT dst_vertex_size = mesh_dst->lpVtbl->GetNumBytesPerVertex(mesh_dst);
609 UINT src_vertex_size = mesh_src->lpVtbl->GetNumBytesPerVertex(mesh_src);
610
611 hr = mesh_src->lpVtbl->GetDeclaration(mesh_src, orig_declaration);
612 if (FAILED(hr)) return hr;
613 hr = mesh_dst->lpVtbl->GetDeclaration(mesh_dst, declaration);
614 if (FAILED(hr)) return hr;
615
616 hr = mesh_src->lpVtbl->LockVertexBuffer(mesh_src, D3DLOCK_READONLY, (void**)&vb_src);
617 if (FAILED(hr)) goto cleanup;
618 hr = mesh_dst->lpVtbl->LockVertexBuffer(mesh_dst, 0, (void**)&vb_dst);
619 if (FAILED(hr)) goto cleanup;
620
621 /* Clear all new fields by clearing the entire vertex buffer. */
622 memset(vb_dst, 0, num_vertices * dst_vertex_size);
623
624 for (i = 0; orig_declaration[i].Stream != 0xff; i++)
625 {
626 INT eq_idx = get_equivalent_declaration_index(orig_declaration[i], declaration);
627
628 if (eq_idx >= 0)
629 {
630 UINT j;
631 for (j = 0; j < num_vertices; j++)
632 {
633 UINT idx_dst = dst_vertex_size * j + declaration[eq_idx].Offset;
634 UINT idx_src = src_vertex_size * j + orig_declaration[i].Offset;
635 UINT type_size = d3dx_decltype_size[orig_declaration[i].Type];
636
637 if (orig_declaration[i].Type == declaration[eq_idx].Type)
638 memcpy(&vb_dst[idx_dst], &vb_src[idx_src], type_size);
639 else
640 convert_component(&vb_dst[idx_dst], &vb_src[idx_src], declaration[eq_idx].Type, orig_declaration[i].Type);
641 }
642 }
643 }
644
645 hr = D3D_OK;
646 cleanup:
647 if (vb_dst) mesh_dst->lpVtbl->UnlockVertexBuffer(mesh_dst);
648 if (vb_src) mesh_src->lpVtbl->UnlockVertexBuffer(mesh_src);
649
650 return hr;
651 }
652
653 static BOOL declaration_equals(const D3DVERTEXELEMENT9 *declaration1, const D3DVERTEXELEMENT9 *declaration2)
654 {
655 UINT size1 = 0, size2 = 0;
656
657 /* Find the size of each declaration */
658 while (declaration1[size1].Stream != 0xff) size1++;
659 while (declaration2[size2].Stream != 0xff) size2++;
660
661 /* If not same size then they are definitely not equal */
662 if (size1 != size2)
663 return FALSE;
664
665 /* Check that all components are the same */
666 if (memcmp(declaration1, declaration2, size1*sizeof(*declaration1)) == 0)
667 return TRUE;
668
669 return FALSE;
670 }
671
672 static HRESULT WINAPI d3dx9_mesh_CloneMesh(struct ID3DXMesh *iface, DWORD options,
673 const D3DVERTEXELEMENT9 *declaration, struct IDirect3DDevice9 *device, struct ID3DXMesh **clone_mesh_out)
674 {
675 struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
676 struct d3dx9_mesh *cloned_this;
677 ID3DXMesh *clone_mesh;
678 D3DVERTEXELEMENT9 orig_declaration[MAX_FVF_DECL_SIZE] = { D3DDECL_END() };
679 void *data_in, *data_out;
680 DWORD vertex_size;
681 HRESULT hr;
682 BOOL same_declaration;
683
684 TRACE("iface %p, options %#x, declaration %p, device %p, clone_mesh_out %p.\n",
685 iface, options, declaration, device, clone_mesh_out);
686
687 if (!clone_mesh_out)
688 return D3DERR_INVALIDCALL;
689
690 hr = iface->lpVtbl->GetDeclaration(iface, orig_declaration);
691 if (FAILED(hr)) return hr;
692
693 hr = D3DXCreateMesh(This->numfaces, This->numvertices, options & ~D3DXMESH_VB_SHARE,
694 declaration, device, &clone_mesh);
695 if (FAILED(hr)) return hr;
696
697 cloned_this = impl_from_ID3DXMesh(clone_mesh);
698 vertex_size = clone_mesh->lpVtbl->GetNumBytesPerVertex(clone_mesh);
699 same_declaration = declaration_equals(declaration, orig_declaration);
700
701 if (options & D3DXMESH_VB_SHARE) {
702 if (!same_declaration) {
703 hr = D3DERR_INVALIDCALL;
704 goto error;
705 }
706 IDirect3DVertexBuffer9_AddRef(This->vertex_buffer);
707 /* FIXME: refactor to avoid creating a new vertex buffer */
708 IDirect3DVertexBuffer9_Release(cloned_this->vertex_buffer);
709 cloned_this->vertex_buffer = This->vertex_buffer;
710 } else if (same_declaration) {
711 hr = iface->lpVtbl->LockVertexBuffer(iface, D3DLOCK_READONLY, &data_in);
712 if (FAILED(hr)) goto error;
713 hr = clone_mesh->lpVtbl->LockVertexBuffer(clone_mesh, 0, &data_out);
714 if (FAILED(hr)) {
715 iface->lpVtbl->UnlockVertexBuffer(iface);
716 goto error;
717 }
718 memcpy(data_out, data_in, This->numvertices * vertex_size);
719 clone_mesh->lpVtbl->UnlockVertexBuffer(clone_mesh);
720 iface->lpVtbl->UnlockVertexBuffer(iface);
721 } else {
722 hr = convert_vertex_buffer(clone_mesh, iface);
723 if (FAILED(hr)) goto error;
724 }
725
726 hr = iface->lpVtbl->LockIndexBuffer(iface, D3DLOCK_READONLY, &data_in);
727 if (FAILED(hr)) goto error;
728 hr = clone_mesh->lpVtbl->LockIndexBuffer(clone_mesh, 0, &data_out);
729 if (FAILED(hr)) {
730 iface->lpVtbl->UnlockIndexBuffer(iface);
731 goto error;
732 }
733 if ((options ^ This->options) & D3DXMESH_32BIT) {
734 DWORD i;
735 if (options & D3DXMESH_32BIT) {
736 for (i = 0; i < This->numfaces * 3; i++)
737 ((DWORD*)data_out)[i] = ((WORD*)data_in)[i];
738 } else {
739 for (i = 0; i < This->numfaces * 3; i++)
740 ((WORD*)data_out)[i] = ((DWORD*)data_in)[i];
741 }
742 } else {
743 memcpy(data_out, data_in, This->numfaces * 3 * (options & D3DXMESH_32BIT ? 4 : 2));
744 }
745 clone_mesh->lpVtbl->UnlockIndexBuffer(clone_mesh);
746 iface->lpVtbl->UnlockIndexBuffer(iface);
747
748 memcpy(cloned_this->attrib_buffer, This->attrib_buffer, This->numfaces * sizeof(*This->attrib_buffer));
749
750 if (This->attrib_table_size)
751 {
752 cloned_this->attrib_table_size = This->attrib_table_size;
753 cloned_this->attrib_table = HeapAlloc(GetProcessHeap(), 0, This->attrib_table_size * sizeof(*This->attrib_table));
754 if (!cloned_this->attrib_table) {
755 hr = E_OUTOFMEMORY;
756 goto error;
757 }
758 memcpy(cloned_this->attrib_table, This->attrib_table, This->attrib_table_size * sizeof(*This->attrib_table));
759 }
760
761 *clone_mesh_out = clone_mesh;
762
763 return D3D_OK;
764 error:
765 IUnknown_Release(clone_mesh);
766 return hr;
767 }
768
769 static HRESULT WINAPI d3dx9_mesh_GetVertexBuffer(struct ID3DXMesh *iface,
770 struct IDirect3DVertexBuffer9 **vertex_buffer)
771 {
772 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
773
774 TRACE("iface %p, vertex_buffer %p.\n", iface, vertex_buffer);
775
776 if (!vertex_buffer)
777 return D3DERR_INVALIDCALL;
778 *vertex_buffer = mesh->vertex_buffer;
779 IDirect3DVertexBuffer9_AddRef(mesh->vertex_buffer);
780
781 return D3D_OK;
782 }
783
784 static HRESULT WINAPI d3dx9_mesh_GetIndexBuffer(struct ID3DXMesh *iface,
785 struct IDirect3DIndexBuffer9 **index_buffer)
786 {
787 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
788
789 TRACE("iface %p, index_buffer %p.\n", iface, index_buffer);
790
791 if (!index_buffer)
792 return D3DERR_INVALIDCALL;
793 *index_buffer = mesh->index_buffer;
794 IDirect3DIndexBuffer9_AddRef(mesh->index_buffer);
795
796 return D3D_OK;
797 }
798
799 static HRESULT WINAPI d3dx9_mesh_LockVertexBuffer(ID3DXMesh *iface, DWORD flags, void **data)
800 {
801 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
802
803 TRACE("iface %p, flags %#x, data %p.\n", iface, flags, data);
804
805 return IDirect3DVertexBuffer9_Lock(mesh->vertex_buffer, 0, 0, data, flags);
806 }
807
808 static HRESULT WINAPI d3dx9_mesh_UnlockVertexBuffer(ID3DXMesh *iface)
809 {
810 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
811
812 TRACE("iface %p.\n", iface);
813
814 return IDirect3DVertexBuffer9_Unlock(mesh->vertex_buffer);
815 }
816
817 static HRESULT WINAPI d3dx9_mesh_LockIndexBuffer(ID3DXMesh *iface, DWORD flags, void **data)
818 {
819 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
820
821 TRACE("iface %p, flags %#x, data %p.\n", iface, flags, data);
822
823 return IDirect3DIndexBuffer9_Lock(mesh->index_buffer, 0, 0, data, flags);
824 }
825
826 static HRESULT WINAPI d3dx9_mesh_UnlockIndexBuffer(ID3DXMesh *iface)
827 {
828 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
829
830 TRACE("iface %p.\n", iface);
831
832 return IDirect3DIndexBuffer9_Unlock(mesh->index_buffer);
833 }
834
835 /* FIXME: This looks just wrong, we never check *attrib_table_size before
836 * copying the data. */
837 static HRESULT WINAPI d3dx9_mesh_GetAttributeTable(ID3DXMesh *iface,
838 D3DXATTRIBUTERANGE *attrib_table, DWORD *attrib_table_size)
839 {
840 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
841
842 TRACE("iface %p, attrib_table %p, attrib_table_size %p.\n",
843 iface, attrib_table, attrib_table_size);
844
845 if (attrib_table_size)
846 *attrib_table_size = mesh->attrib_table_size;
847
848 if (attrib_table)
849 memcpy(attrib_table, mesh->attrib_table, mesh->attrib_table_size * sizeof(*attrib_table));
850
851 return D3D_OK;
852 }
853
854 struct edge_face
855 {
856 struct list entry;
857 DWORD v2;
858 DWORD face;
859 };
860
861 struct edge_face_map
862 {
863 struct list *lists;
864 struct edge_face *entries;
865 };
866
867 /* Builds up a map of which face a new edge belongs to. That way the adjacency
868 * of another edge can be looked up. An edge has an adjacent face if there
869 * is an edge going in the opposite direction in the map. For example if the
870 * edge (v1, v2) belongs to face 4, and there is a mapping (v2, v1)->7, then
871 * face 4 and 7 are adjacent.
872 *
873 * Each edge might have been replaced with another edge, or none at all. There
874 * is at most one edge to face mapping, i.e. an edge can only belong to one
875 * face.
876 */
877 static HRESULT init_edge_face_map(struct edge_face_map *edge_face_map, const DWORD *index_buffer,
878 const DWORD *point_reps, DWORD num_faces)
879 {
880 DWORD face, edge;
881 DWORD i;
882
883 edge_face_map->lists = HeapAlloc(GetProcessHeap(), 0, 3 * num_faces * sizeof(*edge_face_map->lists));
884 if (!edge_face_map->lists) return E_OUTOFMEMORY;
885
886 edge_face_map->entries = HeapAlloc(GetProcessHeap(), 0, 3 * num_faces * sizeof(*edge_face_map->entries));
887 if (!edge_face_map->entries) return E_OUTOFMEMORY;
888
889
890 /* Initialize all lists */
891 for (i = 0; i < 3 * num_faces; i++)
892 {
893 list_init(&edge_face_map->lists[i]);
894 }
895 /* Build edge face mapping */
896 for (face = 0; face < num_faces; face++)
897 {
898 for (edge = 0; edge < 3; edge++)
899 {
900 DWORD v1 = index_buffer[3*face + edge];
901 DWORD v2 = index_buffer[3*face + (edge+1)%3];
902 DWORD new_v1 = point_reps[v1]; /* What v1 has been replaced with */
903 DWORD new_v2 = point_reps[v2];
904
905 if (v1 != v2) /* Only map non-collapsed edges */
906 {
907 i = 3*face + edge;
908 edge_face_map->entries[i].v2 = new_v2;
909 edge_face_map->entries[i].face = face;
910 list_add_head(&edge_face_map->lists[new_v1], &edge_face_map->entries[i].entry);
911 }
912 }
913 }
914
915 return D3D_OK;
916 }
917
918 static DWORD find_adjacent_face(struct edge_face_map *edge_face_map, DWORD vertex1, DWORD vertex2, DWORD num_faces)
919 {
920 struct edge_face *edge_face_ptr;
921
922 LIST_FOR_EACH_ENTRY(edge_face_ptr, &edge_face_map->lists[vertex2], struct edge_face, entry)
923 {
924 if (edge_face_ptr->v2 == vertex1)
925 return edge_face_ptr->face;
926 }
927
928 return -1;
929 }
930
931 static DWORD *generate_identity_point_reps(DWORD num_vertices)
932 {
933 DWORD *id_point_reps;
934 DWORD i;
935
936 id_point_reps = HeapAlloc(GetProcessHeap(), 0, num_vertices * sizeof(*id_point_reps));
937 if (!id_point_reps)
938 return NULL;
939
940 for (i = 0; i < num_vertices; i++)
941 {
942 id_point_reps[i] = i;
943 }
944
945 return id_point_reps;
946 }
947
948 static HRESULT WINAPI d3dx9_mesh_ConvertPointRepsToAdjacency(ID3DXMesh *iface,
949 const DWORD *point_reps, DWORD *adjacency)
950 {
951 HRESULT hr;
952 DWORD num_faces = iface->lpVtbl->GetNumFaces(iface);
953 DWORD num_vertices = iface->lpVtbl->GetNumVertices(iface);
954 DWORD options = iface->lpVtbl->GetOptions(iface);
955 BOOL indices_are_16_bit = !(options & D3DXMESH_32BIT);
956 DWORD *ib = NULL;
957 void *ib_ptr = NULL;
958 DWORD face;
959 DWORD edge;
960 struct edge_face_map edge_face_map = {0};
961 const DWORD *point_reps_ptr = NULL;
962 DWORD *id_point_reps = NULL;
963
964 TRACE("iface %p, point_reps %p, adjacency %p.\n", iface, point_reps, adjacency);
965
966 if (!adjacency) return D3DERR_INVALIDCALL;
967
968 if (!point_reps) /* Identity point reps */
969 {
970 id_point_reps = generate_identity_point_reps(num_vertices);
971 if (!id_point_reps)
972 {
973 hr = E_OUTOFMEMORY;
974 goto cleanup;
975 }
976
977 point_reps_ptr = id_point_reps;
978 }
979 else
980 {
981 point_reps_ptr = point_reps;
982 }
983
984 hr = iface->lpVtbl->LockIndexBuffer(iface, D3DLOCK_READONLY, &ib_ptr);
985 if (FAILED(hr)) goto cleanup;
986
987 if (indices_are_16_bit)
988 {
989 /* Widen 16 bit to 32 bit */
990 DWORD i;
991 WORD *ib_16bit = ib_ptr;
992 ib = HeapAlloc(GetProcessHeap(), 0, 3 * num_faces * sizeof(DWORD));
993 if (!ib)
994 {
995 hr = E_OUTOFMEMORY;
996 goto cleanup;
997 }
998 for (i = 0; i < 3 * num_faces; i++)
999 {
1000 ib[i] = ib_16bit[i];
1001 }
1002 }
1003 else
1004 {
1005 ib = ib_ptr;
1006 }
1007
1008 hr = init_edge_face_map(&edge_face_map, ib, point_reps_ptr, num_faces);
1009 if (FAILED(hr)) goto cleanup;
1010
1011 /* Create adjacency */
1012 for (face = 0; face < num_faces; face++)
1013 {
1014 for (edge = 0; edge < 3; edge++)
1015 {
1016 DWORD v1 = ib[3*face + edge];
1017 DWORD v2 = ib[3*face + (edge+1)%3];
1018 DWORD new_v1 = point_reps_ptr[v1];
1019 DWORD new_v2 = point_reps_ptr[v2];
1020 DWORD adj_face;
1021
1022 adj_face = find_adjacent_face(&edge_face_map, new_v1, new_v2, num_faces);
1023 adjacency[3*face + edge] = adj_face;
1024 }
1025 }
1026
1027 hr = D3D_OK;
1028 cleanup:
1029 HeapFree(GetProcessHeap(), 0, id_point_reps);
1030 if (indices_are_16_bit) HeapFree(GetProcessHeap(), 0, ib);
1031 HeapFree(GetProcessHeap(), 0, edge_face_map.lists);
1032 HeapFree(GetProcessHeap(), 0, edge_face_map.entries);
1033 if(ib_ptr) iface->lpVtbl->UnlockIndexBuffer(iface);
1034 return hr;
1035 }
1036
1037 /* ConvertAdjacencyToPointReps helper function.
1038 *
1039 * Goes around the edges of each face and replaces the vertices in any adjacent
1040 * face's edge with its own vertices(if its vertices have a lower index). This
1041 * way as few as possible low index vertices are shared among the faces. The
1042 * re-ordered index buffer is stored in new_indices.
1043 *
1044 * The vertices in a point representation must be ordered sequentially, e.g.
1045 * index 5 holds the index of the vertex that replaces vertex 5, i.e. if
1046 * vertex 5 is replaced by vertex 3 then index 5 would contain 3. If no vertex
1047 * replaces it, then it contains the same number as the index itself, e.g.
1048 * index 5 would contain 5. */
1049 static HRESULT propagate_face_vertices(const DWORD *adjacency, DWORD *point_reps,
1050 const DWORD *indices, DWORD *new_indices, DWORD face, DWORD numfaces)
1051 {
1052 const unsigned int VERTS_PER_FACE = 3;
1053 DWORD edge, opp_edge;
1054 DWORD face_base = VERTS_PER_FACE * face;
1055
1056 for (edge = 0; edge < VERTS_PER_FACE; edge++)
1057 {
1058 DWORD adj_face = adjacency[face_base + edge];
1059 DWORD adj_face_base;
1060 DWORD i;
1061 if (adj_face == -1) /* No adjacent face. */
1062 continue;
1063 else if (adj_face >= numfaces)
1064 {
1065 /* This throws exception on Windows */
1066 WARN("Index out of bounds. Got %d expected less than %d.\n",
1067 adj_face, numfaces);
1068 return D3DERR_INVALIDCALL;
1069 }
1070 adj_face_base = 3 * adj_face;
1071
1072 /* Find opposite edge in adjacent face. */
1073 for (opp_edge = 0; opp_edge < VERTS_PER_FACE; opp_edge++)
1074 {
1075 DWORD opp_edge_index = adj_face_base + opp_edge;
1076 if (adjacency[opp_edge_index] == face)
1077 break; /* Found opposite edge. */
1078 }
1079
1080 /* Replaces vertices in opposite edge with vertices from current edge. */
1081 for (i = 0; i < 2; i++)
1082 {
1083 DWORD from = face_base + (edge + (1 - i)) % VERTS_PER_FACE;
1084 DWORD to = adj_face_base + (opp_edge + i) % VERTS_PER_FACE;
1085
1086 /* Propagate lowest index. */
1087 if (new_indices[to] > new_indices[from])
1088 {
1089 new_indices[to] = new_indices[from];
1090 point_reps[indices[to]] = new_indices[from];
1091 }
1092 }
1093 }
1094
1095 return D3D_OK;
1096 }
1097
1098 static HRESULT WINAPI d3dx9_mesh_ConvertAdjacencyToPointReps(ID3DXMesh *iface,
1099 const DWORD *adjacency, DWORD *point_reps)
1100 {
1101 struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
1102 HRESULT hr;
1103 DWORD face;
1104 DWORD i;
1105 DWORD *indices = NULL;
1106 WORD *indices_16bit = NULL;
1107 DWORD *new_indices = NULL;
1108 const unsigned int VERTS_PER_FACE = 3;
1109
1110 TRACE("iface %p, adjacency %p, point_reps %p.\n", iface, adjacency, point_reps);
1111
1112 if (!adjacency)
1113 {
1114 WARN("NULL adjacency.\n");
1115 hr = D3DERR_INVALIDCALL;
1116 goto cleanup;
1117 }
1118
1119 if (!point_reps)
1120 {
1121 WARN("NULL point_reps.\n");
1122 hr = D3DERR_INVALIDCALL;
1123 goto cleanup;
1124 }
1125
1126 /* Should never happen as CreateMesh does not allow meshes with 0 faces */
1127 if (This->numfaces == 0)
1128 {
1129 ERR("Number of faces was zero.\n");
1130 hr = D3DERR_INVALIDCALL;
1131 goto cleanup;
1132 }
1133
1134 new_indices = HeapAlloc(GetProcessHeap(), 0, VERTS_PER_FACE * This->numfaces * sizeof(*indices));
1135 if (!new_indices)
1136 {
1137 hr = E_OUTOFMEMORY;
1138 goto cleanup;
1139 }
1140
1141 if (This->options & D3DXMESH_32BIT)
1142 {
1143 hr = iface->lpVtbl->LockIndexBuffer(iface, D3DLOCK_READONLY, (void**)&indices);
1144 if (FAILED(hr)) goto cleanup;
1145 memcpy(new_indices, indices, VERTS_PER_FACE * This->numfaces * sizeof(*indices));
1146 }
1147 else
1148 {
1149 /* Make a widening copy of indices_16bit into indices and new_indices
1150 * in order to re-use the helper function */
1151 hr = iface->lpVtbl->LockIndexBuffer(iface, D3DLOCK_READONLY, (void**)&indices_16bit);
1152 if (FAILED(hr)) goto cleanup;
1153 indices = HeapAlloc(GetProcessHeap(), 0, VERTS_PER_FACE * This->numfaces * sizeof(*indices));
1154 if (!indices)
1155 {
1156 hr = E_OUTOFMEMORY;
1157 goto cleanup;
1158 }
1159 for (i = 0; i < VERTS_PER_FACE * This->numfaces; i++)
1160 {
1161 new_indices[i] = indices_16bit[i];
1162 indices[i] = indices_16bit[i];
1163 }
1164 }
1165
1166 /* Vertices are ordered sequentially in the point representation. */
1167 for (i = 0; i < This->numvertices; i++)
1168 {
1169 point_reps[i] = i;
1170 }
1171
1172 /* Propagate vertices with low indices so as few vertices as possible
1173 * are used in the mesh.
1174 */
1175 for (face = 0; face < This->numfaces; face++)
1176 {
1177 hr = propagate_face_vertices(adjacency, point_reps, indices, new_indices, face, This->numfaces);
1178 if (FAILED(hr)) goto cleanup;
1179 }
1180 /* Go in opposite direction to catch all face orderings */
1181 for (face = 0; face < This->numfaces; face++)
1182 {
1183 hr = propagate_face_vertices(adjacency, point_reps,
1184 indices, new_indices,
1185 (This->numfaces - 1) - face, This->numfaces);
1186 if (FAILED(hr)) goto cleanup;
1187 }
1188
1189 hr = D3D_OK;
1190 cleanup:
1191 if (This->options & D3DXMESH_32BIT)
1192 {
1193 if (indices) iface->lpVtbl->UnlockIndexBuffer(iface);
1194 }
1195 else
1196 {
1197 if (indices_16bit) iface->lpVtbl->UnlockIndexBuffer(iface);
1198 HeapFree(GetProcessHeap(), 0, indices);
1199 }
1200 HeapFree(GetProcessHeap(), 0, new_indices);
1201 return hr;
1202 }
1203
1204 struct vertex_metadata {
1205 float key;
1206 DWORD vertex_index;
1207 DWORD first_shared_index;
1208 };
1209
1210 static int compare_vertex_keys(const void *a, const void *b)
1211 {
1212 const struct vertex_metadata *left = a;
1213 const struct vertex_metadata *right = b;
1214 if (left->key == right->key)
1215 return 0;
1216 return left->key < right->key ? -1 : 1;
1217 }
1218
1219 static HRESULT WINAPI d3dx9_mesh_GenerateAdjacency(ID3DXMesh *iface, float epsilon, DWORD *adjacency)
1220 {
1221 struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
1222 HRESULT hr;
1223 BYTE *vertices = NULL;
1224 const DWORD *indices = NULL;
1225 DWORD vertex_size;
1226 DWORD buffer_size;
1227 /* sort the vertices by (x + y + z) to quickly find coincident vertices */
1228 struct vertex_metadata *sorted_vertices;
1229 /* shared_indices links together identical indices in the index buffer so
1230 * that adjacency checks can be limited to faces sharing a vertex */
1231 DWORD *shared_indices = NULL;
1232 const FLOAT epsilon_sq = epsilon * epsilon;
1233 DWORD i;
1234
1235 TRACE("iface %p, epsilon %.8e, adjacency %p.\n", iface, epsilon, adjacency);
1236
1237 if (!adjacency)
1238 return D3DERR_INVALIDCALL;
1239
1240 buffer_size = This->numfaces * 3 * sizeof(*shared_indices) + This->numvertices * sizeof(*sorted_vertices);
1241 if (!(This->options & D3DXMESH_32BIT))
1242 buffer_size += This->numfaces * 3 * sizeof(*indices);
1243 shared_indices = HeapAlloc(GetProcessHeap(), 0, buffer_size);
1244 if (!shared_indices)
1245 return E_OUTOFMEMORY;
1246 sorted_vertices = (struct vertex_metadata*)(shared_indices + This->numfaces * 3);
1247
1248 hr = iface->lpVtbl->LockVertexBuffer(iface, D3DLOCK_READONLY, (void**)&vertices);
1249 if (FAILED(hr)) goto cleanup;
1250 hr = iface->lpVtbl->LockIndexBuffer(iface, D3DLOCK_READONLY, (void**)&indices);
1251 if (FAILED(hr)) goto cleanup;
1252
1253 if (!(This->options & D3DXMESH_32BIT)) {
1254 const WORD *word_indices = (const WORD*)indices;
1255 DWORD *dword_indices = (DWORD*)(sorted_vertices + This->numvertices);
1256 indices = dword_indices;
1257 for (i = 0; i < This->numfaces * 3; i++)
1258 *dword_indices++ = *word_indices++;
1259 }
1260
1261 vertex_size = iface->lpVtbl->GetNumBytesPerVertex(iface);
1262 for (i = 0; i < This->numvertices; i++) {
1263 D3DXVECTOR3 *vertex = (D3DXVECTOR3*)(vertices + vertex_size * i);
1264 sorted_vertices[i].first_shared_index = -1;
1265 sorted_vertices[i].key = vertex->x + vertex->y + vertex->z;
1266 sorted_vertices[i].vertex_index = i;
1267 }
1268 for (i = 0; i < This->numfaces * 3; i++) {
1269 DWORD *first_shared_index = &sorted_vertices[indices[i]].first_shared_index;
1270 shared_indices[i] = *first_shared_index;
1271 *first_shared_index = i;
1272 adjacency[i] = -1;
1273 }
1274 qsort(sorted_vertices, This->numvertices, sizeof(*sorted_vertices), compare_vertex_keys);
1275
1276 for (i = 0; i < This->numvertices; i++) {
1277 struct vertex_metadata *sorted_vertex_a = &sorted_vertices[i];
1278 D3DXVECTOR3 *vertex_a = (D3DXVECTOR3*)(vertices + sorted_vertex_a->vertex_index * vertex_size);
1279 DWORD shared_index_a = sorted_vertex_a->first_shared_index;
1280
1281 while (shared_index_a != -1) {
1282 DWORD j = i;
1283 DWORD shared_index_b = shared_indices[shared_index_a];
1284 struct vertex_metadata *sorted_vertex_b = sorted_vertex_a;
1285
1286 while (TRUE) {
1287 while (shared_index_b != -1) {
1288 /* faces are adjacent if they have another coincident vertex */
1289 DWORD base_a = (shared_index_a / 3) * 3;
1290 DWORD base_b = (shared_index_b / 3) * 3;
1291 BOOL adjacent = FALSE;
1292 int k;
1293
1294 for (k = 0; k < 3; k++) {
1295 if (adjacency[base_b + k] == shared_index_a / 3) {
1296 adjacent = TRUE;
1297 break;
1298 }
1299 }
1300 if (!adjacent) {
1301 for (k = 1; k <= 2; k++) {
1302 DWORD vertex_index_a = base_a + (shared_index_a + k) % 3;
1303 DWORD vertex_index_b = base_b + (shared_index_b + (3 - k)) % 3;
1304 adjacent = indices[vertex_index_a] == indices[vertex_index_b];
1305 if (!adjacent && epsilon >= 0.0f) {
1306 D3DXVECTOR3 delta = {0.0f, 0.0f, 0.0f};
1307 FLOAT length_sq;
1308
1309 D3DXVec3Subtract(&delta,
1310 (D3DXVECTOR3*)(vertices + indices[vertex_index_a] * vertex_size),
1311 (D3DXVECTOR3*)(vertices + indices[vertex_index_b] * vertex_size));
1312 length_sq = D3DXVec3LengthSq(&delta);
1313 adjacent = epsilon == 0.0f ? length_sq == 0.0f : length_sq < epsilon_sq;
1314 }
1315 if (adjacent) {
1316 DWORD adj_a = base_a + 2 - (vertex_index_a + shared_index_a + 1) % 3;
1317 DWORD adj_b = base_b + 2 - (vertex_index_b + shared_index_b + 1) % 3;
1318 if (adjacency[adj_a] == -1 && adjacency[adj_b] == -1) {
1319 adjacency[adj_a] = base_b / 3;
1320 adjacency[adj_b] = base_a / 3;
1321 break;
1322 }
1323 }
1324 }
1325 }
1326
1327 shared_index_b = shared_indices[shared_index_b];
1328 }
1329 while (++j < This->numvertices) {
1330 D3DXVECTOR3 *vertex_b;
1331
1332 sorted_vertex_b++;
1333 if (sorted_vertex_b->key - sorted_vertex_a->key > epsilon * 3.0f) {
1334 /* no more coincident vertices to try */
1335 j = This->numvertices;
1336 break;
1337 }
1338 /* check for coincidence */
1339 vertex_b = (D3DXVECTOR3*)(vertices + sorted_vertex_b->vertex_index * vertex_size);
1340 if (fabsf(vertex_a->x - vertex_b->x) <= epsilon &&
1341 fabsf(vertex_a->y - vertex_b->y) <= epsilon &&
1342 fabsf(vertex_a->z - vertex_b->z) <= epsilon)
1343 {
1344 break;
1345 }
1346 }
1347 if (j >= This->numvertices)
1348 break;
1349 shared_index_b = sorted_vertex_b->first_shared_index;
1350 }
1351
1352 sorted_vertex_a->first_shared_index = shared_indices[sorted_vertex_a->first_shared_index];
1353 shared_index_a = sorted_vertex_a->first_shared_index;
1354 }
1355 }
1356
1357 hr = D3D_OK;
1358 cleanup:
1359 if (indices) iface->lpVtbl->UnlockIndexBuffer(iface);
1360 if (vertices) iface->lpVtbl->UnlockVertexBuffer(iface);
1361 HeapFree(GetProcessHeap(), 0, shared_indices);
1362 return hr;
1363 }
1364
1365 static HRESULT WINAPI d3dx9_mesh_UpdateSemantics(ID3DXMesh *iface, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
1366 {
1367 struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
1368 HRESULT hr;
1369 UINT vertex_declaration_size;
1370 int i;
1371
1372 TRACE("iface %p, declaration %p.\n", iface, declaration);
1373
1374 if (!declaration)
1375 {
1376 WARN("Invalid declaration. Can't use NULL declaration.\n");
1377 return D3DERR_INVALIDCALL;
1378 }
1379
1380 /* New declaration must be same size as original */
1381 vertex_declaration_size = D3DXGetDeclVertexSize(declaration, declaration[0].Stream);
1382 if (vertex_declaration_size != This->vertex_declaration_size)
1383 {
1384 WARN("Invalid declaration. New vertex size does not match the original vertex size.\n");
1385 return D3DERR_INVALIDCALL;
1386 }
1387
1388 /* New declaration must not contain non-zero Stream value */
1389 for (i = 0; declaration[i].Stream != 0xff; i++)
1390 {
1391 if (declaration[i].Stream != 0)
1392 {
1393 WARN("Invalid declaration. New declaration contains non-zero Stream value.\n");
1394 return D3DERR_INVALIDCALL;
1395 }
1396 }
1397
1398 This->num_elem = i + 1;
1399 copy_declaration(This->cached_declaration, declaration, This->num_elem);
1400
1401 if (This->vertex_declaration)
1402 IDirect3DVertexDeclaration9_Release(This->vertex_declaration);
1403
1404 /* An application can pass an invalid declaration to UpdateSemantics and
1405 * still expect D3D_OK (see tests). If the declaration is invalid, then
1406 * subsequent calls to DrawSubset will fail. This is handled by setting the
1407 * vertex declaration to NULL.
1408 * GetDeclaration, GetNumBytesPerVertex must, however, use the new
1409 * invalid declaration. This is handled by them using the cached vertex
1410 * declaration instead of the actual vertex declaration.
1411 */
1412 hr = IDirect3DDevice9_CreateVertexDeclaration(This->device,
1413 declaration,
1414 &This->vertex_declaration);
1415 if (FAILED(hr))
1416 {
1417 WARN("Using invalid declaration. Calls to DrawSubset will fail.\n");
1418 This->vertex_declaration = NULL;
1419 }
1420
1421 return D3D_OK;
1422 }
1423
1424 static HRESULT WINAPI d3dx9_mesh_LockAttributeBuffer(ID3DXMesh *iface, DWORD flags, DWORD **data)
1425 {
1426 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
1427
1428 TRACE("iface %p, flags %#x, data %p.\n", iface, flags, data);
1429
1430 InterlockedIncrement(&mesh->attrib_buffer_lock_count);
1431
1432 if (!(flags & D3DLOCK_READONLY))
1433 {
1434 D3DXATTRIBUTERANGE *attrib_table = mesh->attrib_table;
1435 mesh->attrib_table_size = 0;
1436 mesh->attrib_table = NULL;
1437 HeapFree(GetProcessHeap(), 0, attrib_table);
1438 }
1439
1440 *data = mesh->attrib_buffer;
1441
1442 return D3D_OK;
1443 }
1444
1445 static HRESULT WINAPI d3dx9_mesh_UnlockAttributeBuffer(ID3DXMesh *iface)
1446 {
1447 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
1448 int lock_count;
1449
1450 TRACE("iface %p.\n", iface);
1451
1452 lock_count = InterlockedDecrement(&mesh->attrib_buffer_lock_count);
1453 if (lock_count < 0)
1454 {
1455 InterlockedIncrement(&mesh->attrib_buffer_lock_count);
1456 return D3DERR_INVALIDCALL;
1457 }
1458
1459 return D3D_OK;
1460 }
1461
1462 static HRESULT WINAPI d3dx9_mesh_Optimize(ID3DXMesh *iface, DWORD flags, const DWORD *adjacency_in,
1463 DWORD *adjacency_out, DWORD *face_remap, ID3DXBuffer **vertex_remap, ID3DXMesh **opt_mesh)
1464 {
1465 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
1466 HRESULT hr;
1467 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE] = { D3DDECL_END() };
1468 ID3DXMesh *optimized_mesh;
1469
1470 TRACE("iface %p, flags %#x, adjacency_in %p, adjacency_out %p, face_remap %p, vertex_remap %p, opt_mesh %p.\n",
1471 iface, flags, adjacency_in, adjacency_out, face_remap, vertex_remap, opt_mesh);
1472
1473 if (!opt_mesh)
1474 return D3DERR_INVALIDCALL;
1475
1476 hr = iface->lpVtbl->GetDeclaration(iface, declaration);
1477 if (FAILED(hr)) return hr;
1478
1479 if (FAILED(hr = iface->lpVtbl->CloneMesh(iface, mesh->options, declaration, mesh->device, &optimized_mesh)))
1480 return hr;
1481
1482 hr = optimized_mesh->lpVtbl->OptimizeInplace(optimized_mesh, flags, adjacency_in, adjacency_out, face_remap, vertex_remap);
1483 if (SUCCEEDED(hr))
1484 *opt_mesh = optimized_mesh;
1485 else
1486 IUnknown_Release(optimized_mesh);
1487 return hr;
1488 }
1489
1490 /* Creates a vertex_remap that removes unused vertices.
1491 * Indices are updated according to the vertex_remap. */
1492 static HRESULT compact_mesh(struct d3dx9_mesh *This, DWORD *indices,
1493 DWORD *new_num_vertices, ID3DXBuffer **vertex_remap)
1494 {
1495 HRESULT hr;
1496 DWORD *vertex_remap_ptr;
1497 DWORD num_used_vertices;
1498 DWORD i;
1499
1500 hr = D3DXCreateBuffer(This->numvertices * sizeof(DWORD), vertex_remap);
1501 if (FAILED(hr)) return hr;
1502 vertex_remap_ptr = ID3DXBuffer_GetBufferPointer(*vertex_remap);
1503
1504 for (i = 0; i < This->numfaces * 3; i++)
1505 vertex_remap_ptr[indices[i]] = 1;
1506
1507 /* create old->new vertex mapping */
1508 num_used_vertices = 0;
1509 for (i = 0; i < This->numvertices; i++) {
1510 if (vertex_remap_ptr[i])
1511 vertex_remap_ptr[i] = num_used_vertices++;
1512 else
1513 vertex_remap_ptr[i] = -1;
1514 }
1515 /* convert indices */
1516 for (i = 0; i < This->numfaces * 3; i++)
1517 indices[i] = vertex_remap_ptr[indices[i]];
1518
1519 /* create new->old vertex mapping */
1520 num_used_vertices = 0;
1521 for (i = 0; i < This->numvertices; i++) {
1522 if (vertex_remap_ptr[i] != -1)
1523 vertex_remap_ptr[num_used_vertices++] = i;
1524 }
1525 for (i = num_used_vertices; i < This->numvertices; i++)
1526 vertex_remap_ptr[i] = -1;
1527
1528 *new_num_vertices = num_used_vertices;
1529
1530 return D3D_OK;
1531 }
1532
1533 /* count the number of unique attribute values in a sorted attribute buffer */
1534 static DWORD count_attributes(const DWORD *attrib_buffer, DWORD numfaces)
1535 {
1536 DWORD last_attribute = attrib_buffer[0];
1537 DWORD attrib_table_size = 1;
1538 DWORD i;
1539 for (i = 1; i < numfaces; i++) {
1540 if (attrib_buffer[i] != last_attribute) {
1541 last_attribute = attrib_buffer[i];
1542 attrib_table_size++;
1543 }
1544 }
1545 return attrib_table_size;
1546 }
1547
1548 static void fill_attribute_table(DWORD *attrib_buffer, DWORD numfaces, void *indices,
1549 BOOL is_32bit_indices, D3DXATTRIBUTERANGE *attrib_table)
1550 {
1551 DWORD attrib_table_size = 0;
1552 DWORD last_attribute = attrib_buffer[0];
1553 DWORD min_vertex, max_vertex;
1554 DWORD i;
1555
1556 attrib_table[0].AttribId = last_attribute;
1557 attrib_table[0].FaceStart = 0;
1558 min_vertex = (DWORD)-1;
1559 max_vertex = 0;
1560 for (i = 0; i < numfaces; i++) {
1561 DWORD j;
1562
1563 if (attrib_buffer[i] != last_attribute) {
1564 last_attribute = attrib_buffer[i];
1565 attrib_table[attrib_table_size].FaceCount = i - attrib_table[attrib_table_size].FaceStart;
1566 attrib_table[attrib_table_size].VertexStart = min_vertex;
1567 attrib_table[attrib_table_size].VertexCount = max_vertex - min_vertex + 1;
1568 attrib_table_size++;
1569 attrib_table[attrib_table_size].AttribId = attrib_buffer[i];
1570 attrib_table[attrib_table_size].FaceStart = i;
1571 min_vertex = (DWORD)-1;
1572 max_vertex = 0;
1573 }
1574 for (j = 0; j < 3; j++) {
1575 DWORD vertex_index = is_32bit_indices ? ((DWORD*)indices)[i * 3 + j] : ((WORD*)indices)[i * 3 + j];
1576 if (vertex_index < min_vertex)
1577 min_vertex = vertex_index;
1578 if (vertex_index > max_vertex)
1579 max_vertex = vertex_index;
1580 }
1581 }
1582 attrib_table[attrib_table_size].FaceCount = i - attrib_table[attrib_table_size].FaceStart;
1583 attrib_table[attrib_table_size].VertexStart = min_vertex;
1584 attrib_table[attrib_table_size].VertexCount = max_vertex - min_vertex + 1;
1585 attrib_table_size++;
1586 }
1587
1588 static int attrib_entry_compare(const DWORD **a, const DWORD **b)
1589 {
1590 const DWORD *ptr_a = *a;
1591 const DWORD *ptr_b = *b;
1592 int delta = *ptr_a - *ptr_b;
1593
1594 if (delta)
1595 return delta;
1596
1597 delta = ptr_a - ptr_b; /* for stable sort */
1598 return delta;
1599 }
1600
1601 /* Create face_remap, a new attribute buffer for attribute sort optimization. */
1602 static HRESULT remap_faces_for_attrsort(struct d3dx9_mesh *This, const DWORD *indices,
1603 DWORD *attrib_buffer, DWORD **sorted_attrib_buffer, DWORD **face_remap)
1604 {
1605 DWORD **sorted_attrib_ptr_buffer = NULL;
1606 DWORD i;
1607
1608 sorted_attrib_ptr_buffer = HeapAlloc(GetProcessHeap(), 0, This->numfaces * sizeof(*sorted_attrib_ptr_buffer));
1609 if (!sorted_attrib_ptr_buffer)
1610 return E_OUTOFMEMORY;
1611
1612 *face_remap = HeapAlloc(GetProcessHeap(), 0, This->numfaces * sizeof(**face_remap));
1613 if (!*face_remap)
1614 {
1615 HeapFree(GetProcessHeap(), 0, sorted_attrib_ptr_buffer);
1616 return E_OUTOFMEMORY;
1617 }
1618
1619 for (i = 0; i < This->numfaces; i++)
1620 sorted_attrib_ptr_buffer[i] = &attrib_buffer[i];
1621 qsort(sorted_attrib_ptr_buffer, This->numfaces, sizeof(*sorted_attrib_ptr_buffer),
1622 (int(*)(const void *, const void *))attrib_entry_compare);
1623
1624 for (i = 0; i < This->numfaces; i++)
1625 {
1626 DWORD old_face = sorted_attrib_ptr_buffer[i] - attrib_buffer;
1627 (*face_remap)[old_face] = i;
1628 }
1629
1630 /* overwrite sorted_attrib_ptr_buffer with the values themselves */
1631 *sorted_attrib_buffer = (DWORD*)sorted_attrib_ptr_buffer;
1632 for (i = 0; i < This->numfaces; i++)
1633 (*sorted_attrib_buffer)[(*face_remap)[i]] = attrib_buffer[i];
1634
1635 return D3D_OK;
1636 }
1637
1638 static HRESULT WINAPI d3dx9_mesh_OptimizeInplace(ID3DXMesh *iface, DWORD flags, const DWORD *adjacency_in,
1639 DWORD *adjacency_out, DWORD *face_remap_out, ID3DXBuffer **vertex_remap_out)
1640 {
1641 struct d3dx9_mesh *This = impl_from_ID3DXMesh(iface);
1642 void *indices = NULL;
1643 DWORD *attrib_buffer = NULL;
1644 HRESULT hr;
1645 ID3DXBuffer *vertex_remap = NULL;
1646 DWORD *face_remap = NULL; /* old -> new mapping */
1647 DWORD *dword_indices = NULL;
1648 DWORD new_num_vertices = 0;
1649 DWORD new_num_alloc_vertices = 0;
1650 IDirect3DVertexBuffer9 *vertex_buffer = NULL;
1651 DWORD *sorted_attrib_buffer = NULL;
1652 DWORD i;
1653
1654 TRACE("iface %p, flags %#x, adjacency_in %p, adjacency_out %p, face_remap_out %p, vertex_remap_out %p.\n",
1655 iface, flags, adjacency_in, adjacency_out, face_remap_out, vertex_remap_out);
1656
1657 if (!flags)
1658 return D3DERR_INVALIDCALL;
1659 if (!adjacency_in && (flags & (D3DXMESHOPT_VERTEXCACHE | D3DXMESHOPT_STRIPREORDER)))
1660 return D3DERR_INVALIDCALL;
1661 if ((flags & (D3DXMESHOPT_VERTEXCACHE | D3DXMESHOPT_STRIPREORDER)) == (D3DXMESHOPT_VERTEXCACHE | D3DXMESHOPT_STRIPREORDER))
1662 return D3DERR_INVALIDCALL;
1663
1664 if (flags & (D3DXMESHOPT_VERTEXCACHE | D3DXMESHOPT_STRIPREORDER))
1665 {
1666 if (flags & D3DXMESHOPT_VERTEXCACHE)
1667 FIXME("D3DXMESHOPT_VERTEXCACHE not implemented.\n");
1668 if (flags & D3DXMESHOPT_STRIPREORDER)
1669 FIXME("D3DXMESHOPT_STRIPREORDER not implemented.\n");
1670 return E_NOTIMPL;
1671 }
1672
1673 hr = iface->lpVtbl->LockIndexBuffer(iface, 0, &indices);
1674 if (FAILED(hr)) goto cleanup;
1675
1676 dword_indices = HeapAlloc(GetProcessHeap(), 0, This->numfaces * 3 * sizeof(DWORD));
1677 if (!dword_indices) return E_OUTOFMEMORY;
1678 if (This->options & D3DXMESH_32BIT) {
1679 memcpy(dword_indices, indices, This->numfaces * 3 * sizeof(DWORD));
1680 } else {
1681 WORD *word_indices = indices;
1682 for (i = 0; i < This->numfaces * 3; i++)
1683 dword_indices[i] = *word_indices++;
1684 }
1685
1686 if ((flags & (D3DXMESHOPT_COMPACT | D3DXMESHOPT_IGNOREVERTS | D3DXMESHOPT_ATTRSORT)) == D3DXMESHOPT_COMPACT)
1687 {
1688 new_num_alloc_vertices = This->numvertices;
1689 hr = compact_mesh(This, dword_indices, &new_num_vertices, &vertex_remap);
1690 if (FAILED(hr)) goto cleanup;
1691 } else if (flags & D3DXMESHOPT_ATTRSORT) {
1692 if (!(flags & D3DXMESHOPT_IGNOREVERTS))
1693 {
1694 FIXME("D3DXMESHOPT_ATTRSORT vertex reordering not implemented.\n");
1695 hr = E_NOTIMPL;
1696 goto cleanup;
1697 }
1698
1699 hr = iface->lpVtbl->LockAttributeBuffer(iface, 0, &attrib_buffer);
1700 if (FAILED(hr)) goto cleanup;
1701
1702 hr = remap_faces_for_attrsort(This, dword_indices, attrib_buffer, &sorted_attrib_buffer, &face_remap);
1703 if (FAILED(hr)) goto cleanup;
1704 }
1705
1706 if (vertex_remap)
1707 {
1708 /* reorder the vertices using vertex_remap */
1709 D3DVERTEXBUFFER_DESC vertex_desc;
1710 DWORD *vertex_remap_ptr = ID3DXBuffer_GetBufferPointer(vertex_remap);
1711 DWORD vertex_size = iface->lpVtbl->GetNumBytesPerVertex(iface);
1712 BYTE *orig_vertices;
1713 BYTE *new_vertices;
1714
1715 hr = IDirect3DVertexBuffer9_GetDesc(This->vertex_buffer, &vertex_desc);
1716 if (FAILED(hr)) goto cleanup;
1717
1718 hr = IDirect3DDevice9_CreateVertexBuffer(This->device, new_num_alloc_vertices * vertex_size,
1719 vertex_desc.Usage, This->fvf, vertex_desc.Pool, &vertex_buffer, NULL);
1720 if (FAILED(hr)) goto cleanup;
1721
1722 hr = IDirect3DVertexBuffer9_Lock(This->vertex_buffer, 0, 0, (void**)&orig_vertices, D3DLOCK_READONLY);
1723 if (FAILED(hr)) goto cleanup;
1724
1725 hr = IDirect3DVertexBuffer9_Lock(vertex_buffer, 0, 0, (void**)&new_vertices, 0);
1726 if (FAILED(hr)) {
1727 IDirect3DVertexBuffer9_Unlock(This->vertex_buffer);
1728 goto cleanup;
1729 }
1730
1731 for (i = 0; i < new_num_vertices; i++)
1732 memcpy(new_vertices + i * vertex_size, orig_vertices + vertex_remap_ptr[i] * vertex_size, vertex_size);
1733
1734 IDirect3DVertexBuffer9_Unlock(This->vertex_buffer);
1735 IDirect3DVertexBuffer9_Unlock(vertex_buffer);
1736 } else if (vertex_remap_out) {
1737 DWORD *vertex_remap_ptr;
1738
1739 hr = D3DXCreateBuffer(This->numvertices * sizeof(DWORD), &vertex_remap);
1740 if (FAILED(hr)) goto cleanup;
1741 vertex_remap_ptr = ID3DXBuffer_GetBufferPointer(vertex_remap);
1742 for (i = 0; i < This->numvertices; i++)
1743 *vertex_remap_ptr++ = i;
1744 }
1745
1746 if (flags & D3DXMESHOPT_ATTRSORT)
1747 {
1748 D3DXATTRIBUTERANGE *attrib_table;
1749 DWORD attrib_table_size;
1750
1751 attrib_table_size = count_attributes(sorted_attrib_buffer, This->numfaces);
1752 attrib_table = HeapAlloc(GetProcessHeap(), 0, attrib_table_size * sizeof(*attrib_table));
1753 if (!attrib_table) {
1754 hr = E_OUTOFMEMORY;
1755 goto cleanup;
1756 }
1757
1758 memcpy(attrib_buffer, sorted_attrib_buffer, This->numfaces * sizeof(*attrib_buffer));
1759
1760 /* reorder the indices using face_remap */
1761 if (This->options & D3DXMESH_32BIT) {
1762 for (i = 0; i < This->numfaces; i++)
1763 memcpy((DWORD*)indices + face_remap[i] * 3, dword_indices + i * 3, 3 * sizeof(DWORD));
1764 } else {
1765 WORD *word_indices = indices;
1766 for (i = 0; i < This->numfaces; i++) {
1767 DWORD new_pos = face_remap[i] * 3;
1768 DWORD old_pos = i * 3;
1769 word_indices[new_pos++] = dword_indices[old_pos++];
1770 word_indices[new_pos++] = dword_indices[old_pos++];
1771 word_indices[new_pos] = dword_indices[old_pos];
1772 }
1773 }
1774
1775 fill_attribute_table(attrib_buffer, This->numfaces, indices,
1776 This->options & D3DXMESH_32BIT, attrib_table);
1777
1778 HeapFree(GetProcessHeap(), 0, This->attrib_table);
1779 This->attrib_table = attrib_table;
1780 This->attrib_table_size = attrib_table_size;
1781 } else {
1782 if (This->options & D3DXMESH_32BIT) {
1783 memcpy(indices, dword_indices, This->numfaces * 3 * sizeof(DWORD));
1784 } else {
1785 WORD *word_indices = indices;
1786 for (i = 0; i < This->numfaces * 3; i++)
1787 *word_indices++ = dword_indices[i];
1788 }
1789 }
1790
1791 if (adjacency_out) {
1792 if (face_remap) {
1793 for (i = 0; i < This->numfaces; i++) {
1794 DWORD old_pos = i * 3;
1795 DWORD new_pos = face_remap[i] * 3;
1796 adjacency_out[new_pos++] = face_remap[adjacency_in[old_pos++]];
1797 adjacency_out[new_pos++] = face_remap[adjacency_in[old_pos++]];
1798 adjacency_out[new_pos++] = face_remap[adjacency_in[old_pos++]];
1799 }
1800 } else {
1801 memcpy(adjacency_out, adjacency_in, This->numfaces * 3 * sizeof(*adjacency_out));
1802 }
1803 }
1804 if (face_remap_out) {
1805 if (face_remap) {
1806 for (i = 0; i < This->numfaces; i++)
1807 face_remap_out[face_remap[i]] = i;
1808 } else {
1809 for (i = 0; i < This->numfaces; i++)
1810 face_remap_out[i] = i;
1811 }
1812 }
1813 if (vertex_remap_out)
1814 *vertex_remap_out = vertex_remap;
1815 vertex_remap = NULL;
1816
1817 if (vertex_buffer) {
1818 IDirect3DVertexBuffer9_Release(This->vertex_buffer);
1819 This->vertex_buffer = vertex_buffer;
1820 vertex_buffer = NULL;
1821 This->numvertices = new_num_vertices;
1822 }
1823
1824 hr = D3D_OK;
1825 cleanup:
1826 HeapFree(GetProcessHeap(), 0, sorted_attrib_buffer);
1827 HeapFree(GetProcessHeap(), 0, face_remap);
1828 HeapFree(GetProcessHeap(), 0, dword_indices);
1829 if (vertex_remap) ID3DXBuffer_Release(vertex_remap);
1830 if (vertex_buffer) IDirect3DVertexBuffer9_Release(vertex_buffer);
1831 if (attrib_buffer) iface->lpVtbl->UnlockAttributeBuffer(iface);
1832 if (indices) iface->lpVtbl->UnlockIndexBuffer(iface);
1833 return hr;
1834 }
1835
1836 static HRESULT WINAPI d3dx9_mesh_SetAttributeTable(ID3DXMesh *iface,
1837 const D3DXATTRIBUTERANGE *attrib_table, DWORD attrib_table_size)
1838 {
1839 struct d3dx9_mesh *mesh = impl_from_ID3DXMesh(iface);
1840 D3DXATTRIBUTERANGE *new_table = NULL;
1841
1842 TRACE("iface %p, attrib_table %p, attrib_table_size %u.\n", iface, attrib_table, attrib_table_size);
1843
1844 if (attrib_table_size) {
1845 size_t size = attrib_table_size * sizeof(*attrib_table);
1846
1847 new_table = HeapAlloc(GetProcessHeap(), 0, size);
1848 if (!new_table)
1849 return E_OUTOFMEMORY;
1850
1851 CopyMemory(new_table, attrib_table, size);
1852 } else if (attrib_table) {
1853 return D3DERR_INVALIDCALL;
1854 }
1855 HeapFree(GetProcessHeap(), 0, mesh->attrib_table);
1856 mesh->attrib_table = new_table;
1857 mesh->attrib_table_size = attrib_table_size;
1858
1859 return D3D_OK;
1860 }
1861
1862 static const struct ID3DXMeshVtbl D3DXMesh_Vtbl =
1863 {
1864 d3dx9_mesh_QueryInterface,
1865 d3dx9_mesh_AddRef,
1866 d3dx9_mesh_Release,
1867 d3dx9_mesh_DrawSubset,
1868 d3dx9_mesh_GetNumFaces,
1869 d3dx9_mesh_GetNumVertices,
1870 d3dx9_mesh_GetFVF,
1871 d3dx9_mesh_GetDeclaration,
1872 d3dx9_mesh_GetNumBytesPerVertex,
1873 d3dx9_mesh_GetOptions,
1874 d3dx9_mesh_GetDevice,
1875 d3dx9_mesh_CloneMeshFVF,
1876 d3dx9_mesh_CloneMesh,
1877 d3dx9_mesh_GetVertexBuffer,
1878 d3dx9_mesh_GetIndexBuffer,
1879 d3dx9_mesh_LockVertexBuffer,
1880 d3dx9_mesh_UnlockVertexBuffer,
1881 d3dx9_mesh_LockIndexBuffer,
1882 d3dx9_mesh_UnlockIndexBuffer,
1883 d3dx9_mesh_GetAttributeTable,
1884 d3dx9_mesh_ConvertPointRepsToAdjacency,
1885 d3dx9_mesh_ConvertAdjacencyToPointReps,
1886 d3dx9_mesh_GenerateAdjacency,
1887 d3dx9_mesh_UpdateSemantics,
1888 d3dx9_mesh_LockAttributeBuffer,
1889 d3dx9_mesh_UnlockAttributeBuffer,
1890 d3dx9_mesh_Optimize,
1891 d3dx9_mesh_OptimizeInplace,
1892 d3dx9_mesh_SetAttributeTable,
1893 };
1894
1895
1896 /* Algorithm taken from the article: An Efficient and Robust Ray-Box Intersection Algorithm
1897 Amy Williams University of Utah
1898 Steve Barrus University of Utah
1899 R. Keith Morley University of Utah
1900 Peter Shirley University of Utah
1901
1902 International Conference on Computer Graphics and Interactive Techniques archive
1903 ACM SIGGRAPH 2005 Courses
1904 Los Angeles, California
1905
1906 This algorithm is free of patents or of copyrights, as confirmed by Peter Shirley himself.
1907
1908 Algorithm: Consider the box as the intersection of three slabs. Clip the ray
1909 against each slab, if there's anything left of the ray after we're
1910 done we've got an intersection of the ray with the box. */
1911 BOOL WINAPI D3DXBoxBoundProbe(const D3DXVECTOR3 *pmin, const D3DXVECTOR3 *pmax,
1912 const D3DXVECTOR3 *prayposition, const D3DXVECTOR3 *praydirection)
1913 {
1914 FLOAT div, tmin, tmax, tymin, tymax, tzmin, tzmax;
1915
1916 div = 1.0f / praydirection->x;
1917 if ( div >= 0.0f )
1918 {
1919 tmin = ( pmin->x - prayposition->x ) * div;
1920 tmax = ( pmax->x - prayposition->x ) * div;
1921 }
1922 else
1923 {
1924 tmin = ( pmax->x - prayposition->x ) * div;
1925 tmax = ( pmin->x - prayposition->x ) * div;
1926 }
1927
1928 if ( tmax < 0.0f ) return FALSE;
1929
1930 div = 1.0f / praydirection->y;
1931 if ( div >= 0.0f )
1932 {
1933 tymin = ( pmin->y - prayposition->y ) * div;
1934 tymax = ( pmax->y - prayposition->y ) * div;
1935 }
1936 else
1937 {
1938 tymin = ( pmax->y - prayposition->y ) * div;
1939 tymax = ( pmin->y - prayposition->y ) * div;
1940 }
1941
1942 if ( ( tymax < 0.0f ) || ( tmin > tymax ) || ( tymin > tmax ) ) return FALSE;
1943
1944 if ( tymin > tmin ) tmin = tymin;
1945 if ( tymax < tmax ) tmax = tymax;
1946
1947 div = 1.0f / praydirection->z;
1948 if ( div >= 0.0f )
1949 {
1950 tzmin = ( pmin->z - prayposition->z ) * div;
1951 tzmax = ( pmax->z - prayposition->z ) * div;
1952 }
1953 else
1954 {
1955 tzmin = ( pmax->z - prayposition->z ) * div;
1956 tzmax = ( pmin->z - prayposition->z ) * div;
1957 }
1958
1959 if ( (tzmax < 0.0f ) || ( tmin > tzmax ) || ( tzmin > tmax ) ) return FALSE;
1960
1961 return TRUE;
1962 }
1963
1964 HRESULT WINAPI D3DXComputeBoundingBox(const D3DXVECTOR3 *pfirstposition,
1965 DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pmin, D3DXVECTOR3 *pmax)
1966 {
1967 D3DXVECTOR3 vec;
1968 unsigned int i;
1969
1970 if( !pfirstposition || !pmin || !pmax ) return D3DERR_INVALIDCALL;
1971
1972 *pmin = *pfirstposition;
1973 *pmax = *pmin;
1974
1975 for(i=0; i<numvertices; i++)
1976 {
1977 vec = *( (const D3DXVECTOR3*)((const char*)pfirstposition + dwstride * i) );
1978
1979 if ( vec.x < pmin->x ) pmin->x = vec.x;
1980 if ( vec.x > pmax->x ) pmax->x = vec.x;
1981
1982 if ( vec.y < pmin->y ) pmin->y = vec.y;
1983 if ( vec.y > pmax->y ) pmax->y = vec.y;
1984
1985 if ( vec.z < pmin->z ) pmin->z = vec.z;
1986 if ( vec.z > pmax->z ) pmax->z = vec.z;
1987 }
1988
1989 return D3D_OK;
1990 }
1991
1992 HRESULT WINAPI D3DXComputeBoundingSphere(const D3DXVECTOR3 *pfirstposition,
1993 DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pcenter, float *pradius)
1994 {
1995 D3DXVECTOR3 temp;
1996 FLOAT d;
1997 unsigned int i;
1998
1999 if( !pfirstposition || !pcenter || !pradius ) return D3DERR_INVALIDCALL;
2000
2001 temp.x = 0.0f;
2002 temp.y = 0.0f;
2003 temp.z = 0.0f;
2004 *pradius = 0.0f;
2005
2006 for(i=0; i<numvertices; i++)
2007 D3DXVec3Add(&temp, &temp, (const D3DXVECTOR3*)((const char*)pfirstposition + dwstride * i));
2008
2009 D3DXVec3Scale(pcenter, &temp, 1.0f / numvertices);
2010
2011 for(i=0; i<numvertices; i++)
2012 {
2013 d = D3DXVec3Length(D3DXVec3Subtract(&temp, (const D3DXVECTOR3*)((const char*)pfirstposition + dwstride * i), pcenter));
2014 if ( d > *pradius ) *pradius = d;
2015 }
2016 return D3D_OK;
2017 }
2018
2019 static void append_decl_element(D3DVERTEXELEMENT9 *declaration, UINT *idx, UINT *offset,
2020 D3DDECLTYPE type, D3DDECLUSAGE usage, UINT usage_idx)
2021 {
2022 declaration[*idx].Stream = 0;
2023 declaration[*idx].Offset = *offset;
2024 declaration[*idx].Type = type;
2025 declaration[*idx].Method = D3DDECLMETHOD_DEFAULT;
2026 declaration[*idx].Usage = usage;
2027 declaration[*idx].UsageIndex = usage_idx;
2028
2029 *offset += d3dx_decltype_size[type];
2030 ++(*idx);
2031 }
2032
2033 /*************************************************************************
2034 * D3DXDeclaratorFromFVF
2035 */
2036 HRESULT WINAPI D3DXDeclaratorFromFVF(DWORD fvf, D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE])
2037 {
2038 static const D3DVERTEXELEMENT9 end_element = D3DDECL_END();
2039 DWORD tex_count = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
2040 unsigned int offset = 0;
2041 unsigned int idx = 0;
2042 unsigned int i;
2043
2044 TRACE("fvf %#x, declaration %p.\n", fvf, declaration);
2045
2046 if (fvf & (D3DFVF_RESERVED0 | D3DFVF_RESERVED2)) return D3DERR_INVALIDCALL;
2047
2048 if (fvf & D3DFVF_POSITION_MASK)
2049 {
2050 BOOL has_blend = (fvf & D3DFVF_XYZB5) >= D3DFVF_XYZB1;
2051 DWORD blend_count = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
2052 BOOL has_blend_idx = (fvf & D3DFVF_LASTBETA_D3DCOLOR) || (fvf & D3DFVF_LASTBETA_UBYTE4);
2053
2054 if (has_blend_idx) --blend_count;
2055
2056 if ((fvf & D3DFVF_POSITION_MASK) == D3DFVF_XYZW
2057 || (has_blend && blend_count > 4))
2058 return D3DERR_INVALIDCALL;
2059
2060 if ((fvf & D3DFVF_POSITION_MASK) == D3DFVF_XYZRHW)
2061 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT4, D3DDECLUSAGE_POSITIONT, 0);
2062 else
2063 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT3, D3DDECLUSAGE_POSITION, 0);
2064
2065 if (has_blend)
2066 {
2067 switch (blend_count)
2068 {
2069 case 0:
2070 break;
2071 case 1:
2072 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT1, D3DDECLUSAGE_BLENDWEIGHT, 0);
2073 break;
2074 case 2:
2075 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT2, D3DDECLUSAGE_BLENDWEIGHT, 0);
2076 break;
2077 case 3:
2078 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT3, D3DDECLUSAGE_BLENDWEIGHT, 0);
2079 break;
2080 case 4:
2081 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT4, D3DDECLUSAGE_BLENDWEIGHT, 0);
2082 break;
2083 default:
2084 ERR("Invalid blend count %u.\n", blend_count);
2085 break;
2086 }
2087
2088 if (has_blend_idx)
2089 {
2090 if (fvf & D3DFVF_LASTBETA_UBYTE4)
2091 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_UBYTE4, D3DDECLUSAGE_BLENDINDICES, 0);
2092 else if (fvf & D3DFVF_LASTBETA_D3DCOLOR)
2093 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_D3DCOLOR, D3DDECLUSAGE_BLENDINDICES, 0);
2094 }
2095 }
2096 }
2097
2098 if (fvf & D3DFVF_NORMAL)
2099 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT3, D3DDECLUSAGE_NORMAL, 0);
2100 if (fvf & D3DFVF_PSIZE)
2101 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT1, D3DDECLUSAGE_PSIZE, 0);
2102 if (fvf & D3DFVF_DIFFUSE)
2103 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_D3DCOLOR, D3DDECLUSAGE_COLOR, 0);
2104 if (fvf & D3DFVF_SPECULAR)
2105 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_D3DCOLOR, D3DDECLUSAGE_COLOR, 1);
2106
2107 for (i = 0; i < tex_count; ++i)
2108 {
2109 switch ((fvf >> (16 + 2 * i)) & 0x03)
2110 {
2111 case D3DFVF_TEXTUREFORMAT1:
2112 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT1, D3DDECLUSAGE_TEXCOORD, i);
2113 break;
2114 case D3DFVF_TEXTUREFORMAT2:
2115 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT2, D3DDECLUSAGE_TEXCOORD, i);
2116 break;
2117 case D3DFVF_TEXTUREFORMAT3:
2118 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT3, D3DDECLUSAGE_TEXCOORD, i);
2119 break;
2120 case D3DFVF_TEXTUREFORMAT4:
2121 append_decl_element(declaration, &idx, &offset, D3DDECLTYPE_FLOAT4, D3DDECLUSAGE_TEXCOORD, i);
2122 break;
2123 }
2124 }
2125
2126 declaration[idx] = end_element;
2127
2128 return D3D_OK;
2129 }
2130
2131 /*************************************************************************
2132 * D3DXFVFFromDeclarator
2133 */
2134 HRESULT WINAPI D3DXFVFFromDeclarator(const D3DVERTEXELEMENT9 *declaration, DWORD *fvf)
2135 {
2136 unsigned int i = 0, texture, offset;
2137
2138 TRACE("(%p, %p)\n", declaration, fvf);
2139
2140 *fvf = 0;
2141 if (declaration[0].Type == D3DDECLTYPE_FLOAT3 && declaration[0].Usage == D3DDECLUSAGE_POSITION)
2142 {
2143 if ((declaration[1].Type == D3DDECLTYPE_FLOAT4 && declaration[1].Usage == D3DDECLUSAGE_BLENDWEIGHT &&
2144 declaration[1].UsageIndex == 0) &&
2145 (declaration[2].Type == D3DDECLTYPE_FLOAT1 && declaration[2].Usage == D3DDECLUSAGE_BLENDINDICES &&
2146 declaration[2].UsageIndex == 0))
2147 {
2148 return D3DERR_INVALIDCALL;
2149 }
2150 else if ((declaration[1].Type == D3DDECLTYPE_UBYTE4 || declaration[1].Type == D3DDECLTYPE_D3DCOLOR) &&
2151 declaration[1].Usage == D3DDECLUSAGE_BLENDINDICES && declaration[1].UsageIndex == 0)
2152 {
2153 if (declaration[1].Type == D3DDECLTYPE_UBYTE4)
2154 {
2155 *fvf |= D3DFVF_XYZB1 | D3DFVF_LASTBETA_UBYTE4;
2156 }
2157 else
2158 {
2159 *fvf |= D3DFVF_XYZB1 | D3DFVF_LASTBETA_D3DCOLOR;
2160 }
2161 i = 2;
2162 }
2163 else if (declaration[1].Type <= D3DDECLTYPE_FLOAT4 && declaration[1].Usage == D3DDECLUSAGE_BLENDWEIGHT &&
2164 declaration[1].UsageIndex == 0)
2165 {
2166 if ((declaration[2].Type == D3DDECLTYPE_UBYTE4 || declaration[2].Type == D3DDECLTYPE_D3DCOLOR) &&
2167 declaration[2].Usage == D3DDECLUSAGE_BLENDINDICES && declaration[2].UsageIndex == 0)
2168 {
2169 if (declaration[2].Type == D3DDECLTYPE_UBYTE4)
2170 {
2171 *fvf |= D3DFVF_LASTBETA_UBYTE4;
2172 }
2173 else
2174 {
2175 *fvf |= D3DFVF_LASTBETA_D3DCOLOR;
2176 }
2177 switch (declaration[1].Type)
2178 {
2179 case D3DDECLTYPE_FLOAT1: *fvf |= D3DFVF_XYZB2; break;
2180 case D3DDECLTYPE_FLOAT2: *fvf |= D3DFVF_XYZB3; break;
2181 case D3DDECLTYPE_FLOAT3: *fvf |= D3DFVF_XYZB4; break;
2182 case D3DDECLTYPE_FLOAT4: *fvf |= D3DFVF_XYZB5; break;
2183 }
2184 i = 3;
2185 }
2186 else
2187 {
2188 switch (declaration[1].Type)
2189 {
2190 case D3DDECLTYPE_FLOAT1: *fvf |= D3DFVF_XYZB1; break;
2191 case D3DDECLTYPE_FLOAT2: *fvf |= D3DFVF_XYZB2; break;
2192 case D3DDECLTYPE_FLOAT3: *fvf |= D3DFVF_XYZB3; break;
2193 case D3DDECLTYPE_FLOAT4: *fvf |= D3DFVF_XYZB4; break;
2194 }
2195 i = 2;
2196 }
2197 }
2198 else
2199 {
2200 *fvf |= D3DFVF_XYZ;
2201 i = 1;
2202 }
2203 }
2204 else if (declaration[0].Type == D3DDECLTYPE_FLOAT4 && declaration[0].Usage == D3DDECLUSAGE_POSITIONT &&
2205 declaration[0].UsageIndex == 0)
2206 {
2207 *fvf |= D3DFVF_XYZRHW;
2208 i = 1;
2209 }
2210
2211 if (declaration[i].Type == D3DDECLTYPE_FLOAT3 && declaration[i].Usage == D3DDECLUSAGE_NORMAL)
2212 {
2213 *fvf |= D3DFVF_NORMAL;
2214 i++;
2215 }
2216 if (declaration[i].Type == D3DDECLTYPE_FLOAT1 && declaration[i].Usage == D3DDECLUSAGE_PSIZE &&
2217 declaration[i].UsageIndex == 0)
2218 {
2219 *fvf |= D3DFVF_PSIZE;
2220 i++;
2221 }
2222 if (declaration[i].Type == D3DDECLTYPE_D3DCOLOR && declaration[i].Usage == D3DDECLUSAGE_COLOR &&
2223 declaration[i].UsageIndex == 0)
2224 {
2225 *fvf |= D3DFVF_DIFFUSE;
2226 i++;
2227 }
2228 if (declaration[i].Type == D3DDECLTYPE_D3DCOLOR && declaration[i].Usage == D3DDECLUSAGE_COLOR &&
2229 declaration[i].UsageIndex == 1)
2230 {
2231 *fvf |= D3DFVF_SPECULAR;
2232 i++;
2233 }
2234
2235 for (texture = 0; texture < D3DDP_MAXTEXCOORD; i++, texture++)
2236 {
2237 if (declaration[i].Stream == 0xFF)
2238 {
2239 break;
2240 }
2241 else if (declaration[i].Type == D3DDECLTYPE_FLOAT1 && declaration[i].Usage == D3DDECLUSAGE_TEXCOORD &&
2242 declaration[i].UsageIndex == texture)
2243 {
2244 *fvf |= D3DFVF_TEXCOORDSIZE1(declaration[i].UsageIndex);
2245 }
2246 else if (declaration[i].Type == D3DDECLTYPE_FLOAT2 && declaration[i].Usage == D3DDECLUSAGE_TEXCOORD &&
2247 declaration[i].UsageIndex == texture)
2248 {
2249 *fvf |= D3DFVF_TEXCOORDSIZE2(declaration[i].UsageIndex);
2250 }
2251 else if (declaration[i].Type == D3DDECLTYPE_FLOAT3 && declaration[i].Usage == D3DDECLUSAGE_TEXCOORD &&
2252 declaration[i].UsageIndex == texture)
2253 {
2254 *fvf |= D3DFVF_TEXCOORDSIZE3(declaration[i].UsageIndex);
2255 }
2256 else if (declaration[i].Type == D3DDECLTYPE_FLOAT4 && declaration[i].Usage == D3DDECLUSAGE_TEXCOORD &&
2257 declaration[i].UsageIndex == texture)
2258 {
2259 *fvf |= D3DFVF_TEXCOORDSIZE4(declaration[i].UsageIndex);
2260 }
2261 else
2262 {
2263 return D3DERR_INVALIDCALL;
2264 }
2265 }
2266
2267 *fvf |= (texture << D3DFVF_TEXCOUNT_SHIFT);
2268
2269 for (offset = 0, i = 0; declaration[i].Stream != 0xFF;
2270 offset += d3dx_decltype_size[declaration[i].Type], i++)
2271 {
2272 if (declaration[i].Offset != offset)
2273 {
2274 return D3DERR_INVALIDCALL;
2275 }
2276 }
2277
2278 return D3D_OK;
2279 }
2280
2281 /*************************************************************************
2282 * D3DXGetFVFVertexSize
2283 */
2284 static UINT Get_TexCoord_Size_From_FVF(DWORD FVF, int tex_num)
2285 {
2286 return (((((FVF) >> (16 + (2 * (tex_num)))) + 1) & 0x03) + 1);
2287 }
2288
2289 UINT WINAPI D3DXGetFVFVertexSize(DWORD FVF)
2290 {
2291 DWORD size = 0;
2292 UINT i;
2293 UINT numTextures = (FVF & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
2294
2295 if (FVF & D3DFVF_NORMAL) size += sizeof(D3DXVECTOR3);
2296 if (FVF & D3DFVF_DIFFUSE) size += sizeof(DWORD);
2297 if (FVF & D3DFVF_SPECULAR) size += sizeof(DWORD);
2298 if (FVF & D3DFVF_PSIZE) size += sizeof(DWORD);
2299
2300 switch (FVF & D3DFVF_POSITION_MASK)
2301 {
2302 case D3DFVF_XYZ: size += sizeof(D3DXVECTOR3); break;
2303 case D3DFVF_XYZRHW: size += 4 * sizeof(FLOAT); break;
2304 case D3DFVF_XYZB1: size += 4 * sizeof(FLOAT); break;
2305 case D3DFVF_XYZB2: size += 5 * sizeof(FLOAT); break;
2306 case D3DFVF_XYZB3: size += 6 * sizeof(FLOAT); break;
2307 case D3DFVF_XYZB4: size += 7 * sizeof(FLOAT); break;
2308 case D3DFVF_XYZB5: size += 8 * sizeof(FLOAT); break;
2309 case D3DFVF_XYZW: size += 4 * sizeof(FLOAT); break;
2310 }
2311
2312 for (i = 0; i < numTextures; i++)
2313 {
2314 size += Get_TexCoord_Size_From_FVF(FVF, i) * sizeof(FLOAT);
2315 }
2316
2317 return size;
2318 }
2319
2320 /*************************************************************************
2321 * D3DXGetDeclVertexSize
2322 */
2323 UINT WINAPI D3DXGetDeclVertexSize(const D3DVERTEXELEMENT9 *decl, DWORD stream_idx)
2324 {
2325 const D3DVERTEXELEMENT9 *element;
2326 UINT size = 0;
2327
2328 TRACE("decl %p, stream_idx %u\n", decl, stream_idx);
2329
2330 if (!decl) return 0;
2331
2332 for (element = decl; element->Stream != 0xff; ++element)
2333 {
2334 UINT type_size;
2335
2336 if (element->Stream != stream_idx) continue;
2337
2338 if (element->Type >= sizeof(d3dx_decltype_size) / sizeof(*d3dx_decltype_size))
2339 {
2340 FIXME("Unhandled element type %#x, size will be incorrect.\n", element->Type);
2341 continue;
2342 }
2343
2344 type_size = d3dx_decltype_size[element->Type];
2345 if (element->Offset + type_size > size) size = element->Offset + type_size;
2346 }
2347
2348 return size;
2349 }
2350
2351 /*************************************************************************
2352 * D3DXGetDeclLength
2353 */
2354 UINT WINAPI D3DXGetDeclLength(const D3DVERTEXELEMENT9 *decl)
2355 {
2356 const D3DVERTEXELEMENT9 *element;
2357
2358 TRACE("decl %p\n", decl);
2359
2360 /* null decl results in exception on Windows XP */
2361
2362 for (element = decl; element->Stream != 0xff; ++element);
2363
2364 return element - decl;
2365 }
2366
2367 BOOL WINAPI D3DXIntersectTri(const D3DXVECTOR3 *p0, const D3DXVECTOR3 *p1, const D3DXVECTOR3 *p2,
2368 const D3DXVECTOR3 *praypos, const D3DXVECTOR3 *praydir, float *pu, float *pv, float *pdist)
2369 {
2370 D3DXMATRIX m;
2371 D3DXVECTOR4 vec;
2372
2373 m.u.m[0][0] = p1->x - p0->x;
2374 m.u.m[1][0] = p2->x - p0->x;
2375 m.u.m[2][0] = -praydir->x;
2376 m.u.m[3][0] = 0.0f;
2377 m.u.m[0][1] = p1->y - p0->z;
2378 m.u.m[1][1] = p2->y - p0->z;
2379 m.u.m[2][1] = -praydir->y;
2380 m.u.m[3][1] = 0.0f;
2381 m.u.m[0][2] = p1->z - p0->z;
2382 m.u.m[1][2] = p2->z - p0->z;
2383 m.u.m[2][2] = -praydir->z;
2384 m.u.m[3][2] = 0.0f;
2385 m.u.m[0][3] = 0.0f;
2386 m.u.m[1][3] = 0.0f;
2387 m.u.m[2][3] = 0.0f;
2388 m.u.m[3][3] = 1.0f;
2389
2390 vec.x = praypos->x - p0->x;
2391 vec.y = praypos->y - p0->y;
2392 vec.z = praypos->z - p0->z;
2393 vec.w = 0.0f;
2394
2395 if ( D3DXMatrixInverse(&m, NULL, &m) )
2396 {
2397 D3DXVec4Transform(&vec, &vec, &m);
2398 if ( (vec.x >= 0.0f) && (vec.y >= 0.0f) && (vec.x + vec.y <= 1.0f) && (vec.z >= 0.0f) )
2399 {
2400 *pu = vec.x;
2401 *pv = vec.y;
2402 *pdist = fabsf( vec.z );
2403 return TRUE;
2404 }
2405 }
2406
2407 return FALSE;
2408 }
2409
2410 BOOL WINAPI D3DXSphereBoundProbe(const D3DXVECTOR3 *pcenter, float radius,
2411 const D3DXVECTOR3 *prayposition, const D3DXVECTOR3 *praydirection)
2412 {
2413 D3DXVECTOR3 difference;
2414 FLOAT a, b, c, d;
2415
2416 a = D3DXVec3LengthSq(praydirection);
2417 if (!D3DXVec3Subtract(&difference, prayposition, pcenter)) return FALSE;
2418 b = D3DXVec3Dot(&difference, praydirection);
2419 c = D3DXVec3LengthSq(&difference) - radius * radius;
2420 d = b * b - a * c;
2421
2422 if ( ( d <= 0.0f ) || ( sqrt(d) <= b ) ) return FALSE;
2423 return TRUE;
2424 }
2425
2426 /*************************************************************************
2427 * D3DXCreateMesh
2428 */
2429 HRESULT WINAPI D3DXCreateMesh(DWORD numfaces, DWORD numvertices, DWORD options,
2430 const D3DVERTEXELEMENT9 *declaration, struct IDirect3DDevice9 *device, struct ID3DXMesh **mesh)
2431 {
2432 HRESULT hr;
2433 DWORD fvf;
2434 IDirect3DVertexDeclaration9 *vertex_declaration;
2435 UINT vertex_declaration_size;
2436 UINT num_elem;
2437 IDirect3DVertexBuffer9 *vertex_buffer;
2438 IDirect3DIndexBuffer9 *index_buffer;
2439 DWORD *attrib_buffer;
2440 struct d3dx9_mesh *object;
2441 DWORD index_usage = 0;
2442 D3DPOOL index_pool = D3DPOOL_DEFAULT;
2443 D3DFORMAT index_format = D3DFMT_INDEX16;
2444 DWORD vertex_usage = 0;
2445 D3DPOOL vertex_pool = D3DPOOL_DEFAULT;
2446 int i;
2447
2448 TRACE("numfaces %u, numvertices %u, options %#x, declaration %p, device %p, mesh %p.\n",
2449 numfaces, numvertices, options, declaration, device, mesh);
2450
2451 if (numfaces == 0 || numvertices == 0 || declaration == NULL || device == NULL || mesh == NULL ||
2452 /* D3DXMESH_VB_SHARE is for cloning, and D3DXMESH_USEHWONLY is for ConvertToBlendedMesh */
2453 (options & (D3DXMESH_VB_SHARE | D3DXMESH_USEHWONLY | 0xfffe0000)))
2454 {
2455 return D3DERR_INVALIDCALL;
2456 }
2457 for (i = 0; declaration[i].Stream != 0xff; i++)
2458 if (declaration[i].Stream != 0)
2459 return D3DERR_INVALIDCALL;
2460 num_elem = i + 1;
2461
2462 if (options & D3DXMESH_32BIT)
2463 index_format = D3DFMT_INDEX32;
2464
2465 if (options & D3DXMESH_DONOTCLIP) {
2466 index_usage |= D3DUSAGE_DONOTCLIP;
2467 vertex_usage |= D3DUSAGE_DONOTCLIP;
2468 }
2469 if (options & D3DXMESH_POINTS) {
2470 index_usage |= D3DUSAGE_POINTS;
2471 vertex_usage |= D3DUSAGE_POINTS;
2472 }
2473 if (options & D3DXMESH_RTPATCHES) {
2474 index_usage |= D3DUSAGE_RTPATCHES;
2475 vertex_usage |= D3DUSAGE_RTPATCHES;
2476 }
2477 if (options & D3DXMESH_NPATCHES) {
2478 index_usage |= D3DUSAGE_NPATCHES;
2479 vertex_usage |= D3DUSAGE_NPATCHES;
2480 }
2481
2482 if (options & D3DXMESH_VB_SYSTEMMEM)
2483 vertex_pool = D3DPOOL_SYSTEMMEM;
2484 else if (options & D3DXMESH_VB_MANAGED)
2485 vertex_pool = D3DPOOL_MANAGED;
2486
2487 if (options & D3DXMESH_VB_WRITEONLY)
2488 vertex_usage |= D3DUSAGE_WRITEONLY;
2489 if (options & D3DXMESH_VB_DYNAMIC)
2490 vertex_usage |= D3DUSAGE_DYNAMIC;
2491 if (options & D3DXMESH_VB_SOFTWAREPROCESSING)
2492 vertex_usage |= D3DUSAGE_SOFTWAREPROCESSING;
2493
2494 if (options & D3DXMESH_IB_SYSTEMMEM)
2495 index_pool = D3DPOOL_SYSTEMMEM;
2496 else if (options & D3DXMESH_IB_MANAGED)
2497 index_pool = D3DPOOL_MANAGED;
2498
2499 if (options & D3DXMESH_IB_WRITEONLY)
2500 index_usage |= D3DUSAGE_WRITEONLY;
2501 if (options & D3DXMESH_IB_DYNAMIC)
2502 index_usage |= D3DUSAGE_DYNAMIC;
2503 if (options & D3DXMESH_IB_SOFTWAREPROCESSING)
2504 index_usage |= D3DUSAGE_SOFTWAREPROCESSING;
2505
2506 hr = D3DXFVFFromDeclarator(declaration, &fvf);
2507 if (hr != D3D_OK)
2508 {
2509 fvf = 0;
2510 }
2511
2512 /* Create vertex declaration */
2513 hr = IDirect3DDevice9_CreateVertexDeclaration(device,
2514 declaration,
2515 &vertex_declaration);
2516 if (FAILED(hr))
2517 {
2518 WARN("Unexpected return value %x from IDirect3DDevice9_CreateVertexDeclaration.\n",hr);
2519 return hr;
2520 }
2521 vertex_declaration_size = D3DXGetDeclVertexSize(declaration, declaration[0].Stream);
2522
2523 /* Create vertex buffer */
2524 hr = IDirect3DDevice9_CreateVertexBuffer(device,
2525 numvertices * vertex_declaration_size,
2526 vertex_usage,
2527 fvf,
2528 vertex_pool,
2529 &vertex_buffer,
2530 NULL);
2531 if (FAILED(hr))
2532 {
2533 WARN("Unexpected return value %x from IDirect3DDevice9_CreateVertexBuffer.\n",hr);
2534 IDirect3DVertexDeclaration9_Release(vertex_declaration);
2535 return hr;
2536 }
2537
2538 /* Create index buffer */
2539 hr = IDirect3DDevice9_CreateIndexBuffer(device,
2540 numfaces * 3 * ((index_format == D3DFMT_INDEX16) ? 2 : 4),
2541 index_usage,
2542 index_format,
2543 index_pool,
2544 &index_buffer,
2545 NULL);
2546 if (FAILED(hr))
2547 {
2548 WARN("Unexpected return value %x from IDirect3DDevice9_CreateVertexBuffer.\n",hr);
2549 IDirect3DVertexBuffer9_Release(vertex_buffer);
2550 IDirect3DVertexDeclaration9_Release(vertex_declaration);
2551 return hr;
2552 }
2553
2554 attrib_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, numfaces * sizeof(*attrib_buffer));
2555 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2556 if (object == NULL || attrib_buffer == NULL)
2557 {
2558 HeapFree(GetProcessHeap(), 0, object);
2559 HeapFree(GetProcessHeap(), 0, attrib_buffer);
2560 IDirect3DIndexBuffer9_Release(index_buffer);
2561 IDirect3DVertexBuffer9_Release(vertex_buffer);
2562 IDirect3DVertexDeclaration9_Release(vertex_declaration);
2563 *mesh = NULL;
2564 return E_OUTOFMEMORY;
2565 }
2566 object->ID3DXMesh_iface.lpVtbl = &D3DXMesh_Vtbl;
2567 object->ref = 1;
2568
2569 object->numfaces = numfaces;
2570 object->numvertices = numvertices;
2571 object->options = options;
2572 object->fvf = fvf;
2573 object->device = device;
2574 IDirect3DDevice9_AddRef(device);
2575
2576 copy_declaration(object->cached_declaration, declaration, num_elem);
2577 object->vertex_declaration = vertex_declaration;
2578 object->vertex_declaration_size = vertex_declaration_size;
2579 object->num_elem = num_elem;
2580 object->vertex_buffer = vertex_buffer;
2581 object->index_buffer = index_buffer;
2582 object->attrib_buffer = attrib_buffer;
2583
2584 *mesh = &object->ID3DXMesh_iface;
2585
2586 return D3D_OK;
2587 }
2588
2589 /*************************************************************************
2590 * D3DXCreateMeshFVF
2591 */
2592 HRESULT WINAPI D3DXCreateMeshFVF(DWORD numfaces, DWORD numvertices, DWORD options,
2593 DWORD fvf, struct IDirect3DDevice9 *device, struct ID3DXMesh **mesh)
2594 {
2595 HRESULT hr;
2596 D3DVERTEXELEMENT9 declaration[MAX_FVF_DECL_SIZE];
2597
2598 TRACE("(%u, %u, %u, %u, %p, %p)\n", numfaces, numvertices, options, fvf, device, mesh);
2599
2600 hr = D3DXDeclaratorFromFVF(fvf, declaration);
2601 if (FAILED(hr)) return hr;
2602
2603 return D3DXCreateMesh(numfaces, numvertices, options, declaration, device, mesh);
2604 }
2605
2606
2607 struct mesh_data {
2608 DWORD num_vertices;
2609 DWORD num_poly_faces;
2610 DWORD num_tri_faces;
2611 D3DXVECTOR3 *vertices;
2612 DWORD *num_tri_per_face;
2613 DWORD *indices;
2614
2615 DWORD fvf;
2616
2617 /* optional mesh data */
2618
2619 DWORD num_normals;
2620 D3DXVECTOR3 *normals;
2621 DWORD *normal_indices;
2622
2623 D3DXVECTOR2 *tex_coords;
2624
2625 DWORD *vertex_colors;
2626
2627 DWORD num_materials;
2628 D3DXMATERIAL *materials;
2629 DWORD *material_indices;
2630
2631 struct ID3DXSkinInfo *skin_info;
2632 DWORD nb_bones;
2633 };
2634
2635 static HRESULT parse_texture_filename(ID3DXFileData *filedata, char **filename_out)
2636 {
2637 HRESULT hr;
2638 SIZE_T data_size;
2639 BYTE *data;
2640 char *filename_in;
2641 char *filename = NULL;
2642
2643 /* template TextureFilename {
2644 * STRING filename;
2645 * }
2646 */
2647
2648 HeapFree(GetProcessHeap(), 0, *filename_out);
2649 *filename_out = NULL;
2650
2651 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
2652 if (FAILED(hr)) return hr;
2653
2654 /* FIXME: String must be retrieved directly instead of through a pointer once ID3DXFILE is fixed */
2655 if (data_size < sizeof(filename_in))
2656 {
2657 WARN("truncated data (%lu bytes)\n", data_size);
2658 filedata->lpVtbl->Unlock(filedata);
2659 return E_FAIL;
2660 }
2661 filename_in = *(char **)data;
2662
2663 filename = HeapAlloc(GetProcessHeap(), 0, strlen(filename_in) + 1);
2664 if (!filename) {
2665 filedata->lpVtbl->Unlock(filedata);
2666 return E_OUTOFMEMORY;
2667 }
2668
2669 strcpy(filename, filename_in);
2670 *filename_out = filename;
2671
2672 filedata->lpVtbl->Unlock(filedata);
2673
2674 return D3D_OK;
2675 }
2676
2677 static HRESULT parse_material(ID3DXFileData *filedata, D3DXMATERIAL *material)
2678 {
2679 HRESULT hr;
2680 SIZE_T data_size;
2681 const BYTE *data;
2682 GUID type;
2683 ID3DXFileData *child;
2684 SIZE_T i, nb_children;
2685
2686 material->pTextureFilename = NULL;
2687
2688 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
2689 if (FAILED(hr)) return hr;
2690
2691 /*
2692 * template ColorRGBA {
2693 * FLOAT red;
2694 * FLOAT green;
2695 * FLOAT blue;
2696 * FLOAT alpha;
2697 * }
2698 * template ColorRGB {
2699 * FLOAT red;
2700 * FLOAT green;
2701 * FLOAT blue;
2702 * }
2703 * template Material {
2704 * ColorRGBA faceColor;
2705 * FLOAT power;
2706 * ColorRGB specularColor;
2707 * ColorRGB emissiveColor;
2708 * [ ... ]
2709 * }
2710 */
2711 if (data_size != sizeof(FLOAT) * 11) {
2712 WARN("incorrect data size (%ld bytes)\n", data_size);
2713 filedata->lpVtbl->Unlock(filedata);
2714 return E_FAIL;
2715 }
2716
2717 memcpy(&material->MatD3D.Diffuse, data, sizeof(D3DCOLORVALUE));
2718 data += sizeof(D3DCOLORVALUE);
2719 material->MatD3D.Power = *(FLOAT*)data;
2720 data += sizeof(FLOAT);
2721 memcpy(&material->MatD3D.Specular, data, sizeof(FLOAT) * 3);
2722 material->MatD3D.Specular.a = 1.0f;
2723 data += 3 * sizeof(FLOAT);
2724 memcpy(&material->MatD3D.Emissive, data, sizeof(FLOAT) * 3);
2725 material->MatD3D.Emissive.a = 1.0f;
2726 material->MatD3D.Ambient.r = 0.0f;
2727 material->MatD3D.Ambient.g = 0.0f;
2728 material->MatD3D.Ambient.b = 0.0f;
2729 material->MatD3D.Ambient.a = 1.0f;
2730
2731 filedata->lpVtbl->Unlock(filedata);
2732
2733 hr = filedata->lpVtbl->GetChildren(filedata, &nb_children);
2734 if (FAILED(hr))
2735 return hr;
2736
2737 for (i = 0; i < nb_children; i++)
2738 {
2739 hr = filedata->lpVtbl->GetChild(filedata, i, &child);
2740 if (FAILED(hr))
2741 return hr;
2742 hr = child->lpVtbl->GetType(child, &type);
2743 if (FAILED(hr))
2744 return hr;
2745
2746 if (IsEqualGUID(&type, &TID_D3DRMTextureFilename)) {
2747 hr = parse_texture_filename(child, &material->pTextureFilename);
2748 if (FAILED(hr))
2749 return hr;
2750 }
2751 }
2752
2753 return D3D_OK;
2754 }
2755
2756 static void destroy_materials(struct mesh_data *mesh)
2757 {
2758 DWORD i;
2759 for (i = 0; i < mesh->num_materials; i++)
2760 HeapFree(GetProcessHeap(), 0, mesh->materials[i].pTextureFilename);
2761 HeapFree(GetProcessHeap(), 0, mesh->materials);
2762 HeapFree(GetProcessHeap(), 0, mesh->material_indices);
2763 mesh->num_materials = 0;
2764 mesh->materials = NULL;
2765 mesh->material_indices = NULL;
2766 }
2767
2768 static HRESULT parse_material_list(ID3DXFileData *filedata, struct mesh_data *mesh)
2769 {
2770 HRESULT hr;
2771 SIZE_T data_size;
2772 const DWORD *data, *in_ptr;
2773 GUID type;
2774 ID3DXFileData *child;
2775 DWORD num_materials;
2776 DWORD i;
2777 SIZE_T nb_children;
2778
2779 destroy_materials(mesh);
2780
2781 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
2782 if (FAILED(hr)) return hr;
2783
2784 /* template MeshMaterialList {
2785 * DWORD nMaterials;
2786 * DWORD nFaceIndexes;
2787 * array DWORD faceIndexes[nFaceIndexes];
2788 * [ Material ]
2789 * }
2790 */
2791
2792 in_ptr = data;
2793 hr = E_FAIL;
2794
2795 if (data_size < sizeof(DWORD)) {
2796 WARN("truncated data (%ld bytes)\n", data_size);
2797 goto end;
2798 }
2799 num_materials = *in_ptr++;
2800 if (!num_materials) {
2801 hr = D3D_OK;
2802 goto end;
2803 }
2804
2805 if (data_size < 2 * sizeof(DWORD)) {
2806 WARN("truncated data (%ld bytes)\n", data_size);
2807 goto end;
2808 }
2809 if (*in_ptr++ != mesh->num_poly_faces) {
2810 WARN("number of material face indices (%u) doesn't match number of faces (%u)\n",
2811 *(in_ptr - 1), mesh->num_poly_faces);
2812 goto end;
2813 }
2814 if (data_size < 2 * sizeof(DWORD) + mesh->num_poly_faces * sizeof(DWORD)) {
2815 WARN("truncated data (%ld bytes)\n", data_size);
2816 goto end;
2817 }
2818 for (i = 0; i < mesh->num_poly_faces; i++) {
2819 if (*in_ptr++ >= num_materials) {
2820 WARN("face %u: reference to undefined material %u (only %u materials)\n",
2821 i, *(in_ptr - 1), num_materials);
2822 goto end;
2823 }
2824 }
2825
2826 mesh->materials = HeapAlloc(GetProcessHeap(), 0, num_materials * sizeof(*mesh->materials));
2827 mesh->material_indices = HeapAlloc(GetProcessHeap(), 0, mesh->num_poly_faces * sizeof(*mesh->material_indices));
2828 if (!mesh->materials || !mesh->material_indices) {
2829 hr = E_OUTOFMEMORY;
2830 goto end;
2831 }
2832 memcpy(mesh->material_indices, data + 2, mesh->num_poly_faces * sizeof(DWORD));
2833
2834 hr = filedata->lpVtbl->GetChildren(filedata, &nb_children);
2835 if (FAILED(hr))
2836 goto end;
2837
2838 for (i = 0; i < nb_children; i++)
2839 {
2840 hr = filedata->lpVtbl->GetChild(filedata, i, &child);
2841 if (FAILED(hr))
2842 goto end;
2843 hr = child->lpVtbl->GetType(child, &type);
2844 if (FAILED(hr))
2845 goto end;
2846
2847 if (IsEqualGUID(&type, &TID_D3DRMMaterial)) {
2848 if (mesh->num_materials >= num_materials) {
2849 WARN("more materials defined than declared\n");
2850 hr = E_FAIL;
2851 goto end;
2852 }
2853 hr = parse_material(child, &mesh->materials[mesh->num_materials++]);
2854 if (FAILED(hr))
2855 goto end;
2856 }
2857 }
2858 if (num_materials != mesh->num_materials) {
2859 WARN("only %u of %u materials defined\n", num_materials, mesh->num_materials);
2860 hr = E_FAIL;
2861 }
2862
2863 end:
2864 filedata->lpVtbl->Unlock(filedata);
2865 return hr;
2866 }
2867
2868 static HRESULT parse_texture_coords(ID3DXFileData *filedata, struct mesh_data *mesh)
2869 {
2870 HRESULT hr;
2871 SIZE_T data_size;
2872 const BYTE *data;
2873
2874 HeapFree(GetProcessHeap(), 0, mesh->tex_coords);
2875 mesh->tex_coords = NULL;
2876
2877 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
2878 if (FAILED(hr)) return hr;
2879
2880 /* template Coords2d {
2881 * FLOAT u;
2882 * FLOAT v;
2883 * }
2884 * template MeshTextureCoords {
2885 * DWORD nTextureCoords;
2886 * array Coords2d textureCoords[nTextureCoords];
2887 * }
2888 */
2889
2890 hr = E_FAIL;
2891
2892 if (data_size < sizeof(DWORD)) {
2893 WARN("truncated data (%ld bytes)\n", data_size);
2894 goto end;
2895 }
2896 if (*(DWORD*)data != mesh->num_vertices) {
2897 WARN("number of texture coordinates (%u) doesn't match number of vertices (%u)\n",
2898 *(DWORD*)data, mesh->num_vertices);
2899 goto end;
2900 }
2901 data += sizeof(DWORD);
2902 if (data_size < sizeof(DWORD) + mesh->num_vertices * sizeof(*mesh->tex_coords)) {
2903 WARN("truncated data (%ld bytes)\n", data_size);
2904 goto end;
2905 }
2906
2907 mesh->tex_coords = HeapAlloc(GetProcessHeap(), 0, mesh->num_vertices * sizeof(*mesh->tex_coords));
2908 if (!mesh->tex_coords) {
2909 hr = E_OUTOFMEMORY;
2910 goto end;
2911 }
2912 memcpy(mesh->tex_coords, data, mesh->num_vertices * sizeof(*mesh->tex_coords));
2913
2914 mesh->fvf |= D3DFVF_TEX1;
2915
2916 hr = D3D_OK;
2917
2918 end:
2919 filedata->lpVtbl->Unlock(filedata);
2920 return hr;
2921 }
2922
2923 static HRESULT parse_vertex_colors(ID3DXFileData *filedata, struct mesh_data *mesh)
2924 {
2925 HRESULT hr;
2926 SIZE_T data_size;
2927 const BYTE *data;
2928 DWORD num_colors;
2929 DWORD i;
2930
2931 HeapFree(GetProcessHeap(), 0, mesh->vertex_colors);
2932 mesh->vertex_colors = NULL;
2933
2934 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
2935 if (FAILED(hr)) return hr;
2936
2937 /* template IndexedColor {
2938 * DWORD index;
2939 * ColorRGBA indexColor;
2940 * }
2941 * template MeshVertexColors {
2942 * DWORD nVertexColors;
2943 * array IndexedColor vertexColors[nVertexColors];
2944 * }
2945 */
2946
2947 hr = E_FAIL;
2948
2949 if (data_size < sizeof(DWORD)) {
2950 WARN("truncated data (%ld bytes)\n", data_size);
2951 goto end;
2952 }
2953 num_colors = *(DWORD*)data;
2954 data += sizeof(DWORD);
2955 if (data_size < sizeof(DWORD) + num_colors * (sizeof(DWORD) + sizeof(D3DCOLORVALUE))) {
2956 WARN("truncated data (%ld bytes)\n", data_size);
2957 goto end;
2958 }
2959
2960 mesh->vertex_colors = HeapAlloc(GetProcessHeap(), 0, mesh->num_vertices * sizeof(DWORD));
2961 if (!mesh->vertex_colors) {
2962 hr = E_OUTOFMEMORY;
2963 goto end;
2964 }
2965
2966 for (i = 0; i < mesh->num_vertices; i++)
2967 mesh->vertex_colors[i] = D3DCOLOR_ARGB(0, 0xff, 0xff, 0xff);
2968 for (i = 0; i < num_colors; i++)
2969 {
2970 D3DCOLORVALUE color;
2971 DWORD index = *(DWORD*)data;
2972 data += sizeof(DWORD);
2973 if (index >= mesh->num_vertices) {
2974 WARN("vertex color %u references undefined vertex %u (only %u vertices)\n",
2975 i, index, mesh->num_vertices);
2976 goto end;
2977 }
2978 memcpy(&color, data, sizeof(color));
2979 data += sizeof(color);
2980 color.r = min(1.0f, max(0.0f, color.r));
2981 color.g = min(1.0f, max(0.0f, color.g));
2982 color.b = min(1.0f, max(0.0f, color.b));
2983 color.a = min(1.0f, max(0.0f, color.a));
2984 mesh->vertex_colors[index] = D3DCOLOR_ARGB((BYTE)(color.a * 255.0f + 0.5f),
2985 (BYTE)(color.r * 255.0f + 0.5f),
2986 (BYTE)(color.g * 255.0f + 0.5f),
2987 (BYTE)(color.b * 255.0f + 0.5f));
2988 }
2989
2990 mesh->fvf |= D3DFVF_DIFFUSE;
2991
2992 hr = D3D_OK;
2993
2994 end:
2995 filedata->lpVtbl->Unlock(filedata);
2996 return hr;
2997 }
2998
2999 static HRESULT parse_normals(ID3DXFileData *filedata, struct mesh_data *mesh)
3000 {
3001 HRESULT hr;
3002 SIZE_T data_size;
3003 const BYTE *data;
3004 DWORD *index_out_ptr;
3005 DWORD i;
3006 DWORD num_face_indices = mesh->num_poly_faces * 2 + mesh->num_tri_faces;
3007
3008 HeapFree(GetProcessHeap(), 0, mesh->normals);
3009 mesh->num_normals = 0;
3010 mesh->normals = NULL;
3011 mesh->normal_indices = NULL;
3012 mesh->fvf |= D3DFVF_NORMAL;
3013
3014 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
3015 if (FAILED(hr)) return hr;
3016
3017 /* template Vector {
3018 * FLOAT x;
3019 * FLOAT y;
3020 * FLOAT z;
3021 * }
3022 * template MeshFace {
3023 * DWORD nFaceVertexIndices;
3024 * array DWORD faceVertexIndices[nFaceVertexIndices];
3025 * }
3026 * template MeshNormals {
3027 * DWORD nNormals;
3028 * array Vector normals[nNormals];
3029 * DWORD nFaceNormals;
3030 * array MeshFace faceNormals[nFaceNormals];
3031 * }
3032 */
3033
3034 hr = E_FAIL;
3035
3036 if (data_size < sizeof(DWORD) * 2) {
3037 WARN("truncated data (%ld bytes)\n", data_size);
3038 goto end;
3039 }
3040 mesh->num_normals = *(DWORD*)data;
3041 data += sizeof(DWORD);
3042 if (data_size < sizeof(DWORD) * 2 + mesh->num_normals * sizeof(D3DXVECTOR3) +
3043 num_face_indices * sizeof(DWORD)) {
3044 WARN("truncated data (%ld bytes)\n", data_size);
3045 goto end;
3046 }
3047
3048 mesh->normals = HeapAlloc(GetProcessHeap(), 0, mesh->num_normals * sizeof(D3DXVECTOR3));
3049 mesh->normal_indices = HeapAlloc(GetProcessHeap(), 0, num_face_indices * sizeof(DWORD));
3050 if (!mesh->normals || !mesh->normal_indices) {
3051 hr = E_OUTOFMEMORY;
3052 goto end;
3053 }
3054
3055 memcpy(mesh->normals, data, mesh->num_normals * sizeof(D3DXVECTOR3));
3056 data += mesh->num_normals * sizeof(D3DXVECTOR3);
3057 for (i = 0; i < mesh->num_normals; i++)
3058 D3DXVec3Normalize(&mesh->normals[i], &mesh->normals[i]);
3059
3060 if (*(DWORD*)data != mesh->num_poly_faces) {
3061 WARN("number of face normals (%u) doesn't match number of faces (%u)\n",
3062 *(DWORD*)data, mesh->num_poly_faces);
3063 goto end;
3064 }
3065 data += sizeof(DWORD);
3066 index_out_ptr = mesh->normal_indices;
3067 for (i = 0; i < mesh->num_poly_faces; i++)
3068 {
3069 DWORD j;
3070 DWORD count = *(DWORD*)data;
3071 if (count != mesh->num_tri_per_face[i] + 2) {
3072 WARN("face %u: number of normals (%u) doesn't match number of vertices (%u)\n",
3073 i, count, mesh->num_tri_per_face[i] + 2);
3074 goto end;
3075 }
3076 data += sizeof(DWORD);
3077
3078 for (j = 0; j < count; j++) {
3079 DWORD normal_index = *(DWORD*)data;
3080 if (normal_index >= mesh->num_normals) {
3081 WARN("face %u, normal index %u: reference to undefined normal %u (only %u normals)\n",
3082 i, j, normal_index, mesh->num_normals);
3083 goto end;
3084 }
3085 *index_out_ptr++ = normal_index;
3086 data += sizeof(DWORD);
3087 }
3088 }
3089
3090 hr = D3D_OK;
3091
3092 end:
3093 filedata->lpVtbl->Unlock(filedata);
3094 return hr;
3095 }
3096
3097 static HRESULT parse_skin_mesh_info(ID3DXFileData *filedata, struct mesh_data *mesh_data, DWORD index)
3098 {
3099 HRESULT hr;
3100 SIZE_T data_size;
3101 const BYTE *data;
3102
3103 TRACE("(%p, %p, %u)\n", filedata, mesh_data, index);
3104
3105 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
3106 if (FAILED(hr)) return hr;
3107
3108 hr = E_FAIL;
3109
3110 if (!mesh_data->skin_info) {
3111 if (data_size < sizeof(WORD) * 3) {
3112 WARN("truncated data (%ld bytes)\n", data_size);
3113 goto end;
3114 }
3115 /* Skip nMaxSkinWeightsPerVertex and nMaxSkinWeightsPerFace */
3116 data += 2 * sizeof(WORD);
3117 mesh_data->nb_bones = *(WORD*)data;
3118 hr = D3DXCreateSkinInfoFVF(mesh_data->num_vertices, mesh_data->fvf, mesh_data->nb_bones, &mesh_data->skin_info);
3119 } else {
3120 const char *name;
3121 DWORD nb_influences;
3122
3123 /* FIXME: String must be retrieved directly instead of through a pointer once ID3DXFILE is fixed */
3124 name = *(const char**)data;
3125 data += sizeof(char*);
3126
3127 nb_influences = *(DWORD*)data;
3128 data += sizeof(DWORD);
3129
3130 if (data_size < (sizeof(char*) + sizeof(DWORD) + nb_influences * (sizeof(DWORD) + sizeof(FLOAT)) + 16 * sizeof(FLOAT))) {
3131 WARN("truncated data (%ld bytes)\n", data_size);
3132 goto end;
3133 }
3134
3135 hr = mesh_data->skin_info->lpVtbl->SetBoneName(mesh_data->skin_info, index, name);
3136 if (SUCCEEDED(hr))
3137 hr = mesh_data->skin_info->lpVtbl->SetBoneInfluence(mesh_data->skin_info, index, nb_influences,
3138 (const DWORD*)data, (const FLOAT*)(data + nb_influences * sizeof(DWORD)));
3139 if (SUCCEEDED(hr))
3140 hr = mesh_data->skin_info->lpVtbl->SetBoneOffsetMatrix(mesh_data->skin_info, index,
3141 (const D3DMATRIX*)(data + nb_influences * (sizeof(DWORD) + sizeof(FLOAT))));
3142 }
3143
3144 end:
3145 filedata->lpVtbl->Unlock(filedata);
3146 return hr;
3147 }
3148
3149 /* for provide_flags parameters */
3150 #define PROVIDE_MATERIALS 0x1
3151 #define PROVIDE_SKININFO 0x2
3152 #define PROVIDE_ADJACENCY 0x4
3153
3154 static HRESULT parse_mesh(ID3DXFileData *filedata, struct mesh_data *mesh_data, DWORD provide_flags)
3155 {
3156 HRESULT hr;
3157 SIZE_T data_size;
3158 const BYTE *data, *in_ptr;
3159 DWORD *index_out_ptr;
3160 GUID type;
3161 ID3DXFileData *child;
3162 DWORD i;
3163 SIZE_T nb_children;
3164 DWORD nb_skin_weigths_info = 0;
3165
3166 /*
3167 * template Mesh {
3168 * DWORD nVertices;
3169 * array Vector vertices[nVertices];
3170 * DWORD nFaces;
3171 * array MeshFace faces[nFaces];
3172 * [ ... ]
3173 * }
3174 */
3175
3176 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
3177 if (FAILED(hr)) return hr;
3178
3179 in_ptr = data;
3180 hr = E_FAIL;
3181
3182 if (data_size < sizeof(DWORD) * 2) {
3183 WARN("truncated data (%ld bytes)\n", data_size);
3184 goto end;
3185 }
3186 mesh_data->num_vertices = *(DWORD*)in_ptr;
3187 if (data_size < sizeof(DWORD) * 2 + mesh_data->num_vertices * sizeof(D3DXVECTOR3)) {
3188 WARN("truncated data (%ld bytes)\n", data_size);
3189 goto end;
3190 }
3191 in_ptr += sizeof(DWORD) + mesh_data->num_vertices * sizeof(D3DXVECTOR3);
3192
3193 mesh_data->num_poly_faces = *(DWORD*)in_ptr;
3194 in_ptr += sizeof(DWORD);
3195
3196 mesh_data->num_tri_faces = 0;
3197 for (i = 0; i < mesh_data->num_poly_faces; i++)
3198 {
3199 DWORD num_poly_vertices;
3200 DWORD j;
3201
3202 if (data_size - (in_ptr - data) < sizeof(DWORD)) {
3203 WARN("truncated data (%ld bytes)\n", data_size);
3204 goto end;
3205 }
3206 num_poly_vertices = *(DWORD*)in_ptr;
3207 in_ptr += sizeof(DWORD);
3208 if (data_size - (in_ptr - data) < num_poly_vertices * sizeof(DWORD)) {
3209 WARN("truncated data (%ld bytes)\n", data_size);
3210 goto end;
3211 }
3212 if (num_poly_vertices < 3) {
3213 WARN("face %u has only %u vertices\n", i, num_poly_vertices);
3214 goto end;
3215 }
3216 for (j = 0; j < num_poly_vertices; j++) {
3217 if (*(DWORD*)in_ptr >= mesh_data->num_vertices) {
3218 WARN("face %u, index %u: undefined vertex %u (only %u vertices)\n",
3219 i, j, *(DWORD*)in_ptr, mesh_data->num_vertices);
3220 goto end;
3221 }
3222 in_ptr += sizeof(DWORD);
3223 }
3224 mesh_data->num_tri_faces += num_poly_vertices - 2;
3225 }
3226
3227 mesh_data->fvf = D3DFVF_XYZ;
3228
3229 mesh_data->vertices = HeapAlloc(GetProcessHeap(), 0,
3230 mesh_data->num_vertices * sizeof(*mesh_data->vertices));
3231 mesh_data->num_tri_per_face = HeapAlloc(GetProcessHeap(), 0,
3232 mesh_data->num_poly_faces * sizeof(*mesh_data->num_tri_per_face));
3233 mesh_data->indices = HeapAlloc(GetProcessHeap(), 0,
3234 (mesh_data->num_tri_faces + mesh_data->num_poly_faces * 2) * sizeof(*mesh_data->indices));
3235 if (!mesh_data->vertices || !mesh_data->num_tri_per_face || !mesh_data->indices) {
3236 hr = E_OUTOFMEMORY;
3237 goto end;
3238 }
3239
3240 in_ptr = data + sizeof(DWORD);
3241 memcpy(mesh_data->vertices, in_ptr, mesh_data->num_vertices * sizeof(D3DXVECTOR3));
3242 in_ptr += mesh_data->num_vertices * sizeof(D3DXVECTOR3) + sizeof(DWORD);
3243
3244 index_out_ptr = mesh_data->indices;
3245 for (i = 0; i < mesh_data->num_poly_faces; i++)
3246 {
3247 DWORD count;
3248
3249 count = *(DWORD*)in_ptr;
3250 in_ptr += sizeof(DWORD);
3251 mesh_data->num_tri_per_face[i] = count - 2;
3252
3253 while (count--) {
3254 *index_out_ptr++ = *(DWORD*)in_ptr;
3255 in_ptr += sizeof(DWORD);
3256 }
3257 }
3258
3259 hr = filedata->lpVtbl->GetChildren(filedata, &nb_children);
3260 if (FAILED(hr))
3261 goto end;
3262
3263 for (i = 0; i < nb_children; i++)
3264 {
3265 hr = filedata->lpVtbl->GetChild(filedata, i, &child);
3266 if (FAILED(hr))
3267 goto end;
3268 hr = child->lpVtbl->GetType(child, &type);
3269 if (FAILED(hr))
3270 goto end;
3271
3272 if (IsEqualGUID(&type, &TID_D3DRMMeshNormals)) {
3273 hr = parse_normals(child, mesh_data);
3274 } else if (IsEqualGUID(&type, &TID_D3DRMMeshVertexColors)) {
3275 hr = parse_vertex_colors(child, mesh_data);
3276 } else if (IsEqualGUID(&type, &TID_D3DRMMeshTextureCoords)) {
3277 hr = parse_texture_coords(child, mesh_data);
3278 hr = filedata->lpVtbl->GetChild(filedata, i, &child);
3279 if (FAILED(hr))
3280 goto end;
3281 } else if (IsEqualGUID(&type, &TID_D3DRMMeshMaterialList) &&
3282 (provide_flags & PROVIDE_MATERIALS))
3283 {
3284 hr = parse_material_list(child, mesh_data);
3285 } else if (provide_flags & PROVIDE_SKININFO) {
3286 if (IsEqualGUID(&type, &DXFILEOBJ_XSkinMeshHeader)) {
3287 if (mesh_data->skin_info) {
3288 WARN("Skin mesh header already encountered\n");
3289 hr = E_FAIL;
3290 goto end;
3291 }
3292 hr = parse_skin_mesh_info(child, mesh_data, 0);
3293 if (FAILED(hr))
3294 goto end;
3295 } else if (IsEqualGUID(&type, &DXFILEOBJ_SkinWeights)) {
3296 if (!mesh_data->skin_info) {
3297 WARN("Skin weigths found but skin mesh header not encountered yet\n");
3298 hr = E_FAIL;
3299 goto end;
3300 }
3301 hr = parse_skin_mesh_info(child, mesh_data, nb_skin_weigths_info);
3302 if (FAILED(hr))
3303 goto end;
3304 nb_skin_weigths_info++;
3305 }
3306 }
3307 if (FAILED(hr))
3308 goto end;
3309 }
3310
3311 if (mesh_data->skin_info && (nb_skin_weigths_info != mesh_data->nb_bones)) {
3312 WARN("Mismatch between nb skin weights info %u encountered and nb bones %u from skin mesh header\n",
3313 nb_skin_weigths_info, mesh_data->nb_bones);
3314 hr = E_FAIL;
3315 goto end;
3316 }
3317
3318 hr = D3D_OK;
3319
3320 end:
3321 filedata->lpVtbl->Unlock(filedata);
3322 return hr;
3323 }
3324
3325 static HRESULT generate_effects(ID3DXBuffer *materials, DWORD num_materials,
3326 ID3DXBuffer **effects)
3327 {
3328 HRESULT hr;
3329 D3DXEFFECTINSTANCE *effect_ptr;
3330 BYTE *out_ptr;
3331 const D3DXMATERIAL *material_ptr = ID3DXBuffer_GetBufferPointer(materials);
3332 static const struct {
3333 const char *param_name;
3334 DWORD name_size;
3335 DWORD num_bytes;
3336 DWORD value_offset;
3337 } material_effects[] = {
3338 #define EFFECT_TABLE_ENTRY(str, field) \
3339 {str, sizeof(str), sizeof(material_ptr->MatD3D.field), offsetof(D3DXMATERIAL, MatD3D.field)}
3340 EFFECT_TABLE_ENTRY("Diffuse", Diffuse),
3341 EFFECT_TABLE_ENTRY("Power", Power),
3342 EFFECT_TABLE_ENTRY("Specular", Specular),
3343 EFFECT_TABLE_ENTRY("Emissive", Emissive),
3344 EFFECT_TABLE_ENTRY("Ambient", Ambient),
3345 #undef EFFECT_TABLE_ENTRY
3346 };
3347 static const char texture_paramname[] = "Texture0@Name";
3348 DWORD buffer_size;
3349 DWORD i;
3350
3351 /* effects buffer layout:
3352 *
3353 * D3DXEFFECTINSTANCE effects[num_materials];
3354 * for (effect in effects)
3355 * {
3356 * D3DXEFFECTDEFAULT defaults[effect.NumDefaults];
3357 * for (default in defaults)
3358 * {
3359 * *default.pParamName;
3360 * *default.pValue;
3361 * }
3362 * }
3363 */
3364 buffer_size = sizeof(D3DXEFFECTINSTANCE);
3365 buffer_size += sizeof(D3DXEFFECTDEFAULT) * ARRAY_SIZE(material_effects);
3366 for (i = 0; i < ARRAY_SIZE(material_effects); i++) {
3367 buffer_size += material_effects[i].name_size;
3368 buffer_size += material_effects[i].num_bytes;
3369 }
3370 buffer_size *= num_materials;
3371 for (i = 0; i < num_materials; i++) {
3372 if (material_ptr[i].pTextureFilename) {
3373 buffer_size += sizeof(D3DXEFFECTDEFAULT);
3374 buffer_size += sizeof(texture_paramname);
3375 buffer_size += strlen(material_ptr[i].pTextureFilename) + 1;
3376 }
3377 }
3378
3379 hr = D3DXCreateBuffer(buffer_size, effects);
3380 if (FAILED(hr)) return hr;
3381 effect_ptr = ID3DXBuffer_GetBufferPointer(*effects);
3382 out_ptr = (BYTE*)(effect_ptr + num_materials);
3383
3384 for (i = 0; i < num_materials; i++)
3385 {
3386 DWORD j;
3387 D3DXEFFECTDEFAULT *defaults = (D3DXEFFECTDEFAULT*)out_ptr;
3388
3389 effect_ptr->pDefaults = defaults;
3390 effect_ptr->NumDefaults = material_ptr->pTextureFilename ? 6 : 5;
3391 out_ptr = (BYTE*)(effect_ptr->pDefaults + effect_ptr->NumDefaults);
3392
3393 for (j = 0; j < ARRAY_SIZE(material_effects); j++)
3394 {
3395 defaults->pParamName = (char *)out_ptr;
3396 strcpy(defaults->pParamName, material_effects[j].param_name);
3397 defaults->pValue = defaults->pParamName + material_effects[j].name_size;
3398 defaults->Type = D3DXEDT_FLOATS;
3399 defaults->NumBytes = material_effects[j].num_bytes;
3400 memcpy(defaults->pValue, (BYTE*)material_ptr + material_effects[j].value_offset, defaults->NumBytes);
3401 out_ptr = (BYTE*)defaults->pValue + defaults->NumBytes;
3402 defaults++;
3403 }
3404
3405 if (material_ptr->pTextureFilename)
3406 {
3407 defaults->pParamName = (char *)out_ptr;
3408 strcpy(defaults->pParamName, texture_paramname);
3409 defaults->pValue = defaults->pParamName + sizeof(texture_paramname);
3410 defaults->Type = D3DXEDT_STRING;
3411 defaults->NumBytes = strlen(material_ptr->pTextureFilename) + 1;
3412 strcpy(defaults->pValue, material_ptr->pTextureFilename);
3413 out_ptr = (BYTE*)defaults->pValue + defaults->NumBytes;
3414 }
3415 material_ptr++;
3416 effect_ptr++;
3417 }
3418 assert(out_ptr - (BYTE*)ID3DXBuffer_GetBufferPointer(*effects) == buffer_size);
3419
3420 return D3D_OK;
3421 }
3422
3423 HRESULT WINAPI D3DXLoadSkinMeshFromXof(struct ID3DXFileData *filedata, DWORD options,
3424 struct IDirect3DDevice9 *device, struct ID3DXBuffer **adjacency_out, struct ID3DXBuffer **materials_out,
3425 struct ID3DXBuffer **effects_out, DWORD *num_materials_out, struct ID3DXSkinInfo **skin_info_out,
3426 struct ID3DXMesh **mesh_out)
3427 {
3428 HRESULT hr;
3429 DWORD *index_in_ptr;
3430 struct mesh_data mesh_data;
3431 DWORD total_vertices;
3432 ID3DXMesh *d3dxmesh = NULL;
3433 ID3DXBuffer *adjacency = NULL;
3434 ID3DXBuffer *materials = NULL;
3435 ID3DXBuffer *effects = NULL;
3436 struct vertex_duplication {
3437 DWORD normal_index;
3438 struct list entry;
3439 } *duplications = NULL;
3440 DWORD i;
3441 void *vertices = NULL;
3442 void *indices = NULL;
3443 BYTE *out_ptr;
3444 DWORD provide_flags = 0;
3445
3446 TRACE("(%p, %x, %p, %p, %p, %p, %p, %p, %p)\n", filedata, options, device, adjacency_out, materials_out,
3447 effects_out, num_materials_out, skin_info_out, mesh_out);
3448
3449 ZeroMemory(&mesh_data, sizeof(mesh_data));
3450
3451 if (num_materials_out || materials_out || effects_out)
3452 provide_flags |= PROVIDE_MATERIALS;
3453 if (skin_info_out)
3454 provide_flags |= PROVIDE_SKININFO;
3455
3456 hr = parse_mesh(filedata, &mesh_data, provide_flags);
3457 if (FAILED(hr)) goto cleanup;
3458
3459 total_vertices = mesh_data.num_vertices;
3460 if (mesh_data.fvf & D3DFVF_NORMAL) {
3461 /* duplicate vertices with multiple normals */
3462 DWORD num_face_indices = mesh_data.num_poly_faces * 2 + mesh_data.num_tri_faces;
3463 duplications = HeapAlloc(GetProcessHeap(), 0, (mesh_data.num_vertices + num_face_indices) * sizeof(*duplications));
3464 if (!duplications) {
3465 hr = E_OUTOFMEMORY;
3466 goto cleanup;
3467 }
3468 for (i = 0; i < total_vertices; i++)
3469 {
3470 duplications[i].normal_index = -1;
3471 list_init(&duplications[i].entry);
3472 }
3473 for (i = 0; i < num_face_indices; i++) {
3474 DWORD vertex_index = mesh_data.indices[i];
3475 DWORD normal_index = mesh_data.normal_indices[i];
3476 struct vertex_duplication *dup_ptr = &duplications[vertex_index];
3477
3478 if (dup_ptr->normal_index == -1) {
3479 dup_ptr->normal_index = normal_index;
3480 } else {
3481 D3DXVECTOR3 *new_normal = &mesh_data.normals[normal_index];
3482 struct list *dup_list = &dup_ptr->entry;
3483 while (TRUE) {
3484 D3DXVECTOR3 *cur_normal = &mesh_data.normals[dup_ptr->normal_index];
3485 if (new_normal->x == cur_normal->x &&
3486 new_normal->y == cur_normal->y &&
3487 new_normal->z == cur_normal->z)
3488 {
3489 mesh_data.indices[i] = dup_ptr - duplications;
3490 break;
3491 } else if (!list_next(dup_list, &dup_ptr->entry)) {
3492 dup_ptr = &duplications[total_vertices++];
3493 dup_ptr->normal_index = normal_index;
3494 list_add_tail(dup_list, &dup_ptr->entry);
3495 mesh_data.indices[i] = dup_ptr - duplications;
3496 break;
3497 } else {
3498 dup_ptr = LIST_ENTRY(list_next(dup_list, &dup_ptr->entry),
3499 struct vertex_duplication, entry);
3500 }
3501 }
3502 }
3503 }
3504 }
3505
3506 hr = D3DXCreateMeshFVF(mesh_data.num_tri_faces, total_vertices, options, mesh_data.fvf, device, &d3dxmesh);
3507 if (FAILED(hr)) goto cleanup;
3508
3509 hr = d3dxmesh->lpVtbl->LockVertexBuffer(d3dxmesh, 0, &vertices);
3510 if (FAILED(hr)) goto cleanup;
3511
3512 out_ptr = vertices;
3513 for (i = 0; i < mesh_data.num_vertices; i++) {
3514 *(D3DXVECTOR3*)out_ptr = mesh_data.vertices[i];
3515 out_ptr += sizeof(D3DXVECTOR3);
3516 if (mesh_data.fvf & D3DFVF_NORMAL) {
3517 if (duplications[i].normal_index == -1)
3518 ZeroMemory(out_ptr, sizeof(D3DXVECTOR3));
3519 else
3520 *(D3DXVECTOR3*)out_ptr = mesh_data.normals[duplications[i].normal_index];
3521 out_ptr += sizeof(D3DXVECTOR3);
3522 }
3523 if (mesh_data.fvf & D3DFVF_DIFFUSE) {
3524 *(DWORD*)out_ptr = mesh_data.vertex_colors[i];
3525 out_ptr += sizeof(DWORD);
3526 }
3527 if (mesh_data.fvf & D3DFVF_TEX1) {
3528 *(D3DXVECTOR2*)out_ptr = mesh_data.tex_coords[i];
3529 out_ptr += sizeof(D3DXVECTOR2);
3530 }
3531 }
3532 if (mesh_data.fvf & D3DFVF_NORMAL) {
3533 DWORD vertex_size = D3DXGetFVFVertexSize(mesh_data.fvf);
3534 out_ptr = vertices;
3535 for (i = 0; i < mesh_data.num_vertices; i++) {
3536 struct vertex_duplication *dup_ptr;
3537 LIST_FOR_EACH_ENTRY(dup_ptr, &duplications[i].entry, struct vertex_duplication, entry)
3538 {
3539 int j = dup_ptr - duplications;
3540 BYTE *dest_vertex = (BYTE*)vertices + j * vertex_size;
3541
3542 memcpy(dest_vertex, out_ptr, vertex_size);
3543 dest_vertex += sizeof(D3DXVECTOR3);
3544 *(D3DXVECTOR3*)dest_vertex = mesh_data.normals[dup_ptr->normal_index];
3545 }
3546 out_ptr += vertex_size;
3547 }
3548 }
3549 d3dxmesh->lpVtbl->UnlockVertexBuffer(d3dxmesh);
3550
3551 hr = d3dxmesh->lpVtbl->LockIndexBuffer(d3dxmesh, 0, &indices);
3552 if (FAILED(hr)) goto cleanup;
3553
3554 index_in_ptr = mesh_data.indices;
3555 #define FILL_INDEX_BUFFER(indices_var) \
3556 for (i = 0; i < mesh_data.num_poly_faces; i++) \
3557 { \
3558 DWORD count = mesh_data.num_tri_per_face[i]; \
3559 WORD first_index = *index_in_ptr++; \
3560 while (count--) { \
3561 *indices_var++ = first_index; \
3562 *indices_var++ = *index_in_ptr; \
3563 index_in_ptr++; \
3564 *indices_var++ = *index_in_ptr; \
3565 } \
3566 index_in_ptr++; \
3567 }
3568 if (options & D3DXMESH_32BIT) {
3569 DWORD *dword_indices = indices;
3570 FILL_INDEX_BUFFER(dword_indices)
3571 } else {
3572 WORD *word_indices = indices;
3573 FILL_INDEX_BUFFER(word_indices)
3574 }
3575 #undef FILL_INDEX_BUFFER
3576 d3dxmesh->lpVtbl->UnlockIndexBuffer(d3dxmesh);
3577
3578 if (mesh_data.material_indices) {
3579 DWORD *attrib_buffer = NULL;
3580 hr = d3dxmesh->lpVtbl->LockAttributeBuffer(d3dxmesh, 0, &attrib_buffer);
3581 if (FAILED(hr)) goto cleanup;
3582 for (i = 0; i < mesh_data.num_poly_faces; i++)
3583 {
3584 DWORD count = mesh_data.num_tri_per_face[i];
3585 while (count--)
3586 *attrib_buffer++ = mesh_data.material_indices[i];
3587 }
3588 d3dxmesh->lpVtbl->UnlockAttributeBuffer(d3dxmesh);
3589
3590 hr = d3dxmesh->lpVtbl->OptimizeInplace(d3dxmesh,
3591 D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_IGNOREVERTS | D3DXMESHOPT_DONOTSPLIT,
3592 NULL, NULL, NULL, NULL);
3593 if (FAILED(hr)) goto cleanup;
3594 }
3595
3596 if (mesh_data.num_materials && (materials_out || effects_out)) {
3597 DWORD buffer_size = mesh_data.num_materials * sizeof(D3DXMATERIAL);
3598 char *strings_out_ptr;
3599 D3DXMATERIAL *materials_ptr;
3600
3601 for (i = 0; i < mesh_data.num_materials; i++) {
3602 if (mesh_data.materials[i].pTextureFilename)
3603 buffer_size += strlen(mesh_data.materials[i].pTextureFilename) + 1;
3604 }
3605
3606 hr = D3DXCreateBuffer(buffer_size, &materials);
3607 if (FAILED(hr)) goto cleanup;
3608
3609 materials_ptr = ID3DXBuffer_GetBufferPointer(materials);
3610 memcpy(materials_ptr, mesh_data.materials, mesh_data.num_materials * sizeof(D3DXMATERIAL));
3611 strings_out_ptr = (char*)(materials_ptr + mesh_data.num_materials);
3612 for (i = 0; i < mesh_data.num_materials; i++) {
3613 if (materials_ptr[i].pTextureFilename) {
3614 strcpy(strings_out_ptr, mesh_data.materials[i].pTextureFilename);
3615 materials_ptr[i].pTextureFilename = strings_out_ptr;
3616 strings_out_ptr += strlen(mesh_data.materials[i].pTextureFilename) + 1;
3617 }
3618 }
3619 }
3620
3621 if (mesh_data.num_materials && effects_out) {
3622 hr = generate_effects(materials, mesh_data.num_materials, &effects);
3623 if (FAILED(hr)) goto cleanup;
3624
3625 if (!materials_out) {
3626 ID3DXBuffer_Release(materials);
3627 materials = NULL;
3628 }
3629 }
3630
3631 if (adjacency_out) {
3632 hr = D3DXCreateBuffer(mesh_data.num_tri_faces * 3 * sizeof(DWORD), &adjacency);
3633 if (FAILED(hr)) goto cleanup;
3634 hr = d3dxmesh->lpVtbl->GenerateAdjacency(d3dxmesh, 0.0f, ID3DXBuffer_GetBufferPointer(adjacency));
3635 if (FAILED(hr)) goto cleanup;
3636 }
3637
3638 *mesh_out = d3dxmesh;
3639 if (adjacency_out) *adjacency_out = adjacency;
3640 if (num_materials_out) *num_materials_out = mesh_data.num_materials;
3641 if (materials_out) *materials_out = materials;
3642 if (effects_out) *effects_out = effects;
3643 if (skin_info_out) *skin_info_out = mesh_data.skin_info;
3644
3645 hr = D3D_OK;
3646 cleanup:
3647 if (FAILED(hr)) {
3648 if (d3dxmesh) IUnknown_Release(d3dxmesh);
3649 if (adjacency) ID3DXBuffer_Release(adjacency);
3650 if (materials) ID3DXBuffer_Release(materials);
3651 if (effects) ID3DXBuffer_Release(effects);
3652 if (mesh_data.skin_info) mesh_data.skin_info->lpVtbl->Release(mesh_data.skin_info);
3653 if (skin_info_out) *skin_info_out = NULL;
3654 }
3655 HeapFree(GetProcessHeap(), 0, mesh_data.vertices);
3656 HeapFree(GetProcessHeap(), 0, mesh_data.num_tri_per_face);
3657 HeapFree(GetProcessHeap(), 0, mesh_data.indices);
3658 HeapFree(GetProcessHeap(), 0, mesh_data.normals);
3659 HeapFree(GetProcessHeap(), 0, mesh_data.normal_indices);
3660 destroy_materials(&mesh_data);
3661 HeapFree(GetProcessHeap(), 0, mesh_data.tex_coords);
3662 HeapFree(GetProcessHeap(), 0, mesh_data.vertex_colors);
3663 HeapFree(GetProcessHeap(), 0, duplications);
3664 return hr;
3665 }
3666
3667 HRESULT WINAPI D3DXLoadMeshHierarchyFromXA(const char *filename, DWORD options, struct IDirect3DDevice9 *device,
3668 struct ID3DXAllocateHierarchy *alloc_hier, struct ID3DXLoadUserData *load_user_data,
3669 D3DXFRAME **frame_hierarchy, struct ID3DXAnimationController **anim_controller)
3670 {
3671 WCHAR *filenameW;
3672 HRESULT hr;
3673 int len;
3674
3675 TRACE("filename %s, options %#x, device %p, alloc_hier %p, "
3676 "load_user_data %p, frame_hierarchy %p, anim_controller %p.\n",
3677 debugstr_a(filename), options, device, alloc_hier,
3678 load_user_data, frame_hierarchy, anim_controller);
3679
3680 if (!filename)
3681 return D3DERR_INVALIDCALL;
3682
3683 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
3684 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3685 if (!filenameW) return E_OUTOFMEMORY;
3686 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
3687
3688 hr = D3DXLoadMeshHierarchyFromXW(filenameW, options, device,
3689 alloc_hier, load_user_data, frame_hierarchy, anim_controller);
3690 HeapFree(GetProcessHeap(), 0, filenameW);
3691
3692 return hr;
3693 }
3694
3695 HRESULT WINAPI D3DXLoadMeshHierarchyFromXW(const WCHAR *filename, DWORD options, struct IDirect3DDevice9 *device,
3696 struct ID3DXAllocateHierarchy *alloc_hier, struct ID3DXLoadUserData *load_user_data,
3697 D3DXFRAME **frame_hierarchy, struct ID3DXAnimationController **anim_controller)
3698 {
3699 void *buffer;
3700 HRESULT hr;
3701 DWORD size;
3702
3703 TRACE("filename %s, options %#x, device %p, alloc_hier %p, "
3704 "load_user_data %p, frame_hierarchy %p, anim_controller %p.\n",
3705 debugstr_w(filename), options, device, alloc_hier,
3706 load_user_data, frame_hierarchy, anim_controller);
3707
3708 if (!filename)
3709 return D3DERR_INVALIDCALL;
3710
3711 hr = map_view_of_file(filename, &buffer, &size);
3712 if (FAILED(hr))
3713 return D3DXERR_INVALIDDATA;
3714
3715 hr = D3DXLoadMeshHierarchyFromXInMemory(buffer, size, options, device,
3716 alloc_hier, load_user_data, frame_hierarchy, anim_controller);
3717
3718 UnmapViewOfFile(buffer);
3719
3720 return hr;
3721 }
3722
3723 static HRESULT filedata_get_name(ID3DXFileData *filedata, char **name)
3724 {
3725 HRESULT hr;
3726 SIZE_T name_len;
3727
3728 hr = filedata->lpVtbl->GetName(filedata, NULL, &name_len);
3729 if (FAILED(hr)) return hr;
3730
3731 if (!name_len)
3732 name_len++;
3733 *name = HeapAlloc(GetProcessHeap(), 0, name_len);
3734 if (!*name) return E_OUTOFMEMORY;
3735
3736 hr = filedata->lpVtbl->GetName(filedata, *name, &name_len);
3737 if (FAILED(hr))
3738 HeapFree(GetProcessHeap(), 0, *name);
3739 else if (!name_len)
3740 (*name)[0] = 0;
3741
3742 return hr;
3743 }
3744
3745 static HRESULT load_mesh_container(struct ID3DXFileData *filedata, DWORD options, struct IDirect3DDevice9 *device,
3746 struct ID3DXAllocateHierarchy *alloc_hier, D3DXMESHCONTAINER **mesh_container)
3747 {
3748 HRESULT hr;
3749 ID3DXBuffer *adjacency = NULL;
3750 ID3DXBuffer *materials = NULL;
3751 ID3DXBuffer *effects = NULL;
3752 ID3DXSkinInfo *skin_info = NULL;
3753 D3DXMESHDATA mesh_data;
3754 DWORD num_materials = 0;
3755 char *name = NULL;
3756
3757 mesh_data.Type = D3DXMESHTYPE_MESH;
3758 mesh_data.u.pMesh = NULL;
3759
3760 hr = D3DXLoadSkinMeshFromXof(filedata, options, device,
3761 &adjacency, &materials, &effects, &num_materials,
3762 &skin_info, &mesh_data.u.pMesh);
3763 if (FAILED(hr)) return hr;
3764
3765 hr = filedata_get_name(filedata, &name);
3766 if (FAILED(hr)) goto cleanup;
3767
3768 hr = alloc_hier->lpVtbl->CreateMeshContainer(alloc_hier, name, &mesh_data,
3769 materials ? ID3DXBuffer_GetBufferPointer(materials) : NULL,
3770 effects ? ID3DXBuffer_GetBufferPointer(effects) : NULL,
3771 num_materials,
3772 adjacency ? ID3DXBuffer_GetBufferPointer(adjacency) : NULL,
3773 skin_info, mesh_container);
3774
3775 cleanup:
3776 if (materials) ID3DXBuffer_Release(materials);
3777 if (effects) ID3DXBuffer_Release(effects);
3778 if (adjacency) ID3DXBuffer_Release(adjacency);
3779 if (skin_info) IUnknown_Release(skin_info);
3780 if (mesh_data.u.pMesh) IUnknown_Release(mesh_data.u.pMesh);
3781 HeapFree(GetProcessHeap(), 0, name);
3782 return hr;
3783 }
3784
3785 static HRESULT parse_transform_matrix(ID3DXFileData *filedata, D3DXMATRIX *transform)
3786 {
3787 HRESULT hr;
3788 SIZE_T data_size;
3789 const BYTE *data;
3790
3791 /* template Matrix4x4 {
3792 * array FLOAT matrix[16];
3793 * }
3794 * template FrameTransformMatrix {
3795 * Matrix4x4 frameMatrix;
3796 * }
3797 */
3798
3799 hr = filedata->lpVtbl->Lock(filedata, &data_size, (const void**)&data);
3800 if (FAILED(hr)) return hr;
3801
3802 if (data_size != sizeof(D3DXMATRIX)) {
3803 WARN("incorrect data size (%ld bytes)\n", data_size);
3804 filedata->lpVtbl->Unlock(filedata);
3805 return E_FAIL;
3806 }
3807
3808 memcpy(transform, data, sizeof(D3DXMATRIX));
3809
3810 filedata->lpVtbl->Unlock(filedata);
3811 return D3D_OK;
3812 }
3813
3814 static HRESULT load_frame(struct ID3DXFileData *filedata, DWORD options, struct IDirect3DDevice9 *device,
3815 struct ID3DXAllocateHierarchy *alloc_hier, D3DXFRAME **frame_out)
3816 {
3817 HRESULT hr;
3818 GUID type;
3819 ID3DXFileData *child;
3820 char *name = NULL;
3821 D3DXFRAME *frame = NULL;
3822 D3DXMESHCONTAINER **next_container;
3823 D3DXFRAME **next_child;
3824 SIZE_T i, nb_children;
3825
3826 hr = filedata_get_name(filedata, &name);
3827 if (FAILED(hr)) return hr;
3828
3829 hr = alloc_hier->lpVtbl->CreateFrame(alloc_hier, name, frame_out);
3830 HeapFree(GetProcessHeap(), 0, name);
3831 if (FAILED(hr)) return E_FAIL;
3832
3833 frame = *frame_out;
3834 D3DXMatrixIdentity(&frame->TransformationMatrix);
3835 next_child = &frame->pFrameFirstChild;
3836 next_container = &frame->pMeshContainer;
3837
3838 hr = filedata->lpVtbl->GetChildren(filedata, &nb_children);
3839 if (FAILED(hr))
3840 return hr;
3841
3842 for (i = 0; i < nb_children; i++)
3843 {
3844 hr = filedata->lpVtbl->GetChild(filedata, i, &child);
3845 if (FAILED(hr))
3846 return hr;
3847 hr = child->lpVtbl->GetType(child, &type);
3848 if (FAILED(hr))
3849 return hr;
3850
3851 if (IsEqualGUID(&type, &TID_D3DRMMesh)) {
3852 hr = load_mesh_container(child, options, device, alloc_hier, next_container);
3853 if (SUCCEEDED(hr))
3854 next_container = &(*next_container)->pNextMeshContainer;
3855 } else if (IsEqualGUID(&type, &TID_D3DRMFrameTransformMatrix)) {
3856 hr = parse_transform_matrix(child, &frame->TransformationMatrix);
3857 } else if (IsEqualGUID(&type, &TID_D3DRMFrame)) {
3858 hr = load_frame(child, options, device, alloc_hier, next_child);
3859 if (SUCCEEDED(hr))
3860 next_child = &(*next_child)->pFrameSibling;
3861 }
3862 if (FAILED(hr))
3863 return hr;
3864 }
3865
3866 return D3D_OK;
3867 }
3868
3869 HRESULT WINAPI D3DXLoadMeshHierarchyFromXInMemory(const void *memory, DWORD memory_size, DWORD options,
3870 struct IDirect3DDevice9 *device, struct ID3DXAllocateHierarchy *alloc_hier,
3871 struct ID3DXLoadUserData *load_user_data, D3DXFRAME **frame_hierarchy,
3872 struct ID3DXAnimationController **anim_controller)
3873 {
3874 HRESULT hr;
3875 ID3DXFile *d3dxfile = NULL;
3876 ID3DXFileEnumObject *enumobj = NULL;
3877 ID3DXFileData *filedata = NULL;
3878 D3DXF_FILELOADMEMORY source;
3879 D3DXFRAME *first_frame = NULL;
3880 D3DXFRAME **next_frame = &first_frame;
3881 SIZE_T i, nb_children;
3882 GUID guid;
3883
3884 TRACE("(%p, %u, %x, %p, %p, %p, %p, %p)\n", memory, memory_size, options,
3885 device, alloc_hier, load_user_data, frame_hierarchy, anim_controller);
3886
3887 if (!memory || !memory_size || !device || !frame_hierarchy || !alloc_hier)
3888 return D3DERR_INVALIDCALL;
3889 if (load_user_data || anim_controller) {
3890 if (load_user_data)
3891 FIXME("Loading user data not implemented\n");
3892 if (anim_controller)
3893 FIXME("Animation controller creation not implemented\n");
3894 return E_NOTIMPL;
3895 }
3896
3897 hr = D3DXFileCreate(&d3dxfile);
3898 if (FAILED(hr)) goto cleanup;
3899
3900 hr = d3dxfile->lpVtbl->RegisterTemplates(d3dxfile, D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES);
3901 if (FAILED(hr)) goto cleanup;
3902
3903 source.lpMemory = (void*)memory;
3904 source.dSize = memory_size;
3905 hr = d3dxfile->lpVtbl->CreateEnumObject(d3dxfile, &source, D3DXF_FILELOAD_FROMMEMORY, &enumobj);
3906 if (FAILED(hr)) goto cleanup;
3907
3908 hr = enumobj->lpVtbl->GetChildren(enumobj, &nb_children);
3909 if (FAILED(hr))
3910 goto cleanup;
3911
3912 for (i = 0; i < nb_children; i++)
3913 {
3914 hr = enumobj->lpVtbl->GetChild(enumobj, i, &filedata);
3915 if (FAILED(hr))
3916 goto cleanup;
3917
3918 hr = filedata->lpVtbl->GetType(filedata, &guid);
3919 if (SUCCEEDED(hr)) {
3920 if (IsEqualGUID(&guid, &TID_D3DRMMesh)) {
3921 hr = alloc_hier->lpVtbl->CreateFrame(alloc_hier, NULL, next_frame);
3922 if (FAILED(hr)) {
3923 hr = E_FAIL;
3924 goto cleanup;
3925 }
3926
3927 D3DXMatrixIdentity(&(*next_frame)->TransformationMatrix);
3928
3929 hr = load_mesh_container(filedata, options, device, alloc_hier, &(*next_frame)->pMeshContainer);
3930 if (FAILED(hr)) goto cleanup;
3931 } else if (IsEqualGUID(&guid, &TID_D3DRMFrame)) {
3932 hr = load_frame(filedata, options, device, alloc_hier, next_frame);
3933 if (FAILED(hr)) goto cleanup;
3934 }
3935 while (*next_frame)
3936 next_frame = &(*next_frame)->pFrameSibling;
3937 }
3938
3939 filedata->lpVtbl->Release(filedata);
3940 filedata = NULL;
3941 if (FAILED(hr))
3942 goto cleanup;
3943 }
3944
3945 if (!first_frame) {
3946 hr = E_FAIL;
3947 } else if (first_frame->pFrameSibling) {
3948 D3DXFRAME *root_frame = NULL;
3949 hr = alloc_hier->lpVtbl->CreateFrame(alloc_hier, NULL, &root_frame);
3950 if (FAILED(hr)) {
3951 hr = E_FAIL;
3952 goto cleanup;
3953 }
3954 D3DXMatrixIdentity(&root_frame->TransformationMatrix);
3955 root_frame->pFrameFirstChild = first_frame;
3956 *frame_hierarchy = root_frame;
3957 hr = D3D_OK;
3958 } else {
3959 *frame_hierarchy = first_frame;
3960 hr = D3D_OK;
3961 }
3962
3963 cleanup:
3964 if (FAILED(hr) && first_frame) D3DXFrameDestroy(first_frame, alloc_hier);
3965 if (filedata) filedata->lpVtbl->Release(filedata);
3966 if (enumobj) enumobj->lpVtbl->Release(enumobj);
3967 if (d3dxfile) d3dxfile->lpVtbl->Release(d3dxfile);
3968 return hr;
3969 }
3970
3971 HRESULT WINAPI D3DXCleanMesh(D3DXCLEANTYPE clean_type, ID3DXMesh *mesh_in, const DWORD *adjacency_in,
3972 ID3DXMesh **mesh_out, DWORD *adjacency_out, ID3DXBuffer **errors_and_warnings)
3973 {
3974 FIXME("(%u, %p, %p, %p, %p, %p)\n", clean_type, mesh_in, adjacency_in, mesh_out, adjacency_out, errors_and_warnings);
3975
3976 return E_NOTIMPL;
3977 }
3978
3979 HRESULT WINAPI D3DXFrameDestroy(D3DXFRAME *frame, ID3DXAllocateHierarchy *alloc_hier)
3980 {
3981 HRESULT hr;
3982 BOOL last = FALSE;
3983
3984 TRACE("(%p, %p)\n", frame, alloc_hier);
3985
3986 if (!frame || !alloc_hier)
3987 return D3DERR_INVALIDCALL;
3988
3989 while (!last) {
3990 D3DXMESHCONTAINER *container;
3991 D3DXFRAME *current_frame;
3992
3993 if (frame->pFrameSibling) {
3994 current_frame = frame->pFrameSibling;
3995 frame->pFrameSibling = current_frame->pFrameSibling;
3996 current_frame->pFrameSibling = NULL;
3997 } else {
3998 current_frame = frame;
3999 last = TRUE;
4000 }
4001
4002 if (current_frame->pFrameFirstChild) {
4003 hr = D3DXFrameDestroy(current_frame->pFrameFirstChild, alloc_hier);
4004 if (FAILED(hr)) return hr;
4005 current_frame->pFrameFirstChild = NULL;
4006 }
4007
4008 container = current_frame->pMeshContainer;
4009 while (container) {
4010 D3DXMESHCONTAINER *next_container = container->pNextMeshContainer;
4011 hr = alloc_hier->lpVtbl->DestroyMeshContainer(alloc_hier, container);
4012 if (FAILED(hr)) return hr;
4013 container = next_container;
4014 }
4015 hr = alloc_hier->lpVtbl->DestroyFrame(alloc_hier, current_frame);
4016 if (FAILED(hr)) return hr;
4017 }
4018 return D3D_OK;
4019 }
4020
4021 HRESULT WINAPI D3DXLoadMeshFromXA(const char *filename, DWORD options, struct IDirect3DDevice9 *device,
4022 struct ID3DXBuffer **adjacency, struct ID3DXBuffer **materials, struct ID3DXBuffer **effect_instances,
4023 DWORD *num_materials, struct ID3DXMesh **mesh)
4024 {
4025 WCHAR *filenameW;
4026 HRESULT hr;
4027 int len;
4028
4029 TRACE("filename %s, options %#x, device %p, adjacency %p, materials %p, "
4030 "effect_instances %p, num_materials %p, mesh %p.\n",
4031 debugstr_a(filename), options, device, adjacency, materials,
4032 effect_instances, num_materials, mesh);
4033
4034 if (!filename)
4035 return D3DERR_INVALIDCALL;
4036
4037 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
4038 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
4039 if (!filenameW) return E_OUTOFMEMORY;
4040 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
4041
4042 hr = D3DXLoadMeshFromXW(filenameW, options, device, adjacency, materials,
4043 effect_instances, num_materials, mesh);
4044 HeapFree(GetProcessHeap(), 0, filenameW);
4045
4046 return hr;
4047 }
4048
4049 HRESULT WINAPI D3DXLoadMeshFromXW(const WCHAR *filename, DWORD options, struct IDirect3DDevice9 *device,
4050 struct ID3DXBuffer **adjacency, struct ID3DXBuffer **materials, struct ID3DXBuffer **effect_instances,
4051 DWORD *num_materials, struct ID3DXMesh **mesh)
4052 {
4053 void *buffer;
4054 HRESULT hr;
4055 DWORD size;
4056
4057 TRACE("filename %s, options %#x, device %p, adjacency %p, materials %p, "
4058 "effect_instances %p, num_materials %p, mesh %p.\n",
4059 debugstr_w(filename), options, device, adjacency, materials,
4060 effect_instances, num_materials, mesh);
4061
4062 if (!filename)
4063 return D3DERR_INVALIDCALL;
4064
4065 hr = map_view_of_file(filename, &buffer, &size);
4066 if (FAILED(hr))
4067 return D3DXERR_INVALIDDATA;
4068
4069 hr = D3DXLoadMeshFromXInMemory(buffer, size, options, device, adjacency,
4070 materials, effect_instances, num_materials, mesh);
4071
4072 UnmapViewOfFile(buffer);
4073
4074 return hr;
4075 }
4076
4077 HRESULT WINAPI D3DXLoadMeshFromXResource(HMODULE module, const char *name, const char *type, DWORD options,
4078 struct IDirect3DDevice9 *device, struct ID3DXBuffer **adjacency, struct ID3DXBuffer **materials,
4079 struct ID3DXBuffer **effect_instances, DWORD *num_materials, struct ID3DXMesh **mesh)
4080 {
4081 HRESULT hr;
4082 HRSRC resinfo;
4083 void *buffer;
4084 DWORD size;
4085
4086 TRACE("module %p, name %s, type %s, options %#x, device %p, adjacency %p, "
4087 "materials %p, effect_instances %p, num_materials %p, mesh %p.\n",
4088 module, debugstr_a(name), debugstr_a(type), options, device, adjacency,
4089 materials, effect_instances, num_materials, mesh);
4090
4091 resinfo = FindResourceA(module, name, type);
4092 if (!resinfo) return D3DXERR_INVALIDDATA;
4093
4094 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
4095 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
4096
4097 return D3DXLoadMeshFromXInMemory(buffer, size, options, device, adjacency,
4098 materials, effect_instances, num_materials, mesh);
4099 }
4100
4101 struct mesh_container
4102 {
4103 struct list entry;
4104 ID3DXMesh *mesh;
4105 ID3DXBuffer *adjacency;
4106 ID3DXBuffer *materials;
4107 ID3DXBuffer *effects;
4108 DWORD num_materials;
4109 D3DXMATRIX transform;
4110 };
4111
4112 static HRESULT parse_frame(struct ID3DXFileData *filedata, DWORD options, struct IDirect3DDevice9 *device,
4113 const D3DXMATRIX *parent_transform, struct list *container_list, DWORD provide_flags)
4114 {
4115 HRESULT hr;
4116 D3DXMATRIX transform = *parent_transform;
4117 ID3DXFileData *child;
4118 GUID type;
4119 SIZE_T i, nb_children;
4120
4121 hr = filedata->lpVtbl->GetChildren(filedata, &nb_children);
4122 if (FAILED(hr))
4123 return hr;
4124
4125 for (i = 0; i < nb_children; i++)
4126 {
4127 hr = filedata->lpVtbl->GetChild(filedata, i, &child);
4128 if (FAILED(hr))
4129 return hr;
4130 hr = child->lpVtbl->GetType(child, &type);
4131 if (FAILED(hr))
4132 return hr;
4133
4134 if (IsEqualGUID(&type, &TID_D3DRMMesh)) {
4135 struct mesh_container *container = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*container));
4136 if (!container)
4137 return E_OUTOFMEMORY;
4138 list_add_tail(container_list, &container->entry);
4139 container->transform = transform;
4140
4141 hr = D3DXLoadSkinMeshFromXof(child, options, device,
4142 (provide_flags & PROVIDE_ADJACENCY) ? &container->adjacency : NULL,
4143 (provide_flags & PROVIDE_MATERIALS) ? &container->materials : NULL,
4144 NULL, &container->num_materials, NULL, &container->mesh);
4145 } else if (IsEqualGUID(&type, &TID_D3DRMFrameTransformMatrix)) {
4146 D3DXMATRIX new_transform;
4147 hr = parse_transform_matrix(child, &new_transform);
4148 D3DXMatrixMultiply(&transform, &transform, &new_transform);
4149 } else if (IsEqualGUID(&type, &TID_D3DRMFrame)) {
4150 hr = parse_frame(child, options, device, &transform, container_list, provide_flags);
4151 }
4152 if (FAILED(hr))
4153 return hr;
4154 }
4155
4156 return D3D_OK;
4157 }
4158
4159 HRESULT WINAPI D3DXLoadMeshFromXInMemory(const void *memory, DWORD memory_size, DWORD options,
4160 struct IDirect3DDevice9 *device, struct ID3DXBuffer **adjacency_out, struct ID3DXBuffer **materials_out,
4161 struct ID3DXBuffer **effects_out, DWORD *num_materials_out, struct ID3DXMesh **mesh_out)
4162 {
4163 HRESULT hr;
4164 ID3DXFile *d3dxfile = NULL;
4165 ID3DXFileEnumObject *enumobj = NULL;
4166 ID3DXFileData *filedata = NULL;
4167 D3DXF_FILELOADMEMORY source;
4168 ID3DXBuffer *materials = NULL;
4169 ID3DXBuffer *effects = NULL;
4170 ID3DXBuffer *adjacency = NULL;
4171 struct list container_list = LIST_INIT(container_list);
4172 struct mesh_container *container_ptr, *next_container_ptr;
4173 DWORD num_materials;
4174 DWORD num_faces, num_vertices;
4175 D3DXMATRIX identity;
4176 DWORD provide_flags = 0;
4177 DWORD fvf;
4178 ID3DXMesh *concat_mesh = NULL;
4179 D3DVERTEXELEMENT9 concat_decl[MAX_FVF_DECL_SIZE];
4180 BYTE *concat_vertices = NULL;
4181 void *concat_indices = NULL;
4182 DWORD index_offset;
4183 DWORD concat_vertex_size;
4184 SIZE_T i, nb_children;
4185 GUID guid;
4186
4187 TRACE("(%p, %u, %x, %p, %p, %p, %p, %p, %p)\n", memory, memory_size, options,
4188 device, adjacency_out, materials_out, effects_out, num_materials_out, mesh_out);
4189
4190 if (!memory || !memory_size || !device || !mesh_out)
4191 return D3DERR_INVALIDCALL;
4192
4193 hr = D3DXFileCreate(&d3dxfile);
4194 if (FAILED(hr)) goto cleanup;
4195
4196 hr = d3dxfile->lpVtbl->RegisterTemplates(d3dxfile, D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES);
4197 if (FAILED(hr)) goto cleanup;
4198
4199 source.lpMemory = (void*)memory;
4200 source.dSize = memory_size;
4201 hr = d3dxfile->lpVtbl->CreateEnumObject(d3dxfile, &source, D3DXF_FILELOAD_FROMMEMORY, &enumobj);
4202 if (FAILED(hr)) goto cleanup;
4203
4204 D3DXMatrixIdentity(&identity);
4205 if (adjacency_out) provide_flags |= PROVIDE_ADJACENCY;
4206 if (materials_out || effects_out) provide_flags |= PROVIDE_MATERIALS;
4207
4208 hr = enumobj->lpVtbl->GetChildren(enumobj, &nb_children);
4209 if (FAILED(hr))
4210 goto cleanup;
4211
4212 for (i = 0; i < nb_children; i++)
4213 {
4214 hr = enumobj->lpVtbl->GetChild(enumobj, i, &filedata);
4215 if (FAILED(hr))
4216 goto cleanup;
4217
4218 hr = filedata->lpVtbl->GetType(filedata, &guid);
4219 if (SUCCEEDED(hr)) {
4220 if (IsEqualGUID(&guid, &TID_D3DRMMesh)) {
4221 container_ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*container_ptr));
4222 if (!container_ptr) {
4223 hr = E_OUTOFMEMORY;
4224 goto cleanup;
4225 }
4226 list_add_tail(&container_list, &container_ptr->entry);
4227 D3DXMatrixIdentity(&container_ptr->transform);
4228
4229 hr = D3DXLoadSkinMeshFromXof(filedata, options, device,
4230 (provide_flags & PROVIDE_ADJACENCY) ? &container_ptr->adjacency : NULL,
4231 (provide_flags & PROVIDE_MATERIALS) ? &container_ptr->materials : NULL,
4232 NULL, &container_ptr->num_materials, NULL, &container_ptr->mesh);
4233 } else if (IsEqualGUID(&guid, &TID_D3DRMFrame)) {
4234 hr = parse_frame(filedata, options, device, &identity, &container_list, provide_flags);
4235 }
4236 if (FAILED(hr)) goto cleanup;
4237 }
4238 filedata->lpVtbl->Release(filedata);
4239 filedata = NULL;
4240 if (FAILED(hr))
4241 goto cleanup;
4242 }
4243
4244 enumobj->lpVtbl->Release(enumobj);
4245 enumobj = NULL;
4246 d3dxfile->lpVtbl->Release(d3dxfile);
4247 d3dxfile = NULL;
4248
4249 if (list_empty(&container_list)) {
4250 hr = E_FAIL;
4251 goto cleanup;
4252 }
4253
4254 fvf = D3DFVF_XYZ;
4255 num_faces = 0;
4256 num_vertices = 0;
4257 num_materials = 0;
4258 LIST_FOR_EACH_ENTRY(container_ptr, &container_list, struct mesh_container, entry)
4259 {
4260 ID3DXMesh *mesh = container_ptr->mesh;
4261 fvf |= mesh->lpVtbl->GetFVF(mesh);
4262 num_faces += mesh->lpVtbl->GetNumFaces(mesh);
4263 num_vertices += mesh->lpVtbl->GetNumVertices(mesh);
4264 num_materials += container_ptr->num_materials;
4265 }
4266
4267 hr = D3DXCreateMeshFVF(num_faces, num_vertices, options, fvf, device, &concat_mesh);
4268 if (FAILED(hr)) goto cleanup;
4269
4270 hr = concat_mesh->lpVtbl->GetDeclaration(concat_mesh, concat_decl);
4271 if (FAILED(hr)) goto cleanup;
4272
4273 concat_vertex_size = D3DXGetDeclVertexSize(concat_decl, 0);
4274
4275 hr = concat_mesh->lpVtbl->LockVertexBuffer(concat_mesh, 0, (void**)&concat_vertices);
4276 if (FAILED(hr)) goto cleanup;
4277
4278 LIST_FOR_EACH_ENTRY(container_ptr, &container_list, struct mesh_container, entry)
4279 {
4280 D3DVERTEXELEMENT9 mesh_decl[MAX_FVF_DECL_SIZE];
4281 ID3DXMesh *mesh = container_ptr->mesh;
4282 DWORD num_mesh_vertices = mesh->lpVtbl->GetNumVertices(mesh);
4283 DWORD mesh_vertex_size;
4284 const BYTE *mesh_vertices;
4285 DWORD i;
4286
4287 hr = mesh->lpVtbl->GetDeclaration(mesh, mesh_decl);
4288 if (FAILED(hr)) goto cleanup;
4289
4290 mesh_vertex_size = D3DXGetDeclVertexSize(mesh_decl, 0);
4291
4292 hr = mesh->lpVtbl->LockVertexBuffer(mesh, D3DLOCK_READONLY, (void**)&mesh_vertices);
4293 if (FAILED(hr)) goto cleanup;
4294
4295 for (i = 0; i < num_mesh_vertices; i++) {
4296 int j;
4297 int k = 1;
4298
4299 D3DXVec3TransformCoord((D3DXVECTOR3*)concat_vertices,
4300 (D3DXVECTOR3*)mesh_vertices,
4301 &container_ptr->transform);
4302 for (j = 1; concat_decl[j].Stream != 0xff; j++)
4303 {
4304 if (concat_decl[j].Usage == mesh_decl[k].Usage &&
4305 concat_decl[j].UsageIndex == mesh_decl[k].UsageIndex)
4306 {
4307 if (concat_decl[j].Usage == D3DDECLUSAGE_NORMAL) {
4308 D3DXVec3TransformCoord((D3DXVECTOR3*)(concat_vertices + concat_decl[j].Offset),
4309 (D3DXVECTOR3*)(mesh_vertices + mesh_decl[k].Offset),
4310 &container_ptr->transform);
4311 } else {
4312 memcpy(concat_vertices + concat_decl[j].Offset,
4313 mesh_vertices + mesh_decl[k].Offset,
4314 d3dx_decltype_size[mesh_decl[k].Type]);
4315 }
4316 k++;
4317 }
4318 }
4319 mesh_vertices += mesh_vertex_size;
4320 concat_vertices += concat_vertex_size;
4321 }
4322
4323 mesh->lpVtbl->UnlockVertexBuffer(mesh);
4324 }
4325
4326 concat_mesh->lpVtbl->UnlockVertexBuffer(concat_mesh);
4327 concat_vertices = NULL;
4328
4329 hr = concat_mesh->lpVtbl->LockIndexBuffer(concat_mesh, 0, &concat_indices);
4330 if (FAILED(hr)) goto cleanup;
4331
4332 index_offset = 0;
4333 LIST_FOR_EACH_ENTRY(container_ptr, &container_list, struct mesh_container, entry)
4334 {
4335 ID3DXMesh *mesh = container_ptr->mesh;
4336 const void *mesh_indices;
4337 DWORD num_mesh_faces = mesh->lpVtbl->GetNumFaces(mesh);
4338 DWORD i;
4339
4340 hr = mesh->lpVtbl->LockIndexBuffer(mesh, D3DLOCK_READONLY, (void**)&mesh_indices);
4341 if (FAILED(hr)) goto cleanup;
4342
4343 if (options & D3DXMESH_32BIT) {
4344 DWORD *dest = concat_indices;
4345 const DWORD *src = mesh_indices;
4346 for (i = 0; i < num_mesh_faces * 3; i++)
4347 *dest++ = index_offset + *src++;
4348 concat_indices = dest;
4349 } else {
4350 WORD *dest = concat_indices;
4351 const WORD *src = mesh_indices;
4352 for (i = 0; i < num_mesh_faces * 3; i++)
4353 *dest++ = index_offset + *src++;
4354 concat_indices = dest;
4355 }
4356 mesh->lpVtbl->UnlockIndexBuffer(mesh);
4357
4358 index_offset += num_mesh_faces * 3;
4359 }
4360
4361 concat_mesh->lpVtbl->UnlockIndexBuffer(concat_mesh);
4362 concat_indices = NULL;
4363
4364 if (num_materials) {
4365 DWORD *concat_attrib_buffer = NULL;
4366 DWORD offset = 0;
4367
4368 hr = concat_mesh->lpVtbl->LockAttributeBuffer(concat_mesh, 0, &concat_attrib_buffer);
4369 if (FAILED(hr)) goto cleanup;
4370
4371 LIST_FOR_EACH_ENTRY(container_ptr, &container_list, struct mesh_container, entry)
4372 {
4373 ID3DXMesh *mesh = container_ptr->mesh;
4374 const DWORD *mesh_attrib_buffer = NULL;
4375 DWORD count = mesh->lpVtbl->GetNumFaces(mesh);
4376
4377 hr = mesh->lpVtbl->LockAttributeBuffer(mesh, D3DLOCK_READONLY, (DWORD**)&mesh_attrib_buffer);
4378 if (FAILED(hr)) {
4379 concat_mesh->lpVtbl->UnlockAttributeBuffer(concat_mesh);
4380 goto cleanup;
4381 }
4382
4383 while (count--)
4384 *concat_attrib_buffer++ = offset + *mesh_attrib_buffer++;
4385
4386 mesh->lpVtbl->UnlockAttributeBuffer(mesh);
4387 offset += container_ptr->num_materials;
4388 }
4389 concat_mesh->lpVtbl->UnlockAttributeBuffer(concat_mesh);
4390 }
4391
4392 if (materials_out || effects_out) {
4393 D3DXMATERIAL *out_ptr;
4394 if (!num_materials) {
4395 /* create default material */
4396 hr = D3DXCreateBuffer(sizeof(D3DXMATERIAL), &materials);
4397 if (FAILED(hr)) goto cleanup;
4398
4399 out_ptr = ID3DXBuffer_GetBufferPointer(materials);
4400 out_ptr->MatD3D.Diffuse.r = 0.5f;
4401 out_ptr->MatD3D.Diffuse.g = 0.5f;
4402 out_ptr->MatD3D.Diffuse.b = 0.5f;
4403 out_ptr->MatD3D.Specular.r = 0.5f;
4404 out_ptr->MatD3D.Specular.g = 0.5f;
4405 out_ptr->MatD3D.Specular.b = 0.5f;
4406 /* D3DXCreateBuffer initializes the rest to zero */
4407 } else {
4408 DWORD buffer_size = num_materials * sizeof(D3DXMATERIAL);
4409 char *strings_out_ptr;
4410
4411 LIST_FOR_EACH_ENTRY(container_ptr, &container_list, struct mesh_container, entry)
4412 {
4413 if (container_ptr->materials) {
4414 DWORD i;
4415 const D3DXMATERIAL *in_ptr = ID3DXBuffer_GetBufferPointer(container_ptr->materials);
4416 for (i = 0; i < container_ptr->num_materials; i++)
4417 {
4418 if (in_ptr->pTextureFilename)
4419 buffer_size += strlen(in_ptr->pTextureFilename) + 1;
4420 in_ptr++;
4421 }
4422 }
4423 }
4424
4425 hr = D3DXCreateBuffer(buffer_size, &materials);
4426 if (FAILED(hr)) goto cleanup;
4427 out_ptr = ID3DXBuffer_GetBufferPointer(materials);
4428 strings_out_ptr = (char*)(out_ptr + num_materials);
4429
4430 LIST_FOR_EACH_ENTRY(container_ptr, &container_list, struct mesh_container, entry)
4431 {
4432 if (container_ptr->materials) {
4433 DWORD i;
4434 const D3DXMATERIAL *in_ptr = ID3DXBuffer_GetBufferPointer(container_ptr->materials);
4435 for (i = 0; i < container_ptr->num_materials; i++)
4436 {
4437 out_ptr->MatD3D = in_ptr->MatD3D;
4438 if (in_ptr->pTextureFilename) {
4439 out_ptr->pTextureFilename = strings_out_ptr;
4440 strcpy(out_ptr->pTextureFilename, in_ptr->pTextureFilename);
4441 strings_out_ptr += strlen(in_ptr->pTextureFilename) + 1;
4442 }
4443 in_ptr++;
4444 out_ptr++;
4445 }
4446 }
4447 }
4448 }
4449 }
4450 if (!num_materials)
4451 num_materials = 1;
4452
4453 if (effects_out) {
4454 generate_effects(materials, num_materials, &effects);
4455 if (!materials_out) {
4456 ID3DXBuffer_Release(materials);
4457 materials = NULL;
4458 }
4459 }
4460
4461 if (adjacency_out) {
4462 if (!list_next(&container_list, list_head(&container_list))) {
4463 container_ptr = LIST_ENTRY(list_head(&container_list), struct mesh_container, entry);
4464 adjacency = container_ptr->adjacency;
4465 container_ptr->adjacency = NULL;
4466 } else {
4467 DWORD offset = 0;
4468 DWORD *out_ptr;
4469
4470 hr = D3DXCreateBuffer(num_faces * 3 * sizeof(DWORD), &adjacency);
4471 if (FAILED(hr)) goto cleanup;
4472
4473 out_ptr = ID3DXBuffer_GetBufferPointer(adjacency);
4474 LIST_FOR_EACH_ENTRY(container_ptr, &container_list, struct mesh_container, entry)
4475 {
4476 DWORD i;
4477 DWORD count = 3 * container_ptr->mesh->lpVtbl->GetNumFaces(container_ptr->mesh);
4478 DWORD *in_ptr = ID3DXBuffer_GetBufferPointer(container_ptr->adjacency);
4479
4480 for (i = 0; i < count; i++)
4481 *out_ptr++ = offset + *in_ptr++;
4482
4483 offset += count;
4484 }
4485 }
4486 }
4487
4488 *mesh_out = concat_mesh;
4489 if (adjacency_out) *adjacency_out = adjacency;
4490 if (materials_out) *materials_out = materials;
4491 if (effects_out) *effects_out = effects;
4492 if (num_materials_out) *num_materials_out = num_materials;
4493
4494 hr = D3D_OK;
4495 cleanup:
4496 if (concat_indices) concat_mesh->lpVtbl->UnlockIndexBuffer(concat_mesh);
4497 if (concat_vertices) concat_mesh->lpVtbl->UnlockVertexBuffer(concat_mesh);
4498 if (filedata) filedata->lpVtbl->Release(filedata);
4499 if (enumobj) enumobj->lpVtbl->Release(enumobj);
4500 if (d3dxfile) d3dxfile->lpVtbl->Release(d3dxfile);
4501 if (FAILED(hr)) {
4502 if (concat_mesh) IUnknown_Release(concat_mesh);
4503 if (materials) ID3DXBuffer_Release(materials);
4504 if (effects) ID3DXBuffer_Release(effects);
4505 if (adjacency) ID3DXBuffer_Release(adjacency);
4506 }
4507 LIST_FOR_EACH_ENTRY_SAFE(container_ptr, next_container_ptr, &container_list, struct mesh_container, entry)
4508 {
4509 if (container_ptr->mesh) IUnknown_Release(container_ptr->mesh);
4510 if (container_ptr->adjacency) ID3DXBuffer_Release(container_ptr->adjacency);
4511 if (container_ptr->materials) ID3DXBuffer_Release(container_ptr->materials);
4512 if (container_ptr->effects) ID3DXBuffer_Release(container_ptr->effects);
4513 HeapFree(GetProcessHeap(), 0, container_ptr);
4514 }
4515 return hr;
4516 }
4517
4518 struct vertex
4519 {
4520 D3DXVECTOR3 position;
4521 D3DXVECTOR3 normal;
4522 };
4523
4524 HRESULT WINAPI D3DXCreateBox(struct IDirect3DDevice9 *device, float width, float height,
4525 float depth, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
4526 {
4527 HRESULT hr;
4528 ID3DXMesh *box;
4529 struct vertex *vertices;
4530 WORD (*faces)[3];
4531 DWORD *adjacency_buf;
4532 unsigned int i, face;
4533 static const D3DXVECTOR3 unit_box[] =
4534 {
4535 {-0.5f, -0.5f, -0.5f}, {-0.5f, -0.5f, 0.5f}, {-0.5f, 0.5f, 0.5f}, {-0.5f, 0.5f, -0.5f},
4536 {-0.5f, 0.5f, -0.5f}, {-0.5f, 0.5f, 0.5f}, { 0.5f, 0.5f, 0.5f}, { 0.5f, 0.5f, -0.5f},
4537 { 0.5f, 0.5f, -0.5f}, { 0.5f, 0.5f, 0.5f}, { 0.5f, -0.5f, 0.5f}, { 0.5f, -0.5f, -0.5f},
4538 {-0.5f, -0.5f, 0.5f}, {-0.5f, -0.5f, -0.5f}, { 0.5f, -0.5f, -0.5f}, { 0.5f, -0.5f, 0.5f},
4539 {-0.5f, -0.5f, 0.5f}, { 0.5f, -0.5f, 0.5f}, { 0.5f, 0.5f, 0.5f}, {-0.5f, 0.5f, 0.5f},
4540 {-0.5f, -0.5f, -0.5f}, {-0.5f, 0.5f, -0.5f}, { 0.5f, 0.5f, -0.5f}, { 0.5f, -0.5f, -0.5f}
4541 };
4542 static const D3DXVECTOR3 normals[] =
4543 {
4544 {-1.0f, 0.0f, 0.0f}, { 0.0f, 1.0f, 0.0f}, { 1.0f, 0.0f, 0.0f},
4545 { 0.0f, -1.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, { 0.0f, 0.0f, -1.0f}
4546 };
4547 static const DWORD adjacency_table[] =
4548 {
4549 6, 9, 1, 2, 10, 0, 1, 9, 3, 4, 10, 2,
4550 3, 8, 5, 7, 11, 4, 0, 11, 7, 5, 8, 6,
4551 7, 4, 9, 2, 0, 8, 1, 3, 11, 5, 6, 10
4552 };
4553
4554 TRACE("device %p, width %f, height %f, depth %f, mesh %p, adjacency %p\n",
4555 device, width, height, depth, mesh, adjacency);
4556
4557 if (!device || width < 0.0f || height < 0.0f || depth < 0.0f || !mesh)
4558 {
4559 return D3DERR_INVALIDCALL;
4560 }
4561
4562 if (FAILED(hr = D3DXCreateMeshFVF(12, 24, D3DXMESH_MANAGED, D3DFVF_XYZ | D3DFVF_NORMAL, device, &box)))
4563 {
4564 return hr;
4565 }
4566
4567 if (FAILED(hr = box->lpVtbl->LockVertexBuffer(box, 0, (void **)&vertices)))
4568 {
4569 box->lpVtbl->Release(box);
4570 return hr;
4571 }
4572
4573 if (FAILED(hr = box->lpVtbl->LockIndexBuffer(box, 0, (void **)&faces)))
4574 {
4575 box->lpVtbl->UnlockVertexBuffer(box);
4576 box->lpVtbl->Release(box);
4577 return hr;
4578 }
4579
4580 for (i = 0; i < 24; i++)
4581 {
4582 vertices[i].position.x = width * unit_box[i].x;
4583 vertices[i].position.y = height * unit_box[i].y;
4584 vertices[i].position.z = depth * unit_box[i].z;
4585 vertices[i].normal.x = normals[i / 4].x;
4586 vertices[i].normal.y = normals[i / 4].y;
4587 vertices[i].normal.z = normals[i / 4].z;
4588 }
4589
4590 face = 0;
4591 for (i = 0; i < 12; i++)
4592 {
4593 faces[i][0] = face++;
4594 faces[i][1] = face++;
4595 faces[i][2] = (i % 2) ? face - 4 : face;
4596 }
4597
4598 box->lpVtbl->UnlockIndexBuffer(box);
4599 box->lpVtbl->UnlockVertexBuffer(box);
4600
4601 if (adjacency)
4602 {
4603 if (FAILED(hr = D3DXCreateBuffer(sizeof(adjacency_table), adjacency)))
4604 {
4605 box->lpVtbl->Release(box);
4606 return hr;
4607 }
4608
4609 adjacency_buf = ID3DXBuffer_GetBufferPointer(*adjacency);
4610 memcpy(adjacency_buf, adjacency_table, sizeof(adjacency_table));
4611 }
4612
4613 *mesh = box;
4614
4615 return D3D_OK;
4616 }
4617
4618 typedef WORD face[3];
4619
4620 struct sincos_table
4621 {
4622 float *sin;
4623 float *cos;
4624 };
4625
4626 static void free_sincos_table(struct sincos_table *sincos_table)
4627 {
4628 HeapFree(GetProcessHeap(), 0, sincos_table->cos);
4629 HeapFree(GetProcessHeap(), 0, sincos_table->sin);
4630 }
4631
4632 /* pre compute sine and cosine tables; caller must free */
4633 static BOOL compute_sincos_table(struct sincos_table *sincos_table, float angle_start, float angle_step, int n)
4634 {
4635 float angle;
4636 int i;
4637
4638 sincos_table->sin = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*sincos_table->sin));
4639 if (!sincos_table->sin)
4640 {
4641 return FALSE;
4642 }
4643 sincos_table->cos = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*sincos_table->cos));
4644 if (!sincos_table->cos)
4645 {
4646 HeapFree(GetProcessHeap(), 0, sincos_table->sin);
4647 return FALSE;
4648 }
4649
4650 angle = angle_start;
4651 for (i = 0; i < n; i++)
4652 {
4653 sincos_table->sin[i] = sinf(angle);
4654 sincos_table->cos[i] = cosf(angle);
4655 angle += angle_step;
4656 }
4657
4658 return TRUE;
4659 }
4660
4661 static WORD vertex_index(UINT slices, int slice, int stack)
4662 {
4663 return stack*slices+slice+1;
4664 }
4665
4666 HRESULT WINAPI D3DXCreateSphere(struct IDirect3DDevice9 *device, float radius, UINT slices,
4667 UINT stacks, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
4668 {
4669 DWORD number_of_vertices, number_of_faces;
4670 HRESULT hr;
4671 ID3DXMesh *sphere;
4672 struct vertex *vertices;
4673 face *faces;
4674 float phi_step, phi_start;
4675 struct sincos_table phi;
4676 float theta_step, theta, sin_theta, cos_theta;
4677 DWORD vertex, face, stack, slice;
4678
4679 TRACE("(%p, %f, %u, %u, %p, %p)\n", device, radius, slices, stacks, mesh, adjacency);
4680
4681 if (!device || radius < 0.0f || slices < 2 || stacks < 2 || !mesh)
4682 {
4683 return D3DERR_INVALIDCALL;
4684 }
4685
4686 if (adjacency)
4687 {
4688 FIXME("Case of adjacency != NULL not implemented.\n");
4689 return E_NOTIMPL;
4690 }
4691
4692 number_of_vertices = 2 + slices * (stacks-1);
4693 number_of_faces = 2 * slices + (stacks - 2) * (2 * slices);
4694
4695 hr = D3DXCreateMeshFVF(number_of_faces, number_of_vertices, D3DXMESH_MANAGED,
4696 D3DFVF_XYZ | D3DFVF_NORMAL, device, &sphere);
4697 if (FAILED(hr))
4698 {
4699 return hr;
4700 }
4701
4702 if (FAILED(hr = sphere->lpVtbl->LockVertexBuffer(sphere, 0, (void **)&vertices)))
4703 {
4704 sphere->lpVtbl->Release(sphere);
4705 return hr;
4706 }
4707
4708 if (FAILED(hr = sphere->lpVtbl->LockIndexBuffer(sphere, 0, (void **)&faces)))
4709 {
4710 sphere->lpVtbl->UnlockVertexBuffer(sphere);
4711 sphere->lpVtbl->Release(sphere);
4712 return hr;
4713 }
4714
4715 /* phi = angle on xz plane wrt z axis */
4716 phi_step = -2.0f * D3DX_PI / slices;
4717 phi_start = D3DX_PI / 2.0f;
4718
4719 if (!compute_sincos_table(&phi, phi_start, phi_step, slices))
4720 {
4721 sphere->lpVtbl->UnlockIndexBuffer(sphere);
4722 sphere->lpVtbl->UnlockVertexBuffer(sphere);
4723 sphere->lpVtbl->Release(sphere);
4724 return E_OUTOFMEMORY;
4725 }
4726
4727 /* theta = angle on xy plane wrt x axis */
4728 theta_step = D3DX_PI / stacks;
4729 theta = theta_step;
4730
4731 vertex = 0;
4732 face = 0;
4733
4734 vertices[vertex].normal.x = 0.0f;
4735 vertices[vertex].normal.y = 0.0f;
4736 vertices[vertex].normal.z = 1.0f;
4737 vertices[vertex].position.x = 0.0f;
4738 vertices[vertex].position.y = 0.0f;
4739 vertices[vertex].position.z = radius;
4740 vertex++;
4741
4742 for (stack = 0; stack < stacks - 1; stack++)
4743 {
4744 sin_theta = sinf(theta);
4745 cos_theta = cosf(theta);
4746
4747 for (slice = 0; slice < slices; slice++)
4748 {
4749 vertices[vertex].normal.x = sin_theta * phi.cos[slice];
4750 vertices[vertex].normal.y = sin_theta * phi.sin[slice];
4751 vertices[vertex].normal.z = cos_theta;
4752 vertices[vertex].position.x = radius * sin_theta * phi.cos[slice];
4753 vertices[vertex].position.y = radius * sin_theta * phi.sin[slice];
4754 vertices[vertex].position.z = radius * cos_theta;
4755 vertex++;
4756
4757 if (slice > 0)
4758 {
4759 if (stack == 0)
4760 {
4761 /* top stack is triangle fan */
4762 faces[face][0] = 0;
4763 faces[face][1] = slice + 1;
4764 faces[face][2] = slice;
4765 face++;
4766 }
4767 else
4768 {
4769 /* stacks in between top and bottom are quad strips */
4770 faces[face][0] = vertex_index(slices, slice-1, stack-1);
4771 faces[face][1] = vertex_index(slices, slice, stack-1);
4772 faces[face][2] = vertex_index(slices, slice-1, stack);
4773 face++;
4774
4775 faces[face][0] = vertex_index(slices, slice, stack-1);
4776 faces[face][1] = vertex_index(slices, slice, stack);
4777 faces[face][2] = vertex_index(slices, slice-1, stack);
4778 face++;
4779 }
4780 }
4781 }
4782
4783 theta += theta_step;
4784
4785 if (stack == 0)
4786 {
4787 faces[face][0] = 0;
4788 faces[face][1] = 1;
4789 faces[face][2] = slice;
4790 face++;
4791 }
4792 else
4793 {
4794 faces[face][0] = vertex_index(slices, slice-1, stack-1);
4795 faces[face][1] = vertex_index(slices, 0, stack-1);
4796 faces[face][2] = vertex_index(slices, slice-1, stack);
4797 face++;
4798
4799 faces[face][0] = vertex_index(slices, 0, stack-1);
4800 faces[face][1] = vertex_index(slices, 0, stack);
4801 faces[face][2] = vertex_index(slices, slice-1, stack);
4802 face++;
4803 }
4804 }
4805
4806 vertices[vertex].position.x = 0.0f;
4807 vertices[vertex].position.y = 0.0f;
4808 vertices[vertex].position.z = -radius;
4809 vertices[vertex].normal.x = 0.0f;
4810 vertices[vertex].normal.y = 0.0f;
4811 vertices[vertex].normal.z = -1.0f;
4812
4813 /* bottom stack is triangle fan */
4814 for (slice = 1; slice < slices; slice++)
4815 {
4816 faces[face][0] = vertex_index(slices, slice-1, stack-1);
4817 faces[face][1] = vertex_index(slices, slice, stack-1);
4818 faces[face][2] = vertex;
4819 face++;
4820 }
4821
4822 faces[face][0] = vertex_index(slices, slice-1, stack-1);
4823 faces[face][1] = vertex_index(slices, 0, stack-1);
4824 faces[face][2] = vertex;
4825
4826 free_sincos_table(&phi);
4827 sphere->lpVtbl->UnlockIndexBuffer(sphere);
4828 sphere->lpVtbl->UnlockVertexBuffer(sphere);
4829 *mesh = sphere;
4830
4831 return D3D_OK;
4832 }
4833
4834 HRESULT WINAPI D3DXCreateCylinder(struct IDirect3DDevice9 *device, float radius1, float radius2,
4835 float length, UINT slices, UINT stacks, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
4836 {
4837 DWORD number_of_vertices, number_of_faces;
4838 HRESULT hr;
4839 ID3DXMesh *cylinder;
4840 struct vertex *vertices;
4841 face *faces;
4842 float theta_step, theta_start;
4843 struct sincos_table theta;
4844 float delta_radius, radius, radius_step;
4845 float z, z_step, z_normal;
4846 DWORD vertex, face, slice, stack;
4847
4848 TRACE("(%p, %f, %f, %f, %u, %u, %p, %p)\n", device, radius1, radius2, length, slices, stacks, mesh, adjacency);
4849
4850 if (device == NULL || radius1 < 0.0f || radius2 < 0.0f || length < 0.0f || slices < 2 || stacks < 1 || mesh == NULL)
4851 {
4852 return D3DERR_INVALIDCALL;
4853 }
4854
4855 if (adjacency)
4856 {
4857 FIXME("Case of adjacency != NULL not implemented.\n");
4858 return E_NOTIMPL;
4859 }
4860
4861 number_of_vertices = 2 + (slices * (3 + stacks));
4862 number_of_faces = 2 * slices + stacks * (2 * slices);
4863
4864 hr = D3DXCreateMeshFVF(number_of_faces, number_of_vertices, D3DXMESH_MANAGED,
4865 D3DFVF_XYZ | D3DFVF_NORMAL, device, &cylinder);
4866 if (FAILED(hr))
4867 {
4868 return hr;
4869 }
4870
4871 if (FAILED(hr = cylinder->lpVtbl->LockVertexBuffer(cylinder, 0, (void **)&vertices)))
4872 {
4873 cylinder->lpVtbl->Release(cylinder);
4874 return hr;
4875 }
4876
4877 if (FAILED(hr = cylinder->lpVtbl->LockIndexBuffer(cylinder, 0, (void **)&faces)))
4878 {
4879 cylinder->lpVtbl->UnlockVertexBuffer(cylinder);
4880 cylinder->lpVtbl->Release(cylinder);
4881 return hr;
4882 }
4883
4884 /* theta = angle on xy plane wrt x axis */
4885 theta_step = -2.0f * D3DX_PI / slices;
4886 theta_start = D3DX_PI / 2.0f;
4887
4888 if (!compute_sincos_table(&theta, theta_start, theta_step, slices))
4889 {
4890 cylinder->lpVtbl->UnlockIndexBuffer(cylinder);
4891 cylinder->lpVtbl->UnlockVertexBuffer(cylinder);
4892 cylinder->lpVtbl->Release(cylinder);
4893 return E_OUTOFMEMORY;
4894 }
4895
4896 vertex = 0;
4897 face = 0;
4898
4899 delta_radius = radius1 - radius2;
4900 radius = radius1;
4901 radius_step = delta_radius / stacks;
4902
4903 z = -length / 2;
4904 z_step = length / stacks;
4905 z_normal = delta_radius / length;
4906 if (isnan(z_normal))
4907 {
4908 z_normal = 0.0f;
4909 }
4910
4911 vertices[vertex].normal.x = 0.0f;
4912 vertices[vertex].normal.y = 0.0f;
4913 vertices[vertex].normal.z = -1.0f;
4914 vertices[vertex].position.x = 0.0f;
4915 vertices[vertex].position.y = 0.0f;
4916 vertices[vertex++].position.z = z;
4917
4918 for (slice = 0; slice < slices; slice++, vertex++)
4919 {
4920 vertices[vertex].normal.x = 0.0f;
4921 vertices[vertex].normal.y = 0.0f;
4922 vertices[vertex].normal.z = -1.0f;
4923 vertices[vertex].position.x = radius * theta.cos[slice];
4924 vertices[vertex].position.y = radius * theta.sin[slice];
4925 vertices[vertex].position.z = z;
4926
4927 if (slice > 0)
4928 {
4929 faces[face][0] = 0;
4930 faces[face][1] = slice;
4931 faces[face++][2] = slice + 1;
4932 }
4933 }
4934
4935 faces[face][0] = 0;
4936 faces[face][1] = slice;
4937 faces[face++][2] = 1;
4938
4939 for (stack = 1; stack <= stacks+1; stack++)
4940 {
4941 for (slice = 0; slice < slices; slice++, vertex++)
4942 {
4943 vertices[vertex].normal.x = theta.cos[slice];
4944 vertices[vertex].normal.y = theta.sin[slice];
4945 vertices[vertex].normal.z = z_normal;
4946 D3DXVec3Normalize(&vertices[vertex].normal, &vertices[vertex].normal);
4947 vertices[vertex].position.x = radius * theta.cos[slice];
4948 vertices[vertex].position.y = radius * theta.sin[slice];
4949 vertices[vertex].position.z = z;
4950
4951 if (stack > 1 && slice > 0)
4952 {
4953 faces[face][0] = vertex_index(slices, slice-1, stack-1);
4954 faces[face][1] = vertex_index(slices, slice-1, stack);
4955 faces[face++][2] = vertex_index(slices, slice, stack-1);
4956
4957 faces[face][0] = vertex_index(slices, slice, stack-1);
4958 faces[face][1] = vertex_index(slices, slice-1, stack);
4959 faces[face++][2] = vertex_index(slices, slice, stack);
4960 }
4961 }
4962
4963 if (stack > 1)
4964 {
4965 faces[face][0] = vertex_index(slices, slice-1, stack-1);
4966 faces[face][1] = vertex_index(slices, slice-1, stack);
4967 faces[face++][2] = vertex_index(slices, 0, stack-1);
4968
4969 faces[face][0] = vertex_index(slices, 0, stack-1);
4970 faces[face][1] = vertex_index(slices, slice-1, stack);
4971 faces[face++][2] = vertex_index(slices, 0, stack);
4972 }
4973
4974 if (stack < stacks + 1)
4975 {
4976 z += z_step;
4977 radius -= radius_step;
4978 }
4979 }
4980
4981 for (slice = 0; slice < slices; slice++, vertex++)
4982 {
4983 vertices[vertex].normal.x = 0.0f;
4984 vertices[vertex].normal.y = 0.0f;
4985 vertices[vertex].normal.z = 1.0f;
4986 vertices[vertex].position.x = radius * theta.cos[slice];
4987 vertices[vertex].position.y = radius * theta.sin[slice];
4988 vertices[vertex].position.z = z;
4989
4990 if (slice > 0)
4991 {
4992 faces[face][0] = vertex_index(slices, slice-1, stack);
4993 faces[face][1] = number_of_vertices - 1;
4994 faces[face++][2] = vertex_index(slices, slice, stack);
4995 }
4996 }
4997
4998 vertices[vertex].position.x = 0.0f;
4999 vertices[vertex].position.y = 0.0f;
5000 vertices[vertex].position.z = z;
5001 vertices[vertex].normal.x = 0.0f;
5002 vertices[vertex].normal.y = 0.0f;
5003 vertices[vertex].normal.z = 1.0f;
5004
5005 faces[face][0] = vertex_index(slices, slice-1, stack);
5006 faces[face][1] = number_of_vertices - 1;
5007 faces[face][2] = vertex_index(slices, 0, stack);
5008
5009 free_sincos_table(&theta);
5010 cylinder->lpVtbl->UnlockIndexBuffer(cylinder);
5011 cylinder->lpVtbl->UnlockVertexBuffer(cylinder);
5012 *mesh = cylinder;
5013
5014 return D3D_OK;
5015 }
5016
5017 HRESULT WINAPI D3DXCreateTeapot(struct IDirect3DDevice9 *device,
5018 struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency)
5019 {
5020 FIXME("(%p, %p, %p): stub\n", device, mesh, adjacency);
5021
5022 return E_NOTIMPL;
5023 }
5024
5025 HRESULT WINAPI D3DXCreateTextA(struct IDirect3DDevice9 *device, HDC hdc, const char *text, float deviation,
5026 float extrusion, struct ID3DXMesh **mesh, struct ID3DXBuffer **adjacency, GLYPHMETRICSFLOAT *glyphmetrics)
5027 {
5028 WCHAR *textW;
5029 HRESULT hr;
5030 int len;
5031
5032 TRACE("device %p, hdc %p, text %s, deviation %.8e, extrusion %.8e, mesh %p, adjacency %p, glyphmetrics %p.\n",
5033 device, hdc, debugstr_a(text), deviation, extrusion, mesh, adjacency, glyphmetrics);
5034
5035 if (!text)
5036 return D3DERR_INVALIDCALL;
5037
5038 len = MultiByteToWideChar(CP_ACP, 0, text, -1, NULL, 0);
5039 textW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
5040 MultiByteToWideChar(CP_ACP, 0, text, -1, textW, len);
5041
5042 hr = D3DXCreateTextW(device, hdc, textW, deviation, extrusion,
5043 mesh, adjacency, glyphmetrics);
5044 HeapFree(GetProcessHeap(), 0, textW);
5045
5046 return hr;
5047 }
5048
5049 enum pointtype {
5050 POINTTYPE_CURVE = 0,
5051 POINTTYPE_CORNER,
5052 POINTTYPE_CURVE_START,
5053 POINTTYPE_CURVE_END,
5054 POINTTYPE_CURVE_MIDDLE,
5055 };
5056
5057 struct point2d
5058 {
5059 D3DXVECTOR2 pos;
5060 enum pointtype corner;
5061 };
5062
5063 struct dynamic_array
5064 {
5065 int count, capacity;
5066 void *items;
5067 };
5068
5069 /* is a dynamic_array */
5070 struct outline
5071 {
5072 int count, capacity;
5073 struct point2d *items;
5074 };
5075
5076 /* is a dynamic_array */
5077 struct outline_array
5078 {
5079 int count, capacity;
5080 struct outline *items;
5081 };
5082
5083 struct face_array
5084 {
5085 int count;
5086 face *items;
5087 };
5088
5089 struct point2d_index
5090 {
5091 struct outline *outline;
5092 int vertex;
5093 };
5094
5095 struct point2d_index_array
5096 {
5097 int count;
5098 struct point2d_index *items;
5099 };
5100
5101 struct glyphinfo
5102 {
5103 struct outline_array outlines;
5104 struct face_array faces;
5105 struct point2d_index_array ordered_vertices;
5106 float offset_x;
5107 };
5108
5109 /* is an dynamic_array */
5110 struct word_array
5111 {
5112 int count, capacity;
5113 WORD *items;
5114 };
5115
5116 /* complex polygons are split into monotone polygons, which have
5117 * at most 2 intersections with the vertical sweep line */
5118 struct triangulation
5119 {
5120 struct word_array vertex_stack;
5121 BOOL last_on_top, merging;
5122 };
5123
5124 /* is an dynamic_array */
5125 struct triangulation_array
5126 {
5127 int count, capacity;
5128 struct triangulation *items;
5129
5130 struct glyphinfo *glyph;
5131 };
5132
5133 static BOOL reserve(struct dynamic_array *array, int count, int itemsize)
5134 {
5135 if (count > array->capacity) {
5136 void *new_buffer;
5137 int new_capacity;
5138 if (array->items && array->capacity) {
5139 new_capacity = max(array->capacity * 2, count);
5140 new_buffer = HeapReAlloc(GetProcessHeap(), 0, array->items, new_capacity * itemsize);
5141 } else {
5142 new_capacity = max(16, count);
5143 new_buffer = HeapAlloc(GetProcessHeap(), 0, new_capacity * itemsize);
5144 }
5145 if (!new_buffer)
5146 return FALSE;
5147 array->items = new_buffer;
5148 array->capacity = new_capacity;
5149 }
5150 return TRUE;
5151 }
5152
5153 static struct point2d *add_points(struct outline *array, int num)
5154 {
5155 struct point2d *item;
5156
5157 if (!reserve((struct dynamic_array *)array, array->count + num, sizeof(array->items[0])))
5158 return NULL;
5159
5160 item = &array->items[array->count];
5161 array->count += num;
5162 return item;
5163 }
5164
5165 static struct outline *add_outline(struct outline_array *array)
5166 {
5167 struct outline *item;
5168
5169 if (!reserve((struct dynamic_array *)array, array->count + 1, sizeof(array->items[0])))
5170 return NULL;
5171
5172 item = &array->items[array->count++];
5173 ZeroMemory(item, sizeof(*item));
5174 return item;
5175 }
5176
5177 static inline face *add_face(struct face_array *array)
5178 {
5179 return &array->items[array->count++];
5180 }
5181
5182 static struct triangulation *add_triangulation(struct triangulation_array *array)
5183 {
5184 struct triangulation *item;
5185
5186 if (!reserve((struct dynamic_array *)array, array->count + 1, sizeof(array->items[0])))
5187 return NULL;
5188
5189 item = &array->items[array->count++];
5190 ZeroMemory(item, sizeof(*item));
5191 return item;
5192 }
5193
5194 static HRESULT add_vertex_index(struct word_array *array, WORD vertex_index)
5195 {
5196 if (!reserve((struct dynamic_array *)array, array->count + 1, sizeof(array->items[0])))
5197 return E_OUTOFMEMORY;
5198
5199 array->items[array->count++] = vertex_index;
5200 return S_OK;
5201 }
5202
5203 /* assume fixed point numbers can be converted to float point in place */
5204 C_ASSERT(sizeof(FIXED) == sizeof(float));
5205 C_ASSERT(sizeof(POINTFX) == sizeof(D3DXVECTOR2));
5206
5207 static inline D3DXVECTOR2 *convert_fixed_to_float(POINTFX *pt, int count, unsigned int emsquare)
5208 {
5209 D3DXVECTOR2 *ret = (D3DXVECTOR2*)pt;
5210 while (count--) {
5211 D3DXVECTOR2 *pt_flt = (D3DXVECTOR2*)pt;
5212 pt_flt->x = (pt->x.value + pt->x.fract / (float)0x10000) / emsquare;
5213 pt_flt->y = (pt->y.value + pt->y.fract / (float)0x10000) / emsquare;
5214 pt++;
5215 }
5216 return ret;
5217 }
5218
5219 static HRESULT add_bezier_points(struct outline *outline, const D3DXVECTOR2 *p1,
5220 const D3DXVECTOR2 *p2, const D3DXVECTOR2 *p3,
5221 float max_deviation_sq)
5222 {
5223 D3DXVECTOR2 split1 = {0, 0}, split2 = {0, 0}, middle, vec;
5224 float deviation_sq;
5225
5226 D3DXVec2Scale(&split1, D3DXVec2Add(&split1, p1, p2), 0.5f);
5227 D3DXVec2Scale(&split2, D3DXVec2Add(&split2, p2, p3), 0.5f);
5228 D3DXVec2Scale(&middle, D3DXVec2Add(&middle, &split1, &split2), 0.5f);
5229
5230 deviation_sq = D3DXVec2LengthSq(D3DXVec2Subtract(&vec, &middle, p2));
5231 if (deviation_sq < max_deviation_sq) {
5232 struct point2d *pt = add_points(outline, 1);
5233 if (!pt) return E_OUTOFMEMORY;
5234 pt->pos = *p2;
5235 pt->corner = POINTTYPE_CURVE;
5236 /* the end point is omitted because the end line merges into the next segment of
5237 * the split bezier curve, and the end of the split bezier curve is added outside
5238 * this recursive function. */
5239 } else {
5240 HRESULT hr = add_bezier_points(outline, p1, &split1, &middle, max_deviation_sq);
5241 if (hr != S_OK) return hr;
5242 hr = add_bezier_points(outline, &middle, &split2, p3, max_deviation_sq);
5243 if (hr != S_OK) return hr;
5244 }
5245
5246 return S_OK;
5247 }
5248
5249 static inline BOOL is_direction_similar(D3DXVECTOR2 *dir1, D3DXVECTOR2 *dir2, float cos_theta)
5250 {
5251 /* dot product = cos(theta) */
5252 return D3DXVec2Dot(dir1, dir2) > cos_theta;
5253 }
5254
5255 static inline D3DXVECTOR2 *unit_vec2(D3DXVECTOR2 *dir, const D3DXVECTOR2 *pt1, const D3DXVECTOR2 *pt2)
5256 {
5257 return D3DXVec2Normalize(D3DXVec2Subtract(dir, pt2, pt1), dir);
5258 }
5259
5260 struct cos_table
5261 {
5262 float cos_half;
5263 float cos_45;
5264 float cos_90;
5265 };
5266
5267 static BOOL attempt_line_merge(struct outline *outline,
5268 int pt_index,
5269 const D3DXVECTOR2 *nextpt,
5270 BOOL to_curve,
5271 const struct cos_table *table)
5272 {
5273 D3DXVECTOR2 curdir, lastdir;
5274 struct point2d *prevpt, *pt;
5275 BOOL ret = FALSE;
5276
5277 pt = &outline->items[pt_index];
5278 pt_index = (pt_index - 1 + outline->count) % outline->count;
5279 prevpt = &outline->items[pt_index];
5280
5281 if (to_curve)
5282 pt->corner = pt->corner != POINTTYPE_CORNER ? POINTTYPE_CURVE_MIDDLE : POINTTYPE_CURVE_START;
5283
5284 if (outline->count < 2)
5285 return FALSE;
5286
5287 /* remove last point if the next line continues the last line */
5288 unit_vec2(&lastdir, &prevpt->pos, &pt->pos);
5289 unit_vec2(&curdir, &pt->pos, nextpt);
5290 if (is_direction_similar(&lastdir, &curdir, table->cos_half))
5291 {
5292 outline->count--;
5293 if (pt->corner == POINTTYPE_CURVE_END)
5294 prevpt->corner = pt->corner;
5295 if (prevpt->corner == POINTTYPE_CURVE_END && to_curve)
5296 prevpt->corner = POINTTYPE_CURVE_MIDDLE;
5297 pt = prevpt;
5298
5299 ret = TRUE;
5300 if (outline->count < 2)
5301 return ret;
5302
5303 pt_index = (pt_index - 1 + outline->count) % outline->count;
5304 prevpt = &outline->items[pt_index];
5305 unit_vec2(&lastdir, &prevpt->pos, &pt->pos);
5306 unit_vec2(&curdir, &pt->pos, nextpt);
5307 }
5308 return ret;
5309 }
5310
5311 static HRESULT create_outline(struct glyphinfo *glyph, void *raw_outline, int datasize,
5312 float max_deviation_sq, unsigned int emsquare,
5313 const struct cos_table *cos_table)
5314 {
5315 TTPOLYGONHEADER *header = (TTPOLYGONHEADER *)raw_outline;
5316
5317 while ((char *)header < (char *)raw_outline + datasize)
5318 {
5319 TTPOLYCURVE *curve = (TTPOLYCURVE *)(header + 1);
5320 struct point2d *lastpt, *pt;
5321 D3DXVECTOR2 lastdir;
5322 D3DXVECTOR2 *pt_flt;
5323 int j;
5324 struct outline *outline = add_outline(&glyph->outlines);
5325
5326 if (!outline)
5327 return E_OUTOFMEMORY;
5328
5329 pt = add_points(outline, 1);
5330 if (!pt)
5331 return E_OUTOFMEMORY;
5332 pt_flt = convert_fixed_to_float(&header->pfxStart, 1, emsquare);
5333 pt->pos = *pt_flt;
5334 pt->corner = POINTTYPE_CORNER;
5335
5336 if (header->dwType != TT_POLYGON_TYPE)
5337 FIXME("Unknown header type %d\n", header->dwType);
5338
5339 while ((char *)curve < (char *)header + header->cb)
5340 {
5341 D3DXVECTOR2 bezier_start = outline->items[outline->count - 1].pos;
5342 BOOL to_curve = curve->wType != TT_PRIM_LINE && curve->cpfx > 1;
5343 unsigned int j2 = 0;
5344
5345 if (!curve->cpfx) {
5346 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
5347 continue;
5348 }
5349
5350 pt_flt = convert_fixed_to_float(curve->apfx, curve->cpfx, emsquare);
5351
5352 attempt_line_merge(outline, outline->count - 1, &pt_flt[0], to_curve, cos_table);
5353
5354 if (to_curve)
5355 {
5356 HRESULT hr;
5357 int count = curve->cpfx;
5358
5359 while (count > 2)
5360 {
5361 D3DXVECTOR2 bezier_end;
5362
5363 D3DXVec2Scale(&bezier_end, D3DXVec2Add(&bezier_end, &pt_flt[j2], &pt_flt[j2+1]), 0.5f);
5364 hr = add_bezier_points(outline, &bezier_start, &pt_flt[j2], &bezier_end, max_deviation_sq);
5365 if (hr != S_OK)
5366 return hr;
5367 bezier_start = bezier_end;
5368 count--;
5369 j2++;
5370 }
5371 hr = add_bezier_points(outline, &bezier_start, &pt_flt[j2], &pt_flt[j2+1], max_deviation_sq);
5372 if (hr != S_OK)
5373 return hr;
5374
5375 pt = add_points(outline, 1);
5376 if (!pt)
5377 return E_OUTOFMEMORY;
5378 j2++;
5379 pt->pos = pt_flt[j2];
5380 pt->corner = POINTTYPE_CURVE_END;
5381 } else {
5382 pt = add_points(outline, curve->cpfx);
5383 if (!pt)
5384 return E_OUTOFMEMORY;
5385 for (j2 = 0; j2 < curve->cpfx; j2++)
5386 {
5387 pt->pos = pt_flt[j2];
5388 pt->corner = POINTTYPE_CORNER;
5389 pt++;
5390 }
5391 }
5392
5393 curve = (TTPOLYCURVE *)&curve->apfx[curve->cpfx];
5394 }
5395
5396 /* remove last point if the next line continues the last line */
5397 if (outline->count >= 3) {
5398 BOOL to_curve;
5399
5400 lastpt = &outline->items[outline->count - 1];
5401 pt = &outline->items[0];
5402 if (pt->pos.x == lastpt->pos.x && pt->pos.y == lastpt->pos.y) {
5403 if (lastpt->corner == POINTTYPE_CURVE_END)
5404 {
5405 if (pt->corner == POINTTYPE_CURVE_START)
5406 pt->corner = POINTTYPE_CURVE_MIDDLE;
5407 else
5408 pt->corner = POINTTYPE_CURVE_END;
5409 }
5410 outline->count--;
5411 lastpt = &outline->items[outline->count - 1];
5412 } else {
5413 /* outline closed with a line from end to start point */
5414 attempt_line_merge(outline, outline->count - 1, &pt->pos, FALSE, cos_table);
5415 }
5416 lastpt = &outline->items[0];
5417 to_curve = lastpt->corner != POINTTYPE_CORNER && lastpt->corner != POINTTYPE_CURVE_END;
5418 if (lastpt->corner == POINTTYPE_CURVE_START)
5419 lastpt->corner = POINTTYPE_CORNER;
5420 pt = &outline->items[1];
5421 if (attempt_line_merge(outline, 0, &pt->pos, to_curve, cos_table))
5422 *lastpt = outline->items[outline->count];
5423 }
5424
5425 lastpt = &outline->items[outline->count - 1];
5426 pt = &outline->items[0];
5427 unit_vec2(&lastdir, &lastpt->pos, &pt->pos);
5428 for (j = 0; j < outline->count; j++)
5429 {
5430 D3DXVECTOR2 curdir;
5431
5432 lastpt = pt;
5433 pt = &outline->items[(j + 1) % outline->count];
5434 unit_vec2(&curdir, &lastpt->pos, &pt->pos);
5435
5436 switch (lastpt->corner)
5437 {
5438 case POINTTYPE_CURVE_START:
5439 case POINTTYPE_CURVE_END:
5440 if (!is_direction_similar(&lastdir, &curdir, cos_table->cos_45))
5441 lastpt->corner = POINTTYPE_CORNER;
5442 break;
5443 case POINTTYPE_CURVE_MIDDLE:
5444 if (!is_direction_similar(&lastdir, &curdir, cos_table->cos_90))
5445 lastpt->corner = POINTTYPE_CORNER;
5446 else
5447 lastpt->corner = POINTTYPE_CURVE;
5448 break;
5449 default:
5450 break;
5451 }
5452 lastdir = curdir;
5453 }
5454
5455 header = (TTPOLYGONHEADER *)((char *)header + header->cb);
5456 }
5457 return S_OK;
5458 }
5459
5460 /* Get the y-distance from a line to a point */
5461 static float get_line_to_point_y_distance(D3DXVECTOR2 *line_pt1,
5462 D3DXVECTOR2 *line_pt2,
5463 D3DXVECTOR2 *point)
5464 {
5465 D3DXVECTOR2 line_vec = {0, 0};
5466 float line_pt_dx;
5467 float line_y;
5468
5469 D3DXVec2Subtract(&line_vec, line_pt2, line_pt1);
5470 line_pt_dx = point->x - line_pt1->x;
5471 line_y = line_pt1->y + (line_vec.y * line_pt_dx) / line_vec.x;
5472 return point->y - line_y;
5473 }
5474
5475 static D3DXVECTOR2 *get_indexed_point(struct point2d_index *pt_idx)
5476 {
5477 return &pt_idx->outline->items[pt_idx->vertex].pos;
5478 }
5479
5480 static D3DXVECTOR2 *get_ordered_vertex(struct glyphinfo *glyph, WORD index)
5481 {
5482 return get_indexed_point(&glyph->ordered_vertices.items[index]);
5483 }
5484
5485 static void remove_triangulation(struct triangulation_array *array, struct triangulation *item)
5486 {
5487 HeapFree(GetProcessHeap(), 0, item->vertex_stack.items);
5488 MoveMemory(item, item + 1, (char*)&array->items[array->count] - (char*)(item + 1));
5489 array->count--;
5490 }
5491
5492 static HRESULT triangulation_add_point(struct triangulation **t_ptr,
5493 struct triangulation_array *triangulations,
5494 WORD vtx_idx,
5495 BOOL to_top)
5496 {
5497 struct glyphinfo *glyph = triangulations->glyph;
5498 struct triangulation *t = *t_ptr;
5499 HRESULT hr;
5500 face *face;
5501 int f1, f2;
5502
5503 if (t->last_on_top) {
5504 f1 = 1;
5505 f2 = 2;
5506 } else {
5507 f1 = 2;
5508 f2 = 1;
5509 }
5510
5511 if (t->last_on_top != to_top && t->vertex_stack.count > 1) {
5512 /* consume all vertices on the stack */
5513 WORD last_pt = t->vertex_stack.items[0];
5514 int i;
5515 for (i = 1; i < t->vertex_stack.count; i++)
5516 {
5517 face = add_face(&glyph->faces);
5518 if (!face) return E_OUTOFMEMORY;
5519 (*face)[0] = vtx_idx;
5520 (*face)[f1] = last_pt;
5521 (*face)[f2] = last_pt = t->vertex_stack.items[i];
5522 }
5523 t->vertex_stack.items[0] = last_pt;
5524 t->vertex_stack.count = 1;
5525 } else if (t->vertex_stack.count > 1) {
5526 int i = t->vertex_stack.count - 1;
5527 D3DXVECTOR2 *point = get_ordered_vertex(glyph, vtx_idx);
5528 WORD top_idx = t->vertex_stack.items[i--];
5529 D3DXVECTOR2 *top_pt = get_ordered_vertex(glyph, top_idx);
5530
5531 while (i >= 0)
5532 {
5533 WORD prev_idx = t->vertex_stack.items[i--];
5534 D3DXVECTOR2 *prev_pt = get_ordered_vertex(glyph, prev_idx);
5535
5536 if (prev_pt->x != top_pt->x &&
5537 ((to_top && get_line_to_point_y_distance(prev_pt, top_pt, point) > 0) ||
5538 (!to_top && get_line_to_point_y_distance(prev_pt, top_pt, point) < 0)))
5539 break;
5540
5541 face = add_face(&glyph->faces);
5542 if (!face) return E_OUTOFMEMORY;
5543 (*face)[0] = vtx_idx;
5544 (*face)[f1] = prev_idx;
5545 (*face)[f2] = top_idx;
5546
5547 top_pt = prev_pt;
5548 top_idx = prev_idx;
5549 t->vertex_stack.count--;
5550 }
5551 }
5552 t->last_on_top = to_top;
5553
5554 hr = add_vertex_index(&t->vertex_stack, vtx_idx);
5555
5556 if (hr == S_OK && t->merging) {
5557 struct triangulation *t2;
5558
5559 t2 = to_top ? t - 1 : t + 1;
5560 t2->merging = FALSE;
5561 hr = triangulation_add_point(&t2, triangulations, vtx_idx, to_top);
5562 if (hr != S_OK) return hr;
5563 remove_triangulation(triangulations, t);
5564 if (t2 > t)
5565 t2--;
5566 *t_ptr = t2;
5567 }
5568 return hr;
5569 }
5570
5571 /* check if the point is next on the outline for either the top or bottom */
5572 static D3DXVECTOR2 *triangulation_get_next_point(struct triangulation *t, struct glyphinfo *glyph, BOOL on_top)
5573 {
5574 int i = t->last_on_top == on_top ? t->vertex_stack.count - 1 : 0;
5575 WORD idx = t->vertex_stack.items[i];
5576 struct point2d_index *pt_idx = &glyph->ordered_vertices.items[idx];
5577 struct outline *outline = pt_idx->outline;
5578
5579 if (on_top)
5580 i = (pt_idx->vertex + outline->count - 1) % outline->count;
5581 else
5582 i = (pt_idx->vertex + 1) % outline->count;
5583
5584 return &outline->items[i].pos;
5585 }
5586
5587 static int compare_vertex_indices(const void *a, const void *b)
5588 {
5589 const struct point2d_index *idx1 = a, *idx2 = b;
5590 const D3DXVECTOR2 *p1 = &idx1->outline->items[idx1->vertex].pos;
5591 const D3DXVECTOR2 *p2 = &idx2->outline->items[idx2->vertex].pos;
5592 float diff = p1->x - p2->x;
5593
5594 if (diff == 0.0f)
5595 diff = p1->y - p2->y;
5596
5597 return diff == 0.0f ? 0 : (diff > 0.0f ? -1 : 1);
5598 }
5599
5600 static HRESULT triangulate(struct triangulation_array *triangulations)
5601 {
5602 int sweep_idx;
5603 HRESULT hr;
5604 struct glyphinfo *glyph = triangulations->glyph;
5605 int nb_vertices = 0;
5606 int i;
5607 struct point2d_index *idx_ptr;
5608
5609 for (i = 0; i < glyph->outlines.count; i++)
5610 nb_vertices += glyph->outlines.items[i].count;
5611
5612 glyph->ordered_vertices.items = HeapAlloc(GetProcessHeap(), 0,
5613 nb_vertices * sizeof(*glyph->ordered_vertices.items));
5614 if (!glyph->ordered_vertices.items)
5615 return E_OUTOFMEMORY;
5616
5617 idx_ptr = glyph->ordered_vertices.items;
5618 for (i = 0; i < glyph->outlines.count; i++)
5619 {
5620 struct outline *outline = &glyph->outlines.items[i];
5621 int j;
5622
5623 idx_ptr->outline = outline;
5624 idx_ptr->vertex = 0;
5625 idx_ptr++;
5626 for (j = outline->count - 1; j > 0; j--)
5627 {
5628 idx_ptr->outline = outline;
5629 idx_ptr->vertex = j;
5630 idx_ptr++;
5631 }
5632 }
5633 glyph->ordered_vertices.count = nb_vertices;
5634
5635 /* Native implementation seems to try to create a triangle fan from
5636 * the first outline point if the glyph only has one outline. */
5637 if (glyph->outlines.count == 1)
5638 {
5639 struct outline *outline = glyph->outlines.items;
5640 D3DXVECTOR2 *base = &outline->items[0].pos;
5641 D3DXVECTOR2 *last = &outline->items[1].pos;
5642 float ccw = 0;
5643
5644 for (i = 2; i < outline->count; i++)
5645 {
5646 D3DXVECTOR2 *next = &outline->items[i].pos;
5647 D3DXVECTOR2 v1 = {0.0f, 0.0f};
5648 D3DXVECTOR2 v2 = {0.0f, 0.0f};
5649
5650 D3DXVec2Subtract(&v1, base, last);
5651 D3DXVec2Subtract(&v2, last, next);
5652 ccw = D3DXVec2CCW(&v1, &v2);
5653 if (ccw > 0.0f)
5654 break;
5655
5656 last = next;
5657 }
5658 if (ccw <= 0)
5659 {
5660 glyph->faces.items = HeapAlloc(GetProcessHeap(), 0,
5661 (outline->count - 2) * sizeof(glyph->faces.items[0]));
5662 if (!glyph->faces.items)
5663 return E_OUTOFMEMORY;
5664
5665 glyph->faces.count = outline->count - 2;
5666 for (i = 0; i < glyph->faces.count; i++)
5667 {
5668 glyph->faces.items[i][0] = 0;
5669 glyph->faces.items[i][1] = i + 1;
5670 glyph->faces.items[i][2] = i + 2;
5671 }
5672 return S_OK;
5673 }
5674 }
5675
5676 /* Perform 2D polygon triangulation for complex glyphs.
5677 * Triangulation is performed using a sweep line concept, from right to left,
5678 * by processing vertices in sorted order. Complex polygons are split into
5679 * monotone polygons which are triangulated separately. */
5680 /* FIXME: The order of the faces is not consistent with the native implementation. */
5681
5682 /* Reserve space for maximum possible faces from triangulation.
5683 * # faces for outer outlines = outline->count - 2
5684 * # faces for inner outlines = outline->count + 2
5685 * There must be at least 1 outer outline. */
5686 glyph->faces.items = HeapAlloc(GetProcessHeap(), 0,
5687 (nb_vertices + glyph->outlines.count * 2 - 4) * sizeof(glyph->faces.items[0]));
5688 if (!glyph->faces.items)
5689 return E_OUTOFMEMORY;
5690
5691 qsort(glyph->ordered_vertices.items, nb_vertices,
5692 sizeof(glyph->ordered_vertices.items[0]), compare_vertex_indices);
5693 for (sweep_idx = 0; sweep_idx < glyph->ordered_vertices.count; sweep_idx++)
5694 {
5695 int start = 0;
5696 int end = triangulations->count;
5697
5698 while (start < end)
5699 {
5700 D3DXVECTOR2 *sweep_vtx = get_ordered_vertex(glyph, sweep_idx);
5701 int current = (start + end) / 2;
5702 struct triangulation *t = &triangulations->items[current];
5703 BOOL on_top_outline = FALSE;
5704 D3DXVECTOR2 *top_next, *bottom_next;
5705 WORD top_idx, bottom_idx;
5706
5707 if (t->merging && t->last_on_top)
5708 top_next = triangulation_get_next_point(t + 1, glyph, TRUE);
5709 else
5710 top_next = triangulation_get_next_point(t, glyph, TRUE);
5711 if (sweep_vtx == top_next)
5712 {
5713 if (t->merging && t->last_on_top)
5714 t++;
5715 hr = triangulation_add_point(&t, triangulations, sweep_idx, TRUE);
5716 if (hr != S_OK) return hr;
5717
5718 if (t + 1 < &triangulations->items[triangulations->count] &&
5719 triangulation_get_next_point(t + 1, glyph, FALSE) == sweep_vtx)
5720 {
5721 /* point also on bottom outline of higher triangulation */
5722 struct triangulation *t2 = t + 1;
5723 hr = triangulation_add_point(&t2, triangulations, sweep_idx, FALSE);
5724 if (hr != S_OK) return hr;
5725
5726 t->merging = TRUE;
5727 t2->merging = TRUE;
5728 }
5729 on_top_outline = TRUE;
5730 }
5731
5732 if (t->merging && !t->last_on_top)
5733 bottom_next = triangulation_get_next_point(t - 1, glyph, FALSE);
5734 else
5735 bottom_next = triangulation_get_next_point(t, glyph, FALSE);
5736 if (sweep_vtx == bottom_next)
5737 {
5738 if (t->merging && !t->last_on_top)
5739 t--;
5740 if (on_top_outline) {
5741 /* outline finished */
5742 remove_triangulation(triangulations, t);
5743 break;
5744 }
5745
5746 hr = triangulation_add_point(&t, triangulations, sweep_idx, FALSE);
5747 if (hr != S_OK) return hr;
5748
5749 if (t > triangulations->items &&
5750 triangulation_get_next_point(t - 1, glyph, TRUE) == sweep_vtx)
5751 {
5752 struct triangulation *t2 = t - 1;
5753 /* point also on top outline of lower triangulation */
5754 hr = triangulation_add_point(&t2, triangulations, sweep_idx, TRUE);
5755 if (hr != S_OK) return hr;
5756 t = t2 + 1; /* t may be invalidated by triangulation merging */
5757
5758 t->merging = TRUE;
5759 t2->merging = TRUE;
5760 }
5761 break;
5762 }
5763 if (on_top_outline)
5764 break;
5765
5766 if (t->last_on_top) {
5767 top_idx = t->vertex_stack.items[t->vertex_stack.count - 1];
5768 bottom_idx = t->vertex_stack.items[0];
5769 } else {
5770 top_idx = t->vertex_stack.items[0];
5771 bottom_idx = t->vertex_stack.items[t->vertex_stack.count - 1];
5772 }
5773
5774 /* check if the point is inside or outside this polygon */
5775 if (get_line_to_point_y_distance(get_ordered_vertex(glyph, top_idx),
5776 top_next, sweep_vtx) > 0)
5777 { /* above */
5778 start = current + 1;
5779 } else if (get_line_to_point_y_distance(get_ordered_vertex(glyph, bottom_idx),
5780 bottom_next, sweep_vtx) < 0)
5781 { /* below */
5782 end = current;
5783 } else if (t->merging) {
5784 /* inside, so cancel merging */
5785 struct triangulation *t2 = t->last_on_top ? t + 1 : t - 1;
5786 t->merging = FALSE;
5787 t2->merging = FALSE;
5788 hr = triangulation_add_point(&t, triangulations, sweep_idx, t->last_on_top);
5789 if (hr != S_OK) return hr;
5790 hr = triangulation_add_point(&t2, triangulations, sweep_idx, t2->last_on_top);
5791 if (hr != S_OK) return hr;
5792 break;
5793 } else {
5794 /* inside, so split polygon into two monotone parts */
5795 struct triangulation *t2 = add_triangulation(triangulations);
5796 if (!t2) return E_OUTOFMEMORY;
5797 MoveMemory(t + 1, t, (char*)(t2 + 1) - (char*)t);
5798 if (t->last_on_top) {
5799 t2 = t + 1;
5800 } else {
5801 t2 = t;
5802 t++;
5803 }
5804
5805 ZeroMemory(&t2->vertex_stack, sizeof(t2->vertex_stack));
5806 hr = add_vertex_index(&t2->vertex_stack, t->vertex_stack.items[t->vertex_stack.count - 1]);
5807 if (hr != S_OK) return hr;
5808 hr = add_vertex_index(&t2->vertex_stack, sweep_idx);
5809 if (hr != S_OK) return hr;
5810 t2->last_on_top = !t->last_on_top;
5811
5812 hr = triangulation_add_point(&t, triangulations, sweep_idx, t->last_on_top);
5813 if (hr != S_OK) return hr;
5814 break;
5815 }
5816 }
5817 if (start >= end)
5818 {
5819 struct triangulation *t;
5820 struct triangulation *t2 = add_triangulation(triangulations);
5821 if (!t2) return E_OUTOFMEMORY;
5822 t = &triangulations->items[start];
5823 MoveMemory(t + 1, t, (char*)(t2 + 1) - (char*)t);
5824 ZeroMemory(t, sizeof(*t));
5825 hr = add_vertex_index(&t->vertex_stack, sweep_idx);
5826 if (hr != S_OK) return hr;
5827 }
5828 }
5829 return S_OK;
5830 }
5831
5832 HRESULT WINAPI D3DXCreateTextW(struct IDirect3DDevice9 *device, HDC hdc, const WCHAR *text, float deviation,
5833 float extrusion, struct ID3DXMesh **mesh_ptr, struct ID3DXBuffer **adjacency, GLYPHMETRICSFLOAT *glyphmetrics)
5834 {
5835 HRESULT hr;
5836 ID3DXMesh *mesh = NULL;
5837 DWORD nb_vertices, nb_faces;
5838 DWORD nb_front_faces, nb_corners, nb_outline_points;
5839 struct vertex *vertices = NULL;
5840 face *faces = NULL;
5841 int textlen = 0;
5842 float offset_x;
5843 LOGFONTW lf;
5844 OUTLINETEXTMETRICW otm;
5845 HFONT font = NULL, oldfont = NULL;
5846 const MAT2 identity = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
5847 void *raw_outline = NULL;
5848 int bufsize = 0;
5849 struct glyphinfo *glyphs = NULL;
5850 GLYPHMETRICS gm;
5851 struct triangulation_array triangulations = {0, 0, NULL};
5852 int i;
5853 struct vertex *vertex_ptr;
5854 face *face_ptr;
5855 float max_deviation_sq;
5856 const struct cos_table cos_table = {
5857 cosf(D3DXToRadian(0.5f)),
5858 cosf(D3DXToRadian(45.0f)),
5859 cosf(D3DXToRadian(90.0f)),
5860 };
5861 int f1, f2;
5862
5863 TRACE("(%p, %p, %s, %f, %f, %p, %p, %p)\n", device, hdc,
5864 debugstr_w(text), deviation, extrusion, mesh_ptr, adjacency, glyphmetrics);
5865
5866 if (!device || !hdc || !text || !*text || deviation < 0.0f || extrusion < 0.0f || !mesh_ptr)
5867 return D3DERR_INVALIDCALL;
5868
5869 if (adjacency)
5870 {
5871 FIXME("Case of adjacency != NULL not implemented.\n");
5872 return E_NOTIMPL;
5873 }
5874
5875 if (!GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf) ||
5876 !GetOutlineTextMetricsW(hdc, sizeof(otm), &otm))
5877 {
5878 return D3DERR_INVALIDCALL;
5879 }
5880
5881 if (deviation == 0.0f)
5882 deviation = 1.0f / otm.otmEMSquare;
5883 max_deviation_sq = deviation * deviation;
5884
5885 lf.lfHeight = otm.otmEMSquare;
5886 lf.lfWidth = 0;
5887 font = CreateFontIndirectW(&lf);
5888 if (!font) {
5889 hr = E_OUTOFMEMORY;
5890 goto error;
5891 }
5892 oldfont = SelectObject(hdc, font);
5893
5894 textlen = strlenW(text);
5895 for (i = 0; i < textlen; i++)
5896 {
5897 int datasize = GetGlyphOutlineW(hdc, text[i], GGO_NATIVE, &gm, 0, NULL, &identity);
5898 if (datasize < 0)
5899 return D3DERR_INVALIDCALL;
5900 if (bufsize < datasize)
5901 bufsize = datasize;
5902 }
5903 if (!bufsize) { /* e.g. text == " " */
5904 hr = D3DERR_INVALIDCALL;
5905 goto error;
5906 }
5907
5908 glyphs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, textlen * sizeof(*glyphs));
5909 raw_outline = HeapAlloc(GetProcessHeap(), 0, bufsize);
5910 if (!glyphs || !raw_outline) {
5911 hr = E_OUTOFMEMORY;
5912 goto error;
5913 }
5914
5915 offset_x = 0.0f;
5916 for (i = 0; i < textlen; i++)
5917 {
5918 /* get outline points from data returned from GetGlyphOutline */
5919 int datasize;
5920
5921 glyphs[i].offset_x = offset_x;
5922
5923 datasize = GetGlyphOutlineW(hdc, text[i], GGO_NATIVE, &gm, bufsize, raw_outline, &identity);
5924 hr = create_outline(&glyphs[i], raw_outline, datasize,
5925 max_deviation_sq, otm.otmEMSquare, &cos_table);
5926 if (hr != S_OK) goto error;
5927
5928 triangulations.glyph = &glyphs[i];
5929 hr = triangulate(&triangulations);
5930 if (hr != S_OK) goto error;
5931 if (triangulations.count) {
5932 ERR("%d incomplete triangulations of glyph (%u).\n", triangulations.count, text[i]);
5933 triangulations.count = 0;
5934 }
5935
5936 if (glyphmetrics)
5937 {
5938 glyphmetrics[i].gmfBlackBoxX = gm.gmBlackBoxX / (float)otm.otmEMSquare;
5939 glyphmetrics[i].gmfBlackBoxY = gm.gmBlackBoxY / (float)otm.otmEMSquare;
5940 glyphmetrics[i].gmfptGlyphOrigin.x = gm.gmptGlyphOrigin.x / (float)otm.otmEMSquare;
5941 glyphmetrics[i].gmfptGlyphOrigin.y = gm.gmptGlyphOrigin.y / (float)otm.otmEMSquare;
5942 glyphmetrics[i].gmfCellIncX = gm.gmCellIncX / (float)otm.otmEMSquare;
5943 glyphmetrics[i].gmfCellIncY = gm.gmCellIncY / (float)otm.otmEMSquare;
5944 }
5945 offset_x += gm.gmCellIncX / (float)otm.otmEMSquare;
5946 }
5947
5948 /* corner points need an extra vertex for the different side faces normals */
5949 nb_corners = 0;
5950 nb_outline_points = 0;
5951 nb_front_faces = 0;
5952 for (i = 0; i < textlen; i++)
5953 {
5954 int j;
5955 nb_outline_points += glyphs[i].ordered_vertices.count;
5956 nb_front_faces += glyphs[i].faces.count;
5957 for (j = 0; j < glyphs[i].outlines.count; j++)
5958 {
5959 int k;
5960 struct outline *outline = &glyphs[i].outlines.items[j];
5961 nb_corners++; /* first outline point always repeated as a corner */
5962 for (k = 1; k < outline->count; k++)
5963 if (outline->items[k].corner)
5964 nb_corners++;
5965 }
5966 }
5967
5968 nb_vertices = (nb_outline_points + nb_corners) * 2 + nb_outline_points * 2;
5969 nb_faces = nb_outline_points * 2 + nb_front_faces * 2;
5970
5971
5972 hr = D3DXCreateMeshFVF(nb_faces, nb_vertices, D3DXMESH_MANAGED,
5973 D3DFVF_XYZ | D3DFVF_NORMAL, device, &mesh);
5974 if (FAILED(hr))
5975 goto error;
5976
5977 if (FAILED(hr = mesh->lpVtbl->LockVertexBuffer(mesh, 0, (void **)&vertices)))
5978 goto error;
5979
5980 if (FAILED(hr = mesh->lpVtbl->LockIndexBuffer(mesh, 0, (void **)&faces)))
5981 goto error;
5982
5983 /* convert 2D vertices and faces into 3D mesh */
5984 vertex_ptr = vertices;
5985 face_ptr = faces;
5986 if (extrusion == 0.0f) {
5987 f1 = 1;
5988 f2 = 2;
5989 } else {
5990 f1 = 2;
5991 f2 = 1;
5992 }
5993 for (i = 0; i < textlen; i++)
5994 {
5995 int j;
5996 int count;
5997 struct vertex *back_vertices;
5998 face *back_faces;
5999
6000 /* side vertices and faces */
6001 for (j = 0; j < glyphs[i].outlines.count; j++)
6002 {
6003 struct vertex *outline_vertices = vertex_ptr;
6004 struct outline *outline = &glyphs[i].outlines.items[j];
6005 int k;
6006 struct point2d *prevpt = &outline->items[outline->count - 1];
6007 struct point2d *pt = &outline->items[0];
6008
6009 for (k = 1; k <= outline->count; k++)
6010 {
6011 struct vertex vtx;
6012 struct point2d *nextpt = &outline->items[k % outline->count];
6013 WORD vtx_idx = vertex_ptr - vertices;
6014 D3DXVECTOR2 vec;
6015
6016 if (pt->corner == POINTTYPE_CURVE_START)
6017 D3DXVec2Subtract(&vec, &pt->pos, &prevpt->pos);
6018 else if (pt->corner)
6019 D3DXVec2Subtract(&vec, &nextpt->pos, &pt->pos);
6020 else
6021 D3DXVec2Subtract(&vec, &nextpt->pos, &prevpt->pos);
6022 D3DXVec2Normalize(&vec, &vec);
6023 vtx.normal.x = -vec.y;
6024 vtx.normal.y = vec.x;
6025 vtx.normal.z = 0;
6026
6027 vtx.position.x = pt->pos.x + glyphs[i].offset_x;
6028 vtx.position.y = pt->pos.y;
6029 vtx.position.z = 0;
6030 *vertex_ptr++ = vtx;
6031
6032 vtx.position.z = -extrusion;
6033 *vertex_ptr++ = vtx;
6034
6035 vtx.position.x = nextpt->pos.x + glyphs[i].offset_x;
6036 vtx.position.y = nextpt->pos.y;
6037 if (pt->corner && nextpt->corner && nextpt->corner != POINTTYPE_CURVE_END) {
6038 vtx.position.z = -extrusion;
6039 *vertex_ptr++ = vtx;
6040 vtx.position.z = 0;
6041 *vertex_ptr++ = vtx;
6042
6043 (*face_ptr)[0] = vtx_idx;
6044 (*face_ptr)[1] = vtx_idx + 2;
6045 (*face_ptr)[2] = vtx_idx + 1;
6046 face_ptr++;
6047
6048 (*face_ptr)[0] = vtx_idx;
6049 (*face_ptr)[1] = vtx_idx + 3;
6050 (*face_ptr)[2] = vtx_idx + 2;
6051 face_ptr++;
6052 } else {
6053 if (nextpt->corner) {
6054 if (nextpt->corner == POINTTYPE_CURVE_END) {
6055 D3DXVECTOR2 *nextpt2 = &outline->items[(k + 1) % outline->count].pos;
6056 D3DXVec2Subtract(&vec, nextpt2, &nextpt->pos);
6057 } else {
6058 D3DXVec2Subtract(&vec, &nextpt->pos, &pt->pos);
6059 }
6060 D3DXVec2Normalize(&vec, &vec);
6061 vtx.normal.x = -vec.y;
6062 vtx.normal.y = vec.x;
6063
6064 vtx.position.z = 0;
6065 *vertex_ptr++ = vtx;
6066 vtx.position.z = -extrusion;
6067 *vertex_ptr++ = vtx;
6068 }
6069
6070 (*face_ptr)[0] = vtx_idx;
6071 (*face_ptr)[1] = vtx_idx + 3;
6072 (*face_ptr)[2] = vtx_idx + 1;
6073 face_ptr++;
6074
6075 (*face_ptr)[0] = vtx_idx;
6076 (*face_ptr)[1] = vtx_idx + 2;
6077 (*face_ptr)[2] = vtx_idx + 3;
6078 face_ptr++;
6079 }
6080
6081 prevpt = pt;
6082 pt = nextpt;
6083 }
6084 if (!pt->corner) {
6085 *vertex_ptr++ = *outline_vertices++;
6086 *vertex_ptr++ = *outline_vertices++;
6087 }
6088 }
6089
6090 /* back vertices and faces */
6091 back_faces = face_ptr;
6092 back_vertices = vertex_ptr;
6093 for (j = 0; j < glyphs[i].ordered_vertices.count; j++)
6094 {
6095 D3DXVECTOR2 *pt = get_ordered_vertex(&glyphs[i], j);
6096 vertex_ptr->position.x = pt->x + glyphs[i].offset_x;
6097 vertex_ptr->position.y = pt->y;
6098 vertex_ptr->position.z = 0;
6099 vertex_ptr->normal.x = 0;
6100 vertex_ptr->normal.y = 0;
6101 vertex_ptr->normal.z = 1;
6102 vertex_ptr++;
6103 }
6104 count = back_vertices - vertices;
6105 for (j = 0; j < glyphs[i].faces.count; j++)
6106 {
6107 face *f = &glyphs[i].faces.items[j];
6108 (*face_ptr)[0] = (*f)[0] + count;
6109 (*face_ptr)[1] = (*f)[1] + count;
6110 (*face_ptr)[2] = (*f)[2] + count;
6111 face_ptr++;
6112 }
6113
6114 /* front vertices and faces */
6115 j = count = vertex_ptr - back_vertices;
6116 while (j--)
6117 {
6118 vertex_ptr->position.x = back_vertices->position.x;
6119 vertex_ptr->position.y = back_vertices->position.y;
6120 vertex_ptr->position.z = -extrusion;
6121 vertex_ptr->normal.x = 0;
6122 vertex_ptr->normal.y = 0;
6123 vertex_ptr->normal.z = extrusion == 0.0f ? 1.0f : -1.0f;
6124 vertex_ptr++;
6125 back_vertices++;
6126 }
6127 j = face_ptr - back_faces;
6128 while (j--)
6129 {
6130 (*face_ptr)[0] = (*back_faces)[0] + count;
6131 (*face_ptr)[1] = (*back_faces)[f1] + count;
6132 (*face_ptr)[2] = (*back_faces)[f2] + count;
6133 face_ptr++;
6134 back_faces++;
6135 }
6136 }
6137
6138 *mesh_ptr = mesh;
6139 hr = D3D_OK;
6140 error:
6141 if (mesh) {
6142 if (faces) mesh->lpVtbl->UnlockIndexBuffer(mesh);
6143 if (vertices) mesh->lpVtbl->UnlockVertexBuffer(mesh);
6144 if (hr != D3D_OK) mesh->lpVtbl->Release(mesh);
6145 }
6146 if (glyphs) {
6147 for (i = 0; i < textlen; i++)
6148 {
6149 int j;
6150 for (j = 0; j < glyphs[i].outlines.count; j++)
6151 HeapFree(GetProcessHeap(), 0, glyphs[i].outlines.items[j].items);
6152 HeapFree(GetProcessHeap(), 0, glyphs[i].outlines.items);
6153 HeapFree(GetProcessHeap(), 0, glyphs[i].faces.items);
6154 HeapFree(GetProcessHeap(), 0, glyphs[i].ordered_vertices.items);
6155 }
6156 HeapFree(GetProcessHeap(), 0, glyphs);
6157 }
6158 if (triangulations.items) {
6159 int i;
6160 for (i = 0; i < triangulations.count; i++)
6161 HeapFree(GetProcessHeap(), 0, triangulations.items[i].vertex_stack.items);
6162 HeapFree(GetProcessHeap(), 0, triangulations.items);
6163 }
6164 HeapFree(GetProcessHeap(), 0, raw_outline);
6165 if (oldfont) SelectObject(hdc, oldfont);
6166 if (font) DeleteObject(font);
6167
6168 return hr;
6169 }
6170
6171 HRESULT WINAPI D3DXValidMesh(ID3DXMesh *mesh, const DWORD *adjacency, ID3DXBuffer **errors_and_warnings)
6172 {
6173 FIXME("(%p, %p, %p): stub\n", mesh, adjacency, *errors_and_warnings);
6174
6175 return E_NOTIMPL;
6176 }
6177
6178 static BOOL weld_float1(void *to, void *from, FLOAT epsilon)
6179 {
6180 FLOAT *v1 = to;
6181 FLOAT *v2 = from;
6182
6183 if (fabsf(*v1 - *v2) <= epsilon)
6184 {
6185 *v1 = *v2;
6186
6187 return TRUE;
6188 }
6189
6190 return FALSE;
6191 }
6192
6193 static BOOL weld_float2(void *to, void *from, FLOAT epsilon)
6194 {
6195 D3DXVECTOR2 *v1 = to;
6196 D3DXVECTOR2 *v2 = from;
6197 FLOAT diff_x = fabsf(v1->x - v2->x);
6198 FLOAT diff_y = fabsf(v1->y - v2->y);
6199 FLOAT max_abs_diff = max(diff_x, diff_y);
6200
6201 if (max_abs_diff <= epsilon)
6202 {
6203 memcpy(to, from, sizeof(D3DXVECTOR2));
6204
6205 return TRUE;
6206 }
6207
6208 return FALSE;
6209 }
6210
6211 static BOOL weld_float3(void *to, void *from, FLOAT epsilon)
6212 {
6213 D3DXVECTOR3 *v1 = to;
6214 D3DXVECTOR3 *v2 = from;
6215 FLOAT diff_x = fabsf(v1->x - v2->x);
6216 FLOAT diff_y = fabsf(v1->y - v2->y);
6217 FLOAT diff_z = fabsf(v1->z - v2->z);
6218 FLOAT max_abs_diff = max(diff_x, diff_y);
6219 max_abs_diff = max(diff_z, max_abs_diff);
6220
6221 if (max_abs_diff <= epsilon)
6222 {
6223 memcpy(to, from, sizeof(D3DXVECTOR3));
6224
6225 return TRUE;
6226 }
6227
6228 return FALSE;
6229 }
6230
6231 static BOOL weld_float4(void *to, void *from, FLOAT epsilon)
6232 {
6233 D3DXVECTOR4 *v1 = to;
6234 D3DXVECTOR4 *v2 = from;
6235 FLOAT diff_x = fabsf(v1->x - v2->x);
6236 FLOAT diff_y = fabsf(v1->y - v2->y);
6237 FLOAT diff_z = fabsf(v1->z - v2->z);
6238 FLOAT diff_w = fabsf(v1->w - v2->w);
6239 FLOAT max_abs_diff = max(diff_x, diff_y);
6240 max_abs_diff = max(diff_z, max_abs_diff);
6241 max_abs_diff = max(diff_w, max_abs_diff);
6242
6243 if (max_abs_diff <= epsilon)
6244 {
6245 memcpy(to, from, sizeof(D3DXVECTOR4));
6246
6247 return TRUE;
6248 }
6249
6250 return FALSE;
6251 }
6252
6253 static BOOL weld_ubyte4(void *to, void *from, FLOAT epsilon)
6254 {
6255 BYTE *b1 = to;
6256 BYTE *b2 = from;
6257 BYTE truncated_epsilon = (BYTE)epsilon;
6258 BYTE diff_x = b1[0] > b2[0] ? b1[0] - b2[0] : b2[0] - b1[0];
6259 BYTE diff_y = b1[1] > b2[1] ? b1[1] - b2[1] : b2[1] - b1[1];
6260 BYTE diff_z = b1[2] > b2[2] ? b1[2] - b2[2] : b2[2] - b1[2];
6261 BYTE diff_w = b1[3] > b2[3] ? b1[3] - b2[3] : b2[3] - b1[3];
6262 BYTE max_diff = max(diff_x, diff_y);
6263 max_diff = max(diff_z, max_diff);
6264 max_diff = max(diff_w, max_diff);
6265
6266 if (max_diff <= truncated_epsilon)
6267 {
6268 memcpy(to, from, 4 * sizeof(BYTE));
6269
6270 return TRUE;
6271 }
6272
6273 return FALSE;
6274 }
6275
6276 static BOOL weld_ubyte4n(void *to, void *from, FLOAT epsilon)
6277 {
6278 return weld_ubyte4(to, from, epsilon * UCHAR_MAX);
6279 }
6280
6281 static BOOL weld_d3dcolor(void *to, void *from, FLOAT epsilon)
6282 {
6283 return weld_ubyte4n(to, from, epsilon);
6284 }
6285
6286 static BOOL weld_short2(void *to, void *from, FLOAT epsilon)
6287 {
6288 SHORT *s1 = to;
6289 SHORT *s2 = from;
6290 SHORT truncated_epsilon = (SHORT)epsilon;
6291 SHORT diff_x = abs(s1[0] - s2[0]);
6292 SHORT diff_y = abs(s1[1] - s2[1]);
6293 SHORT max_abs_diff = max(diff_x, diff_y);
6294
6295 if (max_abs_diff <= truncated_epsilon)
6296 {
6297 memcpy(to, from, 2 * sizeof(SHORT));
6298
6299 return TRUE;
6300 }
6301
6302 return FALSE;
6303 }
6304
6305 static BOOL weld_short2n(void *to, void *from, FLOAT epsilon)
6306 {
6307 return weld_short2(to, from, epsilon * SHRT_MAX);
6308 }
6309
6310 static BOOL weld_short4(void *to, void *from, FLOAT epsilon)
6311 {
6312 SHORT *s1 = to;
6313 SHORT *s2 = from;
6314 SHORT truncated_epsilon = (SHORT)epsilon;
6315 SHORT diff_x = abs(s1[0] - s2[0]);
6316 SHORT diff_y = abs(s1[1] - s2[1]);
6317 SHORT diff_z = abs(s1[2] - s2[2]);
6318 SHORT diff_w = abs(s1[3] - s2[3]);
6319 SHORT max_abs_diff = max(diff_x, diff_y);
6320 max_abs_diff = max(diff_z, max_abs_diff);
6321 max_abs_diff = max(diff_w, max_abs_diff);
6322
6323 if (max_abs_diff <= truncated_epsilon)
6324 {
6325 memcpy(to, from, 4 * sizeof(SHORT));
6326
6327 return TRUE;
6328 }
6329
6330 return FALSE;
6331 }
6332
6333 static BOOL weld_short4n(void *to, void *from, FLOAT epsilon)
6334 {
6335 return weld_short4(to, from, epsilon * SHRT_MAX);
6336 }
6337
6338 static BOOL weld_ushort2n(void *to, void *from, FLOAT epsilon)
6339 {
6340 USHORT *s1 = to;
6341 USHORT *s2 = from;
6342 USHORT scaled_epsilon = (USHORT)(epsilon * USHRT_MAX);
6343 USHORT diff_x = s1[0] > s2[0] ? s1[0] - s2[0] : s2[0] - s1[0];
6344 USHORT diff_y = s1[1] > s2[1] ? s1[1] - s2[1] : s2[1] - s1[1];
6345 USHORT max_diff = max(diff_x, diff_y);
6346
6347 if (max_diff <= scaled_epsilon)
6348 {
6349 memcpy(to, from, 2 * sizeof(USHORT));
6350
6351 return TRUE;
6352 }
6353
6354 return FALSE;
6355 }
6356
6357 static BOOL weld_ushort4n(void *to, void *from, FLOAT epsilon)
6358 {
6359 USHORT *s1 = to;
6360 USHORT *s2 = from;
6361 USHORT scaled_epsilon = (USHORT)(epsilon * USHRT_MAX);
6362 USHORT diff_x = s1[0] > s2[0] ? s1[0] - s2[0] : s2[0] - s1[0];
6363 USHORT diff_y = s1[1] > s2[1] ? s1[1] - s2[1] : s2[1] - s1[1];
6364 USHORT diff_z = s1[2] > s2[2] ? s1[2] - s2[2] : s2[2] - s1[2];
6365 USHORT diff_w = s1[3] > s2[3] ? s1[3] - s2[3] : s2[3] - s1[3];
6366 USHORT max_diff = max(diff_x, diff_y);
6367 max_diff = max(diff_z, max_diff);
6368 max_diff = max(diff_w, max_diff);
6369
6370 if (max_diff <= scaled_epsilon)
6371 {
6372 memcpy(to, from, 4 * sizeof(USHORT));
6373
6374 return TRUE;
6375 }
6376
6377 return FALSE;
6378 }
6379
6380 struct udec3
6381 {
6382 UINT x;
6383 UINT y;
6384 UINT z;
6385 UINT w;
6386 };
6387
6388 static struct udec3 dword_to_udec3(DWORD d)
6389 {
6390 struct udec3 v;
6391
6392 v.x = d & 0x3ff;
6393 v.y = (d & 0xffc00) >> 10;
6394 v.z = (d & 0x3ff00000) >> 20;
6395 v.w = (d & 0xc0000000) >> 30;
6396
6397 return v;
6398 }
6399
6400 static BOOL weld_udec3(void *to, void *from, FLOAT epsilon)
6401 {
6402 DWORD *d1 = to;
6403 DWORD *d2 = from;
6404 struct udec3 v1 = dword_to_udec3(*d1);
6405 struct udec3 v2 = dword_to_udec3(*d2);
6406 UINT truncated_epsilon = (UINT)epsilon;
6407 UINT diff_x = v1.x > v2.x ? v1.x - v2.x : v2.x - v1.x;
6408 UINT diff_y = v1.y > v2.y ? v1.y - v2.y : v2.y - v1.y;
6409 UINT diff_z = v1.z > v2.z ? v1.z - v2.z : v2.z - v1.z;
6410 UINT diff_w = v1.w > v2.w ? v1.w - v2.w : v2.w - v1.w;
6411 UINT max_diff = max(diff_x, diff_y);
6412 max_diff = max(diff_z, max_diff);
6413 max_diff = max(diff_w, max_diff);
6414
6415 if (max_diff <= truncated_epsilon)
6416 {
6417 memcpy(to, from, sizeof(DWORD));
6418
6419 return TRUE;
6420 }
6421
6422 return FALSE;
6423 }
6424
6425 struct dec3n
6426 {
6427 INT x;
6428 INT y;
6429 INT z;
6430 INT w;
6431 };
6432
6433 static struct dec3n dword_to_dec3n(DWORD d)
6434 {
6435 struct dec3n v;
6436
6437 v.x = d & 0x3ff;
6438 v.y = (d & 0xffc00) >> 10;
6439 v.z = (d & 0x3ff00000) >> 20;
6440 v.w = (d & 0xc0000000) >> 30;
6441
6442 return v;
6443 }
6444
6445 static BOOL weld_dec3n(void *to, void *from, FLOAT epsilon)
6446 {
6447 const UINT MAX_DEC3N = 511;
6448 DWORD *d1 = to;
6449 DWORD *d2 = from;
6450 struct dec3n v1 = dword_to_dec3n(*d1);
6451 struct dec3n v2 = dword_to_dec3n(*d2);
6452 INT scaled_epsilon = (INT)(epsilon * MAX_DEC3N);
6453 INT diff_x = abs(v1.x - v2.x);
6454 INT diff_y = abs(v1.y - v2.y);
6455 INT diff_z = abs(v1.z - v2.z);
6456 INT diff_w = abs(v1.w - v2.w);
6457 INT max_abs_diff = max(diff_x, diff_y);
6458 max_abs_diff = max(diff_z, max_abs_diff);
6459 max_abs_diff = max(diff_w, max_abs_diff);
6460
6461 if (max_abs_diff <= scaled_epsilon)
6462 {
6463 memcpy(to, from, sizeof(DWORD));
6464
6465 return TRUE;
6466 }
6467
6468 return FALSE;
6469 }
6470
6471 static BOOL weld_float16_2(void *to, void *from, FLOAT epsilon)
6472 {
6473 D3DXFLOAT16 *v1_float16 = to;
6474 D3DXFLOAT16 *v2_float16 = from;
6475 FLOAT diff_x;
6476 FLOAT diff_y;
6477 FLOAT max_abs_diff;
6478 #define NUM_ELEM 2
6479 FLOAT v1[NUM_ELEM];
6480 FLOAT v2[NUM_ELEM];
6481
6482 D3DXFloat16To32Array(v1, v1_float16, NUM_ELEM);
6483 D3DXFloat16To32Array(v2, v2_float16, NUM_ELEM);
6484
6485 diff_x = fabsf(v1[0] - v2[0]);
6486 diff_y = fabsf(v1[1] - v2[1]);
6487 max_abs_diff = max(diff_x, diff_y);
6488
6489 if (max_abs_diff <= epsilon)
6490 {
6491 memcpy(to, from, NUM_ELEM * sizeof(D3DXFLOAT16));
6492
6493 return TRUE;
6494 }
6495
6496 return FALSE;
6497 #undef NUM_ELEM
6498 }
6499
6500 static BOOL weld_float16_4(void *to, void *from, FLOAT epsilon)
6501 {
6502 D3DXFLOAT16 *v1_float16 = to;
6503 D3DXFLOAT16 *v2_float16 = from;
6504 FLOAT diff_x;
6505 FLOAT diff_y;
6506 FLOAT diff_z;
6507 FLOAT diff_w;
6508 FLOAT max_abs_diff;
6509 #define NUM_ELEM 4
6510 FLOAT v1[NUM_ELEM];
6511 FLOAT v2[NUM_ELEM];
6512
6513 D3DXFloat16To32Array(v1, v1_float16, NUM_ELEM);
6514 D3DXFloat16To32Array(v2, v2_float16, NUM_ELEM);
6515
6516 diff_x = fabsf(v1[0] - v2[0]);
6517 diff_y = fabsf(v1[1] - v2[1]);
6518 diff_z = fabsf(v1[2] - v2[2]);
6519 diff_w = fabsf(v1[3] - v2[3]);
6520 max_abs_diff = max(diff_x, diff_y);
6521 max_abs_diff = max(diff_z, max_abs_diff);
6522 max_abs_diff = max(diff_w, max_abs_diff);
6523
6524 if (max_abs_diff <= epsilon)
6525 {
6526 memcpy(to, from, NUM_ELEM * sizeof(D3DXFLOAT16));
6527
6528 return TRUE;
6529 }
6530
6531 return FALSE;
6532 #undef NUM_ELEM
6533 }
6534
6535 /* Sets the vertex components to the same value if they are within epsilon. */
6536 static BOOL weld_component(void *to, void *from, D3DDECLTYPE type, FLOAT epsilon)
6537 {
6538 /* Quiet FIXMEs as this is in a loop with potentially thousand of iterations. */
6539 BOOL fixme_once_unused = FALSE;
6540 BOOL fixme_once_unknown = FALSE;
6541
6542 switch (type)
6543 {
6544 case D3DDECLTYPE_FLOAT1:
6545 return weld_float1(to, from, epsilon);
6546
6547 case D3DDECLTYPE_FLOAT2:
6548 return weld_float2(to, from, epsilon);
6549
6550 case D3DDECLTYPE_FLOAT3:
6551 return weld_float3(to, from, epsilon);
6552
6553 case D3DDECLTYPE_FLOAT4:
6554 return weld_float4(to, from, epsilon);
6555
6556 case D3DDECLTYPE_D3DCOLOR:
6557 return weld_d3dcolor(to, from, epsilon);
6558
6559 case D3DDECLTYPE_UBYTE4:
6560 return weld_ubyte4(to, from, epsilon);
6561
6562 case D3DDECLTYPE_SHORT2:
6563 return weld_short2(to, from, epsilon);
6564
6565 case D3DDECLTYPE_SHORT4:
6566 return weld_short4(to, from, epsilon);
6567
6568 case D3DDECLTYPE_UBYTE4N:
6569 return weld_ubyte4n(to, from, epsilon);
6570
6571 case D3DDECLTYPE_SHORT2N:
6572 return weld_short2n(to, from, epsilon);
6573
6574 case D3DDECLTYPE_SHORT4N:
6575 return weld_short4n(to, from, epsilon);
6576
6577 case D3DDECLTYPE_USHORT2N:
6578 return weld_ushort2n(to, from, epsilon);
6579
6580 case D3DDECLTYPE_USHORT4N:
6581 return weld_ushort4n(to, from, epsilon);
6582
6583 case D3DDECLTYPE_UDEC3:
6584 return weld_udec3(to, from, epsilon);
6585
6586 case D3DDECLTYPE_DEC3N:
6587 return weld_dec3n(to, from, epsilon);
6588
6589 case D3DDECLTYPE_FLOAT16_2:
6590 return weld_float16_2(to, from, epsilon);
6591
6592 case D3DDECLTYPE_FLOAT16_4:
6593 return weld_float16_4(to, from, epsilon);
6594
6595 case D3DDECLTYPE_UNUSED:
6596 if (!fixme_once_unused++)
6597 FIXME("D3DDECLTYPE_UNUSED welding not implemented.\n");
6598 break;
6599
6600 default:
6601 if (!fixme_once_unknown++)
6602 FIXME("Welding of unknown declaration type %d is not implemented.\n", type);
6603 break;
6604 }
6605
6606 return FALSE;
6607 }
6608
6609 static FLOAT get_component_epsilon(const D3DVERTEXELEMENT9 *decl_ptr, const D3DXWELDEPSILONS *epsilons)
6610 {
6611 FLOAT epsilon = 0.0f;
6612 /* Quiet FIXMEs as this is in a loop with potentially thousand of iterations. */
6613 static BOOL fixme_once_blendindices = FALSE;
6614 static BOOL fixme_once_positiont = FALSE;
6615 static BOOL fixme_once_fog = FALSE;
6616 static BOOL fixme_once_depth = FALSE;
6617 static BOOL fixme_once_sample = FALSE;
6618 static BOOL fixme_once_unknown = FALSE;
6619
6620 switch (decl_ptr->Usage)
6621 {
6622 case D3DDECLUSAGE_POSITION:
6623 epsilon = epsilons->Position;
6624 break;
6625 case D3DDECLUSAGE_BLENDWEIGHT:
6626 epsilon = epsilons->BlendWeights;
6627 break;
6628 case D3DDECLUSAGE_NORMAL:
6629 epsilon = epsilons->Normals;
6630 break;
6631 case D3DDECLUSAGE_PSIZE:
6632 epsilon = epsilons->PSize;
6633 break;
6634 case D3DDECLUSAGE_TEXCOORD:
6635 {
6636 BYTE usage_index = decl_ptr->UsageIndex;
6637 if (usage_index > 7)
6638 usage_index = 7;
6639 epsilon = epsilons->Texcoords[usage_index];
6640 break;
6641 }
6642 case D3DDECLUSAGE_TANGENT:
6643 epsilon = epsilons->Tangent;
6644 break;
6645 case D3DDECLUSAGE_BINORMAL:
6646 epsilon = epsilons->Binormal;
6647 break;
6648 case D3DDECLUSAGE_TESSFACTOR:
6649 epsilon = epsilons->TessFactor;
6650 break;
6651 case D3DDECLUSAGE_COLOR:
6652 if (decl_ptr->UsageIndex == 0)
6653 epsilon = epsilons->Diffuse;
6654 else if (decl_ptr->UsageIndex == 1)
6655 epsilon = epsilons->Specular;
6656 else
6657 epsilon = 1e-6f;
6658 break;
6659 case D3DDECLUSAGE_BLENDINDICES:
6660 if (!fixme_once_blendindices++)
6661 FIXME("D3DDECLUSAGE_BLENDINDICES welding not implemented.\n");
6662 break;
6663 case D3DDECLUSAGE_POSITIONT:
6664 if (!fixme_once_positiont++)
6665 FIXME("D3DDECLUSAGE_POSITIONT welding not implemented.\n");
6666 break;
6667 case D3DDECLUSAGE_FOG:
6668 if (!fixme_once_fog++)
6669 FIXME("D3DDECLUSAGE_FOG welding not implemented.\n");
6670 break;
6671 case D3DDECLUSAGE_DEPTH:
6672 if (!fixme_once_depth++)
6673 FIXME("D3DDECLUSAGE_DEPTH welding not implemented.\n");
6674 break;
6675 case D3DDECLUSAGE_SAMPLE:
6676 if (!fixme_once_sample++)
6677 FIXME("D3DDECLUSAGE_SAMPLE welding not implemented.\n");
6678 break;
6679 default:
6680 if (!fixme_once_unknown++)
6681 FIXME("Unknown usage %x\n", decl_ptr->Usage);
6682 break;
6683 }
6684
6685 return epsilon;
6686 }
6687
6688 /* Helper function for reading a 32-bit index buffer. */
6689 static inline DWORD read_ib(void *index_buffer, BOOL indices_are_32bit,
6690 DWORD index)
6691 {
6692 if (indices_are_32bit)
6693 {
6694 DWORD *indices = index_buffer;
6695 return indices[index];
6696 }
6697 else
6698 {
6699 WORD *indices = index_buffer;
6700 return indices[index];
6701 }
6702 }
6703
6704 /* Helper function for writing to a 32-bit index buffer. */
6705 static inline void write_ib(void *index_buffer, BOOL indices_are_32bit,
6706 DWORD index, DWORD value)
6707 {
6708 if (indices_are_32bit)
6709 {
6710 DWORD *indices = index_buffer;
6711 indices[index] = value;
6712 }
6713 else
6714 {
6715 WORD *indices = index_buffer;
6716 indices[index] = value;
6717 }
6718 }
6719
6720 /*************************************************************************
6721 * D3DXWeldVertices (D3DX9_36.@)
6722 *
6723 * Welds together similar vertices. The similarity between vert-
6724 * ices can be the position and other components such as
6725 * normal and color.
6726 *
6727 * PARAMS
6728 * mesh [I] Mesh which vertices will be welded together.
6729 * flags [I] D3DXWELDEPSILONSFLAGS specifying how to weld.
6730 * epsilons [I] How similar a component needs to be for welding.
6731 * adjacency [I] Which faces are adjacent to other faces.
6732 * adjacency_out [O] Updated adjacency after welding.
6733 * face_remap_out [O] Which faces the old faces have been mapped to.
6734 * vertex_remap_out [O] Which vertices the old vertices have been mapped to.
6735 *
6736 * RETURNS
6737 * Success: D3D_OK.
6738 * Failure: D3DERR_INVALIDCALL, E_OUTOFMEMORY.
6739 *
6740 * BUGS
6741 * Attribute sorting not implemented.
6742 *
6743 */
6744 HRESULT WINAPI D3DXWeldVertices(ID3DXMesh *mesh, DWORD flags, const D3DXWELDEPSILONS *epsilons,
6745 const DWORD *adjacency, DWORD *adjacency_out, DWORD *face_remap_out, ID3DXBuffer **vertex_remap_out)
6746 {
6747 DWORD *adjacency_generated = NULL;
6748 const DWORD *adjacency_ptr;
6749 DWORD *attributes = NULL;
6750 const FLOAT DEFAULT_EPSILON = 1.0e-6f;
6751 HRESULT hr;
6752 DWORD i;
6753 void *indices = NULL;
6754 BOOL indices_are_32bit = mesh->lpVtbl->GetOptions(mesh) & D3DXMESH_32BIT;
6755 DWORD optimize_flags;
6756 DWORD *point_reps = NULL;
6757 struct d3dx9_mesh *This = impl_from_ID3DXMesh(mesh);
6758 DWORD *vertex_face_map = NULL;
6759 ID3DXBuffer *vertex_remap = NULL;
6760 BYTE *vertices = NULL;
6761
6762 TRACE("mesh %p, flags %#x, epsilons %p, adjacency %p, adjacency_out %p, face_remap_out %p, vertex_remap_out %p.\n",
6763 mesh, flags, epsilons, adjacency, adjacency_out, face_remap_out, vertex_remap_out);
6764
6765 if (flags == 0)
6766 {
6767 WARN("No flags is undefined. Using D3DXWELDEPSILONS_WELDPARTIALMATCHES instead.\n");
6768 flags = D3DXWELDEPSILONS_WELDPARTIALMATCHES;
6769 }
6770
6771 if (adjacency) /* Use supplied adjacency. */
6772 {
6773 adjacency_ptr = adjacency;
6774 }
6775 else /* Adjacency has to be generated. */
6776 {
6777 adjacency_generated = HeapAlloc(GetProcessHeap(), 0, 3 * This->numfaces * sizeof(*adjacency_generated));
6778 if (!adjacency_generated)
6779 {
6780 ERR("Couldn't allocate memory for adjacency_generated.\n");
6781 hr = E_OUTOFMEMORY;
6782 goto cleanup;
6783 }
6784 hr = mesh->lpVtbl->GenerateAdjacency(mesh, DEFAULT_EPSILON, adjacency_generated);
6785 if (FAILED(hr))
6786 {
6787 ERR("Couldn't generate adjacency.\n");
6788 goto cleanup;
6789 }
6790 adjacency_ptr = adjacency_generated;
6791 }
6792
6793 /* Point representation says which vertices can be replaced. */
6794 point_reps = HeapAlloc(GetProcessHeap(), 0, This->numvertices * sizeof(*point_reps));
6795 if (!point_reps)
6796 {
6797 hr = E_OUTOFMEMORY;
6798 ERR("Couldn't allocate memory for point_reps.\n");
6799 goto cleanup;
6800 }
6801 hr = mesh->lpVtbl->ConvertAdjacencyToPointReps(mesh, adjacency_ptr, point_reps);
6802 if (FAILED(hr))
6803 {
6804 ERR("ConvertAdjacencyToPointReps failed.\n");
6805 goto cleanup;
6806 }
6807
6808 hr = mesh->lpVtbl->LockIndexBuffer(mesh, 0, &indices);
6809 if (FAILED(hr))
6810 {
6811 ERR("Couldn't lock index buffer.\n");
6812 goto cleanup;
6813 }
6814
6815 hr = mesh->lpVtbl->LockAttributeBuffer(mesh, 0, &attributes);
6816 if (FAILED(hr))
6817 {
6818 ERR("Couldn't lock attribute buffer.\n");
6819 goto cleanup;
6820 }
6821 vertex_face_map = HeapAlloc(GetProcessHeap(), 0, This->numvertices * sizeof(*vertex_face_map));
6822 if (!vertex_face_map)
6823 {
6824 hr = E_OUTOFMEMORY;
6825 ERR("Couldn't allocate memory for vertex_face_map.\n");
6826 goto cleanup;
6827 }
6828 /* Build vertex face map, so that a vertex's face can be looked up. */
6829 for (i = 0; i < This->numfaces; i++)
6830 {
6831 DWORD j;
6832 for (j = 0; j < 3; j++)
6833 {
6834 DWORD index = read_ib(indices, indices_are_32bit, 3*i + j);
6835 vertex_face_map[index] = i;
6836 }
6837 }
6838
6839 if (flags & D3DXWELDEPSILONS_WELDPARTIALMATCHES)
6840 {
6841 hr = mesh->lpVtbl->LockVertexBuffer(mesh, 0, (void**)&vertices);
6842 if (FAILED(hr))
6843 {
6844 ERR("Couldn't lock vertex buffer.\n");
6845 goto cleanup;
6846 }
6847 /* For each vertex that can be removed, compare its vertex components
6848 * with the vertex components from the vertex that can replace it. A
6849 * vertex is only fully replaced if all the components match and the
6850 * flag D3DXWELDEPSILONS_DONOTREMOVEVERTICES is not set, and they
6851 * belong to the same attribute group. Otherwise the vertex components
6852 * that are within epsilon are set to the same value.
6853 */
6854 for (i = 0; i < 3 * This->numfaces; i++)
6855 {
6856 D3DVERTEXELEMENT9 *decl_ptr;
6857 DWORD vertex_size = mesh->lpVtbl->GetNumBytesPerVertex(mesh);
6858 DWORD num_vertex_components;
6859 INT matches = 0;
6860 BOOL all_match;
6861 DWORD index = read_ib(indices, indices_are_32bit, i);
6862
6863 for (decl_ptr = This->cached_declaration, num_vertex_components = 0; decl_ptr->Stream != 0xFF; decl_ptr++, num_vertex_components++)
6864 {
6865 BYTE *to = &vertices[vertex_size*index + decl_ptr->Offset];
6866 BYTE *from = &vertices[vertex_size*point_reps[index] + decl_ptr->Offset];
6867 FLOAT epsilon = get_component_epsilon(decl_ptr, epsilons);
6868
6869 /* Don't weld self */
6870 if (index == point_reps[index])
6871 {
6872 matches++;
6873 continue;
6874 }
6875
6876 if (weld_component(to, from, decl_ptr->Type, epsilon))
6877 matches++;
6878 }
6879
6880 all_match = (num_vertex_components == matches);
6881 if (all_match && !(flags & D3DXWELDEPSILONS_DONOTREMOVEVERTICES))
6882 {
6883 DWORD to_face = vertex_face_map[index];
6884 DWORD from_face = vertex_face_map[point_reps[index]];
6885 if(attributes[to_face] != attributes[from_face] && !(flags & D3DXWELDEPSILONS_DONOTSPLIT))
6886 continue;
6887 write_ib(indices, indices_are_32bit, i, point_reps[index]);
6888 }
6889 }
6890 mesh->lpVtbl->UnlockVertexBuffer(mesh);
6891 vertices = NULL;
6892 }
6893 else if (flags & D3DXWELDEPSILONS_WELDALL)
6894 {
6895 for (i = 0; i < 3 * This->numfaces; i++)
6896 {
6897 DWORD index = read_ib(indices, indices_are_32bit, i);
6898 DWORD to_face = vertex_face_map[index];
6899 DWORD from_face = vertex_face_map[point_reps[index]];
6900 if(attributes[to_face] != attributes[from_face] && !(flags & D3DXWELDEPSILONS_DONOTSPLIT))
6901 continue;
6902 write_ib(indices, indices_are_32bit, i, point_reps[index]);
6903 }
6904 }
6905 mesh->lpVtbl->UnlockAttributeBuffer(mesh);
6906 attributes = NULL;
6907 mesh->lpVtbl->UnlockIndexBuffer(mesh);
6908 indices = NULL;
6909
6910 /* Compact mesh using OptimizeInplace */
6911 optimize_flags = D3DXMESHOPT_COMPACT;
6912 hr = mesh->lpVtbl->OptimizeInplace(mesh, optimize_flags, adjacency_ptr, adjacency_out, face_remap_out, vertex_remap_out);
6913 if (FAILED(hr))
6914 {
6915 ERR("Couldn't compact mesh.\n");
6916 goto cleanup;
6917 }
6918
6919 hr = D3D_OK;
6920 cleanup:
6921 HeapFree(GetProcessHeap(), 0, adjacency_generated);
6922 HeapFree(GetProcessHeap(), 0, point_reps);
6923 HeapFree(GetProcessHeap(), 0, vertex_face_map);
6924 if (attributes) mesh->lpVtbl->UnlockAttributeBuffer(mesh);
6925 if (indices) mesh->lpVtbl->UnlockIndexBuffer(mesh);
6926 if (vertex_remap) ID3DXBuffer_Release(vertex_remap);
6927 if (vertices) mesh->lpVtbl->UnlockVertexBuffer(mesh);
6928
6929 return hr;
6930 }
6931
6932 /*************************************************************************
6933 * D3DXOptimizeFaces (D3DX9_36.@)
6934 *
6935 * Re-orders the faces so the vertex cache is used optimally.
6936 *
6937 * PARAMS
6938 * indices [I] Pointer to an index buffer belonging to a mesh.
6939 * num_faces [I] Number of faces in the mesh.
6940 * num_vertices [I] Number of vertices in the mesh.
6941 * indices_are_32bit [I] Specifies whether indices are 32- or 16-bit.
6942 * face_remap [I/O] The new order the faces should be drawn in.
6943 *
6944 * RETURNS
6945 * Success: D3D_OK.
6946 * Failure: D3DERR_INVALIDCALL.
6947 *
6948 * BUGS
6949 * The face re-ordering does not use the vertex cache optimally.
6950 *
6951 */
6952 HRESULT WINAPI D3DXOptimizeFaces(const void *indices, UINT num_faces,
6953 UINT num_vertices, BOOL indices_are_32bit, DWORD *face_remap)
6954 {
6955 UINT i;
6956 UINT j = num_faces - 1;
6957 UINT limit_16_bit = 2 << 15; /* According to MSDN */
6958 HRESULT hr = D3D_OK;
6959
6960 FIXME("indices %p, num_faces %u, num_vertices %u, indices_are_32bit %#x, face_remap %p semi-stub. "
6961 "Face order will not be optimal.\n",
6962 indices, num_faces, num_vertices, indices_are_32bit, face_remap);
6963
6964 if (!indices_are_32bit && num_faces >= limit_16_bit)
6965 {
6966 WARN("Number of faces must be less than %d when using 16-bit indices.\n",
6967 limit_16_bit);
6968 hr = D3DERR_INVALIDCALL;
6969 goto error;
6970 }
6971
6972 if (!face_remap)
6973 {
6974 WARN("Face remap pointer is NULL.\n");
6975 hr = D3DERR_INVALIDCALL;
6976 goto error;
6977 }
6978
6979 /* The faces are drawn in reverse order for simple meshes. This ordering
6980 * is not optimal for complicated meshes, but will not break anything
6981 * either. The ordering should be changed to take advantage of the vertex
6982 * cache on the graphics card.
6983 *
6984 * TODO Re-order to take advantage of vertex cache.
6985 */
6986 for (i = 0; i < num_faces; i++)
6987 {
6988 face_remap[i] = j--;
6989 }
6990
6991 return D3D_OK;
6992
6993 error:
6994 return hr;
6995 }