📄 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
{
public class ImageBackground
{
/// <summary>
/// Texture to be drawn with. Passed into the constructor.
/// </summary>
public Texture BackgroundTexture;
/// <summary>
/// This defines the shape of our background image and the texture of the
/// vertices.
/// </summary>
public CustomVertex.PositionTextured[] Vertices;
/// <summary>
/// Vertex buffer which is used by the display hardware
/// recieve details of what we want to draw.
/// </summary>
public VertexBuffer VertBuffer = null;
public ImageBackground(Device device, System.IO.Stream textureStream, float sz, float z)
{
BackgroundTexture = TextureLoader.FromStream(device, textureStream);
Vertices = new CustomVertex.PositionTextured[6];
Vertices[0] = new CustomVertex.PositionTextured(-sz, -sz, z, 0.0f, 1.0f);
Vertices[1] = new CustomVertex.PositionTextured(-sz, +sz, z, 0.0f, 0.0f);
Vertices[2] = new CustomVertex.PositionTextured(+sz, -sz, z, 1.0f, 1.0f);
Vertices[3] = new CustomVertex.PositionTextured(+sz, -sz, z, 1.0f, 1.0f);
Vertices[4] = new CustomVertex.PositionTextured(-sz, +sz, z, 0.0f, 0.0f);
Vertices[5] = new CustomVertex.PositionTextured(+sz, +sz, z, 1.0f, 0.0f);
VertBuffer = new VertexBuffer(
typeof(CustomVertex.PositionTextured), // type of the buffer
Vertices.Length, // holding vertices array
device, // for our device
Usage.WriteOnly, // never going to read it
CustomVertex.PositionTextured.Format, // source format
Pool.SystemMemory); // mobile 3D requires this
}
/// <summary>
/// Draw the textured image.
/// </summary>
/// <param name="device">target device to draw on</param>
public void Draw(Device device)
{
// Set the data in the vertex buffer to our triangles
VertBuffer.SetData(Vertices, 0, LockFlags.None);
// clear off any existing transformations
device.Transform.World = Matrix.Identity;
// Point the device at our vertex buffer
device.SetStreamSource(0, VertBuffer, 0);
// Assign the texture to the device.
device.SetTexture(0, BackgroundTexture);
// Ask the device to draw the contents of the buffer
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
// Stop using this texture
device.SetTexture(0, null);
}
}
ImageBackground imageBackground;
/// <summary>
/// Currently executing assembly. Cached because it is used
/// to load a lot of resources.
/// </summary>
readonly System.Reflection.Assembly assembly =
System.Reflection.Assembly.GetExecutingAssembly();
/// <summary>
/// Direct3D device we are going to draw on.
/// </summary>
Device device;
public CustomVertex.PositionColored[] Vert;
public VertexBuffer VertBuffer = null;
public void MakeBlock()
{
Vert = new CustomVertex.PositionColored[36];
int color = Color.Red.ToArgb();
/// Front face
Vert[0] = new CustomVertex.PositionColored(-0.5f, -0.5f, -0.5f, color);
Vert[1] = new CustomVertex.PositionColored(-0.5f, +0.5f, -0.5f, color);
Vert[2] = new CustomVertex.PositionColored(+0.5f, -0.5f, -0.5f, color);
Vert[3] = new CustomVertex.PositionColored(+0.5f, -0.5f, -0.5f, color);
Vert[4] = new CustomVertex.PositionColored(-0.5f, +0.5f, -0.5f, color);
Vert[5] = new CustomVertex.PositionColored(+0.5f, +0.5f, -0.5f, color);
color = Color.Green.ToArgb();
/// Right hand face
Vert[6] = new CustomVertex.PositionColored(+0.5f, -0.5f, -0.5f, color);
Vert[7] = new CustomVertex.PositionColored(+0.5f, +0.5f, -0.5f, color);
Vert[8] = new CustomVertex.PositionColored(+0.5f, -0.5f, +0.5f, color);
Vert[9] = new CustomVertex.PositionColored(+0.5f, +0.5f, -0.5f, color);
Vert[10] = new CustomVertex.PositionColored(+0.5f, +0.5f, +0.5f, color);
Vert[11] = new CustomVertex.PositionColored(+0.5f, -0.5f, +0.5f, color);
color = Color.Yellow.ToArgb();
/// Top face
Vert[12] = new CustomVertex.PositionColored(-0.5f, -0.5f, -0.5f, color);
Vert[13] = new CustomVertex.PositionColored(+0.5f, -0.5f, -0.5f, color);
Vert[14] = new CustomVertex.PositionColored(+0.5f, -0.5f, +0.5f, color);
Vert[15] = new CustomVertex.PositionColored(-0.5f, -0.5f, -0.5f, color);
Vert[16] = new CustomVertex.PositionColored(+0.5f, -0.5f, +0.5f, color);
Vert[17] = new CustomVertex.PositionColored(-0.5f, -0.5f, +0.5f, color);
color = Color.Blue.ToArgb();
/// left hand face
Vert[18] = new CustomVertex.PositionColored(-0.5f, -0.5f, -0.5f, color);
Vert[19] = new CustomVertex.PositionColored(-0.5f, -0.5f, +0.5f, color);
Vert[20] = new CustomVertex.PositionColored(-0.5f, +0.5f, -0.5f, color);
Vert[21] = new CustomVertex.PositionColored(-0.5f, +0.5f, -0.5f, color);
Vert[22] = new CustomVertex.PositionColored(-0.5f, -0.5f, +0.5f, color);
Vert[23] = new CustomVertex.PositionColored(-0.5f, +0.5f, +0.5f, color);
color = Color.Orange.ToArgb();
/// Back face
Vert[24] = new CustomVertex.PositionColored(-0.5f, -0.5f, +0.5f, color);
Vert[25] = new CustomVertex.PositionColored(+0.5f, -0.5f, +0.5f, color);
Vert[26] = new CustomVertex.PositionColored(-0.5f, +0.5f, +0.5f, color);
Vert[27] = new CustomVertex.PositionColored(+0.5f, -0.5f, +0.5f, color);
Vert[28] = new CustomVertex.PositionColored(+0.5f, +0.5f, +0.5f, color);
Vert[29] = new CustomVertex.PositionColored(-0.5f, +0.5f, +0.5f, color);
color = Color.Indigo.ToArgb();
/// Bottom face
Vert[30] = new CustomVertex.PositionColored(-0.5f, +0.5f, -0.5f, color);
Vert[31] = new CustomVertex.PositionColored(-0.5f, +0.5f, +0.5f, color);
Vert[32] = new CustomVertex.PositionColored(+0.5f, +0.5f, +0.5f, color);
Vert[33] = new CustomVertex.PositionColored(-0.5f, +0.5f, -0.5f, color);
Vert[34] = new CustomVertex.PositionColored(+0.5f, +0.5f, +0.5f, color);
Vert[35] = new CustomVertex.PositionColored(+0.5f, +0.5f, -0.5f, color);
VertBuffer = new VertexBuffer(
typeof(CustomVertex.PositionColored), // type of the buffer
Vert.Length, // holding vertices array
device, // for our device
Usage.WriteOnly, // never going to read it
CustomVertex.PositionColored.Format, // source format
Pool.SystemMemory); // mobile 3D requires this
}
Matrix scale1;
Matrix scale2;
Matrix rot1;
Matrix rot2;
Matrix pos1;
Matrix pos2;
public void Init()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
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.RenderState.Lighting = false;
device.Transform.View = Matrix.LookAtLH(
new Vector3(0.0f, 0.0f, -5.0f), // camera position
new Vector3(0.0f, 0.0f, 0.0f), // camera target
new Vector3(0.0f, 1.0f, 0.0f) // camera up vector
);
device.Transform.Projection = Matrix.PerspectiveFovLH(
(float)Math.PI / 4, // field of view
1.0f, // aspect ratio
1.0f, // near Z plane
100.0f // far Z plane
);
MakeBlock();
imageBackground = new ImageBackground(device,
assembly.GetManifestResourceStream("Block.Resources.Background.png"),
3.0f,
0.2f);
scale1 = Matrix.Scaling(2, 2, 2);
scale2 = Matrix.Scaling(0.5f, 0.5f, 0.5f);
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);
}
/// <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, Color.LightGray, 1.0f, 0);
device.BeginScene();
//imageBackground.Draw(device);
Yaw += YawSpeed;
Pitch += PitchSpeed;
Roll += RollSpeed;
Matrix rrot = Matrix.RotationYawPitchRoll(Yaw, Pitch, Roll);
// Set the data in the vertex buffer to our triangles
VertBuffer.SetData(Vert, 0, LockFlags.None);
// block 1
device.Transform.World = scale1 * rrot * rot1 * pos1;
// Point the device at our vertex buffer
device.SetStreamSource(0, VertBuffer, 0);
// Ask the device to draw the contents of the buffer
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
// block 2
device.Transform.World = scale2 * rrot* rot2 * pos2;
// Ask the device to draw the contents of the buffer
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
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 + -