mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
chore: add clang-format and pre-commit configuration
This commit is contained in:
@@ -1,30 +1,23 @@
|
||||
#include <Cubed/tools/cubed_random.hpp>
|
||||
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include "Cubed/tools/cubed_random.hpp"
|
||||
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
Random::Random() {
|
||||
|
||||
}
|
||||
Random::Random() {}
|
||||
|
||||
bool Random::random_bool(double probability) {
|
||||
std::bernoulli_distribution dist(probability);
|
||||
return dist(m_engine);
|
||||
}
|
||||
|
||||
std::mt19937& Random::engine() {
|
||||
return m_engine;
|
||||
}
|
||||
std::mt19937& Random::engine() { return m_engine; }
|
||||
|
||||
unsigned Random::seed() {
|
||||
return m_seed;
|
||||
}
|
||||
unsigned Random::seed() { return m_seed; }
|
||||
|
||||
void Random::init(unsigned seed) {
|
||||
m_seed = seed;
|
||||
m_engine.seed(seed);
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace Cubed
|
||||
@@ -1,14 +1,14 @@
|
||||
#include <Cubed/tools/font.hpp>
|
||||
#include <Cubed/constants.hpp>
|
||||
#include <Cubed/shader.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
#include "Cubed/tools/font.hpp"
|
||||
|
||||
#include "Cubed/constants.hpp"
|
||||
#include "Cubed/shader.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
#include "Cubed/tools/shader_tools.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
Font::Font() {
|
||||
|
||||
if (FT_Init_FreeType(&m_ft)) {
|
||||
@@ -18,12 +18,12 @@ Font::Font() {
|
||||
Logger::error("FREETYPE: Failed to load font");
|
||||
}
|
||||
|
||||
FT_Set_Pixel_Sizes(m_face, 0, 48);
|
||||
FT_Set_Pixel_Sizes(m_face, 0, 48);
|
||||
setup_font_character();
|
||||
}
|
||||
|
||||
Font::~Font() {
|
||||
|
||||
|
||||
FT_Done_Face(m_face);
|
||||
FT_Done_FreeType(m_ft);
|
||||
glDeleteTextures(1, &m_text_texture);
|
||||
@@ -31,54 +31,35 @@ Font::~Font() {
|
||||
|
||||
void Font::load_character(char8_t c) {
|
||||
if (FT_Load_Char(m_face, c, FT_LOAD_RENDER)) {
|
||||
Logger::error("FREETYTPE: Failed to load Glyph");
|
||||
return;
|
||||
}
|
||||
const auto& width = m_face->glyph->bitmap.width;
|
||||
const auto& height = m_face->glyph->bitmap.rows;
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_text_texture);
|
||||
glTexSubImage3D(
|
||||
GL_TEXTURE_2D_ARRAY,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
static_cast<int>(c),
|
||||
width,
|
||||
height,
|
||||
1,
|
||||
GL_RED,
|
||||
GL_UNSIGNED_BYTE,
|
||||
m_face->glyph->bitmap.buffer
|
||||
);
|
||||
Logger::error("FREETYTPE: Failed to load Glyph");
|
||||
return;
|
||||
}
|
||||
const auto& width = m_face->glyph->bitmap.width;
|
||||
const auto& height = m_face->glyph->bitmap.rows;
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_text_texture);
|
||||
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, static_cast<int>(c), width,
|
||||
height, 1, GL_RED, GL_UNSIGNED_BYTE,
|
||||
m_face->glyph->bitmap.buffer);
|
||||
|
||||
Character character = {
|
||||
glm::vec2{0.0f, 0.0f},
|
||||
glm::vec2{static_cast<float>(width) / m_texture_width, static_cast<float>(height) / m_texture_height},
|
||||
glm::ivec2(m_face->glyph->bitmap.width, m_face->glyph->bitmap.rows),
|
||||
glm::ivec2(m_face->glyph->bitmap_left, m_face->glyph->bitmap_top),
|
||||
static_cast<GLuint>(m_face->glyph->advance.x)
|
||||
};
|
||||
Character character = {
|
||||
glm::vec2{0.0f, 0.0f},
|
||||
glm::vec2{static_cast<float>(width) / m_texture_width,
|
||||
static_cast<float>(height) / m_texture_height},
|
||||
glm::ivec2(m_face->glyph->bitmap.width, m_face->glyph->bitmap.rows),
|
||||
glm::ivec2(m_face->glyph->bitmap_left, m_face->glyph->bitmap_top),
|
||||
static_cast<GLuint>(m_face->glyph->advance.x)};
|
||||
|
||||
m_characters.insert({c, std::move(character)});
|
||||
m_characters.insert({c, std::move(character)});
|
||||
}
|
||||
|
||||
void Font::setup_font_character() {
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
|
||||
glGenTextures(1, &m_text_texture);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_text_texture);
|
||||
glTexImage3D(
|
||||
GL_TEXTURE_2D_ARRAY,
|
||||
0,
|
||||
GL_RED,
|
||||
m_texture_width,
|
||||
m_texture_height,
|
||||
MAX_CHARACTER,
|
||||
0,
|
||||
GL_RED,
|
||||
GL_UNSIGNED_BYTE,
|
||||
nullptr
|
||||
);
|
||||
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RED, m_texture_width,
|
||||
m_texture_height, MAX_CHARACTER, 0, GL_RED, GL_UNSIGNED_BYTE,
|
||||
nullptr);
|
||||
|
||||
for (char8_t c = 0; c < 128; c++) {
|
||||
load_character(c);
|
||||
@@ -88,12 +69,13 @@ void Font::setup_font_character() {
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
|
||||
}
|
||||
|
||||
std::vector<Vertex2D> Font::vertices(const std::string &text, float x, float y, float scale) {
|
||||
std::vector<Vertex2D> Font::vertices(const std::string& text, float x, float y,
|
||||
float scale) {
|
||||
static Font font;
|
||||
|
||||
|
||||
std::vector<Vertex2D> vertices;
|
||||
|
||||
for (char8_t c : text) {
|
||||
@@ -109,26 +91,27 @@ std::vector<Vertex2D> Font::vertices(const std::string &text, float x, float y,
|
||||
float w = ch.size.x * scale;
|
||||
float h = ch.size.y * scale;
|
||||
|
||||
vertices.emplace_back(xpos, ypos + h, ch.uv_min.x, ch.uv_max.y, static_cast<float>(c));
|
||||
vertices.emplace_back(xpos, ypos, ch.uv_min.x, ch.uv_min.y, static_cast<float>(c));
|
||||
vertices.emplace_back(xpos + w, ypos, ch.uv_max.x, ch.uv_min.y, static_cast<float>(c));
|
||||
vertices.emplace_back(xpos, ypos + h, ch.uv_min.x, ch.uv_max.y, static_cast<float>(c));
|
||||
vertices.emplace_back(xpos + w, ypos, ch.uv_max.x, ch.uv_min.y, static_cast<float>(c));
|
||||
vertices.emplace_back(xpos + w, ypos + h, ch.uv_max.x, ch.uv_max.y, static_cast<float>(c));
|
||||
|
||||
vertices.emplace_back(xpos, ypos + h, ch.uv_min.x, ch.uv_max.y,
|
||||
static_cast<float>(c));
|
||||
vertices.emplace_back(xpos, ypos, ch.uv_min.x, ch.uv_min.y,
|
||||
static_cast<float>(c));
|
||||
vertices.emplace_back(xpos + w, ypos, ch.uv_max.x, ch.uv_min.y,
|
||||
static_cast<float>(c));
|
||||
vertices.emplace_back(xpos, ypos + h, ch.uv_min.x, ch.uv_max.y,
|
||||
static_cast<float>(c));
|
||||
vertices.emplace_back(xpos + w, ypos, ch.uv_max.x, ch.uv_min.y,
|
||||
static_cast<float>(c));
|
||||
vertices.emplace_back(xpos + w, ypos + h, ch.uv_max.x, ch.uv_max.y,
|
||||
static_cast<float>(c));
|
||||
|
||||
x += (ch.advance >> 6) * scale;
|
||||
|
||||
}
|
||||
|
||||
return vertices;
|
||||
}
|
||||
|
||||
GLuint Font::text_texture() {
|
||||
return m_text_texture;
|
||||
}
|
||||
GLuint Font::text_texture() { return m_text_texture; }
|
||||
|
||||
const std::string& Font::font_path() {
|
||||
return m_font_path;
|
||||
}
|
||||
const std::string& Font::font_path() { return m_font_path; }
|
||||
|
||||
}
|
||||
} // namespace Cubed
|
||||
@@ -1,38 +1,42 @@
|
||||
#include <Cubed/tools/math_tools.hpp>
|
||||
#include "Cubed/tools/math_tools.hpp"
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
namespace Math {
|
||||
void extract_frustum_planes(const glm::mat4& mvp_matrix, std::vector<glm::vec4>& planes) {
|
||||
if (planes.size() != 6) {
|
||||
planes.resize(6);
|
||||
}
|
||||
|
||||
const float* m = glm::value_ptr(mvp_matrix);
|
||||
|
||||
// left plane
|
||||
planes[0] = glm::vec4(m[3] + m[0], m[7] + m[4], m[11] + m[8], m[15] + m[12]);
|
||||
// right plane
|
||||
planes[1] = glm::vec4(m[3] - m[0], m[7] - m[4], m[11] - m[8], m[15] - m[12]);
|
||||
// bottom plane
|
||||
planes[2] = glm::vec4(m[3] + m[1], m[7] + m[5], m[11] + m[9], m[15] + m[13]);
|
||||
// top plane
|
||||
planes[3] = glm::vec4(m[3] - m[1], m[7] - m[5], m[11] - m[9], m[15] - m[13]);
|
||||
// near plane
|
||||
planes[4] = glm::vec4(m[3] + m[2], m[7] + m[6], m[11] + m[10], m[15] + m[14]);
|
||||
// far plane
|
||||
planes[5] = glm::vec4(m[3] - m[2], m[7] - m[6], m[11] - m[10], m[15] - m[14]);
|
||||
|
||||
for (auto& p : planes) {
|
||||
p = glm::normalize(p);
|
||||
}
|
||||
void extract_frustum_planes(const glm::mat4& mvp_matrix,
|
||||
std::vector<glm::vec4>& planes) {
|
||||
if (planes.size() != 6) {
|
||||
planes.resize(6);
|
||||
}
|
||||
|
||||
|
||||
const float* m = glm::value_ptr(mvp_matrix);
|
||||
|
||||
// left plane
|
||||
planes[0] =
|
||||
glm::vec4(m[3] + m[0], m[7] + m[4], m[11] + m[8], m[15] + m[12]);
|
||||
// right plane
|
||||
planes[1] =
|
||||
glm::vec4(m[3] - m[0], m[7] - m[4], m[11] - m[8], m[15] - m[12]);
|
||||
// bottom plane
|
||||
planes[2] =
|
||||
glm::vec4(m[3] + m[1], m[7] + m[5], m[11] + m[9], m[15] + m[13]);
|
||||
// top plane
|
||||
planes[3] =
|
||||
glm::vec4(m[3] - m[1], m[7] - m[5], m[11] - m[9], m[15] - m[13]);
|
||||
// near plane
|
||||
planes[4] =
|
||||
glm::vec4(m[3] + m[2], m[7] + m[6], m[11] + m[10], m[15] + m[14]);
|
||||
// far plane
|
||||
planes[5] =
|
||||
glm::vec4(m[3] - m[2], m[7] - m[6], m[11] - m[10], m[15] - m[14]);
|
||||
|
||||
for (auto& p : planes) {
|
||||
p = glm::normalize(p);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Math
|
||||
|
||||
}
|
||||
} // namespace Cubed
|
||||
@@ -1,15 +1,15 @@
|
||||
#include <Cubed/tools/perlin_noise.hpp>
|
||||
#include "Cubed/tools/perlin_noise.hpp"
|
||||
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/cubed_random.hpp"
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/cubed_random.hpp>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
void PerlinNoise::init(unsigned seed) {
|
||||
p.resize(256);
|
||||
std::iota(p.begin(), p.end(), 0);
|
||||
@@ -41,27 +41,23 @@ float PerlinNoise::noise(float x, float y, float z) {
|
||||
int ba = p[b] + iz;
|
||||
int bb = p[b + 1] + iz;
|
||||
|
||||
float res = lerp (w,
|
||||
lerp (v,
|
||||
lerp(u, grad(p[aa], x, y, z), grad(p[ba], x - 1, y, z)),
|
||||
lerp(u, grad(p[ab], x, y - 1, z), grad(p[bb], x - 1, y - 1, z))
|
||||
),
|
||||
float res = lerp(
|
||||
w,
|
||||
lerp(v, lerp(u, grad(p[aa], x, y, z), grad(p[ba], x - 1, y, z)),
|
||||
lerp(u, grad(p[ab], x, y - 1, z), grad(p[bb], x - 1, y - 1, z))),
|
||||
lerp(v,
|
||||
lerp(u, grad(p[aa + 1], x, y, z - 1), grad(p[ba + 1], x - 1, y, z - 1)),
|
||||
lerp(u, grad(p[ab + 1], x, y - 1, z - 1), grad(p[bb + 1 ], x - 1, y - 1, z - 1))
|
||||
)
|
||||
|
||||
lerp(u, grad(p[aa + 1], x, y, z - 1),
|
||||
grad(p[ba + 1], x - 1, y, z - 1)),
|
||||
lerp(u, grad(p[ab + 1], x, y - 1, z - 1),
|
||||
grad(p[bb + 1], x - 1, y - 1, z - 1)))
|
||||
|
||||
);
|
||||
return (res + 1.0f) / 2.0f;
|
||||
}
|
||||
|
||||
float PerlinNoise::fade(float t) {
|
||||
return t * t * t * (t * (t * 6 - 15) + 10);
|
||||
}
|
||||
float PerlinNoise::fade(float t) { return t * t * t * (t * (t * 6 - 15) + 10); }
|
||||
|
||||
float PerlinNoise::lerp(float t, float a, float b) {
|
||||
return a + t * (b - a);
|
||||
}
|
||||
float PerlinNoise::lerp(float t, float a, float b) { return a + t * (b - a); }
|
||||
|
||||
float PerlinNoise::grad(int hash, float x, float y, float z) {
|
||||
int h = hash & 15;
|
||||
@@ -83,4 +79,4 @@ void PerlinNoise::reload(unsigned seed) {
|
||||
is_init = true;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace Cubed
|
||||
@@ -1,140 +1,138 @@
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include "Cubed/tools/shader_tools.hpp"
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace Tools {
|
||||
|
||||
GLuint create_shader_program(const std::string& v_shader_path, const std::string& f_shader_path) {
|
||||
GLuint create_shader_program(const std::string& v_shader_path,
|
||||
const std::string& f_shader_path) {
|
||||
|
||||
std::string v_shader_str = Tools::read_shader_source(ASSETS_PATH + v_shader_path);
|
||||
std::string f_shader_str = Tools::read_shader_source(ASSETS_PATH + f_shader_path);
|
||||
const char *v_shader_source = v_shader_str.c_str();
|
||||
const char *f_shader_source = f_shader_str.c_str();
|
||||
std::string v_shader_str =
|
||||
Tools::read_shader_source(ASSETS_PATH + v_shader_path);
|
||||
std::string f_shader_str =
|
||||
Tools::read_shader_source(ASSETS_PATH + f_shader_path);
|
||||
const char* v_shader_source = v_shader_str.c_str();
|
||||
const char* f_shader_source = f_shader_str.c_str();
|
||||
|
||||
GLuint v_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||
GLuint f_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
GLuint v_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||
GLuint f_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
GLint vc, fc;
|
||||
glShaderSource(v_shader, 1, &v_shader_source, NULL);
|
||||
glShaderSource(f_shader, 1, &f_shader_source, NULL);
|
||||
glCompileShader(v_shader);
|
||||
Tools::check_opengl_error();
|
||||
glGetShaderiv(v_shader, GL_COMPILE_STATUS, &vc);
|
||||
if (vc != 1) {
|
||||
Logger::error("vertex compilation failed");
|
||||
Tools::print_shader_log(v_shader);
|
||||
ASSERT(0);
|
||||
}
|
||||
glCompileShader(f_shader);
|
||||
Tools::check_opengl_error();
|
||||
glGetShaderiv(f_shader, GL_COMPILE_STATUS, &fc);
|
||||
if (fc != 1) {
|
||||
Logger::error("vertex compilation failed");
|
||||
Tools::print_shader_log(f_shader);
|
||||
ASSERT(0);
|
||||
}
|
||||
GLuint vf_program = glCreateProgram();
|
||||
glAttachShader(vf_program, v_shader);
|
||||
glAttachShader(vf_program, f_shader);
|
||||
glLinkProgram(vf_program);
|
||||
|
||||
GLint linked;
|
||||
Tools::check_opengl_error();
|
||||
glGetProgramiv(vf_program, GL_LINK_STATUS, &linked);
|
||||
if (linked != 1) {
|
||||
Logger::error("linking failed");
|
||||
Tools::print_program_info(vf_program);
|
||||
ASSERT(0);
|
||||
}
|
||||
glDeleteShader(v_shader);
|
||||
glDeleteShader(f_shader);
|
||||
return vf_program;
|
||||
GLint vc, fc;
|
||||
glShaderSource(v_shader, 1, &v_shader_source, NULL);
|
||||
glShaderSource(f_shader, 1, &f_shader_source, NULL);
|
||||
glCompileShader(v_shader);
|
||||
Tools::check_opengl_error();
|
||||
glGetShaderiv(v_shader, GL_COMPILE_STATUS, &vc);
|
||||
if (vc != 1) {
|
||||
Logger::error("vertex compilation failed");
|
||||
Tools::print_shader_log(v_shader);
|
||||
ASSERT(0);
|
||||
}
|
||||
|
||||
|
||||
void print_shader_log(GLuint shader) {
|
||||
int len = 0;
|
||||
int ch_written = 0;
|
||||
char *log;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
|
||||
if (len > 0) {
|
||||
log = (char*)malloc(len);
|
||||
glGetShaderInfoLog(shader, len, &ch_written, log);
|
||||
Logger::info("Shader Info Log: {}", log);
|
||||
free(log);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void print_program_info(int prog) {
|
||||
int len = 0;
|
||||
int ch_written = 0;
|
||||
char *log;
|
||||
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
|
||||
if (len > 0) {
|
||||
log = (char*)malloc(len);
|
||||
glGetProgramInfoLog(prog, len, &ch_written, log);
|
||||
Logger::info("Program Info Log: {}", log);
|
||||
free(log);
|
||||
}
|
||||
glCompileShader(f_shader);
|
||||
Tools::check_opengl_error();
|
||||
glGetShaderiv(f_shader, GL_COMPILE_STATUS, &fc);
|
||||
if (fc != 1) {
|
||||
Logger::error("vertex compilation failed");
|
||||
Tools::print_shader_log(f_shader);
|
||||
ASSERT(0);
|
||||
}
|
||||
GLuint vf_program = glCreateProgram();
|
||||
glAttachShader(vf_program, v_shader);
|
||||
glAttachShader(vf_program, f_shader);
|
||||
glLinkProgram(vf_program);
|
||||
|
||||
bool check_opengl_error() {
|
||||
bool found_error = false;
|
||||
int gl_err = glGetError();
|
||||
while (gl_err != GL_NO_ERROR) {
|
||||
Logger::error("glEorr: {}", gl_err);
|
||||
found_error = true;
|
||||
gl_err = glGetError();
|
||||
}
|
||||
|
||||
return found_error;
|
||||
GLint linked;
|
||||
Tools::check_opengl_error();
|
||||
glGetProgramiv(vf_program, GL_LINK_STATUS, &linked);
|
||||
if (linked != 1) {
|
||||
Logger::error("linking failed");
|
||||
Tools::print_program_info(vf_program);
|
||||
ASSERT(0);
|
||||
}
|
||||
|
||||
|
||||
std::string read_shader_source(const std::string& file_path) {
|
||||
std::string content;
|
||||
std::ifstream file_stream(file_path, std::ios::in);
|
||||
|
||||
if (!file_stream.is_open()) {
|
||||
Logger::error("{} not exist", file_path);
|
||||
}
|
||||
|
||||
std::string line = "";
|
||||
while (!file_stream.eof()) {
|
||||
|
||||
getline(file_stream, line);
|
||||
content.append(line + "\n");
|
||||
}
|
||||
file_stream.close();
|
||||
return content;
|
||||
}
|
||||
|
||||
void delete_image_data(unsigned char* data) {
|
||||
SOIL_free_image_data(data);
|
||||
}
|
||||
|
||||
unsigned char* load_image_data(const std::string& tex_image_path) {
|
||||
fs::path path = ASSETS_PATH + tex_image_path;
|
||||
ASSERT_MSG(fs::is_regular_file(path), path.c_str());
|
||||
unsigned char* data = nullptr;
|
||||
int width, height, channels;
|
||||
data = SOIL_load_image(path.string().c_str(), &width, &height, &channels, SOIL_LOAD_AUTO);
|
||||
ASSERT_MSG(data, "Could not load texture" + path.string());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
glDeleteShader(v_shader);
|
||||
glDeleteShader(f_shader);
|
||||
return vf_program;
|
||||
}
|
||||
|
||||
}
|
||||
void print_shader_log(GLuint shader) {
|
||||
int len = 0;
|
||||
int ch_written = 0;
|
||||
char* log;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
|
||||
if (len > 0) {
|
||||
log = (char*)malloc(len);
|
||||
glGetShaderInfoLog(shader, len, &ch_written, log);
|
||||
Logger::info("Shader Info Log: {}", log);
|
||||
free(log);
|
||||
}
|
||||
}
|
||||
|
||||
void print_program_info(int prog) {
|
||||
int len = 0;
|
||||
int ch_written = 0;
|
||||
char* log;
|
||||
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len);
|
||||
if (len > 0) {
|
||||
log = (char*)malloc(len);
|
||||
glGetProgramInfoLog(prog, len, &ch_written, log);
|
||||
Logger::info("Program Info Log: {}", log);
|
||||
free(log);
|
||||
}
|
||||
}
|
||||
|
||||
bool check_opengl_error() {
|
||||
bool found_error = false;
|
||||
int gl_err = glGetError();
|
||||
while (gl_err != GL_NO_ERROR) {
|
||||
Logger::error("glEorr: {}", gl_err);
|
||||
found_error = true;
|
||||
gl_err = glGetError();
|
||||
}
|
||||
|
||||
return found_error;
|
||||
}
|
||||
|
||||
std::string read_shader_source(const std::string& file_path) {
|
||||
std::string content;
|
||||
std::ifstream file_stream(file_path, std::ios::in);
|
||||
|
||||
if (!file_stream.is_open()) {
|
||||
Logger::error("{} not exist", file_path);
|
||||
}
|
||||
|
||||
std::string line = "";
|
||||
while (!file_stream.eof()) {
|
||||
|
||||
getline(file_stream, line);
|
||||
content.append(line + "\n");
|
||||
}
|
||||
file_stream.close();
|
||||
return content;
|
||||
}
|
||||
|
||||
void delete_image_data(unsigned char* data) { SOIL_free_image_data(data); }
|
||||
|
||||
unsigned char* load_image_data(const std::string& tex_image_path) {
|
||||
fs::path path = ASSETS_PATH + tex_image_path;
|
||||
ASSERT_MSG(fs::is_regular_file(path), path.c_str());
|
||||
unsigned char* data = nullptr;
|
||||
int width, height, channels;
|
||||
data = SOIL_load_image(path.string().c_str(), &width, &height, &channels,
|
||||
SOIL_LOAD_AUTO);
|
||||
ASSERT_MSG(data, "Could not load texture" + path.string());
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
} // namespace Tools
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user