2 * Copyright (c) 1998 Lionel ULMER
4 * This file contains the implementation of Direct3DViewport2.
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.
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.
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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
37 #include "wine/debug.h"
39 #include "d3d_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
43 static void activate(IDirect3DViewportImpl* This) {
44 IDirect3DLightImpl* light;
47 /* Activate all the lights associated with this context */
50 while (light != NULL) {
51 light->activate(light);
55 /* And copy the values in the structure used by the device */
57 vp.dwX = This->viewports.vp2.dwX;
58 vp.dwY = This->viewports.vp2.dwY;
59 vp.dwHeight = This->viewports.vp2.dwHeight;
60 vp.dwWidth = This->viewports.vp2.dwWidth;
61 vp.dvMinZ = This->viewports.vp2.dvMinZ;
62 vp.dvMaxZ = This->viewports.vp2.dvMaxZ;
64 vp.dwX = This->viewports.vp1.dwX;
65 vp.dwY = This->viewports.vp1.dwY;
66 vp.dwHeight = This->viewports.vp1.dwHeight;
67 vp.dwWidth = This->viewports.vp1.dwWidth;
68 vp.dvMinZ = This->viewports.vp1.dvMinZ;
69 vp.dvMaxZ = This->viewports.vp1.dvMaxZ;
72 /* And also set the viewport */
73 IDirect3DDevice7_SetViewport(ICOM_INTERFACE(This->active_device, IDirect3DDevice7), &vp);
77 static void _dump_D3DVIEWPORT(D3DVIEWPORT *lpvp)
79 TRACE(" - dwSize = %ld dwX = %ld dwY = %ld\n",
80 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
81 TRACE(" - dwWidth = %ld dwHeight = %ld\n",
82 lpvp->dwWidth, lpvp->dwHeight);
83 TRACE(" - dvScaleX = %f dvScaleY = %f\n",
84 lpvp->dvScaleX, lpvp->dvScaleY);
85 TRACE(" - dvMaxX = %f dvMaxY = %f\n",
86 lpvp->dvMaxX, lpvp->dvMaxY);
87 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
88 lpvp->dvMinZ, lpvp->dvMaxZ);
91 static void _dump_D3DVIEWPORT2(D3DVIEWPORT2 *lpvp)
93 TRACE(" - dwSize = %ld dwX = %ld dwY = %ld\n",
94 lpvp->dwSize, lpvp->dwX, lpvp->dwY);
95 TRACE(" - dwWidth = %ld dwHeight = %ld\n",
96 lpvp->dwWidth, lpvp->dwHeight);
97 TRACE(" - dvClipX = %f dvClipY = %f\n",
98 lpvp->dvClipX, lpvp->dvClipY);
99 TRACE(" - dvClipWidth = %f dvClipHeight = %f\n",
100 lpvp->dvClipWidth, lpvp->dvClipHeight);
101 TRACE(" - dvMinZ = %f dvMaxZ = %f\n",
102 lpvp->dvMinZ, lpvp->dvMaxZ);
106 Main_IDirect3DViewportImpl_3_2_1_QueryInterface(LPDIRECT3DVIEWPORT3 iface,
110 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
111 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(riid), obp);
115 if ( IsEqualGUID(&IID_IUnknown, riid) ||
116 IsEqualGUID(&IID_IDirect3DViewport, riid) ||
117 IsEqualGUID(&IID_IDirect3DViewport2, riid) ||
118 IsEqualGUID(&IID_IDirect3DViewport3, riid) ) {
119 IDirect3DViewport3_AddRef(ICOM_INTERFACE(This, IDirect3DViewport3));
120 *obp = ICOM_INTERFACE(This, IDirect3DViewport3);
121 TRACE(" Creating IDirect3DViewport1/2/3 interface %p\n", *obp);
124 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
125 return OLE_E_ENUM_NOMORE;
129 Main_IDirect3DViewportImpl_3_2_1_AddRef(LPDIRECT3DVIEWPORT3 iface)
131 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
132 ULONG ref = InterlockedIncrement(&This->ref);
134 TRACE("(%p/%p)->() incrementing from %lu.\n", This, iface, ref - 1);
140 Main_IDirect3DViewportImpl_3_2_1_Release(LPDIRECT3DVIEWPORT3 iface)
142 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
143 ULONG ref = InterlockedDecrement(&This->ref);
145 TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, ref + 1);
148 HeapFree(GetProcessHeap(), 0, This);
156 Main_IDirect3DViewportImpl_3_2_1_Initialize(LPDIRECT3DVIEWPORT3 iface,
157 LPDIRECT3D lpDirect3D)
159 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
160 TRACE("(%p/%p)->(%p) no-op...\n", This, iface, lpDirect3D);
165 Main_IDirect3DViewportImpl_3_2_1_GetViewport(LPDIRECT3DVIEWPORT3 iface,
166 LPD3DVIEWPORT lpData)
168 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
170 TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
171 if (This->use_vp2 != 0) {
172 ERR(" Requesting to get a D3DVIEWPORT struct where a D3DVIEWPORT2 was set !\n");
173 return DDERR_INVALIDPARAMS;
175 dwSize = lpData->dwSize;
176 memset(lpData, 0, dwSize);
177 memcpy(lpData, &(This->viewports.vp1), dwSize);
179 if (TRACE_ON(ddraw)) {
180 TRACE(" returning D3DVIEWPORT :\n");
181 _dump_D3DVIEWPORT(lpData);
188 Main_IDirect3DViewportImpl_3_2_1_SetViewport(LPDIRECT3DVIEWPORT3 iface,
189 LPD3DVIEWPORT lpData)
191 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
192 LPDIRECT3DVIEWPORT3 current_viewport;
193 TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
195 if (TRACE_ON(ddraw)) {
196 TRACE(" getting D3DVIEWPORT :\n");
197 _dump_D3DVIEWPORT(lpData);
201 memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
202 memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
204 /* Tests on two games show that these values are never used properly so override
205 them with proper ones :-)
207 This->viewports.vp1.dvMinZ = 0.0;
208 This->viewports.vp1.dvMaxZ = 1.0;
210 if (This->active_device) {
211 IDirect3DDevice3_GetCurrentViewport(ICOM_INTERFACE(This->active_device, IDirect3DDevice3), ¤t_viewport);
212 if (ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, current_viewport) == This)
213 This->activate(This);
214 IDirect3DViewport3_Release(current_viewport);
221 Main_IDirect3DViewportImpl_3_2_1_TransformVertices(LPDIRECT3DVIEWPORT3 iface,
223 LPD3DTRANSFORMDATA lpData,
227 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
228 FIXME("(%p/%p)->(%08lx,%p,%08lx,%p): stub!\n", This, iface, dwVertexCount, lpData, dwFlags, lpOffScreen);
235 Main_IDirect3DViewportImpl_3_2_1_LightElements(LPDIRECT3DVIEWPORT3 iface,
236 DWORD dwElementCount,
237 LPD3DLIGHTDATA lpData)
239 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
240 FIXME("(%p/%p)->(%08lx,%p): stub!\n", This, iface, dwElementCount, lpData);
245 Main_IDirect3DViewportImpl_3_2_1_SetBackground(LPDIRECT3DVIEWPORT3 iface,
246 D3DMATERIALHANDLE hMat)
248 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
249 TRACE("(%p/%p)->(%08lx)\n", This, iface, (DWORD) hMat);
251 This->background = (IDirect3DMaterialImpl *) hMat;
252 TRACE(" setting background color : %f %f %f %f\n",
253 This->background->mat.u.diffuse.u1.r,
254 This->background->mat.u.diffuse.u2.g,
255 This->background->mat.u.diffuse.u3.b,
256 This->background->mat.u.diffuse.u4.a);
262 Main_IDirect3DViewportImpl_3_2_1_GetBackground(LPDIRECT3DVIEWPORT3 iface,
263 LPD3DMATERIALHANDLE lphMat,
266 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
267 FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lphMat, lpValid);
272 Main_IDirect3DViewportImpl_3_2_1_SetBackgroundDepth(LPDIRECT3DVIEWPORT3 iface,
273 LPDIRECTDRAWSURFACE lpDDSurface)
275 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
276 FIXME("(%p/%p)->(%p): stub!\n", This, iface, lpDDSurface);
281 Main_IDirect3DViewportImpl_3_2_1_GetBackgroundDepth(LPDIRECT3DVIEWPORT3 iface,
282 LPDIRECTDRAWSURFACE* lplpDDSurface,
285 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
286 FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lplpDDSurface, lpValid);
291 Main_IDirect3DViewportImpl_3_2_1_Clear(LPDIRECT3DVIEWPORT3 iface,
296 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
297 DWORD color = 0x00000000;
299 TRACE("(%p/%p)->(%08lx,%p,%08lx)\n", This, iface, dwCount, lpRects, dwFlags);
300 if (This->active_device == NULL) {
301 ERR(" Trying to clear a viewport not attached to a device !\n");
302 return D3DERR_VIEWPORTHASNODEVICE;
304 if (dwFlags & D3DCLEAR_TARGET) {
305 if (This->background == NULL) {
306 ERR(" Trying to clear the color buffer without background material !\n");
309 ((int) ((This->background->mat.u.diffuse.u1.r) * 255) << 16) |
310 ((int) ((This->background->mat.u.diffuse.u2.g) * 255) << 8) |
311 ((int) ((This->background->mat.u.diffuse.u3.b) * 255) << 0) |
312 ((int) ((This->background->mat.u.diffuse.u4.a) * 255) << 24);
315 return This->active_device->clear(This->active_device, dwCount, lpRects,
316 dwFlags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET),
317 color, 1.0, 0x00000000);
321 Main_IDirect3DViewportImpl_3_2_1_AddLight(LPDIRECT3DVIEWPORT3 iface,
322 LPDIRECT3DLIGHT lpDirect3DLight)
324 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
325 IDirect3DLightImpl *lpDirect3DLightImpl = ICOM_OBJECT(IDirect3DLightImpl, IDirect3DLight, lpDirect3DLight);
327 DWORD map = This->map_lights;
329 TRACE("(%p/%p)->(%p)\n", This, iface, lpDirect3DLight);
331 if (This->num_lights >= 8)
332 return DDERR_INVALIDPARAMS;
334 /* Find a light number and update both light and viewports objects accordingly */
339 lpDirect3DLightImpl->dwLightIndex = i;
341 This->map_lights |= 1<<i;
343 /* Add the light in the 'linked' chain */
344 lpDirect3DLightImpl->next = This->lights;
345 This->lights = lpDirect3DLightImpl;
347 /* Attach the light to the viewport */
348 lpDirect3DLightImpl->active_viewport = This;
350 /* If active, activate the light */
351 if (This->active_device != NULL) {
352 lpDirect3DLightImpl->activate(lpDirect3DLightImpl);
359 Main_IDirect3DViewportImpl_3_2_1_DeleteLight(LPDIRECT3DVIEWPORT3 iface,
360 LPDIRECT3DLIGHT lpDirect3DLight)
362 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
363 IDirect3DLightImpl *lpDirect3DLightImpl = ICOM_OBJECT(IDirect3DLightImpl, IDirect3DLight, lpDirect3DLight);
364 IDirect3DLightImpl *cur_light, *prev_light = NULL;
366 TRACE("(%p/%p)->(%p)\n", This, iface, lpDirect3DLight);
367 cur_light = This->lights;
368 while (cur_light != NULL) {
369 if (cur_light == lpDirect3DLightImpl) {
370 lpDirect3DLightImpl->desactivate(lpDirect3DLightImpl);
371 if (prev_light == NULL) This->lights = cur_light->next;
372 else prev_light->next = cur_light->next;
373 /* Detach the light to the viewport */
374 cur_light->active_viewport = NULL;
376 This->map_lights &= ~(1<<lpDirect3DLightImpl->dwLightIndex);
379 prev_light = cur_light;
380 cur_light = cur_light->next;
382 return DDERR_INVALIDPARAMS;
386 Main_IDirect3DViewportImpl_3_2_1_NextLight(LPDIRECT3DVIEWPORT3 iface,
387 LPDIRECT3DLIGHT lpDirect3DLight,
388 LPDIRECT3DLIGHT* lplpDirect3DLight,
391 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
392 FIXME("(%p/%p)->(%p,%p,%08lx): stub!\n", This, iface, lpDirect3DLight, lplpDirect3DLight, dwFlags);
397 Main_IDirect3DViewportImpl_3_2_GetViewport2(LPDIRECT3DVIEWPORT3 iface,
398 LPD3DVIEWPORT2 lpData)
400 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
402 TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
403 if (This->use_vp2 != 1) {
404 ERR(" Requesting to get a D3DVIEWPORT2 struct where a D3DVIEWPORT was set !\n");
405 return DDERR_INVALIDPARAMS;
407 dwSize = lpData->dwSize;
408 memset(lpData, 0, dwSize);
409 memcpy(lpData, &(This->viewports.vp2), dwSize);
411 if (TRACE_ON(ddraw)) {
412 TRACE(" returning D3DVIEWPORT2 :\n");
413 _dump_D3DVIEWPORT2(lpData);
420 Main_IDirect3DViewportImpl_3_2_SetViewport2(LPDIRECT3DVIEWPORT3 iface,
421 LPD3DVIEWPORT2 lpData)
423 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
424 LPDIRECT3DVIEWPORT3 current_viewport;
425 TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
427 if (TRACE_ON(ddraw)) {
428 TRACE(" getting D3DVIEWPORT2 :\n");
429 _dump_D3DVIEWPORT2(lpData);
433 memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
434 memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
436 if (This->active_device) {
437 IDirect3DDevice3_GetCurrentViewport(ICOM_INTERFACE(This->active_device, IDirect3DDevice3), ¤t_viewport);
438 if (ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, current_viewport) == This)
439 This->activate(This);
440 IDirect3DViewport3_Release(current_viewport);
447 Main_IDirect3DViewportImpl_3_SetBackgroundDepth2(LPDIRECT3DVIEWPORT3 iface,
448 LPDIRECTDRAWSURFACE4 lpDDS)
450 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
451 FIXME("(%p/%p)->(%p): stub!\n", This, iface, lpDDS);
456 Main_IDirect3DViewportImpl_3_GetBackgroundDepth2(LPDIRECT3DVIEWPORT3 iface,
457 LPDIRECTDRAWSURFACE4* lplpDDS,
460 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
461 FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lplpDDS, lpValid);
466 Main_IDirect3DViewportImpl_3_Clear2(LPDIRECT3DVIEWPORT3 iface,
474 ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
475 TRACE("(%p/%p)->(%08lx,%p,%08lx,%08lx,%f,%08lx)\n", This, iface, dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
476 if (This->active_device == NULL) {
477 ERR(" Trying to clear a viewport not attached to a device !\n");
478 return D3DERR_VIEWPORTHASNODEVICE;
480 return This->active_device->clear(This->active_device, dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
483 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
484 # define XCAST(fun) (typeof(VTABLE_IDirect3DViewport3.fun))
486 # define XCAST(fun) (void*)
489 static const IDirect3DViewport3Vtbl VTABLE_IDirect3DViewport3 =
491 XCAST(QueryInterface) Main_IDirect3DViewportImpl_3_2_1_QueryInterface,
492 XCAST(AddRef) Main_IDirect3DViewportImpl_3_2_1_AddRef,
493 XCAST(Release) Main_IDirect3DViewportImpl_3_2_1_Release,
494 XCAST(Initialize) Main_IDirect3DViewportImpl_3_2_1_Initialize,
495 XCAST(GetViewport) Main_IDirect3DViewportImpl_3_2_1_GetViewport,
496 XCAST(SetViewport) Main_IDirect3DViewportImpl_3_2_1_SetViewport,
497 XCAST(TransformVertices) Main_IDirect3DViewportImpl_3_2_1_TransformVertices,
498 XCAST(LightElements) Main_IDirect3DViewportImpl_3_2_1_LightElements,
499 XCAST(SetBackground) Main_IDirect3DViewportImpl_3_2_1_SetBackground,
500 XCAST(GetBackground) Main_IDirect3DViewportImpl_3_2_1_GetBackground,
501 XCAST(SetBackgroundDepth) Main_IDirect3DViewportImpl_3_2_1_SetBackgroundDepth,
502 XCAST(GetBackgroundDepth) Main_IDirect3DViewportImpl_3_2_1_GetBackgroundDepth,
503 XCAST(Clear) Main_IDirect3DViewportImpl_3_2_1_Clear,
504 XCAST(AddLight) Main_IDirect3DViewportImpl_3_2_1_AddLight,
505 XCAST(DeleteLight) Main_IDirect3DViewportImpl_3_2_1_DeleteLight,
506 XCAST(NextLight) Main_IDirect3DViewportImpl_3_2_1_NextLight,
507 XCAST(GetViewport2) Main_IDirect3DViewportImpl_3_2_GetViewport2,
508 XCAST(SetViewport2) Main_IDirect3DViewportImpl_3_2_SetViewport2,
509 XCAST(SetBackgroundDepth2) Main_IDirect3DViewportImpl_3_SetBackgroundDepth2,
510 XCAST(GetBackgroundDepth2) Main_IDirect3DViewportImpl_3_GetBackgroundDepth2,
511 XCAST(Clear2) Main_IDirect3DViewportImpl_3_Clear2,
514 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
521 HRESULT d3dviewport_create(IDirect3DViewportImpl **obj, IDirectDrawImpl *d3d)
523 IDirect3DViewportImpl *object;
525 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DViewportImpl));
526 if (object == NULL) return DDERR_OUTOFMEMORY;
530 object->activate = activate;
531 object->use_vp2 = 0xFF;
533 object->lights = NULL;
534 object->num_lights = 0;
535 object->map_lights = 0;
537 ICOM_INIT_INTERFACE(object, IDirect3DViewport3, VTABLE_IDirect3DViewport3);
541 TRACE(" creating implementation at %p.\n", *obj);