Added camera

This commit is contained in:
Ano-sys
2025-07-04 00:55:50 +02:00
parent 318263051a
commit 6bc2ea1650
7 changed files with 285 additions and 250 deletions
+44 -108
View File
@@ -1,115 +1,51 @@
#include "vulkan_app.hpp"
namespace vapp{
void Vulkan::processInput(){
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
void Camera::setOthographicProjection(float left, float right, float bottom, float top){
}
void Camera::setPerspectiveProjection(float fovy, float aspect, float zNear, float zFar){
}
void Camera::setViewDirection(glm::vec3 position, glm::vec3 direction, glm::vec3 up) {
const glm::vec3 w{glm::normalize(direction)};
const glm::vec3 u{glm::normalize(glm::cross(w, up))};
const glm::vec3 v{glm::cross(w, u)};
viewMatrix = glm::mat4{1.f};
viewMatrix[0][0] = u.x;
viewMatrix[1][0] = u.y;
viewMatrix[2][0] = u.z;
viewMatrix[0][1] = v.x;
viewMatrix[1][1] = v.y;
viewMatrix[2][1] = v.z;
viewMatrix[0][2] = w.x;
viewMatrix[1][2] = w.y;
viewMatrix[2][2] = w.z;
viewMatrix[3][0] = -glm::dot(u, position);
viewMatrix[3][1] = -glm::dot(v, position);
viewMatrix[3][2] = -glm::dot(w, position);
}
void Camera::setViewTarget(glm::vec3 position, glm::vec3 target, glm::vec3 up) {
setViewDirection(position, target - position, up);
}
void Camera::setViewYXZ(glm::vec3 position, glm::vec3 rotation) {
const float c3 = glm::cos(rotation.z);
const float s3 = glm::sin(rotation.z);
const float c2 = glm::cos(rotation.x);
const float s2 = glm::sin(rotation.x);
const float c1 = glm::cos(rotation.y);
const float s1 = glm::sin(rotation.y);
const glm::vec3 u{(c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)};
const glm::vec3 v{(c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)};
const glm::vec3 w{(c2 * s1), (-s2), (c1 * c2)};
viewMatrix = glm::mat4{1.f};
viewMatrix[0][0] = u.x;
viewMatrix[1][0] = u.y;
viewMatrix[2][0] = u.z;
viewMatrix[0][1] = v.x;
viewMatrix[1][1] = v.y;
viewMatrix[2][1] = v.z;
viewMatrix[0][2] = w.x;
viewMatrix[1][2] = w.y;
viewMatrix[2][2] = w.z;
viewMatrix[3][0] = -glm::dot(u, position);
viewMatrix[3][1] = -glm::dot(v, position);
viewMatrix[3][2] = -glm::dot(w, position);
}
void Vulkan::handleKeyboardInput(){
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS){
cameraPos.x += 0.001f;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS){
cameraPos.x -= 0.001f;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS){
cameraPos.y -= 0.001f;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS){
cameraPos.y += 0.001f;
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS){
cameraPos.x += 0.001f;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS){
cameraPos.x -= 0.001f;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS){
cameraPos.z += 0.001f;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS){
cameraPos.z -= 0.001f;
}
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
camera.ProcessKeyboard(UP, deltaTime);
if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
camera.ProcessKeyboard(DOWN, deltaTime);
}
void Vulkan::handleMouseInput(GLFWwindow* window, double xpos, double ypos){
double xoffset = xpos - (_width / 2.0);
double yoffset = ypos - (_height / 2.0);
glfwSetCursorPos(window, _width / 2.0, _height / 2.0);
float sensitivity = 0.001f;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if(pitch > 89.0f)
pitch = 89.0f;
if(pitch < -89.0f)
pitch = -89.0f;
glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
direction = glm::normalize(direction);
cameraFront = cameraPos + direction;
void Vulkan::keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods){
Vulkan *app = static_cast<Vulkan*>(glfwGetWindowUserPointer(window));
if(key == GLFW_KEY_LEFT_SHIFT){
if(action == GLFW_PRESS)
app->camera.MovementSpeed = 5.0f;
else if(action == GLFW_RELEASE)
app->camera.MovementSpeed = 2.5f;
}
}
}
void Vulkan::mouseCallback(GLFWwindow *window, double xpos, double ypos){
auto app = static_cast<Vulkan*>(glfwGetWindowUserPointer(window));
if(app->firstMouse){
app->lastX = xpos;
app->lastY = ypos;
app->firstMouse = false;
}
float xoff = xpos - app->lastX;
float yoff = app->lastY - ypos;
app->lastX = xpos;
app->lastY = ypos;
app->camera.ProcessMouseMovement(xoff, yoff);
}
void Vulkan::scrollCallback(GLFWwindow *window, double /*x*/, double yoffset){
Vulkan *app = static_cast<Vulkan*>(glfwGetWindowUserPointer(window));
app->camera.ProcessMouseScroll(static_cast<float>(yoffset));
}
}