XNA Texture mapping on model

I have a cube as my model and want to map a texture on every side of the cube. I have been searching for a while and most answers suggest using 3D max or using VertexPositionTextures.

I am not allowed to use any moddeling program, I have to use XNA, so that is not an option. For VertexPositionTextures you have to provide a Vector3 and a vector2 containing the position and the some area of your texture. But then, why would I use a model if I provide the position myself? Do I get this wrong maybe?

I tried to add the texture to my effect with effect.texure = ... but I can't specify what to map. It just puts the texture on my model.

So how can I map a texture to my model and specify what and where to map?

So you're not using a modelling program- thus you'll have to provide the cube/texture information yourself from code.

Let's start out simple with one textured triangle. VertexPositionTextures contains Vertex (location of points in space) and texture (where does that point match to a point on an image). Keep in mind that the location for the texture is on a scale of 0 (top,left) to 1 (bottom,right): Thus for one triangle:

  1
/ \
0---2

Vertices[0].Position = new Vector3(-5, 0, 0);
Vertices[1].Position = new Vector3(0, 10, 0);
Vertices[2].Position = new Vector3(5, 0, 0);

Vertices[0].TextureCoordinate = new Vector2(0, 1);
Vertices[1].TextureCoordinate = new Vector2(0.5f, 0);
Vertices[2].TextureCoordinate = new Vector2(1, 1);


This holds the information of the location of the points in space, and the location of the texture. The point (2) is located at 0,10,0 in 3D space, and the location of the the point on the texture is like this halfway along the X axis (x=0.5) and top of y-axis (y=0):

+--2--+
|     |
|     |
+-----+


For a 3D cube you basically you have a list of Vector3 points for each corner. To create your cube you'll need to create 12 triangles like that (3 points per triangle, 2 triangles per side, 6 sides for a cube) = VertexPositionTexture[36], and map each corner to the corners of your texture.

Another approach is to use indexed vertices; this means you need to define 8 VertextPositionTextures only for each 3D coordinate, and have a separate array int[36] to hold the triangle information. This makes it easier to work with, but in your case may be a headache to get the texture coordinates right; as each point would also share the texture position. For more insight in that method read this: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/Indices.php

Sidenote: you can load models in XNA so I'm not sure how to interpret "I have to use XNA, so that is not an option"

• I mean I can not use some modeling programs for the mapping. Is it possible to get the vertices from a model? Or get the vertex positions textures? Commented Jul 17, 2015 at 21:42
• If you have a model, there are tools that extract the texturecoordinates and generate a bitmap for you. Google for 'uv mapping tool'. The thing is, this information should be present in your model already Commented Jul 18, 2015 at 5:56