I'm trying to render terrain, everything compiles fine, but I can't see anything on screen. ( I'm moving camera around in case it was on the other side)
Can't figure out whats wrong.
public class Terrain
{
public VertexBuffer vertexBuffer;
public IndexBuffer indexBuffer;
public VertexPositionNormalTexture[] vertices;
public int[] indices;
//int gridSize;
Camera camera;
ContentManager Content;
GraphicsDevice device;
Texture2D heightmap;
public Texture2D grass;
Effect effect;
public Terrain(ContentManager Content, GraphicsDevice device, Camera camera)
{
this.Content = Content;
this.device = device;
this.camera = camera;
heightmap = Content.Load<Texture2D>("Gfx//heightmap");
grass = Content.Load<Texture2D>("Gfx//grass");
effect = Content.Load<Effect>("Effects//terrain");
CreateVertices();
CreateIndices();
}
public void Draw()
{
effect.CurrentTechnique = effect.Techniques["Texture1"];
effect.Parameters["xWorldViewProjection"].SetValue(camera.worldMatrix * camera.viewMatrix * camera.projMatrix);
effect.Parameters["xTexture"].SetValue(grass);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3);
}
}
private void CreateVertices()
{
vertices = new VertexPositionNormalTexture[heightmap.Width * heightmap.Height];
for (int x = 0; x < heightmap.Width; x++)
{
for (int y = 0; y < heightmap.Height; y++)
{
vertices[y * heightmap.Width + x].Position = new Vector3(x, 0, y);
vertices[y * heightmap.Width + x].Normal = new Vector3(0, 1, 0);
vertices[y * heightmap.Width + x].TextureCoordinate = new Vector2(y, x);
}
}
//device.SetVertexBuffer(vertexBuffer);
}
private void CreateIndices()
{
indices = new int[(heightmap.Width - 1) * (heightmap.Height - 1) * 6];
int counter = 0;
for (int x = 0; x < heightmap.Width - 1; x++)
{
for (int y = 0; y < heightmap.Height - 1; y++)
{
int lowerLeft = x + y * heightmap.Width;
int lowerRight = (x + 1) + y * heightmap.Width;
int topLeft = x + (y + 1) * heightmap.Width;
int topRight = (x + 1) + (y + 1) * heightmap.Width;
indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;
indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}
}
}
