Home Articles Tutorials Resources About

Tutorial 4 - Texturing a quad


By Pieter Germishuys, January 4 2006

Hi there again everyone. How did you enjoy those spinning triangles?

Today we are going to be looking at how to texture a quad (2 triangles form a quad). We are going to build the quad in the order given in the image below.
Texturing quads is a really easy method. It involves placing what they call UV coordinates (Texture coordinates). UV coordinates (Texure coordinates) are used to tell direct3d how to map the texture points to the object. Look at the image below. It shows the UV coordinates for a face on a cube.

Let's move on. We are going to be using the CustomVertex.PositionTextured vertex format and create and texture our quad. The most important part of this exercise is the use of the Texture class. For a better understanding. Let's take an extract out of the SDK Docs.

"Early computer-generated 3-D images, although generally advanced for their time, tended to have a shiny plastic look. They lacked the types of markings?such as scuffs, cracks, fingerprints, and smudges?that give 3-D objects realistic visual complexity. In recent years, textures have gained popularity among developers as a tool for enhancing the realism of computer-generated 3-D images.

In its everyday use, the word texture refers to an object's smoothness or roughness. In computer graphics, however, a texture is a bitmap of pixel colors that give an object the appearance of texture.

Because Microsoft Direct3D textures are bitmaps, any bitmap can be applied to a Direct3D primitive. For instance, applications can create and manipulate objects that appear to have a wood grain pattern in them. Grass, dirt, and rocks can be applied to a set of 3-D primitives that form a hill. The result is a realistic-looking hillside. Textures also can be used to create effects such as signs along a road, rock strata in a cliff, or the appearance of marble on a floor.

In addition, Direct3D supports more advanced texturing techniques such as texture blending?with or without transparency?and light mapping.

If an application creates a hardware abstraction layer (HAL) device or a software device, it can use 8, 16, 24, 32, 64, or 128-bit textures."

Armed with some more knowledge on Textures. We are going to create a texture from file. Direct3D is one step ahead of us. It provides us with a TextureLoader class. The class has a method that creates a texture from file and is called FromFile(). The First parameter it takes is the Device and the second is a string that contains the path to the file. Let's look at the code.

public void createQuad()
{
?????texture = TextureLoader.FromFile(direct3d.XilathDevice, "crate.jpg"); //Load the image from the file

?????CustomVertex.PositionTextured[] quad = new CustomVertex.PositionTextured[4];
?????quad[0] = new CustomVertex.PositionTextured(-1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
?????quad[1] = new CustomVertex.PositionTextured(-1.0f, 2.0f, 0.0f, 0.0f, 0.0f);
?????quad[2] = new CustomVertex.PositionTextured( 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
?????quad[3] = new CustomVertex.PositionTextured( 1.0f, 2.0f, 0.0f, 1.0f, 0.0f);

?????vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, direct3d.XilathDevice, Usage.WriteOnly, ?????CustomVertex.PositionTextured.Format, Pool.Managed);
?????GraphicsStream stm = vb.Lock(0, 0, 0);
???????????stm.Write(quad);
?????vb.Unlock();
?????worldMatrix.Translate(0.0f, -1.0f, 0.0f);
}
private Texture texture;


Now that we have loaded the texture and stored the quad into the Vertex Buffer. Let's render the quad.
Note: When we set the texture we are telling direct3d that any object rendered after that call should be rendered with that texture. So keep this in mind. If you want to set the texture to nothing just do SetTexture(0, 0);


public void Render()
{
?????direct3d.BeginRender(Color.Black); //render our scene with the background as black

?????direct3d.XilathDevice.Transform.World = worldMatrix;
?????direct3d.XilathDevice.SetTexture(0, texture); //Here we tell direct3d that anything after this method should be rendered with this ?????texture
?????direct3d.XilathDevice.SetStreamSource(0, vb, 0);
?????direct3d.XilathDevice.VertexFormat = CustomVertex.PositionTextured.Format;
?????direct3d.XilathDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

?????direct3d.FinishRender(); //finish rendering our scene
}




Files for this tutorial

Filename Size
? tut4.rar 178.0 KB
?
MDX info is an initiative by vector4. All content is copyright ? 2005-2006 by its respective authors | About MDX info | Terms of Use |
Coming soon!