DirectX SDK |
The information in this section pertains only to applications written in C and C++. See Direct3D Immediate Mode Visual Basic Tutorials.
Direct3D represents textures as two-dimensional arrays of color values. Each element in the array is referred to as a texel. You can identify a texel through column and row numbers, which are labeled U and V, respectively.
Your application can assign texture coordinates directly to vertices by initializing a D3DVERTEX structure with values describing the texture coordinates of the vertex. The following code fragment, taken from the Texture application, creates a set of four vertices describing the front face of a cube:
*pVertices++ = D3DVERTEX( D3DVECTOR(-1.0f, 1.0f,-1.0f), n0, 0.0f, 0.0f ); *pVertices++ = D3DVERTEX( D3DVECTOR( 1.0f, 1.0f,-1.0f), n0, 1.0f, 0.0f ); *pVertices++ = D3DVERTEX( D3DVECTOR(-1.0f,-1.0f,-1.0f), n0, 0.0f, 1.0f ); *pVertices++ = D3DVERTEX( D3DVECTOR( 1.0f,-1.0f,-1.0f), n0, 1.0f, 1.0f );
For each D3DVERTEX assignment, the first argument defines a vertex, the second argument defines the orientation of the vertex normal vector (for the definition of the normal, see the Texture application code in the SDK), and the next two arguments represent the texture coordinates of the vertex. In this case, you want the application to map the entire bitmap texture onto the front face of a cube, so you will use the following texture coordinates: (0, 0), (1, 0), (0, 1), and (1, 1).
For more information on texture coordinates, see Texture Coordinates.
You should employ the same procedures defined previously for the back, top, bottom, right, and left faces of the cube. See the Texture application code in the SDK for information on how to do this.
The Texture application implements the programmer-defined function CreateTexture to return an IDirectDrawSurface7 object as a texture surface object:
g_pddsTexture1 = CreateTexture( pd3dDevice, "tree1.bmp" ); g_pddsTexture2 = CreateTexture( pd3dDevice, "tex1.bmp" ); g_pddsTexture3 = CreateTexture( pd3dDevice, "earth.bmp" );
In the preceding code, three texture surface objects are created from the following file-based bitmaps: "tree1.bmp, " "tex1.bmp," and "earth.bmp." In each function call, the first argument passed, pd3dDevice, is a reference to the rendering device for the application. The returned texture surface objects (g_pddsTexture1 , g_pddsTexture2 , and g_pddsTexture3) will be set to the cube primitive during rendering.
Now, you can look at the programmer-defined function CreateTexture in detail, to see how texture surface objects are prepared and created. The initial step of this process is illustrated in Step 1.2: Prepare the Texture Surface.