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
38 #include "wine/exception.h"
44 #include "ddraw_private.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
48 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
50 static void dump_material(LPD3DMATERIAL mat)
52 DPRINTF(" dwSize : %d\n", mat->dwSize);
55 /*****************************************************************************
57 *****************************************************************************/
59 /*****************************************************************************
60 * IDirect3DMaterial3::QueryInterface
62 * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
66 * riid: Interface id queried for
67 * obj: Address to pass the interface pointer back
71 * E_NOINTERFACE if the requested interface wasn't found
73 *****************************************************************************/
75 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
79 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
80 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
84 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
85 IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
87 TRACE(" Creating IUnknown interface at %p.\n", *obp);
90 if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
91 IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
92 *obp = ICOM_INTERFACE(This, IDirect3DMaterial);
93 TRACE(" Creating IDirect3DMaterial interface %p\n", *obp);
96 if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
97 IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
98 *obp = ICOM_INTERFACE(This, IDirect3DMaterial2);
99 TRACE(" Creating IDirect3DMaterial2 interface %p\n", *obp);
102 if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
103 IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
104 *obp = ICOM_INTERFACE(This, IDirect3DMaterial3);
105 TRACE(" Creating IDirect3DMaterial3 interface %p\n", *obp);
108 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
109 return E_NOINTERFACE;
112 /*****************************************************************************
113 * IDirect3DMaterial3::AddRef
115 * Increases the refcount.
120 *****************************************************************************/
122 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
124 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
125 ULONG ref = InterlockedIncrement(&This->ref);
127 TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
132 /*****************************************************************************
133 * IDirect3DMaterial3::Release
135 * Reduces the refcount by one. If the refcount falls to 0, the object
141 *****************************************************************************/
143 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
145 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
146 ULONG ref = InterlockedDecrement(&This->ref);
148 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
154 This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
155 This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
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 memset(&This->mat, 0, sizeof(This->mat));
252 memcpy(&This->mat, lpMat, lpMat->dwSize);
257 /*****************************************************************************
258 * IDirect3DMaterial3::GetMaterial
260 * Returns the material assigned to this interface
263 * Mat: Pointer to a D3DMATERIAL structure to store the material description
267 * DDERR_INVALIDPARAMS if Mat is NULL
269 *****************************************************************************/
270 static HRESULT WINAPI
271 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
274 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
276 TRACE("(%p)->(%p)\n", This, lpMat);
277 if (TRACE_ON(d3d7)) {
278 TRACE(" Returning material : ");
279 dump_material(&This->mat);
282 /* Copies the material structure */
283 dwSize = lpMat->dwSize;
284 memset(lpMat, 0, dwSize);
285 memcpy(lpMat, &This->mat, dwSize);
290 /*****************************************************************************
291 * IDirect3DMaterial3::GetHandle
293 * Returns a handle for the material interface. The handle is simply a
294 * pointer to the material implementation
297 * Direct3DDevice3: The device this handle is assigned to
298 * Handle: Address to write the handle to
302 * DDERR_INVALIDPARAMS if Handle is NULL
304 *****************************************************************************/
305 static HRESULT WINAPI
306 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
307 IDirect3DDevice3 *lpDirect3DDevice3,
308 D3DMATERIALHANDLE *lpHandle)
310 ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
311 IDirect3DDeviceImpl *device = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, lpDirect3DDevice3);
312 TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
314 This->active_device = device;
317 This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
320 ERR("Error creating a handle\n");
321 return DDERR_INVALIDPARAMS; /* Unchecked */
323 device->Handles[This->Handle - 1].ptr = This;
324 device->Handles[This->Handle - 1].type = DDrawHandle_Material;
326 *lpHandle = This->Handle;
327 TRACE(" returning handle %08x.\n", *lpHandle);
332 static HRESULT WINAPI
333 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
334 LPDIRECT3DDEVICE2 lpDirect3DDevice2,
335 LPD3DMATERIALHANDLE lpHandle)
337 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
338 return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
339 COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice2, IDirect3DDevice3, lpDirect3DDevice2),
343 static HRESULT WINAPI
344 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
345 LPDIRECT3DDEVICE lpDirect3DDevice,
346 LPD3DMATERIALHANDLE lpHandle)
348 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
349 return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
350 COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice3, lpDirect3DDevice),
354 static HRESULT WINAPI
355 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
359 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
360 return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
365 static HRESULT WINAPI
366 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
370 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
371 return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
377 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
379 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
380 return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
384 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
386 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
387 return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
391 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
393 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
394 return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
398 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
400 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
401 return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
404 static HRESULT WINAPI
405 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
408 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
409 return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
413 static HRESULT WINAPI
414 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
417 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
418 return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
422 static HRESULT WINAPI
423 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
426 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
427 return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
431 static HRESULT WINAPI
432 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
435 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
436 return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
441 /*****************************************************************************
444 * Uses IDirect3DDevice7::SetMaterial to activate the material
447 * This: Pointer to the material implementation to activate
449 *****************************************************************************/
450 void material_activate(IDirect3DMaterialImpl* This)
452 D3DMATERIAL7 d3d7mat;
454 TRACE("Activating material %p\n", This);
455 d3d7mat.u.diffuse = This->mat.u.diffuse;
456 d3d7mat.u1.ambient = This->mat.u1.ambient;
457 d3d7mat.u2.specular = This->mat.u2.specular;
458 d3d7mat.u3.emissive = This->mat.u3.emissive;
459 d3d7mat.u4.power = This->mat.u4.power;
461 IDirect3DDevice7_SetMaterial(ICOM_INTERFACE(This->active_device, IDirect3DDevice7),
465 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
467 /*** IUnknown Methods ***/
468 IDirect3DMaterialImpl_QueryInterface,
469 IDirect3DMaterialImpl_AddRef,
470 IDirect3DMaterialImpl_Release,
471 /*** IDirect3DMaterial3 Methods ***/
472 IDirect3DMaterialImpl_SetMaterial,
473 IDirect3DMaterialImpl_GetMaterial,
474 IDirect3DMaterialImpl_GetHandle,
477 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
479 /*** IUnknown Methods ***/
480 Thunk_IDirect3DMaterialImpl_2_QueryInterface,
481 Thunk_IDirect3DMaterialImpl_2_AddRef,
482 Thunk_IDirect3DMaterialImpl_2_Release,
483 /*** IDirect3DMaterial2 Methods ***/
484 Thunk_IDirect3DMaterialImpl_2_SetMaterial,
485 Thunk_IDirect3DMaterialImpl_2_GetMaterial,
486 Thunk_IDirect3DMaterialImpl_2_GetHandle,
489 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
491 /*** IUnknown Methods ***/
492 Thunk_IDirect3DMaterialImpl_1_QueryInterface,
493 Thunk_IDirect3DMaterialImpl_1_AddRef,
494 Thunk_IDirect3DMaterialImpl_1_Release,
495 /*** IDirect3DMaterial1 Methods ***/
496 IDirect3DMaterialImpl_Initialize,
497 Thunk_IDirect3DMaterialImpl_1_SetMaterial,
498 Thunk_IDirect3DMaterialImpl_1_GetMaterial,
499 Thunk_IDirect3DMaterialImpl_1_GetHandle,
500 IDirect3DMaterialImpl_Reserve,
501 IDirect3DMaterialImpl_Unreserve