ok() does not support '%S'. Store the Ansi version, convert to Unicode
[wine] / dlls / ddraw / d3dlight.c
1 /* Direct3D Light
2  * Copyright (c) 1998 / 2002 Lionel ULMER
3  *
4  * This file contains the implementation of Direct3DLight.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 #include "config.h"
24 #include "windef.h"
25 #include "winerror.h"
26 #include "objbase.h"
27 #include "ddraw.h"
28 #include "d3d.h"
29 #include "wine/debug.h"
30
31 #include "d3d_private.h"
32 #include "mesa_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
35
36 /* First, the 'main' interface... */
37 HRESULT WINAPI
38 Main_IDirect3DLightImpl_1_QueryInterface(LPDIRECT3DLIGHT iface,
39                                          REFIID riid,
40                                          LPVOID* obp)
41 {
42     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
43     FIXME("(%p/%p)->(%s,%p): stub!\n", This, iface, debugstr_guid(riid), obp);
44     return DD_OK;
45 }
46
47 ULONG WINAPI
48 Main_IDirect3DLightImpl_1_AddRef(LPDIRECT3DLIGHT iface)
49 {
50     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
51     TRACE("(%p/%p)->() incrementing from %lu.\n", This, iface, This->ref);
52     return ++(This->ref);
53 }
54
55 ULONG WINAPI
56 Main_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
57 {
58     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
59     TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, This->ref);
60     if (!--(This->ref)) {
61         HeapFree(GetProcessHeap(), 0, This);
62         return 0;
63     }
64     return This->ref;
65 }
66
67 HRESULT WINAPI
68 Main_IDirect3DLightImpl_1_Initialize(LPDIRECT3DLIGHT iface,
69                                      LPDIRECT3D lpDirect3D)
70 {
71     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
72     TRACE("(%p/%p)->(%p) no-op...\n", This, iface, lpDirect3D);
73     return DD_OK;
74 }
75
76 /*** IDirect3DLight methods ***/
77 static void dump_light(LPD3DLIGHT2 light)
78 {
79     DPRINTF("    - dwSize : %ld\n", light->dwSize);
80 }
81
82 HRESULT WINAPI
83 Main_IDirect3DLightImpl_1_SetLight(LPDIRECT3DLIGHT iface,
84                                    LPD3DLIGHT lpLight)
85 {
86     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
87     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
88     if (TRACE_ON(ddraw)) {
89         TRACE("  Light definition : \n");
90         dump_light((LPD3DLIGHT2) lpLight);
91     }
92     memcpy(&This->light, lpLight, lpLight->dwSize);
93     if ((This->light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
94         This->update(This);        
95     }
96     return DD_OK;
97 }
98
99 HRESULT WINAPI
100 Main_IDirect3DLightImpl_1_GetLight(LPDIRECT3DLIGHT iface,
101                                    LPD3DLIGHT lpLight)
102 {
103     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
104     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
105     if (TRACE_ON(ddraw)) {
106         TRACE("  Returning light definition : \n");
107         dump_light(&This->light);
108     }
109     memcpy(lpLight, &This->light, lpLight->dwSize);
110     return DD_OK;
111 }
112
113 /*******************************************************************************
114  *                              Light static functions
115  */
116 static const float zero_value[] = {
117     0.0, 0.0, 0.0, 0.0
118 };
119
120 static void update(IDirect3DLightImpl* This) {
121     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
122     ENTER_GL();
123     switch (glThis->parent.light.dltType) {
124         case D3DLIGHT_POINT:         /* 1 */
125             FIXME("Activating POINT - not supported yet\n");
126             break;
127
128         case D3DLIGHT_SPOT:          /* 2 */
129             FIXME("Activating SPOT - not supported yet\n");
130             break;
131
132         case D3DLIGHT_DIRECTIONAL: {  /* 3 */
133             float direction[4];
134
135             if (TRACE_ON(ddraw)) {
136                 TRACE("Activating DIRECTIONAL\n");
137                 DPRINTF(" - direction     : "); dump_D3DVECTOR(&(glThis->parent.light.dvDirection)); DPRINTF("\n");
138                 DPRINTF(" - color         : "); dump_D3DCOLORVALUE(&(glThis->parent.light.dcvColor)); DPRINTF("\n");
139             }
140             glLightfv(glThis->light_num, GL_AMBIENT, (float *) zero_value);
141             glLightfv(glThis->light_num, GL_DIFFUSE, (float *) &(glThis->parent.light.dcvColor));
142
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 */
147
148             glLightfv(glThis->light_num, GL_POSITION, (float *) direction);
149         } break;
150
151         case D3DLIGHT_PARALLELPOINT:  /* 4 */
152             FIXME("Activating PARRALLEL-POINT - not supported yet\n");
153             break;
154
155         default:
156             WARN("Not a known Light Type: %d\n", glThis->parent.light.dltType);
157             break;
158     }
159     LEAVE_GL();
160 }
161
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;
170     }
171     LEAVE_GL();
172 }
173
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;
181     }
182     LEAVE_GL();
183 }
184
185 ULONG WINAPI
186 GL_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
187 {
188     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
189     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
190     
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;
196     }
197     return This->ref;
198 }
199
200 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
201 # define XCAST(fun)     (typeof(VTABLE_IDirect3DLight.fun))
202 #else
203 # define XCAST(fun)     (void*)
204 #endif
205
206 ICOM_VTABLE(IDirect3DLight) VTABLE_IDirect3DLight =
207 {
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,
215 };
216
217 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
218 #undef XCAST
219 #endif
220
221
222
223
224 HRESULT d3dlight_create(IDirect3DLightImpl **obj, IDirect3DImpl *d3d, GLenum light_num)
225 {
226     IDirect3DLightImpl *object;
227     IDirect3DLightGLImpl *gl_object;
228     
229     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DLightGLImpl));
230     if (object == NULL) return DDERR_OUTOFMEMORY;
231     gl_object = (IDirect3DLightGLImpl *) object;
232     
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;
240     
241     ICOM_INIT_INTERFACE(object, IDirect3DLight, VTABLE_IDirect3DLight);
242
243     *obj = object;
244
245     TRACE(" creating implementation at %p.\n", *obj);
246     
247     return D3D_OK;
248 }