Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- using Microsoft.DirectX.DirectInput;
- namespace Reproduction.simulation
- {
- public partial class Reproduction : Form
- {
- private Microsoft.DirectX.Direct3D.Device device;
- private Microsoft.DirectX.DirectInput.Device keyboard;
- private float angle = 0f;
- private int width = 64;
- private int height = 64;
- private int minimumHeight = 255;
- private int maximumHeight = 0;
- private CustomVertex.PositionColored[] vertices;
- private short[] indices;
- private int[,] heightData;
- private Mesh meshTerrain;
- public Reproduction()
- {
- InitializeComponent();
- this.SetStyle(ControlStyles.AllPaintingInWmPaint |
- ControlStyles.Opaque, true);
- }
- public void InitializeDevice()
- {
- PresentParameters presentParams = new PresentParameters();
- presentParams.Windowed = true;
- presentParams.SwapEffect = SwapEffect.Discard;
- presentParams.AutoDepthStencilFormat = DepthFormat.D16;
- presentParams.EnableAutoDepthStencil = true;
- device = new Microsoft.DirectX.Direct3D.Device(0,
- Microsoft.DirectX.Direct3D.DeviceType.Hardware, this,
- CreateFlags.SoftwareVertexProcessing, presentParams);
- //device.RenderState.FillMode = FillMode.WireFrame;// siatka
- }
- public void Initialization()
- {
- LoadHeightData();
- VertaxDeclaration();
- IndicesDeclaration();
- CreateMesh();
- CameraPositioning();
- }
- public void InitializeKeyboard()
- {
- keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
- keyboard.SetCooperativeLevel(this, CooperativeLevelFlags.Background
- | CooperativeLevelFlags.NonExclusive);
- keyboard.Acquire();
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
- Color.DarkSlateBlue, 1.0f, 0);
- device.BeginScene();
- device.Transform.World = Matrix.Translation(-height / 2,
- - width / 2, 0) * Matrix.RotationZ(angle);
- int numSubSets = meshTerrain.GetAttributeTable().Length;
- for (int i = 0; i < numSubSets; ++i)
- meshTerrain.DrawSubset(i);
- device.EndScene();
- device.Present();
- this.Invalidate();
- ReadKeyboard();
- }
- private void CameraPositioning()
- {
- device.Transform.Projection =
- Matrix.PerspectiveFovLH((float)Math.PI / 4,
- this.Width / this.Height, 1f, 250f);
- device.Transform.View = Matrix.LookAtLH(
- new Vector3(80, 0, 120), new Vector3(-20, 0, 0),
- new Vector3(0, 0, 1));
- device.RenderState.CullMode = Cull.None;
- device.RenderState.Lighting = true;
- device.Lights[0].Type = LightType.Directional;
- device.Lights[0].Diffuse = Color.White;
- device.Lights[0].Direction = new Vector3(-0.5f, 0, -1f);
- device.Lights[0].Enabled = true;
- }
- private void VertaxDeclaration()
- {
- vertices = new CustomVertex.PositionColored[width*height];
- for (int x = 0; x < width; x++)
- for (int y = 0; y < height; y++)
- {
- vertices[x + y * width].Position
- = new Vector3(x, y, heightData[x,y]);
- if (heightData[x, y] < minimumHeight
- + (maximumHeight - minimumHeight) / 4)
- vertices[x + y * width].Color
- = Color.Blue.ToArgb();
- else if (heightData[x, y] < minimumHeight
- + (maximumHeight - minimumHeight) * 2 / 4)
- vertices[x + y * width].Color
- = Color.Green.ToArgb();
- else if (heightData[x, y] < minimumHeight
- + (maximumHeight - minimumHeight) * 3 / 4)
- vertices[x + y * width].Color
- = Color.Brown.ToArgb();
- else vertices[x + y * width].Color
- = Color.White.ToArgb();
- }
- }
- private void IndicesDeclaration()
- {
- indices = new short[(width - 1) * (height - 1) * 6];
- for (int x = 0; x < width - 1; x++)
- for (int y = 0; y < height - 1; y++)
- {
- indices[(x + y * (width - 1)) * 6] =
- (short) ((x + 1) + (y + 1) * width);
- indices[(x + y * (width - 1)) * 6 + 1] =
- (short) ((x + 1) + y * width);
- indices[(x + y * (width - 1)) * 6 + 2] =
- (short) (x + y * width);
- indices[(x + y * (width - 1)) * 6 + 3] =
- (short) ((x + 1) + (y + 1) * width);
- indices[(x + y * (width - 1)) * 6 + 4] =
- (short) (x + y * width);
- indices[(x + y * (width - 1)) * 6 + 5] =
- (short) (x + (y + 1) * width);
- }
- }
- private void CreateMesh()
- {
- meshTerrain = new Mesh((width - 1) * (height - 1) * 2,
- width * height, MeshFlags.Managed,
- CustomVertex.PositionColored.Format,
- device);
- meshTerrain.SetVertexBufferData(vertices, LockFlags.None);
- meshTerrain.SetIndexBufferData(indices, LockFlags.None);
- int[] adjac = new int[meshTerrain.NumberFaces * 3];
- meshTerrain.GenerateAdjacency(0.5f, adjac);
- meshTerrain.OptimizeInPlace(MeshFlags.OptimizeVertexCache,
- adjac);
- meshTerrain = meshTerrain.Clone(meshTerrain.Options.Value,
- CustomVertex.PositionNormalColored.Format, device);
- meshTerrain.ComputeNormals();
- }
- private void LoadHeightData()
- {
- int offset;
- byte dummy;
- FileStream fs = new FileStream("heightmap.bmp",
- FileMode.Open, FileAccess.Read);
- BinaryReader r = new BinaryReader(fs);
- for (int i = 0; i < 10; i++)
- dummy = r.ReadByte();
- offset = r.ReadByte()
- + r.ReadByte() * 256
- + r.ReadByte() * 256 * 256
- + r.ReadByte() * 256 * 256 * 256;
- for (int i = 0; i < 4; i++)
- dummy = r.ReadByte();
- width = r.ReadByte()
- + r.ReadByte() * 256
- + r.ReadByte() * 256 * 256
- + r.ReadByte() * 256 * 256 * 256;
- height = r.ReadByte()
- + r.ReadByte() * 256
- + r.ReadByte() * 256 * 256
- + r.ReadByte() * 256 * 256 * 256;
- heightData = new int[width, height];
- for (int i = 0; i < (offset - 26); i++)
- dummy = r.ReadByte();
- for (int i = 0; i < height; i++)
- for (int y = 0; y < width; y++)
- {
- int h = (int)(r.ReadByte())
- + (int)(r.ReadByte())
- + (int)(r.ReadByte());
- h /= 8;
- heightData[width - 1 - y, height - 1 - i] = h;
- if (h < minimumHeight)
- minimumHeight = h;
- if (h > maximumHeight)
- maximumHeight = h;
- }
- }
- private void LoadHeightDataFromRAW()
- {
- heightData = new int[width, height];
- FileStream fs = new FileStream("heightdata.raw",
- FileMode.Open, FileAccess.Read);
- BinaryReader r = new BinaryReader(fs);
- for (int i = 0; i < height; i++)
- for (int y = 0; y < width; y++)
- {
- int h = (int)(r.ReadByte() / 50);
- heightData[width - 1 - y, height - 1 - i] = h;
- }
- fs.Close();
- }
- private void ReadKeyboard()
- {
- KeyboardState keys = keyboard.GetCurrentKeyboardState();
- if (keys[Key.Delete])
- angle += 0.03f;
- if (keys[Key.Next])
- angle -= 0.03f;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement