测试

测试

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1using System;
  2using System.Drawing;
  3using System.Windows.Forms;
  4using Microsoft.DirectX;
  5using Microsoft.DirectX.Direct3D;
  6
  7namespace Chapter11Code
  8ExpandedBlockStart.gifContractedBlock.gif{
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 10    /// Summary description for Form1.
 11    /// </summary>

 12    public class Form1 : System.Windows.Forms.Form
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 14        private Device device = null;
 15        private Mesh mesh = null;
 16        private Material[] meshMaterials;
 17        private Texture[] meshTextures;
 18
 19        private Effect effect = null;
 20        // Matrices
 21        private Matrix worldMatrix;
 22        private Matrix viewMatrix;
 23        private Matrix projMatrix;
 24
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 26        /// Required designer variable.
 27        /// </summary>

 28        private System.ComponentModel.Container components = null;
 29        private float angle = 0.0f;
 30
 31        public Form1()
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 33            //
 34            // Required for Windows Form Designer support
 35            //
 36            InitializeComponent();
 37
 38            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
 39        }

 40
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 42        /// We will initialize our graphics device here
 43        /// </summary>

 44        public bool InitializeGraphics()
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 46            // Set our presentation parameters
 47            PresentParameters presentParams = new PresentParameters();
 48
 49            presentParams.Windowed = true;
 50            presentParams.SwapEffect = SwapEffect.Discard;
 51            presentParams.AutoDepthStencilFormat = DepthFormat.D16;
 52            presentParams.EnableAutoDepthStencil = true;
 53
 54
 55            bool canDoShaders = true;
 56            // Does a hardware device support shaders?
 57            Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware);
 58            if ((hardware.VertexShaderVersion >= new Version(11)) &&
 59                (hardware.PixelShaderVersion >= new Version(11)))
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 61                // Default to software processing
 62                CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
 63
 64                // Use hardware if it's available
 65                if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
 66                    flags = CreateFlags.HardwareVertexProcessing;
 67
 68                // Use pure if it's available
 69                if (hardware.DeviceCaps.SupportsPureDevice)
 70                    flags |= CreateFlags.PureDevice;
 71
 72                // Yes, Create our device
 73                device = new Device(0, DeviceType.Hardware, this, flags, presentParams);
 74            }

 75            else
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 77                // No shader support
 78                canDoShaders = false;
 79
 80                // Create a reference device
 81                device = new Device(0, DeviceType.Reference, this
 82                    CreateFlags.SoftwareVertexProcessing, presentParams);
 83            }

 84
 85            // Create our effect
 86            //2005/07/11. TheZBuffer.com. Updated for June 2005 SDK
 87            //effect = Effect.FromFile(device, @"..\..\simple.fx", null, ShaderFlags.None, null);
 88            effect = Effect.FromFile(device, @"..\..\simple.fx"null"", ShaderFlags.None, null);
 89            effect.Technique = "TransformTexture";
 90
 91            // Store our project and view matrices
 92            projMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 4
 93                this.Width / this.Height, 1.0f10000.0f);
 94
 95            viewMatrix = Matrix.LookAtLH(new Vector3(0,0580.0f), new Vector3(), 
 96                new Vector3(0,1,0));
 97
 98            // Load our mesh
 99            LoadMesh(@"..\..\tiny.x");
100
101            return canDoShaders;
102        }

103
104        private void LoadMesh(string file)
105ExpandedSubBlockStart.gifContractedSubBlock.gif        {
106            ExtendedMaterial[] mtrl;
107
108            // Load our mesh
109            mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);
110
111            // If we have any materials, store them
112            if ((mtrl != null&& (mtrl.Length > 0))
113ExpandedSubBlockStart.gifContractedSubBlock.gif            {
114                meshMaterials = new Material[mtrl.Length];
115                meshTextures = new Texture[mtrl.Length];
116
117                // Store each material and texture
118                for (int i = 0; i < mtrl.Length; i++)
119ExpandedSubBlockStart.gifContractedSubBlock.gif                {
120                    meshMaterials[i] = mtrl[i].Material3D;
121                    if ((mtrl[i].TextureFilename != null&& (mtrl[i].TextureFilename != string.Empty))
122ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
123                        // We have a texture, try to load it
124                        meshTextures[i] = TextureLoader.FromFile(device, @"..\..\" + mtrl[i].TextureFilename);
125                    }

126                }

127            }

128        }

129
130        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
131ExpandedSubBlockStart.gifContractedSubBlock.gif        {
132            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f0);
133
134            device.BeginScene();
135
136            // Draw our Mesh
137            DrawMesh(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f
138                angle / (float)Math.PI / 4.0f0.0f0.0f0.0f);
139
140            device.EndScene();
141
142            device.Present();
143
144            this.Invalidate();
145        }

146
147        private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z)
148ExpandedSubBlockStart.gifContractedSubBlock.gif        {
149            angle += 0.01f;
150
151            worldMatrix = Matrix.RotationYawPitchRoll(yaw, pitch, roll) 
152                * Matrix.Translation(x, y, z);
153
154            Matrix worldViewProj = worldMatrix * viewMatrix * projMatrix;
155            effect.SetValue("WorldViewProj", worldViewProj);
156
157            int numPasses = effect.Begin(0);
158            for (int iPass = 0; iPass < numPasses; iPass++)
159ExpandedSubBlockStart.gifContractedSubBlock.gif            {
160                //2005/07/11. TheZBuffer.com. Updated for June 2005 SDK
161                //effect.Pass(iPass);
162                effect.BeginPass(iPass);
163                for (int i = 0; i < meshMaterials.Length; i++)
164ExpandedSubBlockStart.gifContractedSubBlock.gif                {
165                    device.SetTexture(0, meshTextures[i]);
166                    mesh.DrawSubset(i);
167                }

168                //2005/07/11. TheZBuffer.com. Updated for June 2005 SDK
169                //Added
170                effect.EndPass();
171            }

172            effect.End();
173        }

174
175        protected override void OnKeyPress(KeyPressEventArgs e)
176ExpandedSubBlockStart.gifContractedSubBlock.gif        {
177            switch (e.KeyChar)
178ExpandedSubBlockStart.gifContractedSubBlock.gif            {
179                case '1':
180                    effect.Technique = "TransformTexture";
181                    break;
182                case '2':
183                    effect.Technique = "TransformInverseTexture";
184                    break;
185                case '3':
186                    effect.Technique = "TransformTextureNoBlue";
187                    break;
188                case '4':
189                    effect.Technique = "TransformTextureOnlyBlue";
190                    break;
191            }

192            base.OnKeyPress (e);
193        }

194
195ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
196        /// Clean up any resources being used.
197        /// </summary>

198        protected override void Dispose( bool disposing )
199ExpandedSubBlockStart.gifContractedSubBlock.gif        {
200            if( disposing )
201ExpandedSubBlockStart.gifContractedSubBlock.gif            {
202                if (components != null
203ExpandedSubBlockStart.gifContractedSubBlock.gif                {
204                    components.Dispose();
205                }

206            }

207            base.Dispose( disposing );
208        }

209
210ContractedSubBlock.gifExpandedSubBlockStart.gif        Windows Form Designer generated code#region Windows Form Designer generated code
211ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
212        /// Required method for Designer support - do not modify
213        /// the contents of this method with the code editor.
214        /// </summary>

215        private void InitializeComponent()
216ExpandedSubBlockStart.gifContractedSubBlock.gif        {
217            this.components = new System.ComponentModel.Container();
218            this.Size = new Size(800,600);
219            this.Text = "Form1";
220        }

221        #endregion

222
223ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
224        /// The main entry point for the application.
225        /// </summary>

226        static void Main() 
227ExpandedSubBlockStart.gifContractedSubBlock.gif        {
228            using (Form1 frm = new Form1())
229ExpandedSubBlockStart.gifContractedSubBlock.gif            {
230                // Show our form and initialize our graphics engine
231                frm.Show();
232                if (!frm.InitializeGraphics())
233ExpandedSubBlockStart.gifContractedSubBlock.gif                {
234                    MessageBox.Show("Your card does not support shaders.  " +
235                        "This application will run in ref mode instead.");
236                }

237                Application.Run(frm);
238            }

239        }

240    }

241}

242

转载于:https://www.cnblogs.com/jiangshui/archive/2009/03/08/1406388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值