19e49845163d6a15181e884abc4c7e1f0e582954
[reactos.git] / reactos / dll / directx / wine / 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
25 #include "ddraw_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
28
29 /*****************************************************************************
30 * light_update
31 *
32 * Updates the Direct3DDevice7 lighting parameters
33 *
34 *****************************************************************************/
35 static void light_update(struct d3d_light *light)
36 {
37 struct d3d_device *device;
38
39 TRACE("light %p.\n", light);
40
41 if (!light->active_viewport || !light->active_viewport->active_device) return;
42 device = light->active_viewport->active_device;
43
44 IDirect3DDevice7_SetLight(&device->IDirect3DDevice7_iface, light->dwLightIndex, &light->light7);
45 }
46
47 /*****************************************************************************
48 * light_activate
49 *
50 * Uses the Direct3DDevice7::LightEnable method to active the light
51 *
52 *****************************************************************************/
53 void light_activate(struct d3d_light *light)
54 {
55 struct d3d_device *device;
56
57 TRACE("light %p.\n", light);
58
59 if (!light->active_viewport || !light->active_viewport->active_device) return;
60 device = light->active_viewport->active_device;
61
62 light_update(light);
63 if (!(light->light.dwFlags & D3DLIGHT_ACTIVE))
64 {
65 IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->dwLightIndex, TRUE);
66 light->light.dwFlags |= D3DLIGHT_ACTIVE;
67 }
68 }
69
70 /*****************************************************************************
71 *
72 * light_deactivate
73 *
74 * Uses the Direct3DDevice7::LightEnable method to deactivate the light
75 *
76 *****************************************************************************/
77 void light_deactivate(struct d3d_light *light)
78 {
79 struct d3d_device *device;
80
81 TRACE("light %p.\n", light);
82
83 if (!light->active_viewport || !light->active_viewport->active_device) return;
84 device = light->active_viewport->active_device;
85
86 /* If was not active, activate it */
87 if (light->light.dwFlags & D3DLIGHT_ACTIVE)
88 {
89 IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->dwLightIndex, FALSE);
90 light->light.dwFlags &= ~D3DLIGHT_ACTIVE;
91 }
92 }
93
94 static inline struct d3d_light *impl_from_IDirect3DLight(IDirect3DLight *iface)
95 {
96 return CONTAINING_RECORD(iface, struct d3d_light, IDirect3DLight_iface);
97 }
98
99 /*****************************************************************************
100 * IDirect3DLight::QueryInterface
101 *
102 * Queries the object for different interfaces. Unimplemented for this
103 * object at the moment
104 *
105 * Params:
106 * riid: Interface id asked for
107 * obj: Address to return the resulting pointer at.
108 *
109 * Returns:
110 * E_NOINTERFACE, because it's a stub
111 *****************************************************************************/
112 static HRESULT WINAPI d3d_light_QueryInterface(IDirect3DLight *iface, REFIID riid, void **object)
113 {
114 FIXME("iface %p, riid %s, object %p stub!\n", iface, debugstr_guid(riid), object);
115
116 *object = NULL;
117 return E_NOINTERFACE;
118 }
119
120 static ULONG WINAPI d3d_light_AddRef(IDirect3DLight *iface)
121 {
122 struct d3d_light *light = impl_from_IDirect3DLight(iface);
123 ULONG ref = InterlockedIncrement(&light->ref);
124
125 TRACE("%p increasing refcount to %u.\n", light, ref);
126
127 return ref;
128 }
129
130 static ULONG WINAPI d3d_light_Release(IDirect3DLight *iface)
131 {
132 struct d3d_light *light = impl_from_IDirect3DLight(iface);
133 ULONG ref = InterlockedDecrement(&light->ref);
134
135 TRACE("%p decreasing refcount to %u.\n", light, ref);
136
137 if (!ref)
138 {
139 HeapFree(GetProcessHeap(), 0, light);
140 return 0;
141 }
142 return ref;
143 }
144
145 /*****************************************************************************
146 * IDirect3DLight Methods.
147 *****************************************************************************/
148
149 /*****************************************************************************
150 * IDirect3DLight::Initialize
151 *
152 * Initializes the interface. This implementation is a no-op, because
153 * initialization takes place at creation time
154 *
155 * Params:
156 * Direct3D: Pointer to an IDirect3D interface.
157 *
158 * Returns:
159 * D3D_OK
160 *
161 *****************************************************************************/
162 static HRESULT WINAPI d3d_light_Initialize(IDirect3DLight *iface, IDirect3D *d3d)
163 {
164 TRACE("iface %p, d3d %p.\n", iface, d3d);
165
166 return D3D_OK;
167 }
168
169 static const float zero_value[] = {
170 0.0, 0.0, 0.0, 0.0
171 };
172
173 static HRESULT WINAPI d3d_light_SetLight(IDirect3DLight *iface, D3DLIGHT *data)
174 {
175 struct d3d_light *light = impl_from_IDirect3DLight(iface);
176 D3DLIGHT7 *light7 = &light->light7;
177
178 TRACE("iface %p, data %p.\n", iface, data);
179
180 if ((!data->dltType) || (data->dltType > D3DLIGHT_PARALLELPOINT))
181 return DDERR_INVALIDPARAMS;
182
183 if (data->dltType == D3DLIGHT_PARALLELPOINT)
184 FIXME("D3DLIGHT_PARALLELPOINT not implemented.\n");
185
186 /* Translate D3DLIGHT2 structure to D3DLIGHT7. */
187 light7->dltType = data->dltType;
188 light7->dcvDiffuse = data->dcvColor;
189 if (data->dwSize >= sizeof(D3DLIGHT2) && (((D3DLIGHT2 *)data)->dwFlags & D3DLIGHT_NO_SPECULAR))
190 light7->dcvSpecular = data->dcvColor;
191 else
192 light7->dcvSpecular = *(const D3DCOLORVALUE *)zero_value;
193 light7->dcvAmbient = data->dcvColor;
194 light7->dvPosition = data->dvPosition;
195 light7->dvDirection = data->dvDirection;
196 light7->dvRange = data->dvRange;
197 light7->dvFalloff = data->dvFalloff;
198 light7->dvAttenuation0 = data->dvAttenuation0;
199 light7->dvAttenuation1 = data->dvAttenuation1;
200 light7->dvAttenuation2 = data->dvAttenuation2;
201 light7->dvTheta = data->dvTheta;
202 light7->dvPhi = data->dvPhi;
203
204 wined3d_mutex_lock();
205 memcpy(&light->light, data, data->dwSize);
206 if (light->light.dwFlags & D3DLIGHT_ACTIVE)
207 light_update(light);
208 wined3d_mutex_unlock();
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 d3d_light_GetLight(IDirect3DLight *iface, D3DLIGHT *lpLight)
226 {
227 struct d3d_light *light = impl_from_IDirect3DLight(iface);
228
229 TRACE("iface %p, light %p.\n", iface, lpLight);
230
231 wined3d_mutex_lock();
232 memcpy(lpLight, &light->light, lpLight->dwSize);
233 wined3d_mutex_unlock();
234
235 return DD_OK;
236 }
237
238 static const struct IDirect3DLightVtbl d3d_light_vtbl =
239 {
240 /*** IUnknown Methods ***/
241 d3d_light_QueryInterface,
242 d3d_light_AddRef,
243 d3d_light_Release,
244 /*** IDirect3DLight Methods ***/
245 d3d_light_Initialize,
246 d3d_light_SetLight,
247 d3d_light_GetLight
248 };
249
250 void d3d_light_init(struct d3d_light *light, struct ddraw *ddraw)
251 {
252 light->IDirect3DLight_iface.lpVtbl = &d3d_light_vtbl;
253 light->ref = 1;
254 light->ddraw = ddraw;
255 }
256
257 struct d3d_light *unsafe_impl_from_IDirect3DLight(IDirect3DLight *iface)
258 {
259 if (!iface)
260 return NULL;
261 assert(iface->lpVtbl == &d3d_light_vtbl);
262
263 return impl_from_IDirect3DLight(iface);
264 }