Assorted spelling fixes.
[wine] / dlls / ddraw / light.c
1 /* Direct3D Light
2  * Copyright (c) 1998 / 2002 Lionel ULMER
3  * Copyright (c) 2006        Stefan DÖSINGER
4  *
5  * This file contains the implementation of Direct3DLight.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #define COBJMACROS
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winnls.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "wine/exception.h"
39 #include "excpt.h"
40
41 #include "ddraw.h"
42 #include "d3d.h"
43
44 #include "ddraw_private.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
47
48 /*****************************************************************************
49  * IUnknown Methods.
50  *****************************************************************************/
51
52 /*****************************************************************************
53  * IDirect3DLight::QueryInterface
54  *
55  * Queries the object for different interfaces. Unimplemented for this
56  * object at the moment
57  *
58  * Params:
59  *  riid: Interface id asked for
60  *  obj: Address to return the resulting pointer at.
61  *
62  * Returns:
63  *  E_NOINTERFACE, because it's a stub
64  *****************************************************************************/
65 static HRESULT WINAPI
66 IDirect3DLightImpl_QueryInterface(IDirect3DLight *iface,
67                                   REFIID riid,
68                                   void **obp)
69 {
70     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
71     FIXME("(%p)->(%s,%p): stub!\n", This, debugstr_guid(riid), obp);
72     *obp = NULL;
73     return E_NOINTERFACE;
74 }
75
76 /*****************************************************************************
77  * IDirect3DLight::AddRef
78  *
79  * Increases the refcount by 1
80  *
81  * Returns:
82  *  The new refcount
83  *
84  *****************************************************************************/
85 static ULONG WINAPI
86 IDirect3DLightImpl_AddRef(IDirect3DLight *iface)
87 {
88     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
89     ULONG ref = InterlockedIncrement(&This->ref);
90
91     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
92
93     return ref;
94 }
95
96 /*****************************************************************************
97  * IDirect3DLight::Release
98  *
99  * Reduces the refcount by one. If the refcount falls to 0, the object
100  * is destroyed
101  *
102  * Returns:
103  *  The new refcount
104  *
105  *****************************************************************************/
106 static ULONG WINAPI
107 IDirect3DLightImpl_Release(IDirect3DLight *iface)
108 {
109     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
110     ULONG ref = InterlockedDecrement(&This->ref);
111
112     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
113
114     if (!ref) {
115         HeapFree(GetProcessHeap(), 0, This);
116         return 0;
117     }
118     return ref;
119 }
120
121 /*****************************************************************************
122  * IDirect3DLight Methods.
123  *****************************************************************************/
124
125 /*****************************************************************************
126  * IDirect3DLight::Initialize
127  *
128  * Initializes the interface. This implementation is a no-op, because
129  * initialization takes place at creation time
130  *
131  * Params:
132  *  Direct3D: Pointer to an IDirect3D interface.
133  *
134  * Returns:
135  *  D3D_OK
136  *
137  *****************************************************************************/
138 static HRESULT WINAPI
139 IDirect3DLightImpl_Initialize(IDirect3DLight *iface,
140                               IDirect3D *lpDirect3D)
141 {
142     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
143     IDirectDrawImpl *d3d = ICOM_OBJECT(IDirectDrawImpl, IDirect3D, lpDirect3D);
144     TRACE("(%p)->(%p) no-op...\n", This, d3d);
145     return D3D_OK;
146 }
147
148 /*****************************************************************************
149  * IDirect3DLight::SetLight
150  *
151  * Assigns a lighting value to this object
152  *
153  * Params:
154  *  Light: Lighting parametes to set
155  *
156  * Returns:
157  *  D3D_OK on success
158  *  DDERR_INVALIDPARAMS if Light is NULL
159  *
160  *****************************************************************************/
161 static void dump_light(LPD3DLIGHT2 light)
162 {
163     DPRINTF("    - dwSize : %d\n", light->dwSize);
164 }
165
166 static const float zero_value[] = {
167     0.0, 0.0, 0.0, 0.0
168 };
169
170 static HRESULT WINAPI
171 IDirect3DLightImpl_SetLight(IDirect3DLight *iface,
172                             D3DLIGHT *lpLight)
173 {
174     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
175     LPD3DLIGHT7 light7 = &(This->light7);
176     TRACE("(%p)->(%p)\n", This, lpLight);
177     if (TRACE_ON(d3d7)) {
178         TRACE("  Light definition :\n");
179         dump_light((LPD3DLIGHT2) lpLight);
180     }
181
182     if ( (lpLight->dltType == 0) || (lpLight->dltType > D3DLIGHT_PARALLELPOINT) )
183          return DDERR_INVALIDPARAMS;
184     
185     if ( lpLight->dltType == D3DLIGHT_PARALLELPOINT )
186          FIXME("D3DLIGHT_PARALLELPOINT no supported\n");
187     
188     /* Translate D3DLIGH2 structure to D3DLIGHT7 */
189     light7->dltType        = lpLight->dltType;
190     light7->dcvDiffuse     = lpLight->dcvColor;
191     if ((((LPD3DLIGHT2)lpLight)->dwFlags & D3DLIGHT_NO_SPECULAR) != 0)      
192       light7->dcvSpecular    = lpLight->dcvColor;
193     else
194       light7->dcvSpecular    = *(const D3DCOLORVALUE*)zero_value;           
195     light7->dcvAmbient     = lpLight->dcvColor;
196     light7->dvPosition     = lpLight->dvPosition;
197     light7->dvDirection    = lpLight->dvDirection;
198     light7->dvRange        = lpLight->dvRange;
199     light7->dvFalloff      = lpLight->dvFalloff;
200     light7->dvAttenuation0 = lpLight->dvAttenuation0;
201     light7->dvAttenuation1 = lpLight->dvAttenuation1;
202     light7->dvAttenuation2 = lpLight->dvAttenuation2;
203     light7->dvTheta        = lpLight->dvTheta;
204     light7->dvPhi          = lpLight->dvPhi;
205
206     memcpy(&This->light, lpLight, lpLight->dwSize);
207     if ((This->light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
208         This->update(This);        
209     }
210     return D3D_OK;
211 }
212
213 /*****************************************************************************
214  * IDirect3DLight::GetLight
215  *
216  * Returns the parameters currently assigned to the IDirect3DLight object
217  *
218  * Params:
219  *  Light: Pointer to an D3DLIGHT structure to store the parameters
220  *
221  * Returns:
222  *  D3D_OK on success
223  *  DDERR_INVALIDPARAMS if Light is NULL
224  *****************************************************************************/
225 static HRESULT WINAPI
226 IDirect3DLightImpl_GetLight(IDirect3DLight *iface,
227                             D3DLIGHT *lpLight)
228 {
229     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
230     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
231     if (TRACE_ON(d3d7)) {
232         TRACE("  Returning light definition :\n");
233         dump_light(&This->light);
234     }
235     memcpy(lpLight, &This->light, lpLight->dwSize);
236     return DD_OK;
237 }
238
239 /*****************************************************************************
240  * light_update
241  *
242  * Updates the Direct3DDevice7 lighting parameters
243  *
244  *****************************************************************************/
245 void light_update(IDirect3DLightImpl* This)
246 {
247     IDirect3DDeviceImpl* device;
248
249     TRACE("(%p)\n", This);
250
251     if (!This->active_viewport || !This->active_viewport->active_device)
252         return;
253     device =  This->active_viewport->active_device;
254
255     IDirect3DDevice7_SetLight(ICOM_INTERFACE(device,IDirect3DDevice7), This->dwLightIndex, &(This->light7));
256 }
257
258 /*****************************************************************************
259  * light_activate
260  *
261  * Uses the Direct3DDevice7::LightEnable method to active the light
262  *
263  *****************************************************************************/
264 void light_activate(IDirect3DLightImpl* This)
265 {
266     IDirect3DDeviceImpl* device;
267
268     TRACE("(%p)\n", This);
269
270     if (!This->active_viewport || !This->active_viewport->active_device)
271         return;
272     device =  This->active_viewport->active_device;
273     
274     light_update(This);
275     /* If was not active, activate it */
276     if ((This->light.dwFlags & D3DLIGHT_ACTIVE) == 0) {
277         IDirect3DDevice7_LightEnable(ICOM_INTERFACE(device,IDirect3DDevice7), This->dwLightIndex, TRUE);
278         This->light.dwFlags |= D3DLIGHT_ACTIVE;
279     }
280 }
281
282 /*****************************************************************************
283  *
284  * light_desactivate
285  *
286  * Uses the Direct3DDevice7::LightEnable method to deactivate the light
287  *
288  *****************************************************************************/
289 void light_desactivate(IDirect3DLightImpl* This)
290 {
291     IDirect3DDeviceImpl* device;
292
293     TRACE("(%p)\n", This);
294
295     if (!This->active_viewport || !This->active_viewport->active_device)
296         return;
297     device =  This->active_viewport->active_device;
298     
299     /* If was not active, activate it */
300     if ((This->light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
301         IDirect3DDevice7_LightEnable(ICOM_INTERFACE(device,IDirect3DDevice7), This->dwLightIndex, FALSE);
302         This->light.dwFlags &= ~D3DLIGHT_ACTIVE;
303     }
304 }
305
306 const IDirect3DLightVtbl IDirect3DLight_Vtbl =
307 {
308     /*** IUnknown Methods ***/
309     IDirect3DLightImpl_QueryInterface,
310     IDirect3DLightImpl_AddRef,
311     IDirect3DLightImpl_Release,
312     /*** IDirect3DLight Methods ***/
313     IDirect3DLightImpl_Initialize,
314     IDirect3DLightImpl_SetLight,
315     IDirect3DLightImpl_GetLight
316 };