📄 form1.cs
字号:
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;
namespace Chapter4Code
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Device device = null;
private VertexBuffer vb = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private const int NumberItems = 12;
private bool needRecreate = false;
// Timing information
private static readonly int InitialTickCount = System.Environment.TickCount;
#region Random Number Generator
private static Random rnd = new Random();
private static int rndTick = System.Environment.TickCount;
public Random Rnd
{
get
{
if ((System.Environment.TickCount - rndTick) > 5000)
{
rndTick = System.Environment.TickCount;
rnd = new Random();
}
return rnd;
}
}
#endregion
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
/// <summary>
/// We will initialize our graphics device here
/// </summary>
public void InitializeGraphics()
{
// Set our presentation parameters
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
// Create our device
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), NumberItems, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
vb.Created += new EventHandler(this.OnVertexBufferCreate);
OnVertexBufferCreate(vb, null);
}
private void OnVertexBufferCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;
// Create a list of vertices equal to the number of items
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[NumberItems];
// Randomly select the position of this vertex.. Keep it within a range of 5.
for(int i = 0; i < NumberItems; i++)
{
float xPos = (float)(Rnd.NextDouble() * 5.0f) - (float)(Rnd.NextDouble() * 5.0f);
float yPos = (float)(Rnd.NextDouble() * 5.0f) - (float)(Rnd.NextDouble() * 5.0f);
float zPos = (float)(Rnd.NextDouble() * 5.0f) - (float)(Rnd.NextDouble() * 5.0f);
verts[i].SetPosition(new Vector3(xPos, yPos, zPos));
verts[i].Color = RandomColor.ToArgb();
}
buffer.SetData(verts, 0, LockFlags.None);
}
private Color RandomColor
{
get
{
if (Rnd.Next(500) > 250)
{
// Get all of the properties
System.Reflection.PropertyInfo[] properties = typeof(Color).GetProperties();
while (true)
{
int index = Rnd.Next(properties.Length);
try
{
Color c = (Color)properties[index].GetGetMethod().Invoke(null, null);
// If this succeeds, this is a color, return it
return c;
}
catch
{
// Must not have been a color, just continue
}
}
}
else
{
// Don't pick a standard color, pick a random color from argb
return Color.FromArgb(Rnd.Next(byte.MaxValue), Rnd.Next(byte.MaxValue), Rnd.Next(byte.MaxValue));
}
}
}
private void SetupCamera()
{
device.RenderState.CullMode = Cull.None;
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 15.0f), new Vector3(), new Vector3(0,1,0));
device.RenderState.Lighting = false;
device.RenderState.PointSize = 3.0f;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
SetupCamera();
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, vb, 0);
// We will decide what to draw based on primitives
int index = ((System.Environment.TickCount - InitialTickCount) / 2000) % 6;
switch (index)
{
case 0: // Points
device.DrawPrimitives(PrimitiveType.PointList, 0, NumberItems);
if (needRecreate)
{
// After the primitives have been drawn, recreate a new set
OnVertexBufferCreate(vb, null);
needRecreate = false;
}
break;
case 1: // LineList
device.DrawPrimitives(PrimitiveType.LineList, 0, NumberItems / 2);
needRecreate = true;
break;
case 2: // LineStrip
device.DrawPrimitives(PrimitiveType.LineStrip, 0, NumberItems - 1);
break;
case 3: // TriangleList
device.DrawPrimitives(PrimitiveType.TriangleList, 0, NumberItems / 3);
break;
case 4: // TriangleStrip
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, NumberItems - 2);
break;
case 5: // TriangleFan
device.DrawPrimitives(PrimitiveType.TriangleFan, 0, NumberItems - 2);
break;
}
device.EndScene();
device.Present();
this.Invalidate();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
using (Form1 frm = new Form1())
{
// Show our form and initialize our graphics engine
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -