HOWTO: Animate Textures in Direct3D Immediate Mode

Last reviewed: September 5, 1997
Article ID: Q153158

The information in this article applies to:
  • Microsoft DirectX 2 Software Development Kit (SDK), for Windows 95

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 3D 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;
Keywords          : Direct3D
Technology        : kbDirectXSDK
Version           : WINDOWS:4.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 5, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.