|
7 | 7 | using OpenTK; |
8 | 8 | using OpenTK.Graphics; |
9 | 9 | using OpenTK.Graphics.OpenGL; |
| 10 | +using OpenTK.Input; |
10 | 11 |
|
11 | 12 | namespace OpenTKTutorial8 |
12 | 13 | { |
@@ -42,6 +43,8 @@ public Game() |
42 | 43 | void initProgram() |
43 | 44 | { |
44 | 45 | lastMousePos = new Vector2(Mouse.X, Mouse.Y); |
| 46 | + CursorVisible = false; |
| 47 | + cam.MouseSensitivity = 0.0025f; |
45 | 48 |
|
46 | 49 | GL.GenBuffers(1, out ibo_elements); |
47 | 50 |
|
@@ -119,6 +122,8 @@ protected override void OnUpdateFrame(FrameEventArgs e) |
119 | 122 | { |
120 | 123 | base.OnUpdateFrame(e); |
121 | 124 |
|
| 125 | + ProcessInput(); |
| 126 | + |
122 | 127 | List<Vector3> verts = new List<Vector3>(); |
123 | 128 | List<int> inds = new List<int>(); |
124 | 129 | List<Vector3> colors = new List<Vector3>(); |
@@ -198,69 +203,66 @@ protected override void OnUpdateFrame(FrameEventArgs e) |
198 | 203 | // Buffer index data |
199 | 204 | GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements); |
200 | 205 | GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indicedata.Length * sizeof(int)), indicedata, BufferUsageHint.StaticDraw); |
| 206 | + } |
201 | 207 |
|
| 208 | + /// <summary> |
| 209 | + /// Handles keyboard and mouse input |
| 210 | + /// </summary> |
| 211 | + private void ProcessInput() |
| 212 | + { |
| 213 | + /* We'll use the escape key to make the program easy to exit from. |
| 214 | + * Otherwise it's annoying since the mouse cursor is trapped inside.*/ |
| 215 | + if (Keyboard.GetState().IsKeyDown(Key.Escape)) |
| 216 | + { |
| 217 | + Exit(); |
| 218 | + } |
202 | 219 |
|
203 | | - // Reset mouse position |
204 | | - if (Focused) |
| 220 | + /** Let's start by adding WASD input (feel free to change the keys if you want, |
| 221 | + * hopefully a later tutorial will have a proper input manager) for translating the camera. */ |
| 222 | + if (Keyboard.GetState().IsKeyDown(Key.W)) |
205 | 223 | { |
206 | | - Vector2 delta = lastMousePos - new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y); |
207 | | - lastMousePos += delta; |
| 224 | + cam.Move(0f, 0.1f, 0f); |
| 225 | + } |
208 | 226 |
|
209 | | - cam.AddRotation(delta.X, delta.Y); |
210 | | - ResetCursor(); |
| 227 | + if (Keyboard.GetState().IsKeyDown(Key.S)) |
| 228 | + { |
| 229 | + cam.Move(0f, -0.1f, 0f); |
211 | 230 | } |
212 | | - } |
213 | 231 |
|
| 232 | + if (Keyboard.GetState().IsKeyDown(Key.A)) |
| 233 | + { |
| 234 | + cam.Move(-0.1f, 0f, 0f); |
| 235 | + } |
214 | 236 |
|
215 | | - protected override void OnKeyPress(KeyPressEventArgs e) |
216 | | - { |
217 | | - base.OnKeyPress(e); |
| 237 | + if (Keyboard.GetState().IsKeyDown(Key.D)) |
| 238 | + { |
| 239 | + cam.Move(0.1f, 0f, 0f); |
| 240 | + } |
218 | 241 |
|
219 | | - if (e.KeyChar == 27) |
| 242 | + if (Keyboard.GetState().IsKeyDown(Key.Q)) |
220 | 243 | { |
221 | | - Exit(); |
| 244 | + cam.Move(0f, 0f, 0.1f); |
222 | 245 | } |
223 | 246 |
|
224 | | - switch (e.KeyChar) |
| 247 | + if (Keyboard.GetState().IsKeyDown(Key.E)) |
225 | 248 | { |
226 | | - case 'w': |
227 | | - cam.Move(0f, 0.1f, 0f); |
228 | | - break; |
229 | | - case 'a': |
230 | | - cam.Move(-0.1f, 0f, 0f); |
231 | | - break; |
232 | | - case 's': |
233 | | - cam.Move(0f, -0.1f, 0f); |
234 | | - break; |
235 | | - case 'd': |
236 | | - cam.Move(0.1f, 0f, 0f); |
237 | | - break; |
238 | | - case 'q': |
239 | | - cam.Move(0f, 0f, 0.1f); |
240 | | - break; |
241 | | - case 'e': |
242 | | - cam.Move(0f, 0f, -0.1f); |
243 | | - break; |
| 249 | + cam.Move(0f, 0f, -0.1f); |
244 | 250 | } |
245 | | - } |
246 | 251 |
|
247 | | - /// <summary> |
248 | | - /// Moves the mouse cursor to the center of the screen |
249 | | - /// </summary> |
250 | | - void ResetCursor() |
251 | | - { |
252 | | - OpenTK.Input.Mouse.SetPosition(Bounds.Left + Bounds.Width / 2, Bounds.Top + Bounds.Height / 2); |
253 | | - lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y); |
| 252 | + if (Focused) |
| 253 | + { |
| 254 | + Vector2 delta = lastMousePos - new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y); |
| 255 | + lastMousePos += delta; |
| 256 | + |
| 257 | + cam.AddRotation(delta.X, delta.Y); |
| 258 | + lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y); |
| 259 | + } |
254 | 260 | } |
255 | 261 |
|
256 | 262 | protected override void OnFocusedChanged(EventArgs e) |
257 | 263 | { |
258 | 264 | base.OnFocusedChanged(e); |
259 | | - |
260 | | - if (Focused) |
261 | | - { |
262 | | - ResetCursor(); |
263 | | - } |
| 265 | + lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y); |
264 | 266 | } |
265 | 267 |
|
266 | 268 | int loadImage(Bitmap image) |
|
0 commit comments