locked
Apply a texture to primitives in DirectX RRS feed

  • Question

  • Hi all,
    i'm developing a simple 3D Cad Application that renders some created objects. I'm able to apply a color-based material to the objects but not to cover their surface with a texture, such as a bitmap. These are the two main code blocks of my application:

    This code, creates the objects and creates materials for them. (To draw the objects, i'm using the Mesh class' methods):
                    objects(0) = Mesh.Box(device, obj.SizeX, obj.SizeY, obj.SizeZ)
    
                    materials(0) = New Material
    
                    materials(0).Diffuse = obj.Material
    
                    materials(0).Specular = Color.LightGray
    
                    materials(0).SpecularSharpness = 15.0F
    
    
    
    
    This code, renders the object and its material by loading it into the device:
                device.Transform.World = Matrix.RotationX(CSng(d.RotationX)) * Matrix.RotationY(CSng(d.RotationY)) * Matrix.RotationZ(CSng(d.RotationZ)) * Matrix.Translation(TX, TY, TZ)
                
    device.Material = materials(0)


    objects(i).DrawSubset(0) device.Transform.World = Matrix.Identity
    How can I create a Material object from texture?
    Or, is there another way to solve my problem?

    Thanks in advance for any reply.
    Sunday, June 14, 2009 5:34 PM

Answers

  • Hi ,

    Textures are a powerful tool in creating realism in computer-generated 3D images. Direct3D supports an extensive texturing feature set, providing developers with easy access to advanced texturing techniques.

    This section will get you started using textures.

    These topics will go into more detail about additional texturing functionality.

     

    Direct3D maps texels in texture space directly to pixels in screen space, skipping the intermediate step for greater efficiency. This mapping process is actually an inverse mapping. That is, for each pixel in screen space, the corresponding texel position in texture space is calculated. The texture color at or around that point is sampled. The sampling process is called texture filtering. For more information, see Texture Filtering (Direct3D 9).

    Each texel in a texture can be specified by its texel coordinate. However, in order to map texels onto primitives, Direct3D requires a uniform address range for all texels in all textures. Therefore, it uses a generic addressing scheme in which all texel addresses are in the range of 0.0 to 1.0 inclusive. Direct3D applications specify texture coordinates in terms of u,v values, much like 2D Cartesian coordinates are specified in terms of x,y coordinates. Technically, the system can actually process texture coordinates outside the range of 0.0 and 1.0, and does so by using the parameters you set for texture addressing. For more information, see Texture Addressing Modes (Direct3D 9).

    A result of this is that identical texture addresses can map to different texel coordinates in different textures. In the following illustration, the texture address being used is (0.5,1.0). However, because the textures are different sizes, the texture address maps to different texels. Texture 1, on the left, is 5x5. The texture address (0.5,1.0) maps to texel (2,4). Texture 2, on the right, is 7x7. The texture address (0.5,1.0) maps to texel (3,6).

    Texture addressing

    A simplified version of the texel mapping process is shown in the following diagram. Admittedly, this example is extremely simple. For more detailed information, see Directly Mapping Texels to Pixels (Direct3D 9).

    Pixel idealized into a square of color

    For this example, a pixel, shown at the left of the illustration, is idealized into a square of color. The addresses of the four corners of the pixel are mapped into the 3D primitive in object space. The shape of the pixel is often distorted because of the shape of the primitive in 3D space and because of the viewing angle. The corners of the surface area on the primitive that correspond the corners of the pixel are then mapped into texture space. The mapping process distorts the pixel's shape again, which is common. The final color value of the pixel is computed from the texels in the region to which the pixel maps. You determine the method that Direct3D uses to arrive at the pixel color when you set the texture filtering method. For more information, see Texture Filtering (Direct3D 9).

    Your application can assign texture coordinates directly to vertices. This capability gives you control over which portion of a texture is mapped into a primitive. For instance, suppose you create a rectangular primitive that is exactly the same size as the texture in the following illustration. In this example, you want your application to map the whole texture onto the whole wall. The texture coordinates your application assigns to the vertices of the primitive are (0.0,0.0), (1.0,0.0), (1.0,1.0), and (0.0,1.0).

    Texture-mapped wall

    If you decide to decrease the height of the wall by one-half, you can distort the texture to fit onto the smaller wall, or you can assign texture coordinates that cause Direct3D to use the lower half of the texture.

    If you decide to distort or scale the texture to fit the smaller wall, the texture filtering method that you use will influence the quality of the image. For more information, see Texture Filtering (Direct3D 9).

    Best wishes
    xingwei Hu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Xingwei Hu Monday, June 22, 2009 6:10 AM
    Friday, June 19, 2009 9:47 AM