⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 一个Wince的小程序
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Runtime.InteropServices;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Microsoft.DirectX.Direct3D.Device device = null;
        private Earth mesh = null;
        private Matrix meshLoc;
        private bool meshLoaded = false;
        private cModel model = null;
        private CMouse mouse = null;
        private bool leftbuttondown = false;
        private float mousexloc;
        public void InitializeInput()
        {
            mouse = new CMouse(this);
        }

        private void UpdateInputState()
        {
            mouse.Update();
            if (mouse.LeftButtonDown)
            {
                if (leftbuttondown == false)
                {
                    mousexloc = 0.0f;
                    leftbuttondown = true;
                }
                else
                {
                    mousexloc = -mouse.X;
                }
            }
            else
            {
                leftbuttondown = false;
                mousexloc = 0.0f;
            }
        }

        public void Render()
        {
            UpdateInputState();
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkGray, 1.0f, 0);
            SetupCamera();
            device.BeginScene();
            model.Update((int)mousexloc);
            model.Light(ref device);
            model.Render(ref device);
            device.EndScene();
            device.Present();
        }


        public Form1()
        {
            InitializeComponent();
        }
        //贴过来的代码
        public void InitializeGraphics()
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Flip;
            presentParams.AutoDepthStencilFormat = DepthFormat.D16;
            presentParams.EnableAutoDepthStencil = true;
            device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
            meshLoc = Matrix.Identity;
            meshLoc.M41 = 2.0f;
            mesh = new Earth(ref device, meshLoc);
            if (mesh.LoadMesh(@"..\..\earth.x"))
            {
                meshLoaded = true;
            }

        }

        private void SetupCamera()
        {
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 1000.00f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, 20.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0, 1, 0));
            device.Lights[0].Type = LightType.Directional;
            device.Lights[0].Diffuse = Color.White;
            device.Lights[0].Direction = new Vector3(0, -1, -1);
            device.Lights[0].Update();
            device.Lights[0].Enabled = true;

        }

        [STAThread]
        static void Main()
        {
            using (Form1 EarthForm = new Form1())
            {
                EarthForm.InitializeGraphics();
                EarthForm.InitializeInput();
                EarthForm.Show();
                while (EarthForm.Created)
                {
                    EarthForm.Render();
                    Application.DoEvents();
                }
                EarthForm.Dispose();
            }
        }

       

        public void Render(Matrix worldTransform)
         {
              //把位置变为世界坐标
              meshDevice.Transform.World = Matrix.Multiply(locationOffset, worldTransform);
              //绘制网格
              for (int i = 0; i < mMaterials.Length; i++)
              {
                   meshDevice.Material = mMaterials[i];
                   meshDevice.SetTexture(0, mTextures[i]);
                   mMesh.DrawSubset(i);
              }
         }




        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -