2022-11-10 19:47:39 +01:00
|
|
|
#ifndef CAMERA_H
|
|
|
|
#define CAMERA_H
|
|
|
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "chunk.hpp"
|
|
|
|
|
|
|
|
class Camera
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
Camera()
|
|
|
|
{
|
|
|
|
view = glm::mat4(1.0f);
|
|
|
|
// note that we're translating the scene in the reverse direction of where we want to move
|
2022-11-17 12:53:42 +01:00
|
|
|
projection = glm::perspective(glm::radians(90.0f), 800.0f / 600.0f, 0.1f, 200.0f);
|
2022-11-10 19:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void update(GLFWwindow *window, float deltaTime)
|
|
|
|
{
|
|
|
|
const float cameraSpeed = 10.0f * deltaTime; // adjust accordingly
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
|
|
this->cameraPos += cameraSpeed * cameraFront;
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
|
|
this->cameraPos -= cameraSpeed * cameraFront;
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
|
|
this->cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
|
|
this->cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
|
2023-02-23 19:07:17 +01:00
|
|
|
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
|
|
|
|
this->cameraPos += cameraSpeed * cameraUp;
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS)
|
|
|
|
this->cameraPos -= cameraSpeed * cameraUp;
|
|
|
|
|
2022-11-10 19:47:39 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
cameraFront = glm::normalize(direction);
|
|
|
|
|
|
|
|
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void viewPortCallBack(GLFWwindow *window, int width, int height)
|
|
|
|
{
|
2022-11-17 12:53:42 +01:00
|
|
|
projection = glm::perspective(glm::radians(70.0f), (float)width / (float)height, 0.1f, 350.0f);
|
2022-11-10 19:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void mouseCallback(GLFWwindow *window, double xpos, double ypos)
|
|
|
|
{
|
|
|
|
float xoffset = xpos - lastX;
|
|
|
|
float yoffset = lastY - ypos; // reversed since y-coordinates range from bottom to top
|
|
|
|
lastX = xpos;
|
|
|
|
lastY = ypos;
|
|
|
|
|
|
|
|
const float sensitivity = 0.1f;
|
|
|
|
xoffset *= sensitivity;
|
|
|
|
yoffset *= sensitivity;
|
|
|
|
|
|
|
|
yaw += xoffset;
|
|
|
|
pitch += yoffset;
|
|
|
|
|
|
|
|
if (pitch > 89.0f)
|
|
|
|
pitch = 89.0f;
|
|
|
|
if (pitch < -89.0f)
|
|
|
|
pitch = -89.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::vec3 getPos()
|
|
|
|
{
|
|
|
|
return cameraPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::vec3 getFront()
|
|
|
|
{
|
|
|
|
return cameraFront;
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::vec3 getUp()
|
|
|
|
{
|
|
|
|
return cameraUp;
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::mat4 getView()
|
|
|
|
{
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
glm::mat4 getProjection()
|
|
|
|
{
|
|
|
|
return projection;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-02-23 19:07:17 +01:00
|
|
|
glm::vec3 cameraPos = glm::vec3(static_cast<float>(CHUNK_SIZE)*24, 40.0f, static_cast<float>(CHUNK_SIZE)*24);
|
2022-11-10 19:47:39 +01:00
|
|
|
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
|
|
|
|
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
|
|
|
|
glm::vec3 direction = glm::vec3(0.0f);
|
|
|
|
|
|
|
|
glm::mat4 view, projection;
|
|
|
|
|
|
|
|
float lastX = 400, lastY = 300;
|
|
|
|
float yaw, pitch;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|