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"
31 #define NONAMELESSUNION
37 #include "wine/exception.h"
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
48 static void dump_material(const D3DMATERIAL *mat)
50 TRACE(" dwSize : %d\n", mat->dwSize);
53 /*****************************************************************************
55 *****************************************************************************/
57 /*****************************************************************************
58 * IDirect3DMaterial3::QueryInterface
60 * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
64 * riid: Interface id queried for
65 * obj: Address to pass the interface pointer back
69 * E_NOINTERFACE if the requested interface wasn't found
71 *****************************************************************************/
73 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
77 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
78 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
82 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
83 IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
85 TRACE(" Creating IUnknown interface at %p.\n", *obp);
88 if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
89 IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
90 *obp = ICOM_INTERFACE(This, IDirect3DMaterial);
91 TRACE(" Creating IDirect3DMaterial interface %p\n", *obp);
94 if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
95 IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
96 *obp = ICOM_INTERFACE(This, IDirect3DMaterial2);
97 TRACE(" Creating IDirect3DMaterial2 interface %p\n", *obp);
100 if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
101 IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
102 *obp = ICOM_INTERFACE(This, IDirect3DMaterial3);
103 TRACE(" Creating IDirect3DMaterial3 interface %p\n", *obp);
106 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
107 return E_NOINTERFACE;
110 /*****************************************************************************
111 * IDirect3DMaterial3::AddRef
113 * Increases the refcount.
118 *****************************************************************************/
120 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
122 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
123 ULONG ref = InterlockedIncrement(&This->ref);
125 TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
130 /*****************************************************************************
131 * IDirect3DMaterial3::Release
133 * Reduces the refcount by one. If the refcount falls to 0, the object
139 *****************************************************************************/
141 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
143 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
144 ULONG ref = InterlockedDecrement(&This->ref);
146 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
152 EnterCriticalSection(&ddraw_cs);
153 This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
154 This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
155 LeaveCriticalSection(&ddraw_cs);
158 HeapFree(GetProcessHeap(), 0, This);
164 /*****************************************************************************
165 * IDirect3DMaterial Methods
166 *****************************************************************************/
168 /*****************************************************************************
169 * IDirect3DMaterial::Initialize
171 * A no-op initialization
174 * Direct3D: Pointer to a Direct3D interface
179 *****************************************************************************/
180 static HRESULT WINAPI
181 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
184 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
186 TRACE("(%p)->(%p) no-op...!\n", This, Direct3D);
191 /*****************************************************************************
192 * IDirect3DMaterial::Reserve
194 * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
195 * Odd. They seem to have mixed their interfaces.
200 *****************************************************************************/
201 static HRESULT WINAPI
202 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
204 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
205 TRACE("(%p)->() not implemented\n", This);
207 return DDERR_UNSUPPORTED;
210 /*****************************************************************************
211 * IDirect3DMaterial::Unreserve
218 *****************************************************************************/
219 static HRESULT WINAPI
220 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
222 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
223 TRACE("(%p)->() not implemented.\n", This);
225 return DDERR_UNSUPPORTED;
228 /*****************************************************************************
229 * IDirect3DMaterial3::SetMaterial
231 * Sets the material description
234 * Mat: Material to set
238 * DDERR_INVALIDPARAMS if Mat is NULL
240 *****************************************************************************/
241 static HRESULT WINAPI
242 IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
245 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
246 TRACE("(%p)->(%p)\n", This, lpMat);
248 dump_material(lpMat);
250 /* Stores the material */
251 EnterCriticalSection(&ddraw_cs);
252 memset(&This->mat, 0, sizeof(This->mat));
253 memcpy(&This->mat, lpMat, lpMat->dwSize);
254 LeaveCriticalSection(&ddraw_cs);
259 /*****************************************************************************
260 * IDirect3DMaterial3::GetMaterial
262 * Returns the material assigned to this interface
265 * Mat: Pointer to a D3DMATERIAL structure to store the material description
269 * DDERR_INVALIDPARAMS if Mat is NULL
271 *****************************************************************************/
272 static HRESULT WINAPI
273 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
276 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
278 TRACE("(%p)->(%p)\n", This, lpMat);
279 if (TRACE_ON(d3d7)) {
280 TRACE(" Returning material : ");
281 dump_material(&This->mat);
284 /* Copies the material structure */
285 EnterCriticalSection(&ddraw_cs);
286 dwSize = lpMat->dwSize;
287 memset(lpMat, 0, dwSize);
288 memcpy(lpMat, &This->mat, dwSize);
289 LeaveCriticalSection(&ddraw_cs);
294 /*****************************************************************************
295 * IDirect3DMaterial3::GetHandle
297 * Returns a handle for the material interface. The handle is simply a
298 * pointer to the material implementation
301 * Direct3DDevice3: The device this handle is assigned to
302 * Handle: Address to write the handle to
306 * DDERR_INVALIDPARAMS if Handle is NULL
308 *****************************************************************************/
309 static HRESULT WINAPI
310 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
311 IDirect3DDevice3 *lpDirect3DDevice3,
312 D3DMATERIALHANDLE *lpHandle)
314 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
315 IDirect3DDeviceImpl *device = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, lpDirect3DDevice3);
316 TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
318 EnterCriticalSection(&ddraw_cs);
319 This->active_device = device;
322 This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
325 ERR("Error creating a handle\n");
326 LeaveCriticalSection(&ddraw_cs);
327 return DDERR_INVALIDPARAMS; /* Unchecked */
329 device->Handles[This->Handle - 1].ptr = This;
330 device->Handles[This->Handle - 1].type = DDrawHandle_Material;
332 *lpHandle = This->Handle;
333 TRACE(" returning handle %08x.\n", *lpHandle);
334 LeaveCriticalSection(&ddraw_cs);
339 static HRESULT WINAPI
340 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
341 LPDIRECT3DDEVICE2 lpDirect3DDevice2,
342 LPD3DMATERIALHANDLE lpHandle)
344 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
345 return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
346 COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice2, IDirect3DDevice3, lpDirect3DDevice2),
350 static HRESULT WINAPI
351 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
352 LPDIRECT3DDEVICE lpDirect3DDevice,
353 LPD3DMATERIALHANDLE lpHandle)
355 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
356 return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
357 COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice3, lpDirect3DDevice),
361 static HRESULT WINAPI
362 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
366 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
367 return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
372 static HRESULT WINAPI
373 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
377 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
378 return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
384 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
386 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
387 return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
391 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
393 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
394 return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
398 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
400 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
401 return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
405 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
407 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
408 return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
411 static HRESULT WINAPI
412 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
415 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
416 return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
420 static HRESULT WINAPI
421 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
424 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
425 return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
429 static HRESULT WINAPI
430 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
433 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
434 return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
438 static HRESULT WINAPI
439 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
442 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
443 return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
448 /*****************************************************************************
451 * Uses IDirect3DDevice7::SetMaterial to activate the material
454 * This: Pointer to the material implementation to activate
456 *****************************************************************************/
457 void material_activate(IDirect3DMaterialImpl* This)
459 D3DMATERIAL7 d3d7mat;
461 TRACE("Activating material %p\n", This);
462 d3d7mat.u.diffuse = This->mat.u.diffuse;
463 d3d7mat.u1.ambient = This->mat.u1.ambient;
464 d3d7mat.u2.specular = This->mat.u2.specular;
465 d3d7mat.u3.emissive = This->mat.u3.emissive;
466 d3d7mat.u4.power = This->mat.u4.power;
468 IDirect3DDevice7_SetMaterial(ICOM_INTERFACE(This->active_device, IDirect3DDevice7),
472 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
474 /*** IUnknown Methods ***/
475 IDirect3DMaterialImpl_QueryInterface,
476 IDirect3DMaterialImpl_AddRef,
477 IDirect3DMaterialImpl_Release,
478 /*** IDirect3DMaterial3 Methods ***/
479 IDirect3DMaterialImpl_SetMaterial,
480 IDirect3DMaterialImpl_GetMaterial,
481 IDirect3DMaterialImpl_GetHandle,
484 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
486 /*** IUnknown Methods ***/
487 Thunk_IDirect3DMaterialImpl_2_QueryInterface,
488 Thunk_IDirect3DMaterialImpl_2_AddRef,
489 Thunk_IDirect3DMaterialImpl_2_Release,
490 /*** IDirect3DMaterial2 Methods ***/
491 Thunk_IDirect3DMaterialImpl_2_SetMaterial,
492 Thunk_IDirect3DMaterialImpl_2_GetMaterial,
493 Thunk_IDirect3DMaterialImpl_2_GetHandle,
496 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
498 /*** IUnknown Methods ***/
499 Thunk_IDirect3DMaterialImpl_1_QueryInterface,
500 Thunk_IDirect3DMaterialImpl_1_AddRef,
501 Thunk_IDirect3DMaterialImpl_1_Release,
502 /*** IDirect3DMaterial1 Methods ***/
503 IDirect3DMaterialImpl_Initialize,
504 Thunk_IDirect3DMaterialImpl_1_SetMaterial,
505 Thunk_IDirect3DMaterialImpl_1_GetMaterial,
506 Thunk_IDirect3DMaterialImpl_1_GetHandle,
507 IDirect3DMaterialImpl_Reserve,
508 IDirect3DMaterialImpl_Unreserve