Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using Microsoft.DirectX;
  11. using Microsoft.DirectX.Direct3D;
  12. using Microsoft.DirectX.DirectInput;
  13.  
  14. namespace Reproduction.simulation
  15. {
  16.     public partial class Reproduction : Form
  17.     {
  18.         private Microsoft.DirectX.Direct3D.Device device;
  19.         private Microsoft.DirectX.DirectInput.Device keyboard;
  20.  
  21.         private float angle = 0f;
  22.         private int width = 64;
  23.         private int height = 64;
  24.         private int minimumHeight = 255;
  25.         private int maximumHeight = 0;
  26.  
  27.         private CustomVertex.PositionColored[] vertices;
  28.         private short[] indices;
  29.         private int[,] heightData;
  30.  
  31.         private Mesh meshTerrain;
  32.  
  33.         public Reproduction()
  34.         {
  35.             InitializeComponent();
  36.             this.SetStyle(ControlStyles.AllPaintingInWmPaint |
  37.                 ControlStyles.Opaque, true);
  38.         }
  39.  
  40.         public void InitializeDevice()
  41.         {
  42.             PresentParameters presentParams = new PresentParameters();
  43.             presentParams.Windowed = true;
  44.             presentParams.SwapEffect = SwapEffect.Discard;
  45.             presentParams.AutoDepthStencilFormat = DepthFormat.D16;
  46.             presentParams.EnableAutoDepthStencil = true;
  47.  
  48.             device = new Microsoft.DirectX.Direct3D.Device(0,
  49.                 Microsoft.DirectX.Direct3D.DeviceType.Hardware, this,
  50.                 CreateFlags.SoftwareVertexProcessing, presentParams);
  51.            
  52.             //device.RenderState.FillMode = FillMode.WireFrame;// siatka
  53.         }
  54.  
  55.         public void Initialization()
  56.         {
  57.             LoadHeightData();
  58.             VertaxDeclaration();
  59.             IndicesDeclaration();
  60.             CreateMesh();
  61.             CameraPositioning();
  62.         }
  63.  
  64.         public void InitializeKeyboard()
  65.         {
  66.             keyboard = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
  67.             keyboard.SetCooperativeLevel(this, CooperativeLevelFlags.Background
  68.                 | CooperativeLevelFlags.NonExclusive);
  69.             keyboard.Acquire();
  70.         }
  71.  
  72.         protected override void OnPaint(PaintEventArgs e)
  73.         {
  74.             device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
  75.                 Color.DarkSlateBlue, 1.0f, 0);
  76.             device.BeginScene();
  77.  
  78.             device.Transform.World = Matrix.Translation(-height / 2,
  79.                 - width / 2, 0) * Matrix.RotationZ(angle);
  80.  
  81.             int numSubSets = meshTerrain.GetAttributeTable().Length;
  82.             for (int i = 0; i < numSubSets; ++i)
  83.                 meshTerrain.DrawSubset(i);
  84.  
  85.             device.EndScene();
  86.             device.Present();
  87.  
  88.             this.Invalidate();
  89.             ReadKeyboard();
  90.         }
  91.  
  92.         private void CameraPositioning()
  93.         {
  94.             device.Transform.Projection =
  95.                 Matrix.PerspectiveFovLH((float)Math.PI / 4,
  96.                 this.Width / this.Height, 1f, 250f);
  97.             device.Transform.View = Matrix.LookAtLH(
  98.                 new Vector3(80, 0, 120), new Vector3(-20, 0, 0),
  99.                 new Vector3(0, 0, 1));
  100.  
  101.             device.RenderState.CullMode = Cull.None;
  102.             device.RenderState.Lighting = true;
  103.  
  104.             device.Lights[0].Type = LightType.Directional;
  105.             device.Lights[0].Diffuse = Color.White;
  106.             device.Lights[0].Direction = new Vector3(-0.5f, 0, -1f);
  107.             device.Lights[0].Enabled = true;
  108.         }
  109.  
  110.         private void VertaxDeclaration()
  111.         {
  112.             vertices = new CustomVertex.PositionColored[width*height];
  113.             for (int x = 0; x < width; x++)
  114.                 for (int y = 0; y < height; y++)
  115.                 {
  116.                     vertices[x + y * width].Position
  117.                         = new Vector3(x, y, heightData[x,y]);
  118.                     if (heightData[x, y] < minimumHeight
  119.                         + (maximumHeight - minimumHeight) / 4)
  120.                         vertices[x + y * width].Color
  121.                             = Color.Blue.ToArgb();
  122.                     else if (heightData[x, y] < minimumHeight
  123.                         + (maximumHeight - minimumHeight) * 2 / 4)
  124.                         vertices[x + y * width].Color
  125.                             = Color.Green.ToArgb();
  126.                     else if (heightData[x, y] < minimumHeight
  127.                         + (maximumHeight - minimumHeight) * 3 / 4)
  128.                         vertices[x + y * width].Color
  129.                             = Color.Brown.ToArgb();
  130.                     else vertices[x + y * width].Color
  131.                             = Color.White.ToArgb();
  132.                 }
  133.         }
  134.  
  135.         private void IndicesDeclaration()
  136.         {
  137.             indices = new short[(width - 1) * (height - 1) * 6];
  138.  
  139.             for (int x = 0; x < width - 1; x++)
  140.                 for (int y = 0; y < height - 1; y++)
  141.                 {
  142.                     indices[(x + y * (width - 1)) * 6] =
  143.                         (short) ((x + 1) + (y + 1) * width);
  144.                     indices[(x + y * (width - 1)) * 6 + 1] =
  145.                         (short) ((x + 1) + y * width);
  146.                     indices[(x + y * (width - 1)) * 6 + 2] =
  147.                         (short) (x + y * width);
  148.  
  149.                     indices[(x + y * (width - 1)) * 6 + 3] =
  150.                         (short) ((x + 1) + (y + 1) * width);
  151.                     indices[(x + y * (width - 1)) * 6 + 4] =
  152.                         (short) (x + y * width);
  153.                     indices[(x + y * (width - 1)) * 6 + 5] =
  154.                         (short) (x + (y + 1) * width);
  155.                 }
  156.         }
  157.  
  158.         private void CreateMesh()
  159.         {
  160.             meshTerrain = new Mesh((width - 1) * (height - 1) * 2,
  161.                 width * height, MeshFlags.Managed,
  162.                 CustomVertex.PositionColored.Format,
  163.                 device);
  164.  
  165.             meshTerrain.SetVertexBufferData(vertices, LockFlags.None);
  166.             meshTerrain.SetIndexBufferData(indices, LockFlags.None);
  167.  
  168.             int[] adjac = new int[meshTerrain.NumberFaces * 3];
  169.             meshTerrain.GenerateAdjacency(0.5f, adjac);
  170.             meshTerrain.OptimizeInPlace(MeshFlags.OptimizeVertexCache,
  171.                 adjac);
  172.  
  173.             meshTerrain = meshTerrain.Clone(meshTerrain.Options.Value,
  174.                 CustomVertex.PositionNormalColored.Format, device);
  175.             meshTerrain.ComputeNormals();
  176.         }
  177.  
  178.         private void LoadHeightData()
  179.         {
  180.             int offset;
  181.             byte dummy;
  182.  
  183.             FileStream fs = new FileStream("heightmap.bmp",
  184.                 FileMode.Open, FileAccess.Read);
  185.             BinaryReader r = new BinaryReader(fs);
  186.  
  187.             for (int i = 0; i < 10; i++)
  188.                 dummy = r.ReadByte();
  189.  
  190.             offset = r.ReadByte()
  191.                 + r.ReadByte() * 256
  192.                 + r.ReadByte() * 256 * 256
  193.                 + r.ReadByte() * 256 * 256 * 256;
  194.  
  195.             for (int i = 0; i < 4; i++)
  196.                 dummy = r.ReadByte();
  197.  
  198.             width = r.ReadByte()
  199.                 + r.ReadByte() * 256
  200.                 + r.ReadByte() * 256 * 256
  201.                 + r.ReadByte() * 256 * 256 * 256;
  202.  
  203.             height = r.ReadByte()
  204.                 + r.ReadByte() * 256
  205.                 + r.ReadByte() * 256 * 256
  206.                 + r.ReadByte() * 256 * 256 * 256;
  207.  
  208.             heightData = new int[width, height];
  209.  
  210.             for (int i = 0; i < (offset - 26); i++)
  211.                 dummy = r.ReadByte();
  212.  
  213.             for (int i = 0; i < height; i++)
  214.                 for (int y = 0; y < width; y++)
  215.                 {
  216.                     int h = (int)(r.ReadByte())
  217.                             + (int)(r.ReadByte())
  218.                             + (int)(r.ReadByte());
  219.                     h /= 8;
  220.                     heightData[width - 1 - y, height - 1 - i] = h;
  221.                     if (h < minimumHeight)
  222.                         minimumHeight = h;
  223.                     if (h > maximumHeight)
  224.                         maximumHeight = h;
  225.                 }
  226.         }
  227.  
  228.         private void LoadHeightDataFromRAW()
  229.         {
  230.             heightData = new int[width, height];
  231.             FileStream fs = new FileStream("heightdata.raw",
  232.                 FileMode.Open, FileAccess.Read);
  233.             BinaryReader r = new BinaryReader(fs);
  234.  
  235.             for (int i = 0; i < height; i++)
  236.                 for (int y = 0; y < width; y++)
  237.                 {
  238.                     int h = (int)(r.ReadByte() / 50);
  239.                     heightData[width - 1 - y, height - 1 - i] = h;
  240.                 }
  241.             fs.Close();
  242.         }
  243.  
  244.         private void ReadKeyboard()
  245.         {
  246.             KeyboardState keys = keyboard.GetCurrentKeyboardState();
  247.             if (keys[Key.Delete])
  248.                 angle += 0.03f;
  249.             if (keys[Key.Next])
  250.                 angle -= 0.03f;
  251.         }
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement