O'Reilly logo

Learning XNA 3.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying Textures

OK, now that you have a rectangular shape, you're ready to apply a texture to your rectangle. First, you'll have to tell the graphics device that you're going to be using textures with your vertices. Currently, the type of object you're using to represent your vertices is VertexPositionColor, which tells XNA that you want to use a position and a color for your vertices. You'll need to change this to use a different object type called VertexPositionTexture, which represents a vertex that has both a position and a texture. Change the type for your vertex array variable at the top of the class to this:

VertexPositionTexture[] verts;

Next, change the code in the LoadContent method that initializes the vertices. The constructor for a VertexPositionTexture takes two parameters: a Vector3 representing the position of the vertex, and a Vector2 representing a texture coordinate.

What a minute! What's a "texture coordinate?" That's an excellent question. A texture coordinate is a way for XNA to map a coordinate on a texture to a vertex of a primitive. When texturing a primitive in this way, you identify points of a texture that correspond to vertices, and then XNA handles grabbing the specified portion of the texture and mapping it accordingly on the primitive.

A texture coordinate is represented by a two-dimensional (U, V) coordinate, where U is horizontal and V is vertical. The top-left corner of an image is represented by texture coordinate (0, 0), and the bottom-right corner ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required