gdi32: Move DescribePixelFormat and SetPixelFormat to the WGL driver.
[wine.git] / dlls / ddraw / light.c
blobad4ac712e2de354f391d2be0be2327bde9812cd4
1 /* Direct3D Light
2 * Copyright (c) 1998 / 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This file contains the implementation of Direct3DLight.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include "ddraw_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
29 /*****************************************************************************
30 * light_update
32 * Updates the Direct3DDevice7 lighting parameters
34 *****************************************************************************/
35 static void light_update(struct d3d_light *light)
37 struct d3d_device *device;
39 TRACE("light %p.\n", light);
41 if (!light->active_viewport || !light->active_viewport->active_device) return;
42 device = light->active_viewport->active_device;
44 IDirect3DDevice7_SetLight(&device->IDirect3DDevice7_iface, light->dwLightIndex, &light->light7);
47 /*****************************************************************************
48 * light_activate
50 * Uses the Direct3DDevice7::LightEnable method to active the light
52 *****************************************************************************/
53 void light_activate(struct d3d_light *light)
55 struct d3d_device *device;
57 TRACE("light %p.\n", light);
59 if (!light->active_viewport || !light->active_viewport->active_device) return;
60 device = light->active_viewport->active_device;
62 light_update(light);
63 if (!(light->light.dwFlags & D3DLIGHT_ACTIVE))
65 IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->dwLightIndex, TRUE);
66 light->light.dwFlags |= D3DLIGHT_ACTIVE;
70 /*****************************************************************************
72 * light_deactivate
74 * Uses the Direct3DDevice7::LightEnable method to deactivate the light
76 *****************************************************************************/
77 void light_deactivate(struct d3d_light *light)
79 struct d3d_device *device;
81 TRACE("light %p.\n", light);
83 if (!light->active_viewport || !light->active_viewport->active_device) return;
84 device = light->active_viewport->active_device;
86 /* If was not active, activate it */
87 if (light->light.dwFlags & D3DLIGHT_ACTIVE)
89 IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->dwLightIndex, FALSE);
90 light->light.dwFlags &= ~D3DLIGHT_ACTIVE;
94 static inline struct d3d_light *impl_from_IDirect3DLight(IDirect3DLight *iface)
96 return CONTAINING_RECORD(iface, struct d3d_light, IDirect3DLight_iface);
99 /*****************************************************************************
100 * IDirect3DLight::QueryInterface
102 * Queries the object for different interfaces. Unimplemented for this
103 * object at the moment
105 * Params:
106 * riid: Interface id asked for
107 * obj: Address to return the resulting pointer at.
109 * Returns:
110 * E_NOINTERFACE, because it's a stub
111 *****************************************************************************/
112 static HRESULT WINAPI d3d_light_QueryInterface(IDirect3DLight *iface, REFIID riid, void **object)
114 FIXME("iface %p, riid %s, object %p stub!\n", iface, debugstr_guid(riid), object);
116 *object = NULL;
117 return E_NOINTERFACE;
120 static ULONG WINAPI d3d_light_AddRef(IDirect3DLight *iface)
122 struct d3d_light *light = impl_from_IDirect3DLight(iface);
123 ULONG ref = InterlockedIncrement(&light->ref);
125 TRACE("%p increasing refcount to %u.\n", light, ref);
127 return ref;
130 static ULONG WINAPI d3d_light_Release(IDirect3DLight *iface)
132 struct d3d_light *light = impl_from_IDirect3DLight(iface);
133 ULONG ref = InterlockedDecrement(&light->ref);
135 TRACE("%p decreasing refcount to %u.\n", light, ref);
137 if (!ref)
139 HeapFree(GetProcessHeap(), 0, light);
140 return 0;
142 return ref;
145 /*****************************************************************************
146 * IDirect3DLight Methods.
147 *****************************************************************************/
149 /*****************************************************************************
150 * IDirect3DLight::Initialize
152 * Initializes the interface. This implementation is a no-op, because
153 * initialization takes place at creation time
155 * Params:
156 * Direct3D: Pointer to an IDirect3D interface.
158 * Returns:
159 * D3D_OK
161 *****************************************************************************/
162 static HRESULT WINAPI d3d_light_Initialize(IDirect3DLight *iface, IDirect3D *d3d)
164 TRACE("iface %p, d3d %p.\n", iface, d3d);
166 return D3D_OK;
169 /*****************************************************************************
170 * IDirect3DLight::SetLight
172 * Assigns a lighting value to this object
174 * Params:
175 * Light: Lighting parameter to set
177 * Returns:
178 * D3D_OK on success
179 * DDERR_INVALIDPARAMS if Light is NULL
181 *****************************************************************************/
182 static void dump_light(const D3DLIGHT2 *light)
184 TRACE(" - dwSize : %d\n", light->dwSize);
187 static const float zero_value[] = {
188 0.0, 0.0, 0.0, 0.0
191 static HRESULT WINAPI d3d_light_SetLight(IDirect3DLight *iface, D3DLIGHT *lpLight)
193 struct d3d_light *light = impl_from_IDirect3DLight(iface);
194 D3DLIGHT7 *light7 = &light->light7;
196 TRACE("iface %p, light %p.\n", iface, lpLight);
198 if (TRACE_ON(ddraw))
200 TRACE(" Light definition :\n");
201 dump_light((LPD3DLIGHT2) lpLight);
204 if ( (lpLight->dltType == 0) || (lpLight->dltType > D3DLIGHT_PARALLELPOINT) )
205 return DDERR_INVALIDPARAMS;
207 if ( lpLight->dltType == D3DLIGHT_PARALLELPOINT )
208 FIXME("D3DLIGHT_PARALLELPOINT not supported\n");
210 /* Translate D3DLIGH2 structure to D3DLIGHT7 */
211 light7->dltType = lpLight->dltType;
212 light7->dcvDiffuse = lpLight->dcvColor;
213 if ((((LPD3DLIGHT2)lpLight)->dwFlags & D3DLIGHT_NO_SPECULAR) != 0)
214 light7->dcvSpecular = lpLight->dcvColor;
215 else
216 light7->dcvSpecular = *(const D3DCOLORVALUE*)zero_value;
217 light7->dcvAmbient = lpLight->dcvColor;
218 light7->dvPosition = lpLight->dvPosition;
219 light7->dvDirection = lpLight->dvDirection;
220 light7->dvRange = lpLight->dvRange;
221 light7->dvFalloff = lpLight->dvFalloff;
222 light7->dvAttenuation0 = lpLight->dvAttenuation0;
223 light7->dvAttenuation1 = lpLight->dvAttenuation1;
224 light7->dvAttenuation2 = lpLight->dvAttenuation2;
225 light7->dvTheta = lpLight->dvTheta;
226 light7->dvPhi = lpLight->dvPhi;
228 wined3d_mutex_lock();
229 memcpy(&light->light, lpLight, lpLight->dwSize);
230 if (light->light.dwFlags & D3DLIGHT_ACTIVE)
231 light_update(light);
232 wined3d_mutex_unlock();
234 return D3D_OK;
237 /*****************************************************************************
238 * IDirect3DLight::GetLight
240 * Returns the parameters currently assigned to the IDirect3DLight object
242 * Params:
243 * Light: Pointer to an D3DLIGHT structure to store the parameters
245 * Returns:
246 * D3D_OK on success
247 * DDERR_INVALIDPARAMS if Light is NULL
248 *****************************************************************************/
249 static HRESULT WINAPI d3d_light_GetLight(IDirect3DLight *iface, D3DLIGHT *lpLight)
251 struct d3d_light *light = impl_from_IDirect3DLight(iface);
253 TRACE("iface %p, light %p.\n", iface, lpLight);
255 if (TRACE_ON(ddraw))
257 TRACE(" Returning light definition :\n");
258 dump_light(&light->light);
261 wined3d_mutex_lock();
262 memcpy(lpLight, &light->light, lpLight->dwSize);
263 wined3d_mutex_unlock();
265 return DD_OK;
268 static const struct IDirect3DLightVtbl d3d_light_vtbl =
270 /*** IUnknown Methods ***/
271 d3d_light_QueryInterface,
272 d3d_light_AddRef,
273 d3d_light_Release,
274 /*** IDirect3DLight Methods ***/
275 d3d_light_Initialize,
276 d3d_light_SetLight,
277 d3d_light_GetLight
280 void d3d_light_init(struct d3d_light *light, struct ddraw *ddraw)
282 light->IDirect3DLight_iface.lpVtbl = &d3d_light_vtbl;
283 light->ref = 1;
284 light->ddraw = ddraw;
287 struct d3d_light *unsafe_impl_from_IDirect3DLight(IDirect3DLight *iface)
289 if (!iface)
290 return NULL;
291 assert(iface->lpVtbl == &d3d_light_vtbl);
293 return impl_from_IDirect3DLight(iface);