Changed CHOOSECOLOR[AW].Flags from UINT to DWORD.
[wine/testsucceed.git] / graphics / d3dtexture.c
blob2dfeadf91d3145e86bc24cdbe7cdf9f3f7c42c41
1 /* Direct3D Texture
2 (c) 1998 Lionel ULMER
4 This files contains the implementation of interface Direct3DTexture2. */
7 #include <string.h>
8 #include "config.h"
9 #include "windef.h"
10 #include "winerror.h"
11 #include "wine/obj_base.h"
12 #include "heap.h"
13 #include "ddraw.h"
14 #include "d3d.h"
15 #include "debug.h"
17 #include "d3d_private.h"
19 #ifdef HAVE_MESAGL
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
23 texture memory) */
24 #undef TEXTURE_SNOOP
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));
37 tex->ref = 1;
38 tex->lpvtbl = &texture2_vtable;
39 tex->surface = surf;
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));
52 tex->ref = 1;
53 tex->lpvtbl = (ICOM_VTABLE(IDirect3DTexture2)*)&texture_vtable;
54 tex->surface = surf;
56 return (LPDIRECT3DTEXTURE)tex;
60 /*******************************************************************************
61 * IDirect3DTexture2 methods
64 static HRESULT WINAPI IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface,
65 REFIID riid,
66 LPVOID* ppvObj)
68 ICOM_THIS(IDirect3DTexture2Impl,iface);
69 char xrefiid[50];
71 WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
72 FIXME(ddraw, "(%p)->(%s,%p): stub\n", This, xrefiid,ppvObj);
74 return S_OK;
79 static ULONG WINAPI IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface)
81 ICOM_THIS(IDirect3DTexture2Impl,iface);
82 TRACE(ddraw, "(%p)->()incrementing from %lu.\n", This, This->ref );
84 return ++(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 );
94 if (!--(This->ref)) {
95 /* Delete texture from OpenGL */
96 glDeleteTextures(1, &(This->tex_name));
98 /* Release surface */
99 IDirectDrawSurface4_Release((IDirectDrawSurface4*)This->surface);
101 HeapFree(GetProcessHeap(),0,This);
102 return 0;
105 return This->ref;
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);
127 return D3D_OK;
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);
145 return D3D_OK;
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);
168 return D3D_OK;
171 /* Common methods */
172 static HRESULT WINAPI IDirect3DTexture2Impl_PaletteChanged(LPDIRECT3DTEXTURE2 iface,
173 DWORD dwStart,
174 DWORD dwCount)
176 ICOM_THIS(IDirect3DTexture2Impl,iface);
177 FIXME(ddraw, "(%p)->(%8ld,%8ld): stub\n", This, dwStart, dwCount);
179 return D3D_OK;
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;
205 } else {
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, &current_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) {
226 /* ****************
227 Paletted Texture
228 **************** */
229 IDirectDrawPaletteImpl* pal = This->surface->s.palette;
230 BYTE table[256][4];
231 int i;
233 if (pal == NULL) {
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))
246 table[i][3] = 0x00;
247 else
248 table[i][3] = 0xFF;
251 #ifdef TEXTURE_SNOOP
253 FILE *f;
254 char buf[32];
255 int x, y;
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);
268 fclose(f);
270 #endif
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 */
280 0, /* level */
281 GL_COLOR_INDEX8_EXT, /* internal format */
282 src_d->dwWidth, src_d->dwHeight, /* width, height */
283 0, /* border */
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) {
288 /* ************
289 RGB Textures
290 ************ */
291 if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 8) {
292 /* **********************
293 GL_UNSIGNED_BYTE_3_3_2
294 ********************** */
295 glTexImage2D(GL_TEXTURE_2D,
297 GL_RGB,
298 src_d->dwWidth, src_d->dwHeight,
300 GL_RGB,
301 GL_UNSIGNED_BYTE_3_3_2,
302 src_d->y.lpSurface);
303 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 16) {
304 if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000000) {
305 #ifdef TEXTURE_SNOOP
307 FILE *f;
308 char buf[32];
309 int x, y;
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);
322 fclose(f);
324 #endif
325 glTexImage2D(GL_TEXTURE_2D,
327 GL_RGB,
328 src_d->dwWidth, src_d->dwHeight,
330 GL_RGB,
331 GL_UNSIGNED_SHORT_5_6_5,
332 src_d->y.lpSurface);
333 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000001) {
334 #ifdef TEXTURE_SNOOP
336 FILE *f;
337 char buf[32];
338 int x, y;
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);
351 fclose(f);
353 #endif
355 glTexImage2D(GL_TEXTURE_2D,
357 GL_RGBA,
358 src_d->dwWidth, src_d->dwHeight,
360 GL_RGBA,
361 GL_UNSIGNED_SHORT_5_5_5_1,
362 src_d->y.lpSurface);
363 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x0000000F) {
364 glTexImage2D(GL_TEXTURE_2D,
366 GL_RGBA,
367 src_d->dwWidth, src_d->dwHeight,
369 GL_RGBA,
370 GL_UNSIGNED_SHORT_4_4_4_4,
371 src_d->y.lpSurface);
372 } else {
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,
378 GL_RGB,
379 src_d->dwWidth, src_d->dwHeight,
381 GL_RGB,
382 GL_UNSIGNED_BYTE,
383 src_d->y.lpSurface);
384 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 32) {
385 glTexImage2D(GL_TEXTURE_2D,
387 GL_RGBA,
388 src_d->dwWidth, src_d->dwHeight,
390 GL_RGBA,
391 GL_UNSIGNED_BYTE,
392 src_d->y.lpSurface);
393 } else {
394 ERR(ddraw, "Unhandled texture format (bad RGB count)\n");
396 } else {
397 ERR(ddraw, "Unhandled texture format (neither RGB nor INDEX)\n");
400 glBindTexture(GL_TEXTURE_2D, current_texture);
403 return D3D_OK;
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");
442 return NULL;
445 LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurface4Impl* surf) {
446 ERR(ddraw, "Should not be called...\n");
447 return NULL;
450 #endif /* HAVE_MESAGL */