#undef interface before including alsa headers.
[wine] / dlls / ddraw / d3dlight.c
1 /* Direct3D Light
2  * Copyright (c) 1998 / 2002 Lionel ULMER
3  *
4  * This file contains the implementation of Direct3DLight.
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
23 #include <stdarg.h>
24
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #define CONST_VTABLE
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "objbase.h"
34 #include "wingdi.h"
35 #include "ddraw.h"
36 #include "d3d.h"
37 #include "wine/debug.h"
38
39 #include "d3d_private.h"
40 #include "mesa_private.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
43
44 /* First, the 'main' interface... */
45 HRESULT WINAPI
46 Main_IDirect3DLightImpl_1_QueryInterface(LPDIRECT3DLIGHT iface,
47                                          REFIID riid,
48                                          LPVOID* obp)
49 {
50     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
51     FIXME("(%p/%p)->(%s,%p): stub!\n", This, iface, debugstr_guid(riid), obp);
52     return DD_OK;
53 }
54
55 ULONG WINAPI
56 Main_IDirect3DLightImpl_1_AddRef(LPDIRECT3DLIGHT iface)
57 {
58     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
59     ULONG ref = InterlockedIncrement(&This->ref);
60
61     TRACE("(%p/%p)->() incrementing from %lu.\n", This, iface, ref - 1);
62
63     return ref;
64 }
65
66 ULONG WINAPI
67 Main_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
68 {
69     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
70     ULONG ref = InterlockedDecrement(&This->ref);
71
72     TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, ref + 1);
73
74     if (!ref) {
75         HeapFree(GetProcessHeap(), 0, This);
76         return 0;
77     }
78     return ref;
79 }
80
81 HRESULT WINAPI
82 Main_IDirect3DLightImpl_1_Initialize(LPDIRECT3DLIGHT iface,
83                                      LPDIRECT3D lpDirect3D)
84 {
85     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
86     TRACE("(%p/%p)->(%p) no-op...\n", This, iface, lpDirect3D);
87     return DD_OK;
88 }
89
90 /*** IDirect3DLight methods ***/
91 static void dump_light(LPD3DLIGHT2 light)
92 {
93     DPRINTF("    - dwSize : %ld\n", light->dwSize);
94 }
95
96 static const float zero_value[] = {
97     0.0, 0.0, 0.0, 0.0
98 };
99
100 HRESULT WINAPI
101 Main_IDirect3DLightImpl_1_SetLight(LPDIRECT3DLIGHT iface,
102                                    LPD3DLIGHT lpLight)
103 {
104     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
105     LPD3DLIGHT7 light7 = &(This->light7);
106     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
107     if (TRACE_ON(ddraw)) {
108         TRACE("  Light definition : \n");
109         dump_light((LPD3DLIGHT2) lpLight);
110     }
111
112     if ( (lpLight->dltType == 0) || (lpLight->dltType > D3DLIGHT_PARALLELPOINT) )
113          return DDERR_INVALIDPARAMS;
114     
115     if ( lpLight->dltType == D3DLIGHT_PARALLELPOINT )
116          FIXME("D3DLIGHT_PARALLELPOINT no supported\n");
117     
118     /* Translate D3DLIGH2 structure to D3DLIGHT7 */
119     light7->dltType        = lpLight->dltType;
120     light7->dcvDiffuse     = lpLight->dcvColor;
121     if ((((LPD3DLIGHT2)lpLight)->dwFlags & D3DLIGHT_NO_SPECULAR) != 0)      
122       light7->dcvSpecular    = lpLight->dcvColor;
123     else
124       light7->dcvSpecular    = *(const D3DCOLORVALUE*)zero_value;           
125     light7->dcvAmbient     = lpLight->dcvColor;
126     light7->dvPosition     = lpLight->dvPosition;
127     light7->dvDirection    = lpLight->dvDirection;
128     light7->dvRange        = lpLight->dvRange;
129     light7->dvFalloff      = lpLight->dvFalloff;
130     light7->dvAttenuation0 = lpLight->dvAttenuation0;
131     light7->dvAttenuation1 = lpLight->dvAttenuation1;
132     light7->dvAttenuation2 = lpLight->dvAttenuation2;
133     light7->dvTheta        = lpLight->dvTheta;
134     light7->dvPhi          = lpLight->dvPhi;
135
136     memcpy(&This->light, lpLight, lpLight->dwSize);
137     if ((This->light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
138         This->update(This);        
139     }
140     return DD_OK;
141 }
142
143 HRESULT WINAPI
144 Main_IDirect3DLightImpl_1_GetLight(LPDIRECT3DLIGHT iface,
145                                    LPD3DLIGHT lpLight)
146 {
147     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
148     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
149     if (TRACE_ON(ddraw)) {
150         TRACE("  Returning light definition : \n");
151         dump_light(&This->light);
152     }
153     memcpy(lpLight, &This->light, lpLight->dwSize);
154     return DD_OK;
155 }
156
157 /*******************************************************************************
158  *                              Light static functions
159  */
160
161 static void update(IDirect3DLightImpl* This) {
162     IDirect3DDeviceImpl* device;
163     if (!This->active_viewport||!This->active_viewport->active_device)
164         return;
165     device =  This->active_viewport->active_device;
166     IDirect3DDevice7_SetLight(ICOM_INTERFACE(device,IDirect3DDevice7),This->dwLightIndex,&(This->light7));
167 }
168
169 static void activate(IDirect3DLightImpl* This) {
170     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
171
172     TRACE("(%p)\n", This);
173     
174     ENTER_GL();
175     update(This);
176     /* If was not active, activate it */
177     if ((glThis->parent.light.dwFlags & D3DLIGHT_ACTIVE) == 0) {
178         glEnable(glThis->light_num);
179         glThis->parent.light.dwFlags |= D3DLIGHT_ACTIVE;
180     }
181     LEAVE_GL();
182 }
183
184 static void desactivate(IDirect3DLightImpl* This) {
185     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
186     
187     TRACE("(%p)\n", This);
188     
189     ENTER_GL();
190     /* If was not active, activate it */
191     if ((glThis->parent.light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
192         glDisable(glThis->light_num);
193         glThis->parent.light.dwFlags &= ~D3DLIGHT_ACTIVE;
194     }
195     LEAVE_GL();
196 }
197
198 ULONG WINAPI
199 GL_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
200 {
201     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
202     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
203     ULONG ref = InterlockedDecrement(&This->ref);
204     
205     TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, ref + 1);
206
207     if (!ref) {
208         ((IDirect3DGLImpl *) This->d3d->d3d_private)->light_released(This->d3d, glThis->light_num);
209         HeapFree(GetProcessHeap(), 0, This);
210         return 0;
211     }
212     return ref;
213 }
214
215 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
216 # define XCAST(fun)     (typeof(VTABLE_IDirect3DLight.fun))
217 #else
218 # define XCAST(fun)     (void*)
219 #endif
220
221 static const IDirect3DLightVtbl VTABLE_IDirect3DLight =
222 {
223     XCAST(QueryInterface) Main_IDirect3DLightImpl_1_QueryInterface,
224     XCAST(AddRef) Main_IDirect3DLightImpl_1_AddRef,
225     XCAST(Release) GL_IDirect3DLightImpl_1_Release,
226     XCAST(Initialize) Main_IDirect3DLightImpl_1_Initialize,
227     XCAST(SetLight) Main_IDirect3DLightImpl_1_SetLight,
228     XCAST(GetLight) Main_IDirect3DLightImpl_1_GetLight,
229 };
230
231 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
232 #undef XCAST
233 #endif
234
235
236
237
238 HRESULT d3dlight_create(IDirect3DLightImpl **obj, IDirectDrawImpl *d3d, GLenum light_num)
239 {
240     IDirect3DLightImpl *object;
241     IDirect3DLightGLImpl *gl_object;
242     
243     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DLightGLImpl));
244     if (object == NULL) return DDERR_OUTOFMEMORY;
245     gl_object = (IDirect3DLightGLImpl *) object;
246     
247     object->ref = 1;
248     object->d3d = d3d;
249     object->next = NULL;
250     object->activate = activate;
251     object->desactivate = desactivate;
252     object->update = update;
253     object->active_viewport = NULL;
254     gl_object->light_num = light_num;
255     
256     ICOM_INIT_INTERFACE(object, IDirect3DLight, VTABLE_IDirect3DLight);
257
258     *obj = object;
259
260     TRACE(" creating implementation at %p.\n", *obj);
261     
262     return D3D_OK;
263 }