4 This files contains the implementation of interface Direct3DTexture2. */
11 #include "wine/obj_base.h"
17 #include "d3d_private.h"
21 /* Define this if you want to save to a file all the textures used by a game
22 (can be funny to see how they managed to cram all the pictures in
26 static ICOM_VTABLE(IDirect3DTexture2
) texture2_vtable
;
27 static ICOM_VTABLE(IDirect3DTexture
) texture_vtable
;
29 /*******************************************************************************
30 * Texture2 Creation functions
32 LPDIRECT3DTEXTURE2
d3dtexture2_create(IDirectDrawSurface4Impl
* surf
)
34 IDirect3DTexture2Impl
* tex
;
36 tex
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDirect3DTexture2Impl
));
38 tex
->lpvtbl
= &texture2_vtable
;
41 return (LPDIRECT3DTEXTURE2
)tex
;
44 /*******************************************************************************
45 * Texture Creation functions
47 LPDIRECT3DTEXTURE
d3dtexture_create(IDirectDrawSurface4Impl
* surf
)
49 IDirect3DTexture2Impl
* tex
;
51 tex
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDirect3DTexture2Impl
));
53 tex
->lpvtbl
= (ICOM_VTABLE(IDirect3DTexture2
)*)&texture_vtable
;
56 return (LPDIRECT3DTEXTURE
)tex
;
60 /*******************************************************************************
61 * IDirect3DTexture2 methods
64 static HRESULT WINAPI
IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface
,
68 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
71 WINE_StringFromCLSID((LPCLSID
)riid
,xrefiid
);
72 FIXME(ddraw
, "(%p)->(%s,%p): stub\n", This
, xrefiid
,ppvObj
);
79 static ULONG WINAPI
IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface
)
81 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
82 TRACE(ddraw
, "(%p)->()incrementing from %lu.\n", This
, This
->ref
);
89 static ULONG WINAPI
IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface
)
91 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
92 FIXME( ddraw
, "(%p)->() decrementing from %lu.\n", This
, This
->ref
);
95 /* Delete texture from OpenGL */
96 glDeleteTextures(1, &(This
->tex_name
));
99 IDirectDrawSurface4_Release((IDirectDrawSurface4
*)This
->surface
);
101 HeapFree(GetProcessHeap(),0,This
);
108 /*** IDirect3DTexture methods ***/
109 static HRESULT WINAPI
IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface
,
110 LPDIRECT3DDEVICE lpD3DDevice
,
111 LPD3DTEXTUREHANDLE lpHandle
)
113 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
114 IDirect3DDeviceImpl
* ilpD3DDevice
=(IDirect3DDeviceImpl
*)lpD3DDevice
;
115 FIXME(ddraw
, "(%p)->(%p,%p): stub\n", This
, ilpD3DDevice
, lpHandle
);
117 *lpHandle
= (D3DTEXTUREHANDLE
) This
;
119 /* Now, bind a new texture */
120 ilpD3DDevice
->set_context(ilpD3DDevice
);
121 This
->D3Ddevice
= (void *) ilpD3DDevice
;
122 if (This
->tex_name
== 0)
123 glGenTextures(1, &(This
->tex_name
));
125 TRACE(ddraw
, "OpenGL texture handle is : %d\n", This
->tex_name
);
130 static HRESULT WINAPI
IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface
,
131 LPDIRECT3DDEVICE lpD3DDevice
,
132 LPDIRECTDRAWSURFACE lpSurface
)
134 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
135 TRACE(ddraw
, "(%p)->(%p,%p)\n", This
, lpD3DDevice
, lpSurface
);
137 return DDERR_ALREADYINITIALIZED
;
140 static HRESULT WINAPI
IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface
)
142 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
143 FIXME(ddraw
, "(%p)->(): stub\n", This
);
148 /*** IDirect3DTexture2 methods ***/
149 static HRESULT WINAPI
IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface
,
150 LPDIRECT3DDEVICE2 lpD3DDevice2
,
151 LPD3DTEXTUREHANDLE lpHandle
)
153 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
154 IDirect3DDevice2Impl
* ilpD3DDevice2
=(IDirect3DDevice2Impl
*)lpD3DDevice2
;
155 TRACE(ddraw
, "(%p)->(%p,%p)\n", This
, ilpD3DDevice2
, lpHandle
);
157 /* For 32 bits OSes, handles = pointers */
158 *lpHandle
= (D3DTEXTUREHANDLE
) This
;
160 /* Now, bind a new texture */
161 ilpD3DDevice2
->set_context(ilpD3DDevice2
);
162 This
->D3Ddevice
= (void *) ilpD3DDevice2
;
163 if (This
->tex_name
== 0)
164 glGenTextures(1, &(This
->tex_name
));
166 TRACE(ddraw
, "OpenGL texture handle is : %d\n", This
->tex_name
);
172 static HRESULT WINAPI
IDirect3DTexture2Impl_PaletteChanged(LPDIRECT3DTEXTURE2 iface
,
176 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
177 FIXME(ddraw
, "(%p)->(%8ld,%8ld): stub\n", This
, dwStart
, dwCount
);
182 /* NOTE : if you experience crashes in this function, you must have a buggy
183 version of Mesa. See the file d3dtexture.c for a cure */
184 static HRESULT WINAPI
IDirect3DTexture2Impl_Load(LPDIRECT3DTEXTURE2 iface
,
185 LPDIRECT3DTEXTURE2 lpD3DTexture2
)
187 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
188 IDirect3DTexture2Impl
* ilpD3DTexture2
=(IDirect3DTexture2Impl
*)lpD3DTexture2
;
189 DDSURFACEDESC
*src_d
, *dst_d
;
190 TRACE(ddraw
, "(%p)->(%p)\n", This
, ilpD3DTexture2
);
192 TRACE(ddraw
, "Copied to surface %p, surface %p\n", This
->surface
, ilpD3DTexture2
->surface
);
194 /* Suppress the ALLOCONLOAD flag */
195 This
->surface
->s
.surface_desc
.ddsCaps
.dwCaps
&= ~DDSCAPS_ALLOCONLOAD
;
197 /* Copy one surface on the other */
198 dst_d
= &(This
->surface
->s
.surface_desc
);
199 src_d
= &(ilpD3DTexture2
->surface
->s
.surface_desc
);
201 if ((src_d
->dwWidth
!= dst_d
->dwWidth
) || (src_d
->dwHeight
!= dst_d
->dwHeight
)) {
202 /* Should also check for same pixel format, lPitch, ... */
203 ERR(ddraw
, "Error in surface sizes\n");
204 return D3DERR_TEXTURE_LOAD_FAILED
;
206 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
207 /* I should put a macro for the calculus of bpp */
208 int bpp
= (src_d
->ddpfPixelFormat
.dwFlags
& DDPF_PALETTEINDEXED8
?
209 1 /* 8 bit of palette index */:
210 src_d
->ddpfPixelFormat
.x
.dwRGBBitCount
/ 8 /* RGB bits for each colors */ );
211 GLuint current_texture
;
213 /* Not sure if this is usefull ! */
214 memcpy(dst_d
->y
.lpSurface
, src_d
->y
.lpSurface
, src_d
->dwWidth
* src_d
->dwHeight
* bpp
);
216 /* Now, load the texture */
217 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
218 glGetIntegerv(GL_TEXTURE_BINDING_2D
, ¤t_texture
);
220 /* If the GetHandle was not done, get the texture name here */
221 if (This
->tex_name
== 0)
222 glGenTextures(1, &(This
->tex_name
));
223 glBindTexture(GL_TEXTURE_2D
, This
->tex_name
);
225 if (src_d
->ddpfPixelFormat
.dwFlags
& DDPF_PALETTEINDEXED8
) {
229 IDirectDrawPaletteImpl
* pal
= This
->surface
->s
.palette
;
234 ERR(ddraw
, "Palettized texture Loading with a NULL palette !\n");
235 return D3DERR_TEXTURE_LOAD_FAILED
;
238 /* Get the surface's palette */
239 for (i
= 0; i
< 256; i
++) {
240 table
[i
][0] = pal
->palents
[i
].peRed
;
241 table
[i
][1] = pal
->palents
[i
].peGreen
;
242 table
[i
][2] = pal
->palents
[i
].peBlue
;
243 if ((This
->surface
->s
.surface_desc
.dwFlags
& DDSD_CKSRCBLT
) &&
244 (i
>= This
->surface
->s
.surface_desc
.ddckCKSrcBlt
.dwColorSpaceLowValue
) &&
245 (i
<= This
->surface
->s
.surface_desc
.ddckCKSrcBlt
.dwColorSpaceHighValue
))
257 sprintf(buf
, "%d.pnm", This
->tex_name
);
258 f
= fopen(buf
, "wb");
259 fprintf(f
, "P6\n%d %d\n255\n", src_d
->dwWidth
, src_d
->dwHeight
);
260 for (y
= 0; y
< src_d
->dwHeight
; y
++) {
261 for (x
= 0; x
< src_d
->dwWidth
; x
++) {
262 unsigned char c
= ((unsigned char *) src_d
->y
.lpSurface
)[y
* src_d
->dwWidth
+ x
];
263 fputc(table
[c
][0], f
);
264 fputc(table
[c
][1], f
);
265 fputc(table
[c
][2], f
);
271 /* Use Paletted Texture Extension */
272 glColorTableEXT(GL_TEXTURE_2D
, /* target */
273 GL_RGBA
, /* internal format */
274 256, /* table size */
275 GL_RGBA
, /* table format */
276 GL_UNSIGNED_BYTE
, /* table type */
277 table
); /* the color table */
279 glTexImage2D(GL_TEXTURE_2D
, /* target */
281 GL_COLOR_INDEX8_EXT
, /* internal format */
282 src_d
->dwWidth
, src_d
->dwHeight
, /* width, height */
284 GL_COLOR_INDEX
, /* texture format */
285 GL_UNSIGNED_BYTE
, /* texture type */
286 src_d
->y
.lpSurface
); /* the texture */
287 } else if (src_d
->ddpfPixelFormat
.dwFlags
& DDPF_RGB
) {
291 if (src_d
->ddpfPixelFormat
.x
.dwRGBBitCount
== 8) {
292 /* **********************
293 GL_UNSIGNED_BYTE_3_3_2
294 ********************** */
295 glTexImage2D(GL_TEXTURE_2D
,
298 src_d
->dwWidth
, src_d
->dwHeight
,
301 GL_UNSIGNED_BYTE_3_3_2
,
303 } else if (src_d
->ddpfPixelFormat
.x
.dwRGBBitCount
== 16) {
304 if (src_d
->ddpfPixelFormat
.xy
.dwRGBAlphaBitMask
== 0x00000000) {
311 sprintf(buf
, "%d.pnm", This
->tex_name
);
312 f
= fopen(buf
, "wb");
313 fprintf(f
, "P6\n%d %d\n255\n", src_d
->dwWidth
, src_d
->dwHeight
);
314 for (y
= 0; y
< src_d
->dwHeight
; y
++) {
315 for (x
= 0; x
< src_d
->dwWidth
; x
++) {
316 unsigned short c
= ((unsigned short *) src_d
->y
.lpSurface
)[y
* src_d
->dwWidth
+ x
];
317 fputc((c
& 0xF800) >> 8, f
);
318 fputc((c
& 0x07E0) >> 3, f
);
319 fputc((c
& 0x001F) << 3, f
);
325 glTexImage2D(GL_TEXTURE_2D
,
328 src_d
->dwWidth
, src_d
->dwHeight
,
331 GL_UNSIGNED_SHORT_5_6_5
,
333 } else if (src_d
->ddpfPixelFormat
.xy
.dwRGBAlphaBitMask
== 0x00000001) {
340 sprintf(buf
, "%d.pnm", This
->tex_name
);
341 f
= fopen(buf
, "wb");
342 fprintf(f
, "P6\n%d %d\n255\n", src_d
->dwWidth
, src_d
->dwHeight
);
343 for (y
= 0; y
< src_d
->dwHeight
; y
++) {
344 for (x
= 0; x
< src_d
->dwWidth
; x
++) {
345 unsigned short c
= ((unsigned short *) src_d
->y
.lpSurface
)[y
* src_d
->dwWidth
+ x
];
346 fputc((c
& 0xF800) >> 8, f
);
347 fputc((c
& 0x07C0) >> 3, f
);
348 fputc((c
& 0x003E) << 2, f
);
355 glTexImage2D(GL_TEXTURE_2D
,
358 src_d
->dwWidth
, src_d
->dwHeight
,
361 GL_UNSIGNED_SHORT_5_5_5_1
,
363 } else if (src_d
->ddpfPixelFormat
.xy
.dwRGBAlphaBitMask
== 0x0000000F) {
364 glTexImage2D(GL_TEXTURE_2D
,
367 src_d
->dwWidth
, src_d
->dwHeight
,
370 GL_UNSIGNED_SHORT_4_4_4_4
,
373 ERR(ddraw
, "Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
375 } else if (src_d
->ddpfPixelFormat
.x
.dwRGBBitCount
== 24) {
376 glTexImage2D(GL_TEXTURE_2D
,
379 src_d
->dwWidth
, src_d
->dwHeight
,
384 } else if (src_d
->ddpfPixelFormat
.x
.dwRGBBitCount
== 32) {
385 glTexImage2D(GL_TEXTURE_2D
,
388 src_d
->dwWidth
, src_d
->dwHeight
,
394 ERR(ddraw
, "Unhandled texture format (bad RGB count)\n");
397 ERR(ddraw
, "Unhandled texture format (neither RGB nor INDEX)\n");
400 glBindTexture(GL_TEXTURE_2D
, current_texture
);
407 /*******************************************************************************
408 * IDirect3DTexture2 VTable
410 static ICOM_VTABLE(IDirect3DTexture2
) texture2_vtable
= {
411 /*** IUnknown methods ***/
412 IDirect3DTexture2Impl_QueryInterface
,
413 IDirect3DTexture2Impl_AddRef
,
414 IDirect3DTexture2Impl_Release
,
415 /*** IDirect3DTexture methods ***/
416 IDirect3DTexture2Impl_GetHandle
,
417 IDirect3DTexture2Impl_PaletteChanged
,
418 IDirect3DTexture2Impl_Load
421 /*******************************************************************************
422 * IDirect3DTexture VTable
424 static ICOM_VTABLE(IDirect3DTexture
) texture_vtable
= {
425 /*** IUnknown methods ***/
426 IDirect3DTexture2Impl_QueryInterface
,
427 IDirect3DTexture2Impl_AddRef
,
428 IDirect3DTexture2Impl_Release
,
429 /*** IDirect3DTexture methods ***/
430 IDirect3DTextureImpl_Initialize
,
431 IDirect3DTextureImpl_GetHandle
,
432 IDirect3DTexture2Impl_PaletteChanged
,
433 IDirect3DTexture2Impl_Load
,
434 IDirect3DTextureImpl_Unload
437 #else /* HAVE_MESAGL */
439 /* These function should never be called if MesaGL is not present */
440 LPDIRECT3DTEXTURE2
d3dtexture2_create(IDirectDrawSurface4Impl
* surf
) {
441 ERR(ddraw
, "Should not be called...\n");
445 LPDIRECT3DTEXTURE
d3dtexture_create(IDirectDrawSurface4Impl
* surf
) {
446 ERR(ddraw
, "Should not be called...\n");
450 #endif /* HAVE_MESAGL */