WE GOT TEXTURES BABY
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <destrum/Graphics/Camera.h>
|
||||
#include <destrum/Input/InputManager.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <glm/gtc/matrix_inverse.hpp>
|
||||
@@ -6,146 +7,112 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtx/string_cast.hpp>
|
||||
|
||||
#include "glm/gtx/norm.hpp"
|
||||
|
||||
|
||||
Camera::Camera(const glm::vec3& position, const glm::vec3& up): m_position{position}, m_up{up} {
|
||||
}
|
||||
|
||||
void Camera::Update(float deltaTime) {
|
||||
// const auto MyWindow = static_cast<Window*>(glfwGetWindowUserPointer(Window::gWindow));
|
||||
//
|
||||
// #pragma region Keyboard Movement
|
||||
// totalPitch = glm::clamp(totalPitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
|
||||
//
|
||||
// float MovementSpeed = m_movementSpeed;
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
|
||||
// MovementSpeed *= 2.0f;
|
||||
// }
|
||||
//
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
// m_position += m_forward * deltaTime * MovementSpeed;
|
||||
// }
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
// m_position -= m_right * deltaTime * MovementSpeed;
|
||||
// }
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
// m_position -= m_forward * deltaTime * MovementSpeed;
|
||||
// }
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
// m_position += m_right * deltaTime * MovementSpeed;
|
||||
// }
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_E) == GLFW_PRESS) {
|
||||
// m_position += m_up * deltaTime * MovementSpeed;
|
||||
// }
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_Q) == GLFW_PRESS) {
|
||||
// m_position -= m_up * deltaTime * MovementSpeed;
|
||||
// }
|
||||
//
|
||||
// // Looking around with arrow keys
|
||||
// constexpr float lookSpeed = 1.0f * glm::radians(1.f);
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_UP) == GLFW_PRESS) {
|
||||
// totalPitch += lookSpeed;
|
||||
// }
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_DOWN) == GLFW_PRESS) {
|
||||
// totalPitch -= lookSpeed;
|
||||
// }
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_LEFT) == GLFW_PRESS) {
|
||||
// totalYaw -= lookSpeed;
|
||||
// }
|
||||
// if (glfwGetKey(Window::gWindow, GLFW_KEY_RIGHT) == GLFW_PRESS) {
|
||||
// totalYaw += lookSpeed;
|
||||
// }
|
||||
//
|
||||
// totalPitch = glm::clamp(totalPitch, -glm::half_pi<float>(), glm::half_pi<float>());
|
||||
//
|
||||
// const glm::mat4 yawMatrix = glm::rotate(glm::mat4(1.0f), -totalYaw, glm::vec3(0, 1, 0));
|
||||
// const glm::mat4 pitchMatrix = glm::rotate(glm::mat4(1.0f), totalPitch, glm::vec3(0, 0, 1));
|
||||
//
|
||||
// const glm::mat4 rotationMatrix = yawMatrix * pitchMatrix;
|
||||
//
|
||||
// m_forward = glm::vec3(rotationMatrix * glm::vec4(1, 0, 0, 0));
|
||||
// m_right = glm::normalize(glm::cross(m_forward, glm::vec3(0, 1, 0)));
|
||||
// m_up = glm::normalize(glm::cross(m_right, m_forward));
|
||||
// #pragma endregion
|
||||
//
|
||||
// #pragma region Mouse Looking
|
||||
// static bool wasCursorLockedLastFrame = false;
|
||||
// static double lastMouseX = 0.0;
|
||||
// static double lastMouseY = 0.0;
|
||||
// static bool firstMouse = true;
|
||||
//
|
||||
// bool isLocked = MyWindow->isCursorLocked();
|
||||
// if (isLocked) {
|
||||
// double mouseX, mouseY;
|
||||
// glfwGetCursorPos(Window::gWindow, &mouseX, &mouseY);
|
||||
//
|
||||
// // Reset mouse reference when locking the cursor (prevents jump)
|
||||
// if (!wasCursorLockedLastFrame) {
|
||||
// lastMouseX = mouseX;
|
||||
// lastMouseY = mouseY;
|
||||
// firstMouse = false;
|
||||
// }
|
||||
//
|
||||
// float xOffset = static_cast<float>(mouseX - lastMouseX);
|
||||
// float yOffset = static_cast<float>(lastMouseY - mouseY); // Reversed: y goes up
|
||||
//
|
||||
// lastMouseX = mouseX;
|
||||
// lastMouseY = mouseY;
|
||||
//
|
||||
// constexpr float sensitivity = 0.002f;
|
||||
// xOffset *= sensitivity;
|
||||
// yOffset *= sensitivity;
|
||||
//
|
||||
// totalYaw += xOffset;
|
||||
// totalPitch += yOffset;
|
||||
//
|
||||
// totalPitch = glm::clamp(totalPitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
|
||||
//
|
||||
// m_frustum.update(m_projectionMatrix * m_viewMatrix);
|
||||
// }
|
||||
//
|
||||
// wasCursorLockedLastFrame = isLocked;
|
||||
// #pragma endregion
|
||||
//
|
||||
// #pragma region Controller Movement
|
||||
//
|
||||
// if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) {
|
||||
// GLFWgamepadstate state;
|
||||
// if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1) && glfwGetGamepadState(GLFW_JOYSTICK_1, &state)) {
|
||||
// float moveSpeed = MovementSpeed * deltaTime;
|
||||
// const float rotSpeed = 1.5f * deltaTime;
|
||||
//
|
||||
//
|
||||
// //TODO: god knows what this is
|
||||
// auto deadzone = [] (float value, float threshold = 0.1f) {
|
||||
// if (fabs(value) < threshold)
|
||||
// return 0.0f;
|
||||
// const float sign = (value > 0) ? 1.0f : -1.0f;
|
||||
// return sign * (fabs(value) - threshold) / (1.0f - threshold);
|
||||
// };
|
||||
//
|
||||
// //LT is x2 speed
|
||||
// const bool isLTPressed = state.buttons[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER] == GLFW_PRESS;
|
||||
// if (isLTPressed) {
|
||||
// moveSpeed *= 3.0f;
|
||||
// }
|
||||
//
|
||||
// const float lx = deadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_X]);
|
||||
// const float ly = deadzone(state.axes[GLFW_GAMEPAD_AXIS_LEFT_Y]);
|
||||
// const float rx = deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_X]);
|
||||
// const float ry = deadzone(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y]);
|
||||
//
|
||||
// m_position += m_forward * (-ly * moveSpeed);
|
||||
// m_position += m_right * (lx * moveSpeed);
|
||||
//
|
||||
// const float lTrigger = (state.axes[GLFW_GAMEPAD_AXIS_LEFT_TRIGGER] + 1.0f) / 2.0f;
|
||||
// const float rTrigger = (state.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER] + 1.0f) / 2.0f;
|
||||
// const float vertical = (rTrigger - lTrigger);
|
||||
// m_position += m_up * vertical * moveSpeed;
|
||||
//
|
||||
// totalYaw += rx * rotSpeed;
|
||||
// totalPitch -= ry * rotSpeed;
|
||||
// }
|
||||
// }
|
||||
// #pragma endregion
|
||||
auto& input = InputManager::GetInstance();
|
||||
|
||||
// --- tuning ---
|
||||
float moveSpeed = m_movementSpeed;
|
||||
if (input.IsKeyDown(SDL_SCANCODE_LSHIFT) || input.IsKeyDown(SDL_SCANCODE_RSHIFT)) {
|
||||
moveSpeed *= 2.0f;
|
||||
}
|
||||
|
||||
// Controller speed boost (pad0 LB)
|
||||
const SDL_JoystickID pad0 = input.GetPadInstanceId(0);
|
||||
if (pad0 >= 0 && input.IsPadButtonDown(pad0, SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) {
|
||||
moveSpeed *= 3.0f;
|
||||
}
|
||||
|
||||
// Clamp pitch like your old code
|
||||
m_pitch = glm::clamp(m_pitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
|
||||
|
||||
// =========================
|
||||
// Movement (Keyboard)
|
||||
// =========================
|
||||
glm::vec3 move(0.0f);
|
||||
|
||||
if (input.IsKeyDown(SDL_SCANCODE_W)) move += m_forward;
|
||||
if (input.IsKeyDown(SDL_SCANCODE_S)) move -= m_forward;
|
||||
if (input.IsKeyDown(SDL_SCANCODE_D)) move += m_right;
|
||||
if (input.IsKeyDown(SDL_SCANCODE_A)) move -= m_right;
|
||||
|
||||
if (input.IsKeyDown(SDL_SCANCODE_Q)) move += m_up;
|
||||
if (input.IsKeyDown(SDL_SCANCODE_E)) move -= m_up;
|
||||
|
||||
if (glm::length2(move) > 0.0f) {
|
||||
move = glm::normalize(move);
|
||||
m_position += move * (moveSpeed * deltaTime);
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Movement (Controller)
|
||||
// =========================
|
||||
if (pad0 >= 0) {
|
||||
const float lx = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_LEFTX); // [-1..1]
|
||||
const float ly = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_LEFTY); // [-1..1]
|
||||
|
||||
// SDL Y is typically +down, so invert for "forward"
|
||||
glm::vec3 padMove(0.0f);
|
||||
padMove += m_forward * (-ly);
|
||||
padMove += m_right * ( lx);
|
||||
|
||||
// Triggers for vertical movement (optional)
|
||||
// SDL controller triggers are axes too: 0..1-ish after normalization in our helper, but signless.
|
||||
// With our NormalizeAxis, triggers will sit near 0 until pressed (depending on mapping).
|
||||
// If your NormalizeAxis maps triggers weirdly, swap to raw event value approach.
|
||||
const float lt = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_TRIGGERLEFT);
|
||||
const float rt = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
|
||||
const float vertical = (rt - lt);
|
||||
padMove += m_up * vertical;
|
||||
|
||||
if (glm::length2(padMove) > 0.0001f) {
|
||||
// do NOT normalize: preserve analog magnitude for smooth movement
|
||||
m_position += padMove * (moveSpeed * deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Look (Keyboard arrows only)
|
||||
// =========================
|
||||
// Use radians/sec so framerate-independent
|
||||
const float keyLookSpeed = glm::radians(120.0f); // degrees per second
|
||||
|
||||
if (input.IsKeyDown(SDL_SCANCODE_UP)) m_pitch += keyLookSpeed * deltaTime;
|
||||
if (input.IsKeyDown(SDL_SCANCODE_DOWN)) m_pitch -= keyLookSpeed * deltaTime;
|
||||
if (input.IsKeyDown(SDL_SCANCODE_LEFT)) m_yaw -= keyLookSpeed * deltaTime;
|
||||
if (input.IsKeyDown(SDL_SCANCODE_RIGHT)) m_yaw += keyLookSpeed * deltaTime;
|
||||
|
||||
// =========================
|
||||
// Look (Controller right stick)
|
||||
// =========================
|
||||
if (pad0 >= 0) {
|
||||
const float rx = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_RIGHTX);
|
||||
const float ry = input.GetPadAxis(pad0, SDL_CONTROLLER_AXIS_RIGHTY);
|
||||
|
||||
const float padLookSpeed = 2.2f; // radians/sec at full deflection
|
||||
m_yaw += rx * padLookSpeed * deltaTime;
|
||||
m_pitch -= ry * padLookSpeed * deltaTime;
|
||||
}
|
||||
|
||||
// Clamp pitch again after modifications
|
||||
m_pitch = glm::clamp(m_pitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
|
||||
|
||||
// Recompute basis from yaw/pitch (same convention you used)
|
||||
const glm::mat4 yawMatrix = glm::rotate(glm::mat4(1.0f), -m_yaw, glm::vec3(0, 1, 0));
|
||||
const glm::mat4 pitchMatrix = glm::rotate(glm::mat4(1.0f), m_pitch, glm::vec3(0, 0, 1));
|
||||
const glm::mat4 rotation = yawMatrix * pitchMatrix;
|
||||
|
||||
m_forward = glm::normalize(glm::vec3(rotation * glm::vec4(1, 0, 0, 0))); // +X forward
|
||||
m_right = glm::normalize(glm::cross(m_forward, glm::vec3(0, 1, 0)));
|
||||
m_up = glm::normalize(glm::cross(m_right, m_forward));
|
||||
|
||||
// keep target mode off when manually controlled
|
||||
m_useTarget = false;
|
||||
|
||||
CalculateProjectionMatrix();
|
||||
CalculateViewMatrix();
|
||||
|
||||
Reference in New Issue
Block a user