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