Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page