feat: grass (#11)
* feat: add grass texture and update grass_block texture * feat: add block data * feat: add blocks_tool * feat: add sync info and change function in blocks_tools * feat: add check and new function * refactor: make block texture loading data-driven * feat: add rendering for grass * feat: passable grass * feat: random grass place * fix: memory leak in TextureManager::load_cross_plane_texture
3
.gitignore
vendored
@@ -40,4 +40,5 @@ CMakeError.log
|
||||
*.swo
|
||||
*~
|
||||
.DS_Store
|
||||
assets/config.toml
|
||||
assets/config.toml
|
||||
.venv/
|
||||
1
.python-version
Normal file
@@ -0,0 +1 @@
|
||||
3.14
|
||||
@@ -122,6 +122,7 @@ add_executable(${PROJECT_NAME}
|
||||
src/gameplay/builders/snowy_plain_builder.cpp
|
||||
src/gameplay/river_worm.cpp
|
||||
src/gameplay/river_path.cpp
|
||||
src/block.cpp
|
||||
)
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
|
||||
6
assets/data/block/air.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 0
|
||||
is_cross_plane = false
|
||||
is_liquid = false
|
||||
is_passable = true
|
||||
is_transparent = true
|
||||
name = 'air'
|
||||
6
assets/data/block/dirt.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 2
|
||||
is_cross_plane = false
|
||||
is_liquid = false
|
||||
is_passable = false
|
||||
is_transparent = false
|
||||
name = 'dirt'
|
||||
6
assets/data/block/grass.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 9
|
||||
is_cross_plane = true
|
||||
is_liquid = false
|
||||
is_passable = true
|
||||
is_transparent = true
|
||||
name = 'grass'
|
||||
6
assets/data/block/grass_block.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 1
|
||||
is_cross_plane = false
|
||||
is_liquid = false
|
||||
is_passable = false
|
||||
is_transparent = false
|
||||
name = 'grass_block'
|
||||
6
assets/data/block/leaf.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 6
|
||||
is_cross_plane = false
|
||||
is_liquid = false
|
||||
is_passable = false
|
||||
is_transparent = true
|
||||
name = 'leaf'
|
||||
6
assets/data/block/log.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 5
|
||||
is_cross_plane = false
|
||||
is_liquid = false
|
||||
is_passable = false
|
||||
is_transparent = false
|
||||
name = 'log'
|
||||
6
assets/data/block/sand.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 4
|
||||
is_cross_plane = false
|
||||
is_liquid = false
|
||||
is_passable = false
|
||||
is_transparent = false
|
||||
name = 'sand'
|
||||
6
assets/data/block/snowy_grass_block.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 8
|
||||
is_cross_plane = false
|
||||
is_liquid = false
|
||||
is_passable = false
|
||||
is_transparent = false
|
||||
name = 'snowy_grass_block'
|
||||
6
assets/data/block/stone.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 3
|
||||
is_cross_plane = false
|
||||
is_liquid = false
|
||||
is_passable = false
|
||||
is_transparent = false
|
||||
name = 'stone'
|
||||
6
assets/data/block/template.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
name = "template"
|
||||
id = 0
|
||||
is_liquid = false
|
||||
is_passable = false
|
||||
is_cross_plane = false
|
||||
is_transparent = false
|
||||
6
assets/data/block/water.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
id = 7
|
||||
is_cross_plane = false
|
||||
is_liquid = true
|
||||
is_passable = false
|
||||
is_transparent = false
|
||||
name = 'water'
|
||||
@@ -8,7 +8,7 @@ layout (binding = 0) uniform sampler2DArray samp;
|
||||
|
||||
void main(void) {
|
||||
color = texture(samp, vec3(tc, tex_layer));
|
||||
if (color.a < 0.5) {
|
||||
if (color.a < 0.1) {
|
||||
discard;
|
||||
}
|
||||
//color = varyingColor;
|
||||
|
||||
BIN
assets/texture/block/grass/cross.png
Normal file
|
After Width: | Height: | Size: 277 B |
|
Before Width: | Height: | Size: 563 B After Width: | Height: | Size: 555 B |
|
Before Width: | Height: | Size: 563 B After Width: | Height: | Size: 555 B |
|
Before Width: | Height: | Size: 563 B After Width: | Height: | Size: 555 B |
|
Before Width: | Height: | Size: 563 B After Width: | Height: | Size: 555 B |
|
Before Width: | Height: | Size: 399 B After Width: | Height: | Size: 381 B |
BIN
assets/texture/item/block/grass.png
Normal file
|
After Width: | Height: | Size: 277 B |
@@ -8,7 +8,6 @@ constexpr int WORLD_SIZE_Y = 256;
|
||||
constexpr int CHUNK_SIZE = 16;
|
||||
constexpr int SEA_LEVEL = 64;
|
||||
|
||||
constexpr int MAX_BLOCK_NUM = 9;
|
||||
constexpr int MAX_UI_NUM = 1;
|
||||
constexpr int MAX_BLOCK_STATUS = 1;
|
||||
constexpr int MAX_BIOME_SUM = 4;
|
||||
@@ -17,7 +16,7 @@ constexpr int MAX_CHARACTER = 128;
|
||||
constexpr int PRE_LOAD_DISTANCE = 24;
|
||||
|
||||
constexpr int MAX_DISTANCE = 128;
|
||||
|
||||
constexpr int CROSS_PLANE_DISTANCE = 8;
|
||||
constexpr float DEFAULT_FOV = 70.0f;
|
||||
constexpr float DEFAULT_MAX_WALK_SPEED = 4.5f;
|
||||
constexpr float DEFAULT_MAX_RUN_SPEED = 7.0f;
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
#pragma once
|
||||
#include "Cubed/constants.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <string>
|
||||
@@ -40,16 +37,40 @@ struct LookBlock {
|
||||
glm::ivec3 normal;
|
||||
};
|
||||
|
||||
constexpr std::array<std::string_view, MAX_BLOCK_NUM> BLOCK_REISTER{
|
||||
"air", "grass_block", "dirt", "stone", "sand", "log", "leaf",
|
||||
"water", "snowy_grass_block"};
|
||||
struct BlockData {
|
||||
std::string name;
|
||||
BlockType id = 0;
|
||||
|
||||
const std::array<bool, MAX_BLOCK_NUM> TRANSPARENT_MAP{
|
||||
true, false, false, false, false, false, true, false, false};
|
||||
bool is_liquid = false;
|
||||
|
||||
inline bool is_in_transparent_map(unsigned id) {
|
||||
ASSERT_MSG(id < MAX_BLOCK_NUM, "ID is invaild");
|
||||
return TRANSPARENT_MAP[id];
|
||||
bool is_passable = false;
|
||||
bool is_cross_plane = false;
|
||||
bool is_transparent = false;
|
||||
BlockData(BlockType b_id, std::string_view b_name, bool liquid,
|
||||
bool passable, bool cross_plane, bool transparent)
|
||||
: name(b_name), id(b_id), is_liquid(liquid), is_passable(passable),
|
||||
is_cross_plane(cross_plane), is_transparent(transparent) {}
|
||||
};
|
||||
|
||||
class BlockManager {
|
||||
|
||||
public:
|
||||
static const std::vector<BlockData>& datas();
|
||||
static void init();
|
||||
static unsigned sums();
|
||||
static unsigned cross_plane_sum();
|
||||
static const std::string& name_form_id(BlockType id);
|
||||
|
||||
static bool is_cross_plane(BlockType id);
|
||||
static bool is_transparent(BlockType id);
|
||||
static bool is_passable(BlockType id);
|
||||
static BlockType cross_plane_index(BlockType id);
|
||||
|
||||
private:
|
||||
static void set_up_cross_plane_map();
|
||||
static inline std::vector<BlockData> m_datas;
|
||||
static inline bool is_init = false;
|
||||
static inline std::unordered_map<BlockType, BlockType> m_cross_plane_map;
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -12,5 +12,6 @@ public:
|
||||
|
||||
protected:
|
||||
void build_bottom();
|
||||
void place_grass();
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "Cubed/primitive_data.hpp"
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include <mutex>
|
||||
namespace Cubed {
|
||||
|
||||
class World;
|
||||
@@ -21,7 +21,8 @@ private:
|
||||
std::atomic<bool> m_dirty{false};
|
||||
std::atomic<bool> m_need_upload{true};
|
||||
std::atomic<bool> m_is_on_gen_vertex_data{false};
|
||||
std::atomic<size_t> m_vertex_sum = 0;
|
||||
std::atomic<size_t> m_normal_vertices_sum = 0;
|
||||
std::atomic<size_t> m_cross_vertices_sum = 0;
|
||||
std::atomic<BiomeType> m_biome = BiomeType::PLAIN;
|
||||
std::mutex m_vertexs_data_mutex;
|
||||
|
||||
@@ -32,9 +33,10 @@ private:
|
||||
HeightMapArray m_heightmap;
|
||||
// the index is a array of block id
|
||||
std::vector<BlockType> m_blocks;
|
||||
GLuint m_vbo = 0;
|
||||
std::vector<Vertex> m_vertexs_data;
|
||||
|
||||
GLuint m_normal_vbo = 0;
|
||||
GLuint m_cross_plane_vbo = 0;
|
||||
std::vector<Vertex> m_normal_vertices;
|
||||
std::vector<Vertex> m_cross_plane_vertices;
|
||||
float frequency = 0.01f;
|
||||
float height = 80;
|
||||
unsigned m_seed = 0;
|
||||
@@ -42,6 +44,9 @@ private:
|
||||
BiomeConditions m_conditions;
|
||||
|
||||
void clear_dirty();
|
||||
void gen_normal_vertices(
|
||||
const std::array<const std::vector<BlockType>*, 4>& neighbor_block);
|
||||
void gen_cross_plane_vertices();
|
||||
|
||||
public:
|
||||
Chunk(World& world, ChunkPos chunk_pos);
|
||||
@@ -93,8 +98,10 @@ public:
|
||||
const std::array<const std::vector<BlockType>*, 4>& neighbor_block);
|
||||
void upload_to_gpu();
|
||||
|
||||
GLuint get_vbo() const;
|
||||
size_t get_vertex_sum() const;
|
||||
GLuint get_normal_vbo() const;
|
||||
size_t get_normal_vertices_sum() const;
|
||||
GLuint get_cross_vbo() const;
|
||||
size_t get_cross_vertices_sum() const;
|
||||
|
||||
bool is_dirty() const;
|
||||
void mark_dirty();
|
||||
|
||||
@@ -15,14 +15,16 @@
|
||||
namespace Cubed {
|
||||
|
||||
struct ChunkRenderSnapshot {
|
||||
GLuint vbo;
|
||||
size_t vertex_count;
|
||||
GLuint normal_vbo;
|
||||
size_t normal_vertices_count;
|
||||
GLuint cross_vbo;
|
||||
size_t cross_vertices_count;
|
||||
glm::vec3 center;
|
||||
glm::vec3 half_extents;
|
||||
};
|
||||
|
||||
class Player;
|
||||
|
||||
class TextureManager;
|
||||
class World {
|
||||
private:
|
||||
using ChunkPtrUpdateList = std::vector<std::pair<ChunkPos, Chunk*>>;
|
||||
@@ -91,11 +93,13 @@ public:
|
||||
|
||||
int get_block(const glm::ivec3& block_pos) const;
|
||||
bool is_block(const glm::ivec3& block_pos) const;
|
||||
bool can_pass_block(const glm::ivec3& block_pos) const;
|
||||
|
||||
static ChunkPos chunk_pos(int world_x, int world_z);
|
||||
|
||||
void need_gen();
|
||||
void render(const glm::mat4& mvp_matrix);
|
||||
void render(const glm::mat4& mvp_matrix,
|
||||
const TextureManager& texture_manager);
|
||||
|
||||
void set_block(const glm::ivec3& pos, unsigned id);
|
||||
void update(float delta_time);
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
// #include <string>
|
||||
// #include <unordered_map>
|
||||
// #include <vector>
|
||||
namespace Cubed {
|
||||
|
||||
class MapTable {
|
||||
private:
|
||||
static inline std::unordered_map<unsigned, std::string> id_to_name_map;
|
||||
static inline std::unordered_map<size_t, unsigned> name_to_id_map;
|
||||
static inline std::vector<std::string> item_id_to_name;
|
||||
|
||||
/*
|
||||
static inline std::unordered_map<unsigned, std::string> id_to_name_map;
|
||||
static inline std::unordered_map<size_t, unsigned> name_to_id_map;
|
||||
static inline std::vector<std::string> item_id_to_name;
|
||||
*/
|
||||
public:
|
||||
// please using reference
|
||||
/*
|
||||
static std::string_view get_name_from_id(unsigned id);
|
||||
static unsigned get_id_from_name(const std::string& name);
|
||||
|
||||
static std::string_view item_name(unsigned id);
|
||||
static const std::vector<std::string>& item_map();
|
||||
*/
|
||||
static void init_map();
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
#pragma region NORMAL_BLOCK
|
||||
constexpr float VERTICES_POS[6][6][3] = {
|
||||
// ===== front (z = +1) =====
|
||||
{{0.0f, 0.0f, 1.0f}, // bottom left
|
||||
@@ -91,7 +91,7 @@ constexpr float TEX_COORDS[6][6][2] = {
|
||||
{0.0f, 1.0f}, // back left
|
||||
{0.0f, 0.0f}} // front left
|
||||
};
|
||||
|
||||
#pragma endregion
|
||||
constexpr float CUBE_VER[24] = {0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0,
|
||||
0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0,
|
||||
0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0};
|
||||
@@ -112,7 +112,43 @@ 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},
|
||||
};
|
||||
#pragma region CROSS_PLANE
|
||||
constexpr float CROSS_VERTICES_POS[2][6][3] = {
|
||||
// ===== Plane 1: bottom-front-left to top-back-right =====
|
||||
{{0.0f, 0.0f, 0.0f}, // bottom front left
|
||||
{0.0f, 1.0f, 0.0f}, // top front left
|
||||
{1.0f, 1.0f, 1.0f}, // top back right
|
||||
{1.0f, 1.0f, 1.0f}, // top back right
|
||||
{1.0f, 0.0f, 1.0f}, // bottom back right
|
||||
{0.0f, 0.0f, 0.0f}}, // bottom front left
|
||||
|
||||
// ===== Plane 2: bottom-front-right to top-back-left =====
|
||||
{{1.0f, 0.0f, 0.0f}, // bottom front right
|
||||
{1.0f, 1.0f, 0.0f}, // top front right
|
||||
{0.0f, 1.0f, 1.0f}, // top back left
|
||||
{0.0f, 1.0f, 1.0f}, // top back left
|
||||
{0.0f, 0.0f, 1.0f}, // bottom back left
|
||||
{1.0f, 0.0f, 0.0f}}, // bottom front right
|
||||
};
|
||||
|
||||
constexpr float CROSS_TEX_COORDS[2][6][2] = {
|
||||
// ===== Plane 1: bottom-front-left to top-back-right =====
|
||||
{{0.0f, 1.0f}, // bottom left
|
||||
{0.0f, 0.0f}, // top left
|
||||
{1.0f, 0.0f}, // top right
|
||||
{1.0f, 0.0f}, // top right
|
||||
{1.0f, 1.0f}, // bottom right
|
||||
{0.0f, 1.0f}}, // bottom left
|
||||
|
||||
// ===== Plane 2: bottom-front-right to top-back-left =====
|
||||
{{0.0f, 1.0f}, // bottom left
|
||||
{0.0f, 0.0f}, // top left
|
||||
{1.0f, 0.0f}, // top right
|
||||
{1.0f, 0.0f}, // top right
|
||||
{1.0f, 1.0f}, // bottom right
|
||||
{0.0f, 1.0f}}, // bottom left
|
||||
};
|
||||
#pragma endregion
|
||||
struct Vertex {
|
||||
float x = 0.0f, y = 0.0f, z = 0.0f;
|
||||
float s = 0.0f, t = 0.0f;
|
||||
|
||||
@@ -8,16 +8,19 @@ namespace Cubed {
|
||||
class TextureManager {
|
||||
private:
|
||||
bool m_need_reload = false;
|
||||
GLuint m_block_status_array;
|
||||
GLuint m_texture_array;
|
||||
GLuint m_ui_array;
|
||||
GLuint m_block_status_array = 0;
|
||||
GLuint m_texture_array = 0;
|
||||
GLuint m_cross_plane_array = 0;
|
||||
GLuint m_ui_array = 0;
|
||||
GLfloat m_max_aniso = 0.0f;
|
||||
int m_aniso = 1;
|
||||
|
||||
std::vector<GLuint> m_item_textures;
|
||||
|
||||
void load_block_status(unsigned status_id);
|
||||
void load_block_texture(unsigned block_id);
|
||||
void load_item_texture(const std::string& name);
|
||||
void load_block_item_texture(unsigned id);
|
||||
void load_cross_plane_texture(unsigned id);
|
||||
void load_ui_texture(unsigned id);
|
||||
void init_item();
|
||||
void init_block();
|
||||
@@ -31,6 +34,7 @@ public:
|
||||
void delet_texture();
|
||||
GLuint get_block_status_array() const;
|
||||
GLuint get_texture_array() const;
|
||||
GLuint get_cross_plane_array() const;
|
||||
GLuint get_ui_array() const;
|
||||
const std::vector<GLuint>& item_textures() const;
|
||||
// Must call after MapTable::init_map() and glfwMakeContextCurrent(window);
|
||||
|
||||
15
pyproject.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[project]
|
||||
name = "cubed"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.14"
|
||||
dependencies = [
|
||||
"loguru>=0.7.3",
|
||||
"pytomlpp>=1.1.0",
|
||||
]
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
"ruff>=0.15.14",
|
||||
]
|
||||
403
scripts/blocks_tool.py
Normal file
@@ -0,0 +1,403 @@
|
||||
import argparse
|
||||
import copy
|
||||
import sys
|
||||
from functools import singledispatch
|
||||
from pathlib import Path
|
||||
from pprint import pprint
|
||||
from typing import Any
|
||||
|
||||
import pytomlpp
|
||||
from loguru import logger
|
||||
|
||||
VERSION = "0.0.1"
|
||||
DATA_PATH = "assets/data/block"
|
||||
TEXTURE_PATH = "assets/texture/block"
|
||||
|
||||
work_path = Path(__file__).parent.parent
|
||||
data_path = work_path / DATA_PATH
|
||||
texture_path = work_path / TEXTURE_PATH
|
||||
|
||||
|
||||
def collect_blocks() -> list[dict[str, Any]]:
|
||||
blocks: list[dict[str, Any]] = []
|
||||
for block in data_path.rglob("*.toml"):
|
||||
if not block.is_file():
|
||||
continue
|
||||
if block.name == "template.toml":
|
||||
continue
|
||||
|
||||
blocks.append(pytomlpp.loads(block.read_text(encoding="utf-8")))
|
||||
blocks.sort(key=lambda x: x["id"])
|
||||
return blocks
|
||||
|
||||
|
||||
def save_data(blocks: list[dict[str, Any]]):
|
||||
for block in blocks:
|
||||
block_path: Path = data_path / (block["name"] + ".toml")
|
||||
if not block_path.is_file():
|
||||
logger.warning(
|
||||
f"Block: {block_path} is not Exists and Will Create A New One"
|
||||
)
|
||||
block_path.write_text(pytomlpp.dumps(block))
|
||||
|
||||
|
||||
def sync_template_value():
|
||||
blocks = collect_blocks()
|
||||
template_path = data_path / "template.toml"
|
||||
if not template_path.is_file():
|
||||
logger.error("Template.toml is not Exists!")
|
||||
return
|
||||
template_block = pytomlpp.loads(template_path.read_text(encoding="utf-8"))
|
||||
add_count = 0
|
||||
for key, value in template_block.items():
|
||||
for block in blocks:
|
||||
if key not in block:
|
||||
block[key] = value
|
||||
add_count += 1
|
||||
save_data(blocks)
|
||||
logger.info(f"Synced {add_count} template fields to blocks")
|
||||
|
||||
|
||||
@singledispatch
|
||||
def show_data_info(arg: Any):
|
||||
logger.error("No Match show_data_info")
|
||||
|
||||
|
||||
@show_data_info.register(type(None))
|
||||
def _(arg: None):
|
||||
blocks = collect_blocks()
|
||||
print("Please Input Block Name or Id, Input exit or e to Exit")
|
||||
while True:
|
||||
input_str = input("Name or Id: ")
|
||||
try:
|
||||
id = int(input_str)
|
||||
if id >= len(blocks) or id < 0:
|
||||
print(f"Id: {id} Not Find, Input e or exit to Exit")
|
||||
continue
|
||||
pprint(blocks[id])
|
||||
except ValueError:
|
||||
if input_str.lower() == "exit" or input_str.lower() == "e":
|
||||
break
|
||||
find = False
|
||||
for block in blocks:
|
||||
if block["name"] == input_str:
|
||||
pprint(block)
|
||||
find = True
|
||||
break
|
||||
if not find:
|
||||
print(f"Name: {input_str} Not Find, Input e or exit to Exit")
|
||||
|
||||
|
||||
@show_data_info.register(int)
|
||||
def _(id: int):
|
||||
blocks = collect_blocks()
|
||||
if id >= len(blocks) or id < 0:
|
||||
logger.error(f"ID: {id} is Not Invaild!")
|
||||
return
|
||||
pprint(blocks[id])
|
||||
|
||||
|
||||
@show_data_info.register(str)
|
||||
def _(name: str):
|
||||
blocks = collect_blocks()
|
||||
find = False
|
||||
for block in blocks:
|
||||
if block["name"] == name:
|
||||
pprint(block)
|
||||
find = True
|
||||
break
|
||||
if not find:
|
||||
logger.error(f"Block Name: {name} Not Find")
|
||||
|
||||
|
||||
def change_key(block: dict[str, Any], key: str, value: str):
|
||||
if type(block[key]) is str:
|
||||
block[key] = value
|
||||
elif type(block[key]) is int:
|
||||
try:
|
||||
v = int(value)
|
||||
block[key] = v
|
||||
except ValueError:
|
||||
logger.error("The Value Is Not A Int")
|
||||
return False
|
||||
elif type(block[key]) is bool:
|
||||
if value.lower() == "true" or value.lower() == "t":
|
||||
block[key] = True
|
||||
elif value.lower() == "false" or value.lower() == "f":
|
||||
block[key] = False
|
||||
else:
|
||||
logger.error("The Value Is Not A Bool")
|
||||
return False
|
||||
elif type(block[key]) is float:
|
||||
try:
|
||||
v = float(value)
|
||||
block[key] = v
|
||||
except ValueError:
|
||||
logger.error("The Value Is Not A Float")
|
||||
return False
|
||||
else:
|
||||
logger.error("Unkown Key Type")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def handle_change(block: dict[str, Any]) -> dict[str, Any]:
|
||||
print("Please Input Block Key, Input exit or e to Exit")
|
||||
while True:
|
||||
key = input("Key: ")
|
||||
if key.lower() == "exit" or key.lower() == "e":
|
||||
break
|
||||
if key not in block:
|
||||
logger.error("The Key Is Not Exists!")
|
||||
continue
|
||||
value = input("Value: ")
|
||||
old_name = block[key]
|
||||
if change_key(block, key, value):
|
||||
print("Change Success")
|
||||
if key == "name":
|
||||
old_path: Path = data_path / (old_name + ".toml")
|
||||
try:
|
||||
old_path.unlink()
|
||||
except FileNotFoundError:
|
||||
logger.warning(
|
||||
f"Name Change But Old File {old_name}.toml is Not Exists!"
|
||||
)
|
||||
else:
|
||||
print("Change Fail")
|
||||
pprint(block)
|
||||
return block
|
||||
|
||||
|
||||
@singledispatch
|
||||
def change_data(arg: Any):
|
||||
logger.error("Not Match change")
|
||||
|
||||
|
||||
@change_data.register(int)
|
||||
def _(id: int):
|
||||
blocks = collect_blocks()
|
||||
if id >= len(blocks) or id < 0:
|
||||
logger.error(f"ID: {id} is Invaild!")
|
||||
return
|
||||
pprint(blocks[id])
|
||||
blocks[id] = handle_change(blocks[id])
|
||||
save_data(blocks)
|
||||
|
||||
|
||||
@change_data.register(str)
|
||||
def _(name: str):
|
||||
blocks = collect_blocks()
|
||||
find = False
|
||||
for i, block in enumerate(blocks):
|
||||
if block["name"] == name:
|
||||
pprint(block)
|
||||
blocks[i] = handle_change(block)
|
||||
save_data(blocks)
|
||||
find = True
|
||||
break
|
||||
if not find:
|
||||
logger.error(f"Block Name: {name} Not Find")
|
||||
|
||||
|
||||
@change_data.register(type(None))
|
||||
def _(arg: None):
|
||||
blocks = collect_blocks()
|
||||
print("Please Input Block Name or Id, Input exit or e to Exit")
|
||||
while True:
|
||||
input_str = input("Name or Id: ")
|
||||
try:
|
||||
id = int(input_str)
|
||||
if id >= len(blocks) or id < 0:
|
||||
print(f"Id: {id} Not Find, Input e or exit to Exit")
|
||||
continue
|
||||
pprint(blocks[id])
|
||||
blocks[id] = handle_change(blocks[id])
|
||||
save_data(blocks)
|
||||
except ValueError:
|
||||
if input_str.lower() == "exit" or input_str.lower() == "e":
|
||||
break
|
||||
find = False
|
||||
for i, block in enumerate(blocks):
|
||||
if block["name"] == input_str:
|
||||
pprint(block)
|
||||
blocks[i] = handle_change(block)
|
||||
save_data(blocks)
|
||||
find = True
|
||||
break
|
||||
if not find:
|
||||
print(f"Name: {input_str} Not Find, Input e or exit to Exit")
|
||||
|
||||
|
||||
def show_data_list():
|
||||
blocks = collect_blocks()
|
||||
for block in blocks:
|
||||
print(f"id: {block['id']} name: {block['name']}")
|
||||
|
||||
|
||||
def check_path():
|
||||
|
||||
logger.info(f"Work Path {work_path.resolve()}")
|
||||
logger.info(f"Script Dir {sys.path[0]}")
|
||||
data_exists = True
|
||||
if not data_path.exists():
|
||||
logger.error(f"Blocks Data Path {data_path} not Exists!")
|
||||
data_exists = False
|
||||
else:
|
||||
logger.info(f"Blocks Data Path {data_path}")
|
||||
texture_exists = True
|
||||
if not texture_path.exists():
|
||||
logger.error(f"Blocks Texture Path {texture_path} not Exists!")
|
||||
texture_exists = False
|
||||
else:
|
||||
logger.info(f"Blocks Texture Path {texture_path}")
|
||||
return data_exists and texture_exists
|
||||
|
||||
|
||||
def check_integrity():
|
||||
find_error = False
|
||||
errors = 0
|
||||
if check_path():
|
||||
blocks = collect_blocks()
|
||||
template_path = data_path / "template.toml"
|
||||
if not template_path.is_file():
|
||||
logger.error("Template.toml is not Exists!")
|
||||
find_error = True
|
||||
errors += 1
|
||||
return
|
||||
template_block = pytomlpp.loads(template_path.read_text(encoding="utf-8"))
|
||||
n = len(blocks)
|
||||
for i in range(n):
|
||||
if "id" not in blocks[i]:
|
||||
logger.error(f"Id: {i} not Exists!")
|
||||
find_error = True
|
||||
errors += 1
|
||||
continue
|
||||
if blocks[i]["id"] != i:
|
||||
logger.error(
|
||||
f"Id Error, Block {blocks[i].get('name', 'Unknow')} Id Should Be {i} Instead of {blocks[i]['id']}"
|
||||
)
|
||||
find_error = True
|
||||
errors += 1
|
||||
for key, value in template_block.items():
|
||||
if key not in blocks[i]:
|
||||
logger.error(
|
||||
f"Key Error, Block {blocks[i].get('name', 'Unknow')} Key {key} not Exists!"
|
||||
)
|
||||
find_error = True
|
||||
errors += 1
|
||||
continue
|
||||
if type(blocks[i][key]) is not type(value):
|
||||
logger.error(
|
||||
f"Value Type Error, Block {blocks[i].get(key, 'Unknow')} The Type Should Be {type(value)}, Instead of {type(blocks[i][key])}"
|
||||
)
|
||||
find_error = True
|
||||
errors += 1
|
||||
if find_error:
|
||||
logger.error(f"Find {errors} Errors")
|
||||
else:
|
||||
logger.info("No Error")
|
||||
|
||||
|
||||
def add_new_block():
|
||||
blocks = collect_blocks()
|
||||
template_path = data_path / "template.toml"
|
||||
if not template_path.is_file():
|
||||
logger.error("Template.toml is not Exists, Can't Create A New Block!")
|
||||
return
|
||||
template_block = pytomlpp.loads(template_path.read_text(encoding="utf-8"))
|
||||
new_block = copy.deepcopy(template_block)
|
||||
num = len(blocks)
|
||||
logger.info(f"New Block Id is {num}")
|
||||
new_block["id"] = num
|
||||
for key in template_block:
|
||||
if key == "id":
|
||||
continue
|
||||
nvalue = input(f"Input {key}: ")
|
||||
if not change_key(new_block, key, nvalue):
|
||||
logger.error(f"Add Key {key} Value {nvalue} Fail")
|
||||
return
|
||||
new_block_path: Path = data_path / (new_block["name"] + ".toml")
|
||||
new_block_path.write_text(pytomlpp.dumps(new_block))
|
||||
logger.info("Successfully Add New Block!")
|
||||
pprint(new_block)
|
||||
|
||||
|
||||
def handle_args(args: argparse.Namespace):
|
||||
if args.version:
|
||||
print(f"Blocks Tools: {VERSION}")
|
||||
print(f"Python: {sys.version}")
|
||||
if args.path:
|
||||
check_path()
|
||||
if args.list:
|
||||
show_data_list()
|
||||
if args.sync:
|
||||
sync_template_value()
|
||||
if args.info:
|
||||
if args.info == "EMPTY":
|
||||
show_data_info(None)
|
||||
else:
|
||||
try:
|
||||
id = int(args.info)
|
||||
show_data_info(id)
|
||||
except ValueError:
|
||||
show_data_info(args.info)
|
||||
if args.change:
|
||||
if args.change == "EMPTY":
|
||||
change_data(None)
|
||||
else:
|
||||
try:
|
||||
id = int(args.change)
|
||||
change_data(id)
|
||||
except ValueError:
|
||||
change_data(args.change)
|
||||
if args.check:
|
||||
check_integrity()
|
||||
if args.new:
|
||||
add_new_block()
|
||||
|
||||
|
||||
def init_parser(parser: argparse.ArgumentParser):
|
||||
parser.add_argument(
|
||||
"-v", "--version", action="store_true", help="Show Blocks Tools Version"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--path", action="store_true", help="Check Blcoks Data and Texture Path"
|
||||
)
|
||||
parser.add_argument("-l", "--list", action="store_true", help="Show Blocks List")
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--sync",
|
||||
action="store_true",
|
||||
help="Sync Template.toml Value to Other Toml, Only New Value Will Add",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-i",
|
||||
"--info",
|
||||
nargs="?",
|
||||
const="EMPTY",
|
||||
help="Show Block Data, If Provide Id Will Print the Corresponding Blcok Data, You Can Input Id or Name",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c", "--change", nargs="?", const="EMPTY", help="Change Block Data"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-C", "--check", action="store_true", help="Check The Block Data Integrity"
|
||||
)
|
||||
parser.add_argument("-n", "--new", action="store_true", help="Add A New Block")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Block Manage Tool")
|
||||
|
||||
init_parser(parser)
|
||||
|
||||
if len(sys.argv) == 1:
|
||||
parser.print_help()
|
||||
exit(0)
|
||||
|
||||
args = parser.parse_args()
|
||||
handle_args(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -51,7 +51,7 @@ void App::init() {
|
||||
cursor_enter_callback);
|
||||
glfwSetCharCallback(m_window.get_glfw_window(), char_callback);
|
||||
ChunkGenerator::init();
|
||||
|
||||
BlockManager::init();
|
||||
m_renderer.init();
|
||||
Logger::info("Renderer Init Success");
|
||||
m_window.update_viewport();
|
||||
|
||||
144
src/block.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
#include "Cubed/gameplay/block.hpp"
|
||||
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <toml++/toml.hpp>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
namespace {
|
||||
std::string block_data_dir = ASSETS_PATH + "data/block"s;
|
||||
|
||||
template <Cubed::TomlValueType T>
|
||||
std::optional<T> safe_get_value(const toml::table& table, std::string_view key,
|
||||
const T& default_value) {
|
||||
auto value = table[key].value<T>();
|
||||
if (value == std::nullopt) {
|
||||
Cubed::Logger::warn("Key {} Is Not Find, Wiil Set the Default Value {}",
|
||||
key, default_value);
|
||||
value = default_value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
const std::vector<BlockData>& BlockManager::datas() {
|
||||
ASSERT(is_init);
|
||||
return m_datas;
|
||||
}
|
||||
|
||||
unsigned BlockManager::sums() {
|
||||
ASSERT(is_init);
|
||||
return m_datas.size();
|
||||
}
|
||||
unsigned BlockManager::cross_plane_sum() {
|
||||
ASSERT(is_init);
|
||||
return m_cross_plane_map.size();
|
||||
}
|
||||
|
||||
const std::string& BlockManager::name_form_id(BlockType id) {
|
||||
if (id >= sums()) {
|
||||
Logger::error("Id {}, is Over The Max Id", id, sums() - 1);
|
||||
return m_datas[0].name;
|
||||
}
|
||||
return m_datas[id].name;
|
||||
}
|
||||
|
||||
bool BlockManager::is_cross_plane(BlockType id) {
|
||||
if (id >= sums()) {
|
||||
Logger::error("Id {}, is Over The Max Id", id, sums() - 1);
|
||||
return m_datas[0].is_cross_plane;
|
||||
}
|
||||
return m_datas[id].is_cross_plane;
|
||||
}
|
||||
|
||||
bool BlockManager::is_transparent(BlockType id) {
|
||||
if (id >= sums()) {
|
||||
Logger::error("Id {}, is Over The Max Id", id, sums() - 1);
|
||||
return m_datas[0].is_transparent;
|
||||
}
|
||||
return m_datas[id].is_transparent;
|
||||
}
|
||||
bool BlockManager::is_passable(BlockType id) {
|
||||
if (id >= sums()) {
|
||||
Logger::error("Id {}, is Over The Max Id", id, sums() - 1);
|
||||
return m_datas[0].is_passable;
|
||||
}
|
||||
return m_datas[id].is_passable;
|
||||
}
|
||||
|
||||
void BlockManager::init() {
|
||||
fs::path data_path{block_data_dir};
|
||||
|
||||
for (auto entry : fs::recursive_directory_iterator(data_path)) {
|
||||
if (!entry.is_regular_file()) {
|
||||
continue;
|
||||
}
|
||||
if (entry.path().filename() == "template.toml") {
|
||||
continue;
|
||||
}
|
||||
toml::table block;
|
||||
try {
|
||||
block = toml::parse_file(entry.path().c_str());
|
||||
} catch (const toml::parse_error& err) {
|
||||
Logger::error("Load Block Data {} Fail, Parser Error {}",
|
||||
entry.path().c_str(), err.what());
|
||||
ASSERT(false);
|
||||
}
|
||||
auto id = block["id"].value<int>();
|
||||
if (id == std::nullopt) {
|
||||
Logger::error("Very Serious Error, Block Id Not Find !!!, Please "
|
||||
"Check The Block Data Integrity");
|
||||
std::abort();
|
||||
}
|
||||
auto name = block["name"].value<std::string>();
|
||||
if (name == std::nullopt) {
|
||||
Logger::error("Very Serious Error, Block Name Not Find !!!, Please "
|
||||
"Check The Block Data Integrity");
|
||||
std::abort();
|
||||
}
|
||||
auto is_liquid = safe_get_value(block, "is_liquid", false);
|
||||
auto is_passable = safe_get_value(block, "is_passable", false);
|
||||
auto is_cross_plane = safe_get_value(block, "is_cross_plane", false);
|
||||
auto is_transparent = safe_get_value(block, "is_transparent", false);
|
||||
m_datas.emplace_back(*id, *name, *is_liquid, *is_passable,
|
||||
*is_cross_plane, *is_transparent);
|
||||
}
|
||||
std::sort(
|
||||
m_datas.begin(), m_datas.end(),
|
||||
[](const BlockData& a, const BlockData& b) { return a.id < b.id; });
|
||||
|
||||
set_up_cross_plane_map();
|
||||
is_init = true;
|
||||
}
|
||||
|
||||
BlockType BlockManager::cross_plane_index(BlockType id) {
|
||||
auto it = m_cross_plane_map.find(id);
|
||||
if (it == m_cross_plane_map.end()) {
|
||||
Logger::error("Can't Find Cross Plane Id {}", id);
|
||||
ASSERT(false);
|
||||
throw std::out_of_range{"Can't Find Cross Plane Id" +
|
||||
std::to_string(id)};
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void BlockManager::set_up_cross_plane_map() {
|
||||
unsigned cur_id = 0;
|
||||
for (const auto& data : m_datas) {
|
||||
if (data.is_cross_plane) {
|
||||
m_cross_plane_map[data.id] = cur_id;
|
||||
cur_id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "Cubed/app.hpp"
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/map_table.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
#include <imgui.h>
|
||||
@@ -551,7 +550,7 @@ void DevPanel::show_player_tab_item() {
|
||||
|
||||
void DevPanel::show_items_tab_item() {
|
||||
auto& textures = m_app.texture_manager().item_textures();
|
||||
auto& names = MapTable::item_map();
|
||||
// auto& names = MapTable::item_map();
|
||||
if (ImGui::BeginTabItem("item")) {
|
||||
ImGui::Text("Place Block ");
|
||||
ImGui::SameLine();
|
||||
@@ -567,7 +566,7 @@ void DevPanel::show_items_tab_item() {
|
||||
}
|
||||
if (ImGui::IsItemHovered()) {
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::Text("%s", names[i].c_str());
|
||||
ImGui::Text("%s", BlockManager::name_form_id(i).c_str());
|
||||
ImGui::EndTooltip();
|
||||
}
|
||||
if (i % 10 != 0) {
|
||||
|
||||
@@ -15,5 +15,28 @@ void BiomeBuilder::build_bottom() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BiomeBuilder::place_grass() {
|
||||
ChunkGenerator& chunk_generator = get_chunk_generator();
|
||||
Chunk& chunk = chunk_generator.chunk();
|
||||
auto& blocks = chunk.blocks();
|
||||
const auto& heightmap = chunk.get_heightmap();
|
||||
auto& random = chunk_generator.random();
|
||||
for (int x = 0; x < SIZE_X; ++x) {
|
||||
for (int z = 0; z < SIZE_Z; ++z) {
|
||||
int y = heightmap[x][z];
|
||||
BlockType top_id = blocks[Chunk::index(x, y, z)];
|
||||
if (top_id != 1) {
|
||||
continue;
|
||||
}
|
||||
if (blocks[Chunk::index(x, y + 1, z)] != 0) {
|
||||
continue;
|
||||
}
|
||||
if (random.random_bool(0.2)) {
|
||||
if (y + 1 < SIZE_Y) {
|
||||
blocks[Chunk::index(x, y + 1, z)] = 9;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace Cubed
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "Cubed/gameplay/chunk_generator.hpp"
|
||||
#include "Cubed/gameplay/tree.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
|
||||
namespace Cubed {
|
||||
@@ -52,6 +53,7 @@ void ForestBuilder::build_vegetation() {
|
||||
}
|
||||
}
|
||||
}
|
||||
place_grass();
|
||||
}
|
||||
|
||||
ChunkGenerator& ForestBuilder::get_chunk_generator() {
|
||||
|
||||
@@ -29,7 +29,7 @@ void PlainBuilder::build_blocks() {
|
||||
}
|
||||
}
|
||||
|
||||
void PlainBuilder::build_vegetation() {}
|
||||
void PlainBuilder::build_vegetation() { place_grass(); }
|
||||
|
||||
ChunkGenerator& PlainBuilder::get_chunk_generator() {
|
||||
return m_chunk_generator;
|
||||
|
||||
@@ -12,38 +12,49 @@ Chunk::Chunk(World& world, ChunkPos chunk_pos)
|
||||
: m_chunk_pos(chunk_pos), m_world(world) {}
|
||||
|
||||
Chunk::~Chunk() {
|
||||
if (m_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_vbo);
|
||||
if (m_normal_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_normal_vbo);
|
||||
}
|
||||
if (m_cross_plane_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_cross_plane_vbo);
|
||||
}
|
||||
}
|
||||
|
||||
Chunk::Chunk(Chunk&& other) noexcept
|
||||
: m_dirty(other.is_dirty()), m_need_upload(other.m_need_upload.load()),
|
||||
m_is_on_gen_vertex_data(other.m_is_on_gen_vertex_data.load()),
|
||||
m_vertex_sum(other.m_vertex_sum.load()), m_biome(other.m_biome.load()),
|
||||
m_chunk_pos(std::move(other.m_chunk_pos)), m_world(other.m_world),
|
||||
m_heightmap(std::move(other.m_heightmap)),
|
||||
m_blocks(std::move(other.m_blocks)), m_vbo(other.m_vbo),
|
||||
m_vertexs_data(std::move(other.m_vertexs_data)), m_seed(other.m_seed),
|
||||
m_conditions(other.m_conditions) {
|
||||
other.m_vbo = 0;
|
||||
m_normal_vertices_sum(other.m_normal_vertices_sum.load()),
|
||||
m_cross_vertices_sum(other.m_cross_vertices_sum.load()),
|
||||
m_biome(other.m_biome.load()), m_chunk_pos(std::move(other.m_chunk_pos)),
|
||||
m_world(other.m_world), m_heightmap(std::move(other.m_heightmap)),
|
||||
m_blocks(std::move(other.m_blocks)), m_normal_vbo(other.m_normal_vbo),
|
||||
m_cross_plane_vbo(other.m_cross_plane_vbo),
|
||||
m_normal_vertices(std::move(other.m_normal_vertices)),
|
||||
m_cross_plane_vertices(std::move(other.m_cross_plane_vertices)),
|
||||
m_seed(other.m_seed), m_conditions(other.m_conditions) {
|
||||
other.m_normal_vbo = 0;
|
||||
other.m_cross_plane_vbo = 0;
|
||||
}
|
||||
|
||||
Chunk& Chunk::operator=(Chunk&& other) noexcept {
|
||||
// Logger::info("other Chunk pos {} {} in Chunk& Chunk::operator=(Chunk&&
|
||||
// other) this {}", other.m_chunk_pos.x, other.m_chunk_pos.z,
|
||||
// static_cast<const void*>(&other));
|
||||
m_vbo = other.m_vbo;
|
||||
other.m_vbo = 0;
|
||||
m_normal_vbo = other.m_normal_vbo;
|
||||
other.m_normal_vbo = 0;
|
||||
m_cross_plane_vbo = other.m_cross_plane_vbo;
|
||||
other.m_cross_plane_vbo = 0;
|
||||
m_chunk_pos = std::move(other.m_chunk_pos);
|
||||
m_heightmap = std::move(other.m_heightmap);
|
||||
m_blocks = std::move(other.m_blocks);
|
||||
m_dirty = other.is_dirty();
|
||||
m_vertexs_data = std::move(other.m_vertexs_data);
|
||||
m_normal_vertices = std::move(other.m_normal_vertices);
|
||||
m_cross_plane_vertices = std::move(other.m_cross_plane_vertices);
|
||||
m_biome = other.m_biome.load();
|
||||
m_is_on_gen_vertex_data = other.m_is_on_gen_vertex_data.load();
|
||||
m_need_upload = other.m_need_upload.load();
|
||||
m_vertex_sum = other.m_vertex_sum.load();
|
||||
m_normal_vertices_sum = other.m_normal_vertices_sum.load();
|
||||
m_cross_vertices_sum = other.m_cross_vertices_sum.load();
|
||||
m_seed = other.m_seed;
|
||||
m_conditions = other.m_conditions;
|
||||
return *this;
|
||||
@@ -115,129 +126,24 @@ void Chunk::gen_vertex_data(
|
||||
}
|
||||
m_is_on_gen_vertex_data = true;
|
||||
std::lock_guard lk(m_vertexs_data_mutex);
|
||||
m_vertexs_data.clear();
|
||||
|
||||
static const glm::ivec3 DIR[6] = {{0, 0, 1}, {1, 0, 0}, {0, 0, -1},
|
||||
{-1, 0, 0}, {0, 1, 0}, {0, -1, 0}};
|
||||
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
for (int y = 0; y < SIZE_Y; y++) {
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
int world_x = x + m_chunk_pos.x * CHUNK_SIZE;
|
||||
int world_z = z + m_chunk_pos.z * CHUNK_SIZE;
|
||||
int world_y = y;
|
||||
int cur_id = m_blocks[index(x, y, z)];
|
||||
// air
|
||||
if (cur_id == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int face = 0; face < 6; face++) {
|
||||
int nx = x + DIR[face].x;
|
||||
int ny = y + DIR[face].y;
|
||||
int nz = z + DIR[face].z;
|
||||
bool neighbor_cull = false;
|
||||
|
||||
if (nx < 0 || nx >= SIZE_X || ny < 0 || ny >= SIZE_Y ||
|
||||
nz < 0 || nz >= SIZE_Z) {
|
||||
|
||||
int world_nx = world_x + DIR[face].x;
|
||||
int world_ny = world_y + DIR[face].y;
|
||||
int world_nz = world_z + DIR[face].z;
|
||||
|
||||
auto [neighbor_x, neighbor_z] =
|
||||
World::chunk_pos(world_nx, world_nz);
|
||||
|
||||
auto is_cull =
|
||||
[&](const std::vector<BlockType>* chunk_blocks) {
|
||||
if (chunk_blocks == nullptr) {
|
||||
return true;
|
||||
}
|
||||
int x, y, z;
|
||||
y = world_ny;
|
||||
x = world_nx - neighbor_x * CHUNK_SIZE;
|
||||
z = world_nz - neighbor_z * CHUNK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 ||
|
||||
x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||
z >= CHUNK_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int idx = Chunk::index(x, y, z);
|
||||
// not init
|
||||
if (static_cast<size_t>(idx) >=
|
||||
chunk_blocks->size()) {
|
||||
// Logger::warn("not init");
|
||||
return true;
|
||||
}
|
||||
auto id = (*chunk_blocks)[idx];
|
||||
if (is_in_transparent_map(id)) {
|
||||
if (id == cur_id) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
if (m_chunk_pos.x + 1 == neighbor_x) {
|
||||
neighbor_cull = is_cull(neighbor_block[0]);
|
||||
} else if (m_chunk_pos.x - 1 == neighbor_x) {
|
||||
neighbor_cull = is_cull(neighbor_block[1]);
|
||||
} else if (m_chunk_pos.z + 1 == neighbor_z) {
|
||||
neighbor_cull = is_cull(neighbor_block[2]);
|
||||
} else if (m_chunk_pos.z - 1 == neighbor_z) {
|
||||
neighbor_cull = is_cull(neighbor_block[3]);
|
||||
}
|
||||
// neighbor_cull = m_world.is_block(glm::ivec3(world_x,
|
||||
// world_y, world_z) + DIR[face]);
|
||||
} else {
|
||||
auto id = m_blocks[index(nx, ny, nz)];
|
||||
if (!is_in_transparent_map(id)) {
|
||||
neighbor_cull = true;
|
||||
} else {
|
||||
if (id == cur_id) {
|
||||
neighbor_cull = true;
|
||||
} else {
|
||||
neighbor_cull = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (neighbor_cull) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Vertex vex = {
|
||||
VERTICES_POS[face][i][0] + (float)world_x * 1.0f,
|
||||
VERTICES_POS[face][i][1] + (float)world_y * 1.0f,
|
||||
VERTICES_POS[face][i][2] + (float)world_z * 1.0f,
|
||||
TEX_COORDS[face][i][0],
|
||||
TEX_COORDS[face][i][1],
|
||||
static_cast<float>(cur_id * 6 + face)
|
||||
|
||||
};
|
||||
m_vertexs_data.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_vertex_sum = m_vertexs_data.size();
|
||||
gen_normal_vertices(neighbor_block);
|
||||
gen_cross_plane_vertices();
|
||||
m_need_upload = true;
|
||||
m_is_on_gen_vertex_data = false;
|
||||
}
|
||||
|
||||
GLuint Chunk::get_vbo() const { return m_vbo; }
|
||||
GLuint Chunk::get_normal_vbo() const { return m_normal_vbo; }
|
||||
|
||||
size_t Chunk::get_vertex_sum() const {
|
||||
if (m_vertex_sum == 0) {
|
||||
Logger::warn("m_vertex_sum is 0");
|
||||
size_t Chunk::get_normal_vertices_sum() const {
|
||||
if (m_normal_vertices_sum == 0) {
|
||||
Logger::warn("m_normal_vertices_sum is 0");
|
||||
}
|
||||
return m_vertex_sum.load();
|
||||
return m_normal_vertices_sum.load();
|
||||
}
|
||||
|
||||
GLuint Chunk::get_cross_vbo() const { return m_cross_plane_vbo; }
|
||||
size_t Chunk::get_cross_vertices_sum() const {
|
||||
return m_cross_vertices_sum.load();
|
||||
}
|
||||
|
||||
void Chunk::gen_phase_one() {
|
||||
@@ -310,14 +216,25 @@ void Chunk::gen_phase_seven() {
|
||||
void Chunk::upload_to_gpu() {
|
||||
|
||||
ASSERT(is_need_upload());
|
||||
if (m_vbo == 0) {
|
||||
glGenBuffers(1, &m_vbo);
|
||||
|
||||
if (m_normal_vbo == 0) {
|
||||
glGenBuffers(1, &m_normal_vbo);
|
||||
}
|
||||
|
||||
std::lock_guard lk(m_vertexs_data_mutex);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex),
|
||||
m_vertexs_data.data(), GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_normal_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, m_normal_vertices.size() * sizeof(Vertex),
|
||||
m_normal_vertices.data(), GL_DYNAMIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
if (m_cross_plane_vertices.size() != 0) {
|
||||
if (m_cross_plane_vbo == 0) {
|
||||
glGenBuffers(1, &m_cross_plane_vbo);
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, m_cross_plane_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER,
|
||||
m_cross_plane_vertices.size() * sizeof(Vertex),
|
||||
m_cross_plane_vertices.data(), GL_DYNAMIC_DRAW);
|
||||
}
|
||||
// after fininshed it, can use
|
||||
clear_dirty();
|
||||
m_need_upload = false;
|
||||
@@ -356,4 +273,160 @@ unsigned Chunk::seed() const {
|
||||
|
||||
BiomeConditions& Chunk::conditions() { return m_conditions; }
|
||||
|
||||
void Chunk::gen_normal_vertices(
|
||||
const std::array<const std::vector<BlockType>*, 4>& neighbor_block) {
|
||||
m_normal_vertices.clear();
|
||||
|
||||
static const glm::ivec3 DIR[6] = {{0, 0, 1}, {1, 0, 0}, {0, 0, -1},
|
||||
{-1, 0, 0}, {0, 1, 0}, {0, -1, 0}};
|
||||
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
for (int y = 0; y < SIZE_Y; y++) {
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
int world_x = x + m_chunk_pos.x * CHUNK_SIZE;
|
||||
int world_z = z + m_chunk_pos.z * CHUNK_SIZE;
|
||||
int world_y = y;
|
||||
int cur_id = m_blocks[index(x, y, z)];
|
||||
// air
|
||||
if (cur_id == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int face = 0; face < 6; face++) {
|
||||
int nx = x + DIR[face].x;
|
||||
int ny = y + DIR[face].y;
|
||||
int nz = z + DIR[face].z;
|
||||
bool neighbor_culled = false;
|
||||
|
||||
if (nx < 0 || nx >= SIZE_X || ny < 0 || ny >= SIZE_Y ||
|
||||
nz < 0 || nz >= SIZE_Z) {
|
||||
|
||||
int world_nx = world_x + DIR[face].x;
|
||||
int world_ny = world_y + DIR[face].y;
|
||||
int world_nz = world_z + DIR[face].z;
|
||||
|
||||
auto [neighbor_x, neighbor_z] =
|
||||
World::chunk_pos(world_nx, world_nz);
|
||||
|
||||
auto is_culled =
|
||||
[&](const std::vector<BlockType>* chunk_blocks) {
|
||||
if (chunk_blocks == nullptr) {
|
||||
return true;
|
||||
}
|
||||
int x, y, z;
|
||||
y = world_ny;
|
||||
x = world_nx - neighbor_x * CHUNK_SIZE;
|
||||
z = world_nz - neighbor_z * CHUNK_SIZE;
|
||||
if (x < 0 || y < 0 || z < 0 ||
|
||||
x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||
z >= CHUNK_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int idx = Chunk::index(x, y, z);
|
||||
// not init
|
||||
if (static_cast<size_t>(idx) >=
|
||||
chunk_blocks->size()) {
|
||||
// Logger::warn("not init");
|
||||
return true;
|
||||
}
|
||||
auto id = (*chunk_blocks)[idx];
|
||||
// transparent
|
||||
if (BlockManager::is_transparent(id)) {
|
||||
if (id == cur_id) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
if (m_chunk_pos.x + 1 == neighbor_x) {
|
||||
neighbor_culled = is_culled(neighbor_block[0]);
|
||||
} else if (m_chunk_pos.x - 1 == neighbor_x) {
|
||||
neighbor_culled = is_culled(neighbor_block[1]);
|
||||
} else if (m_chunk_pos.z + 1 == neighbor_z) {
|
||||
neighbor_culled = is_culled(neighbor_block[2]);
|
||||
} else if (m_chunk_pos.z - 1 == neighbor_z) {
|
||||
neighbor_culled = is_culled(neighbor_block[3]);
|
||||
}
|
||||
// neighbor_cull = m_world.is_block(glm::ivec3(world_x,
|
||||
// world_y, world_z) + DIR[face]);
|
||||
} else {
|
||||
auto neighbor_id = m_blocks[index(nx, ny, nz)];
|
||||
// transparent block
|
||||
if (!BlockManager::is_transparent(neighbor_id)) {
|
||||
neighbor_culled = true;
|
||||
} else {
|
||||
if (neighbor_id == cur_id) {
|
||||
neighbor_culled = true;
|
||||
} else {
|
||||
neighbor_culled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (neighbor_culled) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Vertex vex = {
|
||||
VERTICES_POS[face][i][0] + (float)world_x * 1.0f,
|
||||
VERTICES_POS[face][i][1] + (float)world_y * 1.0f,
|
||||
VERTICES_POS[face][i][2] + (float)world_z * 1.0f,
|
||||
TEX_COORDS[face][i][0],
|
||||
TEX_COORDS[face][i][1],
|
||||
static_cast<float>(cur_id * 6 + face)
|
||||
|
||||
};
|
||||
m_normal_vertices.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_normal_vertices_sum = m_normal_vertices.size();
|
||||
}
|
||||
void Chunk::gen_cross_plane_vertices() {
|
||||
|
||||
m_cross_plane_vertices.clear();
|
||||
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
for (int y = 0; y < SIZE_Y; y++) {
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
int world_x = x + m_chunk_pos.x * CHUNK_SIZE;
|
||||
int world_z = z + m_chunk_pos.z * CHUNK_SIZE;
|
||||
int world_y = y;
|
||||
int id = m_blocks[index(x, y, z)];
|
||||
|
||||
if (!BlockManager::is_cross_plane(id)) {
|
||||
continue;
|
||||
}
|
||||
for (int face = 0; face < 2; face++) {
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Vertex vex = {CROSS_VERTICES_POS[face][i][0] +
|
||||
(float)world_x * 1.0f,
|
||||
CROSS_VERTICES_POS[face][i][1] +
|
||||
(float)world_y * 1.0f,
|
||||
CROSS_VERTICES_POS[face][i][2] +
|
||||
(float)world_z * 1.0f,
|
||||
CROSS_TEX_COORDS[face][i][0],
|
||||
CROSS_TEX_COORDS[face][i][1],
|
||||
static_cast<float>(
|
||||
BlockManager::cross_plane_index(id))
|
||||
|
||||
};
|
||||
m_cross_plane_vertices.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_cross_vertices_sum = m_cross_plane_vertices.size();
|
||||
// Logger::info("Cross Sum {}", m_cross_vertices_sum.load());
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/tree.hpp"
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/cubed_hash.hpp"
|
||||
#include "Cubed/tools/math_tools.hpp"
|
||||
#include "Cubed/tools/perlin_noise.hpp"
|
||||
|
||||
@@ -421,7 +421,7 @@ void Player::update_x_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (m_world.is_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -455,7 +455,7 @@ void Player::update_y_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (m_world.is_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -493,7 +493,7 @@ void Player::update_z_move() {
|
||||
for (int x = minx; x <= maxx; ++x) {
|
||||
for (int y = miny; y <= maxy; ++y) {
|
||||
for (int z = minz; z <= maxz; ++z) {
|
||||
if (m_world.is_block(glm::vec3{x, y, z})) {
|
||||
if (!m_world.can_pass_block(glm::vec3{x, y, z})) {
|
||||
AABB block_box = {glm::vec3{static_cast<float>(x),
|
||||
static_cast<float>(y),
|
||||
static_cast<float>(z)},
|
||||
@@ -526,13 +526,13 @@ void Player::update_scroll(double yoffset) {
|
||||
if (m_game_mode == CREATIVE) {
|
||||
if (yoffset < 0) {
|
||||
m_place_block += 1;
|
||||
if (m_place_block >= MAX_BLOCK_NUM) {
|
||||
if (m_place_block >= BlockManager::sums()) {
|
||||
m_place_block = 1;
|
||||
}
|
||||
} else {
|
||||
m_place_block -= 1;
|
||||
if (m_place_block <= 0) {
|
||||
m_place_block = MAX_BLOCK_NUM - 1;
|
||||
m_place_block = BlockManager::sums() - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "Cubed/gameplay/tree.hpp"
|
||||
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/debug_collector.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/texture_manager.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/cubed_hash.hpp"
|
||||
#include "Cubed/tools/math_tools.hpp"
|
||||
@@ -290,13 +291,18 @@ void World::init_chunks() {
|
||||
}
|
||||
}
|
||||
*/
|
||||
void World::render(const glm::mat4& mvp_matrix) {
|
||||
void World::render(const glm::mat4& mvp_matrix,
|
||||
const TextureManager& texture_manager) {
|
||||
Math::extract_frustum_planes(mvp_matrix, m_planes);
|
||||
int rendered_sum = 0;
|
||||
auto player_pos = get_player("TestPlayer").get_player_pos();
|
||||
glm::vec2 player_pos_xz{player_pos.x, player_pos.z};
|
||||
for (const auto& snapshot : m_render_snapshots) {
|
||||
|
||||
if (is_aabb_in_frustum(snapshot.center, snapshot.half_extents)) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.vbo);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY,
|
||||
texture_manager.get_texture_array());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.normal_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
(void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||
@@ -308,8 +314,37 @@ void World::render(const glm::mat4& mvp_matrix) {
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, snapshot.vertex_count);
|
||||
glDrawArrays(GL_TRIANGLES, 0, snapshot.normal_vertices_count);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
if (snapshot.cross_vertices_count != 0) {
|
||||
glm::vec2 center_xz{snapshot.center.x, snapshot.center.z};
|
||||
float dist = glm::distance(player_pos_xz, center_xz);
|
||||
if (dist <= CROSS_PLANE_DISTANCE * 16) {
|
||||
|
||||
glDepthMask(GL_FALSE);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY,
|
||||
texture_manager.get_cross_plane_array());
|
||||
glBindBuffer(GL_ARRAY_BUFFER, snapshot.cross_vbo);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
|
||||
sizeof(Vertex), (void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
|
||||
sizeof(Vertex),
|
||||
(void*)offsetof(Vertex, s));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE,
|
||||
sizeof(Vertex),
|
||||
(void*)offsetof(Vertex, layer));
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0,
|
||||
snapshot.cross_vertices_count);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDepthMask(GL_TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
rendered_sum++;
|
||||
}
|
||||
}
|
||||
@@ -745,6 +780,24 @@ bool World::is_block(const glm::ivec3& block_pos) const {
|
||||
}
|
||||
}
|
||||
|
||||
bool World::can_pass_block(const glm::ivec3& block_pos) const {
|
||||
auto [chunk_x, chunk_z] = chunk_pos(block_pos.x, block_pos.z);
|
||||
std::lock_guard lk(m_chunks_mutex);
|
||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||
|
||||
if (it == m_chunks.end()) {
|
||||
return true;
|
||||
}
|
||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||
auto [x, y, z] = Chunk::world_to_block(block_pos, {chunk_x, chunk_z});
|
||||
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
|
||||
z >= CHUNK_SIZE) {
|
||||
return true;
|
||||
}
|
||||
auto id = chunk_blocks[Chunk::index(x, y, z)];
|
||||
return BlockManager::is_passable(id);
|
||||
}
|
||||
|
||||
void World::set_block(const glm::ivec3& block_pos, unsigned id) {
|
||||
|
||||
int world_x, world_y, world_z;
|
||||
@@ -841,7 +894,8 @@ void World::update(float delta_time) {
|
||||
chunk.upload_to_gpu();
|
||||
}
|
||||
m_render_snapshots.push_back(
|
||||
{chunk.get_vbo(), chunk.get_vertex_sum(),
|
||||
{chunk.get_normal_vbo(), chunk.get_normal_vertices_sum(),
|
||||
chunk.get_cross_vbo(), chunk.get_cross_vertices_sum(),
|
||||
glm::vec3(static_cast<float>(pos.x * CHUNK_SIZE) +
|
||||
static_cast<float>(CHUNK_SIZE / 2),
|
||||
static_cast<float>(WORLD_SIZE_Y / 2),
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
#include "Cubed/map_table.hpp"
|
||||
|
||||
#include "Cubed/gameplay/block.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/cubed_hash.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
/*
|
||||
std::string_view MapTable::get_name_from_id(unsigned id) {
|
||||
auto it = id_to_name_map.find(id);
|
||||
ASSERT_MSG(it != id_to_name_map.end(),
|
||||
@@ -25,8 +21,9 @@ std::string_view MapTable::item_name(unsigned id) {
|
||||
}
|
||||
|
||||
const std::vector<std::string>& MapTable::item_map() { return item_id_to_name; }
|
||||
|
||||
*/
|
||||
void MapTable::init_map() {
|
||||
/*
|
||||
id_to_name_map.reserve(MAX_BLOCK_NUM);
|
||||
name_to_id_map.reserve(MAX_BLOCK_NUM);
|
||||
|
||||
@@ -37,6 +34,7 @@ void MapTable::init_map() {
|
||||
for (auto s : BLOCK_REISTER) {
|
||||
item_id_to_name.emplace_back(s);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
|
||||
@@ -280,14 +280,13 @@ void Renderer::render_world() {
|
||||
m_proj_loc = shader.loc("proj_matrix");
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
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;
|
||||
glUniformMatrix4fv(m_mv_loc, 1, GL_FALSE, glm::value_ptr(m_mv_mat));
|
||||
glUniformMatrix4fv(m_proj_loc, 1, GL_FALSE, glm::value_ptr(m_p_mat));
|
||||
m_mvp_mat = m_p_mat * m_mv_mat;
|
||||
m_world.render(m_mvp_mat);
|
||||
m_world.render(m_mvp_mat, m_texture_manager);
|
||||
}
|
||||
|
||||
void Renderer::render_dev_panel() {
|
||||
|
||||
@@ -16,6 +16,7 @@ TextureManager::~TextureManager() { delet_texture(); }
|
||||
void TextureManager::delet_texture() {
|
||||
glDeleteTextures(1, &m_texture_array);
|
||||
glDeleteTextures(1, &m_block_status_array);
|
||||
glDeleteTextures(1, &m_cross_plane_array);
|
||||
for (auto& id : m_item_textures) {
|
||||
glDeleteTextures(1, &id);
|
||||
}
|
||||
@@ -28,6 +29,9 @@ GLuint TextureManager::get_block_status_array() const {
|
||||
|
||||
GLuint TextureManager::get_texture_array() const { return m_texture_array; }
|
||||
|
||||
GLuint TextureManager::get_cross_plane_array() const {
|
||||
return m_cross_plane_array;
|
||||
}
|
||||
GLuint TextureManager::get_ui_array() const { return m_ui_array; }
|
||||
|
||||
const std::vector<GLuint>& TextureManager::item_textures() const {
|
||||
@@ -47,12 +51,18 @@ void TextureManager::load_block_status(unsigned id) {
|
||||
}
|
||||
|
||||
void TextureManager::load_block_texture(unsigned id) {
|
||||
ASSERT_MSG(id < MAX_BLOCK_NUM, "Exceed the max block sum limit");
|
||||
std::string name{MapTable::get_name_from_id(id)};
|
||||
ASSERT_MSG(id < BlockManager::sums(), "Exceed the max block sum limit");
|
||||
std::string name{BlockManager::name_form_id(id)};
|
||||
// air don`t need texture
|
||||
if (id == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (BlockManager::is_cross_plane(id)) {
|
||||
load_cross_plane_texture(id);
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char* image_data[6];
|
||||
|
||||
std::string block_texture_path = "texture/block/" + name;
|
||||
@@ -73,7 +83,11 @@ void TextureManager::load_block_texture(unsigned id) {
|
||||
}
|
||||
}
|
||||
|
||||
void TextureManager::load_item_texture(const std::string& name) {
|
||||
void TextureManager::load_block_item_texture(unsigned id) {
|
||||
|
||||
ASSERT_MSG(id < BlockManager::sums(), "Exceed the max block sum limit");
|
||||
std::string name{BlockManager::name_form_id(id)};
|
||||
|
||||
std::string path = "texture/item/block/" + name + ".png";
|
||||
unsigned char* data = nullptr;
|
||||
data = Tools::load_image_data(path);
|
||||
@@ -95,6 +109,17 @@ void TextureManager::load_item_texture(const std::string& name) {
|
||||
Tools::delete_image_data(data);
|
||||
}
|
||||
|
||||
void TextureManager::load_cross_plane_texture(unsigned id) {
|
||||
std::string path =
|
||||
"texture/block/" + BlockManager::name_form_id(id) + "/cross.png";
|
||||
unsigned char* image_data = Tools::load_image_data(path);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_cross_plane_array);
|
||||
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0,
|
||||
BlockManager::cross_plane_index(id), 16, 16, 1, GL_RGBA,
|
||||
GL_UNSIGNED_BYTE, image_data);
|
||||
Tools::delete_image_data(image_data);
|
||||
}
|
||||
|
||||
void TextureManager::load_ui_texture(unsigned id) {
|
||||
ASSERT_MSG(id < MAX_UI_NUM, "Exceed the max ui sum limit");
|
||||
|
||||
@@ -107,19 +132,22 @@ void TextureManager::load_ui_texture(unsigned id) {
|
||||
Tools::delete_image_data(image_data);
|
||||
}
|
||||
|
||||
void TextureManager::init_item() {
|
||||
auto& map = MapTable::item_map();
|
||||
for (const auto& name : map) {
|
||||
load_item_texture(name);
|
||||
}
|
||||
}
|
||||
void TextureManager::init_block() {
|
||||
|
||||
glGenTextures(1, &m_texture_array);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_array);
|
||||
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 16, 16, MAX_BLOCK_NUM * 6, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
for (int i = 0; i < MAX_BLOCK_NUM; i++) {
|
||||
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 16, 16,
|
||||
BlockManager::sums() * 6, 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
nullptr);
|
||||
|
||||
glGenTextures(1, &m_cross_plane_array);
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_cross_plane_array);
|
||||
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 16, 16,
|
||||
BlockManager::cross_plane_sum(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
|
||||
nullptr);
|
||||
for (unsigned i = 0; i < BlockManager::sums(); i++) {
|
||||
load_block_texture(i);
|
||||
load_block_item_texture(i);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_texture_array);
|
||||
@@ -131,6 +159,17 @@ void TextureManager::init_block() {
|
||||
glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY,
|
||||
static_cast<GLfloat>(m_aniso));
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D_ARRAY, m_cross_plane_array);
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER,
|
||||
GL_LINEAR_MIPMAP_LINEAR);
|
||||
glGenerateMipmap(GL_TEXTURE_2D_ARRAY);
|
||||
if (m_aniso >= 1) {
|
||||
glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY,
|
||||
static_cast<GLfloat>(m_aniso));
|
||||
}
|
||||
|
||||
Logger::info("Block Texture Load Success");
|
||||
}
|
||||
void TextureManager::init_ui() {
|
||||
@@ -181,7 +220,6 @@ void TextureManager::init_texture() {
|
||||
init_block();
|
||||
init_block_status();
|
||||
init_ui();
|
||||
init_item();
|
||||
}
|
||||
|
||||
void TextureManager::update() {
|
||||
|
||||
88
uv.lock
generated
Normal file
@@ -0,0 +1,88 @@
|
||||
version = 1
|
||||
revision = 3
|
||||
requires-python = ">=3.14"
|
||||
|
||||
[[package]]
|
||||
name = "colorama"
|
||||
version = "0.4.6"
|
||||
source = { registry = "https://mirrors.ustc.edu.cn/pypi/simple" }
|
||||
sdist = { url = "https://mirrors.ustc.edu.cn/pypi/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697, upload-time = "2022-10-25T02:36:22.414Z" }
|
||||
wheels = [
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cubed"
|
||||
version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
dependencies = [
|
||||
{ name = "loguru" },
|
||||
{ name = "pytomlpp" },
|
||||
]
|
||||
|
||||
[package.dev-dependencies]
|
||||
dev = [
|
||||
{ name = "ruff" },
|
||||
]
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "loguru", specifier = ">=0.7.3" },
|
||||
{ name = "pytomlpp", specifier = ">=1.1.0" },
|
||||
]
|
||||
|
||||
[package.metadata.requires-dev]
|
||||
dev = [{ name = "ruff", specifier = ">=0.15.14" }]
|
||||
|
||||
[[package]]
|
||||
name = "loguru"
|
||||
version = "0.7.3"
|
||||
source = { registry = "https://mirrors.ustc.edu.cn/pypi/simple" }
|
||||
dependencies = [
|
||||
{ name = "colorama", marker = "sys_platform == 'win32'" },
|
||||
{ name = "win32-setctime", marker = "sys_platform == 'win32'" },
|
||||
]
|
||||
sdist = { url = "https://mirrors.ustc.edu.cn/pypi/packages/3a/05/a1dae3dffd1116099471c643b8924f5aa6524411dc6c63fdae648c4f1aca/loguru-0.7.3.tar.gz", hash = "sha256:19480589e77d47b8d85b2c827ad95d49bf31b0dcde16593892eb51dd18706eb6", size = 63559, upload-time = "2024-12-06T11:20:56.608Z" }
|
||||
wheels = [
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/0c/29/0348de65b8cc732daa3e33e67806420b2ae89bdce2b04af740289c5c6c8c/loguru-0.7.3-py3-none-any.whl", hash = "sha256:31a33c10c8e1e10422bfd431aeb5d351c7cf7fa671e3c4df004162264b28220c", size = 61595, upload-time = "2024-12-06T11:20:54.538Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pytomlpp"
|
||||
version = "1.1.0"
|
||||
source = { registry = "https://mirrors.ustc.edu.cn/pypi/simple" }
|
||||
sdist = { url = "https://mirrors.ustc.edu.cn/pypi/packages/10/fe/50ca1ac0c1a932d3e71311baff531c0395e546ec0cd42bc8a8a88c36e00b/pytomlpp-1.1.0.tar.gz", hash = "sha256:61a0f73e7ba2fe8bba4e99ce6d2701491885850b53aad8f3e46d03c4b31e594d", size = 1323755, upload-time = "2025-11-29T20:52:09.582Z" }
|
||||
|
||||
[[package]]
|
||||
name = "ruff"
|
||||
version = "0.15.14"
|
||||
source = { registry = "https://mirrors.ustc.edu.cn/pypi/simple" }
|
||||
sdist = { url = "https://mirrors.ustc.edu.cn/pypi/packages/dc/8a/8bce2894573e9dae6ff4d77fe34ad727d79b9e6238ad288c5638990d90f6/ruff-0.15.14.tar.gz", hash = "sha256:48e866b165be4a9bdbf310f7d3c9a07edef2fe8cd63ffeb4e00bb590506ebf9f", size = 4700910, upload-time = "2026-05-21T14:34:55.177Z" }
|
||||
wheels = [
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/b9/c8/74a92c6ff9fcfb4f1f947126d3ebee8389276e161ecc85de5bda7cda51bd/ruff-0.15.14-py3-none-linux_armv6l.whl", hash = "sha256:8dd2db9416e487c8d4b01fa7056bb02c4d05969d4f8d17a08c229c2f4ff3c108", size = 10739177, upload-time = "2026-05-21T14:34:37.332Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/45/91/254a35c20acc38a7223c9d2d594af12e794432464f2cdeb52af1dc4a892d/ruff-0.15.14-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:be4ff55af755bd71a00ab3dc6bd7ffc467bd76e0df6881e286c2e3d23e8fb43b", size = 11144969, upload-time = "2026-05-21T14:34:43.978Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/56/9e/d13e40f83b8d0a94430e6778ce1d94a43b38cf2efe63278bdd2b4c65abbf/ruff-0.15.14-py3-none-macosx_11_0_arm64.whl", hash = "sha256:48d5909d7d06276ce7dde6d32bfa4b0d4cb2651145cd8ee4b440722cbc77832f", size = 10478207, upload-time = "2026-05-21T14:34:48.378Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/8d/f1/b15a7839fa4f332f8acec78e20564f26bb2d866e3d21710b877fd0263000/ruff-0.15.14-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ca8cbfa94c4f90984a67561978602746d4cd27103568f745fa90eee3f0d4107d", size = 10818459, upload-time = "2026-05-21T14:34:22.318Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/45/33/53d651177f84f94b400a0e27f8824eeada3dddc9d5ee8aeb048f4352a520/ruff-0.15.14-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:9a6bbc0333f1ab053423bcbf6226477d266ca7cec7738c4c8e3f55647803f3c4", size = 10541800, upload-time = "2026-05-21T14:34:20.209Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/b8/a6/868f87e0bf9786ed24b5d0d0ad8676b8a94fd1912f42cddf9cfc7857818a/ruff-0.15.14-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:8a24a4f7605d7003a6674d4387651effd939dead3fddd0f36561eb77a9a2e542", size = 11342149, upload-time = "2026-05-21T14:34:46.365Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/a7/8b/38cd5c19faffdcc05a408d2b78edccc69492ab9720eadb49ea15ef80d768/ruff-0.15.14-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:049b5326e53ed80978f2fc041a280603f69dd6b0c95464342a2bb4572d9d9e2f", size = 12212563, upload-time = "2026-05-21T14:34:28.579Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/3e/4d/a3c5b874a556d5731e3e657aaf04311bb76f0a5c3ec220ed43051be6b64b/ruff-0.15.14-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:d4ed42e6696c8dfa5f06728e6441993901f548eb92d73bc472cb5a38d1395fbf", size = 11493299, upload-time = "2026-05-21T14:34:41.836Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/1e/c0/56472c251d09858a53e51efbd485b09e1995d8731668b76d52e5dd6ee0f1/ruff-0.15.14-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:715c543cf450c4888251f91c52f1942a800541d9bddd7ac060aa4e6b77ae7cba", size = 11455931, upload-time = "2026-05-21T14:34:57.276Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/2c/4a/e2e7b4d8dbf233d4eace59c75bc3435fa6d8bd3bae82d351d4e4300c0fd1/ruff-0.15.14-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:72ebab6013ec887d439d8b7593737a0a4ffb06d45d209d4e4bf2e92813082d3f", size = 11400794, upload-time = "2026-05-21T14:34:39.773Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/97/c7/83c0539fe34c3e09136204d1e75d6052492364e0b3cb05e9465423f567d7/ruff-0.15.14-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:49072d36abdbe97a8dd7f480afe9c675699c0c495d4c84076e2c1203c4550581", size = 10804759, upload-time = "2026-05-21T14:34:31.045Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/86/a6/18f2bfc095a2ab4a78745644e428205532ce6653a5d0fa8501572891534d/ruff-0.15.14-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:958522aee105068640c2c2ceae08f413ae44d922f52a1374ac13d6a96032fc93", size = 10539517, upload-time = "2026-05-21T14:34:53.064Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/54/3a/5a8b3b69c654d4e4bf1d246ac5b49cbcdac6eaab6905925f8915f31e3b80/ruff-0.15.14-py3-none-musllinux_1_2_i686.whl", hash = "sha256:f3707da619a143a2e8830e2abab8224478d69ace2d28cb6c20543ae97c36bf61", size = 11065169, upload-time = "2026-05-21T14:34:24.484Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/ed/c5/8864e4e7925b836ea354b31d57641ec03830564e281a8b6f061f8c3e0ec1/ruff-0.15.14-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:bb01d645694e3ec0102105d07ef2d53703970407d59c04e59d3ba0b7a1d53553", size = 11560214, upload-time = "2026-05-21T14:34:50.975Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/36/38/012bf76752e1f89ed50b77b99532d90f3a3e287bc7918e1fc0948ac866ac/ruff-0.15.14-py3-none-win32.whl", hash = "sha256:6d0c1ad2a0ab718d39b6d8fd2217981ce4d625cd96a720095f798fb47d8b13e6", size = 10805548, upload-time = "2026-05-21T14:34:33.453Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/d1/b7/4ea2c170f10ad760fff2a5250beb18897719dc8b52b53a24cddbb9dd3f19/ruff-0.15.14-py3-none-win_amd64.whl", hash = "sha256:802342981e056db3851a7836e5b070f8f15f67d4a685ae2a6160939d364b2902", size = 11939523, upload-time = "2026-05-21T14:34:18.077Z" },
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/62/d5/bc97ff895ec35cf3925d4bd60f3b39d822f377a446906ec9bcc87405e59b/ruff-0.15.14-py3-none-win_arm64.whl", hash = "sha256:ff47b90a9ef6a40c9e2f3b479c1fb78531adf055b94c1eba0a7ba04b31951826", size = 11208607, upload-time = "2026-05-21T14:34:26.525Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "win32-setctime"
|
||||
version = "1.2.0"
|
||||
source = { registry = "https://mirrors.ustc.edu.cn/pypi/simple" }
|
||||
sdist = { url = "https://mirrors.ustc.edu.cn/pypi/packages/b3/8f/705086c9d734d3b663af0e9bb3d4de6578d08f46b1b101c2442fd9aecaa2/win32_setctime-1.2.0.tar.gz", hash = "sha256:ae1fdf948f5640aae05c511ade119313fb6a30d7eabe25fef9764dca5873c4c0", size = 4867, upload-time = "2024-12-07T15:28:28.314Z" }
|
||||
wheels = [
|
||||
{ url = "https://mirrors.ustc.edu.cn/pypi/packages/e1/07/c6fe3ad3e685340704d314d765b7912993bcb8dc198f0e7a89382d37974b/win32_setctime-1.2.0-py3-none-any.whl", hash = "sha256:95d644c4e708aba81dc3704a116d8cbc974d70b3bdb8be1d150e36be6e9d1390", size = 4083, upload-time = "2024-12-07T15:28:26.465Z" },
|
||||
]
|
||||