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