2 Commits

Author SHA1 Message Date
c7383a4208 refactor(render): use Texture, VertexBuffer, VertexArray classes 2026-07-09 20:38:55 +08:00
4e12c69f1b refactor(texture): encapsulate textures with unique_ptr and bind methods
Replace raw GLuint framebuffer textures with std::unique_ptr<Texture>. Update TextureManager to return const Texture* instead of GLuint. Use Texture::bind() for active texture binding. Add RGBA16F and RGB formats. Add texture parameter methods. Update destructors accordingly.
2026-07-09 20:10:08 +08:00
10 changed files with 150 additions and 214 deletions

View File

@@ -3,6 +3,7 @@
#include "Cubed/constants.hpp" #include "Cubed/constants.hpp"
#include "Cubed/primitive_data.hpp" #include "Cubed/primitive_data.hpp"
#include "Cubed/render/player_renderer.hpp" #include "Cubed/render/player_renderer.hpp"
#include "Cubed/render/texture.hpp"
#include "Cubed/render/vertex_array.hpp" #include "Cubed/render/vertex_array.hpp"
#include "Cubed/render/vertex_buffer.hpp" #include "Cubed/render/vertex_buffer.hpp"
#include "Cubed/shader.hpp" #include "Cubed/shader.hpp"
@@ -129,24 +130,23 @@ private:
glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat, m_norm_mat; glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat, m_norm_mat;
std::unique_ptr<VertexBuffer> m_sky_vbo; std::unique_ptr<VertexBuffer> m_sky_vbo;
std::unique_ptr<VertexBuffer> m_text_vbo = 0; std::unique_ptr<VertexBuffer> m_outline_indices_vbo;
std::unique_ptr<VertexBuffer> m_outline_indices_vbo = 0; std::unique_ptr<VertexBuffer> m_outline_vbo;
std::unique_ptr<VertexBuffer> m_outline_vbo = 0; std::unique_ptr<VertexBuffer> m_ui_vbo;
std::unique_ptr<VertexBuffer> m_ui_vbo = 0; std::unique_ptr<VertexBuffer> m_player_vbo;
std::unique_ptr<VertexBuffer> m_player_vbo = 0; std::unique_ptr<VertexBuffer> m_quad_vbo;
std::unique_ptr<VertexBuffer> m_quad_vbo = 0;
GLuint m_fbo = 0; GLuint m_fbo = 0;
GLuint m_screen_texture = 0; std::unique_ptr<Texture> m_screen_texture;
GLuint m_screen_depth_texture = 0; std::unique_ptr<Texture> m_screen_depth_texture;
GLuint m_oit_fbo = 0; GLuint m_oit_fbo = 0;
GLuint m_accum_texture = 0; std::unique_ptr<Texture> m_accum_texture;
GLuint m_reveal_texture = 0; std::unique_ptr<Texture> m_reveal_texture;
GLuint m_oit_depth_texture = 0; std::unique_ptr<Texture> m_oit_depth_texture;
GLuint m_depth_map_fbo = 0; GLuint m_depth_map_fbo = 0;
GLuint m_depth_map_texture = 0; std::unique_ptr<Texture> m_depth_map_texture;
glm::mat4 m_ui_proj; glm::mat4 m_ui_proj;
glm::mat4 m_ui_m_matrix; glm::mat4 m_ui_m_matrix;

View File

@@ -32,8 +32,10 @@ enum TextureFormat : GLenum {
DEPTH_COMPONENT32F = GL_DEPTH_COMPONENT32F, DEPTH_COMPONENT32F = GL_DEPTH_COMPONENT32F,
DEPTH_COMPONENT = GL_DEPTH_COMPONENT, DEPTH_COMPONENT = GL_DEPTH_COMPONENT,
R16F = GL_R16F, R16F = GL_R16F,
RGBA16F = GL_RGBA16F,
RED = GL_RED, RED = GL_RED,
RGBA = GL_RGBA, RGBA = GL_RGBA,
RGB = GL_RGB,
RGBA8 = GL_RGBA8, RGBA8 = GL_RGBA8,
}; };

View File

@@ -40,13 +40,13 @@ public:
~TextureManager(); ~TextureManager();
void delet_texture(); void delet_texture();
GLuint get_block_status_array() const; const Texture* get_block_status_array() const;
GLuint get_texture_array() const; const Texture* get_texture_array() const;
GLuint get_cross_plane_array() const; const Texture* get_cross_plane_array() const;
GLuint get_ui_array() const; const Texture* get_ui_array() const;
GLuint get_pbr_texture() const; const Texture* get_pbr_texture() const;
const std::vector<std::unique_ptr<Texture>>& item_textures() const; const std::vector<std::unique_ptr<Texture>>& item_textures() const;
GLuint get_skin() const; const Texture* get_skin() const;
// Must call after MapTable::init_map() and glfwMakeContextCurrent(window); // Must call after MapTable::init_map() and glfwMakeContextCurrent(window);
void init_texture(); void init_texture();

View File

@@ -1,5 +1,8 @@
#pragma once #pragma once
#include "Cubed/render/texture.hpp"
#include <ft2build.h> #include <ft2build.h>
#include <memory>
#include FT_FREETYPE_H #include FT_FREETYPE_H
#include "Cubed/primitive_data.hpp" #include "Cubed/primitive_data.hpp"
@@ -29,7 +32,7 @@ public:
static std::vector<Vertex2D> vertices(const std::string& text, static std::vector<Vertex2D> vertices(const std::string& text,
float x = 0.0f, float y = 0.0f, float x = 0.0f, float y = 0.0f,
float scale = 1.0f); float scale = 1.0f);
static GLuint text_texture(); static const Texture* text_texture();
static const std::string& font_path(); static const std::string& font_path();
private: private:
@@ -39,7 +42,7 @@ private:
float m_texture_width = 64; float m_texture_width = 64;
float m_texture_height = 64; float m_texture_height = 64;
static inline GLuint m_text_texture; static inline std::unique_ptr<Texture> m_text_texture;
static inline std::string m_font_path{ASSETS_PATH static inline std::string m_font_path{ASSETS_PATH
"fonts/IBMPlexSans-Regular.ttf"}; "fonts/IBMPlexSans-Regular.ttf"};
std::unordered_map<char8_t, Character> m_characters; std::unordered_map<char8_t, Character> m_characters;

View File

@@ -1,6 +1,8 @@
#pragma once #pragma once
#include "Cubed/primitive_data.hpp" #include "Cubed/primitive_data.hpp"
#include "Cubed/render/vertex_array.hpp"
#include "Cubed/render/vertex_buffer.hpp"
#include "Cubed/ui/color.hpp" #include "Cubed/ui/color.hpp"
#include <glad/glad.h> #include <glad/glad.h>
@@ -27,8 +29,7 @@ public:
Text& text(std::string_view str); Text& text(std::string_view str);
std::size_t uuid() const; std::size_t uuid() const;
static void set_loc(const Shader& shader); void render(const Shader& shader);
void render();
bool operator==(const Text& other) const; bool operator==(const Text& other) const;
@@ -38,14 +39,14 @@ private:
const std::string NAME; const std::string NAME;
const std::size_t UUID; const std::size_t UUID;
std::string m_text; std::string m_text;
glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f}; glm::vec4 m_color{1.0f, 1.0f, 1.0f, 1.0f};
glm::mat4 m_model_matrix; glm::mat4 m_model_matrix;
std::vector<Vertex2D> m_vertices; std::vector<Vertex2D> m_vertices;
GLuint m_vbo = 0; std::unique_ptr<VertexBuffer> m_vbo;
static inline GLuint m_color_loc = 0; std::unique_ptr<VertexArray> m_vao;
static inline GLuint m_mv_loc = 0;
void update_vertices(); void update_vertices();
void upload_to_gpu(); void upload_to_gpu();

View File

@@ -223,8 +223,7 @@ void PlayerRenderer::render(const Shader& shader) {
glm::vec3(0, 1, 0)); glm::vec3(0, 1, 0));
model = glm::translate(model, glm::vec3(-0.5f, 0.0f, -0.5f)); model = glm::translate(model, glm::vec3(-0.5f, 0.0f, -0.5f));
glActiveTexture(GL_TEXTURE1); m_renderer.texture_mamger().get_skin()->bind(1);
glBindTexture(GL_TEXTURE_2D, m_renderer.texture_mamger().get_skin());
auto make_rotated = [&](glm::vec3 pivot, float angle) { auto make_rotated = [&](glm::vec3 pivot, float angle) {
glm::mat4 mat = model; glm::mat4 mat = model;

View File

@@ -34,21 +34,20 @@ Renderer::~Renderer() {
m_outline_indices_vbo.reset(); m_outline_indices_vbo.reset();
m_sky_vbo.reset(); m_sky_vbo.reset();
m_ui_vbo.reset(); m_ui_vbo.reset();
m_text_vbo.reset();
m_player_vbo.reset(); m_player_vbo.reset();
glBindVertexArray(0); glBindVertexArray(0);
m_vao.clear(); m_vao.clear();
glDeleteFramebuffers(1, &m_fbo); glDeleteFramebuffers(1, &m_fbo);
glDeleteTextures(1, &m_screen_texture); m_screen_texture.reset();
glDeleteTextures(1, &m_screen_depth_texture); m_screen_depth_texture.reset();
glDeleteFramebuffers(1, &m_oit_fbo); glDeleteFramebuffers(1, &m_oit_fbo);
glDeleteTextures(1, &m_accum_texture); m_accum_texture.reset();
glDeleteTextures(1, &m_reveal_texture); m_oit_depth_texture.reset();
glDeleteTextures(1, &m_oit_depth_texture); m_reveal_texture.reset();
glDeleteFramebuffers(1, &m_depth_map_fbo); glDeleteFramebuffers(1, &m_depth_map_fbo);
glDeleteTextures(1, &m_depth_map_texture); m_depth_map_texture.reset();
} }
} }
@@ -141,7 +140,6 @@ void Renderer::init(bool debug_on) {
m_player_vbo = std::make_unique<VertexBuffer>(); m_player_vbo = std::make_unique<VertexBuffer>();
m_quad_vbo = std::make_unique<VertexBuffer>(); m_quad_vbo = std::make_unique<VertexBuffer>();
m_sky_vbo = std::make_unique<VertexBuffer>(); m_sky_vbo = std::make_unique<VertexBuffer>();
m_text_vbo = std::make_unique<VertexBuffer>();
m_ui_vbo = std::make_unique<VertexBuffer>(); m_ui_vbo = std::make_unique<VertexBuffer>();
m_vao[2].bind(); m_vao[2].bind();
@@ -200,12 +198,6 @@ void Renderer::init_quad() {
void Renderer::init_text() { void Renderer::init_text() {
m_vao[4].bind(); m_vao[4].bind();
m_text_vbo->buffer_data(NULL, sizeof(float) * 6 * 4,
BufferUsage::DYNAMIC_DRAW);
const auto& shader = get_shader("text");
Text::set_loc(shader);
DebugCollector::get().init_text(); DebugCollector::get().init_text();
} }
@@ -395,8 +387,6 @@ void Renderer::render_sky() {
void Renderer::render_text() { void Renderer::render_text() {
m_vao[4].bind();
const auto& shader = get_shader("text"); const auto& shader = get_shader("text");
shader.use(); shader.use();
@@ -410,7 +400,7 @@ void Renderer::render_text() {
auto& texts = DebugCollector::get().all_texts(); auto& texts = DebugCollector::get().all_texts();
for (auto& t : texts) { for (auto& t : texts) {
t.second.render(); t.second.render(shader);
} }
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
@@ -428,9 +418,7 @@ void Renderer::render_ui() {
shader.set_loc("proj_matrix", m_ui_proj); shader.set_loc("proj_matrix", m_ui_proj);
m_vao[3].bind(); m_vao[3].bind();
m_texture_manager.get_ui_array()->bind(0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_ui_array());
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
Tools::check_opengl_error(); Tools::check_opengl_error();
@@ -455,12 +443,11 @@ void Renderer::render_underwater() {
shader.set_loc("InverseViewProjection", glm::inverse(m_p_mat * m_v_mat)); shader.set_loc("InverseViewProjection", glm::inverse(m_p_mat * m_v_mat));
shader.set_loc("sunColor", m_parallel_light.sun_color); shader.set_loc("sunColor", m_parallel_light.sun_color);
shader.set_loc("u_lightSpaceMatrix", m_parallel_light.light_space_matrix); shader.set_loc("u_lightSpaceMatrix", m_parallel_light.light_space_matrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_screen_texture); m_screen_texture->bind(0);
glActiveTexture(GL_TEXTURE1); m_screen_depth_texture->bind(1);
glBindTexture(GL_TEXTURE_2D, m_screen_depth_texture); m_depth_map_texture->bind(2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_depth_map_texture);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0); glBindVertexArray(0);
} }
@@ -494,28 +481,22 @@ void Renderer::updata_framebuffer(int width, int height) {
glGenFramebuffers(1, &m_oit_fbo); glGenFramebuffers(1, &m_oit_fbo);
} }
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
m_screen_texture = std::make_unique<Texture>(TextureType::TEXTURE_2D);
m_screen_texture->tex_image_2d(TextureFormat::RGB, TextureFormat::RGB,
GL_UNSIGNED_BYTE, nullptr, width, height);
glDeleteTextures(1, &m_screen_texture); m_screen_texture->set_linear();
glDeleteTextures(1, &m_screen_depth_texture);
glGenTextures(1, &m_screen_texture);
glBindTexture(GL_TEXTURE_2D, m_screen_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
m_screen_texture, 0); m_screen_texture->id(), 0);
glGenTextures(1, &m_screen_depth_texture); m_screen_depth_texture = std::make_unique<Texture>(TextureType::TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_screen_depth_texture); m_screen_depth_texture->tex_image_2d(TextureFormat::DEPTH_COMPONENT32F,
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, TextureFormat::DEPTH_COMPONENT,
GL_DEPTH_COMPONENT, GL_FLOAT, NULL); GL_FLOAT, nullptr, width, height);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // m_screen_depth_texture->set_nearest();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); m_screen_depth_texture->set_linear();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
m_screen_depth_texture, 0); m_screen_depth_texture->id(), 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
Logger::error("FBO incomplete after resize!"); Logger::error("FBO incomplete after resize!");
@@ -525,33 +506,27 @@ void Renderer::updata_framebuffer(int width, int height) {
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, m_oit_fbo); glBindFramebuffer(GL_FRAMEBUFFER, m_oit_fbo);
glDeleteTextures(1, &m_accum_texture); m_accum_texture = std::make_unique<Texture>(TextureType::TEXTURE_2D);
glDeleteTextures(1, &m_reveal_texture); m_accum_texture->tex_image_2d(TextureFormat::RGBA16F, TextureFormat::RGBA,
glDeleteTextures(1, &m_oit_depth_texture); GL_HALF_FLOAT, nullptr, width, height);
glGenTextures(1, &m_accum_texture); m_accum_texture->set_linear();
glBindTexture(GL_TEXTURE_2D, m_accum_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA,
GL_HALF_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
m_accum_texture, 0); m_accum_texture->id(), 0);
glGenTextures(1, &m_reveal_texture); m_reveal_texture = std::make_unique<Texture>(TextureType::TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_reveal_texture); m_reveal_texture->tex_image_2d(TextureFormat::R16F, TextureFormat::RED,
glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, width, height, 0, GL_RED, GL_HALF_FLOAT, nullptr, width, height);
GL_HALF_FLOAT, NULL); m_reveal_texture->set_linear();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D,
m_reveal_texture, 0); m_reveal_texture->id(), 0);
glGenTextures(1, &m_oit_depth_texture); m_oit_depth_texture = std::make_unique<Texture>(TextureType::TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_oit_depth_texture); m_oit_depth_texture->tex_image_2d(TextureFormat::DEPTH_COMPONENT32F,
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, TextureFormat::DEPTH_COMPONENT, GL_FLOAT,
GL_DEPTH_COMPONENT, GL_FLOAT, NULL); nullptr, width, height);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // m_oit_depth_texture->set_nearest();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); m_oit_depth_texture->set_linear();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
m_oit_depth_texture, 0); m_oit_depth_texture->id(), 0);
GLenum draw_buffer[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; GLenum draw_buffer[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, draw_buffer); glDrawBuffers(2, draw_buffer);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
@@ -565,27 +540,27 @@ void Renderer::updata_framebuffer(int width, int height) {
if (m_depth_map_fbo == 0) { if (m_depth_map_fbo == 0) {
glGenFramebuffers(1, &m_depth_map_fbo); glGenFramebuffers(1, &m_depth_map_fbo);
} }
glDeleteTextures(1, &m_depth_map_texture);
glGenTextures(1, &m_depth_map_texture);
glBindTexture(GL_TEXTURE_2D, m_depth_map_texture); m_depth_map_texture = std::make_unique<Texture>(TextureType::TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, DEPTH_MAP_SIZE,
DEPTH_MAP_SIZE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); m_depth_map_texture->tex_image_2d(TextureFormat::DEPTH_COMPONENT32F,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); TextureFormat::DEPTH_COMPONENT, GL_FLOAT,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); nullptr, DEPTH_MAP_SIZE, DEPTH_MAP_SIZE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); m_depth_map_texture->set_linear();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); m_depth_map_texture->set_clamp_to_border(false, true, true);
float border_color[] = {1.0f, 1.0f, 1.0f, 1.0f}; float border_color[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
// Manually compare shadows // Manually compare shadows
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); m_depth_map_texture->parameterfv(TexturePname::BORDER_COLOR, border_color);
m_depth_map_texture->parameter(TexturePname::COMPARE_MODE,
TextureParam::T_NONE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
// GL_COMPARE_REF_TO_TEXTURE); // GL_COMPARE_REF_TO_TEXTURE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glBindFramebuffer(GL_FRAMEBUFFER, m_depth_map_fbo); glBindFramebuffer(GL_FRAMEBUFFER, m_depth_map_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
m_depth_map_texture, 0); m_depth_map_texture->id(), 0);
glDrawBuffer(GL_NONE); glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE); glReadBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
@@ -662,14 +637,12 @@ void Renderer::render_world() {
glBindFramebuffer(GL_FRAMEBUFFER, m_depth_map_fbo); glBindFramebuffer(GL_FRAMEBUFFER, m_depth_map_fbo);
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
for (const auto& snapshot : m_render_snapshots) { for (const auto& snapshot : m_render_snapshots) {
if (!snapshot) { if (!snapshot) {
continue; continue;
} }
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_texture_array()->bind(1);
m_texture_manager.get_texture_array());
glBindVertexArray(snapshot->normal_vao); glBindVertexArray(snapshot->normal_vao);
glDrawArrays(GL_TRIANGLES, 0, snapshot->normal_vertices_count); glDrawArrays(GL_TRIANGLES, 0, snapshot->normal_vertices_count);
@@ -686,8 +659,7 @@ void Renderer::render_world() {
glm::vec2 center_xz{snapshot->center.x, snapshot->center.z}; glm::vec2 center_xz{snapshot->center.x, snapshot->center.z};
float dist2d = glm::distance(camera_pos_xz, center_xz); float dist2d = glm::distance(camera_pos_xz, center_xz);
if (dist2d <= CROSS_PLANE_DISTANCE * 16) { if (dist2d <= CROSS_PLANE_DISTANCE * 16) {
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_cross_plane_array()->bind(1);
m_texture_manager.get_cross_plane_array());
glBindVertexArray(snapshot->cross_vao); glBindVertexArray(snapshot->cross_vao);
glDrawArrays(GL_TRIANGLES, 0, glDrawArrays(GL_TRIANGLES, 0,
@@ -695,8 +667,9 @@ void Renderer::render_world() {
} }
} }
if (snapshot->normal_discard_vertices_count != 0) { if (snapshot->normal_discard_vertices_count != 0) {
glBindTexture(GL_TEXTURE_2D_ARRAY,
m_texture_manager.get_texture_array()); m_texture_manager.get_texture_array()->bind(1);
glBindVertexArray(snapshot->normal_discard_vao); glBindVertexArray(snapshot->normal_discard_vao);
glDrawArrays(GL_TRIANGLES, 0, glDrawArrays(GL_TRIANGLES, 0,
@@ -751,12 +724,11 @@ void Renderer::render_world() {
int rendered_sum = 0; int rendered_sum = 0;
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0); m_depth_map_texture->bind(0);
glBindTexture(GL_TEXTURE_2D, m_depth_map_texture);
glActiveTexture(GL_TEXTURE1); m_texture_manager.get_texture_array()->bind(1);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_texture_array()); m_texture_manager.get_pbr_texture()->bind(2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_pbr_texture());
normal_block_shader.set_loc("enablePBR", m_pbr); normal_block_shader.set_loc("enablePBR", m_pbr);
for (const auto& snapshot : m_render_snapshots) { for (const auto& snapshot : m_render_snapshots) {
if (!snapshot) { if (!snapshot) {
@@ -789,9 +761,7 @@ void Renderer::render_world() {
} }
} }
// cross_plane // cross_plane
glActiveTexture(GL_TEXTURE1); m_texture_manager.get_cross_plane_array()->bind(1);
glBindTexture(GL_TEXTURE_2D_ARRAY,
m_texture_manager.get_cross_plane_array());
normal_block_shader.set_loc("enablePBR", false); normal_block_shader.set_loc("enablePBR", false);
for (const auto& snapshot : m_render_snapshots) { for (const auto& snapshot : m_render_snapshots) {
if (!snapshot) { if (!snapshot) {
@@ -855,8 +825,9 @@ void Renderer::render_world() {
set_accum_loc(accum_shader); set_accum_loc(accum_shader);
accum_shader.set_loc("cameraPos", m_camera.get_camera_pos()); accum_shader.set_loc("cameraPos", m_camera.get_camera_pos());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_texture_array()); m_texture_manager.get_texture_array()->bind(0);
for (const auto& snapshot : m_render_snapshots) { for (const auto& snapshot : m_render_snapshots) {
if (!snapshot) { if (!snapshot) {
continue; continue;
@@ -902,14 +873,9 @@ void Renderer::render_world() {
water_shader.set_loc("refractStrength", m_refract_strength); water_shader.set_loc("refractStrength", m_refract_strength);
water_shader.set_loc("enablePerturb", m_water_perturb); water_shader.set_loc("enablePerturb", m_water_perturb);
water_shader.set_loc("enableDepthFade", m_water_depth_fade); water_shader.set_loc("enableDepthFade", m_water_depth_fade);
m_screen_texture->bind(1);
glActiveTexture(GL_TEXTURE1); m_screen_depth_texture->bind(2);
glBindTexture(GL_TEXTURE_2D, m_screen_texture); m_texture_manager.get_texture_array()->bind(0);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_screen_depth_texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_texture_array());
for (const auto& snapshot : m_render_snapshots) { for (const auto& snapshot : m_render_snapshots) {
if (!snapshot) { if (!snapshot) {
continue; continue;
@@ -939,11 +905,8 @@ void Renderer::render_world() {
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
m_vao[0].bind(); m_vao[0].bind();
m_accum_texture->bind(0);
glActiveTexture(GL_TEXTURE0); m_reveal_texture->bind(1);
glBindTexture(GL_TEXTURE_2D, m_accum_texture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_reveal_texture);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
@@ -973,10 +936,7 @@ void Renderer::render_player() {
shader.set_loc("samples", m_samples); shader.set_loc("samples", m_samples);
// shader.set_loc("renderDistance", m_world.rendering_distance()); // shader.set_loc("renderDistance", m_world.rendering_distance());
// shader.set_loc("skyColor", m_sky_uniform.sky_top); // shader.set_loc("skyColor", m_sky_uniform.sky_top);
m_depth_map_texture->bind(0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_depth_map_texture);
m_player_renderer.render(shader); m_player_renderer.render(shader);
} }

View File

@@ -50,21 +50,21 @@ void TextureManager::delet_texture() {
} }
} }
GLuint TextureManager::get_block_status_array() const { const Texture* TextureManager::get_block_status_array() const {
return m_block_status_array->id(); return m_block_status_array.get();
} }
GLuint TextureManager::get_texture_array() const { const Texture* TextureManager::get_texture_array() const {
return m_texture_array->id(); return m_texture_array.get();
} }
GLuint TextureManager::get_cross_plane_array() const { const Texture* TextureManager::get_cross_plane_array() const {
return m_cross_plane_array->id(); return m_cross_plane_array.get();
} }
GLuint TextureManager::get_ui_array() const { return m_ui_array->id(); } const Texture* TextureManager::get_ui_array() const { return m_ui_array.get(); }
GLuint TextureManager::get_pbr_texture() const { const Texture* TextureManager::get_pbr_texture() const {
return m_normal_texture_array->id(); return m_normal_texture_array.get();
} }
const std::vector<std::unique_ptr<Texture>>& const std::vector<std::unique_ptr<Texture>>&
@@ -72,7 +72,7 @@ TextureManager::item_textures() const {
return m_item_textures; return m_item_textures;
} }
GLuint TextureManager::get_skin() const { return m_skin->id(); } const Texture* TextureManager::get_skin() const { return m_skin.get(); }
void TextureManager::load_block_status(unsigned id) { void TextureManager::load_block_status(unsigned id) {

View File

@@ -1,9 +1,7 @@
#include "Cubed/tools/font.hpp" #include "Cubed/tools/font.hpp"
#include "Cubed/constants.hpp" #include "Cubed/constants.hpp"
#include "Cubed/shader.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
#include "Cubed/tools/shader_tools.hpp"
namespace fs = std::filesystem; namespace fs = std::filesystem;
@@ -26,7 +24,6 @@ Font::~Font() {
FT_Done_Face(m_face); FT_Done_Face(m_face);
FT_Done_FreeType(m_ft); FT_Done_FreeType(m_ft);
glDeleteTextures(1, &m_text_texture);
} }
void Font::load_character(char8_t c) { void Font::load_character(char8_t c) {
@@ -36,10 +33,9 @@ void Font::load_character(char8_t c) {
} }
const auto& width = m_face->glyph->bitmap.width; const auto& width = m_face->glyph->bitmap.width;
const auto& height = m_face->glyph->bitmap.rows; const auto& height = m_face->glyph->bitmap.rows;
glBindTexture(GL_TEXTURE_2D_ARRAY, m_text_texture); m_text_texture->tex_sub_image_3d(TextureFormat::RED, GL_UNSIGNED_BYTE,
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, static_cast<int>(c), width, m_face->glyph->bitmap.buffer, 0, 0,
height, 1, GL_RED, GL_UNSIGNED_BYTE, static_cast<int>(c), width, height);
m_face->glyph->bitmap.buffer);
Character character = { Character character = {
glm::vec2{0.0f, 0.0f}, glm::vec2{0.0f, 0.0f},
@@ -54,22 +50,16 @@ void Font::load_character(char8_t c) {
void Font::setup_font_character() { void Font::setup_font_character() {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
m_text_texture = std::make_unique<Texture>(TextureType::TEXTURE_2D_ARRAY);
glGenTextures(1, &m_text_texture); m_text_texture->tex_image_3d(TextureFormat::RED, TextureFormat::RED,
glBindTexture(GL_TEXTURE_2D_ARRAY, m_text_texture); GL_UNSIGNED_BYTE, nullptr, m_texture_width,
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RED, m_texture_width, m_texture_height, MAX_CHARACTER);
m_texture_height, MAX_CHARACTER, 0, GL_RED, GL_UNSIGNED_BYTE,
nullptr);
for (char8_t c = 0; c < 128; c++) { for (char8_t c = 0; c < 128; c++) {
load_character(c); load_character(c);
} }
m_text_texture->set_linear();
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); m_text_texture->set_clamp_to_edge(false, true, true);
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);
} }
std::vector<Vertex2D> Font::vertices(const std::string& text, float x, float y, std::vector<Vertex2D> Font::vertices(const std::string& text, float x, float y,
@@ -110,7 +100,7 @@ std::vector<Vertex2D> Font::vertices(const std::string& text, float x, float y,
return vertices; return vertices;
} }
GLuint Font::text_texture() { return m_text_texture; } const Texture* Font::text_texture() { return m_text_texture.get(); }
const std::string& Font::font_path() { return m_font_path; } const std::string& Font::font_path() { return m_font_path; }

View File

@@ -9,30 +9,30 @@
namespace Cubed { namespace Cubed {
Text::Text(std::string_view name) : NAME(name), UUID(HASH::str(name)) {} Text::Text(std::string_view name)
: NAME(name), UUID(HASH::str(name)),
m_vbo(std::make_unique<VertexBuffer>()),
m_vao(std::make_unique<VertexArray>()) {}
Text::Text(std::string_view name, std::string_view str, glm::vec2 pos, Text::Text(std::string_view name, std::string_view str, glm::vec2 pos,
Color color) Color color)
: NAME(name), UUID(HASH::str(name)) { : NAME(name), UUID(HASH::str(name)),
m_vbo(std::make_unique<VertexBuffer>()),
m_vao(std::make_unique<VertexArray>()) {
m_text.assign(str); m_text.assign(str);
m_pos = pos; m_pos = pos;
m_color = color_value(color); m_color = color_value(color);
update_vertices(); update_vertices();
} }
Text::~Text() { Text::~Text() { m_vbo.reset(); }
if (m_vbo != 0) {
glDeleteBuffers(1, &m_vbo);
}
}
Text::Text(Text&& other) noexcept Text::Text(Text&& other) noexcept
: m_scale(other.m_scale), m_pos(other.m_pos), NAME(other.NAME), : m_scale(other.m_scale), m_pos(other.m_pos), NAME(other.NAME),
UUID(other.UUID), m_text(std::move(other.m_text)), m_color(other.m_color), UUID(other.UUID), m_text(std::move(other.m_text)), m_color(other.m_color),
m_model_matrix(other.m_model_matrix), m_model_matrix(other.m_model_matrix),
m_vertices(std::move(other.m_vertices)), m_vbo(other.m_vbo) { m_vertices(std::move(other.m_vertices)), m_vbo(std::move(other.m_vbo)),
other.m_vbo = 0; m_vao(std::move(other.m_vao)) {}
}
Text& Text::color(Color color) { Text& Text::color(Color color) {
m_color = color_value(color); m_color = color_value(color);
@@ -51,45 +51,24 @@ Text& Text::scale(float s) {
std::size_t Text::uuid() const { return UUID; } std::size_t Text::uuid() const { return UUID; }
void Text::set_loc(const Shader& shader) {
m_color_loc = shader.loc("textColor");
m_mv_loc = shader.loc("mv_matrix");
}
Text& Text::text(std::string_view str) { Text& Text::text(std::string_view str) {
m_text.assign(str); m_text.assign(str);
update_vertices(); update_vertices();
return *this; return *this;
} }
void Text::render() { void Text::render(const Shader& shader) {
ASSERT_MSG(m_vbo != 0, "VBO not initialized!"); ASSERT_MSG(m_vbo != 0, "VBO not initialized!");
ASSERT_MSG(!m_vertices.empty(), "Text String Not Set"); ASSERT_MSG(!m_vertices.empty(), "Text String Not Set");
glActiveTexture(GL_TEXTURE0); Font::text_texture()->bind(0);
glBindTexture(GL_TEXTURE_2D_ARRAY, Font::text_texture()); m_vao->bind();
ASSERT_MSG(m_color_loc, "m_color_loc is null");
m_model_matrix = m_model_matrix =
glm::translate(glm::mat4(1.0f), glm::vec3(m_pos.x, m_pos.y, 0.0f)) * glm::translate(glm::mat4(1.0f), glm::vec3(m_pos.x, m_pos.y, 0.0f)) *
glm::scale(glm::mat4(1.0f), glm::vec3(m_scale, m_scale, 1.0f)); glm::scale(glm::mat4(1.0f), glm::vec3(m_scale, m_scale, 1.0f));
glUniform3f(m_color_loc, m_color.x, m_color.y, m_color.z); shader.set_loc("textColor", glm::vec3(m_color.x, m_color.y, m_color.z));
glUniformMatrix4fv(m_mv_loc, 1, GL_FALSE, glm::value_ptr(m_model_matrix)); shader.set_loc("mv_matrix", m_model_matrix);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D),
(void*)offsetof(Vertex2D, s));
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex2D),
(void*)offsetof(Vertex2D, layer));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glDrawArrays(GL_TRIANGLES, 0, m_vertices.size()); glDrawArrays(GL_TRIANGLES, 0, m_vertices.size());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
} }
void Text::update_vertices() { void Text::update_vertices() {
@@ -98,13 +77,15 @@ void Text::update_vertices() {
} }
void Text::upload_to_gpu() { void Text::upload_to_gpu() {
if (m_vbo == 0) {
glGenBuffers(1, &m_vbo);
}
ASSERT_MSG(m_vbo, "Vbo Is Not Gen"); ASSERT_MSG(m_vbo, "Vbo Is Not Gen");
glBindBuffer(GL_ARRAY_BUFFER, m_vbo); m_vao->bind();
glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(Vertex2D), m_vbo->buffer_data(m_vertices.data(), m_vertices.size() * sizeof(Vertex2D),
m_vertices.data(), GL_DYNAMIC_DRAW); BufferUsage::DYNAMIC_DRAW);
m_vao->attribute(0, 2, GL_FLOAT, sizeof(Vertex2D), (void*)0);
m_vao->attribute(1, 2, GL_FLOAT, sizeof(Vertex2D),
(void*)offsetof(Vertex2D, s));
m_vao->attribute(2, 1, GL_FLOAT, sizeof(Vertex2D),
(void*)offsetof(Vertex2D, layer));
} }
bool Text::operator==(const Text& other) const { return UUID == other.uuid(); } bool Text::operator==(const Text& other) const { return UUID == other.uuid(); }