How do I apply a texture?

3D objects look much more convincing when texture-mapped. Textures are typically loaded from image files (BMP, JPEG, TGA, PPM, DDS etc).

Like a vertex buffer, textures have Lock() and Unlock() functions to access (read or write) the image data. Textures have a width, height, miplevel, and pixel format. The miplevel is for "mipmapped" textures, an advanced performance-enhancing feature which uses lower resolutions of the texture for objects in the distance where detail is less noticeable. The pixel format determines how the colors are stored in a texel. You can use D3DFMT_R5G6B5 (5 Bits for Red, 6 bits for Green, 5 bits for Blue)

Textures are associated with geometry through texture coordinates. Each vertex has one or more sets of texture coordinates, which are named tu and tv and range from 0.0 to 1.0. Texture coordinates can be supplied by the geometry, or can be automatically generated using the Direct3D texture coordinate generation!

To set a Texture into a geometry the vertices must have a D3DCOLOR element and tu, tv coordinates other than x,y,z elements. The following is a sample struct used to store a vertice.

struct CUSTOMVERTEX
{
    D3DXVECTOR3 position; // The 3D position for the vertex
    D3DCOLOR    color;    // The color
    FLOAT       tu, tv;   // The texture coordinates
};


Also we must specify a custom FVF(Flexible Vertex Format), which describes our custom vertex structure. The following Macro does this!

 
  1. define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)


You have to load Data into the Vertex Buffer between Lock () and UnLock () calls. As usual Load the Texture into a LPDIRECT3DTEXTURE9 structure using D3DXCreateTextureFromFile call. During Rendering between BeginScene-EndScene Pair you have to set the textures through SetTexture function call before DrawPrimitive Call. The sample Application "TextureMap.zip" shows how to texture map a cone.

FAQ Menu

 
Printer friendly version of the FAQ-DIRECTX-Apply-A-Texture page


Sponsored links

SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
3 Months Free - ASP.NET Web Hosting
3 Months Free & No Setup Fees on ASP.NET 3.5/2.0 Hosting on Windows 2008/2003 Servers ? Click Here!
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!
Virtual File System SDK
Create your own file systems in Windows and .NET applications
CSTSOFT Instrumentation .NET & ActiveX Components
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.