d3d10core: Implement d3d10_device_RSGetState().
[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 > 2)
1247             {
1248                 FIXME("Only one material per mesh supported, first one applies to all faces\n");
1249                 nb_materials = 1;
1250             }
1251
1252             while (SUCCEEDED(hr = IDirectXFileData_GetNextObject(pData2, &child)) && (i < nb_materials))
1253             {
1254                 LPDIRECTXFILEDATA data;
1255                 LPDIRECTXFILEDATAREFERENCE reference;
1256                 LPDIRECT3DRMMATERIAL2 material;
1257                 LPDIRECTXFILEOBJECT material_child;
1258
1259                 hr = IDirectXFileObject_QueryInterface(child, &IID_IDirectXFileData, (void **)&data);
1260                 if (FAILED(hr))
1261                 {
1262                     hr = IDirectXFileObject_QueryInterface(child, &IID_IDirectXFileDataReference, (void **)&reference);
1263                     if (FAILED(hr))
1264                         goto end;
1265
1266                     hr = IDirectXFileDataReference_Resolve(reference, &data);
1267                     IDirectXFileDataReference_Release(reference);
1268                     if (FAILED(hr))
1269                         goto end;
1270                 }
1271
1272                 hr = Direct3DRMMaterial_create(&material);
1273                 if (FAILED(hr))
1274                 {
1275                     IDirectXFileData_Release(data);
1276                     goto end;
1277                 }
1278
1279                 hr = IDirectXFileData_GetData(data, NULL, &size, (void**)&ptr);
1280                 if (hr != DXFILE_OK)
1281                 {
1282                     IDirectXFileData_Release(data);
1283                     goto end;
1284                 }
1285
1286                 if (size != 44)
1287                     WARN("Material size %u does not match expected one %u\n", size, 44);
1288
1289                 values = (float*)ptr;
1290
1291                 IDirect3DRMMeshBuilder3_SetColorRGB(iface, values[0], values [1], values[2]); /* Alpha ignored */
1292
1293                 IDirect3DRMMaterial2_SetAmbient(material, values[0], values [1], values[2]); /* Alpha ignored */
1294                 IDirect3DRMMaterial2_SetPower(material, values[4]);
1295                 IDirect3DRMMaterial2_SetSpecular(material, values[5], values[6], values[7]);
1296                 IDirect3DRMMaterial2_SetEmissive(material, values[8], values[9], values[10]);
1297
1298                 This->material = material;
1299
1300                 hr = IDirectXFileData_GetNextObject(data, &material_child);
1301                 if (hr == S_OK)
1302                 {
1303                     LPDIRECTXFILEDATA data;
1304                     LPDIRECT3DRMTEXTURE3 texture;
1305                     char** filename;
1306
1307                     hr = IDirectXFileObject_QueryInterface(material_child, &IID_IDirectXFileData, (void **)&data);
1308                     if (FAILED(hr))
1309                     {
1310                         LPDIRECTXFILEDATAREFERENCE reference;
1311
1312                         hr = IDirectXFileObject_QueryInterface(material_child, &IID_IDirectXFileDataReference, (void **)&reference);
1313                         if (FAILED(hr))
1314                             goto end;
1315
1316                         hr = IDirectXFileDataReference_Resolve(reference, &data);
1317                         IDirectXFileDataReference_Release(reference);
1318                         if (FAILED(hr))
1319                             goto end;
1320                     }
1321
1322                     hr = IDirectXFileData_GetType(data, &pGuid);
1323                     if (hr != DXFILE_OK)
1324                         goto end;
1325                     if (!IsEqualGUID(pGuid, &TID_D3DRMTextureFilename))
1326                     {
1327                          WARN("Not a texture filename\n");
1328                          goto end;
1329                     }
1330
1331                     size = 4;
1332                     hr = IDirectXFileData_GetData(data, NULL, &size, (void**)&filename);
1333                     if (SUCCEEDED(hr))
1334                     {
1335                         HANDLE file;
1336
1337                         file = CreateFileA(*filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1338                         if (file != INVALID_HANDLE_VALUE)
1339                         {
1340                             hr = Direct3DRMTexture_create(&IID_IDirect3DRMTexture3, (LPUNKNOWN*)&texture);
1341                             if (FAILED(hr))
1342                             {
1343                                 IDirectXFileData_Release(data);
1344                                 goto end;
1345                             }
1346                             This->texture = texture;
1347                         }
1348                     }
1349                 }
1350                 else if (hr != DXFILEERR_NOMOREOBJECTS)
1351                 {
1352                     goto end;
1353                 }
1354                 hr = S_OK;
1355
1356                 IDirectXFileData_Release(data);
1357                 i++;
1358             }
1359             if (hr == S_OK)
1360                 WARN("Found more sub-objects than expected\n");
1361             else if (hr != DXFILEERR_NOMOREOBJECTS)
1362                 goto end;
1363             hr = S_OK;
1364         }
1365         else
1366         {
1367             FIXME("Unknown GUID %s, ignoring...\n", debugstr_guid(pGuid));
1368         }
1369
1370         IDirectXFileData_Release(pData2);
1371         pData2 = NULL;
1372     }
1373
1374     if (!This->nb_normals)
1375     {
1376         /* Allocate normals, one per vertex */
1377         This->pNormals = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nb_vertices * sizeof(D3DVECTOR));
1378         if (!This->pNormals)
1379             goto end;
1380     }
1381
1382     for (i = 0; i < This->nb_faces; i++)
1383     {
1384         DWORD j;
1385         DWORD nb_face_indexes;
1386         D3DVECTOR face_normal;
1387
1388         if (faces_vertex_idx_size < sizeof(DWORD))
1389             WARN("Not enough data to read number of indices of face %d\n", i);
1390
1391         nb_face_indexes  = *(faces_data_ptr + faces_data_size++) = *(faces_vertex_idx_ptr++);
1392         faces_vertex_idx_size--;
1393         if (faces_normal_idx_data && (*(faces_normal_idx_ptr++) != nb_face_indexes))
1394             WARN("Faces indices number mismatch\n");
1395
1396         if (faces_vertex_idx_size < (nb_face_indexes * sizeof(DWORD)))
1397             WARN("Not enough data to read all indices of face %d\n", i);
1398
1399         if (!This->nb_normals)
1400         {
1401             /* Compute face normal */
1402             if (nb_face_indexes > 2)
1403             {
1404                 D3DVECTOR a, b;
1405
1406                 D3DRMVectorSubtract(&a, &This->pVertices[faces_vertex_idx_ptr[2]], &This->pVertices[faces_vertex_idx_ptr[1]]);
1407                 D3DRMVectorSubtract(&a, &This->pVertices[faces_vertex_idx_ptr[1]], &This->pVertices[faces_vertex_idx_ptr[0]]);
1408                 D3DRMVectorCrossProduct(&face_normal, &a, &b);
1409                 D3DRMVectorNormalize(&face_normal);
1410             }
1411             else
1412             {
1413                 face_normal.u1.x = 0.0f;
1414                 face_normal.u2.y = 0.0f;
1415                 face_normal.u3.z = 0.0f;
1416             }
1417         }
1418
1419         for (j = 0; j < nb_face_indexes; j++)
1420         {
1421             /* Copy vertex index */
1422             *(faces_data_ptr + faces_data_size++) = *faces_vertex_idx_ptr;
1423             /* Copy normal index */
1424             if (This->nb_normals)
1425             {
1426                 /* Read from x file */
1427                 *(faces_data_ptr + faces_data_size++) = *(faces_normal_idx_ptr++);
1428             }
1429             else
1430             {
1431                 DWORD vertex_idx = *faces_vertex_idx_ptr;
1432                 if (vertex_idx >= This->nb_vertices)
1433                 {
1434                     WARN("Found vertex index %u but only %u vertices available => use index 0\n", vertex_idx, This->nb_vertices);
1435                     vertex_idx = 0;
1436                 }
1437                 *(faces_data_ptr + faces_data_size++) = vertex_idx;
1438                 /* Add face normal to vertex normal */
1439                 D3DRMVectorAdd(&This->pNormals[vertex_idx], &This->pNormals[vertex_idx], &face_normal);
1440             }
1441             faces_vertex_idx_ptr++;
1442         }
1443         faces_vertex_idx_size -= nb_face_indexes;
1444     }
1445
1446     /* Last DWORD must be 0 */
1447     *(faces_data_ptr + faces_data_size++) = 0;
1448
1449     /* Set size (in number of DWORD) of all faces data */
1450     This->face_data_size = faces_data_size;
1451
1452     if (!This->nb_normals)
1453     {
1454         /* Normalize all normals */
1455         for (i = 0; i < This->nb_vertices; i++)
1456         {
1457             D3DRMVectorNormalize(&This->pNormals[i]);
1458         }
1459         This->nb_normals = This->nb_vertices;
1460     }
1461
1462     /* If there is no texture coordinates, generate default texture coordinates (0.0f, 0.0f) for each vertex */
1463     if (!This->pCoords2d)
1464     {
1465         This->nb_coords2d = This->nb_vertices;
1466         This->pCoords2d = HeapAlloc(GetProcessHeap(), 0, This->nb_coords2d * sizeof(Coords2d));
1467         for (i = 0; i < This->nb_coords2d; i++)
1468         {
1469             This->pCoords2d[i].u = 0.0f;
1470             This->pCoords2d[i].v = 0.0f;
1471         }
1472     }
1473
1474     TRACE("Mesh data loaded successfully\n");
1475
1476     ret = D3DRM_OK;
1477
1478 end:
1479
1480     HeapFree(GetProcessHeap(), 0, faces_normal_idx_data);
1481     HeapFree(GetProcessHeap(), 0, faces_vertex_idx_data);
1482
1483     return ret;
1484 }
1485
1486 /*** IDirect3DRMMeshBuilder3 methods ***/
1487 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Load(IDirect3DRMMeshBuilder3* iface,
1488                                                        LPVOID filename, LPVOID name,
1489                                                        D3DRMLOADOPTIONS loadflags,
1490                                                        D3DRMLOADTEXTURE3CALLBACK cb, LPVOID arg)
1491 {
1492     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1493     DXFILELOADOPTIONS load_options;
1494     LPDIRECTXFILE pDXFile = NULL;
1495     LPDIRECTXFILEENUMOBJECT pEnumObject = NULL;
1496     LPDIRECTXFILEDATA pData = NULL;
1497     LPDIRECTXFILEDATA pData2 = NULL;
1498     const GUID* pGuid;
1499     DWORD size;
1500     Header* pHeader;
1501     HRESULT hr;
1502     HRESULT ret = D3DRMERR_BADOBJECT;
1503
1504     TRACE("(%p)->(%p,%p,%x,%p,%p)\n", This, filename, name, loadflags, cb, arg);
1505
1506     /* First free allocated buffers of previous mesh data */
1507     HeapFree(GetProcessHeap(), 0, This->pVertices);
1508     This->pVertices = NULL;
1509     HeapFree(GetProcessHeap(), 0, This->pNormals);
1510     This->pNormals = NULL;
1511     HeapFree(GetProcessHeap(), 0, This->pFaceData);
1512     This->pFaceData = NULL;
1513     HeapFree(GetProcessHeap(), 0, This->pCoords2d);
1514     This->pCoords2d = NULL;
1515
1516     if (loadflags == D3DRMLOAD_FROMMEMORY)
1517     {
1518         load_options = DXFILELOAD_FROMMEMORY;
1519     }
1520     else if (loadflags == D3DRMLOAD_FROMFILE)
1521     {
1522         load_options = DXFILELOAD_FROMFILE;
1523         TRACE("Loading from file %s\n", debugstr_a(filename));
1524     }
1525     else
1526     {
1527         FIXME("Load options %d not supported yet\n", loadflags);
1528         return E_NOTIMPL;
1529     }
1530
1531     hr = DirectXFileCreate(&pDXFile);
1532     if (hr != DXFILE_OK)
1533         goto end;
1534
1535     hr = IDirectXFile_RegisterTemplates(pDXFile, templates, strlen(templates));
1536     if (hr != DXFILE_OK)
1537         goto end;
1538
1539     hr = IDirectXFile_CreateEnumObject(pDXFile, filename, load_options, &pEnumObject);
1540     if (hr != DXFILE_OK)
1541         goto end;
1542
1543     hr = IDirectXFileEnumObject_GetNextDataObject(pEnumObject, &pData);
1544     if (hr != DXFILE_OK)
1545         goto end;
1546
1547     hr = IDirectXFileData_GetType(pData, &pGuid);
1548     if (hr != DXFILE_OK)
1549         goto end;
1550
1551     TRACE("Found object type whose GUID = %s\n", debugstr_guid(pGuid));
1552
1553     if (!IsEqualGUID(pGuid, &TID_DXFILEHeader))
1554     {
1555         ret = D3DRMERR_BADFILE;
1556         goto end;
1557     }
1558
1559     hr = IDirectXFileData_GetData(pData, NULL, &size, (void**)&pHeader);
1560     if ((hr != DXFILE_OK) || (size != sizeof(Header)))
1561         goto end;
1562
1563     TRACE("Version is %d %d %d\n", pHeader->major, pHeader->minor, pHeader->flags);
1564
1565     /* Version must be 1.0.x */
1566     if ((pHeader->major != 1) || (pHeader->minor != 0))
1567     {
1568         ret = D3DRMERR_BADFILE;
1569         goto end;
1570     }
1571
1572     IDirectXFileData_Release(pData);
1573     pData = NULL;
1574
1575     hr = IDirectXFileEnumObject_GetNextDataObject(pEnumObject, &pData);
1576     if (hr != DXFILE_OK)
1577     {
1578         ret = D3DRMERR_NOTFOUND;
1579         goto end;
1580     }
1581
1582     hr = IDirectXFileData_GetType(pData, &pGuid);
1583     if (hr != DXFILE_OK)
1584         goto end;
1585
1586     TRACE("Found object type whose GUID = %s\n", debugstr_guid(pGuid));
1587
1588     if (!IsEqualGUID(pGuid, &TID_D3DRMMesh))
1589     {
1590         ret = D3DRMERR_NOTFOUND;
1591         goto end;
1592     }
1593
1594     hr = load_mesh_data(iface, pData);
1595     if (hr == S_OK)
1596         ret = D3DRM_OK;
1597
1598 end:
1599
1600     if (pData2)
1601         IDirectXFileData_Release(pData2);
1602     if (pData)
1603         IDirectXFileData_Release(pData);
1604     if (pEnumObject)
1605         IDirectXFileEnumObject_Release(pEnumObject);
1606     if (pDXFile)
1607         IDirectXFile_Release(pDXFile);
1608
1609     if (ret != D3DRM_OK)
1610     {
1611         /* Clean mesh data */
1612         This->nb_vertices = 0;
1613         This->nb_normals = 0;
1614         This->nb_faces = 0;
1615         This->face_data_size = 0;
1616         This->nb_coords2d = 0;
1617         HeapFree(GetProcessHeap(), 0, This->pVertices);
1618         This->pVertices = NULL;
1619         HeapFree(GetProcessHeap(), 0, This->pNormals);
1620         This->pNormals = NULL;
1621         HeapFree(GetProcessHeap(), 0, This->pFaceData);
1622         This->pFaceData = NULL;
1623         HeapFree(GetProcessHeap(), 0, This->pCoords2d);
1624         This->pCoords2d = NULL;
1625     }
1626
1627     return ret;
1628 }
1629
1630 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Save(IDirect3DRMMeshBuilder3* iface,
1631                                                        const char* filename, D3DRMXOFFORMAT format,
1632                                                        D3DRMSAVEOPTIONS save)
1633 {
1634     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1635
1636     FIXME("(%p)->(%s,%d,%d): stub\n", This, filename, format, save);
1637
1638     return E_NOTIMPL;
1639 }
1640
1641 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Scale(IDirect3DRMMeshBuilder3* iface,
1642                                                         D3DVALUE sx, D3DVALUE sy, D3DVALUE sz)
1643 {
1644     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1645     int i;
1646
1647     TRACE("(%p)->(%f,%f,%f)\n", This, sx, sy, sz);
1648
1649     for (i = 0; i < This->nb_vertices; i++)
1650     {
1651         This->pVertices[i].u1.x *= sx;
1652         This->pVertices[i].u2.y *= sy;
1653         This->pVertices[i].u3.z *= sz;
1654     }
1655
1656     /* Normals are not affected by Scale */
1657
1658     return D3DRM_OK;
1659 }
1660
1661 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Translate(IDirect3DRMMeshBuilder3* iface,
1662                                                             D3DVALUE tx, D3DVALUE ty, D3DVALUE tz)
1663 {
1664     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1665
1666     FIXME("(%p)->(%f,%f,%f): stub\n", This, tx, ty, tz);
1667
1668     return E_NOTIMPL;
1669 }
1670
1671 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetColorSource(IDirect3DRMMeshBuilder3* iface,
1672                                                                  D3DRMCOLORSOURCE color)
1673 {
1674     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1675
1676     FIXME("(%p)->(%x): stub\n", This, color);
1677
1678     return E_NOTIMPL;
1679 }
1680
1681 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetBox(IDirect3DRMMeshBuilder3* iface,
1682                                                          D3DRMBOX* box)
1683 {
1684     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1685
1686     FIXME("(%p)->(%p): stub\n", This, box);
1687
1688     return E_NOTIMPL;
1689 }
1690
1691 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GenerateNormals(IDirect3DRMMeshBuilder3* iface,
1692                                                                   D3DVALUE crease, DWORD flags)
1693 {
1694     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1695
1696     FIXME("(%p)->(%f,%u): stub\n", This, crease, flags);
1697
1698     return E_NOTIMPL;
1699 }
1700
1701 static D3DRMCOLORSOURCE WINAPI IDirect3DRMMeshBuilder3Impl_GetColorSource(IDirect3DRMMeshBuilder3* iface)
1702 {
1703     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1704
1705     FIXME("(%p)->(): stub\n", This);
1706
1707     return E_NOTIMPL;
1708 }
1709
1710 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddMesh(IDirect3DRMMeshBuilder3* iface,
1711                                                           LPDIRECT3DRMMESH Mesh)
1712 {
1713     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1714
1715     FIXME("(%p)->(%p): stub\n", This, Mesh);
1716
1717     return E_NOTIMPL;
1718 }
1719
1720 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddMeshBuilder(IDirect3DRMMeshBuilder3* iface,
1721                                                                  LPDIRECT3DRMMESHBUILDER3 MeshBuilder,
1722                                                                  DWORD flags)
1723 {
1724     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1725
1726     FIXME("(%p)->(%p,%u): stub\n", This, MeshBuilder, flags);
1727
1728     return E_NOTIMPL;
1729 }
1730
1731 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddFrame(IDirect3DRMMeshBuilder3* iface,
1732                                                            LPDIRECT3DRMFRAME3 Frame)
1733 {
1734     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1735
1736     FIXME("(%p)->(%p): stub\n", This, Frame);
1737
1738     return E_NOTIMPL;
1739 }
1740
1741 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddFace(IDirect3DRMMeshBuilder3* iface,
1742                                                           LPDIRECT3DRMFACE2 Face)
1743 {
1744     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1745
1746     FIXME("(%p)->(%p): stub\n", This, Face);
1747
1748     return E_NOTIMPL;
1749 }
1750
1751 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddFaces(IDirect3DRMMeshBuilder3* iface,
1752                                                            DWORD vcount, D3DVECTOR* vertices,
1753                                                            DWORD ncount, D3DVECTOR* normals,
1754                                                            DWORD* data,
1755                                                            LPDIRECT3DRMFACEARRAY* FaceArray)
1756 {
1757     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1758
1759     FIXME("(%p)->(%d,%p,%d,%p,%p,%p): stub\n", This, vcount, vertices, ncount,
1760           normals, data, FaceArray);
1761
1762     return E_NOTIMPL;
1763 }
1764
1765 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_ReserveSpace(IDirect3DRMMeshBuilder3* iface,
1766                                                                DWORD vertex_Count,
1767                                                                DWORD normal_count,
1768                                                                DWORD face_count)
1769 {
1770     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1771
1772     FIXME("(%p)->(%d,%d,%d): stub\n", This, vertex_Count, normal_count, face_count);
1773
1774     return E_NOTIMPL;
1775 }
1776
1777 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetColorRGB(IDirect3DRMMeshBuilder3* iface,
1778                                                               D3DVALUE red, D3DVALUE green,
1779                                                               D3DVALUE blue)
1780 {
1781     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1782
1783     TRACE("(%p)->(%f,%f,%f)\n", This, red, green, blue);
1784
1785     This->color = D3DCOLOR_ARGB(0xff, (BYTE)(red   * 255.0f),
1786                                       (BYTE)(green * 255.0f),
1787                                       (BYTE)(blue  * 255.0f));
1788
1789     return D3DRM_OK;
1790 }
1791
1792 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetColor(IDirect3DRMMeshBuilder3* iface,
1793                                                            D3DCOLOR color)
1794 {
1795     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1796
1797     TRACE("(%p)->(%x)\n", This, color);
1798
1799     This->color = color;
1800
1801     return D3DRM_OK;
1802 }
1803
1804 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetTexture(IDirect3DRMMeshBuilder3* iface,
1805                                                              LPDIRECT3DRMTEXTURE3 texture)
1806 {
1807     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1808
1809     TRACE("(%p)->(%p)\n", This, texture);
1810
1811     if (texture)
1812         IDirect3DRMTexture3_AddRef(texture);
1813     if (This->texture)
1814         IDirect3DRMTexture3_Release(This->texture);
1815     This->texture = texture;
1816
1817     return D3DRM_OK;
1818 }
1819
1820 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetMaterial(IDirect3DRMMeshBuilder3* iface,
1821                                                               LPDIRECT3DRMMATERIAL2 material)
1822 {
1823     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1824
1825     TRACE("(%p)->(%p)\n", This, material);
1826
1827     if (material)
1828         IDirect3DRMTexture2_AddRef(material);
1829     if (This->material)
1830         IDirect3DRMTexture2_Release(This->material);
1831     This->material = material;
1832
1833     return D3DRM_OK;
1834 }
1835
1836 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetTextureTopology(IDirect3DRMMeshBuilder3* iface,
1837                                                                      BOOL wrap_u, BOOL wrap_v)
1838 {
1839     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1840
1841     FIXME("(%p)->(%d,%d): stub\n", This, wrap_u, wrap_v);
1842
1843     return E_NOTIMPL;
1844 }
1845
1846 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetQuality(IDirect3DRMMeshBuilder3* iface,
1847                                                              D3DRMRENDERQUALITY quality)
1848 {
1849     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1850
1851     FIXME("(%p)->(%d): stub\n", This, quality);
1852
1853     return E_NOTIMPL;
1854 }
1855
1856 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetPerspective(IDirect3DRMMeshBuilder3* iface,
1857                                                                  BOOL enable)
1858 {
1859     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1860
1861     FIXME("(%p)->(%d): stub\n", This, enable);
1862
1863     return E_NOTIMPL;
1864 }
1865
1866 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetVertex(IDirect3DRMMeshBuilder3* iface,
1867                                                             DWORD index,
1868                                                             D3DVALUE x, D3DVALUE y, D3DVALUE z)
1869 {
1870     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1871
1872     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
1873
1874     return E_NOTIMPL;
1875 }
1876
1877 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetNormal(IDirect3DRMMeshBuilder3* iface,
1878                                                             DWORD index,
1879                                                             D3DVALUE x, D3DVALUE y, D3DVALUE z)
1880 {
1881     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1882
1883     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
1884
1885     return E_NOTIMPL;
1886 }
1887
1888 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetTextureCoordinates(IDirect3DRMMeshBuilder3* iface,
1889                                                                         DWORD index, D3DVALUE u,
1890                                                                         D3DVALUE v)
1891 {
1892     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1893
1894     TRACE("(%p)->(%u,%f,%f)\n", This, index, u, v);
1895
1896     if (index >= This->nb_coords2d)
1897         return D3DRMERR_BADVALUE;
1898
1899     This->pCoords2d[index].u = u;
1900     This->pCoords2d[index].v = v;
1901
1902     return D3DRM_OK;
1903 }
1904
1905 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetVertexColor(IDirect3DRMMeshBuilder3* iface,
1906                                                                  DWORD index, D3DCOLOR color)
1907 {
1908     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1909
1910     FIXME("(%p)->(%d,%x): stub\n", This, index, color);
1911
1912     return E_NOTIMPL;
1913 }
1914
1915 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetVertexColorRGB(IDirect3DRMMeshBuilder3* iface,
1916                                                                     DWORD index,
1917                                                                     D3DVALUE red, D3DVALUE green,
1918                                                                     D3DVALUE blue)
1919 {
1920     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1921
1922     FIXME("(%p)->(%d,%f,%f,%f): stub\n", This, index, red, green, blue);
1923
1924     return E_NOTIMPL;
1925 }
1926
1927 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetFaces(IDirect3DRMMeshBuilder3* iface,
1928                                                            LPDIRECT3DRMFACEARRAY* FaceArray)
1929 {
1930     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1931
1932     FIXME("(%p)->(%p): stub\n", This, FaceArray);
1933
1934     return E_NOTIMPL;
1935 }
1936
1937 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetGeometry(IDirect3DRMMeshBuilder3* iface,
1938                                                               DWORD* vcount, D3DVECTOR* vertices,
1939                                                               DWORD* ncount, D3DVECTOR* normals,
1940                                                               DWORD* face_data_size,
1941                                                               DWORD* face_data)
1942 {
1943     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1944
1945     FIXME("(%p)->(%p,%p,%p,%p,%p,%p): stub\n", This, vcount, vertices, ncount, normals,
1946           face_data_size, face_data);
1947
1948     return E_NOTIMPL;
1949 }
1950
1951 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetTextureCoordinates(IDirect3DRMMeshBuilder3* iface,
1952                                                                         DWORD index, D3DVALUE* u,
1953                                                                         D3DVALUE* v)
1954 {
1955     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1956
1957     TRACE("(%p)->(%d,%p,%p)\n", This, index, u, v);
1958
1959     if (index >= This->nb_coords2d)
1960         return D3DRMERR_BADVALUE;
1961
1962     *u = This->pCoords2d[index].u;
1963     *v = This->pCoords2d[index].v;
1964
1965     return D3DRM_OK;
1966 }
1967
1968
1969 static int WINAPI IDirect3DRMMeshBuilder3Impl_AddVertex(IDirect3DRMMeshBuilder3* iface,
1970                                                         D3DVALUE x, D3DVALUE y, D3DVALUE z)
1971 {
1972     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1973
1974     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
1975
1976     return 0;
1977 }
1978
1979 static int WINAPI IDirect3DRMMeshBuilder3Impl_AddNormal(IDirect3DRMMeshBuilder3* iface,
1980                                                         D3DVALUE x, D3DVALUE y, D3DVALUE z)
1981 {
1982     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1983
1984     FIXME("(%p)->(%f,%f,%f): stub\n", This, x, y, z);
1985
1986     return 0;
1987 }
1988
1989 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_CreateFace(IDirect3DRMMeshBuilder3* iface,
1990                                                              LPDIRECT3DRMFACE2* Face)
1991 {
1992     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1993
1994     FIXME("(%p)->(%p): stub\n", This, Face);
1995
1996     return E_NOTIMPL;
1997 }
1998
1999 static D3DRMRENDERQUALITY WINAPI IDirect3DRMMeshBuilder3Impl_GetQuality(IDirect3DRMMeshBuilder3* iface)
2000 {
2001     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2002
2003     FIXME("(%p)->(): stub\n", This);
2004
2005     return 0;
2006 }
2007
2008 static BOOL WINAPI IDirect3DRMMeshBuilder3Impl_GetPerspective(IDirect3DRMMeshBuilder3* iface)
2009 {
2010     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2011
2012     FIXME("(%p)->(): stub\n", This);
2013
2014     return FALSE;
2015 }
2016
2017 static int WINAPI IDirect3DRMMeshBuilder3Impl_GetFaceCount(IDirect3DRMMeshBuilder3* iface)
2018 {
2019     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2020
2021     TRACE("(%p)->()\n", This);
2022
2023     return This->nb_faces;
2024 }
2025
2026 static int WINAPI IDirect3DRMMeshBuilder3Impl_GetVertexCount(IDirect3DRMMeshBuilder3* iface)
2027 {
2028     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2029
2030     TRACE("(%p)->()\n", This);
2031
2032     return This->nb_vertices;
2033 }
2034
2035 static D3DCOLOR WINAPI IDirect3DRMMeshBuilder3Impl_GetVertexColor(IDirect3DRMMeshBuilder3* iface,
2036                                                                   DWORD index)
2037 {
2038     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2039
2040     FIXME("(%p)->(%d): stub\n", This, index);
2041
2042     return 0;
2043 }
2044
2045 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_CreateMesh(IDirect3DRMMeshBuilder3* iface,
2046                                                              LPDIRECT3DRMMESH* mesh)
2047 {
2048     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2049     HRESULT hr;
2050     D3DRMGROUPINDEX group;
2051
2052     TRACE("(%p)->(%p)\n", This, mesh);
2053
2054     if (!mesh)
2055         return E_POINTER;
2056
2057     hr = Direct3DRMMesh_create(mesh);
2058     if (FAILED(hr))
2059         return hr;
2060
2061     /* If there is mesh data, create a group and put data inside */
2062     if (This->nb_vertices)
2063     {
2064         unsigned* face_data;
2065         unsigned* out_ptr;
2066         DWORD* in_ptr = This->pFaceData;
2067         int i, j;
2068
2069         face_data = HeapAlloc(GetProcessHeap(), 0, This->face_data_size * sizeof(DWORD));
2070         if (!face_data)
2071         {
2072             IDirect3DRMMesh_Release(*mesh);
2073             return E_OUTOFMEMORY;
2074         }
2075         out_ptr = face_data;
2076
2077         /* Put only vertex indices */
2078         for (i = 0; i < This->nb_faces; i++)
2079         {
2080             DWORD nb_indices = *out_ptr++ = *in_ptr++;
2081             for (j = 0; j < nb_indices; j++)
2082             {
2083                 *out_ptr++ = *in_ptr++;
2084                 /* Skip normal index */
2085                 in_ptr++;
2086             }
2087         }
2088
2089         hr = IDirect3DRMMesh_AddGroup(*mesh, This->nb_vertices, This->nb_faces, 0, face_data, &group);
2090         HeapFree(GetProcessHeap(), 0, face_data);
2091         if (SUCCEEDED(hr))
2092         {
2093             D3DRMVERTEX* vertices;
2094
2095             vertices = HeapAlloc(GetProcessHeap(), 0, This->nb_vertices * sizeof(D3DRMVERTEX));
2096             if (vertices)
2097             {
2098                 for (i = 0; i < This->nb_vertices; i++)
2099                 {
2100                     vertices[i].position = This->pVertices[i];
2101                 }
2102                 hr = IDirect3DRMMesh_SetVertices(*mesh, 0, 0, This->nb_vertices, vertices);
2103                 HeapFree(GetProcessHeap(), 0, vertices);
2104             }
2105             else
2106             {
2107                 hr = E_OUTOFMEMORY;
2108             }
2109         }
2110         if (SUCCEEDED(hr))
2111             hr = IDirect3DRMMesh_SetGroupColor(*mesh, 0, This->color);
2112         if (SUCCEEDED(hr))
2113             hr = IDirect3DRMMesh_SetGroupMaterial(*mesh, 0, (LPDIRECT3DRMMATERIAL)This->material);
2114         if (SUCCEEDED(hr) && This->texture)
2115         {
2116              LPDIRECT3DRMTEXTURE texture;
2117
2118              IDirect3DRMTexture3_QueryInterface(This->texture, &IID_IDirect3DRMTexture, (LPVOID*)&texture);
2119              hr = IDirect3DRMMesh_SetGroupTexture(*mesh, 0, texture);
2120              IDirect3DRMTexture_Release(texture);
2121         }
2122         if (FAILED(hr))
2123             IDirect3DRMMesh_Release(*mesh);
2124     }
2125
2126     return hr;
2127 }
2128
2129 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetFace(IDirect3DRMMeshBuilder3* iface,
2130                                                           DWORD index, LPDIRECT3DRMFACE2* Face)
2131 {
2132     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2133
2134     FIXME("(%p)->(%u,%p): stub\n", This, index, Face);
2135
2136     return E_NOTIMPL;
2137 }
2138
2139 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetVertex(IDirect3DRMMeshBuilder3* iface,
2140                                                             DWORD index, LPD3DVECTOR vector)
2141 {
2142     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2143
2144     FIXME("(%p)->(%u,%p): stub\n", This, index, vector);
2145
2146     return E_NOTIMPL;
2147 }
2148
2149 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetNormal(IDirect3DRMMeshBuilder3* iface,
2150                                                             DWORD index, LPD3DVECTOR vector)
2151 {
2152     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2153
2154     FIXME("(%p)->(%u,%p): stub\n", This, index, vector);
2155
2156     return E_NOTIMPL;
2157 }
2158
2159 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteVertices(IDirect3DRMMeshBuilder3* iface,
2160                                                                  DWORD IndexFirst, DWORD count)
2161 {
2162     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2163
2164     FIXME("(%p)->(%u,%u): stub\n", This, IndexFirst, count);
2165
2166     return E_NOTIMPL;
2167 }
2168
2169 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteNormals(IDirect3DRMMeshBuilder3* iface,
2170                                                                 DWORD IndexFirst, DWORD count)
2171 {
2172     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2173
2174     FIXME("(%p)->(%u,%u): stub\n", This, IndexFirst, count);
2175
2176     return E_NOTIMPL;
2177 }
2178
2179 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteFace(IDirect3DRMMeshBuilder3* iface,
2180                                                              LPDIRECT3DRMFACE2 Face)
2181 {
2182     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2183
2184     FIXME("(%p)->(%p): stub\n", This, Face);
2185
2186     return E_NOTIMPL;
2187 }
2188
2189 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Empty(IDirect3DRMMeshBuilder3* iface, DWORD flags)
2190 {
2191     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2192
2193     FIXME("(%p)->(%u): stub\n", This, flags);
2194
2195     return E_NOTIMPL;
2196 }
2197
2198 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Optimize(IDirect3DRMMeshBuilder3* iface,
2199                                                            DWORD flags)
2200 {
2201     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2202
2203     FIXME("(%p)->(%u): stub\n", This, flags);
2204
2205     return E_NOTIMPL;
2206 }
2207
2208 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddFacesIndexed(IDirect3DRMMeshBuilder3* iface,
2209                                                                   DWORD flags, DWORD* indices,
2210                                                                   DWORD* IndexFirst, DWORD* count)
2211 {
2212     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2213
2214     FIXME("(%p)->(%u,%p,%p,%p): stub\n", This, flags, indices, IndexFirst, count);
2215
2216     return E_NOTIMPL;
2217 }
2218
2219 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_CreateSubMesh(IDirect3DRMMeshBuilder3* iface,
2220                                                                 LPUNKNOWN *unkwn)
2221 {
2222     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2223
2224     FIXME("(%p)->(%p): stub\n", This, unkwn);
2225
2226     return E_NOTIMPL;
2227 }
2228
2229 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetParentMesh(IDirect3DRMMeshBuilder3* iface,
2230                                                                 DWORD flags, LPUNKNOWN *unkwn)
2231 {
2232     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2233
2234     FIXME("(%p)->(%u,%p): stub\n", This, flags, unkwn);
2235
2236     return E_NOTIMPL;
2237 }
2238
2239 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetSubMeshes(IDirect3DRMMeshBuilder3* iface,
2240                                                                LPDWORD count, LPUNKNOWN *unkwn)
2241 {
2242     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2243
2244     FIXME("(%p)->(%p,%p): stub\n", This, count, unkwn);
2245
2246     return E_NOTIMPL;
2247 }
2248
2249 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_DeleteSubMesh(IDirect3DRMMeshBuilder3* iface,
2250                                                                 LPUNKNOWN unkwn)
2251 {
2252     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2253
2254     FIXME("(%p)->(%p): stub\n", This, unkwn);
2255
2256     return E_NOTIMPL;
2257 }
2258
2259 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_Enable(IDirect3DRMMeshBuilder3* iface,
2260                                                          DWORD index)
2261 {
2262     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2263
2264     FIXME("(%p)->(%u): stub\n", This, index);
2265
2266     return E_NOTIMPL;
2267 }
2268
2269 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetEnable(IDirect3DRMMeshBuilder3* iface,
2270                                                             DWORD* indices)
2271 {
2272     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2273
2274     FIXME("(%p)->(%p): stub\n", This, indices);
2275
2276     return E_NOTIMPL;
2277 }
2278
2279 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_AddTriangles(IDirect3DRMMeshBuilder3* iface,
2280                                                                DWORD flags, DWORD format,
2281                                                                DWORD VertexCount, LPVOID data)
2282 {
2283     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2284
2285     FIXME("(%p)->(%u,%u,%u,%p): stub\n", This, flags, format, VertexCount, data);
2286
2287     return E_NOTIMPL;
2288 }
2289
2290 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetVertices(IDirect3DRMMeshBuilder3* iface,
2291                                                               DWORD IndexFirst, DWORD count,
2292                                                               LPD3DVECTOR vector)
2293 {
2294     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2295
2296     FIXME("(%p)->(%u,%u,%p): stub\n", This, IndexFirst, count, vector);
2297
2298     return E_NOTIMPL;
2299 }
2300
2301 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetVertices(IDirect3DRMMeshBuilder3* iface,
2302                                                               DWORD IndexFirst, LPDWORD vcount,
2303                                                               LPD3DVECTOR vertices)
2304 {
2305     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2306     DWORD count = This->nb_vertices - IndexFirst;
2307
2308     TRACE("(%p)->(%u,%p,%p)\n", This, IndexFirst, vcount, vertices);
2309
2310     if (vcount)
2311         *vcount = count;
2312     if (vertices && This->nb_vertices)
2313         memcpy(vertices, This->pVertices + IndexFirst, count * sizeof(D3DVECTOR));
2314
2315     return D3DRM_OK;
2316 }
2317
2318 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_SetNormals(IDirect3DRMMeshBuilder3* iface,
2319                                                              DWORD IndexFirst, DWORD count,
2320                                                              LPD3DVECTOR vector)
2321 {
2322     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2323
2324     FIXME("(%p)->(%u,%u,%p): stub\n", This, IndexFirst, count, vector);
2325
2326     return E_NOTIMPL;
2327 }
2328
2329 static HRESULT WINAPI IDirect3DRMMeshBuilder3Impl_GetNormals(IDirect3DRMMeshBuilder3* iface,
2330                                                              DWORD IndexFirst, LPDWORD count,
2331                                                              LPD3DVECTOR vector)
2332 {
2333     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2334
2335     FIXME("(%p)->(%u,%p,%p): stub\n", This, IndexFirst, count, vector);
2336
2337     return E_NOTIMPL;
2338 }
2339
2340 static int WINAPI IDirect3DRMMeshBuilder3Impl_GetNormalCount(IDirect3DRMMeshBuilder3* iface)
2341 {
2342     IDirect3DRMMeshBuilderImpl *This = impl_from_IDirect3DRMMeshBuilder3(iface);
2343
2344     FIXME("(%p)->(): stub\n", This);
2345
2346     return E_NOTIMPL;
2347 }
2348
2349 static const struct IDirect3DRMMeshBuilder3Vtbl Direct3DRMMeshBuilder3_Vtbl =
2350 {
2351     /*** IUnknown methods ***/
2352     IDirect3DRMMeshBuilder3Impl_QueryInterface,
2353     IDirect3DRMMeshBuilder3Impl_AddRef,
2354     IDirect3DRMMeshBuilder3Impl_Release,
2355     /*** IDirect3DRMObject methods ***/
2356     IDirect3DRMMeshBuilder3Impl_Clone,
2357     IDirect3DRMMeshBuilder3Impl_AddDestroyCallback,
2358     IDirect3DRMMeshBuilder3Impl_DeleteDestroyCallback,
2359     IDirect3DRMMeshBuilder3Impl_SetAppData,
2360     IDirect3DRMMeshBuilder3Impl_GetAppData,
2361     IDirect3DRMMeshBuilder3Impl_SetName,
2362     IDirect3DRMMeshBuilder3Impl_GetName,
2363     IDirect3DRMMeshBuilder3Impl_GetClassName,
2364     /*** IDirect3DRMMeshBuilder3 methods ***/
2365     IDirect3DRMMeshBuilder3Impl_Load,
2366     IDirect3DRMMeshBuilder3Impl_Save,
2367     IDirect3DRMMeshBuilder3Impl_Scale,
2368     IDirect3DRMMeshBuilder3Impl_Translate,
2369     IDirect3DRMMeshBuilder3Impl_SetColorSource,
2370     IDirect3DRMMeshBuilder3Impl_GetBox,
2371     IDirect3DRMMeshBuilder3Impl_GenerateNormals,
2372     IDirect3DRMMeshBuilder3Impl_GetColorSource,
2373     IDirect3DRMMeshBuilder3Impl_AddMesh,
2374     IDirect3DRMMeshBuilder3Impl_AddMeshBuilder,
2375     IDirect3DRMMeshBuilder3Impl_AddFrame,
2376     IDirect3DRMMeshBuilder3Impl_AddFace,
2377     IDirect3DRMMeshBuilder3Impl_AddFaces,
2378     IDirect3DRMMeshBuilder3Impl_ReserveSpace,
2379     IDirect3DRMMeshBuilder3Impl_SetColorRGB,
2380     IDirect3DRMMeshBuilder3Impl_SetColor,
2381     IDirect3DRMMeshBuilder3Impl_SetTexture,
2382     IDirect3DRMMeshBuilder3Impl_SetMaterial,
2383     IDirect3DRMMeshBuilder3Impl_SetTextureTopology,
2384     IDirect3DRMMeshBuilder3Impl_SetQuality,
2385     IDirect3DRMMeshBuilder3Impl_SetPerspective,
2386     IDirect3DRMMeshBuilder3Impl_SetVertex,
2387     IDirect3DRMMeshBuilder3Impl_SetNormal,
2388     IDirect3DRMMeshBuilder3Impl_SetTextureCoordinates,
2389     IDirect3DRMMeshBuilder3Impl_SetVertexColor,
2390     IDirect3DRMMeshBuilder3Impl_SetVertexColorRGB,
2391     IDirect3DRMMeshBuilder3Impl_GetFaces,
2392     IDirect3DRMMeshBuilder3Impl_GetGeometry,
2393     IDirect3DRMMeshBuilder3Impl_GetTextureCoordinates,
2394     IDirect3DRMMeshBuilder3Impl_AddVertex,
2395     IDirect3DRMMeshBuilder3Impl_AddNormal,
2396     IDirect3DRMMeshBuilder3Impl_CreateFace,
2397     IDirect3DRMMeshBuilder3Impl_GetQuality,
2398     IDirect3DRMMeshBuilder3Impl_GetPerspective,
2399     IDirect3DRMMeshBuilder3Impl_GetFaceCount,
2400     IDirect3DRMMeshBuilder3Impl_GetVertexCount,
2401     IDirect3DRMMeshBuilder3Impl_GetVertexColor,
2402     IDirect3DRMMeshBuilder3Impl_CreateMesh,
2403     IDirect3DRMMeshBuilder3Impl_GetFace,
2404     IDirect3DRMMeshBuilder3Impl_GetVertex,
2405     IDirect3DRMMeshBuilder3Impl_GetNormal,
2406     IDirect3DRMMeshBuilder3Impl_DeleteVertices,
2407     IDirect3DRMMeshBuilder3Impl_DeleteNormals,
2408     IDirect3DRMMeshBuilder3Impl_DeleteFace,
2409     IDirect3DRMMeshBuilder3Impl_Empty,
2410     IDirect3DRMMeshBuilder3Impl_Optimize,
2411     IDirect3DRMMeshBuilder3Impl_AddFacesIndexed,
2412     IDirect3DRMMeshBuilder3Impl_CreateSubMesh,
2413     IDirect3DRMMeshBuilder3Impl_GetParentMesh,
2414     IDirect3DRMMeshBuilder3Impl_GetSubMeshes,
2415     IDirect3DRMMeshBuilder3Impl_DeleteSubMesh,
2416     IDirect3DRMMeshBuilder3Impl_Enable,
2417     IDirect3DRMMeshBuilder3Impl_GetEnable,
2418     IDirect3DRMMeshBuilder3Impl_AddTriangles,
2419     IDirect3DRMMeshBuilder3Impl_SetVertices,
2420     IDirect3DRMMeshBuilder3Impl_GetVertices,
2421     IDirect3DRMMeshBuilder3Impl_SetNormals,
2422     IDirect3DRMMeshBuilder3Impl_GetNormals,
2423     IDirect3DRMMeshBuilder3Impl_GetNormalCount
2424 };
2425
2426 HRESULT Direct3DRMMeshBuilder_create(REFIID riid, IUnknown** ppObj)
2427 {
2428     IDirect3DRMMeshBuilderImpl* object;
2429
2430     TRACE("(%p)\n", ppObj);
2431
2432     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DRMMeshBuilderImpl));
2433     if (!object)
2434     {
2435         ERR("Out of memory\n");
2436         return E_OUTOFMEMORY;
2437     }
2438
2439     object->IDirect3DRMMeshBuilder2_iface.lpVtbl = &Direct3DRMMeshBuilder2_Vtbl;
2440     object->IDirect3DRMMeshBuilder3_iface.lpVtbl = &Direct3DRMMeshBuilder3_Vtbl;
2441     object->ref = 1;
2442
2443     if (IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder3))
2444         *ppObj = (IUnknown*)&object->IDirect3DRMMeshBuilder3_iface;
2445     else
2446         *ppObj = (IUnknown*)&object->IDirect3DRMMeshBuilder2_iface;
2447
2448     return S_OK;
2449 }
2450
2451 /*** IUnknown methods ***/
2452 static HRESULT WINAPI IDirect3DRMMeshImpl_QueryInterface(IDirect3DRMMesh* iface,
2453                                                          REFIID riid, void** ppvObject)
2454 {
2455     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2456
2457     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), ppvObject);
2458
2459     *ppvObject = NULL;
2460
2461     if (IsEqualGUID(riid, &IID_IUnknown) ||
2462         IsEqualGUID(riid, &IID_IDirect3DRMMesh))
2463     {
2464         *ppvObject = &This->IDirect3DRMMesh_iface;
2465     }
2466     else
2467     {
2468         FIXME("interface %s not implemented\n", debugstr_guid(riid));
2469         return E_NOINTERFACE;
2470     }
2471
2472     IDirect3DRMMesh_AddRef(iface);
2473     return S_OK;
2474 }
2475
2476 static ULONG WINAPI IDirect3DRMMeshImpl_AddRef(IDirect3DRMMesh* iface)
2477 {
2478     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2479     ULONG ref = InterlockedIncrement(&This->ref);
2480
2481     TRACE("(%p)->(): new ref = %d\n", This, ref);
2482
2483     return ref;
2484 }
2485
2486 static ULONG WINAPI IDirect3DRMMeshImpl_Release(IDirect3DRMMesh* iface)
2487 {
2488     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2489     ULONG ref = InterlockedDecrement(&This->ref);
2490
2491     TRACE("(%p)->(): new ref = %d\n", This, ref);
2492
2493     if (!ref)
2494     {
2495         int i;
2496
2497         for (i = 0; i < This->nb_groups; i++)
2498         {
2499             HeapFree(GetProcessHeap(), 0, This->groups[i].vertices);
2500             HeapFree(GetProcessHeap(), 0, This->groups[i].face_data);
2501             if (This->groups[i].material)
2502                 IDirect3DRMMaterial2_Release(This->groups[i].material);
2503             if (This->groups[i].texture)
2504                 IDirect3DRMTexture3_Release(This->groups[i].texture);
2505         }
2506         HeapFree(GetProcessHeap(), 0, This->groups);
2507         HeapFree(GetProcessHeap(), 0, This);
2508     }
2509
2510     return ref;
2511 }
2512
2513 /*** IDirect3DRMObject methods ***/
2514 static HRESULT WINAPI IDirect3DRMMeshImpl_Clone(IDirect3DRMMesh* iface,
2515                                                 LPUNKNOWN pUnkOuter, REFIID riid,
2516                                                 LPVOID *ppvObj)
2517 {
2518     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2519
2520     FIXME("(%p)->(%p,%s,%p): stub\n", This, pUnkOuter, debugstr_guid(riid), ppvObj);
2521
2522     return E_NOTIMPL;
2523 }
2524
2525 static HRESULT WINAPI IDirect3DRMMeshImpl_AddDestroyCallback(IDirect3DRMMesh* iface,
2526                                                              D3DRMOBJECTCALLBACK cb,
2527                                                              LPVOID argument)
2528 {
2529     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2530
2531     FIXME("(%p)->(%p,%p): stub\n", This, cb, argument);
2532
2533     return E_NOTIMPL;
2534 }
2535
2536 static HRESULT WINAPI IDirect3DRMMeshImpl_DeleteDestroyCallback(IDirect3DRMMesh* iface,
2537                                                                  D3DRMOBJECTCALLBACK cb,
2538                                                                  LPVOID argument)
2539 {
2540     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2541
2542     FIXME("(%p)->(%p,%p): stub\n", This, cb, argument);
2543
2544     return E_NOTIMPL;
2545 }
2546
2547 static HRESULT WINAPI IDirect3DRMMeshImpl_SetAppData(IDirect3DRMMesh* iface,
2548                                                      DWORD data)
2549 {
2550     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2551
2552     FIXME("(%p)->(%u): stub\n", This, data);
2553
2554     return E_NOTIMPL;
2555 }
2556
2557 static DWORD WINAPI IDirect3DRMMeshImpl_GetAppData(IDirect3DRMMesh* iface)
2558 {
2559     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2560
2561     FIXME("(%p)->(): stub\n", This);
2562
2563     return 0;
2564 }
2565
2566 static HRESULT WINAPI IDirect3DRMMeshImpl_SetName(IDirect3DRMMesh* iface,
2567                                                   LPCSTR pName)
2568 {
2569     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2570
2571     FIXME("(%p)->(%s): stub\n", This, pName);
2572
2573     return E_NOTIMPL;
2574 }
2575
2576 static HRESULT WINAPI IDirect3DRMMeshImpl_GetName(IDirect3DRMMesh* iface,
2577                                                   LPDWORD lpdwSize, LPSTR lpName)
2578 {
2579     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2580
2581     FIXME("(%p)->(%p,%p): stub\n", This, lpdwSize, lpName);
2582
2583     return E_NOTIMPL;
2584 }
2585
2586 static HRESULT WINAPI IDirect3DRMMeshImpl_GetClassName(IDirect3DRMMesh* iface,
2587                                                        LPDWORD size, LPSTR name)
2588 {
2589     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2590
2591     TRACE("(%p)->(%p, %p)\n", This, size, name);
2592
2593     if (!size || *size < strlen("Mesh") || !name)
2594         return E_INVALIDARG;
2595
2596     strcpy(name, "Mesh");
2597     *size = sizeof("Mesh");
2598
2599     return D3DRM_OK;
2600 }
2601
2602 /*** IDirect3DRMMesh methods ***/
2603 static HRESULT WINAPI IDirect3DRMMeshImpl_Scale(IDirect3DRMMesh* iface,
2604                                                 D3DVALUE sx, D3DVALUE sy, D3DVALUE sz)
2605 {
2606     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2607
2608     FIXME("(%p)->(%f,%f,%f): stub\n", This, sx, sy,sz);
2609
2610     return E_NOTIMPL;
2611 }
2612
2613 static HRESULT WINAPI IDirect3DRMMeshImpl_Translate(IDirect3DRMMesh* iface,
2614                                                     D3DVALUE tx, D3DVALUE ty, D3DVALUE tz)
2615 {
2616     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2617
2618     FIXME("(%p)->(%f,%f,%f): stub\n", This, tx, ty,tz);
2619
2620     return E_NOTIMPL;
2621 }
2622
2623 static HRESULT WINAPI IDirect3DRMMeshImpl_GetBox(IDirect3DRMMesh* iface,
2624                                                  D3DRMBOX * box)
2625 {
2626     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2627
2628     FIXME("(%p)->(%p): stub\n", This, box);
2629
2630     return E_NOTIMPL;
2631 }
2632
2633 static HRESULT WINAPI IDirect3DRMMeshImpl_AddGroup(IDirect3DRMMesh* iface,
2634                                                    unsigned vertex_count, unsigned face_count, unsigned vertex_per_face,
2635                                                    unsigned *face_data, D3DRMGROUPINDEX *return_id)
2636 {
2637     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2638     mesh_group* group;
2639
2640     TRACE("(%p)->(%u,%u,%u,%p,%p)\n", This, vertex_count, face_count, vertex_per_face, face_data, return_id);
2641
2642     if (!face_data || !return_id)
2643         return E_POINTER;
2644
2645     if ((This->nb_groups + 1) > This->groups_capacity)
2646     {
2647         ULONG new_capacity;
2648         mesh_group* groups;
2649
2650         if (!This->groups_capacity)
2651         {
2652             new_capacity = 16;
2653             groups = HeapAlloc(GetProcessHeap(), 0, new_capacity * sizeof(mesh_group));
2654         }
2655         else
2656         {
2657             new_capacity = This->groups_capacity * 2;
2658             groups = HeapReAlloc(GetProcessHeap(), 0, This->groups, new_capacity * sizeof(mesh_group));
2659         }
2660
2661         if (!groups)
2662             return E_OUTOFMEMORY;
2663
2664         This->groups_capacity = new_capacity;
2665         This->groups = groups;
2666     }
2667
2668     group = This->groups + This->nb_groups;
2669
2670     group->vertices = HeapAlloc(GetProcessHeap(), 0, vertex_count * sizeof(D3DRMVERTEX));
2671     if (!group->vertices)
2672         return E_OUTOFMEMORY;
2673     group->nb_vertices = vertex_count;
2674     group->nb_faces = face_count;
2675     group->vertex_per_face = vertex_per_face;
2676
2677     if (vertex_per_face)
2678     {
2679         group->face_data_size = face_count * vertex_per_face;
2680     }
2681     else
2682     {
2683         int i;
2684         unsigned nb_indices;
2685         unsigned* face_data_ptr = face_data;
2686         group->face_data_size = 0;
2687
2688         for (i = 0; i < face_count; i++)
2689         {
2690             nb_indices = *face_data_ptr;
2691             group->face_data_size += nb_indices + 1;
2692             face_data_ptr += nb_indices;
2693         }
2694     }
2695
2696     group->face_data = HeapAlloc(GetProcessHeap(), 0, group->face_data_size * sizeof(unsigned));
2697     if (!group->face_data)
2698     {
2699         HeapFree(GetProcessHeap(), 0 , group->vertices);
2700         return E_OUTOFMEMORY;
2701     }
2702
2703     memcpy(group->face_data, face_data, group->face_data_size * sizeof(unsigned));
2704
2705     group->material = NULL;
2706     group->texture = NULL;
2707
2708     *return_id = This->nb_groups++;
2709
2710     return D3DRM_OK;
2711 }
2712
2713 static HRESULT WINAPI IDirect3DRMMeshImpl_SetVertices(IDirect3DRMMesh* iface,
2714                                                       D3DRMGROUPINDEX id, unsigned index, unsigned count,
2715                                                       D3DRMVERTEX *values)
2716 {
2717     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2718
2719     TRACE("(%p)->(%u,%u,%u,%p)\n", This, id, index, count, values);
2720
2721     if (id >= This->nb_groups)
2722         return D3DRMERR_BADVALUE;
2723
2724     if ((index + count - 1) >= This->groups[id].nb_vertices)
2725         return D3DRMERR_BADVALUE;
2726
2727     if (!values)
2728         return E_POINTER;
2729
2730     memcpy(This->groups[id].vertices + index, values, count * sizeof(D3DRMVERTEX));
2731
2732     return D3DRM_OK;
2733 }
2734
2735 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupColor(IDirect3DRMMesh* iface,
2736                                                         D3DRMGROUPINDEX id, D3DCOLOR color)
2737 {
2738     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2739
2740     TRACE("(%p)->(%u,%x)\n", This, id, color);
2741
2742     if (id >= This->nb_groups)
2743         return D3DRMERR_BADVALUE;
2744
2745     This->groups[id].color = color;
2746
2747     return D3DRM_OK;
2748 }
2749
2750 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupColorRGB(IDirect3DRMMesh* iface,
2751                                                            D3DRMGROUPINDEX id, D3DVALUE red, D3DVALUE green, D3DVALUE blue)
2752 {
2753     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2754
2755     TRACE("(%p)->(%u,%f,%f,%f)\n", This, id, red, green, blue);
2756
2757     if (id >= This->nb_groups)
2758         return D3DRMERR_BADVALUE;
2759
2760     This->groups[id].color = D3DCOLOR_ARGB(0xff, (BYTE)(red   * 255.0f),
2761                                                  (BYTE)(green * 255.0f),
2762                                                  (BYTE)(blue  * 255.0f));
2763
2764     return D3DRM_OK;
2765 }
2766
2767 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupMapping(IDirect3DRMMesh* iface,
2768                                                           D3DRMGROUPINDEX id, D3DRMMAPPING value)
2769 {
2770     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2771
2772     FIXME("(%p)->(%u,%u): stub\n", This, id, value);
2773
2774     return E_NOTIMPL;
2775 }
2776
2777 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupQuality(IDirect3DRMMesh* iface,
2778                                                           D3DRMGROUPINDEX id, D3DRMRENDERQUALITY value)
2779 {
2780     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2781
2782     FIXME("(%p)->(%u,%u): stub\n", This, id, value);
2783
2784     return E_NOTIMPL;
2785 }
2786
2787 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupMaterial(IDirect3DRMMesh* iface,
2788                                                            D3DRMGROUPINDEX id, LPDIRECT3DRMMATERIAL material)
2789 {
2790     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2791
2792     TRACE("(%p)->(%u,%p)\n", This, id, material);
2793
2794     if (id >= This->nb_groups)
2795         return D3DRMERR_BADVALUE;
2796
2797     if (This->groups[id].material)
2798         IDirect3DRMMaterial2_Release(This->groups[id].material);
2799
2800     This->groups[id].material = (LPDIRECT3DRMMATERIAL2)material;
2801
2802     if (material)
2803         IDirect3DRMMaterial2_AddRef(This->groups[id].material);
2804
2805     return D3DRM_OK;
2806 }
2807
2808 static HRESULT WINAPI IDirect3DRMMeshImpl_SetGroupTexture(IDirect3DRMMesh* iface,
2809                                                           D3DRMGROUPINDEX id, LPDIRECT3DRMTEXTURE texture)
2810 {
2811     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2812
2813     TRACE("(%p)->(%u,%p)\n", This, id, texture);
2814
2815     if (id >= This->nb_groups)
2816         return D3DRMERR_BADVALUE;
2817
2818     if (This->groups[id].texture)
2819         IDirect3DRMTexture3_Release(This->groups[id].texture);
2820
2821     if (!texture)
2822     {
2823         This->groups[id].texture = NULL;
2824         return D3DRM_OK;
2825     }
2826
2827     return IDirect3DRMTexture3_QueryInterface(texture, &IID_IDirect3DRMTexture, (LPVOID*)&This->groups[id].texture);
2828 }
2829
2830 static DWORD WINAPI IDirect3DRMMeshImpl_GetGroupCount(IDirect3DRMMesh* iface)
2831 {
2832     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2833
2834     TRACE("(%p)->()\n", This);
2835
2836     return This->nb_groups;
2837 }
2838
2839 static HRESULT WINAPI IDirect3DRMMeshImpl_GetGroup(IDirect3DRMMesh* iface,
2840                                                    D3DRMGROUPINDEX id, unsigned *vertex_count, unsigned *face_count, unsigned *vertex_per_face,
2841                                                    DWORD *face_data_size, unsigned *face_data)
2842 {
2843     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2844
2845     TRACE("(%p)->(%u,%p,%p,%p,%p,%p)\n", This, id, vertex_count, face_count, vertex_per_face, face_data_size, face_data);
2846
2847     if (id >= This->nb_groups)
2848         return D3DRMERR_BADVALUE;
2849
2850     if (vertex_count)
2851         *vertex_count = This->groups[id].nb_vertices;
2852     if (face_count)
2853         *face_count = This->groups[id].nb_faces;
2854     if (vertex_per_face)
2855         *vertex_per_face = This->groups[id].vertex_per_face;
2856     if (face_data_size)
2857         *face_data_size = This->groups[id].face_data_size;
2858     if (face_data)
2859         memcpy(face_data, This->groups[id].face_data, This->groups[id].face_data_size);
2860
2861     return D3DRM_OK;
2862 }
2863
2864 static HRESULT WINAPI IDirect3DRMMeshImpl_GetVertices(IDirect3DRMMesh* iface,
2865                                                       D3DRMGROUPINDEX id, DWORD index, DWORD count, D3DRMVERTEX *return_ptr)
2866 {
2867     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2868
2869     TRACE("(%p)->(%u,%u,%u,%p)\n", This, id, index, count, return_ptr);
2870
2871     if (id >= This->nb_groups)
2872         return D3DRMERR_BADVALUE;
2873
2874     if ((index + count - 1) >= This->groups[id].nb_vertices)
2875         return D3DRMERR_BADVALUE;
2876
2877     if (!return_ptr)
2878         return E_POINTER;
2879
2880     memcpy(return_ptr, This->groups[id].vertices + index, count * sizeof(D3DRMVERTEX));
2881
2882     return D3DRM_OK;
2883 }
2884
2885 static D3DCOLOR WINAPI IDirect3DRMMeshImpl_GetGroupColor(IDirect3DRMMesh* iface, D3DRMGROUPINDEX id)
2886 {
2887     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2888
2889     TRACE("(%p)->(%u)\n", This, id);
2890
2891     return This->groups[id].color;
2892 }
2893
2894 static D3DRMMAPPING WINAPI IDirect3DRMMeshImpl_GetGroupMapping(IDirect3DRMMesh* iface, D3DRMGROUPINDEX id)
2895 {
2896     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2897
2898     FIXME("(%p)->(%u): stub\n", This, id);
2899
2900     return 0;
2901 }
2902 static D3DRMRENDERQUALITY WINAPI IDirect3DRMMeshImpl_GetGroupQuality(IDirect3DRMMesh* iface, D3DRMGROUPINDEX id)
2903 {
2904     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2905
2906     FIXME("(%p)->(%u): stub\n", This, id);
2907
2908     return 0;
2909 }
2910
2911 static HRESULT WINAPI IDirect3DRMMeshImpl_GetGroupMaterial(IDirect3DRMMesh* iface,
2912                                                            D3DRMGROUPINDEX id, LPDIRECT3DRMMATERIAL *material)
2913 {
2914     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2915
2916     TRACE("(%p)->(%u,%p)\n", This, id, material);
2917
2918     if (id >= This->nb_groups)
2919         return D3DRMERR_BADVALUE;
2920
2921     if (!material)
2922         return E_POINTER;
2923
2924     if (This->groups[id].material)
2925         IDirect3DRMTexture_QueryInterface(This->groups[id].material, &IID_IDirect3DRMMaterial, (void**)material);
2926     else
2927         *material = NULL;
2928
2929     return D3DRM_OK;
2930 }
2931
2932 static HRESULT WINAPI IDirect3DRMMeshImpl_GetGroupTexture(IDirect3DRMMesh* iface,
2933                                                           D3DRMGROUPINDEX id, LPDIRECT3DRMTEXTURE *texture)
2934 {
2935     IDirect3DRMMeshImpl *This = impl_from_IDirect3DRMMesh(iface);
2936
2937     TRACE("(%p)->(%u,%p)\n", This, id, texture);
2938
2939     if (id >= This->nb_groups)
2940         return D3DRMERR_BADVALUE;
2941
2942     if (!texture)
2943         return E_POINTER;
2944
2945     if (This->groups[id].texture)
2946         IDirect3DRMTexture_QueryInterface(This->groups[id].texture, &IID_IDirect3DRMTexture, (void**)texture);
2947     else
2948         *texture = NULL;
2949
2950     return D3DRM_OK;
2951 }
2952
2953 static const struct IDirect3DRMMeshVtbl Direct3DRMMesh_Vtbl =
2954 {
2955     /*** IUnknown methods ***/
2956     IDirect3DRMMeshImpl_QueryInterface,
2957     IDirect3DRMMeshImpl_AddRef,
2958     IDirect3DRMMeshImpl_Release,
2959     /*** IDirect3DRMObject methods ***/
2960     IDirect3DRMMeshImpl_Clone,
2961     IDirect3DRMMeshImpl_AddDestroyCallback,
2962     IDirect3DRMMeshImpl_DeleteDestroyCallback,
2963     IDirect3DRMMeshImpl_SetAppData,
2964     IDirect3DRMMeshImpl_GetAppData,
2965     IDirect3DRMMeshImpl_SetName,
2966     IDirect3DRMMeshImpl_GetName,
2967     IDirect3DRMMeshImpl_GetClassName,
2968     /*** IDirect3DRMMesh methods ***/
2969     IDirect3DRMMeshImpl_Scale,
2970     IDirect3DRMMeshImpl_Translate,
2971     IDirect3DRMMeshImpl_GetBox,
2972     IDirect3DRMMeshImpl_AddGroup,
2973     IDirect3DRMMeshImpl_SetVertices,
2974     IDirect3DRMMeshImpl_SetGroupColor,
2975     IDirect3DRMMeshImpl_SetGroupColorRGB,
2976     IDirect3DRMMeshImpl_SetGroupMapping,
2977     IDirect3DRMMeshImpl_SetGroupQuality,
2978     IDirect3DRMMeshImpl_SetGroupMaterial,
2979     IDirect3DRMMeshImpl_SetGroupTexture,
2980     IDirect3DRMMeshImpl_GetGroupCount,
2981     IDirect3DRMMeshImpl_GetGroup,
2982     IDirect3DRMMeshImpl_GetVertices,
2983     IDirect3DRMMeshImpl_GetGroupColor,
2984     IDirect3DRMMeshImpl_GetGroupMapping,
2985     IDirect3DRMMeshImpl_GetGroupQuality,
2986     IDirect3DRMMeshImpl_GetGroupMaterial,
2987     IDirect3DRMMeshImpl_GetGroupTexture
2988 };
2989
2990 HRESULT Direct3DRMMesh_create(IDirect3DRMMesh** obj)
2991 {
2992     IDirect3DRMMeshImpl* object;
2993
2994     TRACE("(%p)\n", obj);
2995
2996     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DRMMeshImpl));
2997     if (!object)
2998     {
2999         ERR("Out of memory\n");
3000         return E_OUTOFMEMORY;
3001     }
3002
3003     object->IDirect3DRMMesh_iface.lpVtbl = &Direct3DRMMesh_Vtbl;
3004     object->ref = 1;
3005
3006     *obj = &object->IDirect3DRMMesh_iface;
3007
3008     return S_OK;
3009 }