HOWTO: Animate Textures in Direct3D Immediate Mode

ID: Q153158


The information in this article applies to:


SUMMARY

A texture in Direct3D immediate mode is stored in a DirectDraw surface with the DDSCAPS_TEXTURE flag set in the dwCaps field of the DDSCAPS structure. Since the textures are stored in DirectDraw surfaces, you can alter the bits in these surfaces to perform texture animation in your Direct3D applications. To make changes to the pixels in a texture, you would lock down the DirectDraw surface associated with that texture with Lock() and then you would make the changes necessary. You would then load the revised texture on the video card when 3-D hardware is used with IDirect3DTexture::Load(). The next time the execute buffer is executed with IDirect3DDevice::Execute(), the revised texture will be used when rendering.


MORE INFORMATION

It is important to create a source texture DirectDraw surface in system memory and a destination DirectDraw surface (in video memory, if 3D hardware is present). The source surface should be created with the DDSCAPS_TEXTURE flag set and it should be created in system memory. The destination texture surface should be created with both the DDSCAPS_TEXTURE and DDSCAPS_ALLOCONLOAD flags set. The destination surface will created initially empty. You should call QueryInterface() on both the source and destination textures to get the IDirect3DTexture interface for the texture surfaces. After you have loaded the source texture surface with the bitmap data desired, you should call the destination texture's IDirect3DTexture::Load() method, specifying the source texture, to load the texture onto the destination surface. You can now call the destination texture's IDirect3DTexture::GetHandle() method to obtain a Direct3D texture handle to be used in the execute buffer. When you are done with your texture animation and these textures and surface are no longer required, you should call Release() on the source and destination textures as well as the source and destination surfaces.

The following five steps describe how to implement texture animation in a rendering loop for a Direct3D immediate mode application:

  1. Call the destination texture's Unload() method to unload the current texture.


  2. Lock down the source texture surface with its Lock() method. Place the new texture data in the surface. Unlock the source texture surface with its Unlock() method.


  3. Call the destination texture's Load() method to load the new texture. Make sure to specify the source texture as the texture to be loaded.


  4. Call the destination texture's GetHandle() method to obtain the texture handle for the destination Direct3DTexture object. This handle is used in all Direct3D API calls where a texture is to be referenced.


  5. Execute your current execute buffer. The revised texture will be used for rendering.


Sample Code

Here is a code example to implement the steps above:

   lpTexture->lpVtbl->Unload(lpTexture);

   ddsd.dwSize = sizeof(ddsd);
   if (lpSrcTextureSurf->lpVtbl->Lock(lpSrcTextureSurf, NULL,

                              &ddsd, DDLOCK_WAIT, NULL) == DD_OK)
    {
    lpDst = (BYTE *)ddsd.lpSurface;

       // Modify the surface with the new texture bitmap.

    lpSrcTextureSurf->lpVtbl->Unlock(lpSrcTextureSurf,NULL);
    }

   lpTexture->lpVtbl->Load(lpTexture, lpSrcTexture);
   lpTexture->lpVtbl->GetHandle(lpTexture,

                              lpDev, &TextureHandle);

   if (lpDev->lpVtbl->BeginScene(lpDev) != D3D_OK)

       return FALSE;

   if (lpDev->lpVtbl->Execute(lpDev, lpD3DExBuf,

                              lpView, D3DEXECUTE_UNCLIPPED) != D3D_OK)
       return FALSE;

   if (lpDev->lpVtbl->EndScene(lpDev) != D3D_OK)

       return FALSE; 

Additional query words:


Keywords          : kbWinOS95 kbDirectX200 kbDirect3D 
Version           : WINDOWS:2.0,95
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: June 21, 1999