mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
feat: add ui renderer
This commit is contained in:
BIN
assets/texture/ui/0.png
Normal file
BIN
assets/texture/ui/0.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 110 B |
@@ -20,15 +20,16 @@ public:
|
||||
|
||||
private:
|
||||
Camera m_camera;
|
||||
TextureManager m_texture_manager;
|
||||
World m_world;
|
||||
Renderer m_renderer{m_camera, m_world};
|
||||
Renderer m_renderer{m_camera, m_world, m_texture_manager};
|
||||
|
||||
Window m_window{m_renderer};
|
||||
|
||||
|
||||
GLuint m_texture_array;
|
||||
|
||||
TextureManager m_texture_manager;
|
||||
|
||||
|
||||
|
||||
void init();
|
||||
|
||||
@@ -3,7 +3,7 @@ constexpr int WORLD_SIZE_X = 32;
|
||||
constexpr int WORLD_SIZE_Z = 32;
|
||||
constexpr int WORLD_SIZE_Y = 16;
|
||||
constexpr int MAX_BLOCK_NUM = 2;
|
||||
|
||||
constexpr int MAX_UI_NUM = 1;
|
||||
|
||||
constexpr int CHUCK_SIZE = 16;
|
||||
constexpr int DISTANCE = 8;
|
||||
@@ -83,3 +83,33 @@ constexpr int OUTLINE_CUBE_INDICES[24] = {
|
||||
4,5, 5,6, 6,7, 7,4,
|
||||
0,4, 1,5, 2,6, 3,7
|
||||
};
|
||||
|
||||
constexpr float SQUARE_VERTICES[6][2] = {
|
||||
-0.5f, -0.5f, // bottom left
|
||||
-0.5f, 0.5f, // top left
|
||||
0.5f, 0.5f, // top right
|
||||
0.5f, 0.5f, // top right
|
||||
0.5f, -0.5f, // bottom right
|
||||
-0.5f, -0.5f // bottom left
|
||||
};
|
||||
|
||||
constexpr float SQUARE_TEXTURE_POS[6][2] = {
|
||||
0.0f, 0.0f,
|
||||
0.0f, 1.0f,
|
||||
1.0f, 1.0f,
|
||||
1.0f, 1.0f,
|
||||
1.0f, 0.0f,
|
||||
0.0f, 0.0f,
|
||||
};
|
||||
|
||||
struct Vertex {
|
||||
float x, y, z;
|
||||
float s, t;
|
||||
float layer;
|
||||
};
|
||||
|
||||
struct Vertex2D {
|
||||
float x, y;
|
||||
float s, t;
|
||||
float layer;
|
||||
};
|
||||
@@ -13,8 +13,4 @@ struct ChunkPos {
|
||||
};
|
||||
};
|
||||
|
||||
struct Vertex {
|
||||
float x, y, z;
|
||||
float s, t;
|
||||
float layer;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,22 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <Cubed/config.hpp>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
|
||||
class Camera;
|
||||
class TextureManager;
|
||||
class World;
|
||||
class Renderer {
|
||||
public:
|
||||
constexpr static int NUM_VAO = 1;
|
||||
|
||||
Renderer(const Camera& camera, World& world);
|
||||
Renderer(const Camera& camera, World& world, const TextureManager& texture_manager);
|
||||
~Renderer();
|
||||
void init();
|
||||
void render(GLuint texture_array);
|
||||
void update_proj_matrix(float aspect);
|
||||
void render();
|
||||
void update_proj_matrix(float aspect, float width, float height);
|
||||
private:
|
||||
|
||||
const Camera& m_camera;
|
||||
const TextureManager& m_texture_manager;
|
||||
World& m_world;
|
||||
|
||||
glm::mat4 m_p_mat, m_v_mat, m_m_mat, m_mv_mat, m_mvp_mat;
|
||||
@@ -27,13 +32,20 @@ private:
|
||||
GLuint m_sky_vbo;
|
||||
GLuint m_outline_indices_vbo;
|
||||
GLuint m_outline_vbo;
|
||||
GLuint m_ui_vbo;
|
||||
|
||||
GLuint m_sky_program;
|
||||
GLuint m_outline_program;
|
||||
GLuint m_ui_program;
|
||||
GLuint m_world_program;
|
||||
|
||||
std::vector<GLuint> m_vao;
|
||||
|
||||
glm::mat4 m_ui_proj;
|
||||
glm::mat4 m_ui_m_matrix;
|
||||
|
||||
std::vector<GLuint> m_vao;
|
||||
std::vector<Vertex2D> m_ui;
|
||||
void render_outline();
|
||||
void render_sky();
|
||||
void render_ui();
|
||||
};
|
||||
@@ -6,19 +6,20 @@
|
||||
|
||||
class TextureManager {
|
||||
private:
|
||||
GLuint m_texture_array;
|
||||
GLuint m_block_status_array;
|
||||
void load_block_status(int status_id);
|
||||
GLuint m_texture_array;
|
||||
GLuint m_ui_array;
|
||||
void load_block_status(unsigned status_id);
|
||||
void load_block_texture(unsigned block_id);
|
||||
|
||||
void load_ui_texture(unsigned id);
|
||||
public:
|
||||
TextureManager();
|
||||
~TextureManager();
|
||||
|
||||
void delet_texture();
|
||||
GLuint get_block_status_array();
|
||||
GLuint get_texture_array();
|
||||
|
||||
GLuint get_block_status_array() const;
|
||||
GLuint get_texture_array() const;
|
||||
GLuint get_ui_array() const;
|
||||
// Must call after MapTable::init_map() and glfwMakeContextCurrent(window);
|
||||
void init_texture();
|
||||
};
|
||||
@@ -36,7 +36,7 @@ void App::init() {
|
||||
|
||||
m_renderer.init();
|
||||
m_window.update_viewport();
|
||||
MapTable::init_map();
|
||||
//MapTable::init_map();
|
||||
m_texture_manager.init_texture();
|
||||
m_world.init_world();
|
||||
m_texture_array = m_texture_manager.get_texture_array();
|
||||
@@ -98,7 +98,7 @@ void App::window_reshape_callback(GLFWwindow* window, int new_width, int new_hei
|
||||
|
||||
void App::render() {
|
||||
|
||||
m_renderer.render(m_texture_array);
|
||||
m_renderer.render();
|
||||
|
||||
glfwSwapBuffers(m_window.get_glfw_window());
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <Cubed/camera.hpp>
|
||||
#include <Cubed/config.hpp>
|
||||
#include <Cubed/gameplay/world.hpp>
|
||||
#include <Cubed/texture_manager.hpp>
|
||||
#include <Cubed/renderer.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
@@ -8,8 +9,9 @@
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
Renderer::Renderer(const Camera& camera, World& world):
|
||||
Renderer::Renderer(const Camera& camera, World& world, const TextureManager& texture_manager):
|
||||
m_camera(camera),
|
||||
m_texture_manager(texture_manager),
|
||||
m_world(world)
|
||||
{
|
||||
|
||||
@@ -20,11 +22,13 @@ Renderer::~Renderer() {
|
||||
glDeleteBuffers(1, &m_outline_vbo);
|
||||
glDeleteBuffers(1, &m_outline_indices_vbo);
|
||||
glDeleteBuffers(1, &m_sky_vbo);
|
||||
glDeleteBuffers(1, &m_ui_vbo);
|
||||
glBindVertexArray(0);
|
||||
glDeleteVertexArrays(NUM_VAO, m_vao.data());
|
||||
glDeleteProgram(m_world_program);
|
||||
glDeleteProgram(m_outline_program);
|
||||
glDeleteProgram(m_sky_program);
|
||||
glDeleteProgram(m_ui_program);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,10 +44,12 @@ void Renderer::init() {
|
||||
m_world_program = Shader::create_shader_program("shaders/block_v_shader.glsl", "shaders/block_f_shader.glsl");
|
||||
m_outline_program = Shader::create_shader_program("shaders/outline_v_shader.glsl", "shaders/outline_f_shader.glsl");
|
||||
m_sky_program = Shader::create_shader_program("shaders/sky_v_shader.glsl", "shaders/sky_f_shader.glsl");
|
||||
m_ui_program = Shader::create_shader_program("shaders/ui_v_shader.glsl", "shaders/ui_f_shader.glsl");
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
#ifndef NDEBUG
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glDebugMessageCallback([](GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* user_param) {
|
||||
@@ -69,14 +75,26 @@ void Renderer::init() {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_sky_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES_POS), VERTICES_POS, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
|
||||
|
||||
glGenBuffers(1, &m_ui_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_ui_vbo);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Vertex2D vex {
|
||||
SQUARE_VERTICES[i][0],
|
||||
SQUARE_VERTICES[i][1],
|
||||
SQUARE_TEXTURE_POS[i][0],
|
||||
SQUARE_TEXTURE_POS[i][1],
|
||||
0
|
||||
};
|
||||
m_ui.emplace_back(vex);
|
||||
}
|
||||
|
||||
void Renderer::render(GLuint texture_array) {
|
||||
glBufferData(GL_ARRAY_BUFFER, m_ui.size() * sizeof(Vertex2D), m_ui.data(), GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void Renderer::render() {
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
@@ -88,7 +106,7 @@ void Renderer::render(GLuint texture_array) {
|
||||
m_proj_loc = glGetUniformLocation(m_world_program, "proj_matrix");
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, texture_array);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_texture_array());
|
||||
m_m_mat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
m_v_mat = m_camera.get_camera_lookat();
|
||||
m_mv_mat = m_v_mat * m_m_mat;
|
||||
@@ -98,6 +116,8 @@ void Renderer::render(GLuint texture_array) {
|
||||
m_world.render(m_mvp_mat);
|
||||
|
||||
render_outline();
|
||||
|
||||
render_ui();
|
||||
}
|
||||
|
||||
void Renderer::render_outline() {
|
||||
@@ -150,6 +170,41 @@ void Renderer::render_sky() {
|
||||
|
||||
}
|
||||
|
||||
void Renderer::update_proj_matrix(float aspect) {
|
||||
m_p_mat = glm::perspective(glm::radians(FOV), aspect, 0.1f, 1000.0f);
|
||||
void Renderer::render_ui() {
|
||||
glUseProgram(m_ui_program);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
m_mv_loc = glGetUniformLocation(m_ui_program, "m_matrix");
|
||||
m_proj_loc = glGetUniformLocation(m_ui_program, "proj_matrix");
|
||||
|
||||
glUniformMatrix4fv(m_mv_loc, 1, GL_FALSE, glm::value_ptr(m_ui_m_matrix));
|
||||
glUniformMatrix4fv(m_proj_loc, 1, GL_FALSE, glm::value_ptr(m_ui_proj));
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_ui_vbo);
|
||||
glVertexAttribPointer(0, 3, 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);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_manager.get_ui_array());
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
Shader::check_opengl_error();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
}
|
||||
|
||||
void Renderer::update_proj_matrix(float aspect, float width, float height) {
|
||||
m_p_mat = glm::perspective(glm::radians(FOV), aspect, 0.1f, 1000.0f);
|
||||
m_ui_proj = glm::ortho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
|
||||
// scale and then translate
|
||||
m_ui_m_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(width / 2.0f, height / 2.0f, 0.0)) *
|
||||
glm::scale(glm::mat4(1.0f), glm::vec3(50.0f, 50.0f, 1.0f));
|
||||
|
||||
}
|
||||
12
src/shaders/ui_f_shader.glsl
Normal file
12
src/shaders/ui_f_shader.glsl
Normal file
@@ -0,0 +1,12 @@
|
||||
#version 460
|
||||
|
||||
in vec2 tc;
|
||||
flat in int tex_layer;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
layout (binding = 0) uniform sampler2DArray samp;
|
||||
|
||||
void main(void) {
|
||||
color = texture(samp, vec3(tc, tex_layer));
|
||||
}
|
||||
18
src/shaders/ui_v_shader.glsl
Normal file
18
src/shaders/ui_v_shader.glsl
Normal file
@@ -0,0 +1,18 @@
|
||||
#version 460
|
||||
|
||||
layout (location = 0) in vec2 pos;
|
||||
layout (location = 1) in vec2 texCoord;
|
||||
layout (location = 2) in float layer;
|
||||
|
||||
out vec2 tc;
|
||||
|
||||
flat out int tex_layer;
|
||||
|
||||
uniform mat4 m_matrix;
|
||||
uniform mat4 proj_matrix;
|
||||
|
||||
void main(void) {
|
||||
gl_Position = proj_matrix * m_matrix * vec4(pos, 0.0, 1.0);
|
||||
tc = texCoord;
|
||||
tex_layer = int(layer);
|
||||
}
|
||||
@@ -17,14 +17,19 @@ void TextureManager::delet_texture() {
|
||||
LOG::info("Successfully delete all texture");
|
||||
}
|
||||
|
||||
GLuint TextureManager::get_block_status_array() {
|
||||
GLuint TextureManager::get_block_status_array() const{
|
||||
return m_block_status_array;
|
||||
}
|
||||
|
||||
GLuint TextureManager::get_texture_array() {
|
||||
GLuint TextureManager::get_texture_array() const{
|
||||
return m_texture_array;
|
||||
}
|
||||
void TextureManager::load_block_status(int id) {
|
||||
|
||||
GLuint TextureManager::get_ui_array() const{
|
||||
return m_ui_array;
|
||||
}
|
||||
|
||||
void TextureManager::load_block_status(unsigned id) {
|
||||
|
||||
CUBED_ASSERT_MSG(id < MAX_BLOCK_STATUS, "Exceed the max status sum limit");
|
||||
std::string path = "assets/texture/status/" + std::to_string(id) + ".png";
|
||||
@@ -39,6 +44,7 @@ void TextureManager::load_block_status(int id) {
|
||||
);
|
||||
Shader::delete_image_data(image_data);
|
||||
}
|
||||
|
||||
void TextureManager::load_block_texture(unsigned id) {
|
||||
CUBED_ASSERT_MSG(id < MAX_BLOCK_NUM, "Exceed the max block sum limit");
|
||||
const std::string& name = MapTable::get_name_from_id(id);
|
||||
@@ -71,6 +77,23 @@ void TextureManager::load_block_texture(unsigned id) {
|
||||
|
||||
}
|
||||
|
||||
void TextureManager::load_ui_texture(unsigned id) {
|
||||
CUBED_ASSERT_MSG(id < MAX_UI_NUM, "Exceed the max ui sum limit");
|
||||
|
||||
std::string path = "assets/texture/ui/" + std::to_string(id) + ".png";
|
||||
unsigned char* image_data = nullptr;
|
||||
image_data = (Shader::load_image_data(path));
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_ui_array);
|
||||
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0,
|
||||
0 ,0, id,
|
||||
16, 16, 1,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
image_data
|
||||
);
|
||||
Shader::delete_image_data(image_data);
|
||||
|
||||
}
|
||||
|
||||
void TextureManager::init_texture() {
|
||||
|
||||
MapTable::init_map();
|
||||
@@ -135,4 +158,24 @@ void TextureManager::init_texture() {
|
||||
glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY, max_aniso);
|
||||
}
|
||||
|
||||
glGenTextures(1, &m_ui_array);
|
||||
Shader::check_opengl_error();
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_ui_array);
|
||||
Shader::check_opengl_error();
|
||||
glTexImage3D(GL_TEXTURE_2D_ARRAY,
|
||||
0, GL_RGBA,
|
||||
16, 16,
|
||||
MAX_UI_NUM,
|
||||
0, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, nullptr);
|
||||
Shader::check_opengl_error();
|
||||
for (int i = 0; i < MAX_UI_NUM; i++) {
|
||||
load_ui_texture(i);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_ui_array);
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
|
||||
|
||||
}
|
||||
@@ -27,7 +27,7 @@ void Window::update_viewport() {
|
||||
glfwGetFramebufferSize(m_window, &m_width, &m_height);
|
||||
m_aspect = (float)m_width / (float)m_height;
|
||||
glViewport(0, 0, m_width, m_height);
|
||||
m_renderer.update_proj_matrix(m_aspect);
|
||||
m_renderer.update_proj_matrix(m_aspect, m_width, m_height) ;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user