-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathGame.cs
executable file
·227 lines (165 loc) · 7.3 KB
/
Game.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#region File Description
//-----------------------------------------------------------------------------
// Game.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion
namespace LensFlare
{
/// <summary>
/// Sample showing how to implement a lensflare effect, using occlusion
/// queries to hide the flares when the sun is hidden behind the landscape.
/// </summary>
public class LensFlareGame : Microsoft.Xna.Framework.Game
{
#region Fields
GraphicsDeviceManager graphics;
KeyboardState currentKeyboardState = new KeyboardState();
GamePadState currentGamePadState = new GamePadState();
Vector3 cameraPosition = new Vector3(-200, 30, 30);
Vector3 cameraFront = new Vector3(1, 0, 0);
Model terrain;
LensFlareComponent lensFlare;
#endregion
#region Initialization
public LensFlareGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Create and add the lensflare component.
lensFlare = new LensFlareComponent(this);
Components.Add(lensFlare);
}
/// <summary>
/// Load your graphics content.
/// </summary>
protected override void LoadContent()
{
terrain = Content.Load<Model>("terrain");
}
#endregion
#region Update and Draw
/// <summary>
/// Allows the game to run logic.
/// </summary>
protected override void Update(GameTime gameTime)
{
HandleInput();
UpdateCamera(gameTime);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Compute camera matrices.
Matrix view = Matrix.CreateLookAt(cameraPosition,
cameraPosition + cameraFront,
Vector3.Up);
float aspectRatio = GraphicsDevice.Viewport.AspectRatio;
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
aspectRatio,
0.1f, 500);
// Draw the terrain.
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
foreach (ModelMesh mesh in terrain.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = Matrix.Identity;
effect.View = view;
effect.Projection = projection;
effect.LightingEnabled = true;
effect.DiffuseColor = new Vector3(1f);
effect.AmbientLightColor = new Vector3(0.5f);
effect.DirectionalLight0.Enabled = true;
effect.DirectionalLight0.DiffuseColor = Vector3.One;
effect.DirectionalLight0.Direction = lensFlare.LightDirection;
effect.FogEnabled = true;
effect.FogStart = 200;
effect.FogEnd = 500;
effect.FogColor = Color.CornflowerBlue.ToVector3();
}
mesh.Draw();
}
// Tell the lensflare component where our camera is positioned.
lensFlare.View = view;
lensFlare.Projection = projection;
base.Draw(gameTime);
}
#endregion
#region Handle Input
/// <summary>
/// Handles input for quitting the game.
/// </summary>
private void HandleInput()
{
currentKeyboardState = Keyboard.GetState();
currentGamePadState = GamePad.GetState(PlayerIndex.One);
// Check for exit.
if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
currentGamePadState.Buttons.Back == ButtonState.Pressed)
{
Exit();
}
}
/// <summary>
/// Handles camera input.
/// </summary>
private void UpdateCamera(GameTime gameTime)
{
float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
// Check for input to rotate the camera.
float pitch = -currentGamePadState.ThumbSticks.Right.Y * time * 0.001f;
float turn = -currentGamePadState.ThumbSticks.Right.X * time * 0.001f;
if (currentKeyboardState.IsKeyDown(Keys.Up))
pitch += time * 0.001f;
if (currentKeyboardState.IsKeyDown(Keys.Down))
pitch -= time * 0.001f;
if (currentKeyboardState.IsKeyDown(Keys.Left))
turn += time * 0.001f;
if (currentKeyboardState.IsKeyDown(Keys.Right))
turn -= time * 0.001f;
Vector3 cameraRight = Vector3.Cross(Vector3.Up, cameraFront);
Vector3 flatFront = Vector3.Cross(cameraRight, Vector3.Up);
Matrix pitchMatrix = Matrix.CreateFromAxisAngle(cameraRight, pitch);
Matrix turnMatrix = Matrix.CreateFromAxisAngle(Vector3.Up, turn);
Vector3 tiltedFront = Vector3.TransformNormal(cameraFront, pitchMatrix *
turnMatrix);
// Check angle so we can't flip over.
if (Vector3.Dot(tiltedFront, flatFront) > 0.001f)
{
cameraFront = Vector3.Normalize(tiltedFront);
}
// Check for input to move the camera around.
if (currentKeyboardState.IsKeyDown(Keys.W))
cameraPosition += cameraFront * time * 0.1f;
if (currentKeyboardState.IsKeyDown(Keys.S))
cameraPosition -= cameraFront * time * 0.1f;
if (currentKeyboardState.IsKeyDown(Keys.A))
cameraPosition += cameraRight * time * 0.1f;
if (currentKeyboardState.IsKeyDown(Keys.D))
cameraPosition -= cameraRight * time * 0.1f;
cameraPosition += cameraFront *
currentGamePadState.ThumbSticks.Left.Y * time * 0.1f;
cameraPosition -= cameraRight *
currentGamePadState.ThumbSticks.Left.X * time * 0.1f;
if (currentGamePadState.Buttons.RightStick == ButtonState.Pressed ||
currentKeyboardState.IsKeyDown(Keys.R))
{
cameraPosition = new Vector3(-200, 30, 30);
cameraFront = new Vector3(1, 0, 0);
}
}
#endregion
}
}