- UID
- 961
- 帖子
- 4088
- 积分
- 13454
- 阅读权限
- 70
- 注册时间
- 2005-7-11
- 最后登录
- 2015-6-19
- 在线时间
- 5897 小时
|
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; //开发directx需要包含的两个命名空间
using Microsoft.DirectX.Direct3D;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
private Device device = null;
VertexBuffer vertexBuffer = null;
PresentParameters presentParameters = new PresentParameters();
bool pause = false;
Random rn = new Random();
public Form1()
{
InitializeComponent();
InitializeGraphics();
}
public bool InitializeGraphics()
{
try
{
//设置屏幕显示模式为窗口模式
presentParameters.Windowed = true;
// presentParameters.FullScreenRefreshRateInHz = 60;
//设置如何从后台缓冲区复制到前台缓冲区(SwapEffect.Discard表示缓冲区在显示后立即被舍弃,这样可以节省开销)
presentParameters.SwapEffect = SwapEffect.Discard;
//创建一个设备
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParameters);
//为设备释放订阅倳件处理
// device.DeviceReset += new System.EventHandler(this.OnResetDevice);
this.OnCreateDevice(device, null);
this.OnResetDevice(device, null);
pause = false;
return true;
}
catch (DirectXException)
{
return false;
}
}
public void OnCreateDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
//创建顶点缓冲,有个顶点
vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 18, dev, 0, CustomVertex.PositionColored.Format, Pool.Default);
//为创建顶点缓存订阅倳件处理
// vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);
this.OnCreateVertexBuffer(vertexBuffer, null);
}
public void OnResetDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
//关闭剔除模式,使我们能看见此四棱锥的前面和后面
dev.RenderState.CullMode = Cull.None;
// 关闭场景里的灯光,显示顶点自己的颜色
dev.RenderState.Lighting = false;
}
public void OnCreateVertexBuffer(object sender, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)sender;
CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vb.Lock(0, 0);
//四棱锥原始的个点
Vector3 vertex1 = new Vector3(25, 0, 100);
Vector3 vertex2 = new Vector3(0, 0, -25);
// Vector3 vertex3 = new Vector3(-25, 0, 0);
// Vector3 vertex4 = new Vector3(0, 0, 25);
Vector3 vertex5 = new Vector3(0, 25, 0);
//四棱锥中包含个三角形,所以要构造个点来绘制
verts[0].Position = vertex1;
verts[1].Position = vertex2;
verts[2].Position = vertex5;
/* verts[3].Position = vertex2;
verts[4].Position = vertex3;
verts[5].Position = vertex5;
verts[6].Position = vertex3;
verts[7].Position = vertex4;
verts[8].Position = vertex5;
verts[9].Position = vertex4;
verts[10].Position = vertex1;
verts[11].Position = vertex5;
verts[12].Position = vertex2;
verts[13].Position = vertex1;
verts[14].Position = vertex3;
verts[15].Position = vertex3;
verts[16].Position = vertex1;
verts[17].Position = vertex4;*/
//给每个点赋予随机颜色
for (int i = 0; i < 3; i++)
{
verts[i].Color = Color.FromArgb(SetColor(), SetColor(), SetColor()).ToArgb();
}
vb.Unlock();
}
public int SetColor()
{
int number = rn.Next(256);
return number;
}
private void SetupCamera()
{
//设置世界矩阵,根据系统运行时间而变化
// device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 3000.0f);
//设置摄像机的位置,它在z轴上-50处,看着原点,y轴为正方向
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -50f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));
//设置摄像机的视界,角度为度,看的最近为,看的最远处为.不再这个视界中的影像都不会被显示
device.Transform.Projection = Matrix.PerspectiveFovLH(((float)(float)Math.PI / 2), 1.0f, 10.0f, 200.0f);
}
public void Render()
{
if (device == null)
return;
if (pause)
return;
//背景设为绿色
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
//开始场景
device.BeginScene();
// 设置世界,视野和投影矩阵
SetupCamera();
// 给设备指定顶点缓存
device.SetStreamSource(0, vertexBuffer, 0);
//设置设备的顶点格式
device.VertexFormat = CustomVertex.PositionColored.Format;
//绘制图形,使用的方法为三角形列表,个数为个
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 6);
//结束场景
device.EndScene();
//更新场景
device.Present();
}
private void timer2_Tick(object sender, EventArgs e)
{
Render();
}
}
}
|
|