zoukankan      html  css  js  c++  java
  • managed DirectX9第一个3D程序

    对3D的东西一直很感兴趣,今天安装了DirectX9.0 的SDK,看了一片文章,挺不错的:
    managed DirectX9(第一章) http://dev.csdn.net/article/62/62703.shtm
    还有SDK里面的文档,比葫芦画瓢地写出了第一个3D程序:
    MDX.JPG

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    using System.Text;

    namespace DXTest
    {
        
    public class Form1 : System.Windows.Forms.Form
        
    {
            
    private Device device=null;//声明一个Device
            private float x=8.0f;
            
    private float y=0.0f;
            
    private float z=7.0f;

            
    private System.ComponentModel.Container components = null;
            
    private System.Windows.Forms.Button button1;
            
    private System.Windows.Forms.Button button2;
            
    private System.Windows.Forms.Button button3;
            
    private System.Windows.Forms.Button button4;

            
    public void InitGraph()
            
    {
                PresentParameters pp
    =new PresentParameters();
                pp.Windowed
    =true;
                pp.SwapEffect
    =SwapEffect.Discard;

                device
    =new Device(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,pp);
            }

            
    protected override void OnPaint(PaintEventArgs e)
            
    {
                
                
    base.OnPaint (e);

                device.RenderState.Lighting
    =true;//开灯
                device.RenderState.CullMode = Cull.None;//关闭剔除模式

                device.Lights[
    0].Type = LightType.Directional;//以下设置light
                device.Lights[0].Direction = new Vector3(
                    (
    float)Math.Cos(Environment.TickCount / 250.0f),
                    
    -7.0f,
                    (
    float)Math.Sin(Environment.TickCount / 250.0f));
                device.Lights[
    0].Diffuse = System.Drawing.Color.White;
                device.Lights[
    0].Attenuation0 = 0.2f;
                device.Lights[
    0].Range = 1000.0f;
                device.Lights[
    0].Enabled = true;

                device.Clear(ClearFlags.Target,System.Drawing.Color.CornflowerBlue,
    1.0f,0);//以清除的方式来显示带有颜色的背景的form

                
    //CustomVertex.TransformedColored[] verts=new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[3];
                
    //verts[0].Color=System.Drawing.Color.Red.ToArgb();
                
    //verts[0].Position=new Vector4(11f,50.0f,0.5f,1.0f);
                
    //verts[1].Color=System.Drawing.Color.White.ToArgb();
                
    //verts[1].Position=new Vector4(222f,10.0f,0.5f,56.0f);
                
    //verts[2].Color=System.Drawing.Color.Black.ToArgb();
                
    //verts[2].Position=new Vector4(this.Width/1.0f-20.0f,this.Height/1.0f-80.0f,2.5f,23.0f);
                
                CustomVertex.PositionNormalColored[] verts2
    =new Microsoft.DirectX.Direct3D.CustomVertex.PositionNormalColored[3];//定义带法线的坐标顶点
                verts2[0].Position=new Vector3(-3.0f,0.0f,0.0f); //中心点的坐标是(0,0,0)
                verts2[0].Normal=new Vector3(0.0f,1.0f,-1.0f);
                verts2[
    0].Color=System.Drawing.Color.Red.ToArgb();
                verts2[
    1].Position=new Vector3(0.0f,4.0f,0.0f);
                verts2[
    1].Normal=new Vector3(0.0f,-3.0f,-1.0f);
                verts2[
    1].Color=System.Drawing.Color.Black.ToArgb();
                verts2[
    2].Position=new Vector3(3.0f,0.0f,0.0f);
                verts2[
    2].Normal=new Vector3(02.0f,1.0f,-1.0f);
                verts2[
    2].Color=System.Drawing.Color.White.ToArgb();

                
    //镜头坐标
                device.Transform.View = Matrix.LookAtLH( 
                    
    new Vector3( x,y,z), 
                    
    new Vector3( 0.0f0.0f0.0f ), 
                    
    new Vector3( 0.0f1.0f0.0f ) );
                device.Transform.Projection 
    = Matrix.PerspectiveFovLH(
                    (
    float)Math.PI / 4
                    
    1.0f
                    
    1.0f
                    
    100.0f );

                
    //产生旋转动作
                int  iTime  = Environment.TickCount % 1000;
                
    float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f+(float)Math.PI;
                device.Transform.World 
    = Matrix.RotationY( fAngle );

                device.BeginScene();
    //开始一个场景
                
    //device.VertexFormat=CustomVertex.TransformedColored.Format;
                device.VertexFormat=CustomVertex.PositionNormalColored.Format;
                device.DrawUserPrimitives(PrimitiveType.TriangleList,
    1,verts2);
                device.EndScene();
                device.Present();

                
    this.Invalidate();//使窗体失效
                
            }


            
    protected override void OnResize(EventArgs e)
            
    {
                
    base.OnResize (e);
            }



            
    public Form1()
            
    {
                InitializeComponent();
                
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
            }


            
    protected override void Dispose( bool disposing )
            
    {
                
    if( disposing )
                
    {
                    
    if (components != null
                    
    {
                        components.Dispose();
                    }

                }

                
    base.Dispose( disposing );
            }



            
    #region Windows 窗体设计器生成的代码
            
    /// <summary>
            
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
            
    /// 此方法的内容。
            
    /// </summary>

            private void InitializeComponent()
            
    {
                
    this.button1 = new System.Windows.Forms.Button();
                
    this.button2 = new System.Windows.Forms.Button();
                
    this.button3 = new System.Windows.Forms.Button();
                
    this.button4 = new System.Windows.Forms.Button();
                
    this.SuspendLayout();
                
    // 
                
    // button1
                
    // 
                this.button1.Location = new System.Drawing.Point(408);
                
    this.button1.Name = "button1";
                
    this.button1.Size = new System.Drawing.Size(3223);
                
    this.button1.TabIndex = 0;
                
    this.button1.Text = "^";
                
    this.button1.Click += new System.EventHandler(this.button1_Click);
                
    // 
                
    // button2
                
    // 
                this.button2.Location = new System.Drawing.Point(832);
                
    this.button2.Name = "button2";
                
    this.button2.Size = new System.Drawing.Size(3223);
                
    this.button2.TabIndex = 1;
                
    this.button2.Text = "<-";
                
    this.button2.Click += new System.EventHandler(this.button2_Click);
                
    // 
                
    // button3
                
    // 
                this.button3.Location = new System.Drawing.Point(7232);
                
    this.button3.Name = "button3";
                
    this.button3.Size = new System.Drawing.Size(3223);
                
    this.button3.TabIndex = 2;
                
    this.button3.Text = "->";
                
    this.button3.Click += new System.EventHandler(this.button3_Click);
                
    // 
                
    // button4
                
    // 
                this.button4.Location = new System.Drawing.Point(4056);
                
    this.button4.Name = "button4";
                
    this.button4.Size = new System.Drawing.Size(3223);
                
    this.button4.TabIndex = 3;
                
    this.button4.Text = "!";
                
    this.button4.Click += new System.EventHandler(this.button4_Click);
                
    // 
                
    // Form1
                
    // 
                this.AutoScaleBaseSize = new System.Drawing.Size(614);
                
    this.ClientSize = new System.Drawing.Size(292266);
                
    this.Controls.Add(this.button4);
                
    this.Controls.Add(this.button3);
                
    this.Controls.Add(this.button2);
                
    this.Controls.Add(this.button1);
                
    this.Name = "Form1";
                
    this.Text = "Form1";
                
    this.Load += new System.EventHandler(this.Form1_Load);
                
    this.ResumeLayout(false);

            }

            
    #endregion


            [STAThread]
            
    static void Main() 
            
    {
                Form1 frm
    =new Form1();
                frm.Show();
                frm.InitGraph();
                Application.Run(frm);
                frm.ResizeRedraw
    =true;
            }


            
    private void button3_Click(object sender, System.EventArgs e)
            
    {
                x
    =x+1.0f;
            }


            
    private void button1_Click(object sender, System.EventArgs e)
            
    {
                y
    =y+1.0f;
            }


            
    private void button2_Click(object sender, System.EventArgs e)
            
    {
                x
    =x-1.0f;
            }


            
    private void button4_Click(object sender, System.EventArgs e)
            
    {
                y
    =y-1.0f;
            }


            
    private void Form1_Load(object sender, System.EventArgs e)
            
    {
            
            }

        }

    }

  • 相关阅读:
    python冲刺(5)列表声称式
    python冲刺(4)切片 等
    python冲刺(3)函数 等
    python冲刺(2)
    python冲刺(1)
    redis初步(1)
    php连接Oracle的时候遇到的编码集问题
    redis初步
    php 命名空间
    指向字符数组的指针与指向整型数组的指针
  • 原文地址:https://www.cnblogs.com/caca/p/203898.html
Copyright © 2011-2022 走看看