Okay guys for probably 24 hours now I've been going through numerous websites and tutorials on textured terrain in XNA 4. I believe I've gotten pretty close to my solution however I think I've finally hit a wall. I've been using Riemers XNA tutorials for the most part, now let me post a bit of code so you guys can see what I'm doing:
Here's the method that does it all (besides draw):
private void LoadVertices()
{
VertexPositionNormalTexture[] terrainVertices = SetUpTerrainVertices();
int[] terrainIndices = SetUpTerrainIndices();
terrainVertices = CalculateNormals(terrainVertices, terrainIndices);
CopyToTerrainBuffers(terrainVertices, terrainIndices);
}
Now here's the method "SetUpTerrainVerticies":
private VertexPositionNormalTexture[] SetUpTerrainVertices()
{
VertexPositionNormalTexture[] terrainVertices = new VertexPositionNormalTexture[terrainWidth * terrainHeight];
for (int x = 0; x < terrainWidth; x++)
{
for (int y = 0; y < terrainHeight; y++)
{
terrainVertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);
terrainVertices[x + y * terrainWidth].TextureCoordinate.X = (float)x / 30.0f;
terrainVertices[x + y * terrainWidth].TextureCoordinate.Y = (float)y / 30.0f;
}
}
return terrainVertices;
}
And SetUpTerrainIndicies():
private int[] SetUpTerrainIndices()
{
int[] indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6];
int counter = 0;
for (int y = 0; y < terrainHeight - 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;
indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;
indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}
return indices;
}
CalculateNormals():
private VertexPositionNormalTexture[] CalculateNormals(VertexPositionNormalTexture[] vertices, int[] indices)
{
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++)
{
int index1 = indices[i * 3];
int index2 = indices[i * 3 + 1];
int index3 = indices[i * 3 + 2];
Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
Vector3 normal = Vector3.Cross(side1, side2);
vertices[index1].Normal += normal;
vertices[index2].Normal += normal;
vertices[index3].Normal += normal;
}
for (int i = 0; i < vertices.Length; i++)
vertices[i].Normal.Normalize();
return vertices;
}
And lastly, CopyToTerrainBuffers():
private void CopyToTerrainBuffers(VertexPositionNormalTexture[] vertices, int[] indices)
{
terrainVertexBuffer = new VertexBuffer(device, typeof(VertexPositionNormalTexture), vertices.Length, BufferUsage.WriteOnly);
terrainVertexBuffer.SetData(vertices);
terrainIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
terrainIndexBuffer.SetData(indices);
}
As for the .fx file itself, I don't believe it is the problem because I'm able to do everything besides displaying the texture.
Now here's the drawing code aswell:
myEffect.Parameters["xView"].SetValue(camera.viewMatrix);
myEffect.Parameters["xProjection"].SetValue(camera.projectionMatrix);
myEffect.Parameters["xWorld"].SetValue(mapMatrix);
myEffect.Parameters["xEnableLighting"].SetValue(true);
Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);
lightDirection.Normalize();
myEffect.Parameters["xLightDirection"].SetValue(lightDirection);
myEffect.Parameters["xAmbient"].SetValue(0.1f);
myEffect.CurrentTechnique = myEffect.Techniques["Textured"];
myEffect.Parameters["xTexture"].SetValue(grassTexture);
foreach (EffectPass pass in myEffect.CurrentTechnique.Passes)
{
pass.Apply();
device.SetVertexBuffer(terrainVertexBuffer);
device.Indices = terrainIndexBuffer;
int noVertices = terrainVertexBuffer.VertexCount;
int noTriangles = terrainIndexBuffer.IndexCount / sizeof(int) / 3;
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, noVertices, 0, noTriangles);
}
When I run this all I see is a black screen. If I draw without textures (just colored) it works fine and I see my map colored white like I told it to.
Any help is appreciated, I'm on the verge of simply copy and pasting someone else's sample project and building on top of that without knowing how it works >.<<br mode="hold" />
Thank you so much in advance, I'm probably making a silly mistake somewhere. I just started XNA 3D programming a couple days ago so I wouldn't be surprised :P