diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b94395..0d1e6b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/include/Cubed/camera.hpp b/include/Cubed/camera.hpp index e5c236c..a5f5765 100644 --- a/include/Cubed/camera.hpp +++ b/include/Cubed/camera.hpp @@ -6,24 +6,30 @@ #include #include +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); diff --git a/include/Cubed/gameplay/player.hpp b/include/Cubed/gameplay/player.hpp new file mode 100644 index 0000000..baa958e --- /dev/null +++ b/include/Cubed/gameplay/player.hpp @@ -0,0 +1,41 @@ +#pragma once +#include + +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); + + +}; \ No newline at end of file diff --git a/src/camera.cpp b/src/camera.cpp index 13afe44..ad67644 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -1,131 +1,41 @@ -#include "Cubed/camera.hpp" +#include +#include +#include +#include +#include -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)); } diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp new file mode 100644 index 0000000..7d1946a --- /dev/null +++ b/src/gameplay/player.cpp @@ -0,0 +1,114 @@ +#include +#include +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); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 70c31bd..a5a625a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,8 @@ #include #include +#include +#include #include #include @@ -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 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); }