feat: add camera and player class

This commit is contained in:
2026-03-06 22:07:26 +08:00
parent 02127c1e01
commit 05a62e7443
6 changed files with 221 additions and 143 deletions

View File

@@ -45,10 +45,10 @@ set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
add_executable(${PROJECT_NAME}
src/main.cpp
src/camera.cpp
src/gameplay/player.cpp
src/tools/shader_tools.cpp
src/tools/log.cpp
src/camera.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC ${INCLUDE_DIR})

View File

@@ -6,24 +6,30 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
class Player;
class Camera {
private:
bool m_firseMouse = true;
Player* m_player;
float m_lastMouseX, m_lastMouseY;
glm::vec3 m_cameraPos;
public:
Camera();
void updateMoveCamera();
void cameraInit(Player* player);
void updateCursorPositionCamera(double xpos, double ypos);
const glm::mat4 getCameraLookAt() const;
struct MoveState {
bool forward = false;
bool back = false;
bool left = false;
bool right = false;
bool down = false;
bool up = false;
};
void updateMoveCamera();
void cameraInit();
void changeView(float offsetX, float offsetY);
void updateCursorPositionCamera(double xpos, double ypos);
glm::mat4 getCameraLookAt();
void updateCameraKey(int key, int action);

View File

@@ -0,0 +1,41 @@
#pragma once
#include <glm/glm.hpp>
struct MoveState {
bool forward = false;
bool back = false;
bool left = false;
bool right = false;
bool down = false;
bool up = false;
};
class Player {
private:
float m_yaw;
float m_pitch;
float m_sensitivity = 0.05f;
float m_speed = 0.1f;
// player is tow block tall, the pos is the lower pos
glm::vec3 m_playerPos;
glm::vec3 m_front = glm::vec3(0, 0, -1);
glm::vec3 m_right;
MoveState m_moveState;
public:
Player();
const glm::vec3& getFront() const;
const glm::vec3& getPlayerPos() const;
const MoveState& getMoveState() const;
void setPlayerPos(const glm::vec3& pos);
void update(float deltaTime);
void updateFrontVec(float offsetX, float offsetY);
void updatePlayerMoveState(int key, int action);
};

View File

@@ -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));
}

114
src/gameplay/player.cpp Normal file
View File

@@ -0,0 +1,114 @@
#include <Cubed/gameplay/player.hpp>
#include <GLFW/glfw3.h>
Player::Player() {
}
const glm::vec3& Player::getFront() const {
return m_front;
}
const glm::vec3& Player::getPlayerPos() const {
return m_playerPos;
}
const MoveState& Player::getMoveState() const {
return m_moveState;
}
void Player::setPlayerPos(const glm::vec3& pos) {
m_playerPos = pos;
}
void Player::update(float deltaTime) {
m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f)));
if (m_moveState.forward) {
m_playerPos += glm::vec3(m_front.x, 0.0f, m_front.z) * m_speed;
}
if (m_moveState.back) {
m_playerPos -= glm::vec3(m_front.x, 0.0f, m_front.z) * m_speed;
}
if (m_moveState.left) {
m_playerPos -= glm::vec3(m_right.x, 0.0f, m_right.z) * m_speed;
}
if (m_moveState.right) {
m_playerPos += glm::vec3(m_right.x, 0.0f, m_right.z) * m_speed;
}
if (m_moveState.up) {
m_playerPos += glm::vec3(0.0f, 1.0f, 0.0f) * m_speed;;
}
if (m_moveState.down) {
m_playerPos -= glm::vec3(0.0f, 1.0f, 0.0f) * m_speed;;
}
}
void Player::updatePlayerMoveState(int key, int action) {
switch(key) {
case GLFW_KEY_W:
if (action == GLFW_PRESS) {
m_moveState.forward = true;
}
if (action == GLFW_RELEASE) {
m_moveState.forward = false;
}
break;
case GLFW_KEY_S:
if (action == GLFW_PRESS) {
m_moveState.back = true;
}
if (action == GLFW_RELEASE) {
m_moveState.back = false;
}
break;
case GLFW_KEY_A:
if (action == GLFW_PRESS) {
m_moveState.left = true;
}
if (action == GLFW_RELEASE) {
m_moveState.left = false;
}
break;
case GLFW_KEY_D:
if (action == GLFW_PRESS) {
m_moveState.right = true;
}
if (action == GLFW_RELEASE) {
m_moveState.right = false;
}
break;
case GLFW_KEY_SPACE:
if (action == GLFW_PRESS) {
m_moveState.up = true;
}
if (action == GLFW_RELEASE) {
m_moveState.up = false;
}
break;
case GLFW_KEY_LEFT_SHIFT:
if (action == GLFW_PRESS) {
m_moveState.down = true;
}
if (action == GLFW_RELEASE) {
m_moveState.down = false;
}
break;
}
}
void Player::updateFrontVec(float offsetX, float offsetY) {
m_yaw += offsetX * m_sensitivity;
m_pitch += offsetY * m_sensitivity;
if (m_pitch > 89.0f) m_pitch = 89.0f;
if (m_pitch < -89.0f) m_pitch = -89.0f;
m_front.x = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front.y = sin(glm::radians(m_pitch));
m_front.z = -cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front = glm::normalize(m_front);
}

View File

@@ -12,6 +12,8 @@
#include <glm/gtc/type_ptr.hpp>
#include <Cubed/camera.hpp>
#include <Cubed/gameplay/player.hpp>
#include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp>
#include <Cubed/tools/shader_tools.hpp>
@@ -29,9 +31,12 @@ float aspect;
glm::mat4 pMat, vMat, mMat, mvMat;
float inc = 0.01f;
float tf = 0.0f;
double lastTime = 0.0f;
double deltaTime = 0.0f;
glm::mat4 tMat, rMat;
std::vector<GLuint> grass_block_texture(6);
Player player;
Camera camera;
void setupVertices(void) {
float verticesPos[108] = {
// ===== front (z = +1) =====
@@ -156,7 +161,7 @@ void init(GLFWwindow* window) {
mvLoc = glGetUniformLocation(renderingProgram, "mv_matrix");
projLoc = glGetUniformLocation(renderingProgram, "proj_matrix");
cameraInit();
camera.cameraInit(&player);
glfwGetFramebufferSize(window, &width, &height);
aspect = (float)width / (float)height;
glViewport(0, 0, width, height);
@@ -194,14 +199,16 @@ void window_reshape_callback(GLFWwindow* window, int newWidth, int newHeight) {
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
updateCursorPositionCamera(xpos, ypos);
camera.updateCursorPositionCamera(xpos, ypos);
}
void display(GLFWwindow* window, double currentTime) {
updateMoveCamera();
deltaTime = currentTime - lastTime;
lastTime = currentTime;
player.update(deltaTime);
camera.updateMoveCamera();
for (int i = 0; i < WORLD_SIZE_X; i++) {
for (int j = 0; j < WORLD_SIZE_Z; j++) {
@@ -233,7 +240,7 @@ void display(GLFWwindow* window, double currentTime) {
int wz = z - WORLD_SIZE_Z / 2;
mMat = glm::translate(glm::mat4(1.0f), glm::vec3((float)wx, 0.0f, (float)wz));
vMat = getCameraLookAt();
vMat = camera.getCameraLookAt();
mvMat = vMat * mMat;
glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mvMat));
glUniformMatrix4fv(projLoc, 1 ,GL_FALSE, glm::value_ptr(pMat));
@@ -286,7 +293,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
}
updateCameraKey(key, action);
player.updatePlayerMoveState(key, action);
}