Chinaunix首页 | 论坛 | 博客
  • 博客访问: 588437
  • 博文数量: 208
  • 博客积分: 3286
  • 博客等级: 中校
  • 技术积分: 1780
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-24 20:38
文章分类

全部博文(208)

文章存档

2012年(7)

2011年(28)

2010年(21)

2009年(76)

2008年(65)

2007年(11)

我的朋友

分类: C/C++

2008-02-15 12:21:48

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
namespace WindowsTriangle
{
    public partial class Form1 : Form
    {
        private Device device = null;
        private float angle = 0.0f;
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
            PresentParameters para = new PresentParameters();
            para.Windowed = true;
            para.SwapEffect = SwapEffect.Discard;
 
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, para);
        }
        public void SetupCamera()
        {
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)this.Width / (float)this.Height, 1.0f, 100.0f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f), new Vector3(), new Vector3(0, 1, 0));
 
            //device.Transform.World = Matrix.RotationZ( Environment.TickCount / ( float ) Math.PI );
 
            device.Transform.World = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f), angle / ((float)Math.PI * 4.0f), angle / ((float)Math.PI * 6.0f)), angle / (float)Math.PI);
            angle += 0.1f;
 
            device.RenderState.CullMode = Cull.None;    //*1
            device.RenderState.Lighting = false;        //*2
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.Blue, 0.0f, 0);
 
            SetupCamera();
 
            CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
            verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
            verts[0].Color = Color.Aqua.ToArgb();
            verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
            verts[1].Color = Color.Black.ToArgb();
            verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
            verts[2].Color = Color.Purple.ToArgb();
            device.BeginScene();
            device.VertexFormat = CustomVertex.PositionColored.Format;
            device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
            device.EndScene();
            device.Present();
            this.Invalidate();
        }
    }
}
阅读(652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~