Avoid excessive heap memory reallocation when generating EMF
[wine] / dlls / ddraw / d3dviewport.c
1 /* Direct3D Viewport
2  * Copyright (c) 1998 Lionel ULMER
3  *
4  * This file contains the implementation of Direct3DViewport2.
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 #include "config.h"
22
23 #include <stdarg.h>
24
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "objbase.h"
31 #include "wingdi.h"
32 #include "ddraw.h"
33 #include "d3d.h"
34 #include "wine/debug.h"
35
36 #include "d3d_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39
40 static void activate(IDirect3DViewportImpl* This) {
41     IDirect3DLightImpl* light;
42     D3DVIEWPORT7 vp;
43     
44     /* Activate all the lights associated with this context */
45     light = This->lights;
46
47     while (light != NULL) {
48         light->activate(light);
49         light = light->next;
50     }
51
52     /* And copy the values in the structure used by the device */
53     if (This->use_vp2) {
54         vp.dwX = This->viewports.vp2.dwX;
55         vp.dwY = This->viewports.vp2.dwY;
56         vp.dwHeight = This->viewports.vp2.dwHeight;
57         vp.dwWidth = This->viewports.vp2.dwWidth;
58         vp.dvMinZ = This->viewports.vp2.dvMinZ;
59         vp.dvMaxZ = This->viewports.vp2.dvMaxZ;
60     } else {
61         vp.dwX = This->viewports.vp1.dwX;
62         vp.dwY = This->viewports.vp1.dwY;
63         vp.dwHeight = This->viewports.vp1.dwHeight;
64         vp.dwWidth = This->viewports.vp1.dwWidth;
65         vp.dvMinZ = This->viewports.vp1.dvMinZ;
66         vp.dvMaxZ = This->viewports.vp1.dvMaxZ;
67     }
68     
69     /* And also set the viewport */
70     IDirect3DDevice7_SetViewport(ICOM_INTERFACE(This->active_device, IDirect3DDevice7), &vp);
71 }
72
73
74 static void _dump_D3DVIEWPORT(D3DVIEWPORT *lpvp)
75 {
76     TRACE("    - dwSize = %ld   dwX = %ld   dwY = %ld\n",
77           lpvp->dwSize, lpvp->dwX, lpvp->dwY);
78     TRACE("    - dwWidth = %ld   dwHeight = %ld\n",
79           lpvp->dwWidth, lpvp->dwHeight);
80     TRACE("    - dvScaleX = %f   dvScaleY = %f\n",
81           lpvp->dvScaleX, lpvp->dvScaleY);
82     TRACE("    - dvMaxX = %f   dvMaxY = %f\n",
83           lpvp->dvMaxX, lpvp->dvMaxY);
84     TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
85           lpvp->dvMinZ, lpvp->dvMaxZ);
86 }
87
88 static void _dump_D3DVIEWPORT2(D3DVIEWPORT2 *lpvp)
89 {
90     TRACE("    - dwSize = %ld   dwX = %ld   dwY = %ld\n",
91           lpvp->dwSize, lpvp->dwX, lpvp->dwY);
92     TRACE("    - dwWidth = %ld   dwHeight = %ld\n",
93           lpvp->dwWidth, lpvp->dwHeight);
94     TRACE("    - dvClipX = %f   dvClipY = %f\n",
95           lpvp->dvClipX, lpvp->dvClipY);
96     TRACE("    - dvClipWidth = %f   dvClipHeight = %f\n",
97           lpvp->dvClipWidth, lpvp->dvClipHeight);
98     TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
99           lpvp->dvMinZ, lpvp->dvMaxZ);
100 }
101
102 HRESULT WINAPI
103 Main_IDirect3DViewportImpl_3_2_1_QueryInterface(LPDIRECT3DVIEWPORT3 iface,
104                                                 REFIID riid,
105                                                 LPVOID* obp)
106 {
107     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
108     TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(riid), obp);
109
110     *obp = NULL;
111
112     if ( IsEqualGUID(&IID_IUnknown,  riid) ||
113          IsEqualGUID(&IID_IDirect3DViewport, riid) ||
114          IsEqualGUID(&IID_IDirect3DViewport2, riid) ||
115          IsEqualGUID(&IID_IDirect3DViewport3, riid) ) {
116         IDirect3DViewport3_AddRef(ICOM_INTERFACE(This, IDirect3DViewport3));
117         *obp = ICOM_INTERFACE(This, IDirect3DViewport3);
118         TRACE("  Creating IDirect3DViewport1/2/3 interface %p\n", *obp);
119         return S_OK;
120     }
121     FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
122     return OLE_E_ENUM_NOMORE;
123 }
124
125 ULONG WINAPI
126 Main_IDirect3DViewportImpl_3_2_1_AddRef(LPDIRECT3DVIEWPORT3 iface)
127 {
128     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
129     TRACE("(%p/%p)->() incrementing from %lu.\n", This, iface, This->ref);
130     return ++(This->ref);
131 }
132
133 ULONG WINAPI
134 Main_IDirect3DViewportImpl_3_2_1_Release(LPDIRECT3DVIEWPORT3 iface)
135 {
136     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
137     TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, This->ref);
138     if (!--(This->ref)) {
139         HeapFree(GetProcessHeap(), 0, This);
140         return 0;
141     }
142     return This->ref;
143 }
144
145
146 HRESULT WINAPI
147 Main_IDirect3DViewportImpl_3_2_1_Initialize(LPDIRECT3DVIEWPORT3 iface,
148                                             LPDIRECT3D lpDirect3D)
149 {
150     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
151     TRACE("(%p/%p)->(%p) no-op...\n", This, iface, lpDirect3D);
152     return DD_OK;
153 }
154
155 HRESULT WINAPI
156 Main_IDirect3DViewportImpl_3_2_1_GetViewport(LPDIRECT3DVIEWPORT3 iface,
157                                              LPD3DVIEWPORT lpData)
158 {
159     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
160     DWORD dwSize;
161     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
162     if (This->use_vp2 != 0) {
163         ERR("  Requesting to get a D3DVIEWPORT struct where a D3DVIEWPORT2 was set !\n");
164         return DDERR_INVALIDPARAMS;
165     }
166     dwSize = lpData->dwSize;
167     memset(lpData, 0, dwSize);
168     memcpy(lpData, &(This->viewports.vp1), dwSize);
169
170     if (TRACE_ON(ddraw)) {
171         TRACE("  returning D3DVIEWPORT :\n");
172         _dump_D3DVIEWPORT(lpData);
173     }
174     
175     return DD_OK;
176 }
177
178 HRESULT WINAPI
179 Main_IDirect3DViewportImpl_3_2_1_SetViewport(LPDIRECT3DVIEWPORT3 iface,
180                                              LPD3DVIEWPORT lpData)
181 {
182     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
183     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
184
185     if (TRACE_ON(ddraw)) {
186         TRACE("  getting D3DVIEWPORT :\n");
187         _dump_D3DVIEWPORT(lpData);
188     }
189
190     This->use_vp2 = 0;
191     memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
192     memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
193
194     /* Tests on two games shows that these values are never used properly so overide
195        them with proper ones :-)
196     */
197     This->viewports.vp1.dvMinZ = 0.0;
198     This->viewports.vp1.dvMaxZ = 1.0;
199     
200     return DD_OK;
201 }
202
203 HRESULT WINAPI
204 Main_IDirect3DViewportImpl_3_2_1_TransformVertices(LPDIRECT3DVIEWPORT3 iface,
205                                                    DWORD dwVertexCount,
206                                                    LPD3DTRANSFORMDATA lpData,
207                                                    DWORD dwFlags,
208                                                    LPDWORD lpOffScreen)
209 {
210     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
211     FIXME("(%p/%p)->(%08lx,%p,%08lx,%p): stub!\n", This, iface, dwVertexCount, lpData, dwFlags, lpOffScreen);
212     return DD_OK;
213 }
214
215 HRESULT WINAPI
216 Main_IDirect3DViewportImpl_3_2_1_LightElements(LPDIRECT3DVIEWPORT3 iface,
217                                                DWORD dwElementCount,
218                                                LPD3DLIGHTDATA lpData)
219 {
220     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
221     FIXME("(%p/%p)->(%08lx,%p): stub!\n", This, iface, dwElementCount, lpData);
222     return DD_OK;
223 }
224
225 HRESULT WINAPI
226 Main_IDirect3DViewportImpl_3_2_1_SetBackground(LPDIRECT3DVIEWPORT3 iface,
227                                                D3DMATERIALHANDLE hMat)
228 {
229     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
230     TRACE("(%p/%p)->(%08lx)\n", This, iface, (DWORD) hMat);
231     
232     This->background = (IDirect3DMaterialImpl *) hMat;
233     TRACE(" setting background color : %f %f %f %f\n",
234           This->background->mat.u.diffuse.u1.r,
235           This->background->mat.u.diffuse.u2.g,
236           This->background->mat.u.diffuse.u3.b,
237           This->background->mat.u.diffuse.u4.a);
238
239     return DD_OK;
240 }
241
242 HRESULT WINAPI
243 Main_IDirect3DViewportImpl_3_2_1_GetBackground(LPDIRECT3DVIEWPORT3 iface,
244                                                LPD3DMATERIALHANDLE lphMat,
245                                                LPBOOL lpValid)
246 {
247     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
248     FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lphMat, lpValid);
249     return DD_OK;
250 }
251
252 HRESULT WINAPI
253 Main_IDirect3DViewportImpl_3_2_1_SetBackgroundDepth(LPDIRECT3DVIEWPORT3 iface,
254                                                     LPDIRECTDRAWSURFACE lpDDSurface)
255 {
256     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
257     FIXME("(%p/%p)->(%p): stub!\n", This, iface, lpDDSurface);
258     return DD_OK;
259 }
260
261 HRESULT WINAPI
262 Main_IDirect3DViewportImpl_3_2_1_GetBackgroundDepth(LPDIRECT3DVIEWPORT3 iface,
263                                                     LPDIRECTDRAWSURFACE* lplpDDSurface,
264                                                     LPBOOL lpValid)
265 {
266     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
267     FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lplpDDSurface, lpValid);
268     return DD_OK;
269 }
270
271 HRESULT WINAPI
272 Main_IDirect3DViewportImpl_3_2_1_Clear(LPDIRECT3DVIEWPORT3 iface,
273                                        DWORD dwCount,
274                                        LPD3DRECT lpRects,
275                                        DWORD dwFlags)
276 {
277     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
278     DWORD color = 0x00000000;
279     
280     TRACE("(%p/%p)->(%08lx,%p,%08lx)\n", This, iface, dwCount, lpRects, dwFlags);
281     if (This->active_device == NULL) {
282         ERR(" Trying to clear a viewport not attached to a device !\n");
283         return D3DERR_VIEWPORTHASNODEVICE;
284     }
285     if (dwFlags & D3DCLEAR_TARGET) {
286         if (This->background == NULL) {
287             ERR(" Trying to clear the color buffer without background material !\n");
288         } else {
289             color = 
290               ((int) ((This->background->mat.u.diffuse.u1.r) * 255) << 16) |
291               ((int) ((This->background->mat.u.diffuse.u2.g) * 255) <<  8) |
292               ((int) ((This->background->mat.u.diffuse.u3.b) * 255) <<  0) |
293               ((int) ((This->background->mat.u.diffuse.u4.a) * 255) << 24);
294         }
295     }
296     return This->active_device->clear(This->active_device, dwCount, lpRects, 
297                                       dwFlags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET),
298                                       color, 1.0, 0x00000000);
299 }
300
301 HRESULT WINAPI
302 Main_IDirect3DViewportImpl_3_2_1_AddLight(LPDIRECT3DVIEWPORT3 iface,
303                                           LPDIRECT3DLIGHT lpDirect3DLight)
304 {
305     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
306     IDirect3DLightImpl *lpDirect3DLightImpl = ICOM_OBJECT(IDirect3DLightImpl, IDirect3DLight, lpDirect3DLight);
307     DWORD i = 0;
308     DWORD map = This->map_lights;
309     
310     TRACE("(%p/%p)->(%p)\n", This, iface, lpDirect3DLight);
311     
312     if (This->num_lights >= 8)
313         return DDERR_INVALIDPARAMS;
314
315     /* Find a light number and update both light and viewports objects accordingly */
316     while(map&1) {
317         map>>=1;
318         i++;
319     }
320     lpDirect3DLightImpl->dwLightIndex = i;
321     This->num_lights++;
322     This->map_lights |= 1<<i;
323
324     /* Add the light in the 'linked' chain */
325     lpDirect3DLightImpl->next = This->lights;
326     This->lights = lpDirect3DLightImpl;
327
328     /* Attach the light to the viewport */
329     lpDirect3DLightImpl->active_viewport = This;
330     
331     /* If active, activate the light */
332     if (This->active_device != NULL) {
333         lpDirect3DLightImpl->activate(lpDirect3DLightImpl);
334     }
335     
336     return DD_OK;
337 }
338
339 HRESULT WINAPI
340 Main_IDirect3DViewportImpl_3_2_1_DeleteLight(LPDIRECT3DVIEWPORT3 iface,
341                                              LPDIRECT3DLIGHT lpDirect3DLight)
342 {
343     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
344     IDirect3DLightImpl *lpDirect3DLightImpl = ICOM_OBJECT(IDirect3DLightImpl, IDirect3DLight, lpDirect3DLight);
345     IDirect3DLightImpl *cur_light, *prev_light = NULL;
346     
347     TRACE("(%p/%p)->(%p)\n", This, iface, lpDirect3DLight);
348     cur_light = This->lights;
349     while (cur_light != NULL) {
350         if (cur_light == lpDirect3DLightImpl) {
351             lpDirect3DLightImpl->desactivate(lpDirect3DLightImpl);
352             if (prev_light == NULL) This->lights = cur_light->next;
353             else prev_light->next = cur_light->next;
354             /* Detach the light to the viewport */
355             cur_light->active_viewport = NULL;
356             This->num_lights--;
357             This->map_lights &= ~(1<<lpDirect3DLightImpl->dwLightIndex);
358             return DD_OK;
359         }
360         prev_light = cur_light;
361         cur_light = cur_light->next;
362     }
363     return DDERR_INVALIDPARAMS;
364 }
365
366 HRESULT WINAPI
367 Main_IDirect3DViewportImpl_3_2_1_NextLight(LPDIRECT3DVIEWPORT3 iface,
368                                            LPDIRECT3DLIGHT lpDirect3DLight,
369                                            LPDIRECT3DLIGHT* lplpDirect3DLight,
370                                            DWORD dwFlags)
371 {
372     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
373     FIXME("(%p/%p)->(%p,%p,%08lx): stub!\n", This, iface, lpDirect3DLight, lplpDirect3DLight, dwFlags);
374     return DD_OK;
375 }
376
377 HRESULT WINAPI
378 Main_IDirect3DViewportImpl_3_2_GetViewport2(LPDIRECT3DVIEWPORT3 iface,
379                                             LPD3DVIEWPORT2 lpData)
380 {
381     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
382     DWORD dwSize;
383     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
384     if (This->use_vp2 != 1) {
385         ERR("  Requesting to get a D3DVIEWPORT2 struct where a D3DVIEWPORT was set !\n");
386         return DDERR_INVALIDPARAMS;
387     }
388     dwSize = lpData->dwSize;
389     memset(lpData, 0, dwSize);
390     memcpy(lpData, &(This->viewports.vp2), dwSize);
391
392     if (TRACE_ON(ddraw)) {
393         TRACE("  returning D3DVIEWPORT2 :\n");
394         _dump_D3DVIEWPORT2(lpData);
395     }
396     
397     return DD_OK;
398 }
399
400 HRESULT WINAPI
401 Main_IDirect3DViewportImpl_3_2_SetViewport2(LPDIRECT3DVIEWPORT3 iface,
402                                             LPD3DVIEWPORT2 lpData)
403 {
404     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
405     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
406
407     if (TRACE_ON(ddraw)) {
408         TRACE("  getting D3DVIEWPORT2 :\n");
409         _dump_D3DVIEWPORT2(lpData);
410     }
411
412     This->use_vp2 = 1;
413     memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
414     memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
415     return DD_OK;
416 }
417
418 HRESULT WINAPI
419 Main_IDirect3DViewportImpl_3_SetBackgroundDepth2(LPDIRECT3DVIEWPORT3 iface,
420                                                  LPDIRECTDRAWSURFACE4 lpDDS)
421 {
422     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
423     FIXME("(%p/%p)->(%p): stub!\n", This, iface, lpDDS);
424     return DD_OK;
425 }
426
427 HRESULT WINAPI
428 Main_IDirect3DViewportImpl_3_GetBackgroundDepth2(LPDIRECT3DVIEWPORT3 iface,
429                                                  LPDIRECTDRAWSURFACE4* lplpDDS,
430                                                  LPBOOL lpValid)
431 {
432     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
433     FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lplpDDS, lpValid);
434     return DD_OK;
435 }
436
437 HRESULT WINAPI
438 Main_IDirect3DViewportImpl_3_Clear2(LPDIRECT3DVIEWPORT3 iface,
439                                     DWORD dwCount,
440                                     LPD3DRECT lpRects,
441                                     DWORD dwFlags,
442                                     DWORD dwColor,
443                                     D3DVALUE dvZ,
444                                     DWORD dwStencil)
445 {
446     ICOM_THIS_FROM(IDirect3DViewportImpl, IDirect3DViewport3, iface);
447     TRACE("(%p/%p)->(%08lx,%p,%08lx,%08lx,%f,%08lx)\n", This, iface, dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
448     if (This->active_device == NULL) {
449         ERR(" Trying to clear a viewport not attached to a device !\n");
450         return D3DERR_VIEWPORTHASNODEVICE;
451     }
452     return This->active_device->clear(This->active_device, dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
453 }
454
455 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
456 # define XCAST(fun)     (typeof(VTABLE_IDirect3DViewport3.fun))
457 #else
458 # define XCAST(fun)     (void*)
459 #endif
460
461 ICOM_VTABLE(IDirect3DViewport3) VTABLE_IDirect3DViewport3 =
462 {
463     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
464     XCAST(QueryInterface) Main_IDirect3DViewportImpl_3_2_1_QueryInterface,
465     XCAST(AddRef) Main_IDirect3DViewportImpl_3_2_1_AddRef,
466     XCAST(Release) Main_IDirect3DViewportImpl_3_2_1_Release,
467     XCAST(Initialize) Main_IDirect3DViewportImpl_3_2_1_Initialize,
468     XCAST(GetViewport) Main_IDirect3DViewportImpl_3_2_1_GetViewport,
469     XCAST(SetViewport) Main_IDirect3DViewportImpl_3_2_1_SetViewport,
470     XCAST(TransformVertices) Main_IDirect3DViewportImpl_3_2_1_TransformVertices,
471     XCAST(LightElements) Main_IDirect3DViewportImpl_3_2_1_LightElements,
472     XCAST(SetBackground) Main_IDirect3DViewportImpl_3_2_1_SetBackground,
473     XCAST(GetBackground) Main_IDirect3DViewportImpl_3_2_1_GetBackground,
474     XCAST(SetBackgroundDepth) Main_IDirect3DViewportImpl_3_2_1_SetBackgroundDepth,
475     XCAST(GetBackgroundDepth) Main_IDirect3DViewportImpl_3_2_1_GetBackgroundDepth,
476     XCAST(Clear) Main_IDirect3DViewportImpl_3_2_1_Clear,
477     XCAST(AddLight) Main_IDirect3DViewportImpl_3_2_1_AddLight,
478     XCAST(DeleteLight) Main_IDirect3DViewportImpl_3_2_1_DeleteLight,
479     XCAST(NextLight) Main_IDirect3DViewportImpl_3_2_1_NextLight,
480     XCAST(GetViewport2) Main_IDirect3DViewportImpl_3_2_GetViewport2,
481     XCAST(SetViewport2) Main_IDirect3DViewportImpl_3_2_SetViewport2,
482     XCAST(SetBackgroundDepth2) Main_IDirect3DViewportImpl_3_SetBackgroundDepth2,
483     XCAST(GetBackgroundDepth2) Main_IDirect3DViewportImpl_3_GetBackgroundDepth2,
484     XCAST(Clear2) Main_IDirect3DViewportImpl_3_Clear2,
485 };
486
487 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
488 #undef XCAST
489 #endif
490
491
492
493
494 HRESULT d3dviewport_create(IDirect3DViewportImpl **obj, IDirectDrawImpl *d3d)
495 {
496     IDirect3DViewportImpl *object;
497
498     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DViewportImpl));
499     if (object == NULL) return DDERR_OUTOFMEMORY;
500
501     object->ref = 1;
502     object->d3d = d3d;
503     object->activate = activate;
504     object->use_vp2 = 0xFF;
505     object->next = NULL;
506     object->lights = NULL;
507     object->num_lights = 0;
508     object->map_lights = 0;
509     
510     ICOM_INIT_INTERFACE(object, IDirect3DViewport3, VTABLE_IDirect3DViewport3);
511
512     *obj = object;
513
514     TRACE(" creating implementation at %p.\n", *obj);
515     
516     return D3D_OK;
517 }