📄 blockform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.DirectX;
using Microsoft.WindowsMobile.DirectX.Direct3D;
namespace Block
{
public partial class BlockForm : Form
{
/// <summary>
/// Direct3D device we are going to draw on.
/// </summary>
Device device;
public CustomVertex.PositionNormalColored[] Vertices;
public VertexBuffer VertBuffer = null;
Matrix scale1;
Matrix scale2;
Matrix rot1;
Matrix rot2;
Matrix pos1;
Matrix pos2;
Mesh box;
Mesh sphere;
Material boxMaterial;
Material sphereMaterial;
public void Init()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.EnableAutoDepthStencil = true;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
device = new Device(
0, // device number 0
DeviceType.Default, // default configuration
this, // reference to the parent window
CreateFlags.None, // no special creation flags
presentParams); // the presentation parameters
device.DeviceReset += new EventHandler(RestoreDeviceObjects);
RestoreDeviceObjects(device, EventArgs.Empty);
}
/// <summary>
/// Ratio for the currently active display. Used when changing the viewing
/// position.
/// </summary>
private float aspectRatio;
void RestoreDeviceObjects(System.Object sender,
System.EventArgs e)
{
device.RenderState.Lighting = true;
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Direction = new Vector3(-.5f, -.5f, .5f);
device.Lights[0].Update();
device.Lights[0].Enabled = true;
device.RenderState.Ambient = Color.Gray;
device.Transform.View = Matrix.LookAtLH(
new Vector3(0.0f, 0.0f, -3.0f), // camera position
new Vector3(0.0f, 0.0f, 0.0f), // camera target
new Vector3(0.0f, 1.0f, 0.0f) // camera up vector
);
aspectRatio =
((float)device.PresentationParameters.BackBufferWidth) /
device.PresentationParameters.BackBufferHeight;
device.Transform.Projection = Matrix.PerspectiveFovLH(
(float)Math.PI / 4, // field of view
aspectRatio, // aspect ratio
1.0f, // near Z plane
100.0f // far Z plane
);
scale1 = Matrix.Scaling(0.6f, 0.6f, 0.6f);
scale2 = Matrix.Scaling(0.6f, 0.6f, 0.6f);
rot1 = Matrix.RotationYawPitchRoll(0.5f, 0.5f, 0.5f);
rot2 = Matrix.RotationYawPitchRoll(-0.5f, -0.5f, -0.5f);
pos1 = Matrix.Translation(-0.6f, 0.6f, 0);
pos2 = Matrix.Translation(0.6f, -0.6f, 0);
box = Mesh.Box(device, 1, 1, 1);
boxMaterial = new Material();
boxMaterial.Ambient = Color.Red;
boxMaterial.Diffuse = Color.Red;
sphere = Mesh.Sphere(device, 1, 18, 18);
sphereMaterial = new Material();
sphereMaterial.Ambient = Color.Blue;
sphereMaterial.Diffuse = Color.Blue;
}
/// <summary>
/// The yaw value for our segment to animate the tumble
/// </summary>
public float Yaw;
/// <summary>
/// Change value for the yaw. Set at random when the
/// segment is constructed.
/// </summary>
public float YawSpeed = 0.07f;
/// <summary>
/// The pitch value for our segment to animate the tumble
/// </summary>
public float Pitch;
/// <summary>
/// Change value for the pitch. Set at random when the
/// segment is constructed.
/// </summary>
public float PitchSpeed = 0.05f;
/// <summary>
/// The roll value for our segment to animate the tumble
/// </summary>
public float Roll;
/// <summary>
/// Change value for the roll. Set at random when the
/// segment is constructed.
/// </summary>
public float RollSpeed = 0.1f;
private void Render()
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
device.BeginScene();
Yaw += YawSpeed;
Pitch += PitchSpeed;
Roll += RollSpeed;
Matrix rrot = Matrix.RotationYawPitchRoll(Yaw, Pitch, Roll);
// box
device.Transform.World = scale1 * rot1 * rrot * pos1;
device.Material = boxMaterial;
box.DrawSubset(0);
// sphere
device.Transform.World = scale2 * rot2 * rrot * pos2;
device.Material = sphereMaterial;
sphere.DrawSubset(0);
device.EndScene();
device.Present();
}
#region Event Handlers
protected override void OnPaintBackground(PaintEventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
Render();
}
#endregion
public BlockForm()
{
InitializeComponent();
}
private void exitMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void updateTimer_Tick(object sender, EventArgs e)
{
Invalidate();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -