diff --git a/OpenWindow/camera.cpp b/OpenWindow/camera.cpp index 5c1742e..91c8729 100644 --- a/OpenWindow/camera.cpp +++ b/OpenWindow/camera.cpp @@ -15,6 +15,7 @@ Camera::Camera() vertical_camera_speed = 0.5; vertical_camera_clamp_up = 90; vertical_camera_clamp_down = -90; + movement_speed = 1.f; } Vec3f Camera::GetForward() { @@ -56,35 +57,31 @@ void Camera::SetFarPlane(float far_val) { far_plane = far_val; } - -void Camera::rotate_camera_right() { - rotation.y += horizontal_camera_speed; +void Camera::rotate_hor(float d_angle) { + rotation.y += d_angle * horizontal_camera_speed; } -void Camera::rotate_camera_left() { - rotation.y -= horizontal_camera_speed; -} -void Camera::rotate_camera_up() { - rotation.x += vertical_camera_speed; - if (rotation.x > vertical_camera_clamp_up) rotation.x = 90; -} -void Camera::rotate_camera_down() { - rotation.x -= vertical_camera_speed; - if (rotation.x < vertical_camera_clamp_down) rotation.x = -90; +void Camera::rotate_ver(float d_angle) { + rotation.x += d_angle * vertical_camera_speed; + rotation.x = std::fmin(rotation.x, vertical_camera_clamp_up); + rotation.x = std::fmax(rotation.x, vertical_camera_clamp_down); } -#define MOVEMENT_SPEED 0.05f; void Camera::move_camera_left() { - position = position - right * MOVEMENT_SPEED; + position = position - right * movement_speed; } void Camera::move_camera_right() { - position = position + right * MOVEMENT_SPEED; + position = position + right * movement_speed; } void Camera::move_camera_forward() { - position = position + forward * MOVEMENT_SPEED; + position = position + forward * movement_speed; } void Camera::move_camera_backward() { - position = position - forward * MOVEMENT_SPEED; + position = position - forward * movement_speed; +} + +void Camera::SetMovementSpeed(float speed) { + movement_speed = speed; } void Camera::ApplyChanges() { diff --git a/OpenWindow/camera.h b/OpenWindow/camera.h index a20117d..c3c3746 100644 --- a/OpenWindow/camera.h +++ b/OpenWindow/camera.h @@ -16,6 +16,7 @@ private: float vertical_camera_speed; float vertical_camera_clamp_up; float vertical_camera_clamp_down; + float movement_speed; public: Camera(); @@ -30,12 +31,11 @@ public: void SetHorizontalRotSpeed(float speed); void SetClampRotUp(float angle); void SetClampRotDown(float angle); + void SetMovementSpeed(float speed); Vec3f GetForward(); - void rotate_camera_right(); - void rotate_camera_left(); - void rotate_camera_up(); - void rotate_camera_down(); + void rotate_hor(float d_angle); + void rotate_ver(float d_angle); void move_camera_left(); void move_camera_right(); void move_camera_forward(); diff --git a/OpenWindow/main.cpp b/OpenWindow/main.cpp index 3feaeee..da1378c 100644 --- a/OpenWindow/main.cpp +++ b/OpenWindow/main.cpp @@ -33,30 +33,14 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine return Msg.wParam; } -bool HandleMouseMovement() { - int xPos, yPos; +void HandleMouseMovement() { POINT point; GetCursorPos(&point); - bool movement_detected_x = true; - bool movement_detected_y = true; - - if (point.x > prev_mouse_x) - camera.rotate_camera_right(); - else if (point.x < prev_mouse_x) - camera.rotate_camera_left(); - else - movement_detected_x = false; - - if (point.y > prev_mouse_y) - camera.rotate_camera_up(); - else if (point.y < prev_mouse_y) - camera.rotate_camera_down(); - else - movement_detected_y = false; + camera.rotate_hor(point.x - prev_mouse_x); + camera.rotate_ver(point.y - prev_mouse_y); SetCursorPos(prev_mouse_x, prev_mouse_y); - return movement_detected_x || movement_detected_y; } bool HandleButtonPressed() { @@ -73,7 +57,6 @@ bool HandleButtonPressed() { void CALLBACK FixedUpdate(HWND hwnd, UINT message, UINT uInt, DWORD dWord) { - HandleMouseMovement(); HandleButtonPressed(); camera.ApplyChanges(); render(); @@ -85,6 +68,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) switch (message) { case WM_MOUSEMOVE: + HandleMouseMovement(); break; case WM_RBUTTONDOWN: break; diff --git a/OpenWindow/renderer.cpp b/OpenWindow/renderer.cpp index aed5563..f6bfc10 100644 --- a/OpenWindow/renderer.cpp +++ b/OpenWindow/renderer.cpp @@ -9,8 +9,8 @@ #include #include "camera.h" -#define HORIZONTAL_CAMERA_SPEED 1 -#define VERTICAL_CAMERA_SPEED 1 +#define HORIZONTAL_CAMERA_SPEED 0.1 +#define VERTICAL_CAMERA_SPEED 0.1 #define VERTICAL_CAMERA_CLAMP_UP 90 #define VERTICAL_CAMERA_CLAMP_DOWN -90 #define NEAR_CLIP_PLANE 0 @@ -18,6 +18,7 @@ #define FOV 30 #define DEFAULT_CAMERA_POS Vec3f(0, 0, 5) #define DEFAULT_CAMERA_ROT Vec3f(0, 0, 0) +#define CAMERA_MOVEMENT_SPEED 1.f const TGAColor white = TGAColor(255, 255, 255, 255); const TGAColor red = TGAColor(255, 0, 0, 255); @@ -204,6 +205,7 @@ void init_camera() { camera.SetClampRotUp(VERTICAL_CAMERA_CLAMP_UP); camera.SetHorizontalRotSpeed(HORIZONTAL_CAMERA_SPEED); camera.SetVerticalRotSpeed(VERTICAL_CAMERA_SPEED); + camera.SetMovementSpeed(CAMERA_MOVEMENT_SPEED); camera.ApplyChanges(); }