light.c 8.09 KB
Newer Older
1
/* Direct3D Light
2
 * Copyright (c) 1998 / 2002 Lionel ULMER
3
 * Copyright (c) 2006        Stefan DÖSINGER
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18
 */
19

20
#include "config.h"
21
#include "wine/port.h"
22

23
#include "ddraw_private.h"
24

25
WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
26

27 28 29 30 31 32
/*****************************************************************************
 * light_update
 *
 * Updates the Direct3DDevice7 lighting parameters
 *
 *****************************************************************************/
33
static void light_update(struct d3d_light *light)
34
{
35
    struct d3d_device *device;
36 37 38 39 40 41

    TRACE("light %p.\n", light);

    if (!light->active_viewport || !light->active_viewport->active_device) return;
    device = light->active_viewport->active_device;

42
    IDirect3DDevice7_SetLight(&device->IDirect3DDevice7_iface, light->dwLightIndex, &light->light7);
43 44 45 46 47 48 49 50
}

/*****************************************************************************
 * light_activate
 *
 * Uses the Direct3DDevice7::LightEnable method to active the light
 *
 *****************************************************************************/
51
void light_activate(struct d3d_light *light)
52
{
53
    struct d3d_device *device;
54 55 56 57 58 59 60 61 62

    TRACE("light %p.\n", light);

    if (!light->active_viewport || !light->active_viewport->active_device) return;
    device = light->active_viewport->active_device;

    light_update(light);
    if (!(light->light.dwFlags & D3DLIGHT_ACTIVE))
    {
63
        IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->dwLightIndex, TRUE);
64 65 66 67 68 69 70 71 72 73 74
        light->light.dwFlags |= D3DLIGHT_ACTIVE;
    }
}

/*****************************************************************************
 *
 * light_deactivate
 *
 * Uses the Direct3DDevice7::LightEnable method to deactivate the light
 *
 *****************************************************************************/
75
void light_deactivate(struct d3d_light *light)
76
{
77
    struct d3d_device *device;
78 79 80 81 82 83 84 85

    TRACE("light %p.\n", light);

    if (!light->active_viewport || !light->active_viewport->active_device) return;
    device = light->active_viewport->active_device;

    if (light->light.dwFlags & D3DLIGHT_ACTIVE)
    {
86
        IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->dwLightIndex, FALSE);
87 88 89 90
        light->light.dwFlags &= ~D3DLIGHT_ACTIVE;
    }
}

91
static inline struct d3d_light *impl_from_IDirect3DLight(IDirect3DLight *iface)
92
{
93
    return CONTAINING_RECORD(iface, struct d3d_light, IDirect3DLight_iface);
94
}
95 96 97 98 99 100 101 102 103 104 105 106 107 108

/*****************************************************************************
 * IDirect3DLight::QueryInterface
 *
 * Queries the object for different interfaces. Unimplemented for this
 * object at the moment
 *
 * Params:
 *  riid: Interface id asked for
 *  obj: Address to return the resulting pointer at.
 *
 * Returns:
 *  E_NOINTERFACE, because it's a stub
 *****************************************************************************/
109
static HRESULT WINAPI d3d_light_QueryInterface(IDirect3DLight *iface, REFIID riid, void **object)
110
{
111 112 113
    FIXME("iface %p, riid %s, object %p stub!\n", iface, debugstr_guid(riid), object);

    *object = NULL;
114
    return E_NOINTERFACE;
115 116
}

117
static ULONG WINAPI d3d_light_AddRef(IDirect3DLight *iface)
118
{
119 120
    struct d3d_light *light = impl_from_IDirect3DLight(iface);
    ULONG ref = InterlockedIncrement(&light->ref);
121

122
    TRACE("%p increasing refcount to %u.\n", light, ref);
123 124

    return ref;
125 126
}

127
static ULONG WINAPI d3d_light_Release(IDirect3DLight *iface)
128
{
129 130
    struct d3d_light *light = impl_from_IDirect3DLight(iface);
    ULONG ref = InterlockedDecrement(&light->ref);
131

132
    TRACE("%p decreasing refcount to %u.\n", light, ref);
133

134 135 136
    if (!ref)
    {
        HeapFree(GetProcessHeap(), 0, light);
137
        return 0;
138
    }
139
    return ref;
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/*****************************************************************************
 * IDirect3DLight Methods.
 *****************************************************************************/

/*****************************************************************************
 * IDirect3DLight::Initialize
 *
 * Initializes the interface. This implementation is a no-op, because
 * initialization takes place at creation time
 *
 * Params:
 *  Direct3D: Pointer to an IDirect3D interface.
 *
 * Returns:
 *  D3D_OK
 *
 *****************************************************************************/
159
static HRESULT WINAPI d3d_light_Initialize(IDirect3DLight *iface, IDirect3D *d3d)
160
{
161 162
    TRACE("iface %p, d3d %p.\n", iface, d3d);

163
    return D3D_OK;
164 165
}

166
static HRESULT WINAPI d3d_light_SetLight(IDirect3DLight *iface, D3DLIGHT *data)
167
{
168
    static const D3DCOLORVALUE zero_value = {{0.0f}, {0.0f}, {0.0f}, {0.0f}};
169
    struct d3d_light *light = impl_from_IDirect3DLight(iface);
170
    DWORD flags = data->dwSize >= sizeof(D3DLIGHT2) ? ((D3DLIGHT2 *)data)->dwFlags : D3DLIGHT_ACTIVE;
171
    D3DLIGHT7 *light7 = &light->light7;
172

173
    TRACE("iface %p, data %p.\n", iface, data);
174

175
    if ((!data->dltType) || (data->dltType > D3DLIGHT_PARALLELPOINT))
176
         return DDERR_INVALIDPARAMS;
177

178 179 180
    /* Translate D3DLIGHT2 structure to D3DLIGHT7. */
    light7->dltType = data->dltType;
    light7->dcvDiffuse = data->dcvColor;
181 182
    if (flags & D3DLIGHT_NO_SPECULAR)
        light7->dcvSpecular = zero_value;
183
    else
184
        light7->dcvSpecular = data->dcvColor;
185 186 187 188 189 190 191 192 193 194
    light7->dcvAmbient = data->dcvColor;
    light7->dvPosition = data->dvPosition;
    light7->dvDirection = data->dvDirection;
    light7->dvRange = data->dvRange;
    light7->dvFalloff = data->dvFalloff;
    light7->dvAttenuation0 = data->dvAttenuation0;
    light7->dvAttenuation1 = data->dvAttenuation1;
    light7->dvAttenuation2 = data->dvAttenuation2;
    light7->dvTheta = data->dvTheta;
    light7->dvPhi = data->dvPhi;
195

196
    wined3d_mutex_lock();
197
    memcpy(&light->light, data, sizeof(*data));
198 199 200 201 202
    if (!(light->light.dwFlags & D3DLIGHT_ACTIVE) && flags & D3DLIGHT_ACTIVE)
        light_activate(light);
    else if (light->light.dwFlags & D3DLIGHT_ACTIVE && !(flags & D3DLIGHT_ACTIVE))
        light_deactivate(light);
    else if (flags & D3DLIGHT_ACTIVE)
203
        light_update(light);
204
    light->light.dwFlags = flags;
205 206
    wined3d_mutex_unlock();

207
    return D3D_OK;
208 209
}

210 211 212 213 214 215 216 217 218 219 220 221
/*****************************************************************************
 * IDirect3DLight::GetLight
 *
 * Returns the parameters currently assigned to the IDirect3DLight object
 *
 * Params:
 *  Light: Pointer to an D3DLIGHT structure to store the parameters
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Light is NULL
 *****************************************************************************/
222
static HRESULT WINAPI d3d_light_GetLight(IDirect3DLight *iface, D3DLIGHT *lpLight)
223
{
224
    struct d3d_light *light = impl_from_IDirect3DLight(iface);
225 226 227

    TRACE("iface %p, light %p.\n", iface, lpLight);

228
    wined3d_mutex_lock();
229
    memcpy(lpLight, &light->light, lpLight->dwSize);
230
    wined3d_mutex_unlock();
231

232 233
    return DD_OK;
}
234

235
static const struct IDirect3DLightVtbl d3d_light_vtbl =
236
{
237
    /*** IUnknown Methods ***/
238 239 240
    d3d_light_QueryInterface,
    d3d_light_AddRef,
    d3d_light_Release,
241
    /*** IDirect3DLight Methods ***/
242 243 244
    d3d_light_Initialize,
    d3d_light_SetLight,
    d3d_light_GetLight
245
};
246

247
void d3d_light_init(struct d3d_light *light, struct ddraw *ddraw)
248
{
249
    light->IDirect3DLight_iface.lpVtbl = &d3d_light_vtbl;
250 251 252
    light->ref = 1;
    light->ddraw = ddraw;
}
253

254
struct d3d_light *unsafe_impl_from_IDirect3DLight(IDirect3DLight *iface)
255 256 257 258 259 260 261
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d_light_vtbl);

    return impl_from_IDirect3DLight(iface);
}