mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
style: unify naming conventions across the project
This commit is contained in:
21
.clang-tidy
Normal file
21
.clang-tidy
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
Checks: '-*,readability-identifier-naming'
|
||||||
|
CheckOptions:
|
||||||
|
|
||||||
|
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
|
||||||
|
- { key: readability-identifier-naming.StructCase, value: CamelCase }
|
||||||
|
|
||||||
|
- { key: readability-identifier-naming.FunctionCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.MemberFunctionCase, value: lower_case }
|
||||||
|
|
||||||
|
- { key: readability-identifier-naming.VariableCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.GlobalVariableCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.LocalVariableCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.StaticVariableCase, value: lower_case }
|
||||||
|
- { key: readability-identifier-naming.ParameterCase, value: lower_case }
|
||||||
|
|
||||||
|
- { key: readability-identifier-naming.ConstantCase, value: UPPER_CASE }
|
||||||
|
- { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE }
|
||||||
|
- { key: readability-identifier-naming.MemberConstantCase, value: UPPER_CASE }
|
||||||
|
- { key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE }
|
||||||
|
|
||||||
|
- { key: readability-identifier-naming.MemberCase, value: lower_case }
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -21,6 +21,7 @@ Release/
|
|||||||
x64/
|
x64/
|
||||||
x86/
|
x86/
|
||||||
out/
|
out/
|
||||||
|
.cache/
|
||||||
.vs/
|
.vs/
|
||||||
.vscode/
|
.vscode/
|
||||||
.idea/
|
.idea/
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ project(Cubed LANGUAGES C CXX)
|
|||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
if(NOT CMAKE_BUILD_TYPE)
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
set(CMAKE_BUILD_TYPE Debug)
|
set(CMAKE_BUILD_TYPE Debug)
|
||||||
|
|||||||
@@ -12,23 +12,23 @@ class Player;
|
|||||||
class Camera {
|
class Camera {
|
||||||
private:
|
private:
|
||||||
|
|
||||||
bool m_firseMouse = true;
|
bool m_firse_mouse = true;
|
||||||
Player* m_player;
|
Player* m_player;
|
||||||
float m_lastMouseX, m_lastMouseY;
|
float m_last_mouse_x, m_last_mouse_y;
|
||||||
glm::vec3 m_cameraPos;
|
glm::vec3 m_camera_pos;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Camera();
|
Camera();
|
||||||
|
|
||||||
void updateMoveCamera();
|
void update_move_camera();
|
||||||
|
|
||||||
void cameraInit(Player* player);
|
void camera_init(Player* player);
|
||||||
|
|
||||||
void updateCursorPositionCamera(double xpos, double ypos);
|
void update_cursor_position_camera(double xpos, double ypos);
|
||||||
|
|
||||||
const glm::mat4 getCameraLookAt() const;
|
const glm::mat4 get_camera_lookat() const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ struct MoveState {
|
|||||||
class Player {
|
class Player {
|
||||||
private:
|
private:
|
||||||
|
|
||||||
bool** m_blockPresent;
|
bool** m_block_present;
|
||||||
|
|
||||||
float m_yaw;
|
float m_yaw;
|
||||||
float m_pitch;
|
float m_pitch;
|
||||||
@@ -24,23 +24,23 @@ private:
|
|||||||
|
|
||||||
float m_speed = 0.1f;
|
float m_speed = 0.1f;
|
||||||
// player is tow block tall, the pos is the lower pos
|
// player is tow block tall, the pos is the lower pos
|
||||||
glm::vec3 m_playerPos = glm::vec3(0.0f, 5.0f, 0.0f);
|
glm::vec3 m_player_pos = glm::vec3(0.0f, 5.0f, 0.0f);
|
||||||
glm::vec3 m_front = glm::vec3(0, 0, -1);
|
glm::vec3 m_front = glm::vec3(0, 0, -1);
|
||||||
glm::vec3 m_right;
|
glm::vec3 m_right;
|
||||||
MoveState m_moveState;
|
MoveState m_move_state;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Player();
|
Player();
|
||||||
|
|
||||||
const glm::vec3& getFront() const;
|
const glm::vec3& get_front() const;
|
||||||
const glm::vec3& getPlayerPos() const;
|
const glm::vec3& get_player_pos() const;
|
||||||
const MoveState& getMoveState() const;
|
const MoveState& get_move_state() const;
|
||||||
|
|
||||||
void init(bool** blockPresent);
|
void init(bool** block_present);
|
||||||
void setPlayerPos(const glm::vec3& pos);
|
void set_player_pos(const glm::vec3& pos);
|
||||||
void update(float deltaTime);
|
void update(float delta_time);
|
||||||
void updateFrontVec(float offsetX, float offsetY);
|
void update_front_vec(float offset_x, float offset_y);
|
||||||
void updatePlayerMoveState(int key, int action);
|
void update_player_move_state(int key, int action);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -1,14 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <fstream>
|
|
||||||
#include <string>
|
|
||||||
#include <SOIL2.h>
|
#include <SOIL2.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
void printShaderLog(GLuint shader);
|
|
||||||
void printProgramInfo(int prog);
|
void print_shader_log(GLuint shader);
|
||||||
bool checkOpenGLError();
|
void print_program_info(int prog);
|
||||||
std::string readShaderSource(const char* filePath);
|
bool check_opengl_error();
|
||||||
GLuint loadTexture(const char* texImagePath);
|
std::string read_shader_source(const char* file_path);
|
||||||
|
GLuint load_texture(const char* tex_image_path);
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
#include <memory>
|
|
||||||
#include <optional>
|
|
||||||
#include <Cubed/camera.hpp>
|
#include <Cubed/camera.hpp>
|
||||||
#include <Cubed/gameplay/player.hpp>
|
#include <Cubed/gameplay/player.hpp>
|
||||||
#include <Cubed/tools/cubed_assert.hpp>
|
#include <Cubed/tools/cubed_assert.hpp>
|
||||||
@@ -8,35 +6,35 @@ Camera::Camera() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::updateMoveCamera() {
|
void Camera::update_move_camera() {
|
||||||
CUBED_ASSERT_MSG(m_player, "nullptr");
|
CUBED_ASSERT_MSG(m_player, "nullptr");
|
||||||
auto pos = m_player->getPlayerPos();
|
auto pos = m_player->get_player_pos();
|
||||||
m_cameraPos = glm::vec3(pos.x, pos.y + 1, pos.z);
|
m_camera_pos = glm::vec3(pos.x, pos.y + 1, pos.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Camera::cameraInit(Player* _player) {
|
void Camera::camera_init(Player* player) {
|
||||||
m_cameraPos = glm::vec3(0.0f, 2.0f, 0.0f);
|
m_camera_pos = glm::vec3(0.0f, 2.0f, 0.0f);
|
||||||
m_player = _player;
|
m_player = player;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::updateCursorPositionCamera(double xpos, double ypos) {
|
void Camera::update_cursor_position_camera(double xpos, double ypos) {
|
||||||
if (m_firseMouse) {
|
if (m_firse_mouse) {
|
||||||
m_lastMouseX = xpos;
|
m_last_mouse_x = xpos;
|
||||||
m_lastMouseY = ypos;
|
m_last_mouse_y = ypos;
|
||||||
m_firseMouse = false;
|
m_firse_mouse = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
float offsetX = xpos - m_lastMouseX;
|
float offset_x = xpos - m_last_mouse_x;
|
||||||
float offsetY = m_lastMouseY - ypos;
|
float offset_y = m_last_mouse_y - ypos;
|
||||||
|
|
||||||
m_lastMouseX = xpos;
|
m_last_mouse_x = xpos;
|
||||||
m_lastMouseY = ypos;
|
m_last_mouse_y = ypos;
|
||||||
CUBED_ASSERT_MSG(m_player, "nullptr");
|
CUBED_ASSERT_MSG(m_player, "nullptr");
|
||||||
m_player->updateFrontVec(offsetX, offsetY);
|
m_player->update_front_vec(offset_x, offset_y);
|
||||||
}
|
}
|
||||||
|
|
||||||
const glm::mat4 Camera::getCameraLookAt() const{
|
const glm::mat4 Camera::get_camera_lookat() const{
|
||||||
CUBED_ASSERT_MSG(m_player, "nullptr");
|
CUBED_ASSERT_MSG(m_player, "nullptr");
|
||||||
return glm::lookAt(m_cameraPos, m_cameraPos + m_player->getFront(), glm::vec3(0.0f, 1.0f, 0.0f));
|
return glm::lookAt(m_camera_pos, m_camera_pos + m_player->get_front(), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,111 +1,112 @@
|
|||||||
#include <Cubed/gameplay/player.hpp>
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
static constexpr int UPDATE_TIME = 0.1f;
|
#include <Cubed/gameplay/player.hpp>
|
||||||
static float currentTime = 0.0f;
|
|
||||||
|
static constexpr float UPDATE_TIME = 0.005f;
|
||||||
|
static float current_time = 0.0f;
|
||||||
|
|
||||||
|
|
||||||
Player::Player() {
|
Player::Player() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const glm::vec3& Player::getFront() const {
|
const glm::vec3& Player::get_front() const {
|
||||||
return m_front;
|
return m_front;
|
||||||
}
|
}
|
||||||
|
|
||||||
const glm::vec3& Player::getPlayerPos() const {
|
const glm::vec3& Player::get_player_pos() const {
|
||||||
return m_playerPos;
|
return m_player_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
const MoveState& Player::getMoveState() const {
|
const MoveState& Player::get_move_state() const {
|
||||||
return m_moveState;
|
return m_move_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::init(bool** blockPresent) {
|
void Player::init(bool** block_present) {
|
||||||
m_blockPresent = blockPresent;
|
m_block_present = block_present;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::setPlayerPos(const glm::vec3& pos) {
|
void Player::set_player_pos(const glm::vec3& pos) {
|
||||||
m_playerPos = pos;
|
m_player_pos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::update(float deltaTime) {
|
void Player::update(float delta_time) {
|
||||||
|
|
||||||
currentTime += deltaTime;
|
current_time += delta_time;
|
||||||
if (currentTime < UPDATE_TIME) {
|
if (current_time < UPDATE_TIME) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
currentTime = 0.0f;
|
current_time = 0.0f;
|
||||||
m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f)));
|
m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f)));
|
||||||
if (m_moveState.forward) {
|
if (m_move_state.forward) {
|
||||||
m_playerPos += glm::vec3(m_front.x, 0.0f, m_front.z) * m_speed;
|
m_player_pos += glm::vec3(m_front.x, 0.0f, m_front.z) * m_speed;
|
||||||
}
|
}
|
||||||
if (m_moveState.back) {
|
if (m_move_state.back) {
|
||||||
m_playerPos -= glm::vec3(m_front.x, 0.0f, m_front.z) * m_speed;
|
m_player_pos -= glm::vec3(m_front.x, 0.0f, m_front.z) * m_speed;
|
||||||
}
|
}
|
||||||
if (m_moveState.left) {
|
if (m_move_state.left) {
|
||||||
m_playerPos -= glm::vec3(m_right.x, 0.0f, m_right.z) * m_speed;
|
m_player_pos -= glm::vec3(m_right.x, 0.0f, m_right.z) * m_speed;
|
||||||
}
|
}
|
||||||
if (m_moveState.right) {
|
if (m_move_state.right) {
|
||||||
m_playerPos += glm::vec3(m_right.x, 0.0f, m_right.z) * m_speed;
|
m_player_pos += glm::vec3(m_right.x, 0.0f, m_right.z) * m_speed;
|
||||||
}
|
}
|
||||||
if (m_moveState.up) {
|
if (m_move_state.up) {
|
||||||
m_playerPos += glm::vec3(0.0f, 1.0f, 0.0f) * m_speed;
|
m_player_pos += glm::vec3(0.0f, 1.0f, 0.0f) * m_speed;
|
||||||
}
|
}
|
||||||
if (m_moveState.down) {
|
if (m_move_state.down) {
|
||||||
m_playerPos -= glm::vec3(0.0f, 1.0f, 0.0f) * m_speed;
|
m_player_pos -= glm::vec3(0.0f, 1.0f, 0.0f) * m_speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::updatePlayerMoveState(int key, int action) {
|
void Player::update_player_move_state(int key, int action) {
|
||||||
switch(key) {
|
switch(key) {
|
||||||
case GLFW_KEY_W:
|
case GLFW_KEY_W:
|
||||||
if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
m_moveState.forward = true;
|
m_move_state.forward = true;
|
||||||
}
|
}
|
||||||
if (action == GLFW_RELEASE) {
|
if (action == GLFW_RELEASE) {
|
||||||
m_moveState.forward = false;
|
m_move_state.forward = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case GLFW_KEY_S:
|
case GLFW_KEY_S:
|
||||||
if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
m_moveState.back = true;
|
m_move_state.back = true;
|
||||||
}
|
}
|
||||||
if (action == GLFW_RELEASE) {
|
if (action == GLFW_RELEASE) {
|
||||||
m_moveState.back = false;
|
m_move_state.back = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case GLFW_KEY_A:
|
case GLFW_KEY_A:
|
||||||
if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
m_moveState.left = true;
|
m_move_state.left = true;
|
||||||
}
|
}
|
||||||
if (action == GLFW_RELEASE) {
|
if (action == GLFW_RELEASE) {
|
||||||
m_moveState.left = false;
|
m_move_state.left = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case GLFW_KEY_D:
|
case GLFW_KEY_D:
|
||||||
if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
m_moveState.right = true;
|
m_move_state.right = true;
|
||||||
}
|
}
|
||||||
if (action == GLFW_RELEASE) {
|
if (action == GLFW_RELEASE) {
|
||||||
m_moveState.right = false;
|
m_move_state.right = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case GLFW_KEY_SPACE:
|
case GLFW_KEY_SPACE:
|
||||||
if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
m_moveState.up = true;
|
m_move_state.up = true;
|
||||||
}
|
}
|
||||||
if (action == GLFW_RELEASE) {
|
if (action == GLFW_RELEASE) {
|
||||||
m_moveState.up = false;
|
m_move_state.up = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case GLFW_KEY_LEFT_SHIFT:
|
case GLFW_KEY_LEFT_SHIFT:
|
||||||
if (action == GLFW_PRESS) {
|
if (action == GLFW_PRESS) {
|
||||||
m_moveState.down = true;
|
m_move_state.down = true;
|
||||||
}
|
}
|
||||||
if (action == GLFW_RELEASE) {
|
if (action == GLFW_RELEASE) {
|
||||||
m_moveState.down = false;
|
m_move_state.down = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -113,9 +114,9 @@ void Player::updatePlayerMoveState(int key, int action) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::updateFrontVec(float offsetX, float offsetY) {
|
void Player::update_front_vec(float offset_x, float offset_y) {
|
||||||
m_yaw += offsetX * m_sensitivity;
|
m_yaw += offset_x * m_sensitivity;
|
||||||
m_pitch += offsetY * m_sensitivity;
|
m_pitch += offset_y * m_sensitivity;
|
||||||
|
|
||||||
|
|
||||||
if (m_pitch > 89.0f) m_pitch = 89.0f;
|
if (m_pitch > 89.0f) m_pitch = 89.0f;
|
||||||
|
|||||||
160
src/main.cpp
160
src/main.cpp
@@ -1,6 +1,3 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <stack>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -18,26 +15,25 @@
|
|||||||
#include <Cubed/tools/log.hpp>
|
#include <Cubed/tools/log.hpp>
|
||||||
#include <Cubed/tools/shader_tools.hpp>
|
#include <Cubed/tools/shader_tools.hpp>
|
||||||
|
|
||||||
constexpr int numVAOs = 1;
|
constexpr int NUM_VAO = 1;
|
||||||
constexpr int numVBOs = 3;
|
constexpr int NUM_VBO = 3;
|
||||||
bool blockPresent[WORLD_SIZE_X][WORLD_SIZE_Z] = {false};
|
bool block_present[WORLD_SIZE_X][WORLD_SIZE_Z] = {false};
|
||||||
GLuint renderingProgram;
|
GLuint rendering_program;
|
||||||
GLuint vao[numVAOs];
|
GLuint vao[NUM_VAO];
|
||||||
GLuint vbo[numVBOs];
|
GLuint vbo[NUM_VBO];
|
||||||
GLuint mvLoc, projLoc;
|
GLuint mv_loc, proj_loc;
|
||||||
int width ,height;
|
int width ,height;
|
||||||
float aspect;
|
float aspect;
|
||||||
glm::mat4 pMat, vMat, mMat, mvMat;
|
glm::mat4 p_mat, v_mat, m_mat, mv_mat;
|
||||||
float inc = 0.01f;
|
float inc = 0.01f;
|
||||||
float tf = 0.0f;
|
float tf = 0.0f;
|
||||||
double lastTime = 0.0f;
|
double last_time = 0.0f;
|
||||||
double deltaTime = 0.0f;
|
double delta_time = 0.0f;
|
||||||
glm::mat4 tMat, rMat;
|
|
||||||
std::vector<GLuint> grass_block_texture(6);
|
std::vector<GLuint> grass_block_texture(6);
|
||||||
Player player;
|
Player player;
|
||||||
Camera camera;
|
Camera camera;
|
||||||
void setupVertices(void) {
|
void setup_vertices(void) {
|
||||||
float verticesPos[108] = {
|
float vertices_pos[108] = {
|
||||||
// ===== front (z = +1) =====
|
// ===== front (z = +1) =====
|
||||||
-0.5f, -0.5f, 0.5f, // bottom left
|
-0.5f, -0.5f, 0.5f, // bottom left
|
||||||
-0.5f, 0.5f, 0.5f, // top left
|
-0.5f, 0.5f, 0.5f, // top left
|
||||||
@@ -93,12 +89,12 @@ void setupVertices(void) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
glGenVertexArrays(numVAOs, vao);
|
glGenVertexArrays(NUM_VAO, vao);
|
||||||
glBindVertexArray(vao[0]);
|
glBindVertexArray(vao[0]);
|
||||||
glGenBuffers(numVBOs, vbo);
|
glGenBuffers(NUM_VBO, vbo);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesPos), verticesPos, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_pos), vertices_pos, GL_STATIC_DRAW);
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(tex_coords), tex_coords, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(tex_coords), tex_coords, GL_STATIC_DRAW);
|
||||||
@@ -111,67 +107,67 @@ void setupVertices(void) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
GLuint createShaderProgram() {
|
GLuint create_shader_program() {
|
||||||
std::string vShaderStr = readShaderSource("shaders/vShader.glsl");
|
std::string v_shader_str = read_shader_source("shaders/vShader.glsl");
|
||||||
std::string fShaderStr = readShaderSource("shaders/fShader.glsl");
|
std::string f_shader_str = read_shader_source("shaders/fShader.glsl");
|
||||||
const char *vshaderSource = vShaderStr.c_str();
|
const char *v_shader_source = v_shader_str.c_str();
|
||||||
const char *fshaderSource = fShaderStr.c_str();
|
const char *f_shader_source = f_shader_str.c_str();
|
||||||
|
|
||||||
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
|
GLuint v_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
|
GLuint f_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
GLint vc, fc;
|
GLint vc, fc;
|
||||||
glShaderSource(vShader, 1, &vshaderSource, NULL);
|
glShaderSource(v_shader, 1, &v_shader_source, NULL);
|
||||||
glShaderSource(fShader, 1, &fshaderSource, NULL);
|
glShaderSource(f_shader, 1, &f_shader_source, NULL);
|
||||||
glCompileShader(vShader);
|
glCompileShader(v_shader);
|
||||||
checkOpenGLError();
|
check_opengl_error();
|
||||||
glGetShaderiv(vShader, GL_COMPILE_STATUS, &vc);
|
glGetShaderiv(v_shader, GL_COMPILE_STATUS, &vc);
|
||||||
if (vc != 1) {
|
if (vc != 1) {
|
||||||
LOG::error("vertex compilation failed");
|
LOG::error("vertex compilation failed");
|
||||||
printShaderLog(vShader);
|
print_shader_log(v_shader);
|
||||||
}
|
}
|
||||||
glCompileShader(fShader);
|
glCompileShader(f_shader);
|
||||||
checkOpenGLError();
|
check_opengl_error();
|
||||||
glGetShaderiv(fShader, GL_COMPILE_STATUS, &fc);
|
glGetShaderiv(f_shader, GL_COMPILE_STATUS, &fc);
|
||||||
if (fc != 1) {
|
if (fc != 1) {
|
||||||
LOG::error("vertex compilation failed");
|
LOG::error("vertex compilation failed");
|
||||||
printShaderLog(fShader);
|
print_shader_log(f_shader);
|
||||||
}
|
}
|
||||||
GLuint vfProgram = glCreateProgram();
|
GLuint vf_program = glCreateProgram();
|
||||||
glAttachShader(vfProgram, vShader);
|
glAttachShader(vf_program, v_shader);
|
||||||
glAttachShader(vfProgram, fShader);
|
glAttachShader(vf_program, f_shader);
|
||||||
glLinkProgram(vfProgram);
|
glLinkProgram(vf_program);
|
||||||
|
|
||||||
GLint linked;
|
GLint linked;
|
||||||
checkOpenGLError();
|
check_opengl_error();
|
||||||
glGetProgramiv(vfProgram, GL_LINK_STATUS, &linked);
|
glGetProgramiv(vf_program, GL_LINK_STATUS, &linked);
|
||||||
if (linked != 1) {
|
if (linked != 1) {
|
||||||
LOG::error("linking failed");
|
LOG::error("linking failed");
|
||||||
printProgramInfo(vfProgram);
|
print_program_info(vf_program);
|
||||||
}
|
}
|
||||||
|
|
||||||
return vfProgram;
|
return vf_program;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void init(GLFWwindow* window) {
|
void init(GLFWwindow* window) {
|
||||||
renderingProgram = createShaderProgram();
|
rendering_program = create_shader_program();
|
||||||
|
|
||||||
mvLoc = glGetUniformLocation(renderingProgram, "mv_matrix");
|
mv_loc = glGetUniformLocation(rendering_program, "mv_matrix");
|
||||||
projLoc = glGetUniformLocation(renderingProgram, "proj_matrix");
|
proj_loc = glGetUniformLocation(rendering_program, "proj_matrix");
|
||||||
|
|
||||||
camera.cameraInit(&player);
|
camera.camera_init(&player);
|
||||||
glfwGetFramebufferSize(window, &width, &height);
|
glfwGetFramebufferSize(window, &width, &height);
|
||||||
aspect = (float)width / (float)height;
|
aspect = (float)width / (float)height;
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
pMat = glm::perspective(glm::radians(60.0f), aspect, 0.1f, 1000.0f);
|
p_mat = glm::perspective(glm::radians(60.0f), aspect, 0.1f, 1000.0f);
|
||||||
|
|
||||||
grass_block_texture[0] = loadTexture("assets/texture/block/grass_block/front.png");
|
grass_block_texture[0] = load_texture("assets/texture/block/grass_block/front.png");
|
||||||
grass_block_texture[1] = loadTexture("assets/texture/block/grass_block/right.png");
|
grass_block_texture[1] = load_texture("assets/texture/block/grass_block/right.png");
|
||||||
grass_block_texture[2] = loadTexture("assets/texture/block/grass_block/back.png");
|
grass_block_texture[2] = load_texture("assets/texture/block/grass_block/back.png");
|
||||||
grass_block_texture[3] = loadTexture("assets/texture/block/grass_block/left.png");
|
grass_block_texture[3] = load_texture("assets/texture/block/grass_block/left.png");
|
||||||
grass_block_texture[4] = loadTexture("assets/texture/block/grass_block/top.png");
|
grass_block_texture[4] = load_texture("assets/texture/block/grass_block/top.png");
|
||||||
grass_block_texture[5] = loadTexture("assets/texture/block/grass_block/base.png");
|
grass_block_texture[5] = load_texture("assets/texture/block/grass_block/base.png");
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
glBindTexture(GL_TEXTURE_2D, grass_block_texture[i]);
|
glBindTexture(GL_TEXTURE_2D, grass_block_texture[i]);
|
||||||
@@ -183,35 +179,35 @@ void init(GLFWwindow* window) {
|
|||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glDepthFunc(GL_LEQUAL);
|
glDepthFunc(GL_LEQUAL);
|
||||||
|
|
||||||
setupVertices();
|
setup_vertices();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void window_reshape_callback(GLFWwindow* window, int newWidth, int newHeight) {
|
void window_reshape_callback(GLFWwindow* window, int new_width, int new_height) {
|
||||||
glfwGetFramebufferSize(window, &width, &height);
|
glfwGetFramebufferSize(window, &width, &height);
|
||||||
aspect = (float)width / (float)height;
|
aspect = (float)width / (float)height;
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
pMat = glm::perspective(glm::radians(60.0f), aspect, 0.1f, 1000.0f);
|
p_mat = glm::perspective(glm::radians(60.0f), aspect, 0.1f, 1000.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
|
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||||
camera.updateCursorPositionCamera(xpos, ypos);
|
camera.update_cursor_position_camera(xpos, ypos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void display(GLFWwindow* window, double currentTime) {
|
void display(GLFWwindow* window, double current_time) {
|
||||||
deltaTime = currentTime - lastTime;
|
delta_time = current_time - last_time;
|
||||||
lastTime = currentTime;
|
last_time = current_time;
|
||||||
player.update(deltaTime);
|
player.update(delta_time);
|
||||||
camera.updateMoveCamera();
|
camera.update_move_camera();
|
||||||
|
|
||||||
for (int i = 0; i < WORLD_SIZE_X; i++) {
|
for (int i = 0; i < WORLD_SIZE_X; i++) {
|
||||||
for (int j = 0; j < WORLD_SIZE_Z; j++) {
|
for (int j = 0; j < WORLD_SIZE_Z; j++) {
|
||||||
blockPresent[i][j] = true;
|
block_present[i][j] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,7 +215,7 @@ void display(GLFWwindow* window, double currentTime) {
|
|||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glClear(GL_DEPTH_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glUseProgram(renderingProgram);
|
glUseProgram(rendering_program);
|
||||||
glBindVertexArray(vao[0]);
|
glBindVertexArray(vao[0]);
|
||||||
|
|
||||||
|
|
||||||
@@ -238,30 +234,30 @@ void display(GLFWwindow* window, double currentTime) {
|
|||||||
int wx = x - WORLD_SIZE_X / 2;
|
int wx = x - WORLD_SIZE_X / 2;
|
||||||
int wz = z - WORLD_SIZE_Z / 2;
|
int wz = z - WORLD_SIZE_Z / 2;
|
||||||
|
|
||||||
mMat = glm::translate(glm::mat4(1.0f), glm::vec3((float)wx, 0.0f, (float)wz));
|
m_mat = glm::translate(glm::mat4(1.0f), glm::vec3((float)wx, 0.0f, (float)wz));
|
||||||
vMat = camera.getCameraLookAt();
|
v_mat = camera.get_camera_lookat();
|
||||||
mvMat = vMat * mMat;
|
mv_mat = v_mat * m_mat;
|
||||||
glUniformMatrix4fv(mvLoc, 1, GL_FALSE, glm::value_ptr(mvMat));
|
glUniformMatrix4fv(mv_loc, 1, GL_FALSE, glm::value_ptr(mv_mat));
|
||||||
glUniformMatrix4fv(projLoc, 1 ,GL_FALSE, glm::value_ptr(pMat));
|
glUniformMatrix4fv(proj_loc, 1 ,GL_FALSE, glm::value_ptr(p_mat));
|
||||||
|
|
||||||
bool drawFace[6] = {true, true, true, true, true, true};
|
bool draw_face[6] = {true, true, true, true, true, true};
|
||||||
|
|
||||||
if (z < WORLD_SIZE_Z - 1&& blockPresent[x][z + 1]) {
|
if (z < WORLD_SIZE_Z - 1&& block_present[x][z + 1]) {
|
||||||
drawFace[0] = false;
|
draw_face[0] = false;
|
||||||
}
|
}
|
||||||
if (x < WORLD_SIZE_X - 1 && blockPresent[x + 1][z]) {
|
if (x < WORLD_SIZE_X - 1 && block_present[x + 1][z]) {
|
||||||
drawFace[1] = false;
|
draw_face[1] = false;
|
||||||
}
|
}
|
||||||
if (z > 0 && blockPresent[x][z - 1]) {
|
if (z > 0 && block_present[x][z - 1]) {
|
||||||
drawFace[2] = false;
|
draw_face[2] = false;
|
||||||
}
|
}
|
||||||
if (x > 0 && blockPresent[x - 1][z]) {
|
if (x > 0 && block_present[x - 1][z]) {
|
||||||
drawFace[3] = false;
|
draw_face[3] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (int face = 0; face < 6; face++) {
|
for (int face = 0; face < 6; face++) {
|
||||||
if (!drawFace[face]) {
|
if (!draw_face[face]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
glBindTexture(GL_TEXTURE_2D, grass_block_texture[face]);
|
glBindTexture(GL_TEXTURE_2D, grass_block_texture[face]);
|
||||||
@@ -292,7 +288,7 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
player.updatePlayerMoveState(key, action);
|
player.update_player_move_state(key, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include <Cubed/tools/shader_tools.hpp>
|
#include <Cubed/tools/shader_tools.hpp>
|
||||||
#include <Cubed/tools/log.hpp>
|
#include <Cubed/tools/log.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void printShaderLog(GLuint shader) {
|
void print_shader_log(GLuint shader) {
|
||||||
int len = 0;
|
int len = 0;
|
||||||
int chWritten = 0;
|
int ch_written = 0;
|
||||||
char *log;
|
char *log;
|
||||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
log = (char*)malloc(len);
|
log = (char*)malloc(len);
|
||||||
glGetShaderInfoLog(shader, len, &chWritten, log);
|
glGetShaderInfoLog(shader, len, &ch_written, log);
|
||||||
LOG::info("Shader Info Log: {}", log);
|
LOG::info("Shader Info Log: {}", log);
|
||||||
free(log);
|
free(log);
|
||||||
}
|
}
|
||||||
@@ -18,55 +20,55 @@ void printShaderLog(GLuint shader) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void printProgramInfo(int prog) {
|
void print_program_info(int prog) {
|
||||||
int len = 0;
|
int len = 0;
|
||||||
int chWritten = 0;
|
int ch_written = 0;
|
||||||
char *log;
|
char *log;
|
||||||
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
|
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
log = (char*)malloc(len);
|
log = (char*)malloc(len);
|
||||||
glGetProgramInfoLog(prog, len, &chWritten, log);
|
glGetProgramInfoLog(prog, len, &ch_written, log);
|
||||||
LOG::info("Program Info Log: {}", log);
|
LOG::info("Program Info Log: {}", log);
|
||||||
free(log);
|
free(log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checkOpenGLError() {
|
bool check_opengl_error() {
|
||||||
bool foundError = false;
|
bool found_error = false;
|
||||||
int glErr = glGetError();
|
int gl_err = glGetError();
|
||||||
while (glErr != GL_NO_ERROR) {
|
while (gl_err != GL_NO_ERROR) {
|
||||||
LOG::error("glEorr: {}", glErr);
|
LOG::error("glEorr: {}", gl_err);
|
||||||
foundError = true;
|
found_error = true;
|
||||||
glErr = glGetError();
|
gl_err = glGetError();
|
||||||
}
|
}
|
||||||
|
|
||||||
return foundError;
|
return found_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string readShaderSource(const char* filePath) {
|
std::string read_shader_source(const char* file_path) {
|
||||||
std::string content;
|
std::string content;
|
||||||
std::ifstream fileStream(filePath, std::ios::in);
|
std::ifstream file_stream(file_path, std::ios::in);
|
||||||
|
|
||||||
if (!fileStream.is_open()) {
|
if (!file_stream.is_open()) {
|
||||||
LOG::error("file not exist");
|
LOG::error("file not exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string line = "";
|
std::string line = "";
|
||||||
while (!fileStream.eof()) {
|
while (!file_stream.eof()) {
|
||||||
|
|
||||||
getline(fileStream, line);
|
getline(file_stream, line);
|
||||||
content.append(line + "\n");
|
content.append(line + "\n");
|
||||||
}
|
}
|
||||||
fileStream.close();
|
file_stream.close();
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint loadTexture(const char* texImagePath) {
|
GLuint load_texture(const char* tex_image_path) {
|
||||||
GLuint textureID;
|
GLuint texture_id;
|
||||||
textureID = SOIL_load_OGL_texture(texImagePath, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
|
texture_id = SOIL_load_OGL_texture(tex_image_path, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
|
||||||
if (textureID == 0) {
|
if (texture_id == 0) {
|
||||||
LOG::error("could not find texture file");
|
LOG::error("could not find texture file");
|
||||||
}
|
}
|
||||||
return textureID;
|
return texture_id;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user