# Multiple layer texture coordinates

Page 1 of 1

## 2 Replies - 1517 Views - Last Post: 26 July 2013 - 02:26 PM

### #1 football1994

Reputation: 2
• Posts: 24
• Joined: 01-July 13

# Multiple layer texture coordinates

Posted 23 July 2013 - 11:57 PM

Hi Guys

Ive been working on a collision detection effect using a multitextured(2Dheightmap) terrain in which I wanted to have a car model as it moves across the terrain emit particles based on the texture coordinates being collided with, but as for now Im just starting with a simple ray pointer that returns the triangle intersected along with the texture coordinates of the terrain. I recieved a lot of good help from this forum which helped me gain a clearer understanding of what to do and not to do with my project which brings me to what I think is the last thing that needs to be done in order to make it work which is creating a layer of coordinates for every texture on the terrain,but the way I think the terrain is set up now is that all the textures share one layer of coordinates and this is what I need help with.

steps taken to modify the code:

step1.) modified the vertex code to take in more than three texture coordinates

```  /*
* Struct to hold vertex information with pos,normal,texcoord and texweights
*/
public struct VertexMultitextured
{
public Vector3 Position;
public Vector3 Normal;
public Vector4 TextureCoordinate;
public Vector4 TexWeights;
public Vector4 TexWeights1;
public Vector4 TexWeights2;
public Vector4 TexWeights3;
public Vector4 TexWeights4;

public static int SizeInBytes = (3 + 3 + 4 + 4 + 4 + 4 + 4 + 4) * sizeof(float); // vec3 + vec3 + vec4 +vec4
public static VertexElement[] VertexElements = new VertexElement[]
{
// stream, offset, type, method, usage and usage index
new VertexElement( 0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0 ),
new VertexElement( 0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0 ),
new VertexElement( 0, sizeof(float) * 6, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0 ),
new VertexElement( 0, sizeof(float) * 10, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1 ),
new VertexElement( 0, sizeof(float) * 14, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 2 ),
new VertexElement( 0, sizeof(float) * 18, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 3 ),
new VertexElement( 0, sizeof(float) * 22, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 4 ),
new VertexElement( 0, sizeof(float) * 26, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 5 ),
};
}
```

step2.)modified the texweights in the terrainvertices method but just turned certain parts of the terrain black

```                    // X = Sand, Y = Grass, Z = Stone and W = Snow
terrainVertices[x + (y * terrainWidth)].TexWeights.X = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 0) / 8.0f, 0, 1);
terrainVertices[x + (y * terrainWidth)].TexWeights1.Y = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 12) / 6.0f, 0, 1);
terrainVertices[x + (y * terrainWidth)].TexWeights2.Z = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 20) / 6.0f, 0, 1);
terrainVertices[x + (y * terrainWidth)].TexWeights3.W = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 30) / 6.0f, 0, 1);

float total = terrainVertices[x + y * terrainWidth].TexWeights.X;
total += terrainVertices[x + y * terrainWidth].TexWeights1.Y;
total += terrainVertices[x + y * terrainWidth].TexWeights2.Z;
total += terrainVertices[x + y * terrainWidth].TexWeights3.W;

terrainVertices[x + y * terrainWidth].TexWeights.X /= total;
terrainVertices[x + y * terrainWidth].TexWeights1.Y /= total;
terrainVertices[x + y * terrainWidth].TexWeights2.Z /= total;
terrainVertices[x + y * terrainWidth].TexWeights3.W /= total;
```

below is a rough draft of what Im trying to do

```public bool grassuv(Ray ray2)
{

for (int y = 0; y < terrain.Tlength - 1; y++)
{
for (int x = 0; x < terrain.Twidth - 1; x++)
{

pointa = terrain.TerrainVertices[x + (-y * terrain.terrainWidth)].Position; // First Point
pointb = terrain.TerrainVertices[x + ((-y + 1) * terrain.terrainWidth)].Position; // Second
pointc = terrain.TerrainVertices[(x + 1) + (-y * terrain.terrainWidth)].Position; // And third

grassu = terrain.TerrainVertices[(x + 1) + (-y * terrain.terrainWidth)].Texcoord1.Z;
grassv = terrain.TerrainVertices[(x + 1) + (-y * terrain.terrainWidth)].Texcoord1.Y;

bool hit = RayTriangleIntersect(
ray2.Position, ray2.Direction,
pointa,
pointb,
pointc,
out grassu, out grassv)

if (hit)
{

return true;
}

}
}
return false;
}
```

called in the update method

```protected override void Update(GameTime gameTime)
{

if(grassuv( ray))
{
pickedTriangle[0].Position = pointa;
pickedTriangle[0].Color = Color.Blue;
pickedTriangle[1].Position = pointb;
pickedTriangle[1].Color = Color.Blue;
pickedTriangle[2].Position = pointc;
pickedTriangle[2].Color = Color.Blue;

}
else if(sanduv(ray))
{
pickedTriangle[0].Position = pointa;
pickedTriangle[0].Color = Color.Red;
pickedTriangle[1].Position = pointb;
pickedTriangle[1].Color = Color.Red;
pickedTriangle[2].Position = pointc;
pickedTriangle[2].Color = Color.Red;

}
```

terrain code

```namespace TRIANGLE
{
public class Terrain
{
/*
* Struct to hold vertex information with pos,normal,texcoord and texweight
*/
public struct VertexMultitextured
{
public Vector3 Position;
public Vector3 Normal;
public Vector4 TextureCoordinate;
public Vector4 TexWeights;
public Vector2 TextureCoordinate1;
public Vector2 TextureCoordinate2;
public Vector2 TextureCoordinate3;
public Vector2 TextureCoordinate4;

public static int SizeInBytes = (3 + 3 + 4 + 4 + 2 + 2 + 2 + 2) * sizeof(float); // vec3 + vec3 + vec4 +vec4
public static VertexElement[] VertexElements = new VertexElement[]
{
// stream, offset, type, method, usage and usage index
new VertexElement( 0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0 ),
new VertexElement( 0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0 ),
new VertexElement( 0, sizeof(float) * 6, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0 ),
new VertexElement( 0, sizeof(float) * 10, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1 ),
new VertexElement( 0, sizeof(float) * 14, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 2 ),
new VertexElement( 0, sizeof(float) * 16, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 3 ),
new VertexElement( 0, sizeof(float) * 18, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 4 ),
new VertexElement( 0, sizeof(float) * 20, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 5 ),
};
}

Game1 GameClass;
GraphicsDevice device;

public int terrainWidth;
public int terrainLength;
public float[,] heightData;
VertexBuffer terrainVertexBuffer;
IndexBuffer terrainIndexBuffer;
VertexDeclaration terrainVertexDeclaration;
VertexMultitextured[] terrainVertices;

public Texture2D grassTexture;
public Texture2D sandTexture;
public Texture2D rockTexture;
public Texture2D snowTexture;

public VertexMultitextured[] TerrainVertices
{
get
{
return terrainVertices;
}

}
public int Twidth
{
get
{
return terrainWidth;
}

}
public int Tlength
{
get
{
return terrainLength;
}

}

public Terrain(Game1 reference,GraphicsDevice device)
{
this.GameClass = reference;
this.device = device;

}

{

}

{

VertexMultitextured[] terrainVertices = SetUpTerrainVertices();
int[] terrainIndices = SetUpTerrainIndices();
terrainVertices = CalculateNormals(terrainVertices, terrainIndices);
CopyToTerrainBuffers(terrainVertices, terrainIndices);
terrainVertexDeclaration = new VertexDeclaration(device, VertexMultitextured.VertexElements);

}

public int[] TerrainIndices
{
get
{
return terrainIndices;
}

}

{
float minimumHeight = float.MaxValue;
float maximumHeight = float.MinValue;

terrainWidth = heightMap.Width;
terrainLength = heightMap.Height;

// get rgb values for each pixel of hightmap
Color[] heightMapColors = new Color[terrainWidth * terrainLength];
heightMap.GetData(heightMapColors);

heightData = new float[terrainWidth, terrainLength];
for (int x = 0; x < terrainWidth; x++)
for (int y = 0; y < terrainLength; y++)
{
heightData[x, y] = heightMapColors[x + (y * terrainWidth)].R; // read r value
// determine minimum and maximum height value in terrain
if (heightData[x, y] < minimumHeight) minimumHeight = heightData[x, y];
if (heightData[x, y] > maximumHeight) maximumHeight = heightData[x, y];
}

// get height values in a range between 0 and 30
for (int x = 0; x < terrainWidth; x++)
for (int y = 0; y < terrainLength; y++)
heightData[x, y] = (heightData[x, y] - minimumHeight) / (maximumHeight - minimumHeight) * 30.0f;

}

/*
* Define Vertices
*/
private VertexMultitextured[] SetUpTerrainVertices()
{
terrainVertices = new VertexMultitextured[terrainWidth * terrainLength];

for (int x = 0; x < terrainWidth; x++)
{
for (int y = 0; y < terrainLength; y++)
{
// generate a vertex for each pixel of the heightmap
// a terrain is generated in x,-y direction
terrainVertices[x + (y * terrainWidth)].Position = new Vector3(x, heightData[x, y], -y);
terrainVertices[x + (y * terrainWidth)].TextureCoordinate.X = (float)x / 30.0f; // /30 to stretch texture so it looks realistic
terrainVertices[x + (y * terrainWidth)].TextureCoordinate.Y = (float)y / 30.0f;

// X = Sand, Y = Grass, Z = Stone and W = Snow
terrainVertices[x + (y * terrainWidth)].TexWeights.X = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 0) / 8.0f, 0, 1);
terrainVertices[x + (y * terrainWidth)].TexWeights.Y = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 12) / 6.0f, 0, 1);
terrainVertices[x + (y * terrainWidth)].TexWeights.Z = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 20) / 6.0f, 0, 1);
terrainVertices[x + (y * terrainWidth)].TexWeights.W = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 30) / 6.0f, 0, 1);

float total = terrainVertices[x + y * terrainWidth].TexWeights.X;
total += terrainVertices[x + y * terrainWidth].TexWeights.Y;
total += terrainVertices[x + y * terrainWidth].TexWeights.Z;
total += terrainVertices[x + y * terrainWidth].TexWeights.W;

terrainVertices[x + y * terrainWidth].TexWeights.X /= total;
terrainVertices[x + y * terrainWidth].TexWeights.Y /= total;
terrainVertices[x + y * terrainWidth].TexWeights.Z /= total;
terrainVertices[x + y * terrainWidth].TexWeights.W /= total;
}
}

return terrainVertices;
}

/*
* Set indices clockwise to build faces
*/
private int[] SetUpTerrainIndices()
{
/*
* new int[(terrainWidth - 1) * (terrainLength - 1) * 6]
* Example, plane consisting of 4x3 points: first row is build out of 3 quads, second one also.
*                                        row   col    3 points are needed for each triangle, 6 for one quad
* so for a 4x3 points plane one can say (4-1)*(3-1) * 6
*/
int[] indices = new int[(terrainWidth - 1) * (terrainLength - 1) * 6];
int counter = 0;
for (int y = 0; y < terrainLength - 1; y++)
{
for (int x = 0; x < terrainWidth - 1; x++)
{
int lowerLeft = x + y * terrainWidth;
int lowerRight = (x + 1) + y * terrainWidth;
int topLeft = x + (y + 1) * terrainWidth;
int topRight = (x + 1) + (y + 1) * terrainWidth;

// order clockwise
indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;

indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}

return indices;
}

/*
* Calculate Normals on Terrain for realistic shadows
*/
private VertexMultitextured[] CalculateNormals(VertexMultitextured[] vertices, int[] indices)
{
// initialise normals with 0 0 0
for (int i = 0; i < vertices.Length; i++)
vertices[i].Normal = new Vector3(0, 0, 0);

for (int i = 0; i < indices.Length / 3; i++)
{
// Calculate Triangle
int index1 = indices[i * 3];
int index2 = indices[i * 3 + 1];
int index3 = indices[i * 3 + 2];

// Calculate normal on triangle
Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
Vector3 normal = Vector3.Cross(side1, side2);

// apply normal on all three vertices of triangle
vertices[index1].Normal += normal;
vertices[index2].Normal += normal;
vertices[index3].Normal += normal;
}

// normalize all normals
for (int i = 0; i < vertices.Length; i++)
vertices[i].Normal.Normalize();

return vertices;
}

/*
* Copy vertices and indices onto grafikcard buffer to save performance (so this data has to be transferred ONLY ONCE)
*/
private void CopyToTerrainBuffers(VertexMultitextured[] vertices, int[] indices)
{
terrainVertexBuffer = new VertexBuffer(device, vertices.Length * VertexMultitextured.SizeInBytes, BufferUsage.WriteOnly);
terrainVertexBuffer.SetData(vertices);

terrainIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
terrainIndexBuffer.SetData(indices);
}

/*
* This draws the terrain
*/
public void DrawTerrain(Effect effect)
{
effect.CurrentTechnique = effect.Techniques["MultiTextured"];
effect.Parameters["xTexture0"].SetValue(sandTexture);
effect.Parameters["xTexture1"].SetValue(grassTexture);
effect.Parameters["xTexture2"].SetValue(rockTexture);
effect.Parameters["xTexture3"].SetValue(snowTexture);

Matrix world = Matrix.CreateTranslation(new Vector3(77, 0, 0));
Matrix worldMatrix = Matrix.Identity;
effect.Parameters["xWorld"].SetValue(worldMatrix);
effect.Parameters["xEnableLighting"].SetValue(true);
effect.Parameters["xAmbient"].SetValue(0.4f);
effect.Parameters["xLightDirection"].SetValue(new Vector3(-0.5f, -1, -0.5f));

effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();

device.Vertices[0].SetSource(terrainVertexBuffer, 0, VertexMultitextured.SizeInBytes); // tell it that this data is in graka buffer
device.Indices = terrainIndexBuffer;
device.VertexDeclaration = terrainVertexDeclaration;

int noVertices = terrainVertexBuffer.SizeInBytes / VertexMultitextured.SizeInBytes;
int noTriangles = terrainIndexBuffer.SizeInBytes / sizeof(int) / 3;
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, noVertices, 0, noTriangles);

pass.End();
}
effect.End();
}
```

Thankyou

Is This A Good Question/Topic? 0

## Replies To: Multiple layer texture coordinates

### #2 BBeck

• Here to help.

Reputation: 742
• Posts: 1,739
• Joined: 24-April 12

## Re: Multiple layer texture coordinates

Posted 26 July 2013 - 07:10 AM

Ok. So it's multi-textured. After glancing at your code, does it use 5 textures? What determines which texture is used at a given spot on the terrain?

### #3 football1994

Reputation: 2
• Posts: 24
• Joined: 01-July 13

## Re: Multiple layer texture coordinates

Posted 26 July 2013 - 02:26 PM

Thanks for responding and forgive me for being a pest, but I think I finally
understand what I need to do, but Im not quite sure how to go about it. I learned
that the heightmap uses the red channel to render itself but that still leaves the
blue and green channels so what I wanted to try to do is create a mini splat map(http://en.wikipedia.org/wiki/Texture_splatting) by implementing one layer of uv coordinates(grass texture) using the blue channel, and another layer of uv coordinates(sand texture) for the green channel and by having different layers of texture coordinates I think Ill finally be able to use the triangle intersect method in the way that I have it setup in the code.

below is a youtube link to what the height based terrain looks like and to the origin of the project that Ive been trying to modify , also in the code my goal is to get the car to shoot out certain particles(grass debris,dirt debris,etc,...)as it moves over a certain texture. I feel pretty comfortable with different info Ive received on producing particles, its just the texture coordinate identification part thats been killing me, but with you guys and other forums helping me I feel a step closer to finally getting this right.

Thanks again