2 * Copyright (c) 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This file contains the implementation of Direct3DMaterial.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
25 #include "ddraw_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
29 static void dump_material(const D3DMATERIAL *mat)
31 TRACE(" dwSize : %d\n", mat->dwSize);
34 static inline IDirect3DMaterialImpl *impl_from_IDirect3DMaterial(IDirect3DMaterial *iface)
36 return CONTAINING_RECORD(iface, IDirect3DMaterialImpl, IDirect3DMaterial_iface);
39 static inline IDirect3DMaterialImpl *impl_from_IDirect3DMaterial2(IDirect3DMaterial2 *iface)
41 return CONTAINING_RECORD(iface, IDirect3DMaterialImpl, IDirect3DMaterial2_iface);
44 static inline IDirect3DMaterialImpl *impl_from_IDirect3DMaterial3(IDirect3DMaterial3 *iface)
46 return CONTAINING_RECORD(iface, IDirect3DMaterialImpl, IDirect3DMaterial3_iface);
49 /*****************************************************************************
51 *****************************************************************************/
53 /*****************************************************************************
54 * IDirect3DMaterial3::QueryInterface
56 * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
60 * riid: Interface id queried for
61 * obj: Address to pass the interface pointer back
65 * E_NOINTERFACE if the requested interface wasn't found
67 *****************************************************************************/
68 static HRESULT WINAPI IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface, REFIID riid,
71 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial3(iface);
73 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obp);
77 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
78 IUnknown_AddRef(iface);
80 TRACE(" Creating IUnknown interface at %p.\n", *obp);
83 if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
84 IDirect3DMaterial_AddRef(&This->IDirect3DMaterial_iface);
85 *obp = &This->IDirect3DMaterial_iface;
86 TRACE(" Creating IDirect3DMaterial interface %p\n", *obp);
89 if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
90 IDirect3DMaterial_AddRef(&This->IDirect3DMaterial2_iface);
91 *obp = &This->IDirect3DMaterial2_iface;
92 TRACE(" Creating IDirect3DMaterial2 interface %p\n", *obp);
95 if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
96 IDirect3DMaterial3_AddRef(&This->IDirect3DMaterial3_iface);
98 TRACE(" Creating IDirect3DMaterial3 interface %p\n", *obp);
101 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
102 return E_NOINTERFACE;
105 /*****************************************************************************
106 * IDirect3DMaterial3::AddRef
108 * Increases the refcount.
113 *****************************************************************************/
114 static ULONG WINAPI IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
116 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial3(iface);
117 ULONG ref = InterlockedIncrement(&This->ref);
119 TRACE("%p increasing refcount to %u.\n", This, ref);
124 /*****************************************************************************
125 * IDirect3DMaterial3::Release
127 * Reduces the refcount by one. If the refcount falls to 0, the object
133 *****************************************************************************/
134 static ULONG WINAPI IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
136 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial3(iface);
137 ULONG ref = InterlockedDecrement(&This->ref);
139 TRACE("%p decreasing refcount to %u.\n", This, ref);
145 EnterCriticalSection(&ddraw_cs);
146 ddraw_free_handle(&This->ddraw->d3ddevice->handle_table, This->Handle - 1, DDRAW_HANDLE_MATERIAL);
147 LeaveCriticalSection(&ddraw_cs);
150 HeapFree(GetProcessHeap(), 0, This);
156 /*****************************************************************************
157 * IDirect3DMaterial Methods
158 *****************************************************************************/
160 /*****************************************************************************
161 * IDirect3DMaterial::Initialize
163 * A no-op initialization
166 * Direct3D: Pointer to a Direct3D interface
171 *****************************************************************************/
172 static HRESULT WINAPI
173 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
176 TRACE("iface %p, d3d %p.\n", iface, Direct3D);
181 /*****************************************************************************
182 * IDirect3DMaterial::Reserve
184 * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
185 * Odd. They seem to have mixed their interfaces.
190 *****************************************************************************/
191 static HRESULT WINAPI
192 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
194 TRACE("iface %p.\n", iface);
196 return DDERR_UNSUPPORTED;
199 /*****************************************************************************
200 * IDirect3DMaterial::Unreserve
207 *****************************************************************************/
208 static HRESULT WINAPI
209 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
211 TRACE("iface %p.\n", iface);
213 return DDERR_UNSUPPORTED;
216 /*****************************************************************************
217 * IDirect3DMaterial3::SetMaterial
219 * Sets the material description
222 * Mat: Material to set
226 * DDERR_INVALIDPARAMS if Mat is NULL
228 *****************************************************************************/
229 static HRESULT WINAPI IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
232 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial3(iface);
234 TRACE("iface %p, material %p.\n", iface, lpMat);
236 dump_material(lpMat);
238 /* Stores the material */
239 EnterCriticalSection(&ddraw_cs);
240 memset(&This->mat, 0, sizeof(This->mat));
241 memcpy(&This->mat, lpMat, lpMat->dwSize);
242 LeaveCriticalSection(&ddraw_cs);
247 /*****************************************************************************
248 * IDirect3DMaterial3::GetMaterial
250 * Returns the material assigned to this interface
253 * Mat: Pointer to a D3DMATERIAL structure to store the material description
257 * DDERR_INVALIDPARAMS if Mat is NULL
259 *****************************************************************************/
260 static HRESULT WINAPI IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
263 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial3(iface);
266 TRACE("iface %p, material %p.\n", iface, lpMat);
269 TRACE(" Returning material : ");
270 dump_material(&This->mat);
273 /* Copies the material structure */
274 EnterCriticalSection(&ddraw_cs);
275 dwSize = lpMat->dwSize;
276 memcpy(lpMat, &This->mat, dwSize);
277 LeaveCriticalSection(&ddraw_cs);
282 /*****************************************************************************
283 * IDirect3DMaterial3::GetHandle
285 * Returns a handle for the material interface. The handle is simply a
286 * pointer to the material implementation
289 * Direct3DDevice3: The device this handle is assigned to
290 * Handle: Address to write the handle to
294 * DDERR_INVALIDPARAMS if Handle is NULL
296 *****************************************************************************/
297 static HRESULT WINAPI IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
298 IDirect3DDevice3 *lpDirect3DDevice3, D3DMATERIALHANDLE *lpHandle)
300 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial3(iface);
301 IDirect3DDeviceImpl *device = device_from_device3(lpDirect3DDevice3);
303 TRACE("iface %p, device %p, handle %p.\n", iface, lpDirect3DDevice3, lpHandle);
305 EnterCriticalSection(&ddraw_cs);
306 This->active_device = device;
309 DWORD h = ddraw_allocate_handle(&device->handle_table, This, DDRAW_HANDLE_MATERIAL);
310 if (h == DDRAW_INVALID_HANDLE)
312 ERR("Failed to allocate a material handle.\n");
313 LeaveCriticalSection(&ddraw_cs);
314 return DDERR_INVALIDPARAMS; /* Unchecked */
317 This->Handle = h + 1;
319 *lpHandle = This->Handle;
320 TRACE(" returning handle %08x.\n", *lpHandle);
321 LeaveCriticalSection(&ddraw_cs);
326 static HRESULT WINAPI IDirect3DMaterialImpl_2_GetHandle(IDirect3DMaterial2 *iface,
327 IDirect3DDevice2 *lpDirect3DDevice2, D3DMATERIALHANDLE *lpHandle)
329 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial2(iface);
331 TRACE("iface %p, device %p, handle %p.\n", iface, lpDirect3DDevice2, lpHandle);
333 return IDirect3DMaterial3_GetHandle(&This->IDirect3DMaterial3_iface, lpDirect3DDevice2 ?
334 (IDirect3DDevice3 *)&device_from_device2(lpDirect3DDevice2)->IDirect3DDevice3_vtbl : NULL, lpHandle);
337 static HRESULT WINAPI IDirect3DMaterialImpl_1_GetHandle(IDirect3DMaterial *iface,
338 IDirect3DDevice *lpDirect3DDevice, D3DMATERIALHANDLE *lpHandle)
340 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial(iface);
342 TRACE("iface %p, device %p, handle %p.\n", iface, lpDirect3DDevice, lpHandle);
344 return IDirect3DMaterial3_GetHandle(&This->IDirect3DMaterial3_iface, lpDirect3DDevice ?
345 (IDirect3DDevice3 *)&device_from_device1(lpDirect3DDevice)->IDirect3DDevice3_vtbl : NULL, lpHandle);
348 static HRESULT WINAPI IDirect3DMaterialImpl_2_QueryInterface(IDirect3DMaterial2 *iface, REFIID riid,
351 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial2(iface);
353 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obp);
355 return IDirect3DMaterial3_QueryInterface(&This->IDirect3DMaterial3_iface, riid, obp);
358 static HRESULT WINAPI IDirect3DMaterialImpl_1_QueryInterface(IDirect3DMaterial *iface, REFIID riid,
361 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial(iface);
363 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obp);
365 return IDirect3DMaterial3_QueryInterface(&This->IDirect3DMaterial3_iface, riid, obp);
368 static ULONG WINAPI IDirect3DMaterialImpl_2_AddRef(IDirect3DMaterial2 *iface)
370 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial2(iface);
372 TRACE("iface %p.\n", iface);
374 return IDirect3DMaterial3_AddRef(&This->IDirect3DMaterial3_iface);
377 static ULONG WINAPI IDirect3DMaterialImpl_1_AddRef(IDirect3DMaterial *iface)
379 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial(iface);
381 TRACE("iface %p.\n", iface);
383 return IDirect3DMaterial3_AddRef(&This->IDirect3DMaterial3_iface);
386 static ULONG WINAPI IDirect3DMaterialImpl_2_Release(IDirect3DMaterial2 *iface)
388 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial2(iface);
390 TRACE("iface %p.\n", iface);
392 return IDirect3DMaterial3_Release(&This->IDirect3DMaterial3_iface);
395 static ULONG WINAPI IDirect3DMaterialImpl_1_Release(IDirect3DMaterial *iface)
397 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial(iface);
399 TRACE("iface %p.\n", iface);
401 return IDirect3DMaterial3_Release(&This->IDirect3DMaterial3_iface);
404 static HRESULT WINAPI IDirect3DMaterialImpl_2_SetMaterial(IDirect3DMaterial2 *iface,
407 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial2(iface);
409 TRACE("iface %p, material %p.\n", iface, lpMat);
411 return IDirect3DMaterial3_SetMaterial(&This->IDirect3DMaterial3_iface, lpMat);
414 static HRESULT WINAPI IDirect3DMaterialImpl_1_SetMaterial(IDirect3DMaterial *iface,
417 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial(iface);
419 TRACE("iface %p, material %p.\n", iface, lpMat);
421 return IDirect3DMaterial3_SetMaterial(&This->IDirect3DMaterial3_iface, lpMat);
424 static HRESULT WINAPI IDirect3DMaterialImpl_2_GetMaterial(IDirect3DMaterial2 *iface,
427 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial2(iface);
429 TRACE("iface %p, material %p.\n", iface, lpMat);
431 return IDirect3DMaterial3_GetMaterial(&This->IDirect3DMaterial3_iface, lpMat);
434 static HRESULT WINAPI IDirect3DMaterialImpl_1_GetMaterial(IDirect3DMaterial *iface,
437 IDirect3DMaterialImpl *This = impl_from_IDirect3DMaterial(iface);
439 TRACE("iface %p, material %p.\n", iface, lpMat);
441 return IDirect3DMaterial3_GetMaterial(&This->IDirect3DMaterial3_iface, lpMat);
445 /*****************************************************************************
448 * Uses IDirect3DDevice7::SetMaterial to activate the material
451 * This: Pointer to the material implementation to activate
453 *****************************************************************************/
454 void material_activate(IDirect3DMaterialImpl* This)
456 D3DMATERIAL7 d3d7mat;
458 TRACE("Activating material %p\n", This);
459 d3d7mat.u.diffuse = This->mat.u.diffuse;
460 d3d7mat.u1.ambient = This->mat.u1.ambient;
461 d3d7mat.u2.specular = This->mat.u2.specular;
462 d3d7mat.u3.emissive = This->mat.u3.emissive;
463 d3d7mat.u4.power = This->mat.u4.power;
465 IDirect3DDevice7_SetMaterial((IDirect3DDevice7 *)This->active_device, &d3d7mat);
468 static const struct IDirect3DMaterial3Vtbl d3d_material3_vtbl =
470 /*** IUnknown Methods ***/
471 IDirect3DMaterialImpl_QueryInterface,
472 IDirect3DMaterialImpl_AddRef,
473 IDirect3DMaterialImpl_Release,
474 /*** IDirect3DMaterial3 Methods ***/
475 IDirect3DMaterialImpl_SetMaterial,
476 IDirect3DMaterialImpl_GetMaterial,
477 IDirect3DMaterialImpl_GetHandle,
480 static const struct IDirect3DMaterial2Vtbl d3d_material2_vtbl =
482 /*** IUnknown Methods ***/
483 IDirect3DMaterialImpl_2_QueryInterface,
484 IDirect3DMaterialImpl_2_AddRef,
485 IDirect3DMaterialImpl_2_Release,
486 /*** IDirect3DMaterial2 Methods ***/
487 IDirect3DMaterialImpl_2_SetMaterial,
488 IDirect3DMaterialImpl_2_GetMaterial,
489 IDirect3DMaterialImpl_2_GetHandle,
492 static const struct IDirect3DMaterialVtbl d3d_material1_vtbl =
494 /*** IUnknown Methods ***/
495 IDirect3DMaterialImpl_1_QueryInterface,
496 IDirect3DMaterialImpl_1_AddRef,
497 IDirect3DMaterialImpl_1_Release,
498 /*** IDirect3DMaterial1 Methods ***/
499 IDirect3DMaterialImpl_Initialize,
500 IDirect3DMaterialImpl_1_SetMaterial,
501 IDirect3DMaterialImpl_1_GetMaterial,
502 IDirect3DMaterialImpl_1_GetHandle,
503 IDirect3DMaterialImpl_Reserve,
504 IDirect3DMaterialImpl_Unreserve
507 IDirect3DMaterialImpl *d3d_material_create(IDirectDrawImpl *ddraw)
509 IDirect3DMaterialImpl *material;
511 material = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*material));
515 material->IDirect3DMaterial3_iface.lpVtbl = &d3d_material3_vtbl;
516 material->IDirect3DMaterial2_iface.lpVtbl = &d3d_material2_vtbl;
517 material->IDirect3DMaterial_iface.lpVtbl = &d3d_material1_vtbl;
519 material->ddraw = ddraw;