-
Notifications
You must be signed in to change notification settings - Fork 355
/
BasicDemo.cs
executable file
·182 lines (146 loc) · 5.5 KB
/
BasicDemo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#region File Description
//-----------------------------------------------------------------------------
// BasicDemo.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SkinnedModel;
using SimpleAnimation;
using GeneratedGeometry;
#endregion
namespace XnaGraphicsDemo
{
/// <summary>
/// Enum controls what kind of lighting to use.
/// </summary>
public enum LightingMode
{
NoLighting,
OneVertexLight,
ThreeVertexLights,
ThreePixelLights,
}
/// <summary>
/// Demo shows how to use BasicEffect.
/// </summary>
class BasicDemo : MenuComponent
{
// Fields.
Model grid;
Tank tank = new Tank();
LightModeMenu lightMode;
BoolMenuEntry textureEnable;
float zoom = 1;
/// <summary>
/// Constructor.
/// </summary>
public BasicDemo(DemoGame game)
: base(game)
{
Entries.Add(textureEnable = new BoolMenuEntry("texture"));
Entries.Add(lightMode = new LightModeMenu());
Entries.Add(new MenuEntry { Text = "back", Clicked = delegate { Game.SetActiveMenu(0); } });
}
/// <summary>
/// Resets the menu state.
/// </summary>
public override void Reset()
{
lightMode.LightMode = LightingMode.ThreeVertexLights;
textureEnable.Value = true;
zoom = 1;
base.Reset();
}
/// <summary>
/// Loads content for this demo.
/// </summary>
protected override void LoadContent()
{
tank.Load(Game.Content);
grid = Game.Content.Load<Model>("grid");
}
/// <summary>
/// Updates the tank animation.
/// </summary>
public override void Update(GameTime gameTime)
{
tank.Animate(gameTime);
base.Update(gameTime);
}
/// <summary>
/// Draws the BasicEffect demo.
/// </summary>
public override void Draw(GameTime gameTime)
{
float time = (float)gameTime.TotalGameTime.TotalSeconds;
// Compute camera matrices.
Matrix rotation = Matrix.CreateRotationY(time * 0.1f);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
GraphicsDevice.Viewport.AspectRatio,
10,
20000);
Matrix view = Matrix.CreateLookAt(new Vector3(1500, 550, 0) * zoom + new Vector3(0, 150, 0),
new Vector3(0, 150, 0),
Vector3.Up);
// Draw the title.
DrawTitle("basic effect", new Color(192, 192, 192), new Color(156, 156, 156));
// Set render states.
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
// Draw the background grid.
grid.Draw(Matrix.CreateScale(1.5f) * rotation, view, projection);
// Draw the tank model.
tank.Draw(rotation, view, projection, lightMode.LightMode, textureEnable.Value);
base.Draw(gameTime);
}
/// <summary>
/// Dragging up and down on the menu background zooms in and out.
/// </summary>
protected override void OnDrag(Vector2 delta)
{
zoom = MathHelper.Clamp(zoom * (float)Math.Exp(delta.Y / 400), 0.4f, 6);
}
/// <summary>
/// Custom menu entry subclass for cycling through the different lighting options.
/// </summary>
class LightModeMenu : MenuEntry
{
public LightingMode LightMode = LightingMode.ThreeVertexLights;
public override void OnClicked()
{
if (LightMode == LightingMode.ThreePixelLights)
LightMode = 0;
else
LightMode++;
base.OnClicked();
}
public override string Text
{
get
{
switch (LightMode)
{
case LightingMode.NoLighting: return "no lighting";
case LightingMode.OneVertexLight: return "one vertex light";
case LightingMode.ThreeVertexLights: return "three vertex lights";
case LightingMode.ThreePixelLights: return "three pixel lights";
default:
throw new NotSupportedException();
}
}
set { }
}
}
}
}