Skip to content

Commit b2fc446

Browse files
committed
Give 8-2 better fixes
1 parent c82cce8 commit b2fc446

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

OpenTKTutorial8-2/OpenTKTutorial8-2/Game.cs

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void initProgram()
4444
{
4545
lastMousePos = new Vector2(Mouse.X, Mouse.Y);
4646
CursorVisible = false;
47+
cam.MouseSensitivity = 0.0025f;
4748

4849
GL.GenBuffers(1, out ibo_elements);
4950

@@ -73,9 +74,7 @@ void initProgram()
7374

7475
// Move camera away from origin
7576
cam.Position += new Vector3(0f, 0f, 3f);
76-
77-
cam.MouseSensitivity = 0.0025f;
78-
77+
7978
textures.Add("earth.png", loadImage("earth.png"));
8079
ObjVolume earth = ObjVolume.LoadFromFile("earth.obj");
8180
earth.TextureID = textures["earth.png"];
@@ -228,6 +227,8 @@ protected override void OnUpdateFrame(FrameEventArgs e)
228227
{
229228
base.OnUpdateFrame(e);
230229

230+
ProcessInput();
231+
231232
List<Vector3> verts = new List<Vector3>();
232233
List<int> inds = new List<int>();
233234
List<Vector3> colors = new List<Vector3>();
@@ -307,63 +308,72 @@ protected override void OnUpdateFrame(FrameEventArgs e)
307308
// Buffer index data
308309
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements);
309310
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indicedata.Length * sizeof(int)), indicedata, BufferUsageHint.StaticDraw);
311+
312+
view = cam.GetViewMatrix();
313+
}
310314

311-
312-
// Reset mouse position
313-
if (Focused)
315+
/// <summary>
316+
/// Handles keyboard and mouse input
317+
/// </summary>
318+
private void ProcessInput()
319+
{
320+
/* We'll use the escape key to make the program easy to exit from.
321+
* Otherwise it's annoying since the mouse cursor is trapped inside.*/
322+
if (Keyboard.GetState().IsKeyDown(Key.Escape))
314323
{
315-
Vector2 delta = lastMousePos - new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
316-
lastMousePos += delta;
324+
Exit();
325+
}
317326

318-
cam.AddRotation(delta.X, delta.Y);
319-
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
327+
/** Let's start by adding WASD input (feel free to change the keys if you want,
328+
* hopefully a later tutorial will have a proper input manager) for translating the camera. */
329+
if (Keyboard.GetState().IsKeyDown(Key.W))
330+
{
331+
cam.Move(0f, 0.1f, 0f);
320332
}
321333

322-
view = cam.GetViewMatrix();
323-
}
334+
if (Keyboard.GetState().IsKeyDown(Key.S))
335+
{
336+
cam.Move(0f, -0.1f, 0f);
337+
}
324338

325-
protected override void OnKeyPress(KeyPressEventArgs e)
326-
{
327-
base.OnKeyPress(e);
339+
if (Keyboard.GetState().IsKeyDown(Key.A))
340+
{
341+
cam.Move(-0.1f, 0f, 0f);
342+
}
328343

329-
if (e.KeyChar == 27)
344+
if (Keyboard.GetState().IsKeyDown(Key.D))
330345
{
331-
Exit();
346+
cam.Move(0.1f, 0f, 0f);
332347
}
333348

334-
switch (e.KeyChar)
349+
if (Keyboard.GetState().IsKeyDown(Key.Q))
335350
{
336-
case 'w':
337-
cam.Move(0f, 0.1f, 0f);
338-
break;
339-
case 'a':
340-
cam.Move(-0.1f, 0f, 0f);
341-
break;
342-
case 's':
343-
cam.Move(0f, -0.1f, 0f);
344-
break;
345-
case 'd':
346-
cam.Move(0.1f, 0f, 0f);
347-
break;
348-
case 'q':
349-
cam.Move(0f, 0f, 0.1f);
350-
break;
351-
case 'e':
352-
cam.Move(0f, 0f, -0.1f);
353-
break;
351+
cam.Move(0f, 0f, 0.1f);
354352
}
355-
}
356353

357-
protected override void OnFocusedChanged(EventArgs e)
358-
{
359-
base.OnFocusedChanged(e);
354+
if (Keyboard.GetState().IsKeyDown(Key.E))
355+
{
356+
cam.Move(0f, 0f, -0.1f);
357+
}
360358

361359
if (Focused)
362360
{
361+
Vector2 delta = lastMousePos - new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
362+
lastMousePos += delta;
363+
364+
cam.AddRotation(delta.X, delta.Y);
363365
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
364366
}
365367
}
366368

369+
370+
protected override void OnFocusedChanged(EventArgs e)
371+
{
372+
base.OnFocusedChanged(e);
373+
lastMousePos = new Vector2(OpenTK.Input.Mouse.GetState().X, OpenTK.Input.Mouse.GetState().Y);
374+
}
375+
376+
367377
int loadImage(Bitmap image)
368378
{
369379
int texID = GL.GenTexture();
@@ -389,7 +399,7 @@ int loadImage(string filename)
389399
Bitmap file = new Bitmap(filename);
390400
return loadImage(file);
391401
}
392-
catch (FileNotFoundException e)
402+
catch (FileNotFoundException)
393403
{
394404
return -1;
395405
}

OpenTKTutorial8-2/OpenTKTutorial8-2/ObjVolume.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static ObjVolume LoadFromFile(string filename)
138138
obj = LoadFromString(reader.ReadToEnd());
139139
}
140140
}
141-
catch (FileNotFoundException e)
141+
catch (FileNotFoundException)
142142
{
143143
Console.WriteLine("File not found: {0}", filename);
144144
}

0 commit comments

Comments
 (0)