📄 blockform.cs
字号:
Vertices[23] = new CustomVertex.PositionNormalColored(
-0.5f, +0.5f, +0.5f, // position
-1.0f, 0.0f, 0.0f, // normal - looking left - negative X axis
colour);
colour = Color.Orange.ToArgb();
/// Back face
Vertices[24] = new CustomVertex.PositionNormalColored(
-0.5f, -0.5f, +0.5f, // position
0.0f, 0.0f, 1.0f, // normal - looking in - positive Z axis
colour);
Vertices[25] = new CustomVertex.PositionNormalColored(
+0.5f, -0.5f, +0.5f, // position
0.0f, 0.0f, 1.0f, // normal - looking in - positive Z axis
colour);
Vertices[26] = new CustomVertex.PositionNormalColored(
-0.5f, +0.5f, +0.5f, // position
0.0f, 0.0f, 1.0f, // normal - looking in - positive Z axis
colour);
colour = Color.White.ToArgb();
Vertices[27] = new CustomVertex.PositionNormalColored(
+0.5f, -0.5f, +0.5f, // position
0.0f, 0.0f, 1.0f, // normal - looking in - positive Z axis
colour);
Vertices[28] = new CustomVertex.PositionNormalColored(
+0.5f, +0.5f, +0.5f, // position
0.0f, 0.0f, 1.0f, // normal - looking in - positive Z axis
colour);
Vertices[29] = new CustomVertex.PositionNormalColored(
-0.5f, +0.5f, +0.5f, // position
0.0f, 0.0f, 1.0f, // normal - looking in - positive Z axis
colour);
colour = Color.Cyan.ToArgb();
/// Top face
Vertices[30] = new CustomVertex.PositionNormalColored(
-0.5f, +0.5f, -0.5f, // position
0.0f, 1.0f, 0.0f, // normal - looking up - positive Y axis
colour);
Vertices[31] = new CustomVertex.PositionNormalColored(
-0.5f, +0.5f, +0.5f, // position
0.0f, 1.0f, 0.0f, // normal - looking up - positive Y axis
colour);
Vertices[32] = new CustomVertex.PositionNormalColored(
+0.5f, +0.5f, +0.5f, // position
0.0f, 1.0f, 0.0f, // normal - looking up - positive Y axis
colour);
colour = Color.White.ToArgb();
Vertices[33] = new CustomVertex.PositionNormalColored(
-0.5f, +0.5f, -0.5f, // position
0.0f, 1.0f, 0.0f, // normal - looking up - positive Y axis
colour);
Vertices[34] = new CustomVertex.PositionNormalColored(
+0.5f, +0.5f, +0.5f, // position
0.0f, 1.0f, 0.0f, // normal - looking up - positive Y axis
colour);
Vertices[35] = new CustomVertex.PositionNormalColored(
+0.5f, +0.5f, -0.5f, // position
0.0f, 1.0f, 0.0f, // normal - looking up - positive Y axis
colour);
VertBuffer = new VertexBuffer(
typeof(CustomVertex.PositionNormalColored), // type of the buffer
Vertices.Length, // holding vertices array
device, // for our device
Usage.WriteOnly, // never going to read it
CustomVertex.PositionNormalColored.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;
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.RenderState.Lighting = true;
device.Lights[0].Diffuse = Color.Blue;
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.Lights[1].Diffuse = Color.White;
device.Lights[1].Type = LightType.Directional;
device.Lights[1].Direction = new Vector3(.5f, .5f, .5f);
device.Lights[1].Update();
device.Lights[1].Enabled = true;
device.RenderState.Ambient = Color.White;
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
);
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(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);
}
/// <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.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(Vertices, 0, LockFlags.None);
// block 1
device.Transform.World = scale1 * rot1 * rrot * 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 * rot2 * rrot * 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 updateTimer_Tick(object sender, EventArgs e)
{
Invalidate();
}
private void exitMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -