Compare commits

...

1 Commits

Author SHA1 Message Date
23b5188079 Fix code style project-wide 2023-03-30 19:26:18 +02:00
9 changed files with 499 additions and 462 deletions

View File

@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED true) set(CMAKE_CXX_STANDARD_REQUIRED true)
include_directories(deps) include_directories(deps)
include_directories(src/common) include_directories("${CMAKE_SOURCE_DIR}/src")
file(GLOB_RECURSE Sources "src/**.cpp" "src/**.h") file(GLOB_RECURSE Sources "src/**.cpp" "src/**.h")

View File

@@ -12,15 +12,14 @@ namespace polygun::engine {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Blocking3D", NULL, NULL); GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Blocking3D", NULL, NULL);
if (window == NULL) if (window == NULL) {
{
std::cout << "Failed to create GLFW window" << std::endl; std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate(); glfwTerminate();
return; return;
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, camera.mouse_callback); glfwSetCursorPosCallback(window, m_camera.mouse_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glewInit() != GLEW_OK) { if (glewInit() != GLEW_OK) {
@@ -76,10 +75,10 @@ namespace polygun::engine {
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f -0.1f, 0.1f, -0.1f, 0.0f, 1.0f
}; };
//32x32x32 chunk | ONLY FOR TEST THIS IS'NT EVEN A CHUNK //32x32x32 chunk | ONLY FOR TEST THIS IS'NT EVEN A CHUNK
glm::vec3 cubePositions[1024]; glm::vec3 cube_positions[1024];
for(int i = 0; i < 1024; i++){ for(int i = 0; i < 1024; i++){
cubePositions[i] = glm::vec3(0.2f * (i % 32), 0.2f * (i / 32), 0.2f * (i / 32)); cube_positions[i] = glm::vec3(0.2f * (i % 32), 0.2f * (i / 32), 0.2f * (i / 32));
} }
unsigned int VBO, VAO; unsigned int VBO, VAO;
@@ -123,14 +122,16 @@ namespace polygun::engine {
chunk_shader.bind(); chunk_shader.bind();
chunk_shader.setuniform("texture1", 0); chunk_shader.set_uniform("texture1", 0);
m_delta_time = 0;
m_last_frame = 0;
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
float currentFrame = static_cast<float>(glfwGetTime()); float current_frame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame; m_delta_time = current_frame - m_last_frame;
lastFrame = currentFrame; m_last_frame = current_frame;
// input // input
// ----- // -----
@@ -139,20 +140,20 @@ namespace polygun::engine {
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::FORWARD, deltaTime); m_camera.process_movement(polygun::engine::FORWARD, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::BACKWARD, deltaTime); m_camera.process_movement(polygun::engine::BACKWARD, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessMovement(LEFT, deltaTime); m_camera.process_movement(LEFT, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::RIGHT, deltaTime); m_camera.process_movement(polygun::engine::RIGHT, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::UP, deltaTime); m_camera.process_movement(polygun::engine::UP, m_delta_time);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
camera.ProcessMovement(polygun::engine::DOWN, deltaTime); m_camera.process_movement(polygun::engine::DOWN, m_delta_time);
camera.Update(); m_camera.update();
// render // render
// ------ // ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
@@ -165,19 +166,19 @@ namespace polygun::engine {
// activate shader // activate shader
chunk_shader.bind(); chunk_shader.bind();
glm::mat4 projection = glm::perspective(glm::radians(camera.m_zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 projection = glm::perspective(glm::radians(m_camera.m_zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
chunk_shader.setuniform("projection", projection); chunk_shader.set_uniform("projection", projection);
glm::mat4 view = camera.GetViewMatrix(); glm::mat4 view = m_camera.get_view_matrix();
chunk_shader.setuniform("view", view); chunk_shader.set_uniform("view", view);
glBindVertexArray(VAO); glBindVertexArray(VAO);
for (unsigned int i = 0; i < (sizeof(cubePositions)/sizeof(*cubePositions)) ; i++) for (unsigned int i = 0; i < (sizeof(cube_positions)/sizeof(*cube_positions)) ; i++)
{ {
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]); model = glm::translate(model, cube_positions[i]);
float angle = 20.0f * i; float angle = 20.0f * i;
chunk_shader.setuniform("model", model); chunk_shader.set_uniform("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
} }

View File

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

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,7 +1,7 @@
#ifndef POLYGUN_ENGINE_CAMERA_H #ifndef POLYGUN_ENGINE_CAMERA_H
#define POLYGUN_ENGINE_CAMERA_H #define POLYGUN_ENGINE_CAMERA_H
#include "../core.hpp" #include "game/core.hpp"
namespace polygun::engine { namespace polygun::engine {
enum camera_movement { enum camera_movement {
@@ -19,8 +19,6 @@ namespace polygun::engine{
const float SENSITIVITY = 0.1f; const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f; const float ZOOM = 45.0f;
static float mx,my;
class Camera { class Camera {
public: public:
glm::vec3 m_position; glm::vec3 m_position;
@@ -34,107 +32,26 @@ namespace polygun::engine{
float m_mouse_sensitivity; float m_mouse_sensitivity;
float m_zoom; 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);
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) { glm::mat4 get_view_matrix() const {
m_position = m_position;
m_worldup = up;
m_yaw = yaw;
m_pitch = pitch;
updateCameraVectors();
}
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();
}
glm::mat4 GetViewMatrix() {
return glm::lookAt(m_position, m_position + m_front, m_up); return glm::lookAt(m_position, m_position + m_front, m_up);
} }
void Update(){ void update();
if (firstMouse) void process_movement(camera_movement direction, float delta_time);
{ void process_mouse_movement(float x_offset, float y_offset, GLboolean constrain_pitch = true);
lastX = mx; void process_mouse_scroll(float y_offset);
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);
}
static void mouse_callback(GLFWwindow* window, double x_pos_in, double y_pos_in);
private: private:
void updateCameraVectors() { void 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));
}
float lastX = SCR_WIDTH / 2.0f; float m_last_x;
float lastY = SCR_HEIGHT / 2.0f; float m_last_y;
bool firstMouse = true; bool m_first_mouse;
}; };
} }

View File

@@ -1,102 +1,130 @@
#include "shader.hpp" #include "game/renderer/shader.hpp"
namespace polygun::renderer { namespace polygun::renderer {
Shader::Shader(const char* vertexPath, const char* fragmentPath) { Shader::Shader() :
std::string vertexCode; m_program(0)
std::string fragmentCode; {}
std::ifstream vShaderFile;
std::ifstream fShaderFile; Shader::Shader(const GLuint id) :
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); m_program(id)
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit); {}
try
{ Shader::Shader(const char* vertex_path, const char* fragment_path) {
vShaderFile.open(vertexPath); std::string vertex_code;
fShaderFile.open(fragmentPath); std::string fragment_code;
std::stringstream vShaderStream, fShaderStream; std::ifstream v_shader_file;
vShaderStream << vShaderFile.rdbuf(); std::ifstream f_shader_file;
fShaderStream << fShaderFile.rdbuf(); v_shader_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
vShaderFile.close(); f_shader_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.close(); try {
vertexCode = vShaderStream.str(); v_shader_file.open(vertex_path);
fragmentCode = fShaderStream.str(); 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) catch (std::ifstream::failure& e) {
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl; std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
} }
const char* vShaderCode = vertexCode.c_str(); const char* v_shader_code = vertex_code.c_str();
const char * fShaderCode = fragmentCode.c_str(); const char * f_shader_code = fragment_code.c_str();
unsigned int vertex, fragment; unsigned int vertex, fragment;
vertex = glCreateShader(GL_VERTEX_SHADER); vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL); glShaderSource(vertex, 1, &v_shader_code, NULL);
glCompileShader(vertex); glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX"); check_compile_errors(vertex, "VERTEX");
fragment = glCreateShader(GL_FRAGMENT_SHADER); fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL); glShaderSource(fragment, 1, &f_shader_code, NULL);
glCompileShader(fragment); glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT"); check_compile_errors(fragment, "FRAGMENT");
program = glCreateProgram(); m_program = glCreateProgram();
glAttachShader(program, vertex); glAttachShader(m_program, vertex);
glAttachShader(program, fragment); glAttachShader(m_program, fragment);
glLinkProgram(program); glLinkProgram(m_program);
checkCompileErrors(program, "PROGRAM"); check_compile_errors(m_program, "PROGRAM");
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);
} }
Shader::~Shader() {
glDeleteProgram(m_program);
}
void Shader::bind() { void Shader::bind() {
glUseProgram(program); glUseProgram(m_program);
} }
void Shader::unbind() { void Shader::unbind() {
glUseProgram(0); glUseProgram(0);
} }
void Shader::setuniform(const GLchar* uName, unsigned int value){ void Shader::set_uniform(const GLchar* u_name, unsigned int value) {
glUniform1i(glGetUniformLocation(program, uName), value); glUniform1i(glGetUniformLocation(m_program, u_name), value);
} }
void Shader::set_uniform(const GLchar* u_name, int value) {
void Shader::setuniform(const GLchar* uName, int value){ glUniform1i(glGetUniformLocation(m_program, u_name), value);
glUniform1i(glGetUniformLocation(program, uName), value);
} }
void Shader::setuniform(const GLchar* uName, GLfloat value){ void Shader::set_uniform(const GLchar* u_name, GLfloat value) {
glUniform1i(glGetUniformLocation(program, uName), value); glUniform1i(glGetUniformLocation(m_program, u_name), value);
} }
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y){ void Shader::set_uniform(const GLchar* u_name, GLfloat x, GLfloat y) {
glUniform2f(glGetUniformLocation(program, uName), x, y); glUniform2f(glGetUniformLocation(m_program, u_name), x, y);
} }
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z){ void Shader::set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z) {
glUniform3f(glGetUniformLocation(program, uName), x, y, z); glUniform3f(glGetUniformLocation(m_program, u_name), x, y, z);
} }
void Shader::setuniform(const GLchar* uName, glm::vec3 vector){ void Shader::set_uniform(const GLchar* u_name, const glm::vec3& vector) {
glUniform3f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z); glUniform3f(glGetUniformLocation(m_program, u_name), vector.x, vector.y, vector.z);
} }
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w){ void Shader::set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
glUniform4f(glGetUniformLocation(program, uName), x, y, z, w); glUniform4f(glGetUniformLocation(m_program, u_name), x, y, z, w);
} }
void Shader::setuniform(const GLchar* name, const glm::mat4 &mat) void Shader::set_uniform(const GLchar* name, const glm::mat4 &mat) {
{ glUniformMatrix4fv(glGetUniformLocation(m_program, name), 1, GL_FALSE, &mat[0][0]);
glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, &mat[0][0]);
} }
void Shader::setuniform(const GLchar* uName, glm::vec2 vector){ void Shader::set_uniform(const GLchar* u_name, const glm::vec2& vector) {
glUniform2f(glGetUniformLocation(program, uName), vector.x, vector.y); glUniform2f(glGetUniformLocation(m_program, u_name), vector.x, vector.y);
} }
void Shader::setuniform(const GLchar* uName, glm::vec4 vector){ void Shader::set_uniform(const GLchar* u_name, const glm::vec4& vector){
glUniform4f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z, vector.w); glUniform4f(glGetUniformLocation(m_program, u_name), vector.x, vector.y, vector.z, vector.w);
} }
void Shader::setuniform(const GLchar* uName, GLuint tex2d, GLint unit){ // sample 2d
void Shader::set_uniform(const GLchar* u_name, GLuint tex2d, GLint unit){ // sample 2d
glActiveTexture(GL_TEXTURE0 + unit); glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, tex2d); glBindTexture(GL_TEXTURE_2D, tex2d);
glUniform1i(glGetUniformLocation(program, uName), unit); 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,60 +1,37 @@
#ifndef POLYGUN_RENDERER_SHADER_H #ifndef POLYGUN_RENDERER_SHADER_H
#define POLYGUN_RENDERER_SHADER_H #define POLYGUN_RENDERER_SHADER_H
#include "../core.hpp" #include "game/core.hpp"
namespace polygun::renderer { namespace polygun::renderer {
class Shader { class Shader {
public: public:
Shader() : program(0) {} Shader();
Shader(const GLuint id) : program(id) {} Shader(const GLuint id);
Shader(const char* vertexPath, const char* fragmentPath); Shader(const char* vertex_path, const char* fragment_path);
~Shader() { glDeleteProgram(program); } ~Shader();
void bind(); void bind();
void unbind(); void unbind();
void setuniform(const GLchar* uName, unsigned int value); void set_uniform(const GLchar* u_name, unsigned int value);
void setuniform(const GLchar* uName, int value); void set_uniform(const GLchar* u_name, int value);
void setuniform(const GLchar* uName, GLfloat value); void set_uniform(const GLchar* u_name, GLfloat value);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y); void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y);
void setuniform(const GLchar* uName, glm::vec2 vector); void set_uniform(const GLchar* u_name, const glm::vec2& vector);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z); void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z);
void setuniform(const GLchar* uName, glm::vec3 vector); void set_uniform(const GLchar* u_name, const glm::vec3& vector);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w); void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void setuniform(const GLchar* uName, glm::vec4 vector); void set_uniform(const GLchar* u_name, const glm::vec4& vector);
void setuniform(const GLchar* uName, const glm::mat4& mtx); void set_uniform(const GLchar* u_name, const glm::mat4& mtx);
void setuniform(const GLchar* uName, GLuint tex2d, GLint unit); // sample 2d void set_uniform(const GLchar* u_name, GLuint tex2d, GLint unit); // sample 2d
GLuint getuniform(const char* name); GLuint get_uniform(const char* name) const;
GLuint getprogram() { return program; } GLuint get_program() const { return m_program; }
private: private:
void check_compile_errors(GLuint shader, const std::string& type);
void checkCompileErrors(GLuint shader, std::string type) GLuint m_program;
{
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;
}; };
} }