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 static inline IDirect3DMaterialImpl *material_from_material1(IDirect3DMaterial *iface)
55 return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial_vtbl));
58 static inline IDirect3DMaterialImpl *material_from_material2(IDirect3DMaterial2 *iface)
60 return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial2_vtbl));
63 /*****************************************************************************
65 *****************************************************************************/
67 /*****************************************************************************
68 * IDirect3DMaterial3::QueryInterface
70 * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
74 * riid: Interface id queried for
75 * obj: Address to pass the interface pointer back
79 * E_NOINTERFACE if the requested interface wasn't found
81 *****************************************************************************/
83 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
87 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
88 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
92 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
93 IUnknown_AddRef(iface);
95 TRACE(" Creating IUnknown interface at %p.\n", *obp);
98 if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
99 IDirect3DMaterial_AddRef((IDirect3DMaterial *)&This->IDirect3DMaterial_vtbl);
100 *obp = &This->IDirect3DMaterial_vtbl;
101 TRACE(" Creating IDirect3DMaterial interface %p\n", *obp);
104 if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
105 IDirect3DMaterial_AddRef((IDirect3DMaterial2 *)&This->IDirect3DMaterial2_vtbl);
106 *obp = &This->IDirect3DMaterial2_vtbl;
107 TRACE(" Creating IDirect3DMaterial2 interface %p\n", *obp);
110 if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
111 IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)This);
113 TRACE(" Creating IDirect3DMaterial3 interface %p\n", *obp);
116 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
117 return E_NOINTERFACE;
120 /*****************************************************************************
121 * IDirect3DMaterial3::AddRef
123 * Increases the refcount.
128 *****************************************************************************/
130 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
132 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
133 ULONG ref = InterlockedIncrement(&This->ref);
135 TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
140 /*****************************************************************************
141 * IDirect3DMaterial3::Release
143 * Reduces the refcount by one. If the refcount falls to 0, the object
149 *****************************************************************************/
151 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
153 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
154 ULONG ref = InterlockedDecrement(&This->ref);
156 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
162 EnterCriticalSection(&ddraw_cs);
163 This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
164 This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
165 LeaveCriticalSection(&ddraw_cs);
168 HeapFree(GetProcessHeap(), 0, This);
174 /*****************************************************************************
175 * IDirect3DMaterial Methods
176 *****************************************************************************/
178 /*****************************************************************************
179 * IDirect3DMaterial::Initialize
181 * A no-op initialization
184 * Direct3D: Pointer to a Direct3D interface
189 *****************************************************************************/
190 static HRESULT WINAPI
191 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
194 IDirect3DMaterialImpl *This = material_from_material1(iface);
196 TRACE("(%p)->(%p) no-op...!\n", This, Direct3D);
201 /*****************************************************************************
202 * IDirect3DMaterial::Reserve
204 * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
205 * Odd. They seem to have mixed their interfaces.
210 *****************************************************************************/
211 static HRESULT WINAPI
212 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
214 IDirect3DMaterialImpl *This = material_from_material1(iface);
215 TRACE("(%p)->() not implemented\n", This);
217 return DDERR_UNSUPPORTED;
220 /*****************************************************************************
221 * IDirect3DMaterial::Unreserve
228 *****************************************************************************/
229 static HRESULT WINAPI
230 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
232 IDirect3DMaterialImpl *This = material_from_material1(iface);
233 TRACE("(%p)->() not implemented.\n", This);
235 return DDERR_UNSUPPORTED;
238 /*****************************************************************************
239 * IDirect3DMaterial3::SetMaterial
241 * Sets the material description
244 * Mat: Material to set
248 * DDERR_INVALIDPARAMS if Mat is NULL
250 *****************************************************************************/
251 static HRESULT WINAPI
252 IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
255 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
256 TRACE("(%p)->(%p)\n", This, lpMat);
258 dump_material(lpMat);
260 /* Stores the material */
261 EnterCriticalSection(&ddraw_cs);
262 memset(&This->mat, 0, sizeof(This->mat));
263 memcpy(&This->mat, lpMat, lpMat->dwSize);
264 LeaveCriticalSection(&ddraw_cs);
269 /*****************************************************************************
270 * IDirect3DMaterial3::GetMaterial
272 * Returns the material assigned to this interface
275 * Mat: Pointer to a D3DMATERIAL structure to store the material description
279 * DDERR_INVALIDPARAMS if Mat is NULL
281 *****************************************************************************/
282 static HRESULT WINAPI
283 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
286 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
288 TRACE("(%p)->(%p)\n", This, lpMat);
289 if (TRACE_ON(d3d7)) {
290 TRACE(" Returning material : ");
291 dump_material(&This->mat);
294 /* Copies the material structure */
295 EnterCriticalSection(&ddraw_cs);
296 dwSize = lpMat->dwSize;
297 memset(lpMat, 0, dwSize);
298 memcpy(lpMat, &This->mat, dwSize);
299 LeaveCriticalSection(&ddraw_cs);
304 /*****************************************************************************
305 * IDirect3DMaterial3::GetHandle
307 * Returns a handle for the material interface. The handle is simply a
308 * pointer to the material implementation
311 * Direct3DDevice3: The device this handle is assigned to
312 * Handle: Address to write the handle to
316 * DDERR_INVALIDPARAMS if Handle is NULL
318 *****************************************************************************/
319 static HRESULT WINAPI
320 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
321 IDirect3DDevice3 *lpDirect3DDevice3,
322 D3DMATERIALHANDLE *lpHandle)
324 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
325 IDirect3DDeviceImpl *device = device_from_device3(lpDirect3DDevice3);
326 TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
328 EnterCriticalSection(&ddraw_cs);
329 This->active_device = device;
332 This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
335 ERR("Error creating a handle\n");
336 LeaveCriticalSection(&ddraw_cs);
337 return DDERR_INVALIDPARAMS; /* Unchecked */
339 device->Handles[This->Handle - 1].ptr = This;
340 device->Handles[This->Handle - 1].type = DDrawHandle_Material;
342 *lpHandle = This->Handle;
343 TRACE(" returning handle %08x.\n", *lpHandle);
344 LeaveCriticalSection(&ddraw_cs);
349 static HRESULT WINAPI
350 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
351 LPDIRECT3DDEVICE2 lpDirect3DDevice2,
352 LPD3DMATERIALHANDLE lpHandle)
354 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
355 return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material2(iface), lpDirect3DDevice2 ?
356 (IDirect3DDevice3 *)&device_from_device2(lpDirect3DDevice2)->IDirect3DDevice3_vtbl : NULL, lpHandle);
359 static HRESULT WINAPI
360 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
361 LPDIRECT3DDEVICE lpDirect3DDevice,
362 LPD3DMATERIALHANDLE lpHandle)
364 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
365 return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material1(iface), lpDirect3DDevice ?
366 (IDirect3DDevice3 *)&device_from_device1(lpDirect3DDevice)->IDirect3DDevice3_vtbl : NULL, lpHandle);
369 static HRESULT WINAPI
370 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
374 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
375 return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material2(iface), riid, obp);
378 static HRESULT WINAPI
379 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
383 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
384 return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material1(iface), riid, obp);
388 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
390 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
391 return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material2(iface));
395 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
397 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
398 return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material1(iface));
402 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
404 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
405 return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material2(iface));
409 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
411 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
412 return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material1(iface));
415 static HRESULT WINAPI
416 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
419 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
420 return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
423 static HRESULT WINAPI
424 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
427 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
428 return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
431 static HRESULT WINAPI
432 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
435 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
436 return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
439 static HRESULT WINAPI
440 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
443 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
444 return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
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((IDirect3DDevice7 *)This->active_device, &d3d7mat);
471 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
473 /*** IUnknown Methods ***/
474 IDirect3DMaterialImpl_QueryInterface,
475 IDirect3DMaterialImpl_AddRef,
476 IDirect3DMaterialImpl_Release,
477 /*** IDirect3DMaterial3 Methods ***/
478 IDirect3DMaterialImpl_SetMaterial,
479 IDirect3DMaterialImpl_GetMaterial,
480 IDirect3DMaterialImpl_GetHandle,
483 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
485 /*** IUnknown Methods ***/
486 Thunk_IDirect3DMaterialImpl_2_QueryInterface,
487 Thunk_IDirect3DMaterialImpl_2_AddRef,
488 Thunk_IDirect3DMaterialImpl_2_Release,
489 /*** IDirect3DMaterial2 Methods ***/
490 Thunk_IDirect3DMaterialImpl_2_SetMaterial,
491 Thunk_IDirect3DMaterialImpl_2_GetMaterial,
492 Thunk_IDirect3DMaterialImpl_2_GetHandle,
495 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
497 /*** IUnknown Methods ***/
498 Thunk_IDirect3DMaterialImpl_1_QueryInterface,
499 Thunk_IDirect3DMaterialImpl_1_AddRef,
500 Thunk_IDirect3DMaterialImpl_1_Release,
501 /*** IDirect3DMaterial1 Methods ***/
502 IDirect3DMaterialImpl_Initialize,
503 Thunk_IDirect3DMaterialImpl_1_SetMaterial,
504 Thunk_IDirect3DMaterialImpl_1_GetMaterial,
505 Thunk_IDirect3DMaterialImpl_1_GetHandle,
506 IDirect3DMaterialImpl_Reserve,
507 IDirect3DMaterialImpl_Unreserve