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