DirectX SDK |
The information in this section pertains only to applications written in C and C++. See Direct3D Immediate Mode Visual Basic Tutorials.
In general, before preparing the actual texture surface, you need to create a handle to the bitmap and load the texture file into it. However, in order to create a handle to the bitmap, you need to ensure that your application can find your bitmap file ("tree1.bmp", "tex1.bmp", and "earth.bmp"). As a first step, check the executable's resource for the bitmap file:
HBITMAP hbm = (HBITMAP)LoadImage( GetModuleHandle(NULL), strName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
In the preceding LoadImage function call, the first argument is a handle to the file used to the create the calling process, in other words, the executable's resource. You want this first parameter to return a handle to an instance of the module that contains the image to be loaded. The second parameter identifies the bitmap image to load. The third, fourth, and fifth parameters specify to load a bitmap type and to use the actual width and height of the bitmap image. The final argument, a load flag, loads a bitmap without mapping it to the colors of the display device.
For more information on LoadImage, see the Platform SDK.
If the Texture application did not find the file by using the handle to the file used to create the calling process, the Texture application tries to load the bitmap image as a file. Note that a real-world application would try to find the bitmap by searching through multiple file paths.
hbm = (HBITMAP)LoadImage( NULL, strName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION );
In the preceding LoadImage function call, the handle of the instance containing the image is not specified, but the LR_LOADFROMFILE load flag is included to indicate that the strName parameter is the name of the file that contains the bitmap image.
The work of preparing and creating the texture is done with the following call to the programmer-defined function CreateTextureFromBitmap. Note that you pass the function a reference to the rendering device and a handle to the bitmap that you want to use as the texture:
return CreateTextureFromBitmap( pd3dDevice, hbm );
Note that CreateTextureFromBitmap retrieves the device capabilities, so that it can check to see if the device has any constraints when using textures:
D3DDEVICEDESC7 ddDesc; ddDesc.dwSize = sizeof(D3DDEVICEDESC7); if( FAILED( pd3dDevice->GetCaps( &ddDesc ) ) ) return NULL;
In the preceding code, a D3DDEVICEDESC7 structure, ddDesc, is initialized. Then, the IDirect3DDevice7::GetCaps method is used to retrieve a description of the rendering device.
Now, retrieve the bitmap that you want to use as a texture by using the GetObject function, so that you can extract the width and height of the bitmap graphics object:
BITMAP bm; GetObject( hbm, sizeof(BITMAP), &bm ); DWORD dwWidth = (DWORD)bm.bmWidth; DWORD dwHeight = (DWORD)bm.bmHeight;
The BITMAP structure is a GDI structure that defines a bitmap, for more information see the Platform SDK.
The GetObject function is a GDI function that obtains information about a specified graphics object, for more information see the Platform SDK.
You can use a DDSURFACEDESC2 structure to describe the texture surface. At this point, you can set up the new surface description for the texture:
DDSURFACEDESC2 ddsd; ZeroMemory( &ddsd, sizeof(DDSURFACEDESC2) ); ddsd.dwSize = sizeof(DDSURFACEDESC2); ddsd.dwFlags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH| DDSD_PIXELFORMAT|DDSD_TEXTURESTAGE; ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE; ddsd.dwWidth = dwWidth; ddsd.dwHeight = dwHeight;
By setting the control flags, specifically DDSCAPS_TEXTURE, you inform the system that the surface describes a texture surface. Now, you are ready to enable automatic texture management. This step is explained in Step 1.3: Enable Texture Management.