Bugfix for EnumPrinters level 2.
[wine] / graphics / d3dlight.c
1 /* Direct3D Light
2    (c) 1998 Lionel ULMER
3    
4    This files contains the implementation of Direct3DLight. */
5
6
7 #include "config.h"
8 #include "windef.h"
9 #include "winerror.h"
10 #include "wine/obj_base.h"
11 #include "heap.h"
12 #include "ddraw.h"
13 #include "d3d.h"
14 #include "debug.h"
15
16 #include "d3d_private.h"
17
18 DEFAULT_DEBUG_CHANNEL(ddraw)
19
20 #ifdef HAVE_MESAGL
21
22 static ICOM_VTABLE(IDirect3DLight) light_vtable;
23
24 enum {
25   D3D_1,
26   D3D_2
27 };
28
29 /*******************************************************************************
30  *                              Light static functions
31  */
32 static const float zero_value[] = {
33   0.0, 0.0, 0.0, 0.0
34 };
35
36 static void update(IDirect3DLightImpl* This) {
37   switch (This->light.dltType) {
38   case D3DLIGHT_POINT:         /* 1 */
39     TRACE(ddraw, "Activating POINT\n");
40     break;
41     
42   case D3DLIGHT_SPOT:          /* 2 */
43     TRACE(ddraw, "Activating SPOT\n");
44     break;
45     
46   case D3DLIGHT_DIRECTIONAL: {  /* 3 */
47     float direction[4];
48     
49     TRACE(ddraw, "Activating DIRECTIONAL\n");
50     TRACE(ddraw, "  direction : %f %f %f\n",
51           This->light.dvDirection.x.x,
52           This->light.dvDirection.y.y,
53           This->light.dvDirection.z.z);
54     _dump_colorvalue(" color    ", This->light.dcvColor);
55
56     glLightfv(This->light_num,
57               GL_AMBIENT,
58               (float *) zero_value);
59
60     glLightfv(This->light_num,
61               GL_DIFFUSE,
62               (float *) &(This->light.dcvColor));
63
64     direction[0] = -This->light.dvDirection.x.x;
65     direction[1] = -This->light.dvDirection.y.y;
66     direction[2] = -This->light.dvDirection.z.z;
67     direction[3] = 0.0; /* This is a directional light */
68     glLightfv(This->light_num,
69               GL_POSITION,
70               (float *) direction);
71   } break;
72     
73   case D3DLIGHT_PARALLELPOINT:  /* 4 */
74     TRACE(ddraw, "Activating PARRALLEL-POINT\n");
75     break;
76
77   default:
78     TRACE(ddraw, "Not a know Light Type\n");
79     break;
80   }
81 }
82
83 static void activate(IDirect3DLightImpl* This) {
84   update(This);
85
86   /* If was not active, activate it */
87   if (This->is_active == 0) {
88     glEnable(This->light_num);
89
90     This->is_active = 1;
91   }
92
93   return ;
94 }
95
96 /*******************************************************************************
97  *                              Light Creation functions
98  */
99 LPDIRECT3DLIGHT d3dlight_create(IDirect3D2Impl* d3d2)
100 {
101   IDirect3DLightImpl* light;
102   
103   light = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DLightImpl));
104   light->ref = 1;
105   light->lpvtbl = &light_vtable;
106   light->d3d.d3d2 = d3d2;
107   light->type = D3D_2;
108
109   light->next = NULL;
110   light->prev = NULL;
111   light->activate = activate;
112   light->is_active = 0;
113   
114   return (LPDIRECT3DLIGHT)light;
115 }
116
117 LPDIRECT3DLIGHT d3dlight_create_dx3(IDirect3DImpl* d3d1)
118 {
119   IDirect3DLightImpl* light;
120   
121   light = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DLightImpl));
122   light->ref = 1;
123   light->lpvtbl = &light_vtable;
124   
125   light->d3d.d3d1 = d3d1;
126   light->type = D3D_1;
127   
128   light->next = NULL;
129   light->prev = NULL;
130   light->activate = activate;
131   light->is_active = 0;
132   
133   return (LPDIRECT3DLIGHT)light;
134 }
135
136 /*******************************************************************************
137  *                              IDirect3DLight methods
138  */
139
140 static HRESULT WINAPI IDirect3DLightImpl_QueryInterface(LPDIRECT3DLIGHT iface,
141                                                     REFIID riid,
142                                                     LPVOID* ppvObj)
143 {
144   ICOM_THIS(IDirect3DLightImpl,iface);
145   char xrefiid[50];
146   
147   WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
148   FIXME(ddraw, "(%p)->(%s,%p): stub\n", This, xrefiid,ppvObj);
149   
150   return S_OK;
151 }
152
153
154
155 static ULONG WINAPI IDirect3DLightImpl_AddRef(LPDIRECT3DLIGHT iface)
156 {
157   ICOM_THIS(IDirect3DLightImpl,iface);
158   TRACE(ddraw, "(%p)->()incrementing from %lu.\n", This, This->ref );
159   
160   return ++(This->ref);
161 }
162
163
164
165 static ULONG WINAPI IDirect3DLightImpl_Release(LPDIRECT3DLIGHT iface)
166 {
167   ICOM_THIS(IDirect3DLightImpl,iface);
168   FIXME( ddraw, "(%p)->() decrementing from %lu.\n", This, This->ref );
169   
170   if (!--(This->ref)) {
171     HeapFree(GetProcessHeap(),0,This);
172     return 0;
173   }
174   
175   return This->ref;
176 }
177
178 /*** IDirect3DLight methods ***/
179 static void dump_light(LPD3DLIGHT light)
180 {
181   fprintf(stderr, "  dwSize : %ld\n", light->dwSize);
182 }
183
184 static HRESULT WINAPI IDirect3DLightImpl_GetLight(LPDIRECT3DLIGHT iface,
185                                               LPD3DLIGHT lpLight)
186 {
187   ICOM_THIS(IDirect3DLightImpl,iface);
188   TRACE(ddraw, "(%p)->(%p)\n", This, lpLight);
189   if (TRACE_ON(ddraw))
190     dump_light(lpLight);
191   
192   /* Copies the light structure */
193   switch (This->type) {
194   case D3D_1:
195     *((LPD3DLIGHT)lpLight) = *((LPD3DLIGHT) &(This->light));
196     break;
197   case D3D_2:
198     *((LPD3DLIGHT2)lpLight) = *((LPD3DLIGHT2) &(This->light));
199     break;
200   }
201   
202   return DD_OK;
203 }
204
205 static HRESULT WINAPI IDirect3DLightImpl_SetLight(LPDIRECT3DLIGHT iface,
206                                               LPD3DLIGHT lpLight)
207 {
208   ICOM_THIS(IDirect3DLightImpl,iface);
209   TRACE(ddraw, "(%p)->(%p)\n", This, lpLight);
210   if (TRACE_ON(ddraw))
211     dump_light(lpLight);
212   
213   /* Stores the light */
214   switch (This->type) {
215   case D3D_1:
216     *((LPD3DLIGHT) &(This->light)) = *((LPD3DLIGHT)lpLight);
217     break;
218   case D3D_2:
219     *((LPD3DLIGHT2) &(This->light)) = *((LPD3DLIGHT2)lpLight);
220     break;
221   }
222   
223   if (This->is_active)
224     update(This);
225   
226   return DD_OK;
227 }
228
229 static HRESULT WINAPI IDirect3DLightImpl_Initialize(LPDIRECT3DLIGHT iface,
230                                                 LPDIRECT3D lpDirect3D)
231
232 {
233   ICOM_THIS(IDirect3DLightImpl,iface);
234   TRACE(ddraw, "(%p)->(%p)\n", This, lpDirect3D);
235
236   return DDERR_ALREADYINITIALIZED;
237 }
238
239
240 /*******************************************************************************
241  *                              IDirect3DLight VTable
242  */
243 static ICOM_VTABLE(IDirect3DLight) light_vtable = {
244   /*** IUnknown methods ***/
245   IDirect3DLightImpl_QueryInterface,
246   IDirect3DLightImpl_AddRef,
247   IDirect3DLightImpl_Release,
248   /*** IDirect3DLight methods ***/
249   IDirect3DLightImpl_Initialize,
250   IDirect3DLightImpl_SetLight,
251   IDirect3DLightImpl_GetLight
252 };
253
254 #else /* HAVE_MESAGL */
255
256 /* These function should never be called if MesaGL is not present */
257 LPDIRECT3DLIGHT d3dlight_create_dx3(IDirect3DImpl* d3d1) {
258   ERR(ddraw, "Should not be called...\n");
259   return NULL;
260 }
261
262 LPDIRECT3DLIGHT d3dlight_create(IDirect3D2Impl* d3d2) {
263   ERR(ddraw, "Should not be called...\n");
264   return NULL;
265 }
266
267 #endif /* HAVE_MESAGL */