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 IDirect3DTexture2_VTable texture2_vtable;
27 static IDirect3DTexture_VTable texture_vtable;
29 /*******************************************************************************
30 * Texture2 Creation functions
32 LPDIRECT3DTEXTURE2 d3dtexture2_create(LPDIRECTDRAWSURFACE4 surf)
34 LPDIRECT3DTEXTURE2 mat;
36 mat = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2));
38 mat->lpvtbl = &texture2_vtable;
44 /*******************************************************************************
45 * Texture Creation functions
47 LPDIRECT3DTEXTURE d3dtexture_create(LPDIRECTDRAWSURFACE4 surf)
49 LPDIRECT3DTEXTURE mat;
51 mat = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture));
53 mat->lpvtbl = (IDirect3DTexture2_VTable*) &texture_vtable;
60 /*******************************************************************************
61 * IDirect3DTexture2 methods
64 static HRESULT WINAPI IDirect3DTexture2_QueryInterface(LPDIRECT3DTEXTURE2 this,
70 WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
71 FIXME(ddraw, "(%p)->(%s,%p): stub\n", this, xrefiid,ppvObj);
78 static ULONG WINAPI IDirect3DTexture2_AddRef(LPDIRECT3DTEXTURE2 this)
80 TRACE(ddraw, "(%p)->()incrementing from %lu.\n", this, this->ref );
87 static ULONG WINAPI IDirect3DTexture2_Release(LPDIRECT3DTEXTURE2 this)
89 FIXME( ddraw, "(%p)->() decrementing from %lu.\n", this, this->ref );
92 /* Delete texture from OpenGL */
93 glDeleteTextures(1, &(this->tex_name));
96 this->surface->lpvtbl->fnRelease(this->surface);
98 HeapFree(GetProcessHeap(),0,this);
105 /*** IDirect3DTexture methods ***/
106 static HRESULT WINAPI IDirect3DTexture_GetHandle(LPDIRECT3DTEXTURE this,
107 LPDIRECT3DDEVICE lpD3DDevice,
108 LPD3DTEXTUREHANDLE lpHandle)
110 FIXME(ddraw, "(%p)->(%p,%p): stub\n", this, lpD3DDevice, lpHandle);
112 *lpHandle = (DWORD) this;
114 /* Now, bind a new texture */
115 lpD3DDevice->set_context(lpD3DDevice);
116 this->D3Ddevice = (void *) lpD3DDevice;
117 if (this->tex_name == 0)
118 glGenTextures(1, &(this->tex_name));
120 TRACE(ddraw, "OpenGL texture handle is : %d\n", this->tex_name);
125 static HRESULT WINAPI IDirect3DTexture_Initialize(LPDIRECT3DTEXTURE this,
126 LPDIRECT3DDEVICE lpD3DDevice,
127 LPDIRECTDRAWSURFACE lpSurface)
129 TRACE(ddraw, "(%p)->(%p,%p)\n", this, lpD3DDevice, lpSurface);
131 return DDERR_ALREADYINITIALIZED;
134 static HRESULT WINAPI IDirect3DTexture_Unload(LPDIRECT3DTEXTURE this)
136 FIXME(ddraw, "(%p)->(): stub\n", this);
141 /*** IDirect3DTexture2 methods ***/
142 static HRESULT WINAPI IDirect3DTexture2_GetHandle(LPDIRECT3DTEXTURE2 this,
143 LPDIRECT3DDEVICE2 lpD3DDevice2,
144 LPD3DTEXTUREHANDLE lpHandle)
146 TRACE(ddraw, "(%p)->(%p,%p)\n", this, lpD3DDevice2, lpHandle);
148 /* For 32 bits OSes, handles = pointers */
149 *lpHandle = (DWORD) this;
151 /* Now, bind a new texture */
152 lpD3DDevice2->set_context(lpD3DDevice2);
153 this->D3Ddevice = (void *) lpD3DDevice2;
154 if (this->tex_name == 0)
155 glGenTextures(1, &(this->tex_name));
157 TRACE(ddraw, "OpenGL texture handle is : %d\n", this->tex_name);
163 static HRESULT WINAPI IDirect3DTexture2_PaletteChanged(LPDIRECT3DTEXTURE2 this,
167 FIXME(ddraw, "(%p)->(%8ld,%8ld): stub\n", this, dwStart, dwCount);
172 /* NOTE : if you experience crashes in this function, you must have a buggy
173 version of Mesa. See the file d3dtexture.c for a cure */
174 static HRESULT WINAPI IDirect3DTexture2_Load(LPDIRECT3DTEXTURE2 this,
175 LPDIRECT3DTEXTURE2 lpD3DTexture2)
177 DDSURFACEDESC *src_d, *dst_d;
178 TRACE(ddraw, "(%p)->(%p)\n", this, lpD3DTexture2);
180 TRACE(ddraw, "Copied to surface %p, surface %p\n", this->surface, lpD3DTexture2->surface);
182 /* Suppress the ALLOCONLOAD flag */
183 this->surface->s.surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
185 /* Copy one surface on the other */
186 dst_d = &(this->surface->s.surface_desc);
187 src_d = &(lpD3DTexture2->surface->s.surface_desc);
189 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
190 /* Should also check for same pixel format, lPitch, ... */
191 ERR(ddraw, "Error in surface sizes\n");
192 return D3DERR_TEXTURE_LOAD_FAILED;
194 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) this->D3Ddevice; */
195 /* I should put a macro for the calculus of bpp */
196 int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
197 1 /* 8 bit of palette index */:
198 src_d->ddpfPixelFormat.x.dwRGBBitCount / 8 /* RGB bits for each colors */ );
199 GLuint current_texture;
201 /* Not sure if this is usefull ! */
202 memcpy(dst_d->y.lpSurface, src_d->y.lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
204 /* Now, load the texture */
205 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
206 glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
208 /* If the GetHandle was not done, get the texture name here */
209 if (this->tex_name == 0)
210 glGenTextures(1, &(this->tex_name));
211 glBindTexture(GL_TEXTURE_2D, this->tex_name);
213 if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
217 LPDIRECTDRAWPALETTE pal = this->surface->s.palette;
222 ERR(ddraw, "Palettized texture Loading with a NULL palette !\n");
223 return D3DERR_TEXTURE_LOAD_FAILED;
226 /* Get the surface's palette */
227 for (i = 0; i < 256; i++) {
228 table[i][0] = pal->palents[i].peRed;
229 table[i][1] = pal->palents[i].peGreen;
230 table[i][2] = pal->palents[i].peBlue;
231 if ((this->surface->s.surface_desc.dwFlags & DDSD_CKSRCBLT) &&
232 (i >= this->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
233 (i <= this->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
245 sprintf(buf, "%d.pnm", this->tex_name);
246 f = fopen(buf, "wb");
247 fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
248 for (y = 0; y < src_d->dwHeight; y++) {
249 for (x = 0; x < src_d->dwWidth; x++) {
250 unsigned char c = ((unsigned char *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
251 fputc(table[c][0], f);
252 fputc(table[c][1], f);
253 fputc(table[c][2], f);
259 /* Use Paletted Texture Extension */
260 glColorTableEXT(GL_TEXTURE_2D, /* target */
261 GL_RGBA, /* internal format */
262 256, /* table size */
263 GL_RGBA, /* table format */
264 GL_UNSIGNED_BYTE, /* table type */
265 table); /* the color table */
267 glTexImage2D(GL_TEXTURE_2D, /* target */
269 GL_COLOR_INDEX8_EXT, /* internal format */
270 src_d->dwWidth, src_d->dwHeight, /* width, height */
272 GL_COLOR_INDEX, /* texture format */
273 GL_UNSIGNED_BYTE, /* texture type */
274 src_d->y.lpSurface); /* the texture */
275 } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
279 if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 8) {
280 /* **********************
281 GL_UNSIGNED_BYTE_3_3_2
282 ********************** */
283 glTexImage2D(GL_TEXTURE_2D,
286 src_d->dwWidth, src_d->dwHeight,
289 GL_UNSIGNED_BYTE_3_3_2,
291 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 16) {
292 if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000000) {
299 sprintf(buf, "%d.pnm", this->tex_name);
300 f = fopen(buf, "wb");
301 fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
302 for (y = 0; y < src_d->dwHeight; y++) {
303 for (x = 0; x < src_d->dwWidth; x++) {
304 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
305 fputc((c & 0xF800) >> 8, f);
306 fputc((c & 0x07E0) >> 3, f);
307 fputc((c & 0x001F) << 3, f);
313 glTexImage2D(GL_TEXTURE_2D,
316 src_d->dwWidth, src_d->dwHeight,
319 GL_UNSIGNED_SHORT_5_6_5,
321 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000001) {
328 sprintf(buf, "%d.pnm", this->tex_name);
329 f = fopen(buf, "wb");
330 fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
331 for (y = 0; y < src_d->dwHeight; y++) {
332 for (x = 0; x < src_d->dwWidth; x++) {
333 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
334 fputc((c & 0xF800) >> 8, f);
335 fputc((c & 0x07C0) >> 3, f);
336 fputc((c & 0x003E) << 2, f);
343 glTexImage2D(GL_TEXTURE_2D,
346 src_d->dwWidth, src_d->dwHeight,
349 GL_UNSIGNED_SHORT_5_5_5_1,
351 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x0000000F) {
352 glTexImage2D(GL_TEXTURE_2D,
355 src_d->dwWidth, src_d->dwHeight,
358 GL_UNSIGNED_SHORT_4_4_4_4,
361 ERR(ddraw, "Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
363 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 24) {
364 glTexImage2D(GL_TEXTURE_2D,
367 src_d->dwWidth, src_d->dwHeight,
372 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 32) {
373 glTexImage2D(GL_TEXTURE_2D,
376 src_d->dwWidth, src_d->dwHeight,
382 ERR(ddraw, "Unhandled texture format (bad RGB count)\n");
385 ERR(ddraw, "Unhandled texture format (neither RGB nor INDEX)\n");
388 glBindTexture(GL_TEXTURE_2D, current_texture);
395 /*******************************************************************************
396 * IDirect3DTexture2 VTable
398 static IDirect3DTexture2_VTable texture2_vtable = {
399 /*** IUnknown methods ***/
400 IDirect3DTexture2_QueryInterface,
401 IDirect3DTexture2_AddRef,
402 IDirect3DTexture2_Release,
403 /*** IDirect3DTexture methods ***/
404 IDirect3DTexture2_GetHandle,
405 IDirect3DTexture2_PaletteChanged,
406 IDirect3DTexture2_Load
409 /*******************************************************************************
410 * IDirect3DTexture VTable
412 static IDirect3DTexture_VTable texture_vtable = {
413 /*** IUnknown methods ***/
414 IDirect3DTexture2_QueryInterface,
415 IDirect3DTexture2_AddRef,
416 IDirect3DTexture2_Release,
417 /*** IDirect3DTexture methods ***/
418 IDirect3DTexture_Initialize,
419 IDirect3DTexture_GetHandle,
420 IDirect3DTexture2_PaletteChanged,
421 IDirect3DTexture2_Load,
422 IDirect3DTexture_Unload
425 #else /* HAVE_MESAGL */
427 /* These function should never be called if MesaGL is not present */
428 LPDIRECT3DTEXTURE2 d3dtexture2_create(LPDIRECTDRAWSURFACE4 surf) {
429 ERR(ddraw, "Should not be called...\n");
433 LPDIRECT3DTEXTURE d3dtexture_create(LPDIRECTDRAWSURFACE4 surf) {
434 ERR(ddraw, "Should not be called...\n");
438 #endif /* HAVE_MESAGL */