diff --git a/TheRepo/TheLabs/MyExampleWindow.cs b/TheRepo/TheLabs/MyExampleWindow.cs index ec44233..21c6fc2 100644 --- a/TheRepo/TheLabs/MyExampleWindow.cs +++ b/TheRepo/TheLabs/MyExampleWindow.cs @@ -21,10 +21,28 @@ namespace TheLabs // - Resources that only need to be created once private Mesh _cubeMesh; private Texture _cubeTexture; + private Texture _cubeTexture2; private float _rotation; // -- Scene Objects private RenderObject _cubeObject; + private RenderObject _cubeObject2; + + private Vector3 _cameraPosition = new Vector3(0.0f, 0.0f, -3.0f); + private Vector3 _cameraFront = new Vector3(0.0f, 0.0f, -1.0f); + + private Vector3 _cameraUp = Vector3.UnitY; + + private float _yaw = -90.0f; + private float _pitch = 0.0f; + + private float _cameraSpeed = 2.5f; + private float _rotationSpeed = 50.0f; + + private float _mouseSensitivity = 0.1f; + + // This prevents a large camera jump when the window first gets focus + private bool _firstMove = true; public MyExampleWindow(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(gameWindowSettings, nativeWindowSettings) @@ -42,12 +60,17 @@ namespace TheLabs // Set The background color to a nice blue. GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f); GL.Enable(EnableCap.DepthTest); + CursorState = CursorState.Grabbed; _shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag"); _cubeMesh = ShapeFactory.CreateTexturedCube(); - _cubeTexture = new Texture("Textures/placeholder.png"); + _cubeTexture = new Texture("Textures/stone.jpg"); + _cubeTexture2 = new Texture("Textures/placeholder.png"); _cubeObject = new RenderObject(_cubeMesh, _shader, _cubeTexture); - _cubeObject.Position = new Vector3(0.0f, 0.0f, -1.0f); + _cubeObject2 = new RenderObject(_cubeMesh, _shader, _cubeTexture2); + _cubeObject.Position = new Vector3(-1.5f, 0.0f, 0.0f); + _cubeObject2.Position = new Vector3(1.5f, 0.0f, 0.0f); _cubeObject.Scale = new Vector3(0.5f, 0.5f, 0.5f); + _cubeObject2.Scale = new Vector3(0.5f, 0.5f, 0.5f); } @@ -60,9 +83,10 @@ namespace TheLabs // Clear the color buffer and the depth buffer GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit); _cubeObject.Rotation = Quaternion.FromEulerAngles(_rotation * 0.5f, _rotation, 0); // + _cubeObject2.Rotation = Quaternion.FromEulerAngles(-_rotation * 0.5f, -_rotation, 0); // --- Set up Camera --- - Matrix4 view = Matrix4.CreateTranslation(0.0f, 0.0f, -3.0f); + Matrix4 view = Matrix4.LookAt(_cameraPosition, _cameraPosition + _cameraFront, _cameraUp); float aspectRatio = (float)Size.X / Size.Y; // 2. Add a safety check for divide-by-zero @@ -78,6 +102,7 @@ namespace TheLabs ); _cubeObject.Draw(view, projection); + _cubeObject2.Draw(view, projection); SwapBuffers(); } @@ -85,15 +110,70 @@ namespace TheLabs protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); - _rotation += (float)e.Time ; + _rotation += (float)e.Time; var input = KeyboardState; if (input.IsKeyDown(Keys.Escape)) { Close(); } + + if (!IsFocused) + { + _firstMove = true; + return; + } + + var mouuse = MouseState; + if (_firstMove) + { + mouuse = MouseState; + _firstMove = false; + } + else + { + // Get the mouse's movement delta + float deltaX = mouuse.Delta.X; + float deltaY = mouuse.Delta.Y; + + // Apply sensitivity + _yaw += deltaX * _mouseSensitivity; + _pitch -= deltaY * _mouseSensitivity; // Y-axis is inverted + } + + + // --- Rotation (Arrow Keys) --- + if (input.IsKeyDown(Keys.Left)) + { + _yaw -= _rotationSpeed * (float)e.Time; + } + if (input.IsKeyDown(Keys.Right)) + { + _yaw += _rotationSpeed * (float)e.Time; } + if (input.IsKeyDown(Keys.Up)) + { + _pitch += _rotationSpeed * (float)e.Time; + } + if (input.IsKeyDown(Keys.Down)) + { + _pitch -= _rotationSpeed * (float)e.Time; + } + _pitch = MathHelper.Clamp(_pitch, -89.0f, 89.0f); + Vector3 front; + front.X = MathF.Cos(MathHelper.DegreesToRadians(_yaw)) * MathF.Cos(MathHelper.DegreesToRadians(_pitch)); + front.Y = MathF.Sin(MathHelper.DegreesToRadians(_pitch)); + front.Z = MathF.Sin(MathHelper.DegreesToRadians(_yaw)) * MathF.Cos(MathHelper.DegreesToRadians(_pitch)); + _cameraFront = Vector3.Normalize(front); + var _cameraRight = Vector3.Normalize(Vector3.Cross(_cameraFront, _cameraUp)); + if (input.IsKeyDown(Keys.W)) _cameraPosition += _cameraFront * _cameraSpeed * (float)e.Time; + if (input.IsKeyDown(Keys.S)) _cameraPosition -= _cameraFront * _cameraSpeed * (float)e.Time; + if (input.IsKeyDown(Keys.A)) _cameraPosition -= _cameraRight * _cameraSpeed * (float)e.Time; + if (input.IsKeyDown(Keys.D)) _cameraPosition += _cameraRight * _cameraSpeed * (float)e.Time; + + } + protected override void OnResize(ResizeEventArgs e) {