comctl32/listview: Implement overlay image support.
[wine] / dlls / d3drm / meshbuilder.c
1 /*
2  * Implementation of IDirect3DRMMeshBuilderX and IDirect3DRMMesh interfaces
3  *
4  * Copyright 2010, 2012 Christian Costa
5  * Copyright 2011 AndrĂ© Hentschel
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define NONAMELESSUNION
23
24 #define COBJMACROS
25
26 #include "wine/debug.h"
27
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "dxfile.h"
31 #include "rmxfguid.h"
32
33 #include "d3drm_private.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(d3drm);
36
37 typedef struct {
38     unsigned nb_vertices;
39     D3DRMVERTEX* vertices;
40     unsigned nb_faces;
41     unsigned vertex_per_face;
42     DWORD face_data_size;
43     unsigned* face_data;
44     D3DCOLOR color;
45     IDirect3DRMMaterial2* material;
46     IDirect3DRMTexture3* texture;
47 } mesh_group;
48
49 typedef struct {
50     IDirect3DRMMesh IDirect3DRMMesh_iface;
51     LONG ref;
52     DWORD groups_capacity;
53     DWORD nb_groups;
54     mesh_group* groups;
55 } IDirect3DRMMeshImpl;
56
57 typedef struct {
58     D3DVALUE u;
59     D3DVALUE v;
60 } Coords2d;
61
62 typedef struct {
63     D3DCOLOR color;
64     IDirect3DRMMaterial2 *material;
65     IDirect3DRMTexture3 *texture;
66 } mesh_material;
67
68 typedef struct {
69     IDirect3DRMMeshBuilder2 IDirect3DRMMeshBuilder2_iface;
70     IDirect3DRMMeshBuilder3 IDirect3DRMMeshBuilder3_iface;
71     LONG ref;
72     char* name;
73     DWORD nb_vertices;
74     D3DVECTOR* pVertices;
75     DWORD nb_normals;
76     D3DVECTOR* pNormals;
77     DWORD nb_faces;
78     DWORD face_data_size;
79     LPVOID pFaceData;
80     DWORD nb_coords2d;
81     Coords2d *pCoords2d;
82     D3DCOLOR color;
83     IDirect3DRMMaterial2 *material;
84     IDirect3DRMTexture3 *texture;
85     DWORD nb_materials;
86     mesh_material *materials;
87     DWORD *material_indices;
88 } IDirect3DRMMeshBuilderImpl;
89
90 char templates[] = {
91 "xof 0302txt 0064"
92 "template Header"
93 "{"
94 "<3D82AB43-62DA-11CF-AB39-0020AF71E433>"
95 "WORD major;"
96 "WORD minor;"
97 "DWORD flags;"
98 "}"
99 "template Vector"
100 "{"
101 "<3D82AB5E-62DA-11CF-AB39-0020AF71E433>"
102 "FLOAT x;"
103 "FLOAT y;"
104 "FLOAT z;"
105 "}"
106 "template Coords2d"
107 "{"
108 "<F6F23F44-7686-11CF-8F52-0040333594A3>"
109 "FLOAT u;"
110 "FLOAT v;"
111 "}"
112 "template Matrix4x4"
113 "{"
114 "<F6F23F45-7686-11CF-8F52-0040333594A3>"
115 "array FLOAT matrix[16];"
116 "}"
117 "template ColorRGBA"
118 "{"
119 "<35FF44E0-6C7C-11CF-8F52-0040333594A3>"
120 "FLOAT red;"
121 "FLOAT green;"
122 "FLOAT blue;"
123 "FLOAT alpha;"
124 "}"
125 "template ColorRGB"
126 "{"
127 "<D3E16E81-7835-11CF-8F52-0040333594A3>"
128 "FLOAT red;"
129 "FLOAT green;"
130 "FLOAT blue;"
131 "}"
132 "template IndexedColor"
133 "{"
134 "<1630B820-7842-11CF-8F52-0040333594A3>"
135 "DWORD index;"
136 "ColorRGBA indexColor;"
137 "}"
138 "template Boolean"
139 "{"
140 "<537DA6A0-CA37-11D0-941C-0080C80CFA7B>"
141 "DWORD truefalse;"
142 "}"
143 "template Boolean2d"
144 "{"
145 "<4885AE63-78E8-11CF-8F52-0040333594A3>"
146 "Boolean u;"
147 "Boolean v;"
148 "}"
149 "template MaterialWrap"
150 "{"
151 "<4885AE60-78E8-11CF-8F52-0040333594A3>"
152 "Boolean u;"
153 "Boolean v;"
154 "}"
155 "template TextureFilename"
156 "{"
157 "<A42790E1-7810-11CF-8F52-0040333594A3>"
158 "STRING filename;"
159 "}"
160 "template Material"
161 "{"
162 "<3D82AB4D-62DA-11CF-AB39-0020AF71E433>"
163 "ColorRGBA faceColor;"
164 "FLOAT power;"
165 "ColorRGB specularColor;"
166 "ColorRGB emissiveColor;"
167 "[...]"
168 "}"
169 "template MeshFace"
170 "{"
171 "<3D82AB5F-62DA-11CF-AB39-0020AF71E433>"
172 "DWORD nFaceVertexIndices;"
173 "array DWORD faceVertexIndices[nFaceVertexIndices];"
174 "}"
175 "template MeshFaceWraps"
176 "{"
177 "<ED1EC5C0-C0A8-11D0-941C-0080C80CFA7B>"
178 "DWORD nFaceWrapValues;"
179 "array Boolean2d faceWrapValues[nFaceWrapValues];"
180 "}"
181 "template MeshTextureCoords"
182 "{"
183 "<F6F23F40-7686-11CF-8F52-0040333594A3>"
184 "DWORD nTextureCoords;"
185 "array Coords2d textureCoords[nTextureCoords];"
186 "}"
187 "template MeshMaterialList"
188 "{"
189 "<F6F23F42-7686-11CF-8F52-0040333594A3>"
190 "DWORD nMaterials;"
191 "DWORD nFaceIndexes;"
192 "array DWORD faceIndexes[nFaceIndexes];"
193 "[Material]"
194 "}"
195 "template MeshNormals"
196 "{"
197 "<F6F23F43-7686-11CF-8F52-0040333594A3>"
198 "DWORD nNormals;"
199 "array Vector normals[nNormals];"
200 "DWORD nFaceNormals;"
201 "array MeshFace faceNormals[nFaceNormals];"
202 "}"
203 "template MeshVertexColors"
204 "{"
205 "<1630B821-7842-11CF-8F52-0040333594A3>"
206 "DWORD nVertexColors;"
207 "array IndexedColor vertexColors[nVertexColors];"
208 "}"
209 "template Mesh"
210 "{"
211 "<3D82AB44-62DA-11CF-AB39-0020AF71E433>"
212 "DWORD nVertices;"
213 "array Vector vertices[nVertices];"
214 "DWORD nFaces;"
215 "array MeshFace faces[nFaces];"
216 "[...]"
217 "}"
218 "template FrameTransformMatrix"
219 "{"
220 "<F6F23F41-7686-11CF-8F52-0040333594A3>"
221 "Matrix4x4 frameMatrix;"
222 "}"
223 "template Frame"
224 "{"
225 "<3D82AB46-62DA-11CF-AB39-0020AF71E433>"
226 "[...]"
227 "}"
228 "template FloatKeys"
229 "{"
230 "<10DD46A9-775B-11CF-8F52-0040333594A3>"
231 "DWORD nValues;"
232 "array FLOAT values[nValues];"
233 "}"
234 "template TimedFloatKeys"
235 "{"
236 "<F406B180-7B3B-11CF-8F52-0040333594A3>"
237 "DWORD time;"
238 "FloatKeys tfkeys;"
239 "}"
240 "template AnimationKey"
241 "{"
242 "<10DD46A8-775B-11CF-8F52-0040333594A3>"
243 "DWORD keyType;"
244 "DWORD nKeys;"
245 "array TimedFloatKeys keys[nKeys];"
246 "}"
247 "template AnimationOptions"
248 "{"
249 "<E2BF56C0-840F-11CF-8F52-0040333594A3>"
250 "DWORD openclosed;"
251 "DWORD positionquality;"
252 "}"
253 "template Animation"
254 "{"
255 "<3D82AB4F-62DA-11CF-AB39-0020AF71E433>"
256 "[...]"
257 "}"
258 "template AnimationSet"
259 "{"
260 "<3D82AB50-62DA-11CF-AB39-0020AF71E433>"
261 "[Animation]"
262 "}"
263 "template InlineData"
264 "{"
265 "<3A23EEA0-94B1-11D0-AB39-0020AF71E433>"
266 "[BINARY]"
267 "}"
268 "template Url"
269 "{"
270 "<3A23EEA1-94B1-11D0-AB39-0020AF71E433>"
271 "DWORD nUrls;"
272 "array STRING urls[nUrls];"
273 "}"
274 "template ProgressiveMesh"
275 "{"
276 "<8A63C360-997D-11D0-941C-0080C80CFA7B>"
277 "[Url,InlineData]"
278 "}"
279 "template Guid"
280 "{"
281 "<A42790E0-7810-11CF-8F52-0040333594A3>"
282 "DWORD data1;"
283 "WORD data2;"
284 "WORD data3;"
285 "array UCHAR data4[8];"
286 "}"
287 "template StringProperty"
288 "{"
289 "<7F0F21E0-BFE1-11D1-82C0-00A0C9697271>"
290 "STRING key;"
291 "STRING value;"
292 "}"
293 "template PropertyBag"
294 "{"
295 "<7F0F21E1-BFE1-11D1-82C0-00A0C9697271>"
296 "[StringProperty]"
297 "}"
298 "template ExternalVisual"
299 "{"
300 "<98116AA0-BDBA-11D1-82C0-00A0C9697271>"
301 "Guid guidExternalVisual;"
302 "[...]"
303 "}"
304 "template RightHanded"
305 "{"
306 "<7F5D5EA0-D53A-11D1-82C0-00A0C9697271>"
307 "DWORD bRightHanded;"
308 "}"
309 };
310
311 static inline IDirect3DRMMeshImpl *impl_from_IDirect3DRMMesh(IDirect3DRMMesh *iface)
312 {
313     return CONTAINING_RECORD(iface, IDirect3DRMMeshImpl, IDirect3DRMMesh_iface);
314 }
315
316 static inline IDirect3DRMMeshBuilderImpl *impl_from_IDirect3DRMMeshBuilder2(IDirect3DRMMeshBuilder2 *iface)
317 {
318     return CONTAINING_RECORD(iface, IDirect3DRMMeshBuilderImpl, IDirect3DRMMeshBuilder2_iface);
319 }
320
321 static inline IDirect3DRMMeshBuilderImpl *impl_from_IDirect3DRMMeshBuilder3(IDirect3DRMMeshBuilder3 *iface)
322 {
323     return CONTAINING_RECORD(iface, IDirect3DRMMeshBuilderImpl, IDirect3DRMMeshBuilder3_iface);
324 }
325
326 static void clean_mesh_builder_data(IDirect3DRMMeshBuilderImpl *mesh_builder)
327 {
328     DWORD i;
329
330     HeapFree(GetProcessHeap(), 0, mesh_builder->name);
331     mesh_builder->name = NULL;
332     HeapFree(GetProcessHeap(), 0, mesh_builder->pVertices);
333     mesh_builder->pVertices = NULL;
334     mesh_builder->nb_vertices = 0;
335     HeapFree(GetProcessHeap(), 0, mesh_builder->pNormals);
336     mesh_builder->pNormals = NULL;
337     mesh_builder->nb_normals = 0;
338     HeapFree(GetProcessHeap(), 0, mesh_builder->pFaceData);
339     mesh_builder->pFaceData = NULL;
340     mesh_builder->face_data_size = 0;
341     mesh_builder->nb_faces = 0;
342     HeapFree(GetProcessHeap(), 0, mesh_builder->pCoords2d);
343     mesh_builder->pCoords2d = NULL;
344     mesh_builder->nb_coords2d = 0;
345     for (i = 0; i < mesh_builder->nb_materials; i++)
346     {
347         if (mesh_builder->materials[i].material)
348             IDirect3DRMMaterial2_Release(mesh_builder->materials[i].material);
349         if (mesh_builder->materials[i].texture)
350             IDirect3DRMTexture3_Release(mesh_builder->materials[i].texture);
351     }
352     mesh_builder->nb_materials = 0;
353     HeapFree(GetProcessHeap(), 0, mesh_builder->materials);
354     HeapFree(GetProcessHeap(), 0, mesh_builder->material_indices);
355 }
356
357 /*** IUnknown methods ***/
358 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_QueryInterface(IDirect3DRMMeshBuilder2* iface,
359                                                                  REFIID riid, void** ppvObject)
360 {
361     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
362
363     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
364
365     *ppvObject = NULL;
366
367     if(IsEqualGUID(riid, &IID_IUnknown) ||
368        IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder) ||
369        IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder2))
370     {
371         *ppvObject = &This->IDirect3DRMMeshBuilder2_iface;
372     }
373     else if(IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder3))
374     {
375         *ppvObject = &This->IDirect3DRMMeshBuilder3_iface;
376     }
377     else
378     {
379         FIXME("interface %s not implemented\n", debugstr_guid(riid));
380         return E_NOINTERFACE;
381     }
382
383     IDirect3DRMMeshBuilder_AddRef(iface);
384     return S_OK;
385 }
386
387 static ULONG WINAPI IDirect3DRMMeshBuilder2Impl_AddRef(IDirect3DRMMeshBuilder2* iface)
388 {
389     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
390     ULONG ref = InterlockedIncrement(&This->ref);
391
392     TRACE("(%p)->(): new ref = %d\n", This, ref);
393
394     return ref;
395 }
396
397 static ULONG WINAPI IDirect3DRMMeshBuilder2Impl_Release(IDirect3DRMMeshBuilder2* iface)
398 {
399     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
400     ULONG ref = InterlockedDecrement(&This->ref);
401
402     TRACE("(%p)->(): new ref = %d\n", This, ref);
403
404     if (!ref)
405     {
406         clean_mesh_builder_data(This);
407         if (This->material)
408             IDirect3DRMMaterial2_Release(This->material);
409         if (This->texture)
410             IDirect3DRMTexture3_Release(This->texture);
411         HeapFree(GetProcessHeap(), 0, This);
412     }
413
414     return ref;
415 }
416
417 /*** IDirect3DRMObject methods ***/
418 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_Clone(IDirect3DRMMeshBuilder2* iface,
419                                                         LPUNKNOWN pUnkOuter, REFIID riid,
420                                                         LPVOID *ppvObj)
421 {
422     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
423
424     FIXME("(%p)->(%p,%s,%p): stub\n", This, pUnkOuter, debugstr_guid(riid), ppvObj);
425
426     return E_NOTIMPL;
427 }
428
429 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_AddDestroyCallback(IDirect3DRMMeshBuilder2* iface,
430                                                                      D3DRMOBJECTCALLBACK cb,
431                                                                      LPVOID argument)
432 {
433     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
434
435     FIXME("(%p)->(%p,%p): stub\n", This, cb, argument);
436
437     return E_NOTIMPL;
438 }
439
440 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_DeleteDestroyCallback(IDirect3DRMMeshBuilder2* iface,
441                                                                         D3DRMOBJECTCALLBACK cb,
442                                                                         LPVOID argument)
443 {
444     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
445
446     FIXME("(%p)->(%p,%p): stub\n", This, cb, argument);
447
448     return E_NOTIMPL;
449 }
450
451 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetAppData(IDirect3DRMMeshBuilder2* iface,
452                                                              DWORD data)
453 {
454     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
455
456     FIXME("(%p)->(%u): stub\n", This, data);
457
458     return E_NOTIMPL;
459 }
460
461 static DWORD WINAPI IDirect3DRMMeshBuilder2Impl_GetAppData(IDirect3DRMMeshBuilder2* iface)
462 {
463     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
464
465     FIXME("(%p)->(): stub\n", This);
466
467     return 0;
468 }
469
470 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetName(IDirect3DRMMeshBuilder2* iface,
471                                                           LPCSTR name)
472 {
473     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
474
475     TRACE("(%p)->(%s)\n", This, debugstr_a(name));
476
477     return IDirect3DRMMeshBuilder3_SetName(&This->IDirect3DRMMeshBuilder3_iface, name);
478 }
479
480 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GetName(IDirect3DRMMeshBuilder2* iface,
481                                                           LPDWORD size, LPSTR name)
482 {
483     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
484
485     TRACE("(%p)->(%p,%p)\n", This, size, name);
486
487     return IDirect3DRMMeshBuilder3_GetName(&This->IDirect3DRMMeshBuilder3_iface, size, name);
488 }
489
490 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GetClassName(IDirect3DRMMeshBuilder2* iface,
491                                                                LPDWORD size, LPSTR name)
492 {
493     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
494
495     TRACE("(%p)->(%p, %p)\n", This, size, name);
496
497     return IDirect3DRMMeshBuilder3_GetClassName(&This->IDirect3DRMMeshBuilder3_iface, size, name);
498 }
499
500 /*** IDirect3DRMMeshBuilder2 methods ***/
501 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_Load(IDirect3DRMMeshBuilder2* iface,
502                                                        LPVOID filename, LPVOID name,
503                                                        D3DRMLOADOPTIONS loadflags,
504                                                        D3DRMLOADTEXTURECALLBACK cb, LPVOID arg)
505 {
506     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
507
508     TRACE("(%p)->(%p,%p,%x,%p,%p)\n", This, filename, name, loadflags, cb, arg);
509
510     if (cb)
511         FIXME("Texture callback is not yet supported\n");
512
513     return IDirect3DRMMeshBuilder3_Load(&This->IDirect3DRMMeshBuilder3_iface, filename, name,
514                                         loadflags, NULL, arg);
515 }
516
517 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_Save(IDirect3DRMMeshBuilder2* iface,
518                                                        const char *filename, D3DRMXOFFORMAT format,
519                                                        D3DRMSAVEOPTIONS save)
520 {
521     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
522
523     FIXME("(%p)->(%s,%d,%d): stub\n", This, filename, format, save);
524
525     return E_NOTIMPL;
526 }
527
528 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_Scale(IDirect3DRMMeshBuilder2* iface,
529                                                         D3DVALUE sx, D3DVALUE sy, D3DVALUE sz)
530 {
531     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
532
533     TRACE("(%p)->(%f,%f,%f)\n", This, sx, sy, sz);
534
535     return IDirect3DRMMeshBuilder3_Scale(&This->IDirect3DRMMeshBuilder3_iface, sx, sy, sz);
536 }
537
538 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_Translate(IDirect3DRMMeshBuilder2* iface,
539                                                             D3DVALUE tx, D3DVALUE ty, D3DVALUE tz)
540 {
541     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
542
543     FIXME("(%p)->(%f,%f,%f): stub\n", This, tx, ty, tz);
544
545     return E_NOTIMPL;
546 }
547
548 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetColorSource(IDirect3DRMMeshBuilder2* iface,
549                                                                  D3DRMCOLORSOURCE color)
550 {
551     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
552
553     FIXME("(%p)->(%x): stub\n", This, color);
554
555     return E_NOTIMPL;
556 }
557
558 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GetBox(IDirect3DRMMeshBuilder2* iface,
559                                                          D3DRMBOX *pBox)
560 {
561     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
562
563     FIXME("(%p)->(%p): stub\n", This, pBox);
564
565     return E_NOTIMPL;
566 }
567
568 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GenerateNormals(IDirect3DRMMeshBuilder2* iface)
569 {
570     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
571
572     FIXME("(%p)->(): stub\n", This);
573
574     return E_NOTIMPL;
575 }
576
577 static D3DRMCOLORSOURCE WINAPI IDirect3DRMMeshBuilder2Impl_GetColorSource(IDirect3DRMMeshBuilder2* iface)
578 {
579     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
580
581     FIXME("(%p)->(): stub\n", This);
582
583     return E_NOTIMPL;
584 }
585
586 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_AddMesh(IDirect3DRMMeshBuilder2* iface,
587                                                           LPDIRECT3DRMMESH pMesh)
588 {
589     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
590
591     FIXME("(%p)->(%p): stub\n", This, pMesh);
592
593     return E_NOTIMPL;
594 }
595
596 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_AddMeshBuilder(IDirect3DRMMeshBuilder2* iface,
597                                                                  LPDIRECT3DRMMESHBUILDER pMeshBuilder)
598 {
599     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
600
601     FIXME("(%p)->(%p): stub\n", This, pMeshBuilder);
602
603     return E_NOTIMPL;
604 }
605
606 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_AddFrame(IDirect3DRMMeshBuilder2* iface,
607                                                            LPDIRECT3DRMFRAME pFrame)
608 {
609     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
610
611     FIXME("(%p)->(%p): stub\n", This, pFrame);
612
613     return E_NOTIMPL;
614 }
615
616 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_AddFace(IDirect3DRMMeshBuilder2* iface,
617                                                           LPDIRECT3DRMFACE pFace)
618 {
619     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
620
621     FIXME("(%p)->(%p): stub\n", This, pFace);
622
623     return E_NOTIMPL;
624 }
625
626 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_AddFaces(IDirect3DRMMeshBuilder2* iface,
627                                                            DWORD vcount, D3DVECTOR *vertices,
628                                                            DWORD ncount, D3DVECTOR *normals,
629                                                            DWORD *data,
630                                                            LPDIRECT3DRMFACEARRAY* pFaceArray)
631 {
632     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
633
634     FIXME("(%p)->(%d,%p,%d,%p,%p,%p): stub\n", This, vcount, vertices, ncount, normals, data, pFaceArray);
635
636     return E_NOTIMPL;
637 }
638
639 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_ReserveSpace(IDirect3DRMMeshBuilder2* iface,
640                                                                DWORD vertex_Count,
641                                                                DWORD normal_count,
642                                                                DWORD face_count)
643 {
644     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
645
646     FIXME("(%p)->(%d,%d,%d): stub\n", This, vertex_Count, normal_count, face_count);
647
648     return E_NOTIMPL;
649 }
650
651 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetColorRGB(IDirect3DRMMeshBuilder2* iface,
652                                                               D3DVALUE red, D3DVALUE green,
653                                                               D3DVALUE blue)
654 {
655     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
656
657     TRACE("(%p)->(%f,%f,%f)\n", This, red, green, blue);
658
659     return IDirect3DRMMeshBuilder3_SetColorRGB(&This->IDirect3DRMMeshBuilder3_iface, red, green, blue);
660 }
661
662 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetColor(IDirect3DRMMeshBuilder2* iface,
663                                                            D3DCOLOR color)
664 {
665     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
666
667     TRACE("(%p)->(%x)\n", This, color);
668
669     return IDirect3DRMMeshBuilder3_SetColor(&This->IDirect3DRMMeshBuilder3_iface, color);
670 }
671
672 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetTexture(IDirect3DRMMeshBuilder2* iface,
673                                                              LPDIRECT3DRMTEXTURE texture)
674 {
675     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
676     LPDIRECT3DRMTEXTURE3 texture3 = NULL;
677     HRESULT hr = D3DRM_OK;
678
679     if (texture)
680         hr = IDirect3DRMTexture_QueryInterface(texture, &IID_IDirect3DRMTexture3, (LPVOID*)&texture3);
681     if (SUCCEEDED(hr))
682         hr = IDirect3DRMMeshBuilder3_SetTexture(&This->IDirect3DRMMeshBuilder3_iface, texture3);
683     if (texture3)
684         IDirect3DRMTexture3_Release(texture3);
685
686     return hr;
687 }
688
689 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetMaterial(IDirect3DRMMeshBuilder2* iface,
690                                                               LPDIRECT3DRMMATERIAL material)
691 {
692     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
693
694     TRACE("(%p)->(%p)\n", This, material);
695
696     return IDirect3DRMMeshBuilder3_SetMaterial(&This->IDirect3DRMMeshBuilder3_iface, (LPDIRECT3DRMMATERIAL2)material);
697 }
698
699 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetTextureTopology(IDirect3DRMMeshBuilder2* iface,
700                                                                      BOOL wrap_u, BOOL wrap_v)
701 {
702     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
703
704     FIXME("(%p)->(%d,%d): stub\n", This, wrap_u, wrap_v);
705
706     return E_NOTIMPL;
707 }
708
709 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetQuality(IDirect3DRMMeshBuilder2* iface,
710                                                              D3DRMRENDERQUALITY quality)
711 {
712     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
713
714     FIXME("(%p)->(%d): stub\n", This, quality);
715
716     return E_NOTIMPL;
717 }
718
719 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetPerspective(IDirect3DRMMeshBuilder2* iface,
720                                                                  BOOL enable)
721 {
722     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
723
724     FIXME("(%p)->(%d): stub\n", This, enable);
725
726     return E_NOTIMPL;
727 }
728
729 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetVertex(IDirect3DRMMeshBuilder2* iface,
730                                                             DWORD index,
731                                                             D3DVALUE x, D3DVALUE y, D3DVALUE z)
732 {
733     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
734
735     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
736
737     return E_NOTIMPL;
738 }
739
740 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetNormal(IDirect3DRMMeshBuilder2* iface,
741                                                             DWORD index,
742                                                             D3DVALUE x, D3DVALUE y, D3DVALUE z)
743 {
744     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
745
746     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
747
748     return E_NOTIMPL;
749 }
750
751 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetTextureCoordinates(IDirect3DRMMeshBuilder2* iface,
752                                                                         DWORD index,
753                                                                         D3DVALUE u, D3DVALUE v)
754 {
755     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
756
757     TRACE("(%p)->(%u,%f,%f)\n", This, index, u, v);
758
759     return IDirect3DRMMeshBuilder3_SetTextureCoordinates(&This->IDirect3DRMMeshBuilder3_iface,
760                                                          index, u, v);
761 }
762
763 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetVertexColor(IDirect3DRMMeshBuilder2* iface,
764                                                                  DWORD index, D3DCOLOR color)
765 {
766     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
767
768     FIXME("(%p)->(%d,%x): stub\n", This, index, color);
769
770     return E_NOTIMPL;
771 }
772
773 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_SetVertexColorRGB(IDirect3DRMMeshBuilder2* iface,
774                                                                     DWORD index, D3DVALUE red,
775                                                                     D3DVALUE green, D3DVALUE blue)
776 {
777     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
778
779     FIXME("(%p)->(%d,%f,%f,%f): stub\n", This, index, red, green, blue);
780
781     return E_NOTIMPL;
782 }
783
784 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GetFaces(IDirect3DRMMeshBuilder2* iface,
785                                                            LPDIRECT3DRMFACEARRAY* pFaceArray)
786 {
787     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
788
789     FIXME("(%p)->(%p): stub\n", This, pFaceArray);
790
791     return E_NOTIMPL;
792 }
793
794 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GetVertices(IDirect3DRMMeshBuilder2* iface,
795                                                               DWORD *vcount, D3DVECTOR *vertices,
796                                                               DWORD *ncount, D3DVECTOR *normals,
797                                                               DWORD *face_data_size,
798                                                               DWORD *face_data)
799 {
800     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
801
802     TRACE("(%p)->(%p,%p,%p,%p,%p,%p)\n", This, vcount, vertices, ncount, normals, face_data_size, face_data);
803
804     if (vertices && (!vcount || (*vcount < This->nb_vertices)))
805         return D3DRMERR_BADVALUE;
806     if (vcount)
807         *vcount = This->nb_vertices;
808     if (vertices && This->nb_vertices)
809         memcpy(vertices, This->pVertices, This->nb_vertices * sizeof(D3DVECTOR));
810
811     if (normals && (!ncount || (*ncount < This->nb_normals)))
812         return D3DRMERR_BADVALUE;
813     if (ncount)
814         *ncount = This->nb_normals;
815     if (normals && This->nb_normals)
816         memcpy(normals, This->pNormals, This->nb_normals * sizeof(D3DVECTOR));
817
818     if (face_data && (!face_data_size || (*face_data_size < This->face_data_size)))
819         return D3DRMERR_BADVALUE;
820     if (face_data_size)
821         *face_data_size = This->face_data_size;
822     if (face_data && This->face_data_size)
823         memcpy(face_data, This->pFaceData, This->face_data_size * sizeof(DWORD));
824
825     return D3DRM_OK;
826 }
827
828 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GetTextureCoordinates(IDirect3DRMMeshBuilder2* iface,
829                                                                         DWORD index,
830                                                                         D3DVALUE *u, D3DVALUE *v)
831 {
832     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
833
834     TRACE("(%p)->(%u,%p,%p)\n", This, index, u, v);
835
836     return IDirect3DRMMeshBuilder3_GetTextureCoordinates(&This->IDirect3DRMMeshBuilder3_iface,
837                                                          index, u, v);
838 }
839
840 static int WINAPI IDirect3DRMMeshBuilder2Impl_AddVertex(IDirect3DRMMeshBuilder2* iface,
841                                                         D3DVALUE x, D3DVALUE y, D3DVALUE z)
842 {
843     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
844
845     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
846
847     return 0;
848 }
849
850 static int WINAPI IDirect3DRMMeshBuilder2Impl_AddNormal(IDirect3DRMMeshBuilder2* iface,
851                                                         D3DVALUE x, D3DVALUE y, D3DVALUE z)
852 {
853     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
854
855     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
856
857     return 0;
858 }
859
860 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_CreateFace(IDirect3DRMMeshBuilder2* iface,
861                                                              LPDIRECT3DRMFACE* ppFace)
862 {
863     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
864
865     FIXME("(%p)->(%p): stub\n", This, ppFace);
866
867     return E_NOTIMPL;
868 }
869
870 static D3DRMRENDERQUALITY WINAPI IDirect3DRMMeshBuilder2Impl_GetQuality(IDirect3DRMMeshBuilder2* iface)
871 {
872     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
873
874     FIXME("(%p)->(): stub\n", This);
875
876     return 0;
877 }
878
879 static BOOL WINAPI IDirect3DRMMeshBuilder2Impl_GetPerspective(IDirect3DRMMeshBuilder2* iface)
880 {
881     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
882
883     FIXME("(%p)->(): stub\n", This);
884
885     return FALSE;
886 }
887
888 static int WINAPI IDirect3DRMMeshBuilder2Impl_GetFaceCount(IDirect3DRMMeshBuilder2* iface)
889 {
890     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
891
892     TRACE("(%p)->()\n", This);
893
894     return This->nb_faces;
895 }
896
897 static int WINAPI IDirect3DRMMeshBuilder2Impl_GetVertexCount(IDirect3DRMMeshBuilder2* iface)
898 {
899     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
900
901     TRACE("(%p)->()\n", This);
902
903     return This->nb_vertices;
904 }
905
906 static D3DCOLOR WINAPI IDirect3DRMMeshBuilder2Impl_GetVertexColor(IDirect3DRMMeshBuilder2* iface,
907                                                                   DWORD index)
908 {
909     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
910
911     FIXME("(%p)->(%d): stub\n", This, index);
912
913     return 0;
914 }
915
916 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_CreateMesh(IDirect3DRMMeshBuilder2* iface,
917                                                              LPDIRECT3DRMMESH* mesh)
918 {
919     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
920
921     TRACE("(%p)->(%p)\n", This, mesh);
922
923     return IDirect3DRMMeshBuilder3_CreateMesh(&This->IDirect3DRMMeshBuilder3_iface, mesh);
924 }
925
926 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GenerateNormals2(IDirect3DRMMeshBuilder2* iface,
927                                                                    D3DVALUE crease, DWORD dwFlags)
928 {
929     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
930
931     FIXME("(%p)->(%f,%u): stub\n", This, crease, dwFlags);
932
933     return E_NOTIMPL;
934 }
935
936 static HRESULT WINAPI IDirect3DRMMeshBuilder2Impl_GetFace(IDirect3DRMMeshBuilder2* iface,
937                                                           DWORD index, LPDIRECT3DRMFACE* ppFace)
938 {
939     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder2(iface);
940
941     FIXME("(%p)->(%u,%p): stub\n", This, index, ppFace);
942
943     return E_NOTIMPL;
944 }
945
946 static const struct IDirect3DRMMeshBuilder2Vtbl Direct3DRMMeshBuilder2_Vtbl =
947 {
948     /*** IUnknown methods ***/
949     IDirect3DRMMeshBuilder2Impl_QueryInterface,
950     IDirect3DRMMeshBuilder2Impl_AddRef,
951     IDirect3DRMMeshBuilder2Impl_Release,
952     /*** IDirect3DRMObject methods ***/
953     IDirect3DRMMeshBuilder2Impl_Clone,
954     IDirect3DRMMeshBuilder2Impl_AddDestroyCallback,
955     IDirect3DRMMeshBuilder2Impl_DeleteDestroyCallback,
956     IDirect3DRMMeshBuilder2Impl_SetAppData,
957     IDirect3DRMMeshBuilder2Impl_GetAppData,
958     IDirect3DRMMeshBuilder2Impl_SetName,
959     IDirect3DRMMeshBuilder2Impl_GetName,
960     IDirect3DRMMeshBuilder2Impl_GetClassName,
961     /*** IDirect3DRMMeshBuilder methods ***/
962     IDirect3DRMMeshBuilder2Impl_Load,
963     IDirect3DRMMeshBuilder2Impl_Save,
964     IDirect3DRMMeshBuilder2Impl_Scale,
965     IDirect3DRMMeshBuilder2Impl_Translate,
966     IDirect3DRMMeshBuilder2Impl_SetColorSource,
967     IDirect3DRMMeshBuilder2Impl_GetBox,
968     IDirect3DRMMeshBuilder2Impl_GenerateNormals,
969     IDirect3DRMMeshBuilder2Impl_GetColorSource,
970     IDirect3DRMMeshBuilder2Impl_AddMesh,
971     IDirect3DRMMeshBuilder2Impl_AddMeshBuilder,
972     IDirect3DRMMeshBuilder2Impl_AddFrame,
973     IDirect3DRMMeshBuilder2Impl_AddFace,
974     IDirect3DRMMeshBuilder2Impl_AddFaces,
975     IDirect3DRMMeshBuilder2Impl_ReserveSpace,
976     IDirect3DRMMeshBuilder2Impl_SetColorRGB,
977     IDirect3DRMMeshBuilder2Impl_SetColor,
978     IDirect3DRMMeshBuilder2Impl_SetTexture,
979     IDirect3DRMMeshBuilder2Impl_SetMaterial,
980     IDirect3DRMMeshBuilder2Impl_SetTextureTopology,
981     IDirect3DRMMeshBuilder2Impl_SetQuality,
982     IDirect3DRMMeshBuilder2Impl_SetPerspective,
983     IDirect3DRMMeshBuilder2Impl_SetVertex,
984     IDirect3DRMMeshBuilder2Impl_SetNormal,
985     IDirect3DRMMeshBuilder2Impl_SetTextureCoordinates,
986     IDirect3DRMMeshBuilder2Impl_SetVertexColor,
987     IDirect3DRMMeshBuilder2Impl_SetVertexColorRGB,
988     IDirect3DRMMeshBuilder2Impl_GetFaces,
989     IDirect3DRMMeshBuilder2Impl_GetVertices,
990     IDirect3DRMMeshBuilder2Impl_GetTextureCoordinates,
991     IDirect3DRMMeshBuilder2Impl_AddVertex,
992     IDirect3DRMMeshBuilder2Impl_AddNormal,
993     IDirect3DRMMeshBuilder2Impl_CreateFace,
994     IDirect3DRMMeshBuilder2Impl_GetQuality,
995     IDirect3DRMMeshBuilder2Impl_GetPerspective,
996     IDirect3DRMMeshBuilder2Impl_GetFaceCount,
997     IDirect3DRMMeshBuilder2Impl_GetVertexCount,
998     IDirect3DRMMeshBuilder2Impl_GetVertexColor,
999     IDirect3DRMMeshBuilder2Impl_CreateMesh,
1000     /*** IDirect3DRMMeshBuilder2 methods ***/
1001     IDirect3DRMMeshBuilder2Impl_GenerateNormals2,
1002     IDirect3DRMMeshBuilder2Impl_GetFace
1003 };
1004
1005
1006 /*** IUnknown methods ***/
1007 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_QueryInterface(IDirect3DRMMeshBuilder3* iface,
1008                                                                  REFIID riid, void** ppvObject)
1009 {
1010     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1011     return IDirect3DRMMeshBuilder2_QueryInterface(&This->IDirect3DRMMeshBuilder2_iface, riid, ppvObject);
1012 }
1013
1014 static ULONG WINAPI IDirect3DRMMeshBuilder3Impl_AddRef(IDirect3DRMMeshBuilder3* iface)
1015 {
1016     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1017     return IDirect3DRMMeshBuilder2_AddRef(&This->IDirect3DRMMeshBuilder2_iface);
1018 }
1019
1020 static ULONG WINAPI IDirect3DRMMeshBuilder3Impl_Release(IDirect3DRMMeshBuilder3* iface)
1021 {
1022     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1023     return IDirect3DRMMeshBuilder2_Release(&This->IDirect3DRMMeshBuilder2_iface);
1024 }
1025
1026 /*** IDirect3DRMObject methods ***/
1027 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Clone(IDirect3DRMMeshBuilder3* iface,
1028                                                         LPUNKNOWN pUnkOuter, REFIID riid,
1029                                                         LPVOID *ppvObj)
1030 {
1031     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1032
1033     FIXME("(%p)->(%p,%s,%p): stub\n", This, pUnkOuter, debugstr_guid(riid), ppvObj);
1034
1035     return E_NOTIMPL;
1036 }
1037
1038 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddDestroyCallback(IDirect3DRMMeshBuilder3* iface,
1039                                                                      D3DRMOBJECTCALLBACK cb,
1040                                                                      LPVOID argument)
1041 {
1042     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1043
1044     FIXME("(%p)->(%p,%p): stub\n", This, cb, argument);
1045
1046     return E_NOTIMPL;
1047 }
1048
1049 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteDestroyCallback(IDirect3DRMMeshBuilder3* iface,
1050                                                                         D3DRMOBJECTCALLBACK cb,
1051                                                                         LPVOID argument)
1052 {
1053     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1054
1055     FIXME("(%p)->(%p,%p): stub\n", This, cb, argument);
1056
1057     return E_NOTIMPL;
1058 }
1059
1060 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetAppData(IDirect3DRMMeshBuilder3* iface,
1061                                                              DWORD data)
1062 {
1063     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1064
1065     FIXME("(%p)->(%u): stub\n", This, data);
1066
1067     return E_NOTIMPL;
1068 }
1069
1070 static DWORD WINAPI IDirect3DRMMeshBuilder3Impl_GetAppData(IDirect3DRMMeshBuilder3* iface)
1071 {
1072     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1073
1074     FIXME("(%p)->(): stub\n", This);
1075
1076     return 0;
1077 }
1078
1079 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetName(IDirect3DRMMeshBuilder3* iface,
1080                                                           LPCSTR name)
1081 {
1082     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1083     char *string = NULL;
1084
1085     TRACE("(%p)->(%s)\n", This, debugstr_a(name));
1086
1087     if (name)
1088     {
1089         string = HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1);
1090         if (!string) return E_OUTOFMEMORY;
1091         strcpy(string, name);
1092     }
1093     HeapFree(GetProcessHeap(), 0, This->name);
1094     This->name = string;
1095
1096     return D3DRM_OK;
1097 }
1098
1099 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetName(IDirect3DRMMeshBuilder3* iface,
1100                                                           LPDWORD size, LPSTR name)
1101 {
1102     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1103
1104     TRACE("(%p)->(%p,%p)\n", This, size, name);
1105
1106     if (!size)
1107         return E_POINTER;
1108
1109     if (!This->name)
1110     {
1111         *size = 0;
1112         return D3DRM_OK;
1113     }
1114
1115     if (*size < (strlen(This->name) + 1))
1116         return E_INVALIDARG;
1117
1118     strcpy(name, This->name);
1119     *size = strlen(This->name) + 1;
1120
1121     return D3DRM_OK;
1122 }
1123
1124 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetClassName(IDirect3DRMMeshBuilder3* iface,
1125                                                                LPDWORD size, LPSTR name)
1126 {
1127     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1128
1129     TRACE("(%p)->(%p, %p)\n", This, size, name);
1130
1131     if (!size || *size < strlen("Builder") || !name)
1132         return E_INVALIDARG;
1133
1134     strcpy(name, "Builder");
1135     *size = sizeof("Builder");
1136
1137     return D3DRM_OK;
1138 }
1139
1140 HRESULT load_mesh_data(IDirect3DRMMeshBuilder3 *iface, IDirectXFileData *pData, D3DRMLOADTEXTURECALLBACK load_texture_proc, void *arg)
1141 {
1142     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1143     IDirectXFileData *pData2 = NULL;
1144     const GUID* guid;
1145     DWORD size;
1146     BYTE *ptr;
1147     HRESULT hr;
1148     HRESULT ret = D3DRMERR_BADOBJECT;
1149     DWORD* faces_vertex_idx_data = NULL;
1150     DWORD* faces_vertex_idx_ptr;
1151     DWORD faces_vertex_idx_size;
1152     DWORD* faces_normal_idx_data = NULL;
1153     DWORD* faces_normal_idx_ptr = NULL;
1154     DWORD* faces_data_ptr;
1155     DWORD faces_data_size = 0;
1156     DWORD i;
1157
1158     TRACE("(%p)->(%p)\n", This, pData);
1159
1160     hr = IDirectXFileData_GetName(pData, NULL, &size);
1161     if (hr != DXFILE_OK)
1162         return hr;
1163     if (size)
1164     {
1165         This->name = HeapAlloc(GetProcessHeap(), 0, size);
1166         if (!This->name)
1167             return E_OUTOFMEMORY;
1168
1169         hr = IDirectXFileData_GetName(pData, This->name, &size);
1170         if (hr != DXFILE_OK)
1171             return hr;
1172     }
1173
1174     TRACE("Mesh name is '%s'\n", This->name ? This->name : "");
1175
1176     This->nb_normals = 0;
1177
1178     hr = IDirectXFileData_GetData(pData, NULL, &size, (void**)&ptr);
1179     if (hr != DXFILE_OK)
1180         goto end;
1181
1182     This->nb_vertices = *(DWORD*)ptr;
1183     This->nb_faces = *(DWORD*)(ptr + sizeof(DWORD) + This->nb_vertices * sizeof(D3DVECTOR));
1184     faces_vertex_idx_size = size - sizeof(DWORD) - This->nb_vertices * sizeof(D3DVECTOR) - sizeof(DWORD);
1185     faces_vertex_idx_ptr = (DWORD*)(ptr + sizeof(DWORD) + This->nb_vertices * sizeof(D3DVECTOR) + sizeof(DWORD));
1186
1187     TRACE("Mesh: nb_vertices = %d, nb_faces = %d, faces_vertex_idx_size = %d\n", This->nb_vertices, This->nb_faces, faces_vertex_idx_size);
1188
1189     This->pVertices = HeapAlloc(GetProcessHeap(), 0, This->nb_vertices * sizeof(D3DVECTOR));
1190     memcpy(This->pVertices, ptr + sizeof(DWORD), This->nb_vertices * sizeof(D3DVECTOR));
1191
1192     faces_vertex_idx_ptr = faces_vertex_idx_data = HeapAlloc(GetProcessHeap(), 0, faces_vertex_idx_size);
1193     memcpy(faces_vertex_idx_data, ptr + sizeof(DWORD) + This->nb_vertices * sizeof(D3DVECTOR) + sizeof(DWORD), faces_vertex_idx_size);
1194
1195     /* Each vertex index will have its normal index counterpart so just allocate twice the size */
1196     This->pFaceData = HeapAlloc(GetProcessHeap(), 0, faces_vertex_idx_size * 2);
1197     faces_data_ptr = (DWORD*)This->pFaceData;
1198
1199     while (1)
1200     {
1201         IDirectXFileObject *object;
1202
1203         hr =  IDirectXFileData_GetNextObject(pData, &object);
1204         if (hr == DXFILEERR_NOMOREOBJECTS)
1205         {
1206             TRACE("No more object\n");
1207             break;
1208         }
1209         if (hr != DXFILE_OK)
1210            goto end;
1211
1212         hr = IDirectXFileObject_QueryInterface(object, &IID_IDirectXFileData, (void**)&pData2);
1213         IDirectXFileObject_Release(object);
1214         if (hr != DXFILE_OK)
1215             goto end;
1216
1217         hr = IDirectXFileData_GetType(pData2, &guid);
1218         if (hr != DXFILE_OK)
1219             goto end;
1220
1221         TRACE("Found object type whose GUID = %s\n", debugstr_guid(guid));
1222
1223         if (IsEqualGUID(guid, &TID_D3DRMMeshNormals))
1224         {
1225             DWORD nb_faces_normals;
1226             DWORD faces_normal_idx_size;
1227
1228             hr = IDirectXFileData_GetData(pData2, NULL, &size, (void**)&ptr);
1229             if (hr != DXFILE_OK)
1230                 goto end;
1231
1232             This->nb_normals = *(DWORD*)ptr;
1233             nb_faces_normals = *(DWORD*)(ptr + sizeof(DWORD) + This->nb_normals * sizeof(D3DVECTOR));
1234
1235             TRACE("MeshNormals: nb_normals = %d, nb_faces_normals = %d\n", This->nb_normals, nb_faces_normals);
1236             if (nb_faces_normals != This->nb_faces)
1237                 WARN("nb_face_normals (%d) != nb_faces (%d)\n", nb_faces_normals, This->nb_normals);
1238
1239             This->pNormals = HeapAlloc(GetProcessHeap(), 0, This->nb_normals * sizeof(D3DVECTOR));
1240             memcpy(This->pNormals, ptr + sizeof(DWORD), This->nb_normals * sizeof(D3DVECTOR));
1241
1242             faces_normal_idx_size = size - (2 * sizeof(DWORD) + This->nb_normals * sizeof(D3DVECTOR));
1243             faces_normal_idx_ptr = faces_normal_idx_data = HeapAlloc(GetProcessHeap(), 0, faces_normal_idx_size);
1244             memcpy(faces_normal_idx_data, ptr + sizeof(DWORD) + This->nb_normals * sizeof(D3DVECTOR) + sizeof(DWORD), faces_normal_idx_size);
1245         }
1246         else if (IsEqualGUID(guid, &TID_D3DRMMeshTextureCoords))
1247         {
1248             hr = IDirectXFileData_GetData(pData2, NULL, &size, (void**)&ptr);
1249             if (hr != DXFILE_OK)
1250                 goto end;
1251
1252             This->nb_coords2d = *(DWORD*)ptr;
1253
1254             TRACE("MeshTextureCoords: nb_coords2d = %d\n", This->nb_coords2d);
1255
1256             This->pCoords2d = HeapAlloc(GetProcessHeap(), 0, This->nb_coords2d * sizeof(Coords2d));
1257             memcpy(This->pCoords2d, ptr + sizeof(DWORD), This->nb_coords2d * sizeof(Coords2d));
1258
1259         }
1260         else if (IsEqualGUID(guid, &TID_D3DRMMeshMaterialList))
1261         {
1262             DWORD nb_materials;
1263             DWORD nb_face_indices;
1264             DWORD data_size;
1265             IDirectXFileObject *child;
1266             DWORD i = 0;
1267             float* values;
1268
1269             TRACE("Process MeshMaterialList\n");
1270
1271             hr = IDirectXFileData_GetData(pData2, NULL, &size, (void**)&ptr);
1272             if (hr != DXFILE_OK)
1273                 goto end;
1274
1275             nb_materials = *(DWORD*)ptr;
1276             nb_face_indices = *(DWORD*)(ptr + sizeof(DWORD));
1277             data_size = 2 * sizeof(DWORD) + nb_face_indices * sizeof(DWORD);
1278
1279             TRACE("nMaterials = %u, nFaceIndexes = %u\n", nb_materials, nb_face_indices);
1280
1281             if (size != data_size)
1282                 WARN("Returned size %u does not match expected one %u\n", size, data_size);
1283
1284             This->material_indices = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->material_indices) * nb_face_indices);
1285             if (!This->material_indices)
1286                 goto end;
1287             memcpy(This->material_indices, ptr + 2 * sizeof(DWORD), sizeof(*This->material_indices) * nb_face_indices),
1288
1289             This->materials = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->materials) * nb_materials);
1290             if (!This->materials)
1291             {
1292                 HeapFree(GetProcessHeap(), 0, This->material_indices);
1293                 goto end;
1294             }
1295             This->nb_materials = nb_materials;
1296
1297             while (SUCCEEDED(hr = IDirectXFileData_GetNextObject(pData2, &child)) && (i < nb_materials))
1298             {
1299                 IDirectXFileData *data;
1300                 IDirectXFileDataReference *reference;
1301                 IDirectXFileObject *material_child;
1302
1303                 hr = IDirectXFileObject_QueryInterface(child, &IID_IDirectXFileData, (void **)&data);
1304                 if (FAILED(hr))
1305                 {
1306                     hr = IDirectXFileObject_QueryInterface(child, &IID_IDirectXFileDataReference, (void **)&reference);
1307                     IDirectXFileObject_Release(child);
1308                     if (FAILED(hr))
1309                         goto end;
1310
1311                     hr = IDirectXFileDataReference_Resolve(reference, &data);
1312                     IDirectXFileDataReference_Release(reference);
1313                     if (FAILED(hr))
1314                         goto end;
1315                 }
1316                 else
1317                 {
1318                     IDirectXFileObject_Release(child);
1319                 }
1320
1321                 hr = Direct3DRMMaterial_create(&This->materials[i].material);
1322                 if (FAILED(hr))
1323                 {
1324                     IDirectXFileData_Release(data);
1325                     goto end;
1326                 }
1327
1328                 hr = IDirectXFileData_GetData(data, NULL, &size, (void**)&ptr);
1329                 if (hr != DXFILE_OK)
1330                 {
1331                     IDirectXFileData_Release(data);
1332                     goto end;
1333                 }
1334
1335                 if (size != 44)
1336                     WARN("Material size %u does not match expected one %u\n", size, 44);
1337
1338                 values = (float*)ptr;
1339
1340                 This->materials[i].color = D3DCOLOR_ARGB((BYTE)(values[3] * 255.0f), (BYTE)(values[0] * 255.0f),
1341                                                          (BYTE)(values[1] * 255.0f), (BYTE)(values[2] * 255.0f));
1342
1343                 IDirect3DRMMaterial2_SetAmbient(This->materials[i].material, values[0], values [1], values[2]); /* Alpha ignored */
1344                 IDirect3DRMMaterial2_SetPower(This->materials[i].material, values[4]);
1345                 IDirect3DRMMaterial2_SetSpecular(This->materials[i].material, values[5], values[6], values[7]);
1346                 IDirect3DRMMaterial2_SetEmissive(This->materials[i].material, values[8], values[9], values[10]);
1347
1348                 This->materials[i].texture = NULL;
1349
1350                 hr = IDirectXFileData_GetNextObject(data, &material_child);
1351                 if (hr == S_OK)
1352                 {
1353                     IDirectXFileData *data;
1354                     char **filename;
1355
1356                     hr = IDirectXFileObject_QueryInterface(material_child, &IID_IDirectXFileData, (void **)&data);
1357                     if (FAILED(hr))
1358                     {
1359                         IDirectXFileDataReference *reference;
1360
1361                         hr = IDirectXFileObject_QueryInterface(material_child, &IID_IDirectXFileDataReference, (void **)&reference);
1362                         if (FAILED(hr))
1363                             goto end;
1364
1365                         hr = IDirectXFileDataReference_Resolve(reference, &data);
1366                         IDirectXFileDataReference_Release(reference);
1367                         if (FAILED(hr))
1368                             goto end;
1369                     }
1370
1371                     hr = IDirectXFileData_GetType(data, &guid);
1372                     if (hr != DXFILE_OK)
1373                         goto end;
1374                     if (!IsEqualGUID(guid, &TID_D3DRMTextureFilename))
1375                     {
1376                          WARN("Not a texture filename\n");
1377                          goto end;
1378                     }
1379
1380                     size = 4;
1381                     hr = IDirectXFileData_GetData(data, NULL, &size, (void**)&filename);
1382                     if (SUCCEEDED(hr))
1383                     {
1384                         if (load_texture_proc)
1385                         {
1386                             IDirect3DRMTexture *texture;
1387
1388                             hr = load_texture_proc(*filename, arg, &texture);
1389                             if (SUCCEEDED(hr))
1390                             {
1391                                 hr = IDirect3DTexture_QueryInterface(texture, &IID_IDirect3DRMTexture3, (void**)&This->materials[i].texture);
1392                                 IDirect3DTexture_Release(texture);
1393                             }
1394                         }
1395                         else
1396                         {
1397                             HANDLE file;
1398
1399                             /* If the texture file is not found, no texture is associated with the material */
1400                             file = CreateFileA(*filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1401                             if (file != INVALID_HANDLE_VALUE)
1402                             {
1403                                 CloseHandle(file);
1404
1405                                 hr = Direct3DRMTexture_create(&IID_IDirect3DRMTexture3, (IUnknown**)&This->materials[i].texture);
1406                                 if (FAILED(hr))
1407                                 {
1408                                     IDirectXFileData_Release(data);
1409                                     goto end;
1410                                 }
1411                             }
1412                         }
1413                     }
1414                 }
1415                 else if (hr != DXFILEERR_NOMOREOBJECTS)
1416                 {
1417                     goto end;
1418                 }
1419                 hr = S_OK;
1420
1421                 IDirectXFileData_Release(data);
1422                 i++;
1423             }
1424             if (hr == S_OK)
1425             {
1426                 IDirectXFileObject_Release(child);
1427                 WARN("Found more sub-objects than expected\n");
1428             }
1429             else if (hr != DXFILEERR_NOMOREOBJECTS)
1430             {
1431                 goto end;
1432             }
1433             hr = S_OK;
1434         }
1435         else
1436         {
1437             FIXME("Unknown GUID %s, ignoring...\n", debugstr_guid(guid));
1438         }
1439
1440         IDirectXFileData_Release(pData2);
1441         pData2 = NULL;
1442     }
1443
1444     if (!This->nb_normals)
1445     {
1446         /* Allocate normals, one per vertex */
1447         This->pNormals = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nb_vertices * sizeof(D3DVECTOR));
1448         if (!This->pNormals)
1449             goto end;
1450     }
1451
1452     for (i = 0; i < This->nb_faces; i++)
1453     {
1454         DWORD j;
1455         DWORD nb_face_indexes;
1456         D3DVECTOR face_normal;
1457
1458         if (faces_vertex_idx_size < sizeof(DWORD))
1459             WARN("Not enough data to read number of indices of face %d\n", i);
1460
1461         nb_face_indexes  = *(faces_data_ptr + faces_data_size++) = *(faces_vertex_idx_ptr++);
1462         faces_vertex_idx_size--;
1463         if (faces_normal_idx_data && (*(faces_normal_idx_ptr++) != nb_face_indexes))
1464             WARN("Faces indices number mismatch\n");
1465
1466         if (faces_vertex_idx_size < (nb_face_indexes * sizeof(DWORD)))
1467             WARN("Not enough data to read all indices of face %d\n", i);
1468
1469         if (!This->nb_normals)
1470         {
1471             /* Compute face normal */
1472             if (nb_face_indexes > 2)
1473             {
1474                 D3DVECTOR a, b;
1475
1476                 D3DRMVectorSubtract(&a, &This->pVertices[faces_vertex_idx_ptr[2]], &This->pVertices[faces_vertex_idx_ptr[1]]);
1477                 D3DRMVectorSubtract(&b, &This->pVertices[faces_vertex_idx_ptr[0]], &This->pVertices[faces_vertex_idx_ptr[1]]);
1478                 D3DRMVectorCrossProduct(&face_normal, &a, &b);
1479                 D3DRMVectorNormalize(&face_normal);
1480             }
1481             else
1482             {
1483                 face_normal.u1.x = 0.0f;
1484                 face_normal.u2.y = 0.0f;
1485                 face_normal.u3.z = 0.0f;
1486             }
1487         }
1488
1489         for (j = 0; j < nb_face_indexes; j++)
1490         {
1491             /* Copy vertex index */
1492             *(faces_data_ptr + faces_data_size++) = *faces_vertex_idx_ptr;
1493             /* Copy normal index */
1494             if (This->nb_normals)
1495             {
1496                 /* Read from x file */
1497                 *(faces_data_ptr + faces_data_size++) = *(faces_normal_idx_ptr++);
1498             }
1499             else
1500             {
1501                 DWORD vertex_idx = *faces_vertex_idx_ptr;
1502                 if (vertex_idx >= This->nb_vertices)
1503                 {
1504                     WARN("Found vertex index %u but only %u vertices available => use index 0\n", vertex_idx, This->nb_vertices);
1505                     vertex_idx = 0;
1506                 }
1507                 *(faces_data_ptr + faces_data_size++) = vertex_idx;
1508                 /* Add face normal to vertex normal */
1509                 D3DRMVectorAdd(&This->pNormals[vertex_idx], &This->pNormals[vertex_idx], &face_normal);
1510             }
1511             faces_vertex_idx_ptr++;
1512         }
1513         faces_vertex_idx_size -= nb_face_indexes;
1514     }
1515
1516     /* Last DWORD must be 0 */
1517     *(faces_data_ptr + faces_data_size++) = 0;
1518
1519     /* Set size (in number of DWORD) of all faces data */
1520     This->face_data_size = faces_data_size;
1521
1522     if (!This->nb_normals)
1523     {
1524         /* Normalize all normals */
1525         for (i = 0; i < This->nb_vertices; i++)
1526         {
1527             D3DRMVectorNormalize(&This->pNormals[i]);
1528         }
1529         This->nb_normals = This->nb_vertices;
1530     }
1531
1532     /* If there is no texture coordinates, generate default texture coordinates (0.0f, 0.0f) for each vertex */
1533     if (!This->pCoords2d)
1534     {
1535         This->nb_coords2d = This->nb_vertices;
1536         This->pCoords2d = HeapAlloc(GetProcessHeap(), 0, This->nb_coords2d * sizeof(Coords2d));
1537         for (i = 0; i < This->nb_coords2d; i++)
1538         {
1539             This->pCoords2d[i].u = 0.0f;
1540             This->pCoords2d[i].v = 0.0f;
1541         }
1542     }
1543
1544     TRACE("Mesh data loaded successfully\n");
1545
1546     ret = D3DRM_OK;
1547
1548 end:
1549
1550     HeapFree(GetProcessHeap(), 0, faces_normal_idx_data);
1551     HeapFree(GetProcessHeap(), 0, faces_vertex_idx_data);
1552
1553     return ret;
1554 }
1555
1556 /*** IDirect3DRMMeshBuilder3 methods ***/
1557 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Load(IDirect3DRMMeshBuilder3* iface,
1558                                                        void *filename, void *name,
1559                                                        D3DRMLOADOPTIONS loadflags,
1560                                                        D3DRMLOADTEXTURE3CALLBACK cb, void *arg)
1561 {
1562     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1563     DXFILELOADOPTIONS load_options;
1564     IDirectXFile *dxfile = NULL;
1565     IDirectXFileEnumObject *enum_object = NULL;
1566     IDirectXFileData *data = NULL;
1567     const GUID* guid;
1568     DWORD size;
1569     Header* header;
1570     HRESULT hr;
1571     HRESULT ret = D3DRMERR_BADOBJECT;
1572
1573     TRACE("(%p)->(%p,%p,%x,%p,%p)\n", This, filename, name, loadflags, cb, arg);
1574
1575     clean_mesh_builder_data(This);
1576
1577     if (loadflags == D3DRMLOAD_FROMMEMORY)
1578     {
1579         load_options = DXFILELOAD_FROMMEMORY;
1580     }
1581     else if (loadflags == D3DRMLOAD_FROMFILE)
1582     {
1583         load_options = DXFILELOAD_FROMFILE;
1584         TRACE("Loading from file %s\n", debugstr_a(filename));
1585     }
1586     else
1587     {
1588         FIXME("Load options %d not supported yet\n", loadflags);
1589         return E_NOTIMPL;
1590     }
1591
1592     hr = DirectXFileCreate(&dxfile);
1593     if (hr != DXFILE_OK)
1594         goto end;
1595
1596     hr = IDirectXFile_RegisterTemplates(dxfile, templates, strlen(templates));
1597     if (hr != DXFILE_OK)
1598         goto end;
1599
1600     hr = IDirectXFile_CreateEnumObject(dxfile, filename, load_options, &enum_object);
1601     if (hr != DXFILE_OK)
1602         goto end;
1603
1604     hr = IDirectXFileEnumObject_GetNextDataObject(enum_object, &data);
1605     if (hr != DXFILE_OK)
1606         goto end;
1607
1608     hr = IDirectXFileData_GetType(data, &guid);
1609     if (hr != DXFILE_OK)
1610         goto end;
1611
1612     TRACE("Found object type whose GUID = %s\n", debugstr_guid(guid));
1613
1614     if (!IsEqualGUID(guid, &TID_DXFILEHeader))
1615     {
1616         ret = D3DRMERR_BADFILE;
1617         goto end;
1618     }
1619
1620     hr = IDirectXFileData_GetData(data, NULL, &size, (void**)&header);
1621     if ((hr != DXFILE_OK) || (size != sizeof(Header)))
1622         goto end;
1623
1624     TRACE("Version is %d %d %d\n", header->major, header->minor, header->flags);
1625
1626     /* Version must be 1.0.x */
1627     if ((header->major != 1) || (header->minor != 0))
1628     {
1629         ret = D3DRMERR_BADFILE;
1630         goto end;
1631     }
1632
1633     IDirectXFileData_Release(data);
1634     data = NULL;
1635
1636     hr = IDirectXFileEnumObject_GetNextDataObject(enum_object, &data);
1637     if (hr != DXFILE_OK)
1638     {
1639         ret = D3DRMERR_NOTFOUND;
1640         goto end;
1641     }
1642
1643     hr = IDirectXFileData_GetType(data, &guid);
1644     if (hr != DXFILE_OK)
1645         goto end;
1646
1647     TRACE("Found object type whose GUID = %s\n", debugstr_guid(guid));
1648
1649     if (!IsEqualGUID(guid, &TID_D3DRMMesh))
1650     {
1651         ret = D3DRMERR_NOTFOUND;
1652         goto end;
1653     }
1654
1655     /* We don't care about the texture interface version since we rely on QueryInterface */
1656     hr = load_mesh_data(iface, data, (D3DRMLOADTEXTURECALLBACK)cb, arg);
1657     if (hr == S_OK)
1658         ret = D3DRM_OK;
1659
1660 end:
1661
1662     if (data)
1663         IDirectXFileData_Release(data);
1664     if (enum_object)
1665         IDirectXFileEnumObject_Release(enum_object);
1666     if (dxfile)
1667         IDirectXFile_Release(dxfile);
1668
1669     if (ret != D3DRM_OK)
1670         clean_mesh_builder_data(This);
1671
1672     return ret;
1673 }
1674
1675 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Save(IDirect3DRMMeshBuilder3* iface,
1676                                                        const char* filename, D3DRMXOFFORMAT format,
1677                                                        D3DRMSAVEOPTIONS save)
1678 {
1679     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1680
1681     FIXME("(%p)->(%s,%d,%d): stub\n", This, filename, format, save);
1682
1683     return E_NOTIMPL;
1684 }
1685
1686 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Scale(IDirect3DRMMeshBuilder3* iface,
1687                                                         D3DVALUE sx, D3DVALUE sy, D3DVALUE sz)
1688 {
1689     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1690     DWORD i;
1691
1692     TRACE("(%p)->(%f,%f,%f)\n", This, sx, sy, sz);
1693
1694     for (i = 0; i < This->nb_vertices; i++)
1695     {
1696         This->pVertices[i].u1.x *= sx;
1697         This->pVertices[i].u2.y *= sy;
1698         This->pVertices[i].u3.z *= sz;
1699     }
1700
1701     /* Normals are not affected by Scale */
1702
1703     return D3DRM_OK;
1704 }
1705
1706 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Translate(IDirect3DRMMeshBuilder3* iface,
1707                                                             D3DVALUE tx, D3DVALUE ty, D3DVALUE tz)
1708 {
1709     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1710
1711     FIXME("(%p)->(%f,%f,%f): stub\n", This, tx, ty, tz);
1712
1713     return E_NOTIMPL;
1714 }
1715
1716 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetColorSource(IDirect3DRMMeshBuilder3* iface,
1717                                                                  D3DRMCOLORSOURCE color)
1718 {
1719     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1720
1721     FIXME("(%p)->(%x): stub\n", This, color);
1722
1723     return E_NOTIMPL;
1724 }
1725
1726 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetBox(IDirect3DRMMeshBuilder3* iface,
1727                                                          D3DRMBOX* box)
1728 {
1729     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1730
1731     FIXME("(%p)->(%p): stub\n", This, box);
1732
1733     return E_NOTIMPL;
1734 }
1735
1736 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GenerateNormals(IDirect3DRMMeshBuilder3* iface,
1737                                                                   D3DVALUE crease, DWORD flags)
1738 {
1739     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1740
1741     FIXME("(%p)->(%f,%u): stub\n", This, crease, flags);
1742
1743     return E_NOTIMPL;
1744 }
1745
1746 static D3DRMCOLORSOURCE WINAPI IDirect3DRMMeshBuilder3Impl_GetColorSource(IDirect3DRMMeshBuilder3* iface)
1747 {
1748     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1749
1750     FIXME("(%p)->(): stub\n", This);
1751
1752     return E_NOTIMPL;
1753 }
1754
1755 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddMesh(IDirect3DRMMeshBuilder3* iface,
1756                                                           LPDIRECT3DRMMESH Mesh)
1757 {
1758     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1759
1760     FIXME("(%p)->(%p): stub\n", This, Mesh);
1761
1762     return E_NOTIMPL;
1763 }
1764
1765 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddMeshBuilder(IDirect3DRMMeshBuilder3* iface,
1766                                                                  LPDIRECT3DRMMESHBUILDER3 MeshBuilder,
1767                                                                  DWORD flags)
1768 {
1769     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1770
1771     FIXME("(%p)->(%p,%u): stub\n", This, MeshBuilder, flags);
1772
1773     return E_NOTIMPL;
1774 }
1775
1776 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddFrame(IDirect3DRMMeshBuilder3* iface,
1777                                                            LPDIRECT3DRMFRAME3 Frame)
1778 {
1779     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1780
1781     FIXME("(%p)->(%p): stub\n", This, Frame);
1782
1783     return E_NOTIMPL;
1784 }
1785
1786 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddFace(IDirect3DRMMeshBuilder3* iface,
1787                                                           LPDIRECT3DRMFACE2 Face)
1788 {
1789     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1790
1791     FIXME("(%p)->(%p): stub\n", This, Face);
1792
1793     return E_NOTIMPL;
1794 }
1795
1796 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddFaces(IDirect3DRMMeshBuilder3* iface,
1797                                                            DWORD vcount, D3DVECTOR* vertices,
1798                                                            DWORD ncount, D3DVECTOR* normals,
1799                                                            DWORD* data,
1800                                                            LPDIRECT3DRMFACEARRAY* FaceArray)
1801 {
1802     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1803
1804     FIXME("(%p)->(%d,%p,%d,%p,%p,%p): stub\n", This, vcount, vertices, ncount,
1805           normals, data, FaceArray);
1806
1807     return E_NOTIMPL;
1808 }
1809
1810 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_ReserveSpace(IDirect3DRMMeshBuilder3* iface,
1811                                                                DWORD vertex_Count,
1812                                                                DWORD normal_count,
1813                                                                DWORD face_count)
1814 {
1815     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1816
1817     FIXME("(%p)->(%d,%d,%d): stub\n", This, vertex_Count, normal_count, face_count);
1818
1819     return E_NOTIMPL;
1820 }
1821
1822 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetColorRGB(IDirect3DRMMeshBuilder3* iface,
1823                                                               D3DVALUE red, D3DVALUE green,
1824                                                               D3DVALUE blue)
1825 {
1826     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1827
1828     TRACE("(%p)->(%f,%f,%f)\n", This, red, green, blue);
1829
1830     This->color = D3DCOLOR_ARGB(0xff, (BYTE)(red   * 255.0f),
1831                                       (BYTE)(green * 255.0f),
1832                                       (BYTE)(blue  * 255.0f));
1833
1834     return D3DRM_OK;
1835 }
1836
1837 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetColor(IDirect3DRMMeshBuilder3* iface,
1838                                                            D3DCOLOR color)
1839 {
1840     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1841
1842     TRACE("(%p)->(%x)\n", This, color);
1843
1844     This->color = color;
1845
1846     return D3DRM_OK;
1847 }
1848
1849 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetTexture(IDirect3DRMMeshBuilder3* iface,
1850                                                              LPDIRECT3DRMTEXTURE3 texture)
1851 {
1852     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1853
1854     TRACE("(%p)->(%p)\n", This, texture);
1855
1856     if (texture)
1857         IDirect3DRMTexture3_AddRef(texture);
1858     if (This->texture)
1859         IDirect3DRMTexture3_Release(This->texture);
1860     This->texture = texture;
1861
1862     return D3DRM_OK;
1863 }
1864
1865 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetMaterial(IDirect3DRMMeshBuilder3* iface,
1866                                                               LPDIRECT3DRMMATERIAL2 material)
1867 {
1868     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1869
1870     TRACE("(%p)->(%p)\n", This, material);
1871
1872     if (material)
1873         IDirect3DRMTexture2_AddRef(material);
1874     if (This->material)
1875         IDirect3DRMTexture2_Release(This->material);
1876     This->material = material;
1877
1878     return D3DRM_OK;
1879 }
1880
1881 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetTextureTopology(IDirect3DRMMeshBuilder3* iface,
1882                                                                      BOOL wrap_u, BOOL wrap_v)
1883 {
1884     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1885
1886     FIXME("(%p)->(%d,%d): stub\n", This, wrap_u, wrap_v);
1887
1888     return E_NOTIMPL;
1889 }
1890
1891 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetQuality(IDirect3DRMMeshBuilder3* iface,
1892                                                              D3DRMRENDERQUALITY quality)
1893 {
1894     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1895
1896     FIXME("(%p)->(%d): stub\n", This, quality);
1897
1898     return E_NOTIMPL;
1899 }
1900
1901 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetPerspective(IDirect3DRMMeshBuilder3* iface,
1902                                                                  BOOL enable)
1903 {
1904     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1905
1906     FIXME("(%p)->(%d): stub\n", This, enable);
1907
1908     return E_NOTIMPL;
1909 }
1910
1911 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetVertex(IDirect3DRMMeshBuilder3* iface,
1912                                                             DWORD index,
1913                                                             D3DVALUE x, D3DVALUE y, D3DVALUE z)
1914 {
1915     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1916
1917     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
1918
1919     return E_NOTIMPL;
1920 }
1921
1922 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetNormal(IDirect3DRMMeshBuilder3* iface,
1923                                                             DWORD index,
1924                                                             D3DVALUE x, D3DVALUE y, D3DVALUE z)
1925 {
1926     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1927
1928     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
1929
1930     return E_NOTIMPL;
1931 }
1932
1933 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetTextureCoordinates(IDirect3DRMMeshBuilder3* iface,
1934                                                                         DWORD index, D3DVALUE u,
1935                                                                         D3DVALUE v)
1936 {
1937     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1938
1939     TRACE("(%p)->(%u,%f,%f)\n", This, index, u, v);
1940
1941     if (index >= This->nb_coords2d)
1942         return D3DRMERR_BADVALUE;
1943
1944     This->pCoords2d[index].u = u;
1945     This->pCoords2d[index].v = v;
1946
1947     return D3DRM_OK;
1948 }
1949
1950 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetVertexColor(IDirect3DRMMeshBuilder3* iface,
1951                                                                  DWORD index, D3DCOLOR color)
1952 {
1953     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1954
1955     FIXME("(%p)->(%d,%x): stub\n", This, index, color);
1956
1957     return E_NOTIMPL;
1958 }
1959
1960 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetVertexColorRGB(IDirect3DRMMeshBuilder3* iface,
1961                                                                     DWORD index,
1962                                                                     D3DVALUE red, D3DVALUE green,
1963                                                                     D3DVALUE blue)
1964 {
1965     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1966
1967     FIXME("(%p)->(%d,%f,%f,%f): stub\n", This, index, red, green, blue);
1968
1969     return E_NOTIMPL;
1970 }
1971
1972 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetFaces(IDirect3DRMMeshBuilder3* iface,
1973                                                            LPDIRECT3DRMFACEARRAY* FaceArray)
1974 {
1975     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1976
1977     FIXME("(%p)->(%p): stub\n", This, FaceArray);
1978
1979     return E_NOTIMPL;
1980 }
1981
1982 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetGeometry(IDirect3DRMMeshBuilder3* iface,
1983                                                               DWORD* vcount, D3DVECTOR* vertices,
1984                                                               DWORD* ncount, D3DVECTOR* normals,
1985                                                               DWORD* face_data_size,
1986                                                               DWORD* face_data)
1987 {
1988     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1989
1990     FIXME("(%p)->(%p,%p,%p,%p,%p,%p): stub\n", This, vcount, vertices, ncount, normals,
1991           face_data_size, face_data);
1992
1993     return E_NOTIMPL;
1994 }
1995
1996 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetTextureCoordinates(IDirect3DRMMeshBuilder3* iface,
1997                                                                         DWORD index, D3DVALUE* u,
1998                                                                         D3DVALUE* v)
1999 {
2000     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2001
2002     TRACE("(%p)->(%d,%p,%p)\n", This, index, u, v);
2003
2004     if (index >= This->nb_coords2d)
2005         return D3DRMERR_BADVALUE;
2006
2007     *u = This->pCoords2d[index].u;
2008     *v = This->pCoords2d[index].v;
2009
2010     return D3DRM_OK;
2011 }
2012
2013
2014 static int WINAPI IDirect3DRMMeshBuilder3Impl_AddVertex(IDirect3DRMMeshBuilder3* iface,
2015                                                         D3DVALUE x, D3DVALUE y, D3DVALUE z)
2016 {
2017     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2018
2019     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
2020
2021     return 0;
2022 }
2023
2024 static int WINAPI IDirect3DRMMeshBuilder3Impl_AddNormal(IDirect3DRMMeshBuilder3* iface,
2025                                                         D3DVALUE x, D3DVALUE y, D3DVALUE z)
2026 {
2027     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2028
2029     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
2030
2031     return 0;
2032 }
2033
2034 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_CreateFace(IDirect3DRMMeshBuilder3* iface,
2035                                                              LPDIRECT3DRMFACE2* Face)
2036 {
2037     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2038
2039     FIXME("(%p)->(%p): stub\n", This, Face);
2040
2041     return E_NOTIMPL;
2042 }
2043
2044 static D3DRMRENDERQUALITY WINAPI IDirect3DRMMeshBuilder3Impl_GetQuality(IDirect3DRMMeshBuilder3* iface)
2045 {
2046     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2047
2048     FIXME("(%p)->(): stub\n", This);
2049
2050     return 0;
2051 }
2052
2053 static BOOL WINAPI IDirect3DRMMeshBuilder3Impl_GetPerspective(IDirect3DRMMeshBuilder3* iface)
2054 {
2055     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2056
2057     FIXME("(%p)->(): stub\n", This);
2058
2059     return FALSE;
2060 }
2061
2062 static int WINAPI IDirect3DRMMeshBuilder3Impl_GetFaceCount(IDirect3DRMMeshBuilder3* iface)
2063 {
2064     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2065
2066     TRACE("(%p)->()\n", This);
2067
2068     return This->nb_faces;
2069 }
2070
2071 static int WINAPI IDirect3DRMMeshBuilder3Impl_GetVertexCount(IDirect3DRMMeshBuilder3* iface)
2072 {
2073     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2074
2075     TRACE("(%p)->()\n", This);
2076
2077     return This->nb_vertices;
2078 }
2079
2080 static D3DCOLOR WINAPI IDirect3DRMMeshBuilder3Impl_GetVertexColor(IDirect3DRMMeshBuilder3* iface,
2081                                                                   DWORD index)
2082 {
2083     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2084
2085     FIXME("(%p)->(%d): stub\n", This, index);
2086
2087     return 0;
2088 }
2089
2090 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_CreateMesh(IDirect3DRMMeshBuilder3* iface,
2091                                                              LPDIRECT3DRMMESH* mesh)
2092 {
2093     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2094     HRESULT hr;
2095     D3DRMGROUPINDEX group;
2096
2097     TRACE("(%p)->(%p)\n", This, mesh);
2098
2099     if (!mesh)
2100         return E_POINTER;
2101
2102     hr = Direct3DRMMesh_create(mesh);
2103     if (FAILED(hr))
2104         return hr;
2105
2106     /* If there is mesh data, create a group and put data inside */
2107     if (This->nb_vertices)
2108     {
2109         DWORD i, j;
2110         int k;
2111         D3DRMVERTEX* vertices;
2112
2113         vertices = HeapAlloc(GetProcessHeap(), 0, This->nb_vertices * sizeof(D3DRMVERTEX));
2114         if (!vertices)
2115         {
2116             IDirect3DRMMesh_Release(*mesh);
2117             return E_OUTOFMEMORY;
2118         }
2119         for (i = 0; i < This->nb_vertices; i++)
2120             vertices[i].position = This->pVertices[i];
2121         hr = IDirect3DRMMesh_SetVertices(*mesh, 0, 0, This->nb_vertices, vertices);
2122         HeapFree(GetProcessHeap(), 0, vertices);
2123
2124         /* Groups are in reverse order compared to materials list in X file */
2125         for (k = This->nb_materials - 1; k >= 0; k--)
2126         {
2127             unsigned* face_data;
2128             unsigned* out_ptr;
2129             DWORD* in_ptr = This->pFaceData;
2130             ULONG vertex_per_face = 0;
2131             BOOL* used_vertices;
2132             unsigned nb_vertices = 0;
2133             unsigned nb_faces = 0;
2134
2135             used_vertices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->face_data_size * sizeof(*used_vertices));
2136             if (!used_vertices)
2137             {
2138                 IDirect3DRMMesh_Release(*mesh);
2139                 return E_OUTOFMEMORY;
2140             }
2141
2142             face_data = HeapAlloc(GetProcessHeap(), 0, This->face_data_size * sizeof(*face_data));
2143             if (!face_data)
2144             {
2145                 HeapFree(GetProcessHeap(), 0, used_vertices);
2146                 IDirect3DRMMesh_Release(*mesh);
2147                 return E_OUTOFMEMORY;
2148             }
2149             out_ptr = face_data;
2150
2151             /* If all faces have the same number of vertex, set vertex_per_face */
2152             for (i = 0; i < This->nb_faces; i++)
2153             {
2154                 /* Process only faces belonging to the group */
2155                 if (This->material_indices[i] == k)
2156                 {
2157                     if (vertex_per_face && (vertex_per_face != *in_ptr))
2158                         break;
2159                     vertex_per_face = *in_ptr;
2160                 }
2161                 in_ptr += 1 + *in_ptr * 2;
2162             }
2163             if (i != This->nb_faces)
2164                 vertex_per_face = 0;
2165
2166             /* Put only vertex indices */
2167             in_ptr = This->pFaceData;
2168             for (i = 0; i < This->nb_faces; i++)
2169             {
2170                 DWORD nb_indices = *in_ptr++;
2171
2172                 /* Skip faces not belonging to the group */
2173                 if (This->material_indices[i] != k)
2174                 {
2175                     in_ptr += 2 * nb_indices;
2176                     continue;
2177                 }
2178
2179                 /* Don't put nb indices when vertex_per_face is set */
2180                 if (vertex_per_face)
2181                     *out_ptr++ = nb_indices;
2182
2183                 for (j = 0; j < nb_indices; j++)
2184                 {
2185                     *out_ptr = *in_ptr++;
2186                     used_vertices[*out_ptr++] = TRUE;
2187                     /* Skip normal index */
2188                     in_ptr++;
2189                 }
2190
2191                 nb_faces++;
2192             }
2193
2194             for (i = 0; i < This->nb_vertices; i++)
2195                 if (used_vertices[i])
2196                     nb_vertices++;
2197
2198             hr = IDirect3DRMMesh_AddGroup(*mesh, nb_vertices, nb_faces, vertex_per_face, face_data, &group);
2199             HeapFree(GetProcessHeap(), 0, used_vertices);
2200             HeapFree(GetProcessHeap(), 0, face_data);
2201             if (SUCCEEDED(hr))
2202                 hr = IDirect3DRMMesh_SetGroupColor(*mesh, group, This->materials[k].color);
2203             if (SUCCEEDED(hr))
2204                 hr = IDirect3DRMMesh_SetGroupMaterial(*mesh, group, (LPDIRECT3DRMMATERIAL)This->materials[k].material);
2205             if (SUCCEEDED(hr) && This->materials[k].texture)
2206             {
2207                 LPDIRECT3DRMTEXTURE texture;
2208
2209                 IDirect3DRMTexture3_QueryInterface(This->materials[k].texture, &IID_IDirect3DRMTexture, (LPVOID*)&texture);
2210                 hr = IDirect3DRMMesh_SetGroupTexture(*mesh, group, texture);
2211                 IDirect3DRMTexture_Release(texture);
2212             }
2213             if (FAILED(hr))
2214             {
2215                 IDirect3DRMMesh_Release(*mesh);
2216                 return hr;
2217             }
2218         }
2219     }
2220
2221     return D3DRM_OK;
2222 }
2223
2224 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetFace(IDirect3DRMMeshBuilder3* iface,
2225                                                           DWORD index, LPDIRECT3DRMFACE2* Face)
2226 {
2227     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2228
2229     FIXME("(%p)->(%u,%p): stub\n", This, index, Face);
2230
2231     return E_NOTIMPL;
2232 }
2233
2234 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetVertex(IDirect3DRMMeshBuilder3 *iface,
2235         DWORD index, D3DVECTOR *vector)
2236 {
2237     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2238
2239     FIXME("(%p)->(%u,%p): stub\n", This, index, vector);
2240
2241     return E_NOTIMPL;
2242 }
2243
2244 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetNormal(IDirect3DRMMeshBuilder3 *iface,
2245         DWORD index, D3DVECTOR *vector)
2246 {
2247     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2248
2249     FIXME("(%p)->(%u,%p): stub\n", This, index, vector);
2250
2251     return E_NOTIMPL;
2252 }
2253
2254 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteVertices(IDirect3DRMMeshBuilder3* iface,
2255                                                                  DWORD IndexFirst, DWORD count)
2256 {
2257     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2258
2259     FIXME("(%p)->(%u,%u): stub\n", This, IndexFirst, count);
2260
2261     return E_NOTIMPL;
2262 }
2263
2264 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteNormals(IDirect3DRMMeshBuilder3* iface,
2265                                                                 DWORD IndexFirst, DWORD count)
2266 {
2267     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2268
2269     FIXME("(%p)->(%u,%u): stub\n", This, IndexFirst, count);
2270
2271     return E_NOTIMPL;
2272 }
2273
2274 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteFace(IDirect3DRMMeshBuilder3* iface,
2275                                                              LPDIRECT3DRMFACE2 Face)
2276 {
2277     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2278
2279     FIXME("(%p)->(%p): stub\n", This, Face);
2280
2281     return E_NOTIMPL;
2282 }
2283
2284 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Empty(IDirect3DRMMeshBuilder3* iface, DWORD flags)
2285 {
2286     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2287
2288     FIXME("(%p)->(%u): stub\n", This, flags);
2289
2290     return E_NOTIMPL;
2291 }
2292
2293 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Optimize(IDirect3DRMMeshBuilder3* iface,
2294                                                            DWORD flags)
2295 {
2296     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2297
2298     FIXME("(%p)->(%u): stub\n", This, flags);
2299
2300     return E_NOTIMPL;
2301 }
2302
2303 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddFacesIndexed(IDirect3DRMMeshBuilder3* iface,
2304                                                                   DWORD flags, DWORD* indices,
2305                                                                   DWORD* IndexFirst, DWORD* count)
2306 {
2307     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2308
2309     FIXME("(%p)->(%u,%p,%p,%p): stub\n", This, flags, indices, IndexFirst, count);
2310
2311     return E_NOTIMPL;
2312 }
2313
2314 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_CreateSubMesh(IDirect3DRMMeshBuilder3* iface,
2315                                                                 LPUNKNOWN *unkwn)
2316 {
2317     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2318
2319     FIXME("(%p)->(%p): stub\n", This, unkwn);
2320
2321     return E_NOTIMPL;
2322 }
2323
2324 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetParentMesh(IDirect3DRMMeshBuilder3* iface,
2325                                                                 DWORD flags, LPUNKNOWN *unkwn)
2326 {
2327     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2328
2329     FIXME("(%p)->(%u,%p): stub\n", This, flags, unkwn);
2330
2331     return E_NOTIMPL;
2332 }
2333
2334 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetSubMeshes(IDirect3DRMMeshBuilder3* iface,
2335                                                                LPDWORD count, LPUNKNOWN *unkwn)
2336 {
2337     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2338
2339     FIXME("(%p)->(%p,%p): stub\n", This, count, unkwn);
2340
2341     return E_NOTIMPL;
2342 }
2343
2344 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteSubMesh(IDirect3DRMMeshBuilder3* iface,
2345                                                                 LPUNKNOWN unkwn)
2346 {
2347     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2348
2349     FIXME("(%p)->(%p): stub\n", This, unkwn);
2350
2351     return E_NOTIMPL;
2352 }
2353
2354 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Enable(IDirect3DRMMeshBuilder3* iface,
2355                                                          DWORD index)
2356 {
2357     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2358
2359     FIXME("(%p)->(%u): stub\n", This, index);
2360
2361     return E_NOTIMPL;
2362 }
2363
2364 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetEnable(IDirect3DRMMeshBuilder3* iface,
2365                                                             DWORD* indices)
2366 {
2367     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2368
2369     FIXME("(%p)->(%p): stub\n", This, indices);
2370
2371     return E_NOTIMPL;
2372 }
2373
2374 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddTriangles(IDirect3DRMMeshBuilder3* iface,
2375                                                                DWORD flags, DWORD format,
2376                                                                DWORD VertexCount, LPVOID data)
2377 {
2378     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2379
2380     FIXME("(%p)->(%u,%u,%u,%p): stub\n", This, flags, format, VertexCount, data);
2381
2382     return E_NOTIMPL;
2383 }
2384
2385 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetVertices(IDirect3DRMMeshBuilder3 *iface,
2386         DWORD IndexFirst, DWORD count, D3DVECTOR *vector)
2387 {
2388     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2389
2390     FIXME("(%p)->(%u,%u,%p): stub\n", This, IndexFirst, count, vector);
2391
2392     return E_NOTIMPL;
2393 }
2394
2395 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetVertices(IDirect3DRMMeshBuilder3 *iface,
2396         DWORD IndexFirst, DWORD *vcount, D3DVECTOR *vertices)
2397 {
2398     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2399     DWORD count = This->nb_vertices - IndexFirst;
2400
2401     TRACE("(%p)->(%u,%p,%p)\n", This, IndexFirst, vcount, vertices);
2402
2403     if (vcount)
2404         *vcount = count;
2405     if (vertices && This->nb_vertices)
2406         memcpy(vertices, This->pVertices + IndexFirst, count * sizeof(D3DVECTOR));
2407
2408     return D3DRM_OK;
2409 }
2410
2411 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetNormals(IDirect3DRMMeshBuilder3 *iface,
2412         DWORD IndexFirst, DWORD count, D3DVECTOR *vector)
2413 {
2414     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2415
2416     FIXME("(%p)->(%u,%u,%p): stub\n", This, IndexFirst, count, vector);
2417
2418     return E_NOTIMPL;
2419 }
2420
2421 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetNormals(IDirect3DRMMeshBuilder3 *iface,
2422         DWORD IndexFirst, DWORD *count, D3DVECTOR *vector)
2423 {
2424     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2425
2426     FIXME("(%p)->(%u,%p,%p): stub\n", This, IndexFirst, count, vector);
2427
2428     return E_NOTIMPL;
2429 }
2430
2431 static int WINAPI IDirect3DRMMeshBuilder3Impl_GetNormalCount(IDirect3DRMMeshBuilder3* iface)
2432 {
2433     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2434
2435     FIXME("(%p)->(): stub\n", This);
2436
2437     return E_NOTIMPL;
2438 }
2439
2440 static const struct IDirect3DRMMeshBuilder3Vtbl Direct3DRMMeshBuilder3_Vtbl =
2441 {
2442     /*** IUnknown methods ***/
2443     IDirect3DRMMeshBuilder3Impl_QueryInterface,
2444     IDirect3DRMMeshBuilder3Impl_AddRef,
2445     IDirect3DRMMeshBuilder3Impl_Release,
2446     /*** IDirect3DRMObject methods ***/
2447     IDirect3DRMMeshBuilder3Impl_Clone,
2448     IDirect3DRMMeshBuilder3Impl_AddDestroyCallback,
2449     IDirect3DRMMeshBuilder3Impl_DeleteDestroyCallback,
2450     IDirect3DRMMeshBuilder3Impl_SetAppData,
2451     IDirect3DRMMeshBuilder3Impl_GetAppData,
2452     IDirect3DRMMeshBuilder3Impl_SetName,
2453     IDirect3DRMMeshBuilder3Impl_GetName,
2454     IDirect3DRMMeshBuilder3Impl_GetClassName,
2455     /*** IDirect3DRMMeshBuilder3 methods ***/
2456     IDirect3DRMMeshBuilder3Impl_Load,
2457     IDirect3DRMMeshBuilder3Impl_Save,
2458     IDirect3DRMMeshBuilder3Impl_Scale,
2459     IDirect3DRMMeshBuilder3Impl_Translate,
2460     IDirect3DRMMeshBuilder3Impl_SetColorSource,
2461     IDirect3DRMMeshBuilder3Impl_GetBox,
2462     IDirect3DRMMeshBuilder3Impl_GenerateNormals,
2463     IDirect3DRMMeshBuilder3Impl_GetColorSource,
2464     IDirect3DRMMeshBuilder3Impl_AddMesh,
2465     IDirect3DRMMeshBuilder3Impl_AddMeshBuilder,
2466     IDirect3DRMMeshBuilder3Impl_AddFrame,
2467     IDirect3DRMMeshBuilder3Impl_AddFace,
2468     IDirect3DRMMeshBuilder3Impl_AddFaces,
2469     IDirect3DRMMeshBuilder3Impl_ReserveSpace,
2470     IDirect3DRMMeshBuilder3Impl_SetColorRGB,
2471     IDirect3DRMMeshBuilder3Impl_SetColor,
2472     IDirect3DRMMeshBuilder3Impl_SetTexture,
2473     IDirect3DRMMeshBuilder3Impl_SetMaterial,
2474     IDirect3DRMMeshBuilder3Impl_SetTextureTopology,
2475     IDirect3DRMMeshBuilder3Impl_SetQuality,
2476     IDirect3DRMMeshBuilder3Impl_SetPerspective,
2477     IDirect3DRMMeshBuilder3Impl_SetVertex,
2478     IDirect3DRMMeshBuilder3Impl_SetNormal,
2479     IDirect3DRMMeshBuilder3Impl_SetTextureCoordinates,
2480     IDirect3DRMMeshBuilder3Impl_SetVertexColor,
2481     IDirect3DRMMeshBuilder3Impl_SetVertexColorRGB,
2482     IDirect3DRMMeshBuilder3Impl_GetFaces,
2483     IDirect3DRMMeshBuilder3Impl_GetGeometry,
2484     IDirect3DRMMeshBuilder3Impl_GetTextureCoordinates,
2485     IDirect3DRMMeshBuilder3Impl_AddVertex,
2486     IDirect3DRMMeshBuilder3Impl_AddNormal,
2487     IDirect3DRMMeshBuilder3Impl_CreateFace,
2488     IDirect3DRMMeshBuilder3Impl_GetQuality,
2489     IDirect3DRMMeshBuilder3Impl_GetPerspective,
2490     IDirect3DRMMeshBuilder3Impl_GetFaceCount,
2491     IDirect3DRMMeshBuilder3Impl_GetVertexCount,
2492     IDirect3DRMMeshBuilder3Impl_GetVertexColor,
2493     IDirect3DRMMeshBuilder3Impl_CreateMesh,
2494     IDirect3DRMMeshBuilder3Impl_GetFace,
2495     IDirect3DRMMeshBuilder3Impl_GetVertex,
2496     IDirect3DRMMeshBuilder3Impl_GetNormal,
2497     IDirect3DRMMeshBuilder3Impl_DeleteVertices,
2498     IDirect3DRMMeshBuilder3Impl_DeleteNormals,
2499     IDirect3DRMMeshBuilder3Impl_DeleteFace,
2500     IDirect3DRMMeshBuilder3Impl_Empty,
2501     IDirect3DRMMeshBuilder3Impl_Optimize,
2502     IDirect3DRMMeshBuilder3Impl_AddFacesIndexed,
2503     IDirect3DRMMeshBuilder3Impl_CreateSubMesh,
2504     IDirect3DRMMeshBuilder3Impl_GetParentMesh,
2505     IDirect3DRMMeshBuilder3Impl_GetSubMeshes,
2506     IDirect3DRMMeshBuilder3Impl_DeleteSubMesh,
2507     IDirect3DRMMeshBuilder3Impl_Enable,
2508     IDirect3DRMMeshBuilder3Impl_GetEnable,
2509     IDirect3DRMMeshBuilder3Impl_AddTriangles,
2510     IDirect3DRMMeshBuilder3Impl_SetVertices,
2511     IDirect3DRMMeshBuilder3Impl_GetVertices,
2512     IDirect3DRMMeshBuilder3Impl_SetNormals,
2513     IDirect3DRMMeshBuilder3Impl_GetNormals,
2514     IDirect3DRMMeshBuilder3Impl_GetNormalCount
2515 };
2516
2517 HRESULT Direct3DRMMeshBuilder_create(REFIID riid, IUnknown** ppObj)
2518 {
2519     IDirect3DRMMeshBuilderImpl* object;
2520
2521     TRACE("(%p)\n", ppObj);
2522
2523     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DRMMeshBuilderImpl));
2524     if (!object)
2525         return E_OUTOFMEMORY;
2526
2527     object->IDirect3DRMMeshBuilder2_iface.lpVtbl = &Direct3DRMMeshBuilder2_Vtbl;
2528     object->IDirect3DRMMeshBuilder3_iface.lpVtbl = &Direct3DRMMeshBuilder3_Vtbl;
2529     object->ref = 1;
2530
2531     if (IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder3))
2532         *ppObj = (IUnknown*)&object->IDirect3DRMMeshBuilder3_iface;
2533     else
2534         *ppObj = (IUnknown*)&object->IDirect3DRMMeshBuilder2_iface;
2535
2536     return S_OK;
2537 }
2538
2539 /*** IUnknown methods ***/
2540 static HRESULT WINAPI IDirect3DRMMeshImpl_QueryInterface(IDirect3DRMMesh* iface,
2541                                                          REFIID riid, void** ppvObject)
2542 {
2543     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2544
2545     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
2546
2547     *ppvObject = NULL;
2548
2549     if (IsEqualGUID(riid, &IID_IUnknown) ||
2550         IsEqualGUID(riid, &IID_IDirect3DRMMesh))
2551     {
2552         *ppvObject = &This->IDirect3DRMMesh_iface;
2553     }
2554     else
2555     {
2556         FIXME("interface %s not implemented\n", debugstr_guid(riid));
2557         return E_NOINTERFACE;
2558     }
2559
2560     IDirect3DRMMesh_AddRef(iface);
2561     return S_OK;
2562 }
2563
2564 static ULONG WINAPI IDirect3DRMMeshImpl_AddRef(IDirect3DRMMesh* iface)
2565 {
2566     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2567     ULONG ref = InterlockedIncrement(&This->ref);
2568
2569     TRACE("(%p)->(): new ref = %d\n", This, ref);
2570
2571     return ref;
2572 }
2573
2574 static ULONG WINAPI IDirect3DRMMeshImpl_Release(IDirect3DRMMesh* iface)
2575 {
2576     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2577     ULONG ref = InterlockedDecrement(&This->ref);
2578
2579     TRACE("(%p)->(): new ref = %d\n", This, ref);
2580
2581     if (!ref)
2582     {
2583         DWORD i;
2584
2585         for (i = 0; i < This->nb_groups; i++)
2586         {
2587             HeapFree(GetProcessHeap(), 0, This->groups[i].vertices);
2588             HeapFree(GetProcessHeap(), 0, This->groups[i].face_data);
2589             if (This->groups[i].material)
2590                 IDirect3DRMMaterial2_Release(This->groups[i].material);
2591             if (This->groups[i].texture)
2592                 IDirect3DRMTexture3_Release(This->groups[i].texture);
2593         }
2594         HeapFree(GetProcessHeap(), 0, This->groups);
2595         HeapFree(GetProcessHeap(), 0, This);
2596     }
2597
2598     return ref;
2599 }
2600
2601 /*** IDirect3DRMObject methods ***/
2602 static HRESULT WINAPI IDirect3DRMMeshImpl_Clone(IDirect3DRMMesh* iface,
2603                                                 LPUNKNOWN pUnkOuter, REFIID riid,
2604                                                 LPVOID *ppvObj)
2605 {
2606     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2607
2608     FIXME("(%p)->(%p,%s,%p): stub\n", This, pUnkOuter, debugstr_guid(riid), ppvObj);
2609
2610     return E_NOTIMPL;
2611 }
2612
2613 static HRESULT WINAPI IDirect3DRMMeshImpl_AddDestroyCallback(IDirect3DRMMesh* iface,
2614                                                              D3DRMOBJECTCALLBACK cb,
2615                                                              LPVOID argument)
2616 {
2617     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2618
2619     FIXME("(%p)->(%p,%p): stub\n", This, cb, argument);
2620
2621     return E_NOTIMPL;
2622 }
2623
2624 static HRESULT WINAPI IDirect3DRMMeshImpl_DeleteDestroyCallback(IDirect3DRMMesh* iface,
2625                                                                  D3DRMOBJECTCALLBACK cb,
2626                                                                  LPVOID argument)
2627 {
2628     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2629
2630     FIXME("(%p)->(%p,%p): stub\n", This, cb, argument);
2631
2632     return E_NOTIMPL;
2633 }
2634
2635 static HRESULT WINAPI IDirect3DRMMeshImpl_SetAppData(IDirect3DRMMesh* iface,
2636                                                      DWORD data)
2637 {
2638     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2639
2640     FIXME("(%p)->(%u): stub\n", This, data);
2641
2642     return E_NOTIMPL;
2643 }
2644
2645 static DWORD WINAPI IDirect3DRMMeshImpl_GetAppData(IDirect3DRMMesh* iface)
2646 {
2647     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2648
2649     FIXME("(%p)->(): stub\n", This);
2650
2651     return 0;
2652 }
2653
2654 static HRESULT WINAPI IDirect3DRMMeshImpl_SetName(IDirect3DRMMesh* iface,
2655                                                   LPCSTR pName)
2656 {
2657     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2658
2659     FIXME("(%p)->(%s): stub\n", This, pName);
2660
2661     return E_NOTIMPL;
2662 }
2663
2664 static HRESULT WINAPI IDirect3DRMMeshImpl_GetName(IDirect3DRMMesh* iface,
2665                                                   LPDWORD lpdwSize, LPSTR lpName)
2666 {
2667     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2668
2669     FIXME("(%p)->(%p,%p): stub\n", This, lpdwSize, lpName);
2670
2671     return E_NOTIMPL;
2672 }
2673
2674 static HRESULT WINAPI IDirect3DRMMeshImpl_GetClassName(IDirect3DRMMesh* iface,
2675                                                        LPDWORD size, LPSTR name)
2676 {
2677     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2678
2679     TRACE("(%p)->(%p, %p)\n", This, size, name);
2680
2681     if (!size || *size < strlen("Mesh") || !name)
2682         return E_INVALIDARG;
2683
2684     strcpy(name, "Mesh");
2685     *size = sizeof("Mesh");
2686
2687     return D3DRM_OK;
2688 }
2689
2690 /*** IDirect3DRMMesh methods ***/
2691 static HRESULT WINAPI IDirect3DRMMeshImpl_Scale(IDirect3DRMMesh* iface,
2692                                                 D3DVALUE sx, D3DVALUE sy, D3DVALUE sz)
2693 {
2694     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2695
2696     FIXME("(%p)->(%f,%f,%f): stub\n", This, sx, sy,sz);
2697
2698     return E_NOTIMPL;
2699 }
2700
2701 static HRESULT WINAPI IDirect3DRMMeshImpl_Translate(IDirect3DRMMesh* iface,
2702                                                     D3DVALUE tx, D3DVALUE ty, D3DVALUE tz)
2703 {
2704     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2705
2706     FIXME("(%p)->(%f,%f,%f): stub\n", This, tx, ty,tz);
2707
2708     return E_NOTIMPL;
2709 }
2710
2711 static HRESULT WINAPI IDirect3DRMMeshImpl_GetBox(IDirect3DRMMesh* iface,
2712                                                  D3DRMBOX * box)
2713 {
2714     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2715
2716     FIXME("(%p)->(%p): stub\n", This, box);
2717
2718     return E_NOTIMPL;
2719 }
2720
2721 static HRESULT WINAPI IDirect3DRMMeshImpl_AddGroup(IDirect3DRMMesh* iface,
2722                                                    unsigned vertex_count, unsigned face_count, unsigned vertex_per_face,
2723                                                    unsigned *face_data, D3DRMGROUPINDEX *return_id)
2724 {
2725     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2726     mesh_group* group;
2727
2728     TRACE("(%p)->(%u,%u,%u,%p,%p)\n", This, vertex_count, face_count, vertex_per_face, face_data, return_id);
2729
2730     if (!face_data || !return_id)
2731         return E_POINTER;
2732
2733     if ((This->nb_groups + 1) > This->groups_capacity)
2734     {
2735         ULONG new_capacity;
2736         mesh_group* groups;
2737
2738         if (!This->groups_capacity)
2739         {
2740             new_capacity = 16;
2741             groups = HeapAlloc(GetProcessHeap(), 0, new_capacity * sizeof(mesh_group));
2742         }
2743         else
2744         {
2745             new_capacity = This->groups_capacity * 2;
2746             groups = HeapReAlloc(GetProcessHeap(), 0, This->groups, new_capacity * sizeof(mesh_group));
2747         }
2748
2749         if (!groups)
2750             return E_OUTOFMEMORY;
2751
2752         This->groups_capacity = new_capacity;
2753         This->groups = groups;
2754     }
2755
2756     group = This->groups + This->nb_groups;
2757
2758     group->vertices = HeapAlloc(GetProcessHeap(), 0, vertex_count * sizeof(D3DRMVERTEX));
2759     if (!group->vertices)
2760         return E_OUTOFMEMORY;
2761     group->nb_vertices = vertex_count;
2762     group->nb_faces = face_count;
2763     group->vertex_per_face = vertex_per_face;
2764
2765     if (vertex_per_face)
2766     {
2767         group->face_data_size = face_count * vertex_per_face;
2768     }
2769     else
2770     {
2771         unsigned i;
2772         unsigned nb_indices;
2773         unsigned* face_data_ptr = face_data;
2774         group->face_data_size = 0;
2775
2776         for (i = 0; i < face_count; i++)
2777         {
2778             nb_indices = *face_data_ptr;
2779             group->face_data_size += nb_indices + 1;
2780             face_data_ptr += nb_indices;
2781         }
2782     }
2783
2784     group->face_data = HeapAlloc(GetProcessHeap(), 0, group->face_data_size * sizeof(unsigned));
2785     if (!group->face_data)
2786     {
2787         HeapFree(GetProcessHeap(), 0 , group->vertices);
2788         return E_OUTOFMEMORY;
2789     }
2790
2791     memcpy(group->face_data, face_data, group->face_data_size * sizeof(unsigned));
2792
2793     group->material = NULL;
2794     group->texture = NULL;
2795
2796     *return_id = This->nb_groups++;
2797
2798     return D3DRM_OK;
2799 }
2800
2801 static HRESULT WINAPI IDirect3DRMMeshImpl_SetVertices(IDirect3DRMMesh* iface,
2802                                                       D3DRMGROUPINDEX id, unsigned index, unsigned count,
2803                                                       D3DRMVERTEX *values)
2804 {
2805     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2806
2807     TRACE("(%p)->(%u,%u,%u,%p)\n", This, id, index, count, values);
2808
2809     if (id >= This->nb_groups)
2810         return D3DRMERR_BADVALUE;
2811
2812     if ((index + count - 1) >= This->groups[id].nb_vertices)
2813         return D3DRMERR_BADVALUE;
2814
2815     if (!values)
2816         return E_POINTER;
2817
2818     memcpy(This->groups[id].vertices + index, values, count * sizeof(D3DRMVERTEX));
2819
2820     return D3DRM_OK;
2821 }
2822
2823 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupColor(IDirect3DRMMesh* iface,
2824                                                         D3DRMGROUPINDEX id, D3DCOLOR color)
2825 {
2826     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2827
2828     TRACE("(%p)->(%u,%x)\n", This, id, color);
2829
2830     if (id >= This->nb_groups)
2831         return D3DRMERR_BADVALUE;
2832
2833     This->groups[id].color = color;
2834
2835     return D3DRM_OK;
2836 }
2837
2838 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupColorRGB(IDirect3DRMMesh* iface,
2839                                                            D3DRMGROUPINDEX id, D3DVALUE red, D3DVALUE green, D3DVALUE blue)
2840 {
2841     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2842
2843     TRACE("(%p)->(%u,%f,%f,%f)\n", This, id, red, green, blue);
2844
2845     if (id >= This->nb_groups)
2846         return D3DRMERR_BADVALUE;
2847
2848     This->groups[id].color = D3DCOLOR_ARGB(0xff, (BYTE)(red   * 255.0f),
2849                                                  (BYTE)(green * 255.0f),
2850                                                  (BYTE)(blue  * 255.0f));
2851
2852     return D3DRM_OK;
2853 }
2854
2855 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupMapping(IDirect3DRMMesh* iface,
2856                                                           D3DRMGROUPINDEX id, D3DRMMAPPING value)
2857 {
2858     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2859
2860     FIXME("(%p)->(%u,%u): stub\n", This, id, value);
2861
2862     return E_NOTIMPL;
2863 }
2864
2865 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupQuality(IDirect3DRMMesh* iface,
2866                                                           D3DRMGROUPINDEX id, D3DRMRENDERQUALITY value)
2867 {
2868     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2869
2870     FIXME("(%p)->(%u,%u): stub\n", This, id, value);
2871
2872     return E_NOTIMPL;
2873 }
2874
2875 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupMaterial(IDirect3DRMMesh* iface,
2876                                                            D3DRMGROUPINDEX id, LPDIRECT3DRMMATERIAL material)
2877 {
2878     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2879
2880     TRACE("(%p)->(%u,%p)\n", This, id, material);
2881
2882     if (id >= This->nb_groups)
2883         return D3DRMERR_BADVALUE;
2884
2885     if (This->groups[id].material)
2886         IDirect3DRMMaterial2_Release(This->groups[id].material);
2887
2888     This->groups[id].material = (LPDIRECT3DRMMATERIAL2)material;
2889
2890     if (material)
2891         IDirect3DRMMaterial2_AddRef(This->groups[id].material);
2892
2893     return D3DRM_OK;
2894 }
2895
2896 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupTexture(IDirect3DRMMesh* iface,
2897                                                           D3DRMGROUPINDEX id, LPDIRECT3DRMTEXTURE texture)
2898 {
2899     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2900
2901     TRACE("(%p)->(%u,%p)\n", This, id, texture);
2902
2903     if (id >= This->nb_groups)
2904         return D3DRMERR_BADVALUE;
2905
2906     if (This->groups[id].texture)
2907         IDirect3DRMTexture3_Release(This->groups[id].texture);
2908
2909     if (!texture)
2910     {
2911         This->groups[id].texture = NULL;
2912         return D3DRM_OK;
2913     }
2914
2915     return IDirect3DRMTexture3_QueryInterface(texture, &IID_IDirect3DRMTexture, (LPVOID*)&This->groups[id].texture);
2916 }
2917
2918 static DWORD WINAPI IDirect3DRMMeshImpl_GetGroupCount(IDirect3DRMMesh* iface)
2919 {
2920     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2921
2922     TRACE("(%p)->()\n", This);
2923
2924     return This->nb_groups;
2925 }
2926
2927 static HRESULT WINAPI IDirect3DRMMeshImpl_GetGroup(IDirect3DRMMesh* iface,
2928                                                    D3DRMGROUPINDEX id, unsigned *vertex_count, unsigned *face_count, unsigned *vertex_per_face,
2929                                                    DWORD *face_data_size, unsigned *face_data)
2930 {
2931     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2932
2933     TRACE("(%p)->(%u,%p,%p,%p,%p,%p)\n", This, id, vertex_count, face_count, vertex_per_face, face_data_size, face_data);
2934
2935     if (id >= This->nb_groups)
2936         return D3DRMERR_BADVALUE;
2937
2938     if (vertex_count)
2939         *vertex_count = This->groups[id].nb_vertices;
2940     if (face_count)
2941         *face_count = This->groups[id].nb_faces;
2942     if (vertex_per_face)
2943         *vertex_per_face = This->groups[id].vertex_per_face;
2944     if (face_data_size)
2945         *face_data_size = This->groups[id].face_data_size;
2946     if (face_data)
2947         memcpy(face_data, This->groups[id].face_data, This->groups[id].face_data_size * sizeof(DWORD));
2948
2949     return D3DRM_OK;
2950 }
2951
2952 static HRESULT WINAPI IDirect3DRMMeshImpl_GetVertices(IDirect3DRMMesh* iface,
2953                                                       D3DRMGROUPINDEX id, DWORD index, DWORD count, D3DRMVERTEX *return_ptr)
2954 {
2955     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2956
2957     TRACE("(%p)->(%u,%u,%u,%p)\n", This, id, index, count, return_ptr);
2958
2959     if (id >= This->nb_groups)
2960         return D3DRMERR_BADVALUE;
2961
2962     if ((index + count - 1) >= This->groups[id].nb_vertices)
2963         return D3DRMERR_BADVALUE;
2964
2965     if (!return_ptr)
2966         return E_POINTER;
2967
2968     memcpy(return_ptr, This->groups[id].vertices + index, count * sizeof(D3DRMVERTEX));
2969
2970     return D3DRM_OK;
2971 }
2972
2973 static D3DCOLOR WINAPI IDirect3DRMMeshImpl_GetGroupColor(IDirect3DRMMesh* iface, D3DRMGROUPINDEX id)
2974 {
2975     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2976
2977     TRACE("(%p)->(%u)\n", This, id);
2978
2979     return This->groups[id].color;
2980 }
2981
2982 static D3DRMMAPPING WINAPI IDirect3DRMMeshImpl_GetGroupMapping(IDirect3DRMMesh* iface, D3DRMGROUPINDEX id)
2983 {
2984     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2985
2986     FIXME("(%p)->(%u): stub\n", This, id);
2987
2988     return 0;
2989 }
2990 static D3DRMRENDERQUALITY WINAPI IDirect3DRMMeshImpl_GetGroupQuality(IDirect3DRMMesh* iface, D3DRMGROUPINDEX id)
2991 {
2992     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2993
2994     FIXME("(%p)->(%u): stub\n", This, id);
2995
2996     return 0;
2997 }
2998
2999 static HRESULT WINAPI IDirect3DRMMeshImpl_GetGroupMaterial(IDirect3DRMMesh* iface,
3000                                                            D3DRMGROUPINDEX id, LPDIRECT3DRMMATERIAL *material)
3001 {
3002     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
3003
3004     TRACE("(%p)->(%u,%p)\n", This, id, material);
3005
3006     if (id >= This->nb_groups)
3007         return D3DRMERR_BADVALUE;
3008
3009     if (!material)
3010         return E_POINTER;
3011
3012     if (This->groups[id].material)
3013         IDirect3DRMTexture_QueryInterface(This->groups[id].material, &IID_IDirect3DRMMaterial, (void**)material);
3014     else
3015         *material = NULL;
3016
3017     return D3DRM_OK;
3018 }
3019
3020 static HRESULT WINAPI IDirect3DRMMeshImpl_GetGroupTexture(IDirect3DRMMesh* iface,
3021                                                           D3DRMGROUPINDEX id, LPDIRECT3DRMTEXTURE *texture)
3022 {
3023     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
3024
3025     TRACE("(%p)->(%u,%p)\n", This, id, texture);
3026
3027     if (id >= This->nb_groups)
3028         return D3DRMERR_BADVALUE;
3029
3030     if (!texture)
3031         return E_POINTER;
3032
3033     if (This->groups[id].texture)
3034         IDirect3DRMTexture_QueryInterface(This->groups[id].texture, &IID_IDirect3DRMTexture, (void**)texture);
3035     else
3036         *texture = NULL;
3037
3038     return D3DRM_OK;
3039 }
3040
3041 static const struct IDirect3DRMMeshVtbl Direct3DRMMesh_Vtbl =
3042 {
3043     /*** IUnknown methods ***/
3044     IDirect3DRMMeshImpl_QueryInterface,
3045     IDirect3DRMMeshImpl_AddRef,
3046     IDirect3DRMMeshImpl_Release,
3047     /*** IDirect3DRMObject methods ***/
3048     IDirect3DRMMeshImpl_Clone,
3049     IDirect3DRMMeshImpl_AddDestroyCallback,
3050     IDirect3DRMMeshImpl_DeleteDestroyCallback,
3051     IDirect3DRMMeshImpl_SetAppData,
3052     IDirect3DRMMeshImpl_GetAppData,
3053     IDirect3DRMMeshImpl_SetName,
3054     IDirect3DRMMeshImpl_GetName,
3055     IDirect3DRMMeshImpl_GetClassName,
3056     /*** IDirect3DRMMesh methods ***/
3057     IDirect3DRMMeshImpl_Scale,
3058     IDirect3DRMMeshImpl_Translate,
3059     IDirect3DRMMeshImpl_GetBox,
3060     IDirect3DRMMeshImpl_AddGroup,
3061     IDirect3DRMMeshImpl_SetVertices,
3062     IDirect3DRMMeshImpl_SetGroupColor,
3063     IDirect3DRMMeshImpl_SetGroupColorRGB,
3064     IDirect3DRMMeshImpl_SetGroupMapping,
3065     IDirect3DRMMeshImpl_SetGroupQuality,
3066     IDirect3DRMMeshImpl_SetGroupMaterial,
3067     IDirect3DRMMeshImpl_SetGroupTexture,
3068     IDirect3DRMMeshImpl_GetGroupCount,
3069     IDirect3DRMMeshImpl_GetGroup,
3070     IDirect3DRMMeshImpl_GetVertices,
3071     IDirect3DRMMeshImpl_GetGroupColor,
3072     IDirect3DRMMeshImpl_GetGroupMapping,
3073     IDirect3DRMMeshImpl_GetGroupQuality,
3074     IDirect3DRMMeshImpl_GetGroupMaterial,
3075     IDirect3DRMMeshImpl_GetGroupTexture
3076 };
3077
3078 HRESULT Direct3DRMMesh_create(IDirect3DRMMesh** obj)
3079 {
3080     IDirect3DRMMeshImpl* object;
3081
3082     TRACE("(%p)\n", obj);
3083
3084     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DRMMeshImpl));
3085     if (!object)
3086         return E_OUTOFMEMORY;
3087
3088     object->IDirect3DRMMesh_iface.lpVtbl = &Direct3DRMMesh_Vtbl;
3089     object->ref = 1;
3090
3091     *obj = &object->IDirect3DRMMesh_iface;
3092
3093     return S_OK;
3094 }