- fix small TRACE bug introduced in a previous patch
[wine.git] / dlls / ddraw / d3dlight.c
blob5237322c0963f7176a63d941157f0404ac402c01
1 /* Direct3D Light
2 * Copyright (c) 1998 / 2002 Lionel ULMER
4 * This file contains the implementation of Direct3DLight.
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.
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.
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
21 #include "config.h"
22 #include "windef.h"
23 #include "winerror.h"
24 #include "objbase.h"
25 #include "ddraw.h"
26 #include "d3d.h"
27 #include "wine/debug.h"
29 #include "d3d_private.h"
30 #include "mesa_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
34 /* First, the 'main' interface... */
35 HRESULT WINAPI
36 Main_IDirect3DLightImpl_1_QueryInterface(LPDIRECT3DLIGHT iface,
37 REFIID riid,
38 LPVOID* obp)
40 ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
41 FIXME("(%p/%p)->(%s,%p): stub!\n", This, iface, debugstr_guid(riid), obp);
42 return DD_OK;
45 ULONG WINAPI
46 Main_IDirect3DLightImpl_1_AddRef(LPDIRECT3DLIGHT iface)
48 ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
49 TRACE("(%p/%p)->() incrementing from %lu.\n", This, iface, This->ref);
50 return ++(This->ref);
53 ULONG WINAPI
54 Main_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
56 ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
57 TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, This->ref);
58 if (!--(This->ref)) {
59 HeapFree(GetProcessHeap(), 0, This);
60 return 0;
62 return This->ref;
65 HRESULT WINAPI
66 Main_IDirect3DLightImpl_1_Initialize(LPDIRECT3DLIGHT iface,
67 LPDIRECT3D lpDirect3D)
69 ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
70 TRACE("(%p/%p)->(%p) no-op...\n", This, iface, lpDirect3D);
71 return DD_OK;
74 /*** IDirect3DLight methods ***/
75 static void dump_light(LPD3DLIGHT2 light)
77 DPRINTF(" - dwSize : %ld\n", light->dwSize);
80 HRESULT WINAPI
81 Main_IDirect3DLightImpl_1_SetLight(LPDIRECT3DLIGHT iface,
82 LPD3DLIGHT lpLight)
84 ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
85 TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
86 if (TRACE_ON(ddraw)) {
87 TRACE(" Light definition : \n");
88 dump_light((LPD3DLIGHT2) lpLight);
90 memcpy(&This->light, lpLight, lpLight->dwSize);
91 if ((This->light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
92 This->update(This);
94 return DD_OK;
97 HRESULT WINAPI
98 Main_IDirect3DLightImpl_1_GetLight(LPDIRECT3DLIGHT iface,
99 LPD3DLIGHT lpLight)
101 ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
102 TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
103 if (TRACE_ON(ddraw)) {
104 TRACE(" Returning light definition : \n");
105 dump_light(&This->light);
107 memcpy(lpLight, &This->light, lpLight->dwSize);
108 return DD_OK;
111 /*******************************************************************************
112 * Light static functions
114 static const float zero_value[] = {
115 0.0, 0.0, 0.0, 0.0
118 static void update(IDirect3DLightImpl* This) {
119 IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
120 ENTER_GL();
121 switch (glThis->parent.light.dltType) {
122 case D3DLIGHT_POINT: /* 1 */
123 TRACE("Activating POINT\n");
124 break;
126 case D3DLIGHT_SPOT: /* 2 */
127 TRACE("Activating SPOT\n");
128 break;
130 case D3DLIGHT_DIRECTIONAL: { /* 3 */
131 float direction[4];
133 TRACE("Activating DIRECTIONAL\n");
134 TRACE(" direction : %f %f %f\n",
135 glThis->parent.light.dvDirection.u1.x,
136 glThis->parent.light.dvDirection.u2.y,
137 glThis->parent.light.dvDirection.u3.z);
138 _dump_colorvalue(" color ", glThis->parent.light.dcvColor);
140 glLightfv(glThis->light_num, GL_AMBIENT, (float *) zero_value);
141 glLightfv(glThis->light_num, GL_DIFFUSE, (float *) &(glThis->parent.light.dcvColor));
143 direction[0] = -glThis->parent.light.dvDirection.u1.x;
144 direction[1] = -glThis->parent.light.dvDirection.u2.y;
145 direction[2] = -glThis->parent.light.dvDirection.u3.z;
146 direction[3] = 0.0; /* This is a directional light */
148 glLightfv(glThis->light_num, GL_POSITION, (float *) direction);
149 } break;
151 case D3DLIGHT_PARALLELPOINT: /* 4 */
152 TRACE("Activating PARRALLEL-POINT\n");
153 break;
155 default:
156 WARN("Not a known Light Type: %d\n", glThis->parent.light.dltType);
157 break;
159 LEAVE_GL();
162 static void activate(IDirect3DLightImpl* This) {
163 IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
164 ENTER_GL();
165 update(This);
166 /* If was not active, activate it */
167 if ((glThis->parent.light.dwFlags & D3DLIGHT_ACTIVE) == 0) {
168 glEnable(glThis->light_num);
169 glThis->parent.light.dwFlags |= D3DLIGHT_ACTIVE;
171 LEAVE_GL();
174 static void desactivate(IDirect3DLightImpl* This) {
175 IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
176 ENTER_GL();
177 /* If was not active, activate it */
178 if ((glThis->parent.light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
179 glDisable(glThis->light_num);
180 glThis->parent.light.dwFlags &= ~D3DLIGHT_ACTIVE;
182 LEAVE_GL();
185 ULONG WINAPI
186 GL_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
188 ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
189 IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
191 TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, This->ref);
192 if (!--(This->ref)) {
193 ((IDirect3DGLImpl *) This->d3d)->light_released(This->d3d, glThis->light_num);
194 HeapFree(GetProcessHeap(), 0, This);
195 return 0;
197 return This->ref;
200 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
201 # define XCAST(fun) (typeof(VTABLE_IDirect3DLight.fun))
202 #else
203 # define XCAST(fun) (void*)
204 #endif
206 ICOM_VTABLE(IDirect3DLight) VTABLE_IDirect3DLight =
208 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
209 XCAST(QueryInterface) Main_IDirect3DLightImpl_1_QueryInterface,
210 XCAST(AddRef) Main_IDirect3DLightImpl_1_AddRef,
211 XCAST(Release) GL_IDirect3DLightImpl_1_Release,
212 XCAST(Initialize) Main_IDirect3DLightImpl_1_Initialize,
213 XCAST(SetLight) Main_IDirect3DLightImpl_1_SetLight,
214 XCAST(GetLight) Main_IDirect3DLightImpl_1_GetLight,
217 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
218 #undef XCAST
219 #endif
224 HRESULT d3dlight_create(IDirect3DLightImpl **obj, IDirect3DImpl *d3d, GLenum light_num)
226 IDirect3DLightImpl *object;
227 IDirect3DLightGLImpl *gl_object;
229 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DLightGLImpl));
230 if (object == NULL) return DDERR_OUTOFMEMORY;
231 gl_object = (IDirect3DLightGLImpl *) object;
233 object->ref = 1;
234 object->d3d = d3d;
235 object->next = NULL;
236 object->activate = activate;
237 object->desactivate = desactivate;
238 object->update = update;
239 gl_object->light_num = light_num;
241 ICOM_INIT_INTERFACE(object, IDirect3DLight, VTABLE_IDirect3DLight);
243 *obj = object;
245 TRACE(" creating implementation at %p.\n", *obj);
247 return D3D_OK;