This commit is contained in:
MihailRis 2024-02-26 01:27:27 +03:00
parent e06350915e
commit 925ef426de
6 changed files with 240 additions and 233 deletions

View File

@ -15,18 +15,19 @@ class Content;
class ContentGfxCache;
class BlocksPreview {
public:
static ImageData* draw(
const ContentGfxCache* cache,
Framebuffer* framebuffer,
Batch3D* batch,
const Block* block,
int size);
int size
);
public:
static std::unique_ptr<Atlas> build(
const ContentGfxCache* cache,
Assets* assets,
const Content* content);
const Content* content
);
};
#endif // FRONTEND_BLOCKS_PREVIEW_H_

View File

@ -34,19 +34,18 @@
#include "graphics/Skybox.h"
#include "graphics/ChunksRenderer.h"
using glm::vec3;
using glm::vec4;
using glm::mat4;
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend)
: engine(engine),
level(frontend->getLevel()),
frustumCulling(new Frustum()),
lineBatch(new LineBatch()),
renderer(new ChunksRenderer(level,
level(frontend->getLevel())
{
frustumCulling = std::make_unique<Frustum>();
lineBatch = std::make_unique<LineBatch>();
renderer = std::make_unique<ChunksRenderer>(
level,
frontend->getContentGfxCache(),
engine->getSettings())),
batch3d(new Batch3D(4096)) {
engine->getSettings()
);
batch3d = std::make_unique<Batch3D>(4096);
auto& settings = engine->getSettings();
level->events->listen(EVT_CHUNK_HIDDEN,
@ -55,53 +54,58 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend)
}
);
auto assets = engine->getAssets();
skybox = new Skybox(settings.graphics.skyboxResolution,
assets->getShader("skybox_gen"));
skybox = std::make_unique<Skybox>(
settings.graphics.skyboxResolution,
assets->getShader("skybox_gen")
);
}
WorldRenderer::~WorldRenderer() {
delete skybox;
delete lineBatch;
delete renderer;
delete frustumCulling;
}
bool WorldRenderer::drawChunk(size_t index,
bool WorldRenderer::drawChunk(
size_t index,
Camera* camera,
Shader* shader,
bool culling){
bool culling
){
auto chunk = level->chunks->chunks[index];
if (!chunk->isLighted()) {
return false;
}
float distance = glm::distance(
camera->position,
glm::vec3((chunk->x + 0.5f) * CHUNK_W, camera->position.y, (chunk->z + 0.5f) * CHUNK_D)
glm::vec3((chunk->x + 0.5f) * CHUNK_W,
camera->position.y,
(chunk->z + 0.5f) * CHUNK_D)
);
auto mesh = renderer->getOrRender(chunk, distance < CHUNK_W*1.5f);
if (mesh == nullptr) {
return false;
}
if (culling){
vec3 min(chunk->x * CHUNK_W,
glm::vec3 min(
chunk->x * CHUNK_W,
chunk->bottom,
chunk->z * CHUNK_D);
vec3 max(chunk->x * CHUNK_W + CHUNK_W,
chunk->z * CHUNK_D
);
glm::vec3 max(
chunk->x * CHUNK_W + CHUNK_W,
chunk->top,
chunk->z * CHUNK_D + CHUNK_D);
chunk->z * CHUNK_D + CHUNK_D
);
if (!frustumCulling->IsBoxVisible(min, max)) return false;
if (!frustumCulling->IsBoxVisible(min, max))
return false;
}
vec3 coord = vec3(chunk->x*CHUNK_W+0.5f, 0.5f, chunk->z*CHUNK_D+0.5f);
mat4 model = glm::translate(mat4(1.0f), coord);
glm::vec3 coord(chunk->x*CHUNK_W+0.5f, 0.5f, chunk->z*CHUNK_D+0.5f);
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
shader->uniformMatrix("u_model", model);
mesh->draw();
return true;
}
void WorldRenderer::drawChunks(Chunks* chunks,
Camera* camera,
Shader* shader) {
void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
renderer->update();
std::vector<size_t> indices;
for (size_t i = 0; i < chunks->volume; i++){
@ -133,8 +137,9 @@ void WorldRenderer::drawChunks(Chunks* chunks,
void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible){
Window::clearDepth();
EngineSettings& settings = engine->getSettings();
Window::clearDepth();
skybox->refresh(pctx, level->world->daytime, 1.0f+fog*2.0f, 4);
const Content* content = level->content;
@ -192,9 +197,9 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
blockid_t id = PlayerController::selectedBlockId;
Block* block = indices->getBlockDef(id);
assert(block != nullptr);
const vec3 pos = PlayerController::selectedBlockPosition;
const vec3 point = PlayerController::selectedPointPosition;
const vec3 norm = PlayerController::selectedBlockNormal;
const glm::vec3 pos = PlayerController::selectedBlockPosition;
const glm::vec3 point = PlayerController::selectedPointPosition;
const glm::vec3 norm = PlayerController::selectedBlockNormal;
std::vector<AABB>& hitboxes = block->rotatable
? block->rt.hitboxes[PlayerController::selectedBlockStates]
@ -204,11 +209,12 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
linesShader->uniformMatrix("u_projview", camera->getProjView());
lineBatch->lineWidth(2.0f);
for (auto& hitbox: hitboxes) {
const vec3 center = pos + hitbox.center();
const vec3 size = hitbox.size();
lineBatch->box(center, size + vec3(0.02), vec4(0.f, 0.f, 0.f, 0.5f));
if (level->player->debug)
lineBatch->line(point, point+norm*0.5f, vec4(1.0f, 0.0f, 1.0f, 1.0f));
const glm::vec3 center = pos + hitbox.center();
const glm::vec3 size = hitbox.size();
lineBatch->box(center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f));
if (level->player->debug) {
lineBatch->line(point, point+norm*0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f));
}
}
lineBatch->render();
}
@ -223,7 +229,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
if (settings.debug.showChunkBorders){
linesShader->uniformMatrix("u_projview", camera->getProjView());
vec3 coord = level->player->camera->position;
glm::vec3 coord = level->player->camera->position;
if (coord.x < 0) coord.x--;
if (coord.z < 0) coord.z--;
int cx = floordiv((int)coord.x, CHUNK_W);
@ -234,7 +240,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
}
float length = 40.f;
vec3 tsl = vec3(displayWidth/2, displayHeight/2, 0.f);
glm::vec3 tsl(displayWidth/2, displayHeight/2, 0.f);
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
linesShader->uniformMatrix("u_projview", glm::ortho(
0.f, (float)displayWidth,

View File

@ -29,10 +29,10 @@ class Skybox;
class WorldRenderer {
Engine* engine;
Level* level;
Frustum* frustumCulling;
LineBatch* lineBatch;
ChunksRenderer* renderer;
Skybox* skybox;
std::unique_ptr<Frustum> frustumCulling;
std::unique_ptr<LineBatch> lineBatch;
std::unique_ptr<ChunksRenderer> renderer;
std::unique_ptr<Skybox> skybox;
std::unique_ptr<Batch3D> batch3d;
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);

View File

@ -106,7 +106,7 @@ int l_file_read_bytes(lua_State* L) {
lua_createtable(L, length, 0);
int newTable = lua_gettop(L);
for(int i = 0;i < length;i++) {
for(size_t i = 0; i < length; i++) {
lua_pushnumber(L, bytes[i]);
lua_rawseti(L, newTable, i+1);
}

View File

@ -11,14 +11,14 @@
#define BLOCK_ITEM_SUFFIX ".item"
const uint FACE_MX = 0;
const uint FACE_PX = 1;
const uint FACE_MY = 2;
const uint FACE_PY = 3;
const uint FACE_MZ = 4;
const uint FACE_PZ = 5;
inline constexpr uint FACE_MX = 0;
inline constexpr uint FACE_PX = 1;
inline constexpr uint FACE_MY = 2;
inline constexpr uint FACE_PY = 3;
inline constexpr uint FACE_MZ = 4;
inline constexpr uint FACE_PZ = 5;
const uint BLOCK_AABB_GRID = 16;
inline constexpr uint BLOCK_AABB_GRID = 16;
struct block_funcs_set {
bool init: 1;

View File

@ -17,7 +17,7 @@ struct ChunkFlag {
static const int UNSAVED = 0x10;
static const int LOADED_LIGHTS = 0x20;
};
constexpr int CHUNK_DATA_LEN = CHUNK_VOL*4;
inline constexpr int CHUNK_DATA_LEN = CHUNK_VOL*4;
class Lightmap;
class ContentLUT;