meshbuilder.c 85.7 KB
Newer Older
1
/*
2
 * Implementation of IDirect3DRMMeshBuilderX and IDirect3DRMMesh interfaces
3
 *
4
 * Copyright 2010, 2012 Christian Costa
5
 * Copyright 2011 André Hentschel
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

22
#define NONAMELESSUNION
23 24 25

#define COBJMACROS

26 27
#include "wine/debug.h"

28 29
#include "winbase.h"
#include "wingdi.h"
30
#include "dxfile.h"
31
#include "rmxfguid.h"
32 33 34 35 36

#include "d3drm_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3drm);

37 38
struct mesh_group
{
39 40 41 42 43 44
    unsigned nb_vertices;
    D3DRMVERTEX* vertices;
    unsigned nb_faces;
    unsigned vertex_per_face;
    DWORD face_data_size;
    unsigned* face_data;
45
    D3DCOLOR color;
46
    IDirect3DRMMaterial2* material;
47
    IDirect3DRMTexture3* texture;
48
};
49

50 51
struct d3drm_mesh
{
52 53
    IDirect3DRMMesh IDirect3DRMMesh_iface;
    LONG ref;
54 55
    DWORD groups_capacity;
    DWORD nb_groups;
56
    struct mesh_group *groups;
57
};
58

59 60
struct coords_2d
{
61 62
    D3DVALUE u;
    D3DVALUE v;
63
};
64

65 66
struct mesh_material
{
67 68 69
    D3DCOLOR color;
    IDirect3DRMMaterial2 *material;
    IDirect3DRMTexture3 *texture;
70
};
71

72 73
struct d3drm_mesh_builder
{
74
    IDirect3DRMMeshBuilder2 IDirect3DRMMeshBuilder2_iface;
75
    IDirect3DRMMeshBuilder3 IDirect3DRMMeshBuilder3_iface;
76
    LONG ref;
77
    char* name;
78 79 80 81 82 83
    DWORD nb_vertices;
    D3DVECTOR* pVertices;
    DWORD nb_normals;
    D3DVECTOR* pNormals;
    DWORD nb_faces;
    DWORD face_data_size;
Henri Verbeet's avatar
Henri Verbeet committed
84
    void *pFaceData;
85
    DWORD nb_coords2d;
86
    struct coords_2d *pCoords2d;
87
    D3DCOLOR color;
88 89 90
    IDirect3DRMMaterial2 *material;
    IDirect3DRMTexture3 *texture;
    DWORD nb_materials;
91
    struct mesh_material *materials;
92
    DWORD *material_indices;
93
};
94

95
char templates[] = {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
"xof 0302txt 0064"
"template Header"
"{"
"<3D82AB43-62DA-11CF-AB39-0020AF71E433>"
"WORD major;"
"WORD minor;"
"DWORD flags;"
"}"
"template Vector"
"{"
"<3D82AB5E-62DA-11CF-AB39-0020AF71E433>"
"FLOAT x;"
"FLOAT y;"
"FLOAT z;"
"}"
"template Coords2d"
"{"
"<F6F23F44-7686-11CF-8F52-0040333594A3>"
"FLOAT u;"
"FLOAT v;"
"}"
"template Matrix4x4"
"{"
"<F6F23F45-7686-11CF-8F52-0040333594A3>"
"array FLOAT matrix[16];"
"}"
"template ColorRGBA"
"{"
"<35FF44E0-6C7C-11CF-8F52-0040333594A3>"
"FLOAT red;"
"FLOAT green;"
"FLOAT blue;"
"FLOAT alpha;"
"}"
"template ColorRGB"
"{"
"<D3E16E81-7835-11CF-8F52-0040333594A3>"
"FLOAT red;"
"FLOAT green;"
"FLOAT blue;"
"}"
"template IndexedColor"
"{"
"<1630B820-7842-11CF-8F52-0040333594A3>"
"DWORD index;"
"ColorRGBA indexColor;"
"}"
"template Boolean"
"{"
"<537DA6A0-CA37-11D0-941C-0080C80CFA7B>"
"DWORD truefalse;"
"}"
"template Boolean2d"
"{"
"<4885AE63-78E8-11CF-8F52-0040333594A3>"
"Boolean u;"
"Boolean v;"
"}"
"template MaterialWrap"
"{"
"<4885AE60-78E8-11CF-8F52-0040333594A3>"
"Boolean u;"
"Boolean v;"
"}"
"template TextureFilename"
"{"
"<A42790E1-7810-11CF-8F52-0040333594A3>"
"STRING filename;"
"}"
"template Material"
"{"
"<3D82AB4D-62DA-11CF-AB39-0020AF71E433>"
"ColorRGBA faceColor;"
"FLOAT power;"
"ColorRGB specularColor;"
"ColorRGB emissiveColor;"
"[...]"
"}"
"template MeshFace"
"{"
"<3D82AB5F-62DA-11CF-AB39-0020AF71E433>"
"DWORD nFaceVertexIndices;"
"array DWORD faceVertexIndices[nFaceVertexIndices];"
"}"
"template MeshFaceWraps"
"{"
"<ED1EC5C0-C0A8-11D0-941C-0080C80CFA7B>"
"DWORD nFaceWrapValues;"
"array Boolean2d faceWrapValues[nFaceWrapValues];"
"}"
"template MeshTextureCoords"
"{"
"<F6F23F40-7686-11CF-8F52-0040333594A3>"
"DWORD nTextureCoords;"
"array Coords2d textureCoords[nTextureCoords];"
"}"
"template MeshMaterialList"
"{"
"<F6F23F42-7686-11CF-8F52-0040333594A3>"
"DWORD nMaterials;"
"DWORD nFaceIndexes;"
"array DWORD faceIndexes[nFaceIndexes];"
"[Material]"
"}"
"template MeshNormals"
"{"
"<F6F23F43-7686-11CF-8F52-0040333594A3>"
"DWORD nNormals;"
"array Vector normals[nNormals];"
"DWORD nFaceNormals;"
"array MeshFace faceNormals[nFaceNormals];"
"}"
"template MeshVertexColors"
"{"
"<1630B821-7842-11CF-8F52-0040333594A3>"
"DWORD nVertexColors;"
"array IndexedColor vertexColors[nVertexColors];"
"}"
"template Mesh"
"{"
"<3D82AB44-62DA-11CF-AB39-0020AF71E433>"
"DWORD nVertices;"
"array Vector vertices[nVertices];"
"DWORD nFaces;"
"array MeshFace faces[nFaces];"
"[...]"
"}"
"template FrameTransformMatrix"
"{"
"<F6F23F41-7686-11CF-8F52-0040333594A3>"
"Matrix4x4 frameMatrix;"
"}"
"template Frame"
"{"
"<3D82AB46-62DA-11CF-AB39-0020AF71E433>"
"[...]"
"}"
"template FloatKeys"
"{"
"<10DD46A9-775B-11CF-8F52-0040333594A3>"
"DWORD nValues;"
"array FLOAT values[nValues];"
"}"
"template TimedFloatKeys"
"{"
"<F406B180-7B3B-11CF-8F52-0040333594A3>"
"DWORD time;"
"FloatKeys tfkeys;"
"}"
"template AnimationKey"
"{"
"<10DD46A8-775B-11CF-8F52-0040333594A3>"
"DWORD keyType;"
"DWORD nKeys;"
"array TimedFloatKeys keys[nKeys];"
"}"
"template AnimationOptions"
"{"
"<E2BF56C0-840F-11CF-8F52-0040333594A3>"
"DWORD openclosed;"
"DWORD positionquality;"
"}"
"template Animation"
"{"
"<3D82AB4F-62DA-11CF-AB39-0020AF71E433>"
"[...]"
"}"
"template AnimationSet"
"{"
"<3D82AB50-62DA-11CF-AB39-0020AF71E433>"
"[Animation]"
"}"
"template InlineData"
"{"
"<3A23EEA0-94B1-11D0-AB39-0020AF71E433>"
"[BINARY]"
"}"
"template Url"
"{"
"<3A23EEA1-94B1-11D0-AB39-0020AF71E433>"
"DWORD nUrls;"
"array STRING urls[nUrls];"
"}"
"template ProgressiveMesh"
"{"
"<8A63C360-997D-11D0-941C-0080C80CFA7B>"
"[Url,InlineData]"
"}"
"template Guid"
"{"
"<A42790E0-7810-11CF-8F52-0040333594A3>"
"DWORD data1;"
"WORD data2;"
"WORD data3;"
"array UCHAR data4[8];"
"}"
"template StringProperty"
"{"
"<7F0F21E0-BFE1-11D1-82C0-00A0C9697271>"
"STRING key;"
"STRING value;"
"}"
"template PropertyBag"
"{"
"<7F0F21E1-BFE1-11D1-82C0-00A0C9697271>"
"[StringProperty]"
"}"
"template ExternalVisual"
"{"
"<98116AA0-BDBA-11D1-82C0-00A0C9697271>"
"Guid guidExternalVisual;"
"[...]"
"}"
"template RightHanded"
"{"
"<7F5D5EA0-D53A-11D1-82C0-00A0C9697271>"
"DWORD bRightHanded;"
"}"
};

316
static inline struct d3drm_mesh *impl_from_IDirect3DRMMesh(IDirect3DRMMesh *iface)
317
{
318
    return CONTAINING_RECORD(iface, struct d3drm_mesh, IDirect3DRMMesh_iface);
319 320
}

321
static inline struct d3drm_mesh_builder *impl_from_IDirect3DRMMeshBuilder2(IDirect3DRMMeshBuilder2 *iface)
322
{
323
    return CONTAINING_RECORD(iface, struct d3drm_mesh_builder, IDirect3DRMMeshBuilder2_iface);
324 325
}

326
static inline struct d3drm_mesh_builder *impl_from_IDirect3DRMMeshBuilder3(IDirect3DRMMeshBuilder3 *iface)
327
{
328
    return CONTAINING_RECORD(iface, struct d3drm_mesh_builder, IDirect3DRMMeshBuilder3_iface);
329 330
}

331
static void clean_mesh_builder_data(struct d3drm_mesh_builder *mesh_builder)
332
{
333
    DWORD i;
334

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    HeapFree(GetProcessHeap(), 0, mesh_builder->name);
    mesh_builder->name = NULL;
    HeapFree(GetProcessHeap(), 0, mesh_builder->pVertices);
    mesh_builder->pVertices = NULL;
    mesh_builder->nb_vertices = 0;
    HeapFree(GetProcessHeap(), 0, mesh_builder->pNormals);
    mesh_builder->pNormals = NULL;
    mesh_builder->nb_normals = 0;
    HeapFree(GetProcessHeap(), 0, mesh_builder->pFaceData);
    mesh_builder->pFaceData = NULL;
    mesh_builder->face_data_size = 0;
    mesh_builder->nb_faces = 0;
    HeapFree(GetProcessHeap(), 0, mesh_builder->pCoords2d);
    mesh_builder->pCoords2d = NULL;
    mesh_builder->nb_coords2d = 0;
350 351 352 353 354 355 356 357 358 359
    for (i = 0; i < mesh_builder->nb_materials; i++)
    {
        if (mesh_builder->materials[i].material)
            IDirect3DRMMaterial2_Release(mesh_builder->materials[i].material);
        if (mesh_builder->materials[i].texture)
            IDirect3DRMTexture3_Release(mesh_builder->materials[i].texture);
    }
    mesh_builder->nb_materials = 0;
    HeapFree(GetProcessHeap(), 0, mesh_builder->materials);
    HeapFree(GetProcessHeap(), 0, mesh_builder->material_indices);
360 361
}

362
static HRESULT WINAPI d3drm_mesh_builder2_QueryInterface(IDirect3DRMMeshBuilder2 *iface, REFIID riid, void **out)
363
{
364
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
365

366
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
367

368 369 370
    if (IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder2)
            || IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder)
            || IsEqualGUID(riid, &IID_IUnknown))
371
    {
372
        *out = &mesh_builder->IDirect3DRMMeshBuilder2_iface;
373
    }
374
    else if (IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder3))
375
    {
376
        *out = &mesh_builder->IDirect3DRMMeshBuilder3_iface;
377 378
    }
    else
379
    {
380 381
        *out = NULL;
        WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
382
        return E_NOINTERFACE;
383 384
    }

385
    IUnknown_AddRef((IUnknown *)*out);
386
    return S_OK;
387 388
}

389
static ULONG WINAPI d3drm_mesh_builder2_AddRef(IDirect3DRMMeshBuilder2 *iface)
390
{
391 392
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
    ULONG refcount = InterlockedIncrement(&mesh_builder->ref);
393

394
    TRACE("%p increasing refcount to %u.\n", mesh_builder, refcount);
395

396
    return refcount;
397 398
}

399
static ULONG WINAPI d3drm_mesh_builder2_Release(IDirect3DRMMeshBuilder2 *iface)
400
{
401 402
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
    ULONG refcount = InterlockedDecrement(&mesh_builder->ref);
403

404
    TRACE("%p decreasing refcount to %u.\n", mesh_builder, refcount);
405

406
    if (!refcount)
407
    {
408 409 410 411 412 413
        clean_mesh_builder_data(mesh_builder);
        if (mesh_builder->material)
            IDirect3DRMMaterial2_Release(mesh_builder->material);
        if (mesh_builder->texture)
            IDirect3DRMTexture3_Release(mesh_builder->texture);
        HeapFree(GetProcessHeap(), 0, mesh_builder);
414
    }
415

416
    return refcount;
417 418
}

419
static HRESULT WINAPI d3drm_mesh_builder2_Clone(IDirect3DRMMeshBuilder2 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
420
        IUnknown *outer, REFIID iid, void **out)
421
{
Henri Verbeet's avatar
Henri Verbeet committed
422
    FIXME("iface %p, outer %p, iid %s, out %p stub!\n", iface, outer, debugstr_guid(iid), out);
423 424 425 426

    return E_NOTIMPL;
}

427
static HRESULT WINAPI d3drm_mesh_builder2_AddDestroyCallback(IDirect3DRMMeshBuilder2 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
428
        D3DRMOBJECTCALLBACK cb, void *ctx)
429
{
Henri Verbeet's avatar
Henri Verbeet committed
430
    FIXME("iface %p, cb %p, ctx %p stub!\n", iface, cb, ctx);
431 432 433 434

    return E_NOTIMPL;
}

435
static HRESULT WINAPI d3drm_mesh_builder2_DeleteDestroyCallback(IDirect3DRMMeshBuilder2 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
436
        D3DRMOBJECTCALLBACK cb, void *ctx)
437
{
Henri Verbeet's avatar
Henri Verbeet committed
438
    FIXME("iface %p, cb %p, ctx %p stub!\n", iface, cb, ctx);
439 440 441 442

    return E_NOTIMPL;
}

443
static HRESULT WINAPI d3drm_mesh_builder2_SetAppData(IDirect3DRMMeshBuilder2 *iface, DWORD data)
444
{
445
    FIXME("iface %p, data %#x stub!\n", iface, data);
446 447 448 449

    return E_NOTIMPL;
}

450
static DWORD WINAPI d3drm_mesh_builder2_GetAppData(IDirect3DRMMeshBuilder2 *iface)
451
{
452
    FIXME("iface %p stub!\n", iface);
453 454 455 456

    return 0;
}

457
static HRESULT WINAPI d3drm_mesh_builder2_SetName(IDirect3DRMMeshBuilder2 *iface, const char *name)
458
{
459
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
460

Henri Verbeet's avatar
Henri Verbeet committed
461
    TRACE("iface %p, name %s.\n", iface, debugstr_a(name));
462

Henri Verbeet's avatar
Henri Verbeet committed
463
    return IDirect3DRMMeshBuilder3_SetName(&mesh_builder->IDirect3DRMMeshBuilder3_iface, name);
464 465
}

466
static HRESULT WINAPI d3drm_mesh_builder2_GetName(IDirect3DRMMeshBuilder2 *iface, DWORD *size, char *name)
467
{
468
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
469

Henri Verbeet's avatar
Henri Verbeet committed
470
    TRACE("iface %p, size %p, name %p.\n", iface, size, name);
471

Henri Verbeet's avatar
Henri Verbeet committed
472
    return IDirect3DRMMeshBuilder3_GetName(&mesh_builder->IDirect3DRMMeshBuilder3_iface, size, name);
473 474
}

475
static HRESULT WINAPI d3drm_mesh_builder2_GetClassName(IDirect3DRMMeshBuilder2 *iface, DWORD *size, char *name)
476
{
477
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
478

Henri Verbeet's avatar
Henri Verbeet committed
479
    TRACE("iface %p, size %p, name %p.\n", iface, size, name);
480

Henri Verbeet's avatar
Henri Verbeet committed
481
    return IDirect3DRMMeshBuilder3_GetClassName(&mesh_builder->IDirect3DRMMeshBuilder3_iface, size, name);
482 483
}

484
static HRESULT WINAPI d3drm_mesh_builder2_Load(IDirect3DRMMeshBuilder2 *iface, void *filename,
Henri Verbeet's avatar
Henri Verbeet committed
485
        void *name, D3DRMLOADOPTIONS flags, D3DRMLOADTEXTURECALLBACK cb, void *ctx)
486
{
487
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
488

Henri Verbeet's avatar
Henri Verbeet committed
489 490
    TRACE("iface %p, filename %p, name %p, flags %#x, cb %p, ctx %p.\n",
            iface, filename, name, flags, cb, ctx);
491

492 493 494
    if (cb)
        FIXME("Texture callback is not yet supported\n");

Henri Verbeet's avatar
Henri Verbeet committed
495 496
    return IDirect3DRMMeshBuilder3_Load(&mesh_builder->IDirect3DRMMeshBuilder3_iface,
            filename, name, flags, NULL, ctx);
497 498
}

499 500
static HRESULT WINAPI d3drm_mesh_builder2_Save(IDirect3DRMMeshBuilder2 *iface,
        const char *filename, D3DRMXOFFORMAT format, D3DRMSAVEOPTIONS flags)
501
{
502 503
    FIXME("iface %p, filename %s, format %#x, flags %#x stub!\n",
            iface, debugstr_a(filename), format, flags);
504 505 506 507

    return E_NOTIMPL;
}

508 509
static HRESULT WINAPI d3drm_mesh_builder2_Scale(IDirect3DRMMeshBuilder2 *iface,
        D3DVALUE sx, D3DVALUE sy, D3DVALUE sz)
510
{
511
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
512

513
    TRACE("iface %p, sx %.8e, sy %.8e, sz %.8e.\n", iface, sx, sy, sz);
514

515
    return IDirect3DRMMeshBuilder3_Scale(&mesh_builder->IDirect3DRMMeshBuilder3_iface, sx, sy, sz);
516 517
}

518 519
static HRESULT WINAPI d3drm_mesh_builder2_Translate(IDirect3DRMMeshBuilder2 *iface,
        D3DVALUE tx, D3DVALUE ty, D3DVALUE tz)
520
{
521
    FIXME("iface %p, tx %.8e, ty %.8e, tz %.8e stub!\n", iface, tx, ty, tz);
522 523 524 525

    return E_NOTIMPL;
}

526
static HRESULT WINAPI d3drm_mesh_builder2_SetColorSource(IDirect3DRMMeshBuilder2 *iface, D3DRMCOLORSOURCE source)
527
{
528
    FIXME("iface %p, source %#x stub!\n", iface, source);
529 530 531 532

    return E_NOTIMPL;
}

533
static HRESULT WINAPI d3drm_mesh_builder2_GetBox(IDirect3DRMMeshBuilder2 *iface, D3DRMBOX *box)
534
{
535
    FIXME("iface %p, box %p stub!\n", iface, box);
536 537 538 539

    return E_NOTIMPL;
}

540
static HRESULT WINAPI d3drm_mesh_builder2_GenerateNormals(IDirect3DRMMeshBuilder2 *iface)
541
{
542
    FIXME("iface %p stub!\n", iface);
543 544 545 546

    return E_NOTIMPL;
}

547
static D3DRMCOLORSOURCE WINAPI d3drm_mesh_builder2_GetColorSource(IDirect3DRMMeshBuilder2 *iface)
548
{
549
    FIXME("iface %p stub!\n", iface);
550 551 552 553

    return E_NOTIMPL;
}

554
static HRESULT WINAPI d3drm_mesh_builder2_AddMesh(IDirect3DRMMeshBuilder2 *iface, IDirect3DRMMesh *mesh)
555
{
556
    FIXME("iface %p, mesh %p stub!\n", iface, mesh);
557 558 559 560

    return E_NOTIMPL;
}

561
static HRESULT WINAPI d3drm_mesh_builder2_AddMeshBuilder(IDirect3DRMMeshBuilder2 *iface,
562
        IDirect3DRMMeshBuilder *mesh_builder)
563
{
564
    FIXME("iface %p, mesh_builder %p stub!\n", iface, mesh_builder);
565 566 567 568

    return E_NOTIMPL;
}

569
static HRESULT WINAPI d3drm_mesh_builder2_AddFrame(IDirect3DRMMeshBuilder2 *iface, IDirect3DRMFrame *frame)
570
{
571
    FIXME("iface %p, frame %p stub!\n", iface, frame);
572 573 574 575

    return E_NOTIMPL;
}

576
static HRESULT WINAPI d3drm_mesh_builder2_AddFace(IDirect3DRMMeshBuilder2 *iface, IDirect3DRMFace *face)
577
{
578
    FIXME("iface %p, face %p stub!\n", iface, face);
579 580 581 582

    return E_NOTIMPL;
}

583
static HRESULT WINAPI d3drm_mesh_builder2_AddFaces(IDirect3DRMMeshBuilder2 *iface,
584 585
        DWORD vertex_count, D3DVECTOR *vertices, DWORD normal_count, D3DVECTOR *normals,
        DWORD *face_data, IDirect3DRMFaceArray **array)
586
{
587 588
    FIXME("iface %p, vertex_count %u, vertices %p, normal_count %u, normals %p, face_data %p, array %p stub!\n",
            iface, vertex_count, vertices, normal_count, normals, face_data, array);
589 590 591 592

    return E_NOTIMPL;
}

593 594
static HRESULT WINAPI d3drm_mesh_builder2_ReserveSpace(IDirect3DRMMeshBuilder2 *iface,
        DWORD vertex_count, DWORD normal_count, DWORD face_count)
595
{
596 597
    FIXME("iface %p, vertex_count %u, normal_count %u, face_count %u stub!\n",
            iface, vertex_count, normal_count, face_count);
598 599 600 601

    return E_NOTIMPL;
}

602 603
static HRESULT WINAPI d3drm_mesh_builder2_SetColorRGB(IDirect3DRMMeshBuilder2 *iface,
        D3DVALUE red, D3DVALUE green, D3DVALUE blue)
604
{
605
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
606

607
    TRACE("iface %p, red %.8e, green %.8e, blue %.8e.\n", iface, red, green, blue);
608

609
    return IDirect3DRMMeshBuilder3_SetColorRGB(&mesh_builder->IDirect3DRMMeshBuilder3_iface, red, green, blue);
610 611
}

612
static HRESULT WINAPI d3drm_mesh_builder2_SetColor(IDirect3DRMMeshBuilder2 *iface, D3DCOLOR color)
613
{
614
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
615

616
    TRACE("iface %p, color 0x%08x.\n", iface, color);
617

618
    return IDirect3DRMMeshBuilder3_SetColor(&mesh_builder->IDirect3DRMMeshBuilder3_iface, color);
619 620
}

621
static HRESULT WINAPI d3drm_mesh_builder2_SetTexture(IDirect3DRMMeshBuilder2 *iface,
622
        IDirect3DRMTexture *texture)
623
{
624
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
625
    IDirect3DRMTexture3 *texture3 = NULL;
626
    HRESULT hr = D3DRM_OK;
627

628 629
    TRACE("iface %p, texture %p.\n", iface, texture);

630
    if (texture)
Henri Verbeet's avatar
Henri Verbeet committed
631
        hr = IDirect3DRMTexture_QueryInterface(texture, &IID_IDirect3DRMTexture3, (void **)&texture3);
632
    if (SUCCEEDED(hr))
633
        hr = IDirect3DRMMeshBuilder3_SetTexture(&mesh_builder->IDirect3DRMMeshBuilder3_iface, texture3);
634 635
    if (texture3)
        IDirect3DRMTexture3_Release(texture3);
636

637
    return hr;
638 639
}

640
static HRESULT WINAPI d3drm_mesh_builder2_SetMaterial(IDirect3DRMMeshBuilder2 *iface,
641
        IDirect3DRMMaterial *material)
642
{
643
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
644

645
    TRACE("iface %p, material %p.\n", iface, material);
646

647
    return IDirect3DRMMeshBuilder3_SetMaterial(&mesh_builder->IDirect3DRMMeshBuilder3_iface,
648
            (IDirect3DRMMaterial2 *)material);
649 650
}

651 652
static HRESULT WINAPI d3drm_mesh_builder2_SetTextureTopology(IDirect3DRMMeshBuilder2 *iface,
        BOOL wrap_u, BOOL wrap_v)
653
{
654
    FIXME("iface %p, wrap_u %#x, wrap_v %#x stub!\n", iface, wrap_u, wrap_v);
655 656 657 658

    return E_NOTIMPL;
}

659 660
static HRESULT WINAPI d3drm_mesh_builder2_SetQuality(IDirect3DRMMeshBuilder2 *iface,
        D3DRMRENDERQUALITY quality)
661
{
662
    FIXME("iface %p, quality %#x stub!\n", iface, quality);
663 664 665 666

    return E_NOTIMPL;
}

667
static HRESULT WINAPI d3drm_mesh_builder2_SetPerspective(IDirect3DRMMeshBuilder2 *iface, BOOL enable)
668
{
669
    FIXME("iface %p, enable %#x stub!\n", iface, enable);
670 671 672 673

    return E_NOTIMPL;
}

674 675
static HRESULT WINAPI d3drm_mesh_builder2_SetVertex(IDirect3DRMMeshBuilder2 *iface,
        DWORD index, D3DVALUE x, D3DVALUE y, D3DVALUE z)
676
{
677
    FIXME("iface %p, index %u, x %.8e, y %.8e, z %.8e stub!\n", iface, index, x, y, z);
678 679 680 681

    return E_NOTIMPL;
}

682 683
static HRESULT WINAPI d3drm_mesh_builder2_SetNormal(IDirect3DRMMeshBuilder2 *iface,
        DWORD index, D3DVALUE x, D3DVALUE y, D3DVALUE z)
684
{
685
    FIXME("iface %p, index %u, x %.8e, y %.8e, z %.8e stub!\n", iface, index, x, y, z);
686 687 688 689

    return E_NOTIMPL;
}

690 691
static HRESULT WINAPI d3drm_mesh_builder2_SetTextureCoordinates(IDirect3DRMMeshBuilder2 *iface,
        DWORD index, D3DVALUE u, D3DVALUE v)
692
{
693
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
694

695
    TRACE("iface %p, index %u, u %.8e, v %.8e.\n", iface, index, u, v);
696

697 698
    return IDirect3DRMMeshBuilder3_SetTextureCoordinates(&mesh_builder->IDirect3DRMMeshBuilder3_iface,
            index, u, v);
699 700
}

701 702
static HRESULT WINAPI d3drm_mesh_builder2_SetVertexColor(IDirect3DRMMeshBuilder2 *iface,
        DWORD index, D3DCOLOR color)
703
{
704
    FIXME("iface %p, index %u, color 0x%08x stub!\n", iface, index, color);
705 706 707 708

    return E_NOTIMPL;
}

709 710
static HRESULT WINAPI d3drm_mesh_builder2_SetVertexColorRGB(IDirect3DRMMeshBuilder2 *iface,
        DWORD index, D3DVALUE red, D3DVALUE green, D3DVALUE blue)
711
{
712 713
    FIXME("iface %p, index %u, red %.8e, green %.8e, blue %.8e stub!\n",
            iface, index, red, green, blue);
714 715 716 717

    return E_NOTIMPL;
}

718
static HRESULT WINAPI d3drm_mesh_builder2_GetFaces(IDirect3DRMMeshBuilder2 *iface,
719
        IDirect3DRMFaceArray **array)
720
{
721
    FIXME("iface %p, array %p stub!\n", iface, array);
722 723 724 725

    return E_NOTIMPL;
}

726 727 728
static HRESULT WINAPI d3drm_mesh_builder2_GetVertices(IDirect3DRMMeshBuilder2 *iface,
        DWORD *vertex_count, D3DVECTOR *vertices, DWORD *normal_count, D3DVECTOR *normals,
        DWORD *face_data_size, DWORD *face_data)
729
{
730
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
731

732 733
    TRACE("iface %p, vertex_count %p, vertices %p, normal_count %p, normals %p, face_data_size %p, face_data %p.\n",
            iface, vertex_count, vertices, normal_count, normals, face_data_size, face_data);
734

735
    if (vertices && (!vertex_count || (*vertex_count < mesh_builder->nb_vertices)))
736
        return D3DRMERR_BADVALUE;
737 738 739 740
    if (vertex_count)
        *vertex_count = mesh_builder->nb_vertices;
    if (vertices && mesh_builder->nb_vertices)
        memcpy(vertices, mesh_builder->pVertices, mesh_builder->nb_vertices * sizeof(*vertices));
741

742
    if (normals && (!normal_count || (*normal_count < mesh_builder->nb_normals)))
743
        return D3DRMERR_BADVALUE;
744 745 746 747
    if (normal_count)
        *normal_count = mesh_builder->nb_normals;
    if (normals && mesh_builder->nb_normals)
        memcpy(normals, mesh_builder->pNormals, mesh_builder->nb_normals * sizeof(*normals));
748

749
    if (face_data && (!face_data_size || (*face_data_size < mesh_builder->face_data_size)))
750
        return D3DRMERR_BADVALUE;
751
    if (face_data_size)
752 753 754
        *face_data_size = mesh_builder->face_data_size;
    if (face_data && mesh_builder->face_data_size)
        memcpy(face_data, mesh_builder->pFaceData, mesh_builder->face_data_size * sizeof(*face_data));
755 756

    return D3DRM_OK;
757 758
}

759 760
static HRESULT WINAPI d3drm_mesh_builder2_GetTextureCoordinates(IDirect3DRMMeshBuilder2 *iface,
        DWORD index, D3DVALUE *u, D3DVALUE *v)
761
{
762
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
763

764
    TRACE("iface %p, index %u, u %p, v %p.\n", iface, index, u, v);
765

766 767
    return IDirect3DRMMeshBuilder3_GetTextureCoordinates(&mesh_builder->IDirect3DRMMeshBuilder3_iface,
            index, u, v);
768 769
}

770 771
static int WINAPI d3drm_mesh_builder2_AddVertex(IDirect3DRMMeshBuilder2 *iface,
        D3DVALUE x, D3DVALUE y, D3DVALUE z)
772
{
773
    FIXME("iface %p, x %.8e, y %.8e, z %.8e stub!\n", iface, x, y, z);
774 775 776 777

    return 0;
}

778 779
static int WINAPI d3drm_mesh_builder2_AddNormal(IDirect3DRMMeshBuilder2 *iface,
        D3DVALUE x, D3DVALUE y, D3DVALUE z)
780
{
781
    FIXME("iface %p, x %.8e, y %.8e, z %.8e stub!\n", iface, x, y, z);
782 783 784 785

    return 0;
}

786
static HRESULT WINAPI d3drm_mesh_builder2_CreateFace(IDirect3DRMMeshBuilder2 *iface, IDirect3DRMFace **face)
787
{
788
    TRACE("iface %p, face %p.\n", iface, face);
789

790
    return Direct3DRMFace_create(&IID_IDirect3DRMFace, (IUnknown **)face);
791 792
}

793
static D3DRMRENDERQUALITY WINAPI d3drm_mesh_builder2_GetQuality(IDirect3DRMMeshBuilder2 *iface)
794
{
795
    FIXME("iface %p stub!\n", iface);
796 797 798 799

    return 0;
}

800
static BOOL WINAPI d3drm_mesh_builder2_GetPerspective(IDirect3DRMMeshBuilder2 *iface)
801
{
802
    FIXME("iface %p stub!\n", iface);
803 804 805 806

    return FALSE;
}

807
static int WINAPI d3drm_mesh_builder2_GetFaceCount(IDirect3DRMMeshBuilder2 *iface)
808
{
809
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
810

811
    TRACE("iface %p.\n", iface);
812

813
    return mesh_builder->nb_faces;
814 815
}

816
static int WINAPI d3drm_mesh_builder2_GetVertexCount(IDirect3DRMMeshBuilder2 *iface)
817
{
818
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
819

820
    TRACE("iface %p.\n", iface);
821

822
    return mesh_builder->nb_vertices;
823 824
}

825
static D3DCOLOR WINAPI d3drm_mesh_builder2_GetVertexColor(IDirect3DRMMeshBuilder2 *iface, DWORD index)
826
{
827
    FIXME("iface %p, index %u stub!\n", iface, index);
828 829 830 831

    return 0;
}

832
static HRESULT WINAPI d3drm_mesh_builder2_CreateMesh(IDirect3DRMMeshBuilder2 *iface, IDirect3DRMMesh **mesh)
833
{
834
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder2(iface);
835

836
    TRACE("iface %p, mesh %p.\n", iface, mesh);
837

838
    return IDirect3DRMMeshBuilder3_CreateMesh(&mesh_builder->IDirect3DRMMeshBuilder3_iface, mesh);
839 840
}

841 842
static HRESULT WINAPI d3drm_mesh_builder2_GenerateNormals2(IDirect3DRMMeshBuilder2 *iface,
        D3DVALUE crease, DWORD flags)
843
{
844
    FIXME("iface %p, crease %.8e, flags %#x stub!\n", iface, crease, flags);
845 846 847 848

    return E_NOTIMPL;
}

849
static HRESULT WINAPI d3drm_mesh_builder2_GetFace(IDirect3DRMMeshBuilder2 *iface,
850
        DWORD index, IDirect3DRMFace **face)
851
{
852
    FIXME("iface %p, index %u, face %p stub!\n", iface, index, face);
853 854 855 856

    return E_NOTIMPL;
}

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
static const struct IDirect3DRMMeshBuilder2Vtbl d3drm_mesh_builder2_vtbl =
{
    d3drm_mesh_builder2_QueryInterface,
    d3drm_mesh_builder2_AddRef,
    d3drm_mesh_builder2_Release,
    d3drm_mesh_builder2_Clone,
    d3drm_mesh_builder2_AddDestroyCallback,
    d3drm_mesh_builder2_DeleteDestroyCallback,
    d3drm_mesh_builder2_SetAppData,
    d3drm_mesh_builder2_GetAppData,
    d3drm_mesh_builder2_SetName,
    d3drm_mesh_builder2_GetName,
    d3drm_mesh_builder2_GetClassName,
    d3drm_mesh_builder2_Load,
    d3drm_mesh_builder2_Save,
    d3drm_mesh_builder2_Scale,
    d3drm_mesh_builder2_Translate,
    d3drm_mesh_builder2_SetColorSource,
    d3drm_mesh_builder2_GetBox,
    d3drm_mesh_builder2_GenerateNormals,
    d3drm_mesh_builder2_GetColorSource,
    d3drm_mesh_builder2_AddMesh,
    d3drm_mesh_builder2_AddMeshBuilder,
    d3drm_mesh_builder2_AddFrame,
    d3drm_mesh_builder2_AddFace,
    d3drm_mesh_builder2_AddFaces,
    d3drm_mesh_builder2_ReserveSpace,
    d3drm_mesh_builder2_SetColorRGB,
    d3drm_mesh_builder2_SetColor,
    d3drm_mesh_builder2_SetTexture,
    d3drm_mesh_builder2_SetMaterial,
    d3drm_mesh_builder2_SetTextureTopology,
    d3drm_mesh_builder2_SetQuality,
    d3drm_mesh_builder2_SetPerspective,
    d3drm_mesh_builder2_SetVertex,
    d3drm_mesh_builder2_SetNormal,
    d3drm_mesh_builder2_SetTextureCoordinates,
    d3drm_mesh_builder2_SetVertexColor,
    d3drm_mesh_builder2_SetVertexColorRGB,
    d3drm_mesh_builder2_GetFaces,
    d3drm_mesh_builder2_GetVertices,
    d3drm_mesh_builder2_GetTextureCoordinates,
    d3drm_mesh_builder2_AddVertex,
    d3drm_mesh_builder2_AddNormal,
    d3drm_mesh_builder2_CreateFace,
    d3drm_mesh_builder2_GetQuality,
    d3drm_mesh_builder2_GetPerspective,
    d3drm_mesh_builder2_GetFaceCount,
    d3drm_mesh_builder2_GetVertexCount,
    d3drm_mesh_builder2_GetVertexColor,
    d3drm_mesh_builder2_CreateMesh,
    d3drm_mesh_builder2_GenerateNormals2,
    d3drm_mesh_builder2_GetFace,
910
};
911

912
static HRESULT WINAPI d3drm_mesh_builder3_QueryInterface(IDirect3DRMMeshBuilder3 *iface, REFIID riid, void **out)
913
{
914 915 916 917 918
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);

    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);

    return d3drm_mesh_builder2_QueryInterface(&mesh_builder->IDirect3DRMMeshBuilder2_iface, riid, out);
919 920
}

921
static ULONG WINAPI d3drm_mesh_builder3_AddRef(IDirect3DRMMeshBuilder3 *iface)
922
{
923 924 925 926 927
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);

    TRACE("iface %p.\n", iface);

    return d3drm_mesh_builder2_AddRef(&mesh_builder->IDirect3DRMMeshBuilder2_iface);
928 929
}

930
static ULONG WINAPI d3drm_mesh_builder3_Release(IDirect3DRMMeshBuilder3 *iface)
931
{
932 933 934 935 936
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);

    TRACE("iface %p.\n", iface);

    return d3drm_mesh_builder2_Release(&mesh_builder->IDirect3DRMMeshBuilder2_iface);
937 938
}

939
static HRESULT WINAPI d3drm_mesh_builder3_Clone(IDirect3DRMMeshBuilder3 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
940
        IUnknown *outer, REFIID iid, void **out)
941
{
Henri Verbeet's avatar
Henri Verbeet committed
942
    FIXME("iface %p, outer %p, iid %s, out %p stub!\n", iface, outer, debugstr_guid(iid), out);
943 944 945 946

    return E_NOTIMPL;
}

947
static HRESULT WINAPI d3drm_mesh_builder3_AddDestroyCallback(IDirect3DRMMeshBuilder3 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
948
        D3DRMOBJECTCALLBACK cb, void *ctx)
949
{
Henri Verbeet's avatar
Henri Verbeet committed
950
    FIXME("iface %p, cb %p, ctx %p stub!\n", iface, cb, ctx);
951 952 953 954

    return E_NOTIMPL;
}

955
static HRESULT WINAPI d3drm_mesh_builder3_DeleteDestroyCallback(IDirect3DRMMeshBuilder3 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
956
        D3DRMOBJECTCALLBACK cb, void *ctx)
957
{
Henri Verbeet's avatar
Henri Verbeet committed
958
    FIXME("iface %p, cb %p, ctx %p stub!\n", iface, cb, ctx);
959 960 961 962

    return E_NOTIMPL;
}

963
static HRESULT WINAPI d3drm_mesh_builder3_SetAppData(IDirect3DRMMeshBuilder3 *iface, DWORD data)
964
{
965
    FIXME("iface %p, data %#x stub!\n", iface, data);
966 967 968 969

    return E_NOTIMPL;
}

970
static DWORD WINAPI d3drm_mesh_builder3_GetAppData(IDirect3DRMMeshBuilder3 *iface)
971
{
972
    FIXME("iface %p stub!\n", iface);
973 974 975 976

    return 0;
}

977
static HRESULT WINAPI d3drm_mesh_builder3_SetName(IDirect3DRMMeshBuilder3 *iface, const char *name)
978
{
979
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
980
    char *string = NULL;
981

Henri Verbeet's avatar
Henri Verbeet committed
982
    TRACE("iface %p, name %s.\n", iface, debugstr_a(name));
983

984 985 986 987 988 989
    if (name)
    {
        string = HeapAlloc(GetProcessHeap(), 0, strlen(name) + 1);
        if (!string) return E_OUTOFMEMORY;
        strcpy(string, name);
    }
Henri Verbeet's avatar
Henri Verbeet committed
990 991
    HeapFree(GetProcessHeap(), 0, mesh_builder->name);
    mesh_builder->name = string;
992 993

    return D3DRM_OK;
994 995
}

996
static HRESULT WINAPI d3drm_mesh_builder3_GetName(IDirect3DRMMeshBuilder3 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
997
        DWORD *size, char *name)
998
{
999
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1000

Henri Verbeet's avatar
Henri Verbeet committed
1001
    TRACE("iface %p, size %p, name %p.\n", iface, size, name);
1002

1003 1004 1005
    if (!size)
        return E_POINTER;

Henri Verbeet's avatar
Henri Verbeet committed
1006
    if (!mesh_builder->name)
1007 1008 1009 1010 1011
    {
        *size = 0;
        return D3DRM_OK;
    }

Henri Verbeet's avatar
Henri Verbeet committed
1012
    if (*size < (strlen(mesh_builder->name) + 1))
1013 1014
        return E_INVALIDARG;

Henri Verbeet's avatar
Henri Verbeet committed
1015 1016
    strcpy(name, mesh_builder->name);
    *size = strlen(mesh_builder->name) + 1;
1017 1018

    return D3DRM_OK;
1019 1020
}

1021
static HRESULT WINAPI d3drm_mesh_builder3_GetClassName(IDirect3DRMMeshBuilder3 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
1022
        DWORD *size, char *name)
1023
{
Henri Verbeet's avatar
Henri Verbeet committed
1024
    TRACE("iface %p, size %p, name %p.\n", iface, size, name);
1025

1026 1027 1028 1029 1030 1031 1032
    if (!size || *size < strlen("Builder") || !name)
        return E_INVALIDARG;

    strcpy(name, "Builder");
    *size = sizeof("Builder");

    return D3DRM_OK;
1033 1034
}

1035 1036
HRESULT load_mesh_data(IDirect3DRMMeshBuilder3 *iface, IDirectXFileData *pData,
        D3DRMLOADTEXTURECALLBACK load_texture_proc, void *arg)
1037
{
1038
    struct d3drm_mesh_builder *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1039 1040
    IDirectXFileData *pData2 = NULL;
    const GUID* guid;
1041
    DWORD size;
1042
    BYTE *ptr;
1043 1044
    HRESULT hr;
    HRESULT ret = D3DRMERR_BADOBJECT;
1045 1046 1047 1048 1049 1050 1051 1052
    DWORD* faces_vertex_idx_data = NULL;
    DWORD* faces_vertex_idx_ptr;
    DWORD faces_vertex_idx_size;
    DWORD* faces_normal_idx_data = NULL;
    DWORD* faces_normal_idx_ptr = NULL;
    DWORD* faces_data_ptr;
    DWORD faces_data_size = 0;
    DWORD i;
1053

1054
    TRACE("(%p)->(%p)\n", This, pData);
1055

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    hr = IDirectXFileData_GetName(pData, NULL, &size);
    if (hr != DXFILE_OK)
        return hr;
    if (size)
    {
        This->name = HeapAlloc(GetProcessHeap(), 0, size);
        if (!This->name)
            return E_OUTOFMEMORY;

        hr = IDirectXFileData_GetName(pData, This->name, &size);
        if (hr != DXFILE_OK)
            return hr;
    }

    TRACE("Mesh name is '%s'\n", This->name ? This->name : "");

1072 1073
    This->nb_normals = 0;

1074 1075 1076 1077 1078 1079
    hr = IDirectXFileData_GetData(pData, NULL, &size, (void**)&ptr);
    if (hr != DXFILE_OK)
        goto end;

    This->nb_vertices = *(DWORD*)ptr;
    This->nb_faces = *(DWORD*)(ptr + sizeof(DWORD) + This->nb_vertices * sizeof(D3DVECTOR));
1080 1081
    faces_vertex_idx_size = size - sizeof(DWORD) - This->nb_vertices * sizeof(D3DVECTOR) - sizeof(DWORD);
    faces_vertex_idx_ptr = (DWORD*)(ptr + sizeof(DWORD) + This->nb_vertices * sizeof(D3DVECTOR) + sizeof(DWORD));
1082

1083
    TRACE("Mesh: nb_vertices = %d, nb_faces = %d, faces_vertex_idx_size = %d\n", This->nb_vertices, This->nb_faces, faces_vertex_idx_size);
1084 1085 1086 1087

    This->pVertices = HeapAlloc(GetProcessHeap(), 0, This->nb_vertices * sizeof(D3DVECTOR));
    memcpy(This->pVertices, ptr + sizeof(DWORD), This->nb_vertices * sizeof(D3DVECTOR));

1088 1089 1090 1091
    faces_vertex_idx_ptr = faces_vertex_idx_data = HeapAlloc(GetProcessHeap(), 0, faces_vertex_idx_size);
    memcpy(faces_vertex_idx_data, ptr + sizeof(DWORD) + This->nb_vertices * sizeof(D3DVECTOR) + sizeof(DWORD), faces_vertex_idx_size);

    /* Each vertex index will have its normal index counterpart so just allocate twice the size */
1092
    This->pFaceData = HeapAlloc(GetProcessHeap(), 0, faces_vertex_idx_size * 2);
1093
    faces_data_ptr = (DWORD*)This->pFaceData;
1094 1095 1096

    while (1)
    {
1097 1098 1099
        IDirectXFileObject *object;

        hr =  IDirectXFileData_GetNextObject(pData, &object);
1100 1101
        if (hr == DXFILEERR_NOMOREOBJECTS)
        {
1102
            TRACE("No more object\n");
1103 1104 1105 1106 1107
            break;
        }
        if (hr != DXFILE_OK)
           goto end;

1108 1109
        hr = IDirectXFileObject_QueryInterface(object, &IID_IDirectXFileData, (void**)&pData2);
        IDirectXFileObject_Release(object);
1110 1111 1112
        if (hr != DXFILE_OK)
            goto end;

1113
        hr = IDirectXFileData_GetType(pData2, &guid);
1114 1115 1116
        if (hr != DXFILE_OK)
            goto end;

1117
        TRACE("Found object type whose GUID = %s\n", debugstr_guid(guid));
1118

1119
        if (IsEqualGUID(guid, &TID_D3DRMMeshNormals))
1120
        {
1121 1122
            DWORD nb_faces_normals;
            DWORD faces_normal_idx_size;
1123

1124
            hr = IDirectXFileData_GetData(pData2, NULL, &size, (void**)&ptr);
1125 1126 1127 1128
            if (hr != DXFILE_OK)
                goto end;

            This->nb_normals = *(DWORD*)ptr;
1129
            nb_faces_normals = *(DWORD*)(ptr + sizeof(DWORD) + This->nb_normals * sizeof(D3DVECTOR));
1130

1131 1132 1133
            TRACE("MeshNormals: nb_normals = %d, nb_faces_normals = %d\n", This->nb_normals, nb_faces_normals);
            if (nb_faces_normals != This->nb_faces)
                WARN("nb_face_normals (%d) != nb_faces (%d)\n", nb_faces_normals, This->nb_normals);
1134 1135 1136

            This->pNormals = HeapAlloc(GetProcessHeap(), 0, This->nb_normals * sizeof(D3DVECTOR));
            memcpy(This->pNormals, ptr + sizeof(DWORD), This->nb_normals * sizeof(D3DVECTOR));
1137

1138
            faces_normal_idx_size = size - (2 * sizeof(DWORD) + This->nb_normals * sizeof(D3DVECTOR));
1139 1140
            faces_normal_idx_ptr = faces_normal_idx_data = HeapAlloc(GetProcessHeap(), 0, faces_normal_idx_size);
            memcpy(faces_normal_idx_data, ptr + sizeof(DWORD) + This->nb_normals * sizeof(D3DVECTOR) + sizeof(DWORD), faces_normal_idx_size);
1141
        }
1142
        else if (IsEqualGUID(guid, &TID_D3DRMMeshTextureCoords))
1143
        {
1144
            hr = IDirectXFileData_GetData(pData2, NULL, &size, (void**)&ptr);
1145 1146 1147 1148 1149
            if (hr != DXFILE_OK)
                goto end;

            This->nb_coords2d = *(DWORD*)ptr;

1150
            TRACE("MeshTextureCoords: nb_coords2d = %d\n", This->nb_coords2d);
1151

1152 1153
            This->pCoords2d = HeapAlloc(GetProcessHeap(), 0, This->nb_coords2d * sizeof(*This->pCoords2d));
            memcpy(This->pCoords2d, ptr + sizeof(DWORD), This->nb_coords2d * sizeof(*This->pCoords2d));
1154 1155

        }
1156
        else if (IsEqualGUID(guid, &TID_D3DRMMeshMaterialList))
1157
        {
1158 1159 1160
            DWORD nb_materials;
            DWORD nb_face_indices;
            DWORD data_size;
1161
            IDirectXFileObject *child;
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
            DWORD i = 0;
            float* values;

            TRACE("Process MeshMaterialList\n");

            hr = IDirectXFileData_GetData(pData2, NULL, &size, (void**)&ptr);
            if (hr != DXFILE_OK)
                goto end;

            nb_materials = *(DWORD*)ptr;
            nb_face_indices = *(DWORD*)(ptr + sizeof(DWORD));
            data_size = 2 * sizeof(DWORD) + nb_face_indices * sizeof(DWORD);

            TRACE("nMaterials = %u, nFaceIndexes = %u\n", nb_materials, nb_face_indices);

            if (size != data_size)
                WARN("Returned size %u does not match expected one %u\n", size, data_size);

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
            This->material_indices = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->material_indices) * nb_face_indices);
            if (!This->material_indices)
                goto end;
            memcpy(This->material_indices, ptr + 2 * sizeof(DWORD), sizeof(*This->material_indices) * nb_face_indices),

            This->materials = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->materials) * nb_materials);
            if (!This->materials)
            {
                HeapFree(GetProcessHeap(), 0, This->material_indices);
                goto end;
            }
            This->nb_materials = nb_materials;
1192 1193 1194

            while (SUCCEEDED(hr = IDirectXFileData_GetNextObject(pData2, &child)) && (i < nb_materials))
            {
1195 1196 1197
                IDirectXFileData *data;
                IDirectXFileDataReference *reference;
                IDirectXFileObject *material_child;
1198 1199 1200 1201 1202

                hr = IDirectXFileObject_QueryInterface(child, &IID_IDirectXFileData, (void **)&data);
                if (FAILED(hr))
                {
                    hr = IDirectXFileObject_QueryInterface(child, &IID_IDirectXFileDataReference, (void **)&reference);
Christian Costa's avatar
Christian Costa committed
1203
                    IDirectXFileObject_Release(child);
1204 1205 1206 1207 1208 1209 1210 1211
                    if (FAILED(hr))
                        goto end;

                    hr = IDirectXFileDataReference_Resolve(reference, &data);
                    IDirectXFileDataReference_Release(reference);
                    if (FAILED(hr))
                        goto end;
                }
Christian Costa's avatar
Christian Costa committed
1212 1213 1214 1215
                else
                {
                    IDirectXFileObject_Release(child);
                }
1216

1217
                hr = Direct3DRMMaterial_create(&This->materials[i].material);
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
                if (FAILED(hr))
                {
                    IDirectXFileData_Release(data);
                    goto end;
                }

                hr = IDirectXFileData_GetData(data, NULL, &size, (void**)&ptr);
                if (hr != DXFILE_OK)
                {
                    IDirectXFileData_Release(data);
                    goto end;
                }

                if (size != 44)
                    WARN("Material size %u does not match expected one %u\n", size, 44);

                values = (float*)ptr;

1236 1237
                This->materials[i].color = RGBA_MAKE((BYTE)(values[0] * 255.0f), (BYTE)(values[1] * 255.0f),
                        (BYTE)(values[2] * 255.0f), (BYTE)(values[3] * 255.0f));
1238

1239 1240 1241 1242
                IDirect3DRMMaterial2_SetAmbient(This->materials[i].material, values[0], values [1], values[2]); /* Alpha ignored */
                IDirect3DRMMaterial2_SetPower(This->materials[i].material, values[4]);
                IDirect3DRMMaterial2_SetSpecular(This->materials[i].material, values[5], values[6], values[7]);
                IDirect3DRMMaterial2_SetEmissive(This->materials[i].material, values[8], values[9], values[10]);
1243

1244
                This->materials[i].texture = NULL;
1245

1246 1247 1248
                hr = IDirectXFileData_GetNextObject(data, &material_child);
                if (hr == S_OK)
                {
1249 1250
                    IDirectXFileData *data;
                    char **filename;
1251 1252 1253 1254

                    hr = IDirectXFileObject_QueryInterface(material_child, &IID_IDirectXFileData, (void **)&data);
                    if (FAILED(hr))
                    {
1255
                        IDirectXFileDataReference *reference;
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

                        hr = IDirectXFileObject_QueryInterface(material_child, &IID_IDirectXFileDataReference, (void **)&reference);
                        if (FAILED(hr))
                            goto end;

                        hr = IDirectXFileDataReference_Resolve(reference, &data);
                        IDirectXFileDataReference_Release(reference);
                        if (FAILED(hr))
                            goto end;
                    }

1267
                    hr = IDirectXFileData_GetType(data, &guid);
1268 1269
                    if (hr != DXFILE_OK)
                        goto end;
1270
                    if (!IsEqualGUID(guid, &TID_D3DRMTextureFilename))
1271 1272 1273 1274 1275 1276 1277 1278 1279
                    {
                         WARN("Not a texture filename\n");
                         goto end;
                    }

                    size = 4;
                    hr = IDirectXFileData_GetData(data, NULL, &size, (void**)&filename);
                    if (SUCCEEDED(hr))
                    {
1280 1281 1282
                        if (load_texture_proc)
                        {
                            IDirect3DRMTexture *texture;
1283

1284 1285 1286 1287 1288 1289 1290 1291
                            hr = load_texture_proc(*filename, arg, &texture);
                            if (SUCCEEDED(hr))
                            {
                                hr = IDirect3DTexture_QueryInterface(texture, &IID_IDirect3DRMTexture3, (void**)&This->materials[i].texture);
                                IDirect3DTexture_Release(texture);
                            }
                        }
                        else
1292
                        {
1293
                            HANDLE file;
1294

1295 1296 1297
                            /* If the texture file is not found, no texture is associated with the material */
                            file = CreateFileA(*filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
                            if (file != INVALID_HANDLE_VALUE)
1298
                            {
1299 1300 1301 1302 1303 1304 1305 1306
                                CloseHandle(file);

                                hr = Direct3DRMTexture_create(&IID_IDirect3DRMTexture3, (IUnknown**)&This->materials[i].texture);
                                if (FAILED(hr))
                                {
                                    IDirectXFileData_Release(data);
                                    goto end;
                                }
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
                            }
                        }
                    }
                }
                else if (hr != DXFILEERR_NOMOREOBJECTS)
                {
                    goto end;
                }
                hr = S_OK;

1317 1318 1319 1320
                IDirectXFileData_Release(data);
                i++;
            }
            if (hr == S_OK)
Christian Costa's avatar
Christian Costa committed
1321 1322
            {
                IDirectXFileObject_Release(child);
1323
                WARN("Found more sub-objects than expected\n");
Christian Costa's avatar
Christian Costa committed
1324
            }
1325
            else if (hr != DXFILEERR_NOMOREOBJECTS)
Christian Costa's avatar
Christian Costa committed
1326
            {
1327
                goto end;
Christian Costa's avatar
Christian Costa committed
1328
            }
1329
            hr = S_OK;
1330 1331 1332
        }
        else
        {
1333
            FIXME("Unknown GUID %s, ignoring...\n", debugstr_guid(guid));
1334 1335 1336
        }

        IDirectXFileData_Release(pData2);
1337
        pData2 = NULL;
1338 1339
    }

1340 1341 1342 1343 1344 1345 1346 1347
    if (!This->nb_normals)
    {
        /* Allocate normals, one per vertex */
        This->pNormals = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->nb_vertices * sizeof(D3DVECTOR));
        if (!This->pNormals)
            goto end;
    }

1348 1349 1350 1351
    for (i = 0; i < This->nb_faces; i++)
    {
        DWORD j;
        DWORD nb_face_indexes;
1352
        D3DVECTOR face_normal;
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364

        if (faces_vertex_idx_size < sizeof(DWORD))
            WARN("Not enough data to read number of indices of face %d\n", i);

        nb_face_indexes  = *(faces_data_ptr + faces_data_size++) = *(faces_vertex_idx_ptr++);
        faces_vertex_idx_size--;
        if (faces_normal_idx_data && (*(faces_normal_idx_ptr++) != nb_face_indexes))
            WARN("Faces indices number mismatch\n");

        if (faces_vertex_idx_size < (nb_face_indexes * sizeof(DWORD)))
            WARN("Not enough data to read all indices of face %d\n", i);

1365 1366 1367 1368 1369 1370 1371 1372
        if (!This->nb_normals)
        {
            /* Compute face normal */
            if (nb_face_indexes > 2)
            {
                D3DVECTOR a, b;

                D3DRMVectorSubtract(&a, &This->pVertices[faces_vertex_idx_ptr[2]], &This->pVertices[faces_vertex_idx_ptr[1]]);
1373
                D3DRMVectorSubtract(&b, &This->pVertices[faces_vertex_idx_ptr[0]], &This->pVertices[faces_vertex_idx_ptr[1]]);
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
                D3DRMVectorCrossProduct(&face_normal, &a, &b);
                D3DRMVectorNormalize(&face_normal);
            }
            else
            {
                face_normal.u1.x = 0.0f;
                face_normal.u2.y = 0.0f;
                face_normal.u3.z = 0.0f;
            }
        }

1385 1386 1387
        for (j = 0; j < nb_face_indexes; j++)
        {
            /* Copy vertex index */
1388
            *(faces_data_ptr + faces_data_size++) = *faces_vertex_idx_ptr;
1389
            /* Copy normal index */
1390
            if (This->nb_normals)
1391 1392 1393 1394 1395 1396
            {
                /* Read from x file */
                *(faces_data_ptr + faces_data_size++) = *(faces_normal_idx_ptr++);
            }
            else
            {
1397
                DWORD vertex_idx = *faces_vertex_idx_ptr;
1398 1399 1400
                if (vertex_idx >= This->nb_vertices)
                {
                    WARN("Found vertex index %u but only %u vertices available => use index 0\n", vertex_idx, This->nb_vertices);
1401
                    vertex_idx = 0;
1402
                }
1403 1404 1405
                *(faces_data_ptr + faces_data_size++) = vertex_idx;
                /* Add face normal to vertex normal */
                D3DRMVectorAdd(&This->pNormals[vertex_idx], &This->pNormals[vertex_idx], &face_normal);
1406
            }
1407
            faces_vertex_idx_ptr++;
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
        }
        faces_vertex_idx_size -= nb_face_indexes;
    }

    /* Last DWORD must be 0 */
    *(faces_data_ptr + faces_data_size++) = 0;

    /* Set size (in number of DWORD) of all faces data */
    This->face_data_size = faces_data_size;

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
    if (!This->nb_normals)
    {
        /* Normalize all normals */
        for (i = 0; i < This->nb_vertices; i++)
        {
            D3DRMVectorNormalize(&This->pNormals[i]);
        }
        This->nb_normals = This->nb_vertices;
    }

1428 1429 1430 1431
    /* If there is no texture coordinates, generate default texture coordinates (0.0f, 0.0f) for each vertex */
    if (!This->pCoords2d)
    {
        This->nb_coords2d = This->nb_vertices;
1432
        This->pCoords2d = HeapAlloc(GetProcessHeap(), 0, This->nb_coords2d * sizeof(*This->pCoords2d));
1433 1434 1435 1436 1437 1438 1439
        for (i = 0; i < This->nb_coords2d; i++)
        {
            This->pCoords2d[i].u = 0.0f;
            This->pCoords2d[i].v = 0.0f;
        }
    }

1440 1441
    TRACE("Mesh data loaded successfully\n");

1442 1443 1444
    ret = D3DRM_OK;

end:
1445

1446 1447
    HeapFree(GetProcessHeap(), 0, faces_normal_idx_data);
    HeapFree(GetProcessHeap(), 0, faces_vertex_idx_data);
1448 1449 1450 1451

    return ret;
}

1452 1453
static HRESULT WINAPI d3drm_mesh_builder3_Load(IDirect3DRMMeshBuilder3 *iface, void *filename,
        void *name, D3DRMLOADOPTIONS loadflags, D3DRMLOADTEXTURE3CALLBACK cb, void *arg)
1454
{
1455
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1456
    DXFILELOADOPTIONS load_options;
1457 1458 1459 1460
    IDirectXFile *dxfile = NULL;
    IDirectXFileEnumObject *enum_object = NULL;
    IDirectXFileData *data = NULL;
    const GUID* guid;
1461
    DWORD size;
1462
    struct d3drm_file_header *header;
1463 1464 1465
    HRESULT hr;
    HRESULT ret = D3DRMERR_BADOBJECT;

1466 1467
    TRACE("iface %p, filename %p, name %p, loadflags %#x, cb %p, arg %p.\n",
            iface, filename, name, loadflags, cb, arg);
1468

1469
    clean_mesh_builder_data(mesh_builder);
1470 1471 1472 1473 1474

    if (loadflags == D3DRMLOAD_FROMMEMORY)
    {
        load_options = DXFILELOAD_FROMMEMORY;
    }
1475 1476 1477 1478 1479
    else if (loadflags == D3DRMLOAD_FROMFILE)
    {
        load_options = DXFILELOAD_FROMFILE;
        TRACE("Loading from file %s\n", debugstr_a(filename));
    }
1480 1481 1482 1483 1484 1485
    else
    {
        FIXME("Load options %d not supported yet\n", loadflags);
        return E_NOTIMPL;
    }

1486
    hr = DirectXFileCreate(&dxfile);
1487 1488 1489
    if (hr != DXFILE_OK)
        goto end;

1490
    hr = IDirectXFile_RegisterTemplates(dxfile, templates, strlen(templates));
1491 1492 1493
    if (hr != DXFILE_OK)
        goto end;

1494
    hr = IDirectXFile_CreateEnumObject(dxfile, filename, load_options, &enum_object);
1495 1496 1497
    if (hr != DXFILE_OK)
        goto end;

1498
    hr = IDirectXFileEnumObject_GetNextDataObject(enum_object, &data);
1499 1500 1501
    if (hr != DXFILE_OK)
        goto end;

1502
    hr = IDirectXFileData_GetType(data, &guid);
1503 1504 1505
    if (hr != DXFILE_OK)
        goto end;

1506
    TRACE("Found object type whose GUID = %s\n", debugstr_guid(guid));
1507

1508
    if (!IsEqualGUID(guid, &TID_DXFILEHeader))
1509 1510 1511 1512 1513
    {
        ret = D3DRMERR_BADFILE;
        goto end;
    }

1514
    hr = IDirectXFileData_GetData(data, NULL, &size, (void**)&header);
1515
    if ((hr != DXFILE_OK) || (size != sizeof(*header)))
1516 1517
        goto end;

1518
    TRACE("Version is %u.%u, flags %#x.\n", header->major, header->minor, header->flags);
1519 1520

    /* Version must be 1.0.x */
1521
    if ((header->major != 1) || (header->minor != 0))
1522 1523 1524 1525 1526
    {
        ret = D3DRMERR_BADFILE;
        goto end;
    }

1527 1528
    IDirectXFileData_Release(data);
    data = NULL;
1529

1530
    hr = IDirectXFileEnumObject_GetNextDataObject(enum_object, &data);
1531 1532 1533 1534 1535 1536
    if (hr != DXFILE_OK)
    {
        ret = D3DRMERR_NOTFOUND;
        goto end;
    }

1537
    hr = IDirectXFileData_GetType(data, &guid);
1538 1539 1540
    if (hr != DXFILE_OK)
        goto end;

1541
    TRACE("Found object type whose GUID = %s\n", debugstr_guid(guid));
1542

1543
    if (!IsEqualGUID(guid, &TID_D3DRMMesh))
1544 1545 1546 1547 1548
    {
        ret = D3DRMERR_NOTFOUND;
        goto end;
    }

1549
    /* We don't care about the texture interface version since we rely on QueryInterface */
1550
    hr = load_mesh_data(iface, data, (D3DRMLOADTEXTURECALLBACK)cb, arg);
1551 1552 1553 1554 1555
    if (hr == S_OK)
        ret = D3DRM_OK;

end:

1556 1557 1558 1559 1560 1561
    if (data)
        IDirectXFileData_Release(data);
    if (enum_object)
        IDirectXFileEnumObject_Release(enum_object);
    if (dxfile)
        IDirectXFile_Release(dxfile);
1562 1563

    if (ret != D3DRM_OK)
1564
        clean_mesh_builder_data(mesh_builder);
1565 1566

    return ret;
1567 1568
}

1569 1570
static HRESULT WINAPI d3drm_mesh_builder3_Save(IDirect3DRMMeshBuilder3 *iface,
        const char *filename, D3DRMXOFFORMAT format, D3DRMSAVEOPTIONS flags)
1571
{
1572 1573
    FIXME("iface %p, filename %s, format %#x, flags %#x stub!\n",
            iface, debugstr_a(filename), format, flags);
1574 1575 1576 1577

    return E_NOTIMPL;
}

1578 1579
static HRESULT WINAPI d3drm_mesh_builder3_Scale(IDirect3DRMMeshBuilder3 *iface,
        D3DVALUE sx, D3DVALUE sy, D3DVALUE sz)
1580
{
1581
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1582
    DWORD i;
1583

1584
    TRACE("iface %p, sx %.8e, sy %.8e, sz %.8e.\n", iface, sx, sy, sz);
1585

1586
    for (i = 0; i < mesh_builder->nb_vertices; ++i)
1587
    {
1588 1589 1590
        mesh_builder->pVertices[i].u1.x *= sx;
        mesh_builder->pVertices[i].u2.y *= sy;
        mesh_builder->pVertices[i].u3.z *= sz;
1591 1592 1593 1594 1595
    }

    /* Normals are not affected by Scale */

    return D3DRM_OK;
1596 1597
}

1598 1599
static HRESULT WINAPI d3drm_mesh_builder3_Translate(IDirect3DRMMeshBuilder3 *iface,
        D3DVALUE tx, D3DVALUE ty, D3DVALUE tz)
1600
{
1601
    FIXME("iface %p, tx %.8e, ty %.8e, tz %.8e stub!\n", iface, tx, ty, tz);
1602 1603 1604 1605

    return E_NOTIMPL;
}

1606 1607
static HRESULT WINAPI d3drm_mesh_builder3_SetColorSource(IDirect3DRMMeshBuilder3 *iface,
        D3DRMCOLORSOURCE source)
1608
{
1609
    FIXME("iface %p, source %#x stub!\n", iface, source);
1610 1611 1612 1613

    return E_NOTIMPL;
}

1614
static HRESULT WINAPI d3drm_mesh_builder3_GetBox(IDirect3DRMMeshBuilder3 *iface, D3DRMBOX *box)
1615
{
1616
    FIXME("iface %p, box %p stub!\n", iface, box);
1617 1618 1619 1620

    return E_NOTIMPL;
}

1621 1622
static HRESULT WINAPI d3drm_mesh_builder3_GenerateNormals(IDirect3DRMMeshBuilder3 *iface,
        D3DVALUE crease, DWORD flags)
1623
{
1624
    FIXME("iface %p, crease %.8e, flags %#x stub!\n", iface, crease, flags);
1625 1626 1627 1628

    return E_NOTIMPL;
}

1629
static D3DRMCOLORSOURCE WINAPI d3drm_mesh_builder3_GetColorSource(IDirect3DRMMeshBuilder3 *iface)
1630
{
1631
    FIXME("iface %p stub!\n", iface);
1632 1633 1634 1635

    return E_NOTIMPL;
}

1636
static HRESULT WINAPI d3drm_mesh_builder3_AddMesh(IDirect3DRMMeshBuilder3 *iface, IDirect3DRMMesh *mesh)
1637
{
1638
    FIXME("iface %p, mesh %p stub!\n", iface, mesh);
1639 1640 1641 1642

    return E_NOTIMPL;
}

1643
static HRESULT WINAPI d3drm_mesh_builder3_AddMeshBuilder(IDirect3DRMMeshBuilder3 *iface,
1644
        IDirect3DRMMeshBuilder3 *mesh_builder, DWORD flags)
1645
{
1646
    FIXME("iface %p, mesh_builder %p, flags %#x stub!\n", iface, mesh_builder, flags);
1647 1648 1649 1650

    return E_NOTIMPL;
}

1651
static HRESULT WINAPI d3drm_mesh_builder3_AddFrame(IDirect3DRMMeshBuilder3 *iface, IDirect3DRMFrame3 *frame)
1652
{
1653
    FIXME("iface %p, frame %p stub!\n", iface, frame);
1654 1655 1656 1657

    return E_NOTIMPL;
}

1658
static HRESULT WINAPI d3drm_mesh_builder3_AddFace(IDirect3DRMMeshBuilder3 *iface, IDirect3DRMFace2 *face)
1659
{
1660
    FIXME("iface %p, face %p stub!\n", iface, face);
1661 1662 1663 1664

    return E_NOTIMPL;
}

1665
static HRESULT WINAPI d3drm_mesh_builder3_AddFaces(IDirect3DRMMeshBuilder3 *iface,
1666 1667
        DWORD vertex_count, D3DVECTOR *vertices, DWORD normal_count, D3DVECTOR *normals,
        DWORD *face_data, IDirect3DRMFaceArray **array)
1668
{
1669 1670
    FIXME("iface %p, vertex_count %u, vertices %p, normal_count %u, normals %p, face_data %p array %p stub!\n",
            iface, vertex_count, vertices, normal_count, normals, face_data, array);
1671 1672 1673 1674

    return E_NOTIMPL;
}

1675 1676
static HRESULT WINAPI d3drm_mesh_builder3_ReserveSpace(IDirect3DRMMeshBuilder3 *iface,
        DWORD vertex_count, DWORD normal_count, DWORD face_count)
1677
{
1678 1679
    FIXME("iface %p, vertex_count %u, normal_count %u, face_count %u stub!\n",
            iface, vertex_count, normal_count, face_count);
1680 1681 1682 1683

    return E_NOTIMPL;
}

1684 1685
static HRESULT WINAPI d3drm_mesh_builder3_SetColorRGB(IDirect3DRMMeshBuilder3 *iface,
        D3DVALUE red, D3DVALUE green, D3DVALUE blue)
1686
{
1687
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1688

1689
    TRACE("iface %p, red %.8e, green %.8e, blue %.8e.\n", iface, red, green, blue);
1690

1691
    mesh_builder->color = RGBA_MAKE((BYTE)(red * 255.0f), (BYTE)(green * 255.0f), (BYTE)(blue * 255.0f), 0xff);
1692 1693

    return D3DRM_OK;
1694 1695
}

1696
static HRESULT WINAPI d3drm_mesh_builder3_SetColor(IDirect3DRMMeshBuilder3 *iface, D3DCOLOR color)
1697
{
1698
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1699

1700
    TRACE("iface %p, color 0x%08x.\n", iface, color);
1701

1702
    mesh_builder->color = color;
1703 1704

    return D3DRM_OK;
1705 1706
}

1707
static HRESULT WINAPI d3drm_mesh_builder3_SetTexture(IDirect3DRMMeshBuilder3 *iface,
1708
        IDirect3DRMTexture3 *texture)
1709
{
1710
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1711

1712
    TRACE("iface %p, texture %p.\n", iface, texture);
1713

1714 1715
    if (texture)
        IDirect3DRMTexture3_AddRef(texture);
1716 1717 1718
    if (mesh_builder->texture)
        IDirect3DRMTexture3_Release(mesh_builder->texture);
    mesh_builder->texture = texture;
1719 1720

    return D3DRM_OK;
1721 1722
}

1723
static HRESULT WINAPI d3drm_mesh_builder3_SetMaterial(IDirect3DRMMeshBuilder3 *iface,
1724
        IDirect3DRMMaterial2 *material)
1725
{
1726
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1727

1728
    TRACE("iface %p, material %p.\n", iface, material);
1729

1730 1731
    if (material)
        IDirect3DRMTexture2_AddRef(material);
1732 1733 1734
    if (mesh_builder->material)
        IDirect3DRMTexture2_Release(mesh_builder->material);
    mesh_builder->material = material;
1735 1736

    return D3DRM_OK;
1737 1738
}

1739 1740
static HRESULT WINAPI d3drm_mesh_builder3_SetTextureTopology(IDirect3DRMMeshBuilder3 *iface,
        BOOL wrap_u, BOOL wrap_v)
1741
{
1742
    FIXME("iface %p, wrap_u %#x, wrap_v %#x stub!\n", iface, wrap_u, wrap_v);
1743 1744 1745 1746

    return E_NOTIMPL;
}

1747 1748
static HRESULT WINAPI d3drm_mesh_builder3_SetQuality(IDirect3DRMMeshBuilder3 *iface,
        D3DRMRENDERQUALITY quality)
1749
{
1750
    FIXME("iface %p, quality %#x stub!\n", iface, quality);
1751 1752 1753 1754

    return E_NOTIMPL;
}

1755 1756
static HRESULT WINAPI d3drm_mesh_builder3_SetPerspective(IDirect3DRMMeshBuilder3 *iface,
        BOOL enable)
1757
{
1758
    FIXME("iface %p, enable %#x stub!\n", iface, enable);
1759 1760 1761 1762

    return E_NOTIMPL;
}

1763 1764
static HRESULT WINAPI d3drm_mesh_builder3_SetVertex(IDirect3DRMMeshBuilder3 *iface,
        DWORD index, D3DVALUE x, D3DVALUE y, D3DVALUE z)
1765
{
1766
    FIXME("iface %p, index %u, x %.8e, y %.8e, z %.8e stub!\n", iface, index, x, y, z);
1767 1768 1769 1770

    return E_NOTIMPL;
}

1771 1772
static HRESULT WINAPI d3drm_mesh_builder3_SetNormal(IDirect3DRMMeshBuilder3 *iface,
        DWORD index, D3DVALUE x, D3DVALUE y, D3DVALUE z)
1773
{
1774
    FIXME("iface %p, index %u, x %.8e, y %.8e, z %.8e stub!\n", iface, index, x, y, z);
1775 1776 1777 1778

    return E_NOTIMPL;
}

1779 1780
static HRESULT WINAPI d3drm_mesh_builder3_SetTextureCoordinates(IDirect3DRMMeshBuilder3 *iface,
        DWORD index, D3DVALUE u, D3DVALUE v)
1781
{
1782
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1783

1784
    TRACE("iface %p, index %u, u %.8e, v %.8e.\n", iface, index, u, v);
1785

1786
    if (index >= mesh_builder->nb_coords2d)
1787 1788
        return D3DRMERR_BADVALUE;

1789 1790
    mesh_builder->pCoords2d[index].u = u;
    mesh_builder->pCoords2d[index].v = v;
1791 1792

    return D3DRM_OK;
1793 1794
}

1795 1796
static HRESULT WINAPI d3drm_mesh_builder3_SetVertexColor(IDirect3DRMMeshBuilder3 *iface,
        DWORD index, D3DCOLOR color)
1797
{
1798
    FIXME("iface %p, index %u, color 0x%08x stub!\n", iface, index, color);
1799 1800 1801 1802

    return E_NOTIMPL;
}

1803 1804
static HRESULT WINAPI d3drm_mesh_builder3_SetVertexColorRGB(IDirect3DRMMeshBuilder3 *iface,
        DWORD index, D3DVALUE red, D3DVALUE green, D3DVALUE blue)
1805
{
1806 1807
    FIXME("iface %p, index %u, red %.8e, green %.8e, blue %.8e stub!\n",
            iface, index, red, green, blue);
1808 1809 1810 1811

    return E_NOTIMPL;
}

1812
static HRESULT WINAPI d3drm_mesh_builder3_GetFaces(IDirect3DRMMeshBuilder3 *iface,
1813
        IDirect3DRMFaceArray **array)
1814
{
1815
    FIXME("iface %p, array %p stub!\n", iface, array);
1816 1817 1818 1819

    return E_NOTIMPL;
}

1820 1821 1822
static HRESULT WINAPI d3drm_mesh_builder3_GetGeometry(IDirect3DRMMeshBuilder3 *iface,
        DWORD *vertex_count, D3DVECTOR *vertices, DWORD *normal_count, D3DVECTOR *normals,
        DWORD *face_data_size, DWORD *face_data)
1823
{
1824 1825 1826
    FIXME("iface %p, vertex_count %p, vertices %p, normal_count %p, normals %p, "
            "face_data_size %p, face_data %p stub!\n",
            iface, vertex_count, vertices, normal_count, normals, face_data_size, face_data);
1827 1828 1829 1830

    return E_NOTIMPL;
}

1831 1832
static HRESULT WINAPI d3drm_mesh_builder3_GetTextureCoordinates(IDirect3DRMMeshBuilder3 *iface,
        DWORD index, D3DVALUE *u, D3DVALUE *v)
1833
{
1834
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1835

1836
    TRACE("iface %p, index %u, u %p, v %p.\n", iface, index, u, v);
1837

1838
    if (index >= mesh_builder->nb_coords2d)
1839
        return D3DRMERR_BADVALUE;
1840

1841 1842
    *u = mesh_builder->pCoords2d[index].u;
    *v = mesh_builder->pCoords2d[index].v;
1843 1844

    return D3DRM_OK;
1845 1846
}

1847 1848
static int WINAPI d3drm_mesh_builder3_AddVertex(IDirect3DRMMeshBuilder3 *iface,
        D3DVALUE x, D3DVALUE y, D3DVALUE z)
1849
{
1850
    FIXME("iface %p, x %.8e, y %.8e, z %.8e stub!\n", iface, x, y, z);
1851 1852 1853 1854

    return 0;
}

1855 1856
static int WINAPI d3drm_mesh_builder3_AddNormal(IDirect3DRMMeshBuilder3 *iface,
        D3DVALUE x, D3DVALUE y, D3DVALUE z)
1857
{
1858
    FIXME("iface %p, x %.8e, y %.8e, z %.8e stub!\n", iface, x, y, z);
1859 1860 1861 1862

    return 0;
}

1863
static HRESULT WINAPI d3drm_mesh_builder3_CreateFace(IDirect3DRMMeshBuilder3 *iface, IDirect3DRMFace2 **face)
1864
{
1865
    TRACE("iface %p, face %p.\n", iface, face);
1866

1867
    return Direct3DRMFace_create(&IID_IDirect3DRMFace2, (IUnknown **)face);
1868 1869
}

1870
static D3DRMRENDERQUALITY WINAPI d3drm_mesh_builder3_GetQuality(IDirect3DRMMeshBuilder3 *iface)
1871
{
1872
    FIXME("iface %p stub!\n", iface);
1873 1874 1875 1876

    return 0;
}

1877
static BOOL WINAPI d3drm_mesh_builder3_GetPerspective(IDirect3DRMMeshBuilder3 *iface)
1878
{
1879
    FIXME("iface %p stub!\n", iface);
1880 1881 1882 1883

    return FALSE;
}

1884
static int WINAPI d3drm_mesh_builder3_GetFaceCount(IDirect3DRMMeshBuilder3 *iface)
1885
{
1886
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1887

1888
    TRACE("iface %p.\n", iface);
1889

1890
    return mesh_builder->nb_faces;
1891 1892
}

1893
static int WINAPI d3drm_mesh_builder3_GetVertexCount(IDirect3DRMMeshBuilder3 *iface)
1894
{
1895
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
1896

1897
    TRACE("iface %p.\n", iface);
1898

1899
    return mesh_builder->nb_vertices;
1900 1901
}

1902 1903
static D3DCOLOR WINAPI d3drm_mesh_builder3_GetVertexColor(IDirect3DRMMeshBuilder3 *iface,
        DWORD index)
1904
{
1905
    FIXME("iface %p, index %u stub!\n", iface, index);
1906 1907 1908 1909

    return 0;
}

1910
static HRESULT WINAPI d3drm_mesh_builder3_CreateMesh(IDirect3DRMMeshBuilder3 *iface, IDirect3DRMMesh **mesh)
1911
{
1912
    struct d3drm_mesh_builder *This = impl_from_IDirect3DRMMeshBuilder3(iface);
1913 1914 1915
    HRESULT hr;
    D3DRMGROUPINDEX group;

1916
    TRACE("iface %p, mesh %p.\n", iface, mesh);
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927

    if (!mesh)
        return E_POINTER;

    hr = Direct3DRMMesh_create(mesh);
    if (FAILED(hr))
        return hr;

    /* If there is mesh data, create a group and put data inside */
    if (This->nb_vertices)
    {
1928 1929
        DWORD i, j;
        int k;
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
        D3DRMVERTEX* vertices;

        vertices = HeapAlloc(GetProcessHeap(), 0, This->nb_vertices * sizeof(D3DRMVERTEX));
        if (!vertices)
        {
            IDirect3DRMMesh_Release(*mesh);
            return E_OUTOFMEMORY;
        }
        for (i = 0; i < This->nb_vertices; i++)
            vertices[i].position = This->pVertices[i];
        hr = IDirect3DRMMesh_SetVertices(*mesh, 0, 0, This->nb_vertices, vertices);
        HeapFree(GetProcessHeap(), 0, vertices);
1942

1943 1944
        /* Groups are in reverse order compared to materials list in X file */
        for (k = This->nb_materials - 1; k >= 0; k--)
1945
        {
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
            unsigned* face_data;
            unsigned* out_ptr;
            DWORD* in_ptr = This->pFaceData;
            ULONG vertex_per_face = 0;
            BOOL* used_vertices;
            unsigned nb_vertices = 0;
            unsigned nb_faces = 0;

            used_vertices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->face_data_size * sizeof(*used_vertices));
            if (!used_vertices)
            {
                IDirect3DRMMesh_Release(*mesh);
                return E_OUTOFMEMORY;
            }
1960

1961 1962 1963
            face_data = HeapAlloc(GetProcessHeap(), 0, This->face_data_size * sizeof(*face_data));
            if (!face_data)
            {
1964
                HeapFree(GetProcessHeap(), 0, used_vertices);
1965 1966 1967 1968
                IDirect3DRMMesh_Release(*mesh);
                return E_OUTOFMEMORY;
            }
            out_ptr = face_data;
1969

1970 1971
            /* If all faces have the same number of vertex, set vertex_per_face */
            for (i = 0; i < This->nb_faces; i++)
1972
            {
1973 1974 1975 1976 1977 1978 1979 1980
                /* Process only faces belonging to the group */
                if (This->material_indices[i] == k)
                {
                    if (vertex_per_face && (vertex_per_face != *in_ptr))
                        break;
                    vertex_per_face = *in_ptr;
                }
                in_ptr += 1 + *in_ptr * 2;
1981
            }
1982 1983
            if (i != This->nb_faces)
                vertex_per_face = 0;
1984

1985 1986 1987 1988 1989
            /* Put only vertex indices */
            in_ptr = This->pFaceData;
            for (i = 0; i < This->nb_faces; i++)
            {
                DWORD nb_indices = *in_ptr++;
1990

1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
                /* Skip faces not belonging to the group */
                if (This->material_indices[i] != k)
                {
                    in_ptr += 2 * nb_indices;
                    continue;
                }

                /* Don't put nb indices when vertex_per_face is set */
                if (vertex_per_face)
                    *out_ptr++ = nb_indices;
2001

2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
                for (j = 0; j < nb_indices; j++)
                {
                    *out_ptr = *in_ptr++;
                    used_vertices[*out_ptr++] = TRUE;
                    /* Skip normal index */
                    in_ptr++;
                }

                nb_faces++;
            }

            for (i = 0; i < This->nb_vertices; i++)
                if (used_vertices[i])
                    nb_vertices++;

            hr = IDirect3DRMMesh_AddGroup(*mesh, nb_vertices, nb_faces, vertex_per_face, face_data, &group);
            HeapFree(GetProcessHeap(), 0, used_vertices);
            HeapFree(GetProcessHeap(), 0, face_data);
            if (SUCCEEDED(hr))
                hr = IDirect3DRMMesh_SetGroupColor(*mesh, group, This->materials[k].color);
            if (SUCCEEDED(hr))
2023 2024
                hr = IDirect3DRMMesh_SetGroupMaterial(*mesh, group,
                        (IDirect3DRMMaterial *)This->materials[k].material);
2025 2026
            if (SUCCEEDED(hr) && This->materials[k].texture)
            {
2027
                IDirect3DRMTexture *texture;
2028

Henri Verbeet's avatar
Henri Verbeet committed
2029 2030
                IDirect3DRMTexture3_QueryInterface(This->materials[k].texture,
                        &IID_IDirect3DRMTexture, (void **)&texture);
2031 2032 2033 2034 2035 2036 2037 2038
                hr = IDirect3DRMMesh_SetGroupTexture(*mesh, group, texture);
                IDirect3DRMTexture_Release(texture);
            }
            if (FAILED(hr))
            {
                IDirect3DRMMesh_Release(*mesh);
                return hr;
            }
2039
        }
2040
    }
2041

2042
    return D3DRM_OK;
2043 2044
}

2045
static HRESULT WINAPI d3drm_mesh_builder3_GetFace(IDirect3DRMMeshBuilder3 *iface,
2046
        DWORD index, IDirect3DRMFace2 **face)
2047
{
2048
    FIXME("iface %p, index %u, face %p stub!\n", iface, index, face);
2049 2050 2051 2052

    return E_NOTIMPL;
}

2053
static HRESULT WINAPI d3drm_mesh_builder3_GetVertex(IDirect3DRMMeshBuilder3 *iface,
2054
        DWORD index, D3DVECTOR *vector)
2055
{
2056
    FIXME("iface %p, index %u, vector %p stub!\n", iface, index, vector);
2057 2058 2059 2060

    return E_NOTIMPL;
}

2061
static HRESULT WINAPI d3drm_mesh_builder3_GetNormal(IDirect3DRMMeshBuilder3 *iface,
2062
        DWORD index, D3DVECTOR *vector)
2063
{
2064
    FIXME("iface %p, index %u, vector %p stub!\n", iface, index, vector);
2065 2066 2067 2068

    return E_NOTIMPL;
}

2069 2070
static HRESULT WINAPI d3drm_mesh_builder3_DeleteVertices(IDirect3DRMMeshBuilder3 *iface,
        DWORD start_idx, DWORD count)
2071
{
2072
    FIXME("iface %p, start_idx %u, count %u stub!\n", iface, start_idx, count);
2073 2074 2075 2076

    return E_NOTIMPL;
}

2077 2078
static HRESULT WINAPI d3drm_mesh_builder3_DeleteNormals(IDirect3DRMMeshBuilder3 *iface,
        DWORD start_idx, DWORD count)
2079
{
2080
    FIXME("iface %p, start_idx %u, count %u stub!\n", iface, start_idx, count);
2081 2082 2083 2084

    return E_NOTIMPL;
}

2085
static HRESULT WINAPI d3drm_mesh_builder3_DeleteFace(IDirect3DRMMeshBuilder3 *iface, IDirect3DRMFace2 *face)
2086
{
2087
    FIXME("iface %p, face %p stub!\n", iface, face);
2088 2089 2090 2091

    return E_NOTIMPL;
}

2092
static HRESULT WINAPI d3drm_mesh_builder3_Empty(IDirect3DRMMeshBuilder3 *iface, DWORD flags)
2093
{
2094
    FIXME("iface %p, flags %#x stub!\n", iface, flags);
2095 2096 2097 2098

    return E_NOTIMPL;
}

2099
static HRESULT WINAPI d3drm_mesh_builder3_Optimize(IDirect3DRMMeshBuilder3 *iface, DWORD flags)
2100
{
2101
    FIXME("iface %p, flags %#x stub!\n", iface, flags);
2102 2103 2104 2105

    return E_NOTIMPL;
}

2106 2107
static HRESULT WINAPI d3drm_mesh_builder3_AddFacesIndexed(IDirect3DRMMeshBuilder3 *iface,
        DWORD flags, DWORD *indices, DWORD *start_idx, DWORD *count)
2108
{
2109 2110
    FIXME("iface %p, flags %#x, indices %p, start_idx %p, count %p stub!\n",
            iface, flags, indices, start_idx, count);
2111 2112 2113 2114

    return E_NOTIMPL;
}

2115
static HRESULT WINAPI d3drm_mesh_builder3_CreateSubMesh(IDirect3DRMMeshBuilder3 *iface, IUnknown **mesh)
2116
{
Henri Verbeet's avatar
Henri Verbeet committed
2117
    FIXME("iface %p, mesh %p stub!\n", iface, mesh);
2118 2119 2120 2121

    return E_NOTIMPL;
}

2122
static HRESULT WINAPI d3drm_mesh_builder3_GetParentMesh(IDirect3DRMMeshBuilder3 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
2123
        DWORD flags, IUnknown **parent)
2124
{
Henri Verbeet's avatar
Henri Verbeet committed
2125
    FIXME("iface %p, flags %#x, parent %p stub!\n", iface, flags, parent);
2126 2127 2128 2129

    return E_NOTIMPL;
}

2130
static HRESULT WINAPI d3drm_mesh_builder3_GetSubMeshes(IDirect3DRMMeshBuilder3 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
2131
        DWORD *count, IUnknown **meshes)
2132
{
Henri Verbeet's avatar
Henri Verbeet committed
2133
    FIXME("iface %p, count %p, meshes %p stub!\n", iface, count, meshes);
2134 2135 2136 2137

    return E_NOTIMPL;
}

2138
static HRESULT WINAPI d3drm_mesh_builder3_DeleteSubMesh(IDirect3DRMMeshBuilder3 *iface, IUnknown *mesh)
2139
{
Henri Verbeet's avatar
Henri Verbeet committed
2140
    FIXME("iface %p, mesh %p stub!\n", iface, mesh);
2141 2142 2143 2144

    return E_NOTIMPL;
}

2145
static HRESULT WINAPI d3drm_mesh_builder3_Enable(IDirect3DRMMeshBuilder3 *iface, DWORD index)
2146
{
2147
    FIXME("iface %p, index %u stub!\n", iface, index);
2148 2149 2150 2151

    return E_NOTIMPL;
}

2152
static HRESULT WINAPI d3drm_mesh_builder3_GetEnable(IDirect3DRMMeshBuilder3 *iface, DWORD *indices)
2153
{
2154
    FIXME("iface %p, indices %p stub!\n", iface, indices);
2155 2156 2157 2158

    return E_NOTIMPL;
}

2159
static HRESULT WINAPI d3drm_mesh_builder3_AddTriangles(IDirect3DRMMeshBuilder3 *iface,
Henri Verbeet's avatar
Henri Verbeet committed
2160
        DWORD flags, DWORD format, DWORD vertex_count, void *data)
2161
{
Henri Verbeet's avatar
Henri Verbeet committed
2162 2163
    FIXME("iface %p, flags %#x, format %#x, vertex_count %u, data %p stub!\n",
            iface, flags, format, vertex_count, data);
2164 2165 2166 2167

    return E_NOTIMPL;
}

2168 2169
static HRESULT WINAPI d3drm_mesh_builder3_SetVertices(IDirect3DRMMeshBuilder3 *iface,
        DWORD start_idx, DWORD count, D3DVECTOR *vector)
2170
{
2171
    FIXME("iface %p, start_idx %u, count %u, vector %p stub!\n", iface, start_idx, count, vector);
2172 2173 2174 2175

    return E_NOTIMPL;
}

2176 2177
static HRESULT WINAPI d3drm_mesh_builder3_GetVertices(IDirect3DRMMeshBuilder3 *iface,
        DWORD start_idx, DWORD *vertex_count, D3DVECTOR *vertices)
2178
{
2179 2180
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
    DWORD count = mesh_builder->nb_vertices - start_idx;
2181

2182 2183
    TRACE("iface %p, start_idx %u, vertex_count %p, vertices %p.\n",
            iface, start_idx, vertex_count, vertices);
2184

2185 2186 2187 2188
    if (vertex_count)
        *vertex_count = count;
    if (vertices && mesh_builder->nb_vertices)
        memcpy(vertices, mesh_builder->pVertices + start_idx, count * sizeof(*vertices));
2189 2190

    return D3DRM_OK;
2191 2192
}

2193 2194
static HRESULT WINAPI d3drm_mesh_builder3_SetNormals(IDirect3DRMMeshBuilder3 *iface,
        DWORD start_idx, DWORD count, D3DVECTOR *vector)
2195
{
2196 2197
    FIXME("iface %p, start_idx %u, count %u, vector %p stub!\n",
            iface, start_idx, count, vector);
2198 2199 2200 2201

    return E_NOTIMPL;
}

2202 2203
static HRESULT WINAPI d3drm_mesh_builder3_GetNormals(IDirect3DRMMeshBuilder3 *iface,
        DWORD start_idx, DWORD *normal_count, D3DVECTOR *normals)
2204
{
2205 2206
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);
    DWORD count = mesh_builder->nb_normals - start_idx;
2207

2208 2209
    TRACE("iface %p, start_idx %u, normal_count %p, normals %p stub!\n",
            iface, start_idx, normal_count, normals);
2210

2211 2212 2213 2214
    if (normal_count)
        *normal_count = count;
    if (normals && mesh_builder->nb_normals)
        memcpy(normals, mesh_builder->pNormals + start_idx, count * sizeof(*normals));
2215 2216

    return D3DRM_OK;
2217 2218
}

2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
static int WINAPI d3drm_mesh_builder3_GetNormalCount(IDirect3DRMMeshBuilder3 *iface)
{
    struct d3drm_mesh_builder *mesh_builder = impl_from_IDirect3DRMMeshBuilder3(iface);

    TRACE("iface %p.\n", iface);

    return mesh_builder->nb_normals;
}

static const struct IDirect3DRMMeshBuilder3Vtbl d3drm_mesh_builder3_vtbl =
{
    d3drm_mesh_builder3_QueryInterface,
    d3drm_mesh_builder3_AddRef,
    d3drm_mesh_builder3_Release,
    d3drm_mesh_builder3_Clone,
    d3drm_mesh_builder3_AddDestroyCallback,
    d3drm_mesh_builder3_DeleteDestroyCallback,
    d3drm_mesh_builder3_SetAppData,
    d3drm_mesh_builder3_GetAppData,
    d3drm_mesh_builder3_SetName,
    d3drm_mesh_builder3_GetName,
    d3drm_mesh_builder3_GetClassName,
    d3drm_mesh_builder3_Load,
    d3drm_mesh_builder3_Save,
    d3drm_mesh_builder3_Scale,
    d3drm_mesh_builder3_Translate,
    d3drm_mesh_builder3_SetColorSource,
    d3drm_mesh_builder3_GetBox,
    d3drm_mesh_builder3_GenerateNormals,
    d3drm_mesh_builder3_GetColorSource,
    d3drm_mesh_builder3_AddMesh,
    d3drm_mesh_builder3_AddMeshBuilder,
    d3drm_mesh_builder3_AddFrame,
    d3drm_mesh_builder3_AddFace,
    d3drm_mesh_builder3_AddFaces,
    d3drm_mesh_builder3_ReserveSpace,
    d3drm_mesh_builder3_SetColorRGB,
    d3drm_mesh_builder3_SetColor,
    d3drm_mesh_builder3_SetTexture,
    d3drm_mesh_builder3_SetMaterial,
    d3drm_mesh_builder3_SetTextureTopology,
    d3drm_mesh_builder3_SetQuality,
    d3drm_mesh_builder3_SetPerspective,
    d3drm_mesh_builder3_SetVertex,
    d3drm_mesh_builder3_SetNormal,
    d3drm_mesh_builder3_SetTextureCoordinates,
    d3drm_mesh_builder3_SetVertexColor,
    d3drm_mesh_builder3_SetVertexColorRGB,
    d3drm_mesh_builder3_GetFaces,
    d3drm_mesh_builder3_GetGeometry,
    d3drm_mesh_builder3_GetTextureCoordinates,
    d3drm_mesh_builder3_AddVertex,
    d3drm_mesh_builder3_AddNormal,
    d3drm_mesh_builder3_CreateFace,
    d3drm_mesh_builder3_GetQuality,
    d3drm_mesh_builder3_GetPerspective,
    d3drm_mesh_builder3_GetFaceCount,
    d3drm_mesh_builder3_GetVertexCount,
    d3drm_mesh_builder3_GetVertexColor,
    d3drm_mesh_builder3_CreateMesh,
    d3drm_mesh_builder3_GetFace,
    d3drm_mesh_builder3_GetVertex,
    d3drm_mesh_builder3_GetNormal,
    d3drm_mesh_builder3_DeleteVertices,
    d3drm_mesh_builder3_DeleteNormals,
    d3drm_mesh_builder3_DeleteFace,
    d3drm_mesh_builder3_Empty,
    d3drm_mesh_builder3_Optimize,
    d3drm_mesh_builder3_AddFacesIndexed,
    d3drm_mesh_builder3_CreateSubMesh,
    d3drm_mesh_builder3_GetParentMesh,
    d3drm_mesh_builder3_GetSubMeshes,
    d3drm_mesh_builder3_DeleteSubMesh,
    d3drm_mesh_builder3_Enable,
    d3drm_mesh_builder3_GetEnable,
    d3drm_mesh_builder3_AddTriangles,
    d3drm_mesh_builder3_SetVertices,
    d3drm_mesh_builder3_GetVertices,
    d3drm_mesh_builder3_SetNormals,
    d3drm_mesh_builder3_GetNormals,
    d3drm_mesh_builder3_GetNormalCount,
2300
};
2301

2302
HRESULT Direct3DRMMeshBuilder_create(REFIID riid, IUnknown **out)
2303
{
2304
    struct d3drm_mesh_builder *object;
2305

2306
    TRACE("riid %s, out %p.\n", debugstr_guid(riid), out);
2307

2308
    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
2309 2310
        return E_OUTOFMEMORY;

2311 2312
    object->IDirect3DRMMeshBuilder2_iface.lpVtbl = &d3drm_mesh_builder2_vtbl;
    object->IDirect3DRMMeshBuilder3_iface.lpVtbl = &d3drm_mesh_builder3_vtbl;
2313 2314 2315
    object->ref = 1;

    if (IsEqualGUID(riid, &IID_IDirect3DRMMeshBuilder3))
2316
        *out = (IUnknown *)&object->IDirect3DRMMeshBuilder3_iface;
2317
    else
2318
        *out = (IUnknown *)&object->IDirect3DRMMeshBuilder2_iface;
2319 2320 2321

    return S_OK;
}
2322

2323
static HRESULT WINAPI d3drm_mesh_QueryInterface(IDirect3DRMMesh *iface, REFIID riid, void **out)
2324
{
2325
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
2326

2327 2328
    if (IsEqualGUID(riid, &IID_IDirect3DRMMesh)
            || IsEqualGUID(riid, &IID_IUnknown))
2329
    {
2330 2331 2332
        IDirect3DRMMesh_AddRef(iface);
        *out = iface;
        return S_OK;
2333 2334
    }

2335 2336 2337 2338
    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

    *out = NULL;
    return E_NOINTERFACE;
2339 2340
}

2341
static ULONG WINAPI d3drm_mesh_AddRef(IDirect3DRMMesh *iface)
2342
{
2343 2344
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
    ULONG refcount = InterlockedIncrement(&mesh->ref);
2345

2346
    TRACE("%p increasing refcount to %u.\n", iface, refcount);
2347

2348
    return refcount;
2349 2350
}

2351
static ULONG WINAPI d3drm_mesh_Release(IDirect3DRMMesh *iface)
2352
{
2353 2354
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
    ULONG refcount = InterlockedDecrement(&mesh->ref);
2355

2356
    TRACE("%p decreasing refcount to %u.\n", iface, refcount);
2357

2358
    if (!refcount)
2359
    {
2360
        DWORD i;
2361

2362
        for (i = 0; i < mesh->nb_groups; ++i)
2363
        {
2364 2365 2366 2367 2368 2369
            HeapFree(GetProcessHeap(), 0, mesh->groups[i].vertices);
            HeapFree(GetProcessHeap(), 0, mesh->groups[i].face_data);
            if (mesh->groups[i].material)
                IDirect3DRMMaterial2_Release(mesh->groups[i].material);
            if (mesh->groups[i].texture)
                IDirect3DRMTexture3_Release(mesh->groups[i].texture);
2370
        }
2371 2372
        HeapFree(GetProcessHeap(), 0, mesh->groups);
        HeapFree(GetProcessHeap(), 0, mesh);
2373
    }
2374

2375
    return refcount;
2376 2377
}

2378
static HRESULT WINAPI d3drm_mesh_Clone(IDirect3DRMMesh *iface,
Henri Verbeet's avatar
Henri Verbeet committed
2379
        IUnknown *outer, REFIID iid, void **out)
2380
{
Henri Verbeet's avatar
Henri Verbeet committed
2381
    FIXME("iface %p, outer %p, iid %s, out %p stub!\n", iface, outer, debugstr_guid(iid), out);
2382 2383 2384 2385

    return E_NOTIMPL;
}

2386
static HRESULT WINAPI d3drm_mesh_AddDestroyCallback(IDirect3DRMMesh *iface,
Henri Verbeet's avatar
Henri Verbeet committed
2387
        D3DRMOBJECTCALLBACK cb, void *ctx)
2388
{
Henri Verbeet's avatar
Henri Verbeet committed
2389
    FIXME("iface %p, cb %p, ctx %p stub!\n", iface, cb, ctx);
2390 2391 2392 2393

    return E_NOTIMPL;
}

2394
static HRESULT WINAPI d3drm_mesh_DeleteDestroyCallback(IDirect3DRMMesh *iface,
Henri Verbeet's avatar
Henri Verbeet committed
2395
        D3DRMOBJECTCALLBACK cb, void *ctx)
2396
{
Henri Verbeet's avatar
Henri Verbeet committed
2397
    FIXME("iface %p, cb %p, ctx %p stub!\n", iface, cb, ctx);
2398 2399 2400 2401

    return E_NOTIMPL;
}

2402
static HRESULT WINAPI d3drm_mesh_SetAppData(IDirect3DRMMesh *iface, DWORD data)
2403
{
2404
    FIXME("iface %p, data %#x stub!\n", iface, data);
2405 2406 2407 2408

    return E_NOTIMPL;
}

2409
static DWORD WINAPI d3drm_mesh_GetAppData(IDirect3DRMMesh *iface)
2410
{
2411
    FIXME("iface %p stub!\n", iface);
2412 2413 2414 2415

    return 0;
}

2416
static HRESULT WINAPI d3drm_mesh_SetName(IDirect3DRMMesh *iface, const char *name)
2417
{
Henri Verbeet's avatar
Henri Verbeet committed
2418
    FIXME("iface %p, name %s stub!\n", iface, debugstr_a(name));
2419 2420 2421 2422

    return E_NOTIMPL;
}

2423
static HRESULT WINAPI d3drm_mesh_GetName(IDirect3DRMMesh *iface, DWORD *size, char *name)
2424
{
Henri Verbeet's avatar
Henri Verbeet committed
2425
    FIXME("iface %p, size %p, name %p stub!\n", iface, size, name);
2426 2427 2428 2429

    return E_NOTIMPL;
}

2430
static HRESULT WINAPI d3drm_mesh_GetClassName(IDirect3DRMMesh *iface, DWORD *size, char *name)
2431
{
Henri Verbeet's avatar
Henri Verbeet committed
2432
    TRACE("iface %p, size %p, name %p.\n", iface, size, name);
2433

2434 2435 2436 2437 2438 2439 2440
    if (!size || *size < strlen("Mesh") || !name)
        return E_INVALIDARG;

    strcpy(name, "Mesh");
    *size = sizeof("Mesh");

    return D3DRM_OK;
2441 2442
}

2443 2444
static HRESULT WINAPI d3drm_mesh_Scale(IDirect3DRMMesh *iface,
        D3DVALUE sx, D3DVALUE sy, D3DVALUE sz)
2445
{
2446
    FIXME("iface %p, sx %.8e, sy %.8e, sz %.8e stub!\n", iface, sx, sy, sz);
2447 2448 2449 2450

    return E_NOTIMPL;
}

2451 2452
static HRESULT WINAPI d3drm_mesh_Translate(IDirect3DRMMesh *iface,
        D3DVALUE tx, D3DVALUE ty, D3DVALUE tz)
2453
{
2454
    FIXME("iface %p, tx %.8e, ty %.8e, tz %.8e stub!\n", iface, tx, ty, tz);
2455 2456 2457 2458

    return E_NOTIMPL;
}

2459
static HRESULT WINAPI d3drm_mesh_GetBox(IDirect3DRMMesh *iface, D3DRMBOX *box)
2460
{
2461
    FIXME("iface %p, box %p stub!\n", iface, box);
2462 2463 2464 2465

    return E_NOTIMPL;
}

2466 2467
static HRESULT WINAPI d3drm_mesh_AddGroup(IDirect3DRMMesh *iface, unsigned vertex_count,
        unsigned face_count, unsigned vertex_per_face, unsigned *face_data, D3DRMGROUPINDEX *id)
2468
{
2469
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2470
    struct mesh_group *group;
2471

2472 2473
    TRACE("iface %p, vertex_count %u, face_count %u, vertex_per_face %u, face_data %p, id %p.\n",
            iface, vertex_count, face_count, vertex_per_face, face_data, id);
2474

2475
    if (!face_data || !id)
2476 2477
        return E_POINTER;

2478
    if ((mesh->nb_groups + 1) > mesh->groups_capacity)
2479
    {
2480
        struct mesh_group *groups;
2481 2482
        ULONG new_capacity;

2483
        if (!mesh->groups_capacity)
2484 2485
        {
            new_capacity = 16;
2486
            groups = HeapAlloc(GetProcessHeap(), 0, new_capacity * sizeof(*groups));
2487 2488 2489
        }
        else
        {
2490 2491
            new_capacity = mesh->groups_capacity * 2;
            groups = HeapReAlloc(GetProcessHeap(), 0, mesh->groups, new_capacity * sizeof(*groups));
2492 2493 2494 2495 2496
        }

        if (!groups)
            return E_OUTOFMEMORY;

2497 2498
        mesh->groups_capacity = new_capacity;
        mesh->groups = groups;
2499 2500
    }

2501
    group = mesh->groups + mesh->nb_groups;
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515

    group->vertices = HeapAlloc(GetProcessHeap(), 0, vertex_count * sizeof(D3DRMVERTEX));
    if (!group->vertices)
        return E_OUTOFMEMORY;
    group->nb_vertices = vertex_count;
    group->nb_faces = face_count;
    group->vertex_per_face = vertex_per_face;

    if (vertex_per_face)
    {
        group->face_data_size = face_count * vertex_per_face;
    }
    else
    {
2516
        unsigned i;
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
        unsigned nb_indices;
        unsigned* face_data_ptr = face_data;
        group->face_data_size = 0;

        for (i = 0; i < face_count; i++)
        {
            nb_indices = *face_data_ptr;
            group->face_data_size += nb_indices + 1;
            face_data_ptr += nb_indices;
        }
    }

    group->face_data = HeapAlloc(GetProcessHeap(), 0, group->face_data_size * sizeof(unsigned));
    if (!group->face_data)
    {
        HeapFree(GetProcessHeap(), 0 , group->vertices);
        return E_OUTOFMEMORY;
    }

    memcpy(group->face_data, face_data, group->face_data_size * sizeof(unsigned));

2538
    group->material = NULL;
2539 2540
    group->texture = NULL;

2541
    *id = mesh->nb_groups++;
2542 2543

    return D3DRM_OK;
2544 2545
}

2546 2547
static HRESULT WINAPI d3drm_mesh_SetVertices(IDirect3DRMMesh *iface, D3DRMGROUPINDEX group_id,
        unsigned int start_idx, unsigned int count, D3DRMVERTEX *values)
2548
{
2549
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2550

2551 2552
    TRACE("iface %p, group_id %#x, start_idx %u, count %u, values %p.\n",
            iface, group_id, start_idx, count, values);
2553

2554
    if (group_id >= mesh->nb_groups)
2555 2556
        return D3DRMERR_BADVALUE;

2557
    if ((start_idx + count - 1) >= mesh->groups[group_id].nb_vertices)
2558 2559 2560 2561 2562
        return D3DRMERR_BADVALUE;

    if (!values)
        return E_POINTER;

2563
    memcpy(mesh->groups[group_id].vertices + start_idx, values, count * sizeof(*values));
2564 2565

    return D3DRM_OK;
2566 2567
}

2568
static HRESULT WINAPI d3drm_mesh_SetGroupColor(IDirect3DRMMesh *iface, D3DRMGROUPINDEX id, D3DCOLOR color)
2569
{
2570
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2571

2572
    TRACE("iface %p, id %#x, color 0x%08x.\n", iface, id, color);
2573

2574
    if (id >= mesh->nb_groups)
2575 2576
        return D3DRMERR_BADVALUE;

2577
    mesh->groups[id].color = color;
2578 2579

    return D3DRM_OK;
2580 2581
}

2582 2583
static HRESULT WINAPI d3drm_mesh_SetGroupColorRGB(IDirect3DRMMesh *iface,
        D3DRMGROUPINDEX id, D3DVALUE red, D3DVALUE green, D3DVALUE blue)
2584
{
2585
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2586

2587
    TRACE("iface %p, id %#x, red %.8e, green %.8e, blue %.8e.\n", iface, id, red, green, blue);
2588

2589
    if (id >= mesh->nb_groups)
2590 2591
        return D3DRMERR_BADVALUE;

2592
    mesh->groups[id].color = RGBA_MAKE((BYTE)(red * 255.0f), (BYTE)(green * 255.0f), (BYTE)(blue * 255.0f), 0xff);
2593 2594

    return D3DRM_OK;
2595 2596
}

2597
static HRESULT WINAPI d3drm_mesh_SetGroupMapping(IDirect3DRMMesh *iface, D3DRMGROUPINDEX id, D3DRMMAPPING value)
2598
{
2599
    FIXME("iface %p, id %#x, value %#x stub!\n", iface, id, value);
2600 2601 2602 2603

    return E_NOTIMPL;
}

2604
static HRESULT WINAPI d3drm_mesh_SetGroupQuality(IDirect3DRMMesh *iface, D3DRMGROUPINDEX id, D3DRMRENDERQUALITY value)
2605
{
2606
    FIXME("iface %p, id %#x, value %#x stub!\n", iface, id, value);
2607 2608 2609 2610

    return E_NOTIMPL;
}

2611
static HRESULT WINAPI d3drm_mesh_SetGroupMaterial(IDirect3DRMMesh *iface,
2612
        D3DRMGROUPINDEX id, IDirect3DRMMaterial *material)
2613
{
2614
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2615

2616
    TRACE("iface %p, id %#x, material %p.\n", iface, id, material);
2617

2618
    if (id >= mesh->nb_groups)
2619 2620
        return D3DRMERR_BADVALUE;

2621 2622
    if (mesh->groups[id].material)
        IDirect3DRMMaterial2_Release(mesh->groups[id].material);
2623

2624
    mesh->groups[id].material = (IDirect3DRMMaterial2 *)material;
2625 2626

    if (material)
2627
        IDirect3DRMMaterial2_AddRef(mesh->groups[id].material);
2628 2629

    return D3DRM_OK;
2630 2631
}

2632
static HRESULT WINAPI d3drm_mesh_SetGroupTexture(IDirect3DRMMesh *iface,
2633
        D3DRMGROUPINDEX id, IDirect3DRMTexture *texture)
2634
{
2635
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2636

2637
    TRACE("iface %p, id %#x, texture %p.\n", iface, id, texture);
2638

2639
    if (id >= mesh->nb_groups)
2640 2641
        return D3DRMERR_BADVALUE;

2642 2643
    if (mesh->groups[id].texture)
        IDirect3DRMTexture3_Release(mesh->groups[id].texture);
2644 2645 2646

    if (!texture)
    {
2647
        mesh->groups[id].texture = NULL;
2648 2649 2650
        return D3DRM_OK;
    }

2651
    return IDirect3DRMTexture3_QueryInterface(texture, &IID_IDirect3DRMTexture, (void **)&mesh->groups[id].texture);
2652 2653
}

2654
static DWORD WINAPI d3drm_mesh_GetGroupCount(IDirect3DRMMesh *iface)
2655
{
2656
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2657

2658
    TRACE("iface %p.\n", iface);
2659

2660
    return mesh->nb_groups;
2661 2662
}

2663 2664
static HRESULT WINAPI d3drm_mesh_GetGroup(IDirect3DRMMesh *iface, D3DRMGROUPINDEX id, unsigned *vertex_count,
        unsigned *face_count, unsigned *vertex_per_face, DWORD *face_data_size, unsigned *face_data)
2665
{
2666
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2667

2668 2669
    TRACE("iface %p, id %#x, vertex_count %p, face_count %p, vertex_per_face %p, face_data_size %p, face_data %p.\n",
            iface, id, vertex_count, face_count, vertex_per_face, face_data_size,face_data);
2670

2671
    if (id >= mesh->nb_groups)
2672 2673 2674
        return D3DRMERR_BADVALUE;

    if (vertex_count)
2675
        *vertex_count = mesh->groups[id].nb_vertices;
2676
    if (face_count)
2677
        *face_count = mesh->groups[id].nb_faces;
2678
    if (vertex_per_face)
2679
        *vertex_per_face = mesh->groups[id].vertex_per_face;
2680
    if (face_data_size)
2681
        *face_data_size = mesh->groups[id].face_data_size;
2682
    if (face_data)
2683
        memcpy(face_data, mesh->groups[id].face_data, mesh->groups[id].face_data_size * sizeof(*face_data));
2684 2685

    return D3DRM_OK;
2686 2687
}

2688 2689
static HRESULT WINAPI d3drm_mesh_GetVertices(IDirect3DRMMesh *iface,
        D3DRMGROUPINDEX group_id, DWORD start_idx, DWORD count, D3DRMVERTEX *vertices)
2690
{
2691
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2692

2693 2694
    TRACE("iface %p, group_id %#x, start_idx %u, count %u, vertices %p.\n",
            iface, group_id, start_idx, count, vertices);
2695

2696
    if (group_id >= mesh->nb_groups)
2697 2698
        return D3DRMERR_BADVALUE;

2699
    if ((start_idx + count - 1) >= mesh->groups[group_id].nb_vertices)
2700 2701
        return D3DRMERR_BADVALUE;

2702
    if (!vertices)
2703 2704
        return E_POINTER;

2705
    memcpy(vertices, mesh->groups[group_id].vertices + start_idx, count * sizeof(*vertices));
2706 2707

    return D3DRM_OK;
2708 2709
}

2710
static D3DCOLOR WINAPI d3drm_mesh_GetGroupColor(IDirect3DRMMesh *iface, D3DRMGROUPINDEX id)
2711
{
2712
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2713

2714
    TRACE("iface %p, id %#x.\n", iface, id);
2715

2716
    return mesh->groups[id].color;
2717 2718
}

2719
static D3DRMMAPPING WINAPI d3drm_mesh_GetGroupMapping(IDirect3DRMMesh *iface, D3DRMGROUPINDEX id)
2720
{
2721
    FIXME("iface %p, id %#x stub!\n", iface, id);
2722 2723 2724

    return 0;
}
2725
static D3DRMRENDERQUALITY WINAPI d3drm_mesh_GetGroupQuality(IDirect3DRMMesh *iface, D3DRMGROUPINDEX id)
2726
{
2727
    FIXME("iface %p, id %#x stub!\n", iface, id);
2728 2729 2730 2731

    return 0;
}

2732
static HRESULT WINAPI d3drm_mesh_GetGroupMaterial(IDirect3DRMMesh *iface,
2733
        D3DRMGROUPINDEX id, IDirect3DRMMaterial **material)
2734
{
2735
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2736

2737
    TRACE("iface %p, id %#x, material %p.\n", iface, id, material);
2738

2739
    if (id >= mesh->nb_groups)
2740 2741 2742 2743 2744
        return D3DRMERR_BADVALUE;

    if (!material)
        return E_POINTER;

2745 2746
    if (mesh->groups[id].material)
        IDirect3DRMTexture_QueryInterface(mesh->groups[id].material, &IID_IDirect3DRMMaterial, (void **)material);
2747 2748 2749 2750
    else
        *material = NULL;

    return D3DRM_OK;
2751 2752
}

2753
static HRESULT WINAPI d3drm_mesh_GetGroupTexture(IDirect3DRMMesh *iface,
2754
        D3DRMGROUPINDEX id, IDirect3DRMTexture **texture)
2755
{
2756
    struct d3drm_mesh *mesh = impl_from_IDirect3DRMMesh(iface);
2757

2758
    TRACE("iface %p, id %#x, texture %p.\n", iface, id, texture);
2759

2760
    if (id >= mesh->nb_groups)
2761 2762 2763 2764 2765
        return D3DRMERR_BADVALUE;

    if (!texture)
        return E_POINTER;

2766 2767
    if (mesh->groups[id].texture)
        IDirect3DRMTexture_QueryInterface(mesh->groups[id].texture, &IID_IDirect3DRMTexture, (void **)texture);
2768 2769 2770 2771
    else
        *texture = NULL;

    return D3DRM_OK;
2772 2773
}

2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
static const struct IDirect3DRMMeshVtbl d3drm_mesh_vtbl =
{
    d3drm_mesh_QueryInterface,
    d3drm_mesh_AddRef,
    d3drm_mesh_Release,
    d3drm_mesh_Clone,
    d3drm_mesh_AddDestroyCallback,
    d3drm_mesh_DeleteDestroyCallback,
    d3drm_mesh_SetAppData,
    d3drm_mesh_GetAppData,
    d3drm_mesh_SetName,
    d3drm_mesh_GetName,
    d3drm_mesh_GetClassName,
    d3drm_mesh_Scale,
    d3drm_mesh_Translate,
    d3drm_mesh_GetBox,
    d3drm_mesh_AddGroup,
    d3drm_mesh_SetVertices,
    d3drm_mesh_SetGroupColor,
    d3drm_mesh_SetGroupColorRGB,
    d3drm_mesh_SetGroupMapping,
    d3drm_mesh_SetGroupQuality,
    d3drm_mesh_SetGroupMaterial,
    d3drm_mesh_SetGroupTexture,
    d3drm_mesh_GetGroupCount,
    d3drm_mesh_GetGroup,
    d3drm_mesh_GetVertices,
    d3drm_mesh_GetGroupColor,
    d3drm_mesh_GetGroupMapping,
    d3drm_mesh_GetGroupQuality,
    d3drm_mesh_GetGroupMaterial,
    d3drm_mesh_GetGroupTexture,
2806 2807
};

2808
HRESULT Direct3DRMMesh_create(IDirect3DRMMesh **mesh)
2809
{
2810
    struct d3drm_mesh *object;
2811

2812
    TRACE("mesh %p.\n", mesh);
2813

2814
    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
2815 2816
        return E_OUTOFMEMORY;

2817
    object->IDirect3DRMMesh_iface.lpVtbl = &d3drm_mesh_vtbl;
2818 2819
    object->ref = 1;

2820
    *mesh = &object->IDirect3DRMMesh_iface;
2821 2822 2823

    return S_OK;
}