1

问候

我用这个方程生成了一个地形:

H(x,y) = (abs(sin(x * y)) + abs(sin(0,2 * x) + sin(0,4 * y)) + abs(cos(0,12 * x) + cos(0,47 * y))) * e^(0.005*(x+y))

现在,这给了我一个混合特征尺寸和一个不错的坡度。当我使用 scilab 绘制它时,这很好用。

我试图在 ac# 应用程序中导入它。

地形是在以下代码中创建的:

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Collections.Generic;

namespace Kamera_eins
{

    public partial class Terrain : Form

    {
        public double[,] DTM;
        string response ;

        public Terrain()
        {

            InitializeComponent();
            response = "";
            DTM =  new double[2048/4,2048/4];


        }

        public void BoxTheSky()
        {


        }

        public void BoxTheLand()
        {

            mesh();

            surf();
        }
        public void begin()
        {

        }

        public void mesh()
        {
            response = "";
            int i = new int();
            int j = new int();
            i = 0;
            j = 0;

            for (i=0;i<2048/4 ;i++ ) {
                for (j=0;j<2048/4 ;j++ ) {
                    DTM[i,j] = Math.Abs (Math.Sin (j*i)) + Math.Abs(Math.Sin(0.2*i) * Math.Sin(0.4*j) ) + Math.Abs(Math.Cos(0.12* i) * Math.Cos(0.47*j));
                    DTM[i,j] = Math.Pow(Math.E, (0.012* (i + j)));


                }
            }

            response = "DTM  mesh ready"; 


        }

        public void surf()
        {

        }
    }
    }

这保存在一个名为 terrain.cs 的文件中,我将其设为 winform,因为我计划添加一个简单的文本框,稍后我可以在其中制作某种过程的实时日志。

现在,有另一个文件,在那个文件中,打算显示这个地形。第二个文件如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.IO;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;


namespace Kamera_eins
{
    public class viewport : Form
    {
        public Microsoft.DirectX.Direct3D.Device device = null;
        public PresentParameters presentparameter = new PresentParameters();
        public bool device_exists;

        public bool show;

        public int HEIGHT;
        public int WIDTH;

        public string paintDesc;


        private float angle ;
        private CustomVertex.PositionColored[] vertices;
        public double[,] heightData;
        private int[] indices;
        private IndexBuffer ib;
        private VertexBuffer vb;
         private Microsoft.DirectX.DirectInput.Device keyb;

        //public 


        public viewport()
        {
            this.ClientSize = new System.Drawing.Size(600, 600);
            this.Text = "Terrain viewport";

            WIDTH = 2048 / 4;
            HEIGHT = 2048 / 4;

            heightData = new double[HEIGHT,WIDTH];


            keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
             keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
             keyb.Acquire();

            presentparameter.Windowed = true;
            presentparameter.SwapEffect = SwapEffect.Discard;
            presentparameter.AutoDepthStencilFormat = DepthFormat.D16;
            presentparameter.EnableAutoDepthStencil = true;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);

            try {

            device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentparameter);
            device.DeviceLost += new EventHandler(this.InvalidateDeviceObjects);
            device.DeviceReset += new EventHandler(this.RestoreDeviceObjects);
            device.Disposing += new EventHandler(this.DeleteDeviceObjects);
            device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);

            device_exists = true;


            } catch (Exception DirectException) {
            device_exists = false;  
            }



        }

        private void setcamera()
        {
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
            device.Transform.View = Matrix.LookAtLH (new Vector3(0, 0, 100), new Vector3(0, 0, 0) , new Vector3(0,0,1) );
            device.RenderState.Lighting = false;
            device.RenderState.FillMode = FillMode.WireFrame;
            device.RenderState.CullMode = Cull.None;
        }


        public void declareVertex()
        {
            vb = new VertexBuffer(typeof(CustomVertex.PositionColored), HEIGHT*WIDTH, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
            vertices = new CustomVertex.PositionColored[HEIGHT*WIDTH];

            for (int x=0;x< WIDTH;x++)            {

                for (int y=0; y< HEIGHT;y++)                {
                    vertices[x + y * WIDTH].Position = new Vector3(x, y, (float)heightData[x,y]);
                    int r = Convert.ToInt32(205 * heightData[x,y] / 200 );
                    if(r>254)
                        r = 254;

                    vertices[x + y * WIDTH].Color =  Color.FromArgb( r , 120 , 120).ToArgb();

                }
            }

            vb.SetData(vertices, 0, LockFlags.None);

        }

        public void declareIndex()
        {
            ib = new IndexBuffer(typeof(int), (WIDTH-1)*(HEIGHT-1)*6, device, Usage.WriteOnly, Pool.Default);
            indices = new int[(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] = (x+1)+(y+1)*WIDTH;
                    indices[(x+y*(WIDTH-1))*6+1] = (x+1)+y*WIDTH;
                    indices[(x+y*(WIDTH-1))*6+2] = x+y*WIDTH;

                    indices[(x+y*(WIDTH-1))*6+3] = (x+1)+(y+1)*WIDTH;
                    indices[(x+y*(WIDTH-1))*6+4] = x+y*WIDTH;
                    indices[(x+y*(WIDTH-1))*6+5] = x+(y+1)*WIDTH;
                }
            }
            ib.SetData(indices, 0, LockFlags.None);
        }



         protected override void Dispose (bool disposing)
         {

             base.Dispose(disposing);
             MessageBox.Show("");
         }


        protected virtual void InvalidateDeviceObjects(object sender, EventArgs e)
        {
        }

        protected virtual void RestoreDeviceObjects(object sender, EventArgs e)
        {
        }

        protected virtual void DeleteDeviceObjects(object sender, EventArgs e)
        {
        }

        protected virtual void EnvironmentResizing(object sender, CancelEventArgs e)
        {
        }

        public void run()
        {
            while(this.Created)
            {
                render();
                setcamera();


                // optional: loading the height using functional call:
                //  loadheight();

                Application.DoEvents();
            }
        }


        public void render()
        {
            if (device != null)
            {
                device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
                device.BeginScene();

                //display terrain
                device.VertexFormat = CustomVertex.PositionColored.Format;
            device.SetStreamSource(0, vb, 0);
            device.Indices = ib;

            device.Transform.World = Matrix.Translation(-HEIGHT/2, -WIDTH/2, 0)*Matrix.RotationZ(angle) ;
            device.DrawIndexedPrimitives(PrimitiveType.TriangleFan, 0, 0, WIDTH*HEIGHT, 0, indices.Length/3);






                //turn off lights now

                device.EndScene();
                device.Present();
                this.Invalidate();
                readkeyboard();

            }
        }

        void readkeyboard()
        {

             KeyboardState keys = keyb.GetCurrentKeyboardState();
            if (keys[Key.Delete])
            {
                angle+=0.03f;
            }
            if (keys[Key.Next])
            {
                angle-=0.03f;
            }
        }

        public void openport()
        {




        }



            protected override void OnPaint(PaintEventArgs e)
        {


                render();
                setcamera();

        }


    }
}

现在,还有第三个文件调用世界创建和显示:

void MainFormLoad(object sender, EventArgs e)
        {
            world = new World();
            world.setterrain();
}

surf 和 box-somthing 函数还没有做任何事情。

我现在得到的只是一个黑色窗口(device.clear(...) 部分) - 我试图调整相机.. 没有成功

请帮忙,我想在窗口中显示地形....

4

0 回答 0