public void Initialise() { sw = new Stopwatch(); sw.Start(); Vertex[] vt = new Vertex[x1.indices.Count]; for (int n = 0; n < (x1.indices.Count); n++) { int m = x1.indices[n]; vt[n].Position[0] = x1.vertices[3 * m]; vt[n].Position[1] = x1.vertices[3 * m + 2]; vt[n].Position[2] = x1.vertices[3 * m + 1]; vt[n].Position[3] = 1.0f; vt[n].Normal[0] = x1.normals[3 * m]; vt[n].Normal[1] = x1.normals[3 * m + 2]; vt[n].Normal[2] = x1.normals[3 * m + 1]; vt[n].Normal[3] = 1.0f; vt[n].TexUV[0] = x1.texUV[2 * m]; vt[n].TexUV[1] = x1.texUV[2 * m + 1]; vt[n].Color[0] = 1.0f; vt[n].Color[1] = 1.0f; vt[n].Color[2] = 1.0f; vt[n].Color[3] = 1.0f; } vertices = Buffer.Create(dev, BindFlags.VertexBuffer, vt); // Vertex and Pixel shaders. vsByteCode = ShaderBytecode.CompileFromFile("cubeTexShader2.hlsl", "VSMain", "vs_5_0", ShaderFlags.None, EffectFlags.None); vShader = new VertexShader(dev, vsByteCode); psByteCode = ShaderBytecode.CompileFromFile("cubeTexShader2.hlsl", "PSMain", "ps_5_0", ShaderFlags.None, EffectFlags.None); pShader = new PixelShader(dev, psByteCode); signature = ShaderSignature.GetInputSignature(vsByteCode); // Input layout. layout = new InputLayout(dev, signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, InputElement.AppendAligned, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0) }); // Constant buffer cBuffer = new Buffer(dev, Utilities.SizeOf <cBufferStruct>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); // Texture ImageLoader imgLoader = new ImageLoader(); var texture = imgLoader.loadImage("cube_uv.png", ref dev); textureView = new ShaderResourceView(dev, texture); colorSampler = new SamplerState(dev, new SamplerStateDescription() { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, BorderColor = Color.Black, ComparisonFunction = Comparison.Never, MaximumAnisotropy = 16, MipLodBias = 0, MinimumLod = -float.MaxValue, MaximumLod = float.MaxValue }); // Set up matrices Vector3 eye = new Vector3(-8, 5, -8); Vector3 at = new Vector3(0, 0, 0); Vector3 up = Vector3.UnitY; view = Matrix.LookAtLH(eye, at, up); proj = Matrix.Identity; // Set up projection matrix with correct aspect ratio. proj = Matrix.PerspectiveFovLH((float)MathUtil.Pi / 4.0f, ((float)formWidth / (float)formHeight), 0.1f, 1000.0f); }
public void Initialise() { sw = new Stopwatch(); sw.Start(); // Set the minimum and maximum values for the bounding box. minV = new Vector3(1000.0f, 1000.0f, 1000.0f); maxV = new Vector3(-1000.0f, -1000.0f, -1000.0f); // Get the terrain size. int te_width = te.getTerrainSize(); int te_length = te.getTerrainSize(); // Initialise the vertex array. vl = new List <Vertex>(); vt = new Vertex[te_width * te_length]; for (int i = 0; i < te_width; i++) { for (int j = 0; j < te_length; j++) { vt[te_width * i + j] = new Vertex() { Position = new Vector4(te.heightmap[i, j].position.X, te.heightmap[i, j].position.Y, te.heightmap[i, j].position.Z, 1.0f), Normal = new Vector4(te.heightmap[i, j].normal.X, te.heightmap[i, j].normal.Y, te.heightmap[i, j].normal.Z, 1.0f), TexUV = new Vector2(0.5f, 0.5f), Color = new Vector4(0.13f, 0.5f, 0.2f, 1.0f) }; // Perform a check for the min and max values. // Update the bounds accordingly. minV.X = getMin(vt[te_width * i + j].Position.X, minV.X); minV.Y = getMin(vt[te_width * i + j].Position.Y, minV.Y); minV.Z = getMin(vt[te_width * i + j].Position.Z, minV.Z); maxV.X = getMax(vt[te_width * i + j].Position.X, maxV.X); maxV.Y = getMax(vt[te_width * i + j].Position.Y, maxV.Y); maxV.Z = getMax(vt[te_width * i + j].Position.Z, maxV.Z); } } for (int i = 0; i < te_width - 1; i++) { for (int j = 0; j < te_length - 1; j++) { // Triangle 1 int index = i * te_width + j; vl.Add(new Vertex() { Position = new Vector4(vt[index].Position.X, vt[index].Position.Y, vt[index].Position.Z, vt[index].Position.W), Normal = new Vector4(vt[index].Normal.X, vt[index].Normal.Y, vt[index].Normal.Z, vt[index].Normal.W), TexUV = new Vector2(0.0f, 0.0f), Color = new Vector4(0.3f, 0.7f, 0.5f, 1.0f) }); index = (i) * te_width + (j + 1); vl.Add(new Vertex() { Position = new Vector4(vt[index].Position.X, vt[index].Position.Y, vt[index].Position.Z, vt[index].Position.W), Normal = new Vector4(vt[index].Normal.X, vt[index].Normal.Y, vt[index].Normal.Z, vt[index].Normal.W), TexUV = new Vector2(0.0f, 1.0f), Color = new Vector4(0.3f, 0.7f, 0.5f, 1.0f) }); index = (i + 1) * te_width + (j + 1); vl.Add(new Vertex() { Position = new Vector4(vt[index].Position.X, vt[index].Position.Y, vt[index].Position.Z, vt[index].Position.W), Normal = new Vector4(vt[index].Normal.X, vt[index].Normal.Y, vt[index].Normal.Z, vt[index].Normal.W), TexUV = new Vector2(1.0f, 1.0f), Color = new Vector4(0.3f, 0.7f, 0.5f, 1.0f) }); // Triangle 2 index = (i + 1) * te_width + (j + 1); vl.Add(new Vertex() { Position = new Vector4(vt[index].Position.X, vt[index].Position.Y, vt[index].Position.Z, vt[index].Position.W), Normal = new Vector4(vt[index].Normal.X, vt[index].Normal.Y, vt[index].Normal.Z, vt[index].Normal.W), TexUV = new Vector2(1.0f, 1.0f), Color = new Vector4(0.3f, 0.7f, 0.5f, 1.0f) }); index = (i + 1) * te_width + (j); vl.Add(new Vertex() { Position = new Vector4(vt[index].Position.X, vt[index].Position.Y, vt[index].Position.Z, vt[index].Position.W), Normal = new Vector4(vt[index].Normal.X, vt[index].Normal.Y, vt[index].Normal.Z, vt[index].Normal.W), TexUV = new Vector2(1.0f, 0.0f), Color = new Vector4(0.3f, 0.7f, 0.5f, 1.0f) }); index = i * te_width + (j); vl.Add(new Vertex() { Position = new Vector4(vt[index].Position.X, vt[index].Position.Y, vt[index].Position.Z, vt[index].Position.W), Normal = new Vector4(vt[index].Normal.X, vt[index].Normal.Y, vt[index].Normal.Z, vt[index].Normal.W), TexUV = new Vector2(0.0f, 0.0f), Color = new Vector4(0.3f, 0.7f, 0.5f, 1.0f) }); } } var vt2 = vl.ToArray(); vertices = Buffer.Create(dev, BindFlags.VertexBuffer, vt2); //vertices = new Buffer(dev, Utilities.SizeOf<Vertex>() * vl.Count, // ResourceUsage.Dynamic, BindFlags.VertexBuffer, // CpuAccessFlags.Write, ResourceOptionFlags.None, 0); //dev.ImmediateContext.UpdateSubresource(vt2, vertices); bbc = new BBCorners(minV, maxV); var bb8corners = bbc.getBB8Corners(); bb = bbc.getBoundingBox(); bBox = Buffer.Create(dev, BindFlags.VertexBuffer, bb8corners); // Vertex and Pixel shaders. vsByteCode = ShaderBytecode.CompileFromFile("TerrainShader.hlsl", "VSMain", "vs_5_0", ShaderFlags.None, EffectFlags.None); vShader = new VertexShader(dev, vsByteCode); psByteCode = ShaderBytecode.CompileFromFile("TerrainShader.hlsl", "PSMain", "ps_5_0", ShaderFlags.None, EffectFlags.None); pShader = new PixelShader(dev, psByteCode); vsByteCode2 = ShaderBytecode.CompileFromFile("BBoxShader.hlsl", "VSMain", "vs_5_0", ShaderFlags.None, EffectFlags.None); vShader2 = new VertexShader(dev, vsByteCode2); psByteCode2 = ShaderBytecode.CompileFromFile("BBoxShader.hlsl", "PSMain", "ps_5_0", ShaderFlags.None, EffectFlags.None); pShader2 = new PixelShader(dev, psByteCode2); signature = ShaderSignature.GetInputSignature(vsByteCode); signature2 = ShaderSignature.GetInputSignature(vsByteCode2); // Input layout. layout = new InputLayout(dev, signature, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, InputElement.AppendAligned, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0) }); layout2 = new InputLayout(dev, signature2, new[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0) }); // Constant buffer cBuffer = new Buffer(dev, Utilities.SizeOf <cBufferStruct>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); // Texture ImageLoader imgLoader = new ImageLoader(); var texture = imgLoader.loadImage("grasstex.png", ref dev); textureView = new ShaderResourceView(dev, texture); colorSampler = new SamplerState(dev, new SamplerStateDescription() { Filter = Filter.MinMagMipLinear, AddressU = TextureAddressMode.Clamp, AddressV = TextureAddressMode.Clamp, AddressW = TextureAddressMode.Clamp, BorderColor = Color.Black, ComparisonFunction = Comparison.Never, MaximumAnisotropy = 16, MipLodBias = 0, MinimumLod = -float.MaxValue, MaximumLod = float.MaxValue }); // Set up matrices Vector3 eye = new Vector3(-20, 5, -20); Vector3 at = new Vector3(0, 0, 0); Vector3 up = Vector3.UnitY; view = Matrix.LookAtLH(eye, at, up); proj = Matrix.Identity; // Set up projection matrix with correct aspect ratio. proj = Matrix.PerspectiveFovLH((float)MathUtil.Pi / 4.0f, ((float)formWidth / (float)formHeight), 0.1f, 1000.0f); }