For all DLLs with defined DllMain and which do not require
[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 static const float zero_value[] = {
83     0.0, 0.0, 0.0, 0.0
84 };
85
86 HRESULT WINAPI
87 Main_IDirect3DLightImpl_1_SetLight(LPDIRECT3DLIGHT iface,
88                                    LPD3DLIGHT lpLight)
89 {
90     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
91     LPD3DLIGHT7 light7 = &(This->light7);
92     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
93     if (TRACE_ON(ddraw)) {
94         TRACE("  Light definition : \n");
95         dump_light((LPD3DLIGHT2) lpLight);
96     }
97
98     if ( (lpLight->dltType == 0) || (lpLight->dltType > D3DLIGHT_PARALLELPOINT) )
99          return DDERR_INVALIDPARAMS;
100     
101     if ( lpLight->dltType == D3DLIGHT_PARALLELPOINT )
102          FIXME("D3DLIGHT_PARALLELPOINT no supported\n");
103     
104     /* Translate D3DLIGH2 structure to D3DLIGHT7 */
105     light7->dltType        = lpLight->dltType;
106     light7->dcvDiffuse     = lpLight->dcvColor;
107     if ((((LPD3DLIGHT2)lpLight)->dwFlags & D3DLIGHT_NO_SPECULAR) != 0)      
108       light7->dcvSpecular    = lpLight->dcvColor;
109     else
110       light7->dcvSpecular    = *(D3DCOLORVALUE*)zero_value;         
111     light7->dcvAmbient     = lpLight->dcvColor;
112     light7->dvPosition     = lpLight->dvPosition;
113     light7->dvDirection    = lpLight->dvDirection;
114     light7->dvRange        = lpLight->dvRange;
115     light7->dvFalloff      = lpLight->dvFalloff;
116     light7->dvAttenuation0 = lpLight->dvAttenuation0;
117     light7->dvAttenuation1 = lpLight->dvAttenuation1;
118     light7->dvAttenuation2 = lpLight->dvAttenuation2;
119     light7->dvTheta        = lpLight->dvTheta;
120     light7->dvPhi          = lpLight->dvPhi;
121
122     memcpy(&This->light, lpLight, lpLight->dwSize);
123     if ((This->light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
124         This->update(This);        
125     }
126     return DD_OK;
127 }
128
129 HRESULT WINAPI
130 Main_IDirect3DLightImpl_1_GetLight(LPDIRECT3DLIGHT iface,
131                                    LPD3DLIGHT lpLight)
132 {
133     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
134     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
135     if (TRACE_ON(ddraw)) {
136         TRACE("  Returning light definition : \n");
137         dump_light(&This->light);
138     }
139     memcpy(lpLight, &This->light, lpLight->dwSize);
140     return DD_OK;
141 }
142
143 /*******************************************************************************
144  *                              Light static functions
145  */
146
147 static void update(IDirect3DLightImpl* This) {
148     IDirect3DDeviceImpl* device;
149     if (!This->active_viewport||!This->active_viewport->active_device)
150         return;
151     device =  This->active_viewport->active_device;
152     IDirect3DDevice7_SetLight(ICOM_INTERFACE(device,IDirect3DDevice7),This->dwLightIndex,&(This->light7));
153 }
154
155 static void activate(IDirect3DLightImpl* This) {
156     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
157
158     TRACE("(%p)\n", This);
159     
160     ENTER_GL();
161     update(This);
162     /* If was not active, activate it */
163     if ((glThis->parent.light.dwFlags & D3DLIGHT_ACTIVE) == 0) {
164         glEnable(glThis->light_num);
165         glThis->parent.light.dwFlags |= D3DLIGHT_ACTIVE;
166     }
167     LEAVE_GL();
168 }
169
170 static void desactivate(IDirect3DLightImpl* This) {
171     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
172     
173     TRACE("(%p)\n", This);
174     
175     ENTER_GL();
176     /* If was not active, activate it */
177     if ((glThis->parent.light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
178         glDisable(glThis->light_num);
179         glThis->parent.light.dwFlags &= ~D3DLIGHT_ACTIVE;
180     }
181     LEAVE_GL();
182 }
183
184 ULONG WINAPI
185 GL_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
186 {
187     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
188     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
189     
190     TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, This->ref);
191     if (!--(This->ref)) {
192         ((IDirect3DGLImpl *) This->d3d)->light_released(This->d3d, glThis->light_num);
193         HeapFree(GetProcessHeap(), 0, This);
194         return 0;
195     }
196     return This->ref;
197 }
198
199 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
200 # define XCAST(fun)     (typeof(VTABLE_IDirect3DLight.fun))
201 #else
202 # define XCAST(fun)     (void*)
203 #endif
204
205 ICOM_VTABLE(IDirect3DLight) VTABLE_IDirect3DLight =
206 {
207     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
208     XCAST(QueryInterface) Main_IDirect3DLightImpl_1_QueryInterface,
209     XCAST(AddRef) Main_IDirect3DLightImpl_1_AddRef,
210     XCAST(Release) GL_IDirect3DLightImpl_1_Release,
211     XCAST(Initialize) Main_IDirect3DLightImpl_1_Initialize,
212     XCAST(SetLight) Main_IDirect3DLightImpl_1_SetLight,
213     XCAST(GetLight) Main_IDirect3DLightImpl_1_GetLight,
214 };
215
216 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
217 #undef XCAST
218 #endif
219
220
221
222
223 HRESULT d3dlight_create(IDirect3DLightImpl **obj, IDirectDrawImpl *d3d, GLenum light_num)
224 {
225     IDirect3DLightImpl *object;
226     IDirect3DLightGLImpl *gl_object;
227     
228     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DLightGLImpl));
229     if (object == NULL) return DDERR_OUTOFMEMORY;
230     gl_object = (IDirect3DLightGLImpl *) object;
231     
232     object->ref = 1;
233     object->d3d = d3d;
234     object->next = NULL;
235     object->activate = activate;
236     object->desactivate = desactivate;
237     object->update = update;
238     object->active_viewport = NULL;
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 }