mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
feat: add camera and player class
This commit is contained in:
144
src/camera.cpp
144
src/camera.cpp
@@ -1,131 +1,41 @@
|
||||
#include "Cubed/camera.hpp"
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <Cubed/camera.hpp>
|
||||
#include <Cubed/gameplay/player.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
|
||||
static glm::vec3 cameraPos;
|
||||
static float speed = 0.1f;
|
||||
static float lastMouseX, lastMouseY;
|
||||
static bool firseMouse = true;
|
||||
Camera::Camera() {
|
||||
|
||||
}
|
||||
|
||||
static MoveState moveState;
|
||||
|
||||
static float yaw, pitch;
|
||||
static glm::vec3 front = glm::vec3(0, 0, -1);
|
||||
static float sensitivity = 0.05f;
|
||||
static glm::vec3 rightPos;
|
||||
|
||||
void updateMoveCamera() {
|
||||
rightPos = glm::normalize(glm::cross(front, glm::vec3(0.0f, 1.0f, 0.0f)));
|
||||
if (moveState.forward) {
|
||||
cameraPos += glm::vec3(front.x, 0.0f, front.z) * speed;
|
||||
}
|
||||
if (moveState.back) {
|
||||
cameraPos -= glm::vec3(front.x, 0.0f, front.z) * speed;
|
||||
}
|
||||
if (moveState.left) {
|
||||
cameraPos -= glm::vec3(rightPos.x, 0.0f, rightPos.z) * speed;
|
||||
}
|
||||
if (moveState.right) {
|
||||
cameraPos += glm::vec3(rightPos.x, 0.0f, rightPos.z) * speed;
|
||||
}
|
||||
if (moveState.up) {
|
||||
cameraPos += glm::vec3(0.0f, 1.0f, 0.0f) * speed;;
|
||||
}
|
||||
if (moveState.down) {
|
||||
cameraPos -= glm::vec3(0.0f, 1.0f, 0.0f) * speed;;
|
||||
}
|
||||
void Camera::updateMoveCamera() {
|
||||
CUBED_ASSERT_MSG(m_player, "nullptr");
|
||||
m_cameraPos = m_player->getPlayerPos();
|
||||
}
|
||||
|
||||
|
||||
void cameraInit() {
|
||||
cameraPos = glm::vec3(0.0f, 2.0f, 0.0f);
|
||||
void Camera::cameraInit(Player* _player) {
|
||||
m_cameraPos = glm::vec3(0.0f, 2.0f, 0.0f);
|
||||
m_player = _player;
|
||||
}
|
||||
|
||||
void changeView(float offsetX, float offsetY) {
|
||||
yaw += offsetX * sensitivity;
|
||||
pitch += offsetY * sensitivity;
|
||||
|
||||
|
||||
if (pitch > 89.0f) pitch = 89.0f;
|
||||
if (pitch < -89.0f) pitch = -89.0f;
|
||||
|
||||
|
||||
front.x = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
front.y = sin(glm::radians(pitch));
|
||||
front.z = -cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
|
||||
front = glm::normalize(front);
|
||||
}
|
||||
|
||||
void updateCursorPositionCamera(double xpos, double ypos) {
|
||||
if (firseMouse) {
|
||||
lastMouseX = xpos;
|
||||
lastMouseY = ypos;
|
||||
firseMouse = false;
|
||||
void Camera::updateCursorPositionCamera(double xpos, double ypos) {
|
||||
if (m_firseMouse) {
|
||||
m_lastMouseX = xpos;
|
||||
m_lastMouseY = ypos;
|
||||
m_firseMouse = false;
|
||||
}
|
||||
|
||||
float offsetX = xpos - lastMouseX;
|
||||
float offsetY = lastMouseY - ypos;
|
||||
float offsetX = xpos - m_lastMouseX;
|
||||
float offsetY = m_lastMouseY - ypos;
|
||||
|
||||
lastMouseX = xpos;
|
||||
lastMouseY = ypos;
|
||||
changeView(offsetX, offsetY);
|
||||
m_lastMouseX = xpos;
|
||||
m_lastMouseY = ypos;
|
||||
CUBED_ASSERT_MSG(m_player, "nullptr");
|
||||
m_player->updateFrontVec(offsetX, offsetY);
|
||||
}
|
||||
|
||||
glm::mat4 getCameraLookAt() {
|
||||
return glm::lookAt(cameraPos, cameraPos + front, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
}
|
||||
|
||||
void updateCameraKey(int key, int action) {
|
||||
switch(key) {
|
||||
case GLFW_KEY_W:
|
||||
if (action == GLFW_PRESS) {
|
||||
moveState.forward = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
moveState.forward = false;
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_S:
|
||||
if (action == GLFW_PRESS) {
|
||||
moveState.back = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
moveState.back = false;
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_A:
|
||||
if (action == GLFW_PRESS) {
|
||||
moveState.left = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
moveState.left = false;
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_D:
|
||||
if (action == GLFW_PRESS) {
|
||||
moveState.right = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
moveState.right = false;
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_SPACE:
|
||||
if (action == GLFW_PRESS) {
|
||||
moveState.up = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
moveState.up = false;
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_LEFT_SHIFT:
|
||||
if (action == GLFW_PRESS) {
|
||||
moveState.down = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
moveState.down = false;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
const glm::mat4 Camera::getCameraLookAt() const{
|
||||
CUBED_ASSERT_MSG(m_player, "nullptr");
|
||||
return glm::lookAt(m_cameraPos, m_cameraPos + m_player->getFront(), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user