Compare commits

...

5 Commits

17 changed files with 512 additions and 463 deletions

View File

@ -6,16 +6,16 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED true)
include_directories(deps)
include_directories(src/common)
include_directories("${CMAKE_SOURCE_DIR}/src")
file(GLOB_RECURSE Sources "src/**.cpp" "src/**.h")
add_executable(${PROJECT_NAME} ${Sources})
if(UNIX AND NOT APPLE) # linux
target_link_libraries(${PROJECT_NAME} GL GLEW glfw)
target_link_libraries(${PROJECT_NAME} GL GLEW glfw)
endif()
if (WIN32) # windows
target_link_libraries(${PROJECT_NAME} opengl32 glew32 glfw3)
endif(WIN32)
if(WIN32) # windows
target_link_libraries(${PROJECT_NAME} opengl32 glew32 glfw3)
endif(WIN32)

View File

@ -11,4 +11,4 @@ PP OOOOOO LLLLL YY GGGGGGG UUUUUU NN NNNN
## Contributing
### See `CODING_STYLE.txt`
### See `CODING_STYLE.MD`

0
src/audio/audio.cpp Normal file
View File

4
src/audio/audio.hpp Normal file
View File

@ -0,0 +1,4 @@
#ifndef POLYGUN_AUDIO_HPP
#define POLYGUN_AUDIO_HPP
#endif // POLYGUN_AUDIO_HPP

View File

View File

@ -0,0 +1,4 @@
#ifndef POLYGUN_AUDIO_ENCODER_HPP
#define POLYGUN_AUDIO_ENCODER_HPP
#endif // POLYGUN_AUDIO_ENCODER_HPP

View File

View File

@ -0,0 +1,4 @@
#ifndef POLYGUN_AUDIO_PLAYER_HPP
#define POLYGUN_AUDIO_PLAYER_HPP
#endif // POLYGUN_AUDIO_PLAYER_HPP

View File

@ -3,192 +3,193 @@
using namespace polygun::renderer;
namespace polygun::engine {
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
void Engine::init() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
void Engine::init() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Blocking3D", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, camera.mouse_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Blocking3D", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, m_camera.mouse_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glewInit() != GLEW_OK) {
std::cout << "GLEW init failed" << std::endl;
return;
}
if (glewInit() != GLEW_OK) {
std::cout << "GLEW init failed" << std::endl;
return;
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
Shader chunk_shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl");
Shader chunk_shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl");
float vertices[] = {
-0.1f, -0.1f, -0.1f, 0.0f, 0.0f,
0.1f, -0.1f, -0.1f, 1.0f, 0.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f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f, 0.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f
};
//32x32x32 chunk | ONLY FOR TEST THIS IS'NT EVEN A CHUNK
glm::vec3 cube_positions[1024];
for(int i = 0; i < 1024; i++){
cubePositions[i] = glm::vec3(0.2f * (i % 32), 0.2f * (i / 32), 0.2f * (i / 32));
}
for(int i = 0; i < 1024; i++){
cube_positions[i] = glm::vec3(0.2f * (i % 32), 0.2f * (i / 32), 0.2f * (i / 32));
}
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
unsigned int texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load("res/textures/grass.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load("res/textures/grass.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
chunk_shader.bind();
chunk_shader.setuniform("texture1", 0);
chunk_shader.bind();
chunk_shader.set_uniform("texture1", 0);
m_delta_time = 0;
m_last_frame = 0;
while (!glfwWindowShouldClose(window))
{
float current_frame = static_cast<float>(glfwGetTime());
m_delta_time = current_frame - m_last_frame;
m_last_frame = current_frame;
// input
// -----
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
m_camera.process_movement(polygun::engine::FORWARD, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
m_camera.process_movement(polygun::engine::BACKWARD, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
m_camera.process_movement(LEFT, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
m_camera.process_movement(polygun::engine::RIGHT, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
m_camera.process_movement(polygun::engine::UP, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
m_camera.process_movement(polygun::engine::DOWN, m_delta_time);
while (!glfwWindowShouldClose(window))
{
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
m_camera.update();
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// input
// -----
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// activate shader
chunk_shader.bind();
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessMovement(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::DOWN, deltaTime);
glm::mat4 projection = glm::perspective(glm::radians(m_camera.m_zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
chunk_shader.set_uniform("projection", projection);
glm::mat4 view = m_camera.get_view_matrix();
chunk_shader.set_uniform("view", view);
camera.Update();
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO);
for (unsigned int i = 0; i < (sizeof(cube_positions)/sizeof(*cube_positions)) ; i++)
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cube_positions[i]);
float angle = 20.0f * i;
chunk_shader.set_uniform("model", model);
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// activate shader
chunk_shader.bind();
glfwSwapBuffers(window);
glfwPollEvents();
}
glm::mat4 projection = glm::perspective(glm::radians(camera.m_zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
chunk_shader.setuniform("projection", projection);
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glm::mat4 view = camera.GetViewMatrix();
chunk_shader.setuniform("view", view);
glBindVertexArray(VAO);
for (unsigned int i = 0; i < (sizeof(cubePositions)/sizeof(*cubePositions)) ; i++)
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
chunk_shader.setuniform("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
}
}
glfwTerminate();
}
}

View File

@ -1,27 +1,28 @@
#ifndef POLYGUN_ENGINE_HPP
#define POLYGUN_ENGINE_HPP
#include "../core.hpp"
#include "../renderer/shader.hpp"
#include "player_camera.hpp"
namespace polygun::engine{
#include "game/core.hpp"
#include "game/renderer/shader.hpp"
#include "game/engine/player_camera.hpp"
class Engine {
private:
GLFWwindow* window;
Camera camera;
namespace polygun::engine {
float deltaTime = 0.0f;
float lastFrame = 0.0f;
class Engine {
private:
GLFWwindow* m_window;
Camera m_camera;
public:
Engine() = default;
~Engine() = default;
float m_delta_time;
float m_last_frame;
void init();
void render();
void update();
};
public:
Engine() = default;
~Engine() = default;
void init();
void render();
void update();
};
}
#endif // POLYGUN_ENGINE_HPP
#endif // POLYGUN_ENGINE_HPP

View File

@ -4,18 +4,18 @@
#include "../core.hpp"
namespace polygun::engine {
/*
struct Node {
std::string name;
std::string texture;
bool spriteNode = false;
};
/*
struct Node {
std::string name;
std::string texture;
bool spriteNode = false;
};
Node nodes[] = {
{"cobble stone", "res/textures/cobble.png"}
{"iron block", "res/textures/iron.png"}
};
*/
Node nodes[] = {
{"cobble stone", "res/textures/cobble.png"}
{"iron block", "res/textures/iron.png"}
};
*/
}
#endif // POLYGUN_ENGINE_NODES_HPP

View File

@ -0,0 +1,113 @@
#include "game/engine/player_camera.hpp"
using namespace polygun::engine;
static float mx,my;
Camera::Camera(glm::vec3 position, glm::vec3 up, float yaw, float pitch) :
m_position(position),
m_front(glm::vec3(0.0f, 0.0f, -1.0f)),
m_up(),
m_right(),
m_worldup(up),
m_yaw(yaw),
m_pitch(pitch),
m_movement_speed(SPEED),
m_mouse_sensitivity(SENSITIVITY),
m_zoom(ZOOM),
m_last_x(SCR_WIDTH / 2.0f),
m_last_y(SCR_HEIGHT / 2.0f),
m_first_mouse(true)
{
update_camera_vectors();
}
Camera::Camera(float pos_x, float pos_y, float pos_z, float up_x, float up_y, float up_z, float yaw, float pitch) :
m_position(pos_x, pos_y, pos_z),
m_front(glm::vec3(0.0f, 0.0f, -1.0f)),
m_up(),
m_right(),
m_worldup(up_x, up_y, up_z),
m_yaw(yaw),
m_pitch(pitch),
m_movement_speed(SPEED),
m_mouse_sensitivity(SENSITIVITY),
m_zoom(ZOOM),
m_last_x(SCR_WIDTH / 2.0f),
m_last_y(SCR_HEIGHT / 2.0f),
m_first_mouse(true)
{
update_camera_vectors();
}
void Camera::update() {
if (m_first_mouse) {
m_last_x = mx;
m_last_y = my;
m_first_mouse = false;
}
float x_offset = mx - m_last_x;
float y_offset = m_last_y - my;
m_last_x = mx;
m_last_y = my;
process_mouse_movement(x_offset, y_offset);
}
void Camera::process_movement(camera_movement direction, float delta_time) {
float velocity = m_movement_speed * delta_time;
if (direction == FORWARD)
m_position += m_front * velocity;
if (direction == BACKWARD)
m_position -= m_front * velocity;
if (direction == LEFT)
m_position -= m_right * velocity;
if (direction == RIGHT)
m_position += m_right * velocity;
if (direction == UP)
m_position += glm::vec3(0.0F, 1.0F, 0.0F) * velocity;
if (direction == DOWN)
m_position += glm::vec3(0.0F, -1.0F, 0.0F) * velocity;
}
void Camera::process_mouse_movement(float x_offset, float y_offset, GLboolean constrain_pitch) {
x_offset *= m_mouse_sensitivity;
y_offset *= m_mouse_sensitivity;
m_yaw += x_offset;
m_pitch += y_offset;
if (constrain_pitch) {
if (m_pitch > 89.0f)
m_pitch = 89.0f;
if (m_pitch < -89.0f)
m_pitch = -89.0f;
}
update_camera_vectors();
}
void Camera::process_mouse_scroll(float y_offset) {
m_zoom -= y_offset;
if (m_zoom < 1.0f)
m_zoom = 1.0f;
if (m_zoom > 45.0f)
m_zoom = 45.0f;
}
void Camera::mouse_callback(GLFWwindow* window, double x_pos_in, double y_pos_in) {
mx = static_cast<float>(x_pos_in);
my = static_cast<float>(y_pos_in);
}
void Camera::update_camera_vectors() {
glm::vec3 front;
front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
front.y = sin(glm::radians(m_pitch));
front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front = glm::normalize(front);
m_right = glm::normalize(glm::cross(m_front, m_worldup));
m_up = glm::normalize(glm::cross(m_right, m_front));
}

View File

@ -1,141 +1,58 @@
#ifndef POLYGUN_ENGINE_CAMERA_H
#define POLYGUN_ENGINE_CAMERA_H
#include "../core.hpp"
#include "game/core.hpp"
namespace polygun::engine{
enum camera_movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT,
UP,
DOWN
};
namespace polygun::engine {
enum camera_movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT,
UP,
DOWN
};
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
static float mx,my;
class Camera {
public:
glm::vec3 m_position;
glm::vec3 m_front;
glm::vec3 m_up;
glm::vec3 m_right;
glm::vec3 m_worldup;
float m_yaw;
float m_pitch;
float m_movement_speed;
float m_mouse_sensitivity;
float m_zoom;
class Camera {
public:
glm::vec3 m_position;
glm::vec3 m_front;
glm::vec3 m_up;
glm::vec3 m_right;
glm::vec3 m_worldup;
float m_yaw;
float m_pitch;
float m_movement_speed;
float m_mouse_sensitivity;
float m_zoom;
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH);
Camera(float pos_x, float pos_y, float pos_z, float up_x, float up_y, float up_z, float yaw, float pitch);
glm::mat4 get_view_matrix() const {
return glm::lookAt(m_position, m_position + m_front, m_up);
}
Camera(glm::vec3 m_position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : m_front(glm::vec3(0.0f, 0.0f, -1.0f)), m_movement_speed(SPEED), m_mouse_sensitivity(SENSITIVITY), m_zoom(ZOOM) {
m_position = m_position;
m_worldup = up;
m_yaw = yaw;
m_pitch = pitch;
updateCameraVectors();
}
void update();
void process_movement(camera_movement direction, float delta_time);
void process_mouse_movement(float x_offset, float y_offset, GLboolean constrain_pitch = true);
void process_mouse_scroll(float y_offset);
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : m_front(glm::vec3(0.0f, 0.0f, -1.0f)), m_movement_speed(SPEED), m_mouse_sensitivity(SENSITIVITY), m_zoom(ZOOM) {
m_position = glm::vec3(posX, posY, posZ);
m_worldup = glm::vec3(upX, upY, upZ);
m_yaw = yaw;
m_pitch = pitch;
updateCameraVectors();
}
static void mouse_callback(GLFWwindow* window, double x_pos_in, double y_pos_in);
glm::mat4 GetViewMatrix() {
return glm::lookAt(m_position, m_position + m_front, m_up);
}
void Update(){
if (firstMouse)
{
lastX = mx;
lastY = my;
firstMouse = false;
}
float xoffset = mx - lastX;
float yoffset = lastY - my;
lastX = mx;
lastY = my;
ProcessMouseMovement(xoffset, yoffset);
}
void ProcessMovement(camera_movement direction, float deltaTime) {
float velocity = m_movement_speed * deltaTime;
if (direction == FORWARD)
m_position += m_front * velocity;
if (direction == BACKWARD)
m_position -= m_front * velocity;
if (direction == LEFT)
m_position -= m_right * velocity;
if (direction == RIGHT)
m_position += m_right * velocity;
if (direction == UP)
m_position += glm::vec3(0.0F, 1.0F, 0.0F) * velocity;
if (direction == DOWN)
m_position += glm::vec3(0.0F, -1.0F, 0.0F) * velocity;
}
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true) {
xoffset *= m_mouse_sensitivity;
yoffset *= m_mouse_sensitivity;
m_yaw += xoffset;
m_pitch += yoffset;
if (constrainPitch)
{
if (m_pitch > 89.0f)
m_pitch = 89.0f;
if (m_pitch < -89.0f)
m_pitch = -89.0f;
}
updateCameraVectors();
}
void ProcessMouseScroll(float yoffset) {
m_zoom -= (float)yoffset;
if (m_zoom < 1.0f)
m_zoom = 1.0f;
if (m_zoom > 45.0f)
m_zoom = 45.0f;
}
static void mouse_callback(GLFWwindow* window, double xposIn, double yposIn) {
mx = static_cast<float>(xposIn);
my = static_cast<float>(yposIn);
}
private:
void updateCameraVectors() {
glm::vec3 front;
front.x = cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
front.y = sin(glm::radians(m_pitch));
front.z = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
m_front = glm::normalize(front);
m_right = glm::normalize(glm::cross(m_front, m_worldup));
m_up = glm::normalize(glm::cross(m_right, m_front));
}
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
};
private:
void update_camera_vectors();
float m_last_x;
float m_last_y;
bool m_first_mouse;
};
}
#endif // POLYGUN_ENGINE_CAMERA_H
#endif // POLYGUN_ENGINE_CAMERA_H

View File

@ -1,102 +1,130 @@
#include "shader.hpp"
#include "game/renderer/shader.hpp"
namespace polygun::renderer {
Shader::Shader(const char* vertexPath, const char* fragmentPath) {
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure& e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
unsigned int vertex, fragment;
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
checkCompileErrors(program, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(fragment);
Shader::Shader() :
m_program(0)
{}
}
Shader::Shader(const GLuint id) :
m_program(id)
{}
void Shader::bind(){
glUseProgram(program);
}
Shader::Shader(const char* vertex_path, const char* fragment_path) {
std::string vertex_code;
std::string fragment_code;
std::ifstream v_shader_file;
std::ifstream f_shader_file;
v_shader_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
f_shader_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
v_shader_file.open(vertex_path);
f_shader_file.open(fragment_path);
std::stringstream v_shader_stream, f_shader_stream;
v_shader_stream << v_shader_file.rdbuf();
f_shader_stream << f_shader_file.rdbuf();
v_shader_file.close();
f_shader_file.close();
vertex_code = v_shader_stream.str();
fragment_code = f_shader_stream.str();
}
catch (std::ifstream::failure& e) {
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
}
const char* v_shader_code = vertex_code.c_str();
const char * f_shader_code = fragment_code.c_str();
unsigned int vertex, fragment;
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &v_shader_code, NULL);
glCompileShader(vertex);
check_compile_errors(vertex, "VERTEX");
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &f_shader_code, NULL);
glCompileShader(fragment);
check_compile_errors(fragment, "FRAGMENT");
m_program = glCreateProgram();
glAttachShader(m_program, vertex);
glAttachShader(m_program, fragment);
glLinkProgram(m_program);
check_compile_errors(m_program, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(fragment);
void Shader::unbind(){
glUseProgram(0);
}
}
void Shader::setuniform(const GLchar* uName, unsigned int value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
Shader::~Shader() {
glDeleteProgram(m_program);
}
void Shader::bind() {
glUseProgram(m_program);
}
void Shader::setuniform(const GLchar* uName, int value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
void Shader::unbind() {
glUseProgram(0);
}
void Shader::setuniform(const GLchar* uName, GLfloat value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
void Shader::set_uniform(const GLchar* u_name, unsigned int value) {
glUniform1i(glGetUniformLocation(m_program, u_name), value);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y){
glUniform2f(glGetUniformLocation(program, uName), x, y);
}
void Shader::set_uniform(const GLchar* u_name, int value) {
glUniform1i(glGetUniformLocation(m_program, u_name), value);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z){
glUniform3f(glGetUniformLocation(program, uName), x, y, z);
}
void Shader::set_uniform(const GLchar* u_name, GLfloat value) {
glUniform1i(glGetUniformLocation(m_program, u_name), value);
}
void Shader::setuniform(const GLchar* uName, glm::vec3 vector){
glUniform3f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z);
}
void Shader::set_uniform(const GLchar* u_name, GLfloat x, GLfloat y) {
glUniform2f(glGetUniformLocation(m_program, u_name), x, y);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w){
glUniform4f(glGetUniformLocation(program, uName), x, y, z, w);
}
void Shader::set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z) {
glUniform3f(glGetUniformLocation(m_program, u_name), x, y, z);
}
void Shader::setuniform(const GLchar* name, const glm::mat4 &mat)
{
glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, &mat[0][0]);
}
void Shader::set_uniform(const GLchar* u_name, const glm::vec3& vector) {
glUniform3f(glGetUniformLocation(m_program, u_name), vector.x, vector.y, vector.z);
}
void Shader::setuniform(const GLchar* uName, glm::vec2 vector){
glUniform2f(glGetUniformLocation(program, uName), vector.x, vector.y);
}
void Shader::set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
glUniform4f(glGetUniformLocation(m_program, u_name), x, y, z, w);
}
void Shader::setuniform(const GLchar* uName, glm::vec4 vector){
glUniform4f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z, vector.w);
}
void Shader::setuniform(const GLchar* uName, GLuint tex2d, GLint unit){ // sample 2d
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, tex2d);
glUniform1i(glGetUniformLocation(program, uName), unit);
}
}
void Shader::set_uniform(const GLchar* name, const glm::mat4 &mat) {
glUniformMatrix4fv(glGetUniformLocation(m_program, name), 1, GL_FALSE, &mat[0][0]);
}
void Shader::set_uniform(const GLchar* u_name, const glm::vec2& vector) {
glUniform2f(glGetUniformLocation(m_program, u_name), vector.x, vector.y);
}
void Shader::set_uniform(const GLchar* u_name, const glm::vec4& vector){
glUniform4f(glGetUniformLocation(m_program, u_name), vector.x, vector.y, vector.z, vector.w);
}
void Shader::set_uniform(const GLchar* u_name, GLuint tex2d, GLint unit){ // sample 2d
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, tex2d);
glUniform1i(glGetUniformLocation(m_program, u_name), unit);
}
void Shader::check_compile_errors(GLuint shader, const std::string& type) {
GLint success;
GLchar info_log[1024];
if(type != "PROGRAM") {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(shader, 1024, NULL, info_log);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << info_log << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
else {
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(shader, 1024, NULL, info_log);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << info_log << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
}

View File

@ -1,61 +1,38 @@
#ifndef POLYGUN_RENDERER_SHADER_H
#define POLYGUN_RENDERER_SHADER_H
#include "../core.hpp"
#include "game/core.hpp"
namespace polygun::renderer {
class Shader {
public:
Shader() : program(0) {}
Shader(const GLuint id) : program(id) {}
Shader(const char* vertexPath, const char* fragmentPath);
~Shader() { glDeleteProgram(program); }
void bind();
void unbind();
void setuniform(const GLchar* uName, unsigned int value);
void setuniform(const GLchar* uName, int value);
void setuniform(const GLchar* uName, GLfloat value);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y);
void setuniform(const GLchar* uName, glm::vec2 vector);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z);
void setuniform(const GLchar* uName, glm::vec3 vector);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void setuniform(const GLchar* uName, glm::vec4 vector);
void setuniform(const GLchar* uName, const glm::mat4& mtx);
void setuniform(const GLchar* uName, GLuint tex2d, GLint unit); // sample 2d
GLuint getuniform(const char* name);
GLuint getprogram() { return program; }
class Shader {
public:
Shader();
Shader(const GLuint id);
Shader(const char* vertex_path, const char* fragment_path);
~Shader();
void bind();
void unbind();
void set_uniform(const GLchar* u_name, unsigned int value);
void set_uniform(const GLchar* u_name, int value);
void set_uniform(const GLchar* u_name, GLfloat value);
void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y);
void set_uniform(const GLchar* u_name, const glm::vec2& vector);
void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z);
void set_uniform(const GLchar* u_name, const glm::vec3& vector);
void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void set_uniform(const GLchar* u_name, const glm::vec4& vector);
void set_uniform(const GLchar* u_name, const glm::mat4& mtx);
void set_uniform(const GLchar* u_name, GLuint tex2d, GLint unit); // sample 2d
GLuint get_uniform(const char* name) const;
GLuint get_program() const { return m_program; }
private:
void checkCompileErrors(GLuint shader, std::string type)
{
GLint success;
GLchar infoLog[1024];
if(type != "PROGRAM")
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
else
{
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
GLuint program;
};
private:
void check_compile_errors(GLuint shader, const std::string& type);
GLuint m_program;
};
}
#endif
#endif

View File

@ -2,7 +2,7 @@
#include "game/engine/engine.hpp"
int main() {
polygun::engine::Engine engine;
engine.init();
return 0;
polygun::engine::Engine engine;
engine.init();
return 0;
}