- removed some useless WARNs
[wine] / dlls / ddraw / d3dmaterial.c
1 /* Direct3D Material
2  * Copyright (c) 2002 Lionel ULMER
3  *
4  * This file contains the implementation of Direct3DMaterial.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "windef.h"
23 #include "winerror.h"
24 #include "objbase.h"
25 #include "ddraw.h"
26 #include "d3d.h"
27 #include "wine/debug.h"
28
29 #include "d3d_private.h"
30 #include "mesa_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33
34 static void dump_material(LPD3DMATERIAL mat)
35 {
36     DPRINTF("  dwSize : %ld\n", mat->dwSize);
37 }
38
39 HRESULT WINAPI
40 Main_IDirect3DMaterialImpl_3_2T_1T_QueryInterface(LPDIRECT3DMATERIAL3 iface,
41                                                   REFIID riid,
42                                                   LPVOID* obp)
43 {
44     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
45     TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(riid), obp);
46
47     *obp = NULL;
48
49     if ( IsEqualGUID( &IID_IUnknown,  riid ) ) {
50         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
51         *obp = iface;
52         TRACE("  Creating IUnknown interface at %p.\n", *obp);
53         return S_OK;
54     }
55     if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
56         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
57         *obp = ICOM_INTERFACE(This, IDirect3DMaterial);
58         TRACE("  Creating IDirect3DMaterial interface %p\n", *obp);
59         return S_OK;
60     }
61     if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
62         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
63         *obp = ICOM_INTERFACE(This, IDirect3DMaterial2);
64         TRACE("  Creating IDirect3DMaterial2 interface %p\n", *obp);
65         return S_OK;
66     }
67     if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
68         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
69         *obp = ICOM_INTERFACE(This, IDirect3DMaterial3);
70         TRACE("  Creating IDirect3DMaterial3 interface %p\n", *obp);
71         return S_OK;
72     }
73     FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
74     return OLE_E_ENUM_NOMORE;
75 }
76
77 ULONG WINAPI
78 Main_IDirect3DMaterialImpl_3_2T_1T_AddRef(LPDIRECT3DMATERIAL3 iface)
79 {
80     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
81     TRACE("(%p/%p)->() incrementing from %lu.\n", This, iface, This->ref);
82     return ++(This->ref);
83 }
84
85 ULONG WINAPI
86 Main_IDirect3DMaterialImpl_3_2T_1T_Release(LPDIRECT3DMATERIAL3 iface)
87 {
88     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
89     TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, This->ref);
90     if (!--(This->ref)) {
91         HeapFree(GetProcessHeap(), 0, This);
92         return 0;
93     }
94     return This->ref;
95 }
96
97 HRESULT WINAPI
98 Main_IDirect3DMaterialImpl_1_Initialize(LPDIRECT3DMATERIAL iface,
99                                         LPDIRECT3D lpDirect3D)
100 {
101     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
102     TRACE("(%p/%p)->(%p) no-op...!\n", This, iface, lpDirect3D);
103     return DD_OK;
104 }
105
106 HRESULT WINAPI
107 Main_IDirect3DMaterialImpl_1_Reserve(LPDIRECT3DMATERIAL iface)
108 {
109     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
110     TRACE("(%p/%p)->() not implemented.\n", This, iface);
111     return DD_OK;
112 }
113
114 HRESULT WINAPI
115 Main_IDirect3DMaterialImpl_1_Unreserve(LPDIRECT3DMATERIAL iface)
116 {
117     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
118     FIXME("(%p/%p)->() not implemented.\n", This, iface);
119     return DD_OK;
120 }
121
122 HRESULT WINAPI
123 Main_IDirect3DMaterialImpl_3_2T_1T_SetMaterial(LPDIRECT3DMATERIAL3 iface,
124                                                LPD3DMATERIAL lpMat)
125 {
126     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
127     TRACE("(%p/%p)->(%p)\n", This, iface, lpMat);
128     if (TRACE_ON(ddraw))
129         dump_material(lpMat);
130
131     /* Stores the material */
132     memset(&This->mat, 0, sizeof(This->mat));
133     memcpy(&This->mat, lpMat, lpMat->dwSize);
134     
135     return DD_OK;
136 }
137
138 HRESULT WINAPI
139 Main_IDirect3DMaterialImpl_3_2T_1T_GetMaterial(LPDIRECT3DMATERIAL3 iface,
140                                                LPD3DMATERIAL lpMat)
141 {
142     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
143     DWORD dwSize;
144     TRACE("(%p/%p)->(%p)\n", This, iface, lpMat);
145     if (TRACE_ON(ddraw)) {
146         TRACE("  Returning material : ");
147         dump_material(&This->mat);
148     }
149
150     /* Copies the material structure */
151     dwSize = lpMat->dwSize;
152     memset(lpMat, 0, dwSize);
153     memcpy(lpMat, &This->mat, dwSize);
154
155     return DD_OK;
156 }
157
158 HRESULT WINAPI
159 Main_IDirect3DMaterialImpl_3_2T_1T_GetHandle(LPDIRECT3DMATERIAL3 iface,
160                                              LPDIRECT3DDEVICE3 lpDirect3DDevice3,
161                                              LPD3DMATERIALHANDLE lpHandle)
162 {
163     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
164     TRACE("(%p/%p)->(%p,%p)\n", This, iface, lpDirect3DDevice3, lpHandle);
165
166     This->active_device = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, lpDirect3DDevice3);
167     *lpHandle = (DWORD) This; /* Warning: this is not 64 bit clean.
168                                  Maybe also we need to store this material somewhere in the device ? */
169
170     TRACE(" returning handle %08lx.\n", *lpHandle);
171     
172     return DD_OK;
173 }
174
175 HRESULT WINAPI
176 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
177                                         LPDIRECT3DDEVICE2 lpDirect3DDevice2,
178                                         LPD3DMATERIALHANDLE lpHandle)
179 {
180     TRACE("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
181     return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
182                                         COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice2, IDirect3DDevice3, lpDirect3DDevice2),
183                                         lpHandle);
184 }
185
186 HRESULT WINAPI
187 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
188                                         LPDIRECT3DDEVICE lpDirect3DDevice,
189                                         LPD3DMATERIALHANDLE lpHandle)
190 {
191     TRACE("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
192     return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
193                                         COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice3, lpDirect3DDevice),
194                                         lpHandle);
195 }
196
197 HRESULT WINAPI
198 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
199                                              REFIID riid,
200                                              LPVOID* obp)
201 {
202     TRACE("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
203     return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
204                                              riid,
205                                              obp);
206 }
207
208 HRESULT WINAPI
209 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
210                                              REFIID riid,
211                                              LPVOID* obp)
212 {
213     TRACE("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
214     return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
215                                              riid,
216                                              obp);
217 }
218
219 ULONG WINAPI
220 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
221 {
222     TRACE("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
223     return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
224 }
225
226 ULONG WINAPI
227 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
228 {
229     TRACE("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
230     return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
231 }
232
233 ULONG WINAPI
234 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
235 {
236     TRACE("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
237     return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
238 }
239
240 ULONG WINAPI
241 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
242 {
243     TRACE("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
244     return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
245 }
246
247 HRESULT WINAPI
248 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
249                                           LPD3DMATERIAL lpMat)
250 {
251     TRACE("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
252     return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
253                                           lpMat);
254 }
255
256 HRESULT WINAPI
257 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
258                                           LPD3DMATERIAL lpMat)
259 {
260     TRACE("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
261     return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
262                                           lpMat);
263 }
264
265 HRESULT WINAPI
266 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
267                                           LPD3DMATERIAL lpMat)
268 {
269     TRACE("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
270     return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
271                                           lpMat);
272 }
273
274 HRESULT WINAPI
275 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
276                                           LPD3DMATERIAL lpMat)
277 {
278     TRACE("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
279     return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
280                                           lpMat);
281 }
282
283 /*******************************************************************************
284  *                              Matrial2 static functions
285  */
286 static void activate(IDirect3DMaterialImpl* This) {
287   TRACE("Activating material %p\n", This);
288
289   ENTER_GL();
290
291   /* Set the current Material */
292   _dump_colorvalue("Diffuse", This->mat.u.diffuse);
293   glMaterialfv(GL_FRONT_AND_BACK,
294                GL_DIFFUSE,
295                (float *) &(This->mat.u.diffuse));
296   _dump_colorvalue("Ambient", This->mat.u1.ambient);
297   glMaterialfv(GL_FRONT_AND_BACK,
298                GL_AMBIENT,
299                (float *) &(This->mat.u1.ambient));
300   _dump_colorvalue("Specular", This->mat.u2.specular);
301   glMaterialfv(GL_FRONT_AND_BACK,
302                GL_SPECULAR,
303                (float *) &(This->mat.u2.specular));
304   _dump_colorvalue("Emissive", This->mat.u3.emissive);
305   glMaterialfv(GL_FRONT_AND_BACK,
306                GL_EMISSION,
307                (float *) &(This->mat.u3.emissive));
308
309   TRACE("Size  : %ld\n", This->mat.dwSize);
310   TRACE("Power : %f\n", This->mat.u4.power);
311
312   TRACE("Texture handle : %08lx\n", (DWORD)This->mat.hTexture);
313   LEAVE_GL();
314
315   return ;
316 }
317
318 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
319 # define XCAST(fun)     (typeof(VTABLE_IDirect3DMaterial3.fun))
320 #else
321 # define XCAST(fun)     (void*)
322 #endif
323
324 ICOM_VTABLE(IDirect3DMaterial3) VTABLE_IDirect3DMaterial3 =
325 {
326     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
327     XCAST(QueryInterface) Main_IDirect3DMaterialImpl_3_2T_1T_QueryInterface,
328     XCAST(AddRef) Main_IDirect3DMaterialImpl_3_2T_1T_AddRef,
329     XCAST(Release) Main_IDirect3DMaterialImpl_3_2T_1T_Release,
330     XCAST(SetMaterial) Main_IDirect3DMaterialImpl_3_2T_1T_SetMaterial,
331     XCAST(GetMaterial) Main_IDirect3DMaterialImpl_3_2T_1T_GetMaterial,
332     XCAST(GetHandle) Main_IDirect3DMaterialImpl_3_2T_1T_GetHandle,
333 };
334
335 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
336 #undef XCAST
337 #endif
338
339
340 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
341 # define XCAST(fun)     (typeof(VTABLE_IDirect3DMaterial2.fun))
342 #else
343 # define XCAST(fun)     (void*)
344 #endif
345
346 ICOM_VTABLE(IDirect3DMaterial2) VTABLE_IDirect3DMaterial2 =
347 {
348     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
349     XCAST(QueryInterface) Thunk_IDirect3DMaterialImpl_2_QueryInterface,
350     XCAST(AddRef) Thunk_IDirect3DMaterialImpl_2_AddRef,
351     XCAST(Release) Thunk_IDirect3DMaterialImpl_2_Release,
352     XCAST(SetMaterial) Thunk_IDirect3DMaterialImpl_2_SetMaterial,
353     XCAST(GetMaterial) Thunk_IDirect3DMaterialImpl_2_GetMaterial,
354     XCAST(GetHandle) Thunk_IDirect3DMaterialImpl_2_GetHandle,
355 };
356
357 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
358 #undef XCAST
359 #endif
360
361
362 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
363 # define XCAST(fun)     (typeof(VTABLE_IDirect3DMaterial.fun))
364 #else
365 # define XCAST(fun)     (void*)
366 #endif
367
368 ICOM_VTABLE(IDirect3DMaterial) VTABLE_IDirect3DMaterial =
369 {
370     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
371     XCAST(QueryInterface) Thunk_IDirect3DMaterialImpl_1_QueryInterface,
372     XCAST(AddRef) Thunk_IDirect3DMaterialImpl_1_AddRef,
373     XCAST(Release) Thunk_IDirect3DMaterialImpl_1_Release,
374     XCAST(Initialize) Main_IDirect3DMaterialImpl_1_Initialize,
375     XCAST(SetMaterial) Thunk_IDirect3DMaterialImpl_1_SetMaterial,
376     XCAST(GetMaterial) Thunk_IDirect3DMaterialImpl_1_GetMaterial,
377     XCAST(GetHandle) Thunk_IDirect3DMaterialImpl_1_GetHandle,
378     XCAST(Reserve) Main_IDirect3DMaterialImpl_1_Reserve,
379     XCAST(Unreserve) Main_IDirect3DMaterialImpl_1_Unreserve,
380 };
381
382 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
383 #undef XCAST
384 #endif
385
386
387
388
389 HRESULT d3dmaterial_create(IDirect3DMaterialImpl **obj, IDirect3DImpl *d3d)
390 {
391     IDirect3DMaterialImpl *object;
392
393     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DMaterialImpl));
394     if (object == NULL) return DDERR_OUTOFMEMORY;
395
396     object->ref = 1;
397     object->d3d = d3d;
398     object->activate = activate;
399     
400     ICOM_INIT_INTERFACE(object, IDirect3DMaterial,  VTABLE_IDirect3DMaterial);
401     ICOM_INIT_INTERFACE(object, IDirect3DMaterial2, VTABLE_IDirect3DMaterial2);
402     ICOM_INIT_INTERFACE(object, IDirect3DMaterial3, VTABLE_IDirect3DMaterial3);
403
404     *obj = object;
405     
406     TRACE(" creating implementation at %p.\n", *obj);
407     
408     return D3D_OK;
409 }