managed DirectX9第一个3D程序

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

None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
using  Microsoft.DirectX;
None.gif
using  Microsoft.DirectX.Direct3D;
None.gif
using  System.Text;
None.gif
None.gif
namespace  DXTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Device device=null;//声明一个Device
InBlock.gif
        private float x=8.0f;
InBlock.gif        
private float y=0.0f;
InBlock.gif        
private float z=7.0f;
InBlock.gif
InBlock.gif        
private System.ComponentModel.Container components = null;
InBlock.gif        
private System.Windows.Forms.Button button1;
InBlock.gif        
private System.Windows.Forms.Button button2;
InBlock.gif        
private System.Windows.Forms.Button button3;
InBlock.gif        
private System.Windows.Forms.Button button4;
InBlock.gif
InBlock.gif        
public void InitGraph()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PresentParameters pp
=new PresentParameters();
InBlock.gif            pp.Windowed
=true;
InBlock.gif            pp.SwapEffect
=SwapEffect.Discard;
InBlock.gif
InBlock.gif            device
=new Device(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,pp);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected override void OnPaint(PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
base.OnPaint (e);
InBlock.gif
InBlock.gif            device.RenderState.Lighting
=true;//开灯
InBlock.gif
            device.RenderState.CullMode = Cull.None;//关闭剔除模式
InBlock.gif

InBlock.gif            device.Lights[
0].Type = LightType.Directional;//以下设置light
InBlock.gif
            device.Lights[0].Direction = new Vector3(
InBlock.gif                (
float)Math.Cos(Environment.TickCount / 250.0f),
InBlock.gif                
-7.0f,
InBlock.gif                (
float)Math.Sin(Environment.TickCount / 250.0f));
InBlock.gif            device.Lights[
0].Diffuse = System.Drawing.Color.White;
InBlock.gif            device.Lights[
0].Attenuation0 = 0.2f;
InBlock.gif            device.Lights[
0].Range = 1000.0f;
InBlock.gif            device.Lights[
0].Enabled = true;
InBlock.gif
InBlock.gif            device.Clear(ClearFlags.Target,System.Drawing.Color.CornflowerBlue,
1.0f,0);//以清除的方式来显示带有颜色的背景的form
InBlock.gif
InBlock.gif            
//CustomVertex.TransformedColored[] verts=new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored[3];
InBlock.gif            
//verts[0].Color=System.Drawing.Color.Red.ToArgb();
InBlock.gif            
//verts[0].Position=new Vector4(11f,50.0f,0.5f,1.0f);
InBlock.gif            
//verts[1].Color=System.Drawing.Color.White.ToArgb();
InBlock.gif            
//verts[1].Position=new Vector4(222f,10.0f,0.5f,56.0f);
InBlock.gif            
//verts[2].Color=System.Drawing.Color.Black.ToArgb();
InBlock.gif            
//verts[2].Position=new Vector4(this.Width/1.0f-20.0f,this.Height/1.0f-80.0f,2.5f,23.0f);
InBlock.gif
            
InBlock.gif            CustomVertex.PositionNormalColored[] verts2
=new Microsoft.DirectX.Direct3D.CustomVertex.PositionNormalColored[3];//定义带法线的坐标顶点
InBlock.gif
            verts2[0].Position=new Vector3(-3.0f,0.0f,0.0f); //中心点的坐标是(0,0,0)
InBlock.gif
            verts2[0].Normal=new Vector3(0.0f,1.0f,-1.0f);
InBlock.gif            verts2[
0].Color=System.Drawing.Color.Red.ToArgb();
InBlock.gif            verts2[
1].Position=new Vector3(0.0f,4.0f,0.0f);
InBlock.gif            verts2[
1].Normal=new Vector3(0.0f,-3.0f,-1.0f);
InBlock.gif            verts2[
1].Color=System.Drawing.Color.Black.ToArgb();
InBlock.gif            verts2[
2].Position=new Vector3(3.0f,0.0f,0.0f);
InBlock.gif            verts2[
2].Normal=new Vector3(02.0f,1.0f,-1.0f);
InBlock.gif            verts2[
2].Color=System.Drawing.Color.White.ToArgb();
InBlock.gif
InBlock.gif            
//镜头坐标
InBlock.gif
            device.Transform.View = Matrix.LookAtLH( 
InBlock.gif                
new Vector3( x,y,z), 
InBlock.gif                
new Vector3( 0.0f0.0f0.0f ), 
InBlock.gif                
new Vector3( 0.0f1.0f0.0f ) );
InBlock.gif            device.Transform.Projection 
= Matrix.PerspectiveFovLH(
InBlock.gif                (
float)Math.PI / 4
InBlock.gif                
1.0f
InBlock.gif                
1.0f
InBlock.gif                
100.0f );
InBlock.gif
InBlock.gif            
//产生旋转动作
InBlock.gif
            int  iTime  = Environment.TickCount % 1000;
InBlock.gif            
float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f+(float)Math.PI;
InBlock.gif            device.Transform.World 
= Matrix.RotationY( fAngle );
InBlock.gif
InBlock.gif            device.BeginScene();
//开始一个场景
InBlock.gif            
//device.VertexFormat=CustomVertex.TransformedColored.Format;
InBlock.gif
            device.VertexFormat=CustomVertex.PositionNormalColored.Format;
InBlock.gif            device.DrawUserPrimitives(PrimitiveType.TriangleList,
1,verts2);
InBlock.gif            device.EndScene();
InBlock.gif            device.Present();
InBlock.gif
InBlock.gif            
this.Invalidate();//使窗体失效
InBlock.gif
            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnResize(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnResize (e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

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

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

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Form1 frm
=new Form1();
InBlock.gif            frm.Show();
InBlock.gif            frm.InitGraph();
InBlock.gif            Application.Run(frm);
InBlock.gif            frm.ResizeRedraw
=true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button3_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            x
=x+1.0f;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            y
=y+1.0f;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button2_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            x
=x-1.0f;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button4_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            y
=y-1.0f;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值