Added an unknown VxD error code.
[wine] / dlls / ddraw / d3dviewport.c
1 /* Direct3D Viewport
2    (c) 1998 Lionel ULMER
3    
4    This files contains the implementation of Direct3DViewport2. */
5
6 #include "config.h"
7 #include "windef.h"
8 #include "winerror.h"
9 #include "wine/obj_base.h"
10 #include "heap.h"
11 #include "ddraw.h"
12 #include "d3d.h"
13 #include "debugtools.h"
14
15 #include "d3d_private.h"
16 #include "mesa_private.h"
17
18 DEFAULT_DEBUG_CHANNEL(ddraw);
19
20 #ifdef HAVE_OPENGL
21
22 #define D3DVPRIVATE(x) mesa_d3dv_private*dvpriv=((mesa_d3dv_private*)x->private)
23 #define D3DLPRIVATE(x) mesa_d3dl_private*dlpriv=((mesa_d3dl_private*)x->private)
24
25 static ICOM_VTABLE(IDirect3DViewport2) viewport2_vtable;
26
27 /*******************************************************************************
28  *                              Viewport1/2 static functions
29  */
30 static void activate(IDirect3DViewport2Impl* This) {
31   IDirect3DLightImpl* l;
32   
33   /* Activate all the lights associated with this context */
34   l = This->lights;
35
36   while (l != NULL) {
37     l->activate(l);
38     l = l->next;
39   }
40 }
41
42 /*******************************************************************************
43  *                              Viewport1/2 Creation functions
44  */
45 LPDIRECT3DVIEWPORT2 d3dviewport2_create(IDirect3D2Impl* d3d2)
46 {
47   IDirect3DViewport2Impl* vp;
48   
49   vp = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DViewport2Impl));
50   vp->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(mesa_d3dv_private));
51   vp->ref = 1;
52   ICOM_VTBL(vp) = &viewport2_vtable;
53   vp->d3d.d3d2 = d3d2;
54   vp->use_d3d2 = 1;
55
56   vp->device.active_device2 = NULL;
57   vp->activate = activate;
58
59   vp->lights = NULL;
60
61   ((mesa_d3dv_private *) vp->private)->nextlight = GL_LIGHT0;
62   
63   return (LPDIRECT3DVIEWPORT2)vp;
64 }
65
66 LPDIRECT3DVIEWPORT d3dviewport_create(IDirect3DImpl* d3d1)
67 {
68   IDirect3DViewport2Impl* vp;
69   
70   vp = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DViewport2Impl));
71   vp->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(mesa_d3dv_private));
72   vp->ref = 1;
73   ICOM_VTBL(vp) = &viewport2_vtable;
74   vp->d3d.d3d1 = d3d1;
75   vp->use_d3d2 = 0;
76
77   vp->device.active_device1 = NULL;
78   vp->activate = activate;
79
80   vp->lights = NULL;
81
82   ((mesa_d3dv_private *) vp->private)->nextlight = GL_LIGHT0;
83   
84   return (LPDIRECT3DVIEWPORT) vp;
85 }
86
87 /*******************************************************************************
88  *                              IDirect3DViewport2 methods
89  */
90
91 HRESULT WINAPI IDirect3DViewport2Impl_QueryInterface(LPDIRECT3DVIEWPORT2 iface,
92                                                         REFIID riid,
93                                                         LPVOID* ppvObj)
94 {
95   ICOM_THIS(IDirect3DViewport2Impl,iface);
96   
97   FIXME("(%p)->(%s,%p): stub\n", This, debugstr_guid(riid),ppvObj);
98   
99   return S_OK;
100 }
101
102
103
104 ULONG WINAPI IDirect3DViewport2Impl_AddRef(LPDIRECT3DVIEWPORT2 iface)
105 {
106   ICOM_THIS(IDirect3DViewport2Impl,iface);
107   TRACE("(%p)->()incrementing from %lu.\n", This, This->ref );
108   
109   return ++(This->ref);
110 }
111
112
113
114 ULONG WINAPI IDirect3DViewport2Impl_Release(LPDIRECT3DVIEWPORT2 iface)
115 {
116   ICOM_THIS(IDirect3DViewport2Impl,iface);
117   FIXME("(%p)->() decrementing from %lu.\n", This, This->ref );
118   
119   if (!--(This->ref)) {
120     HeapFree(GetProcessHeap(),0,This);
121     return 0;
122   }
123   
124   return This->ref;
125 }
126
127 /*** IDirect3DViewport methods ***/
128 HRESULT WINAPI IDirect3DViewport2Impl_Initialize(LPDIRECT3DVIEWPORT2 iface,
129                                                     LPDIRECT3D d3d)
130 {
131   ICOM_THIS(IDirect3DViewport2Impl,iface);
132   FIXME("(%p)->(%p): stub\n", This, d3d);
133   
134   return DD_OK;
135 }
136
137 HRESULT WINAPI IDirect3DViewport2Impl_GetViewport(LPDIRECT3DVIEWPORT2 iface,
138                                                      LPD3DVIEWPORT lpvp)
139 {
140   ICOM_THIS(IDirect3DViewport2Impl,iface);
141   FIXME("(%p)->(%p): stub\n", This, lpvp);
142   
143   if (This->use_vp2 != 0)
144     return DDERR_INVALIDPARAMS;
145
146   *lpvp = This->viewport.vp1;
147   
148   return DD_OK;
149 }
150
151 HRESULT WINAPI IDirect3DViewport2Impl_SetViewport(LPDIRECT3DVIEWPORT2 iface,
152                                                      LPD3DVIEWPORT lpvp)
153 {
154   ICOM_THIS(IDirect3DViewport2Impl,iface);
155   FIXME("(%p)->(%p): stub\n", This, lpvp);
156
157   This->use_vp2 = 0;
158   This->viewport.vp1 = *lpvp;
159   
160   TRACE("dwSize = %ld   dwX = %ld   dwY = %ld\n",
161         lpvp->dwSize, lpvp->dwX, lpvp->dwY);
162   TRACE("dwWidth = %ld   dwHeight = %ld\n",
163         lpvp->dwWidth, lpvp->dwHeight);
164   TRACE("dvScaleX = %f   dvScaleY = %f\n",
165         lpvp->dvScaleX, lpvp->dvScaleY);
166   TRACE("dvMaxX = %f   dvMaxY = %f\n",
167         lpvp->dvMaxX, lpvp->dvMaxY);
168   TRACE("dvMinZ = %f   dvMaxZ = %f\n",
169         lpvp->dvMinZ, lpvp->dvMaxZ);
170
171   
172   return DD_OK;
173 }
174
175 HRESULT WINAPI IDirect3DViewport2Impl_TransformVertices(LPDIRECT3DVIEWPORT2 iface,
176                                                            DWORD dwVertexCount,
177                                                            LPD3DTRANSFORMDATA lpData,
178                                                            DWORD dwFlags,
179                                                            LPDWORD lpOffScreen)
180 {
181   ICOM_THIS(IDirect3DViewport2Impl,iface);
182   FIXME("(%p)->(%8ld,%p,%08lx,%p): stub\n",
183         This, dwVertexCount, lpData, dwFlags, lpOffScreen);
184   
185   return DD_OK;
186 }
187
188 HRESULT WINAPI IDirect3DViewport2Impl_LightElements(LPDIRECT3DVIEWPORT2 iface,
189                                                        DWORD dwElementCount,
190                                                        LPD3DLIGHTDATA lpData)
191 {
192   ICOM_THIS(IDirect3DViewport2Impl,iface);
193   FIXME("(%p)->(%8ld,%p): stub\n", This, dwElementCount, lpData);
194   
195   return DD_OK;
196 }
197
198 HRESULT WINAPI IDirect3DViewport2Impl_SetBackground(LPDIRECT3DVIEWPORT2 iface,
199                                                        D3DMATERIALHANDLE hMat)
200 {
201   ICOM_THIS(IDirect3DViewport2Impl,iface);
202   FIXME("(%p)->(%08lx): stub\n", This, (DWORD) hMat);
203   
204   return DD_OK;
205 }
206
207 HRESULT WINAPI IDirect3DViewport2Impl_GetBackground(LPDIRECT3DVIEWPORT2 iface,
208                                                        LPD3DMATERIALHANDLE lphMat,
209                                                        LPBOOL lpValid)
210 {
211   ICOM_THIS(IDirect3DViewport2Impl,iface);
212   FIXME("(%p)->(%p,%p): stub\n", This, lphMat, lpValid);
213   
214   return DD_OK;
215 }
216
217 HRESULT WINAPI IDirect3DViewport2Impl_SetBackgroundDepth(LPDIRECT3DVIEWPORT2 iface,
218                                                             LPDIRECTDRAWSURFACE lpDDSurface)
219 {
220   ICOM_THIS(IDirect3DViewport2Impl,iface);
221   FIXME("(%p)->(%p): stub\n", This, lpDDSurface);
222   
223   return DD_OK;
224 }
225
226 HRESULT WINAPI IDirect3DViewport2Impl_GetBackgroundDepth(LPDIRECT3DVIEWPORT2 iface,
227                                                             LPDIRECTDRAWSURFACE* lplpDDSurface,
228                                                             LPBOOL lpValid)
229 {
230   ICOM_THIS(IDirect3DViewport2Impl,iface);
231   FIXME("(%p)->(%p,%p): stub\n", This, lplpDDSurface, lpValid);
232   
233   return DD_OK;
234 }
235
236 HRESULT WINAPI IDirect3DViewport2Impl_Clear(LPDIRECT3DVIEWPORT2 iface,
237                                                DWORD dwCount,
238                                                LPD3DRECT lpRects,
239                                                DWORD dwFlags)
240 {
241   ICOM_THIS(IDirect3DViewport2Impl,iface);
242   GLboolean ztest;
243   FIXME("(%p)->(%8ld,%p,%08lx): stub\n", This, dwCount, lpRects, dwFlags);
244
245   /* For the moment, ignore the rectangles */
246   if (This->device.active_device1 != NULL) {
247     /* Get the rendering context */
248     if (This->use_d3d2)
249       This->device.active_device2->set_context(This->device.active_device2);
250     else
251       This->device.active_device1->set_context(This->device.active_device1);
252   }
253
254     /* Clears the screen */
255     ENTER_GL();
256     glGetBooleanv(GL_DEPTH_TEST, &ztest);
257     glDepthMask(GL_TRUE); /* Enables Z writing to be sure to delete also the Z buffer */
258     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
259     glDepthMask(ztest);
260     LEAVE_GL();
261   
262   return DD_OK;
263 }
264
265 HRESULT WINAPI IDirect3DViewport2Impl_AddLight(LPDIRECT3DVIEWPORT2 iface,
266                                                   LPDIRECT3DLIGHT lpLight)
267 {
268   ICOM_THIS(IDirect3DViewport2Impl,iface);
269   IDirect3DLightImpl* ilpLight=(IDirect3DLightImpl*)lpLight;
270   FIXME("(%p)->(%p): stub\n", This, ilpLight);
271
272   /* Add the light in the 'linked' chain */
273   ilpLight->next = This->lights;
274   This->lights = ilpLight;
275
276   /* If active, activate the light */
277   if (This->device.active_device1 != NULL) {
278     D3DVPRIVATE(This);
279     D3DLPRIVATE(ilpLight);
280     
281     /* Get the rendering context */
282     if (This->use_d3d2)
283       This->device.active_device2->set_context(This->device.active_device2);
284     else
285       This->device.active_device1->set_context(This->device.active_device1);
286     
287     /* Activate the light */
288     dlpriv->light_num = dvpriv->nextlight++;
289     ilpLight->activate(ilpLight);
290   }
291   
292   return DD_OK;
293 }
294
295 HRESULT WINAPI IDirect3DViewport2Impl_DeleteLight(LPDIRECT3DVIEWPORT2 iface,
296                                                      LPDIRECT3DLIGHT lpLight)
297 {
298   ICOM_THIS(IDirect3DViewport2Impl,iface);
299   FIXME("(%p)->(%p): stub\n", This, lpLight);
300   
301   return DD_OK;
302 }
303
304 HRESULT WINAPI IDirect3DViewport2Impl_NextLight(LPDIRECT3DVIEWPORT2 iface,
305                                                    LPDIRECT3DLIGHT lpLight,
306                                                    LPDIRECT3DLIGHT* lplpLight,
307                                                    DWORD dwFlags)
308 {
309   ICOM_THIS(IDirect3DViewport2Impl,iface);
310   FIXME("(%p)->(%p,%p,%08lx): stub\n", This, lpLight, lplpLight, dwFlags);
311   
312   return DD_OK;
313 }
314
315 /*** IDirect3DViewport2 methods ***/
316 HRESULT WINAPI IDirect3DViewport2Impl_GetViewport2(LPDIRECT3DVIEWPORT2 iface,
317                                                       LPD3DVIEWPORT2 lpViewport2)
318 {
319   ICOM_THIS(IDirect3DViewport2Impl,iface);
320   TRACE("(%p)->(%p)\n", This, lpViewport2);
321
322   if (This->use_vp2 != 1)
323     return DDERR_INVALIDPARAMS;
324
325   *lpViewport2 = This->viewport.vp2;
326   
327   return DD_OK;
328 }
329
330 HRESULT WINAPI IDirect3DViewport2Impl_SetViewport2(LPDIRECT3DVIEWPORT2 iface,
331                                                       LPD3DVIEWPORT2 lpViewport2)
332 {
333   ICOM_THIS(IDirect3DViewport2Impl,iface);
334   TRACE("(%p)->(%p)\n", This, lpViewport2);
335
336   TRACE("dwSize = %ld   dwX = %ld   dwY = %ld\n",
337         lpViewport2->dwSize, lpViewport2->dwX, lpViewport2->dwY);
338   TRACE("dwWidth = %ld   dwHeight = %ld\n",
339         lpViewport2->dwWidth, lpViewport2->dwHeight);
340   TRACE("dvClipX = %f   dvClipY = %f\n",
341         lpViewport2->dvClipX, lpViewport2->dvClipY);
342   TRACE("dvClipWidth = %f   dvClipHeight = %f\n",
343         lpViewport2->dvClipWidth, lpViewport2->dvClipHeight);
344   TRACE("dvMinZ = %f   dvMaxZ = %f\n",
345         lpViewport2->dvMinZ, lpViewport2->dvMaxZ);
346
347   This->viewport.vp2 = *lpViewport2;
348   This->use_vp2 = 1;
349   
350   return DD_OK;
351 }
352
353
354 /*******************************************************************************
355  *                              IDirect3DViewport1/2 VTable
356  */
357 static ICOM_VTABLE(IDirect3DViewport2) viewport2_vtable = 
358 {
359   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
360   /*** IUnknown methods ***/
361   IDirect3DViewport2Impl_QueryInterface,
362   IDirect3DViewport2Impl_AddRef,
363   IDirect3DViewport2Impl_Release,
364   /*** IDirect3DViewport methods ***/
365   IDirect3DViewport2Impl_Initialize,
366   IDirect3DViewport2Impl_GetViewport,
367   IDirect3DViewport2Impl_SetViewport,
368   IDirect3DViewport2Impl_TransformVertices,
369   IDirect3DViewport2Impl_LightElements,
370   IDirect3DViewport2Impl_SetBackground,
371   IDirect3DViewport2Impl_GetBackground,
372   IDirect3DViewport2Impl_SetBackgroundDepth,
373   IDirect3DViewport2Impl_GetBackgroundDepth,
374   IDirect3DViewport2Impl_Clear,
375   IDirect3DViewport2Impl_AddLight,
376   IDirect3DViewport2Impl_DeleteLight,
377   IDirect3DViewport2Impl_NextLight,
378   /*** IDirect3DViewport2 methods ***/
379   IDirect3DViewport2Impl_GetViewport2,
380   IDirect3DViewport2Impl_SetViewport2
381 };
382
383 #else /* HAVE_OPENGL */
384
385 LPDIRECT3DVIEWPORT d3dviewport_create(IDirect3DImpl* d3d1) {
386   ERR("Should not be called...\n");
387   return NULL;
388 }
389
390 LPDIRECT3DVIEWPORT2 d3dviewport2_create(IDirect3D2Impl* d3d2) {
391   ERR("Should not be called...\n");
392   return NULL;
393 }
394
395 #endif /* HAVE_OPENGL */