📄 form1.cs
字号:
#define SAFE
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 Chapter8Code
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Device device = null;
private Mesh mesh = null;
private Material[] meshMaterials;
private Texture[] meshTextures;
private uint texColor = 0;
private bool randomTextureColor = false;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private float angle = 0.0f;
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;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;
// Create our device
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
// Load our mesh
LoadMesh(@"..\..\tiny.x");
}
private void FillTexture(Texture t, bool random)
{
SurfaceDescription s = t.GetLevelDescription(0);
Random r = new Random();
uint[,] data = (uint[,])t.LockRectangle(typeof(uint), 0,
LockFlags.None, s.Width, s.Height);
for (int i = 0; i < s.Width; i++)
{
for (int j = 0; j < s.Height; j++)
{
if (random)
{
data[i,j] = (uint)Color.FromArgb(
r.Next(byte.MaxValue),
r.Next(byte.MaxValue),
r.Next(byte.MaxValue)).ToArgb();
}
else
{
data[i,j] = texColor++;
if (texColor >= 0x00ffffff)
texColor = 0;
}
}
}
t.UnlockRectangle(0);
}
private unsafe void FillTextureUnsafe(Texture t, bool random)
{
SurfaceDescription s = t.GetLevelDescription(0);
Random r = new Random();
uint* pData = (uint*)t.LockRectangle(0,
LockFlags.None).InternalData.ToPointer();
for (int i = 0; i < s.Width; i++)
{
for (int j = 0; j < s.Height; j++)
{
if (random)
{
*pData = (uint)Color.FromArgb(
r.Next(byte.MaxValue),
r.Next(byte.MaxValue),
r.Next(byte.MaxValue)).ToArgb();
}
else
{
*pData = texColor++;
if (texColor >= 0x00ffffff)
texColor = 0;
}
pData++;
}
}
t.UnlockRectangle(0);
}
private void LoadMesh(string file)
{
ExtendedMaterial[] mtrl;
GraphicsStream adj;
// Load our mesh
mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out adj, out mtrl);
// If we have any materials, store them
if ((mtrl != null) && (mtrl.Length > 0))
{
meshMaterials = new Material[mtrl.Length];
meshTextures = new Texture[mtrl.Length];
// Store each material and texture
for (int i = 0; i < mtrl.Length; i++)
{
meshMaterials[i] = mtrl[i].Material3D;
meshTextures[i] = new Texture(device, 256, 256, 1, 0, Format.X8R8G8B8 ,Pool.Managed);
#if (UNSAFE)
FillTextureUnsafe(meshTextures[i], randomTextureColor);
#else
FillTexture(meshTextures[i], randomTextureColor);
#endif
}
}
}
private void SetupCamera()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 10000.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 580.0f), new Vector3(), new Vector3(0,1,0));
device.RenderState.Lighting = false;
if (device.DeviceCaps.TextureFilterCaps.SupportsMinifyAnisotropic)
device.SamplerState[0].MinFilter = TextureFilter.Anisotropic;
else if (device.DeviceCaps.TextureFilterCaps.SupportsMinifyLinear)
device.SamplerState[0].MinFilter = TextureFilter.Linear;
if (device.DeviceCaps.TextureFilterCaps.SupportsMagnifyAnisotropic)
device.SamplerState[0].MagFilter = TextureFilter.Anisotropic;
else if (device.DeviceCaps.TextureFilterCaps.SupportsMagnifyLinear)
device.SamplerState[0].MagFilter = TextureFilter.Linear;
foreach(Texture t in meshTextures)
#if (UNSAFE)
FillTextureUnsafe(t, randomTextureColor);
#else
FillTexture(t, randomTextureColor);
#endif
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
SetupCamera();
device.BeginScene();
// Draw our Mesh
DrawMesh(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, 0.0f, 0.0f);
device.EndScene();
device.Present();
this.Invalidate();
}
private void DrawMesh(float yaw, float pitch, float roll, float x, float y, float z)
{
angle += 0.11f;
device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z);
for (int i = 0; i < meshMaterials.Length; i++)
{
device.Material = meshMaterials[i];
device.SetTexture(0, meshTextures[i]);
mesh.DrawSubset(i);
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
randomTextureColor = !randomTextureColor;
}
/// <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 Size(800,600);
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 + -