frontend/graphics moved to graphics/render

This commit is contained in:
MihailRis
2024-03-19 00:34:43 +03:00
parent 5393977e57
commit 258666c377
7 changed files with 41 additions and 39 deletions
+24 -24
View File
@@ -5,36 +5,36 @@
#include <memory>
#include <assert.h>
#include "../window/Window.h"
#include "../window/Camera.h"
#include "../content/Content.h"
#include "../graphics/core/Mesh.h"
#include "../graphics/core/Atlas.h"
#include "../graphics/core/Shader.h"
#include "../graphics/core/Batch3D.h"
#include "../graphics/core/Texture.h"
#include "../graphics/core/LineBatch.h"
#include "../graphics/core/PostProcessing.h"
#include "../graphics/core/GfxContext.h"
#include "../voxels/Chunks.h"
#include "../voxels/Chunk.h"
#include "../voxels/Block.h"
#include "../world/World.h"
#include "../world/Level.h"
#include "../world/LevelEvents.h"
#include "../objects/Player.h"
#include "../assets/Assets.h"
#include "../content/Content.h"
#include "../engine.h"
#include "../graphics/core/Atlas.h"
#include "../graphics/core/Batch3D.h"
#include "../graphics/core/GfxContext.h"
#include "../graphics/core/LineBatch.h"
#include "../graphics/core/Mesh.h"
#include "../graphics/core/PostProcessing.h"
#include "../graphics/core/Shader.h"
#include "../graphics/core/Texture.h"
#include "../graphics/render/ChunksRenderer.h"
#include "../graphics/render/Skybox.h"
#include "../items/Inventory.h"
#include "../items/ItemDef.h"
#include "../items/ItemStack.h"
#include "../logic/PlayerController.h"
#include "../maths/FrustumCulling.h"
#include "../maths/voxmaths.h"
#include "../objects/Player.h"
#include "../settings.h"
#include "../engine.h"
#include "../items/ItemDef.h"
#include "../items/ItemStack.h"
#include "../items/Inventory.h"
#include "../voxels/Block.h"
#include "../voxels/Chunk.h"
#include "../voxels/Chunks.h"
#include "../window/Camera.h"
#include "../window/Window.h"
#include "../world/Level.h"
#include "../world/LevelEvents.h"
#include "../world/World.h"
#include "LevelFrontend.h"
#include "graphics/Skybox.h"
#include "graphics/ChunksRenderer.h"
WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player)
: engine(engine),
-472
View File
@@ -1,472 +0,0 @@
#include "BlocksRenderer.h"
#include <glm/glm.hpp>
#include "../../graphics/core/Mesh.h"
#include "../../graphics/core/UVRegion.h"
#include "../../constants.h"
#include "../../content/Content.h"
#include "../../voxels/Block.h"
#include "../../voxels/Chunk.h"
#include "../../voxels/VoxelsVolume.h"
#include "../../voxels/ChunksStorage.h"
#include "../../lighting/Lightmap.h"
#include "../../frontend/ContentGfxCache.h"
using glm::ivec3;
using glm::vec3;
using glm::vec4;
const uint BlocksRenderer::VERTEX_SIZE = 6;
const vec3 BlocksRenderer::SUN_VECTOR (0.411934f, 0.863868f, -0.279161f);
BlocksRenderer::BlocksRenderer(size_t capacity,
const Content* content,
const ContentGfxCache* cache,
const EngineSettings& settings)
: content(content),
vertexOffset(0),
indexOffset(0),
indexSize(0),
capacity(capacity),
cache(cache),
settings(settings) {
vertexBuffer = new float[capacity];
indexBuffer = new int[capacity];
voxelsBuffer = new VoxelsVolume(CHUNK_W + 2, CHUNK_H, CHUNK_D + 2);
blockDefsCache = content->getIndices()->getBlockDefs();
}
BlocksRenderer::~BlocksRenderer() {
delete voxelsBuffer;
delete[] vertexBuffer;
delete[] indexBuffer;
}
/* Basic vertex add method */
void BlocksRenderer::vertex(const vec3& coord, float u, float v, const vec4& light) {
vertexBuffer[vertexOffset++] = coord.x;
vertexBuffer[vertexOffset++] = coord.y;
vertexBuffer[vertexOffset++] = coord.z;
vertexBuffer[vertexOffset++] = u;
vertexBuffer[vertexOffset++] = v;
union {
float floating;
uint32_t integer;
} compressed;
compressed.integer = (uint32_t(light.r * 255) & 0xff) << 24;
compressed.integer |= (uint32_t(light.g * 255) & 0xff) << 16;
compressed.integer |= (uint32_t(light.b * 255) & 0xff) << 8;
compressed.integer |= (uint32_t(light.a * 255) & 0xff);
vertexBuffer[vertexOffset++] = compressed.floating;
}
void BlocksRenderer::index(int a, int b, int c, int d, int e, int f) {
indexBuffer[indexSize++] = indexOffset + a;
indexBuffer[indexSize++] = indexOffset + b;
indexBuffer[indexSize++] = indexOffset + c;
indexBuffer[indexSize++] = indexOffset + d;
indexBuffer[indexSize++] = indexOffset + e;
indexBuffer[indexSize++] = indexOffset + f;
indexOffset += 4;
}
/* Add face with precalculated lights */
void BlocksRenderer::face(const vec3& coord,
float w, float h, float d,
const vec3& axisX,
const vec3& axisY,
const vec3& axisZ,
const UVRegion& region,
const vec4(&lights)[4],
const vec4& tint) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) {
overflow = true;
return;
}
vec3 X = axisX * w;
vec3 Y = axisY * h;
vec3 Z = axisZ * d;
float s = 0.5f;
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, lights[0] * tint);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, lights[1] * tint);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, lights[2] * tint);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, lights[3] * tint);
index(0, 1, 3, 1, 2, 3);
}
void BlocksRenderer::vertex(const vec3& coord,
float u, float v,
const vec4& tint,
const vec3& axisX,
const vec3& axisY,
const vec3& axisZ) {
vec3 pos = coord+axisZ*0.5f+(axisX+axisY)*0.5f;
vec4 light = pickSoftLight(ivec3(round(pos.x), round(pos.y), round(pos.z)), axisX, axisY);
vertex(coord, u, v, light * tint);
}
void BlocksRenderer::face(const vec3& coord,
const vec3& X,
const vec3& Y,
const vec3& Z,
const UVRegion& region,
bool lights) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) {
overflow = true;
return;
}
float s = 0.5f;
if (lights) {
float d = glm::dot(Z, SUN_VECTOR);
d = 0.8f + d * 0.2f;
vec3 axisX = glm::normalize(X);
vec3 axisY = glm::normalize(Y);
vec3 axisZ = glm::normalize(Z);
vec4 tint(d);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, axisX, axisY, axisZ);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, axisX, axisY, axisZ);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, axisX, axisY, axisZ);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, axisX, axisY, axisZ);
} else {
vec4 tint(1.0f);
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint);
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint);
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint);
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint);
}
index(0, 1, 2, 0, 2, 3);
}
void BlocksRenderer::tetragonicFace(const vec3& coord, const vec3& p1,
const vec3& p2, const vec3& p3, const vec3& p4,
const vec3& X,
const vec3& Y,
const vec3& Z,
const UVRegion& texreg,
bool lights) {
const vec3 fp1 = (p1.x - 0.5f) * X + (p1.y - 0.5f) * Y + (p1.z - 0.5f) * Z;
const vec3 fp2 = (p2.x - 0.5f) * X + (p2.y - 0.5f) * Y + (p2.z - 0.5f) * Z;
const vec3 fp3 = (p3.x - 0.5f) * X + (p3.y - 0.5f) * Y + (p3.z - 0.5f) * Z;
const vec3 fp4 = (p4.x - 0.5f) * X + (p4.y - 0.5f) * Y + (p4.z - 0.5f) * Z;
vec4 tint(1.0f);
if (lights) {
vec3 dir = glm::cross(fp2 - fp1, fp3 - fp1);
vec3 normal = glm::normalize(dir);
float d = glm::dot(normal, SUN_VECTOR);
d = 0.8f + d * 0.2f;
tint *= d;
tint *= pickLight(coord);
// debug normal
// tint.x = normal.x * 0.5f + 0.5f;
// tint.y = normal.y * 0.5f + 0.5f;
// tint.z = normal.z * 0.5f + 0.5f;
}
vertex(coord + fp1, texreg.u1, texreg.v1, tint);
vertex(coord + fp2, texreg.u2, texreg.v1, tint);
vertex(coord + fp3, texreg.u2, texreg.v2, tint);
vertex(coord + fp4, texreg.u1, texreg.v2, tint);
index(0, 1, 3, 1, 2, 3);
}
void BlocksRenderer::blockXSprite(int x, int y, int z,
const vec3& size,
const UVRegion& texface1,
const UVRegion& texface2,
float spread) {
vec4 lights[]{
pickSoftLight({x, y + 1, z}, {1, 0, 0}, {0, 1, 0}),
pickSoftLight({x + 1, y + 1, z}, {1, 0, 0}, {0, 1, 0}),
pickSoftLight({x + 1, y + 1, z}, {1, 0, 0}, {0, 1, 0}),
pickSoftLight({x, y + 1, z}, {1, 0, 0}, {0, 1, 0}) };
int rand = ((x * z + y) ^ (z * y - x)) * (z + y);
float xs = ((float)(char)rand / 512) * spread;
float zs = ((float)(char)(rand >> 8) / 512) * spread;
const float w = size.x / 1.41f;
const float tint = 0.8f;
face(vec3(x + xs, y, z + zs),
w, size.y, 0, vec3(1, 0, 1), vec3(0, 1, 0), vec3(),
texface1, lights, vec4(tint));
face(vec3(x + xs, y, z + zs),
w, size.y, 0, vec3(-1, 0, -1), vec3(0, 1, 0), vec3(),
texface1, lights, vec4(tint));
face(vec3(x + xs, y, z + zs),
w, size.y, 0, vec3(1, 0, -1), vec3(0, 1, 0), vec3(),
texface1, lights, vec4(tint));
face(vec3(x + xs, y, z + zs),
w, size.y, 0, vec3(-1, 0, 1), vec3(0, 1, 0), vec3(),
texface1, lights, vec4(tint));
}
// HINT: texture faces order: {east, west, bottom, top, south, north}
/* AABB blocks render method */
void BlocksRenderer::blockAABB(const ivec3& icoord,
const UVRegion(&texfaces)[6],
const Block* block, ubyte rotation,
bool lights) {
if (block->hitboxes.empty()) {
return;
}
AABB hitbox = block->hitboxes[0];
for (const auto& box : block->hitboxes) {
hitbox.a = glm::min(hitbox.a, box.a);
hitbox.b = glm::max(hitbox.b, box.b);
}
vec3 size = hitbox.size();
vec3 X(1, 0, 0);
vec3 Y(0, 1, 0);
vec3 Z(0, 0, 1);
vec3 coord(icoord);
if (block->rotatable) {
auto& rotations = block->rotations;
auto& orient = rotations.variants[rotation];
X = orient.axisX;
Y = orient.axisY;
Z = orient.axisZ;
orient.transform(hitbox);
}
coord = vec3(icoord) - vec3(0.5f) + hitbox.center();
face(coord, X*size.x, Y*size.y, Z*size.z, texfaces[5], lights); // north
face(coord, -X*size.x, Y*size.y, -Z*size.z, texfaces[4], lights); // south
face(coord, X*size.x, -Z*size.z, Y*size.y, texfaces[3], lights); // top
face(coord, -X*size.x, -Z*size.z, -Y*size.y, texfaces[2], lights); // bottom
face(coord, -Z*size.z, Y*size.y, X*size.x, texfaces[1], lights); // west
face(coord, Z*size.z, Y*size.y, -X*size.x, texfaces[0], lights); // east
}
void BlocksRenderer::blockCustomModel(const ivec3& icoord,
const Block* block, ubyte rotation, bool lights) {
vec3 X(1, 0, 0);
vec3 Y(0, 1, 0);
vec3 Z(0, 0, 1);
CoordSystem orient(X,Y,Z);
vec3 coord(icoord);
if (block->rotatable) {
auto& rotations = block->rotations;
orient = rotations.variants[rotation];
X = orient.axisX;
Y = orient.axisY;
Z = orient.axisZ;
}
for (size_t i = 0; i < block->modelBoxes.size(); i++) {
AABB box = block->modelBoxes[i];
vec3 size = box.size();
if (block->rotatable) {
orient.transform(box);
}
vec3 center_coord = coord - vec3(0.5f) + box.center();
face(center_coord, X * size.x, Y * size.y, Z * size.z, block->modelUVs[i * 6 + 5], lights); // north
face(center_coord, -X * size.x, Y * size.y, -Z * size.z, block->modelUVs[i * 6 + 4], lights); // south
face(center_coord, X * size.x, -Z * size.z, Y * size.y, block->modelUVs[i * 6 + 3], lights); // top
face(center_coord, -X * size.x, -Z * size.z, -Y * size.y, block->modelUVs[i * 6 + 2], lights); // bottom
face(center_coord, -Z * size.z, Y * size.y, X * size.x, block->modelUVs[i * 6 + 1], lights); // west
face(center_coord, Z * size.z, Y * size.y, -X * size.x, block->modelUVs[i * 6 + 0], lights); // east
}
for (size_t i = 0; i < block->modelExtraPoints.size()/4; i++) {
tetragonicFace(coord,
block->modelExtraPoints[i * 4 + 0],
block->modelExtraPoints[i * 4 + 1],
block->modelExtraPoints[i * 4 + 2],
block->modelExtraPoints[i * 4 + 3],
X, Y, Z,
block->modelUVs[block->modelBoxes.size()*6 + i], lights);
}
}
/* Fastest solid shaded blocks render method */
void BlocksRenderer::blockCube(int x, int y, int z,
const UVRegion(&texfaces)[6],
const Block* block,
ubyte states,
bool lights) {
ubyte group = block->drawGroup;
vec3 X(1, 0, 0);
vec3 Y(0, 1, 0);
vec3 Z(0, 0, 1);
vec3 coord(x, y, z);
if (block->rotatable) {
auto& rotations = block->rotations;
auto& orient = rotations.variants[states & BLOCK_ROT_MASK];
X = orient.axisX;
Y = orient.axisY;
Z = orient.axisZ;
}
if (isOpen(x+Z.x, y+Z.y, z+Z.z, group)) {
face(coord, X, Y, Z, texfaces[5], lights);
}
if (isOpen(x-Z.x, y-Z.y, z-Z.z, group)) {
face(coord, -X, Y, -Z, texfaces[4], lights);
}
if (isOpen(x+Y.x, y+Y.y, z+Y.z, group)) {
face(coord, X, -Z, Y, texfaces[3], lights);
}
if (isOpen(x-Y.x, y-Y.y, z-Y.z, group)) {
face(coord, X, Z, -Y, texfaces[2], lights);
}
if (isOpen(x+X.x, y+X.y, z+X.z, group)) {
face(coord, -Z, Y, X, texfaces[1], lights);
}
if (isOpen(x-X.x, y-X.y, z-X.z, group)) {
face(coord, Z, Y, -X, texfaces[0], lights);
}
}
// Does block allow to see other blocks sides (is it transparent)
bool BlocksRenderer::isOpen(int x, int y, int z, ubyte group) const {
blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x,
y,
chunk->z * CHUNK_D + z);
if (id == BLOCK_VOID)
return false;
const Block& block = *blockDefsCache[id];
if ((block.drawGroup != group && block.lightPassing) || !block.rt.solid) {
return true;
}
return !id;
}
bool BlocksRenderer::isOpenForLight(int x, int y, int z) const {
blockid_t id = voxelsBuffer->pickBlockId(chunk->x * CHUNK_W + x,
y,
chunk->z * CHUNK_D + z);
if (id == BLOCK_VOID)
return false;
const Block& block = *blockDefsCache[id];
if (block.lightPassing) {
return true;
}
return !id;
}
vec4 BlocksRenderer::pickLight(int x, int y, int z) const {
if (isOpenForLight(x, y, z)) {
light_t light = voxelsBuffer->pickLight(chunk->x * CHUNK_W + x,
y,
chunk->z * CHUNK_D + z);
return vec4(Lightmap::extract(light, 0) / 15.0f,
Lightmap::extract(light, 1) / 15.0f,
Lightmap::extract(light, 2) / 15.0f,
Lightmap::extract(light, 3) / 15.0f);
}
else {
return vec4(0.0f);
}
}
vec4 BlocksRenderer::pickLight(const ivec3& coord) const {
return pickLight(coord.x, coord.y, coord.z);
}
vec4 BlocksRenderer::pickSoftLight(const ivec3& coord,
const ivec3& right,
const ivec3& up) const {
return (
pickLight(coord) +
pickLight(coord - right) +
pickLight(coord - right - up) +
pickLight(coord - up)) * 0.25f;
}
vec4 BlocksRenderer::pickSoftLight(float x, float y, float z,
const ivec3& right,
const ivec3& up) const {
return pickSoftLight({int(round(x)), int(round(y)), int(round(z))}, right, up);
}
void BlocksRenderer::render(const voxel* voxels) {
int begin = chunk->bottom * (CHUNK_W * CHUNK_D);
int end = chunk->top * (CHUNK_W * CHUNK_D);
for (const auto drawGroup : *content->drawGroups) {
for (int i = begin; i < end; i++) {
const voxel& vox = voxels[i];
blockid_t id = vox.id;
const Block& def = *blockDefsCache[id];
if (id == 0 || def.drawGroup != drawGroup)
continue;
const UVRegion texfaces[6]{ cache->getRegion(id, 0),
cache->getRegion(id, 1),
cache->getRegion(id, 2),
cache->getRegion(id, 3),
cache->getRegion(id, 4),
cache->getRegion(id, 5)};
int x = i % CHUNK_W;
int y = i / (CHUNK_D * CHUNK_W);
int z = (i / CHUNK_D) % CHUNK_W;
switch (def.model) {
case BlockModel::block:
blockCube(x, y, z, texfaces, &def, vox.states, !def.rt.emissive);
break;
case BlockModel::xsprite: {
blockXSprite(x, y, z, vec3(1.0f),
texfaces[FACE_MX], texfaces[FACE_MZ], 1.0f);
break;
}
case BlockModel::aabb: {
blockAABB(ivec3(x,y,z), texfaces, &def, vox.rotation(), !def.rt.emissive);
break;
}
case BlockModel::custom: {
blockCustomModel(ivec3(x, y, z), &def, vox.rotation(), !def.rt.emissive);
break;
}
default:
break;
}
if (overflow)
return;
}
}
}
void BlocksRenderer::build(const Chunk* chunk, const ChunksStorage* chunks) {
this->chunk = chunk;
voxelsBuffer->setPosition(chunk->x * CHUNK_W - 1, 0, chunk->z * CHUNK_D - 1);
chunks->getVoxels(voxelsBuffer, settings.graphics.backlight);
overflow = false;
vertexOffset = 0;
indexOffset = indexSize = 0;
const voxel* voxels = chunk->voxels;
render(voxels);
}
Mesh* BlocksRenderer::createMesh() {
const vattr attrs[]{ {3}, {2}, {1}, {0} };
size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE;
Mesh* mesh = new Mesh(vertexBuffer, vcount, indexBuffer, indexSize, attrs);
return mesh;
}
Mesh* BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) {
build(chunk, chunks);
return createMesh();
}
VoxelsVolume* BlocksRenderer::getVoxelsBuffer() const {
return voxelsBuffer;
}
-102
View File
@@ -1,102 +0,0 @@
#ifndef GRAPHICS_BLOCKS_RENDERER_H
#define GRAPHICS_BLOCKS_RENDERER_H
#include <stdlib.h>
#include <vector>
#include <glm/glm.hpp>
#include "../../voxels/voxel.h"
#include "../../typedefs.h"
#include "../../settings.h"
class Content;
class Mesh;
class Block;
class Chunk;
class Chunks;
class VoxelsVolume;
class ChunksStorage;
class ContentGfxCache;
class UVRegion;
class BlocksRenderer {
static const glm::vec3 SUN_VECTOR;
static const uint VERTEX_SIZE;
const Content* const content;
float* vertexBuffer;
int* indexBuffer;
size_t vertexOffset;
size_t indexOffset, indexSize;
size_t capacity;
bool overflow = false;
const Chunk* chunk = nullptr;
VoxelsVolume* voxelsBuffer;
const Block* const* blockDefsCache;
const ContentGfxCache* const cache;
const EngineSettings& settings;
void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light);
void index(int a, int b, int c, int d, int e, int f);
void vertex(const glm::vec3& coord, float u, float v,
const glm::vec4& brightness,
const glm::vec3& axisX,
const glm::vec3& axisY,
const glm::vec3& axisZ);
void face(const glm::vec3& coord, float w, float h, float d,
const glm::vec3& axisX,
const glm::vec3& axisY,
const glm::vec3& axisZ,
const UVRegion& region,
const glm::vec4(&lights)[4],
const glm::vec4& tint);
void face(const glm::vec3& coord,
const glm::vec3& axisX,
const glm::vec3& axisY,
const glm::vec3& axisZ,
const UVRegion& region,
bool lights);
void tetragonicFace(const glm::vec3& coord,
const glm::vec3& p1, const glm::vec3& p2,
const glm::vec3& p3, const glm::vec3& p4,
const glm::vec3& X,
const glm::vec3& Y,
const glm::vec3& Z,
const UVRegion& texreg,
bool lights);
void blockCube(int x, int y, int z, const UVRegion(&faces)[6], const Block* block, ubyte states, bool lights);
void blockAABB(const glm::ivec3& coord,
const UVRegion(&faces)[6],
const Block* block,
ubyte rotation,
bool lights);
void blockXSprite(int x, int y, int z, const glm::vec3& size, const UVRegion& face1, const UVRegion& face2, float spread);
void blockCustomModel(const glm::ivec3& icoord,
const Block* block, ubyte rotation,
bool lights);
bool isOpenForLight(int x, int y, int z) const;
bool isOpen(int x, int y, int z, ubyte group) const;
glm::vec4 pickLight(int x, int y, int z) const;
glm::vec4 pickLight(const glm::ivec3& coord) const;
glm::vec4 pickSoftLight(const glm::ivec3& coord, const glm::ivec3& right, const glm::ivec3& up) const;
glm::vec4 pickSoftLight(float x, float y, float z, const glm::ivec3& right, const glm::ivec3& up) const;
void render(const voxel* voxels);
public:
BlocksRenderer(size_t capacity, const Content* content, const ContentGfxCache* cache, const EngineSettings& settings);
virtual ~BlocksRenderer();
void build(const Chunk* chunk, const ChunksStorage* chunks);
Mesh* render(const Chunk* chunk, const ChunksStorage* chunks);
Mesh* createMesh();
VoxelsVolume* getVoxelsBuffer() const;
};
#endif // GRAPHICS_BLOCKS_RENDERER_H
-150
View File
@@ -1,150 +0,0 @@
#include "ChunksRenderer.h"
#include "../../graphics/core/Mesh.h"
#include "BlocksRenderer.h"
#include "../../voxels/Chunk.h"
#include "../../world/Level.h"
#include <iostream>
#include <glm/glm.hpp>
#include <glm/ext.hpp>
ChunksRenderer::ChunksRenderer(Level* level, const ContentGfxCache* cache, const EngineSettings& settings)
: level(level), cache(cache), settings(settings) {
const int MAX_FULL_CUBES = 3000;
renderer = std::make_unique<BlocksRenderer>(
9 * 6 * 6 * MAX_FULL_CUBES, level->content, cache, settings
);
const uint num_threads = std::thread::hardware_concurrency();
for (uint i = 0; i < num_threads; i++) {
threads.emplace_back(&ChunksRenderer::threadLoop, this, i);
workersBlocked.emplace_back();
}
std::cout << "created " << num_threads << " chunks rendering threads" << std::endl;
}
ChunksRenderer::~ChunksRenderer() {
{
std::unique_lock<std::mutex> lock(jobsMutex);
working = false;
}
resultsMutex.lock();
while (!results.empty()) {
mesh_entry entry = results.front();
results.pop();
entry.locked = false;
entry.variable.notify_all();
}
resultsMutex.unlock();
jobsMutexCondition.notify_all();
for (auto& thread : threads) {
thread.join();
}
}
void ChunksRenderer::threadLoop(int index) {
const int MAX_FULL_CUBES = 3000;
BlocksRenderer renderer(
9 * 6 * 6 * MAX_FULL_CUBES, level->content, cache, settings
);
std::condition_variable variable;
std::mutex mutex;
bool locked = false;
while (working) {
std::shared_ptr<Chunk> chunk;
{
std::unique_lock<std::mutex> lock(jobsMutex);
jobsMutexCondition.wait(lock, [this] {
return !jobs.empty() || !working;
});
if (!working) {
break;
}
chunk = jobs.front();
jobs.pop();
}
process(chunk, renderer);
{
resultsMutex.lock();
results.push(mesh_entry {renderer, variable, index, locked, glm::ivec2(chunk->x, chunk->z)});
locked = true;
resultsMutex.unlock();
}
{
std::unique_lock<std::mutex> lock(mutex);
variable.wait(lock, [&] {
return !working || !locked;
});
}
}
}
void ChunksRenderer::process(std::shared_ptr<Chunk> chunk, BlocksRenderer& renderer) {
renderer.build(chunk.get(), level->chunksStorage.get());
}
std::shared_ptr<Mesh> ChunksRenderer::render(std::shared_ptr<Chunk> chunk, bool important) {
chunk->setModified(false);
if (important) {
Mesh* mesh = renderer->render(chunk.get(), level->chunksStorage.get());
auto sptr = std::shared_ptr<Mesh>(mesh);
meshes[glm::ivec2(chunk->x, chunk->z)] = sptr;
return sptr;
}
glm::ivec2 key(chunk->x, chunk->z);
if (inwork.find(key) != inwork.end()) {
return nullptr;
}
inwork[key] = true;
jobsMutex.lock();
jobs.push(chunk);
jobsMutex.unlock();
jobsMutexCondition.notify_one();
return nullptr;
}
void ChunksRenderer::unload(Chunk* chunk) {
auto found = meshes.find(glm::ivec2(chunk->x, chunk->z));
if (found != meshes.end()) {
meshes.erase(found);
}
}
std::shared_ptr<Mesh> ChunksRenderer::getOrRender(std::shared_ptr<Chunk> chunk, bool important) {
auto found = meshes.find(glm::ivec2(chunk->x, chunk->z));
if (found != meshes.end()){
if (chunk->isModified()) {
render(chunk, important);
}
return found->second;
}
return render(chunk, important);
}
std::shared_ptr<Mesh> ChunksRenderer::get(Chunk* chunk) {
auto found = meshes.find(glm::ivec2(chunk->x, chunk->z));
if (found != meshes.end()) {
return found->second;
}
return nullptr;
}
void ChunksRenderer::update() {
resultsMutex.lock();
while (!results.empty()) {
mesh_entry entry = results.front();
results.pop();
meshes[entry.key] = std::shared_ptr<Mesh>(entry.renderer.createMesh());
inwork.erase(entry.key);
entry.locked = false;
entry.variable.notify_all();
}
resultsMutex.unlock();
}
-67
View File
@@ -1,67 +0,0 @@
#ifndef SRC_GRAPHICS_CHUNKSRENDERER_H_
#define SRC_GRAPHICS_CHUNKSRENDERER_H_
#include <queue>
#include <mutex>
#include <thread>
#include <memory>
#include <vector>
#include <unordered_map>
#include <glm/glm.hpp>
#include <condition_variable>
#include "../../voxels/Block.h"
#include "../../voxels/ChunksStorage.h"
#include "../../settings.h"
class Mesh;
class Chunk;
class Level;
class BlocksRenderer;
class ContentGfxCache;
struct mesh_entry {
BlocksRenderer& renderer;
std::condition_variable& variable;
int workerIndex;
bool& locked;
glm::ivec2 key;
};
class ChunksRenderer {
std::unique_ptr<BlocksRenderer> renderer;
Level* level;
std::unordered_map<glm::ivec2, std::shared_ptr<Mesh>> meshes;
std::unordered_map<glm::ivec2, bool> inwork;
std::vector<std::thread> threads;
std::queue<mesh_entry> results;
std::mutex resultsMutex;
std::queue<std::shared_ptr<Chunk>> jobs;
std::condition_variable jobsMutexCondition;
std::mutex jobsMutex;
bool working = true;
const ContentGfxCache* cache;
const EngineSettings& settings;
std::vector<std::unique_lock<std::mutex>> workersBlocked;
void threadLoop(int index);
void process(std::shared_ptr<Chunk> chunk, BlocksRenderer& renderer);
public:
ChunksRenderer(Level* level,
const ContentGfxCache* cache,
const EngineSettings& settings);
virtual ~ChunksRenderer();
std::shared_ptr<Mesh> render(std::shared_ptr<Chunk> chunk, bool important);
void unload(Chunk* chunk);
std::shared_ptr<Mesh> getOrRender(std::shared_ptr<Chunk> chunk, bool important);
std::shared_ptr<Mesh> get(Chunk* chunk);
void update();
};
#endif // SRC_GRAPHICS_CHUNKSRENDERER_H_
-207
View File
@@ -1,207 +0,0 @@
#include "Skybox.h"
#include <iostream>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include "../../assets/Assets.h"
#include "../../graphics/core/Shader.h"
#include "../../graphics/core/Mesh.h"
#include "../../graphics/core/Batch3D.h"
#include "../../graphics/core/Texture.h"
#include "../../graphics/core/Cubemap.h"
#include "../../graphics/core/Framebuffer.h"
#include "../../graphics/core/GfxContext.h"
#include "../../window/Window.h"
#include "../../window/Camera.h"
#ifndef M_PI
#define M_PI 3.141592
#endif // M_PI
const int STARS_COUNT = 3000;
const int STARS_SEED = 632;
Skybox::Skybox(uint size, Shader* shader)
: size(size),
shader(shader),
batch3d(std::make_unique<Batch3D>(4096))
{
auto cubemap = std::make_unique<Cubemap>(size, size, ImageFormat::rgb888);
uint fboid;
glGenFramebuffers(1, &fboid);
fbo = std::make_unique<Framebuffer>(fboid, 0, std::move(cubemap));
float vertices[] {
-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f
};
vattr attrs[] {{2}, {0}};
mesh = std::make_unique<Mesh>(vertices, 6, attrs);
sprites.push_back(skysprite {
"misc/moon",
M_PI*0.5f,
4.0f,
false
});
sprites.push_back(skysprite {
"misc/sun",
M_PI*1.5f,
4.0f,
true
});
}
Skybox::~Skybox() {
}
void Skybox::drawBackground(Camera* camera, Assets* assets, int width, int height) {
Shader* backShader = assets->getShader("background");
backShader->use();
backShader->uniformMatrix("u_view", camera->getView(false));
backShader->uniform1f("u_zoom", camera->zoom*camera->getFov()/(M_PI*0.5f));
backShader->uniform1f("u_ar", float(width)/float(height));
backShader->uniform1i("u_cubemap", 1);
bind();
mesh->draw();
unbind();
}
void Skybox::drawStars(float angle, float opacity) {
batch3d->texture(nullptr);
random.setSeed(STARS_SEED);
for (int i = 0; i < STARS_COUNT; i++) {
float rx = (random.randFloat()) - 0.5f;
float ry = (random.randFloat()) - 0.5f;
float z = (random.randFloat()) - 0.5f;
float x = rx * sin(angle) + ry * -cos(angle);
float y = rx * cos(angle) + ry * sin(angle);
float sopacity = random.randFloat();
if (y < 0.0f)
continue;
sopacity *= (0.2f+sqrt(cos(angle))*0.5) - 0.05;
glm::vec4 tint (1,1,1, sopacity * opacity);
batch3d->point(glm::vec3(x, y, z), tint);
}
batch3d->flushPoints();
}
void Skybox::draw(
const GfxContext& pctx,
Camera* camera,
Assets* assets,
float daytime,
float fog)
{
const Viewport& viewport = pctx.getViewport();
int width = viewport.getWidth();
int height = viewport.getHeight();
drawBackground(camera, assets, width, height);
GfxContext ctx = pctx.sub();
ctx.setBlendMode(blendmode::addition);
Shader* shader = assets->getShader("ui3d");
shader->use();
shader->uniformMatrix("u_projview", camera->getProjView(false));
shader->uniformMatrix("u_apply", glm::mat4(1.0f));
batch3d->begin();
float angle = daytime * M_PI * 2;
float opacity = glm::pow(1.0f-fog, 7.0f);
for (auto& sprite : sprites) {
batch3d->texture(assets->getTexture(sprite.texture));
float sangle = daytime * M_PI*2 + sprite.phase;
float distance = sprite.distance;
glm::vec3 pos(-cos(sangle)*distance, sin(sangle)*distance, 0);
glm::vec3 up(-sin(-sangle), cos(-sangle), 0.0f);
glm::vec4 tint (1,1,1, opacity);
if (!sprite.emissive) {
tint *= 0.6f+cos(angle)*0.4;
}
batch3d->sprite(pos, glm::vec3(0, 0, 1),
up, 1, 1, UVRegion(), tint);
}
drawStars(angle, opacity);
}
void Skybox::refresh(const GfxContext& pctx, float t, float mie, uint quality) {
GfxContext ctx = pctx.sub();
ctx.setDepthMask(false);
ctx.setDepthTest(false);
ctx.setFramebuffer(fbo.get());
ctx.setViewport(Viewport(size, size));
auto cubemap = dynamic_cast<Cubemap*>(fbo->getTexture());
ready = true;
glActiveTexture(GL_TEXTURE1);
cubemap->bind();
shader->use();
const glm::vec3 xaxs[] = {
{0.0f, 0.0f, -1.0f},
{0.0f, 0.0f, 1.0f},
{-1.0f, 0.0f, 0.0f},
{-1.0f, 0.0f, 0.0f},
{-1.0f, 0.0f, 0.0f},
{1.0f, 0.0f, 0.0f},
};
const glm::vec3 yaxs[] = {
{0.0f, 1.0f, 0.0f},
{0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, -1.0f},
{0.0f, 0.0f, 1.0f},
{0.0f, 1.0f, 0.0f},
{0.0f, 1.0f, 0.0f},
};
const glm::vec3 zaxs[] = {
{1.0f, 0.0f, 0.0f},
{-1.0f, 0.0f, 0.0f},
{0.0f, -1.0f, 0.0f},
{0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, -1.0f},
{0.0f, 0.0f, 1.0f},
};
t *= M_PI*2.0f;
shader->uniform1i("u_quality", quality);
shader->uniform1f("u_mie", mie);
shader->uniform1f("u_fog", mie - 1.0f);
shader->uniform3f("u_lightDir", glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f)));
for (uint face = 0; face < 6; face++) {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, cubemap->getId(), 0);
shader->uniform3f("u_xaxis", xaxs[face]);
shader->uniform3f("u_yaxis", yaxs[face]);
shader->uniform3f("u_zaxis", zaxs[face]);
mesh->draw();
}
cubemap->unbind();
glActiveTexture(GL_TEXTURE0);
}
void Skybox::bind() const {
glActiveTexture(GL_TEXTURE1);
fbo->getTexture()->bind();
glActiveTexture(GL_TEXTURE0);
}
void Skybox::unbind() const {
glActiveTexture(GL_TEXTURE1);
fbo->getTexture()->unbind();
glActiveTexture(GL_TEXTURE0);
}
-58
View File
@@ -1,58 +0,0 @@
#ifndef FRONTEND_GRAPHICS_SKYBOX_H_
#define FRONTEND_GRAPHICS_SKYBOX_H_
#include <memory>
#include <string>
#include <vector>
#include "../../typedefs.h"
#include "../../maths/fastmaths.h"
class Mesh;
class Shader;
class Assets;
class Camera;
class Batch3D;
class Framebuffer;
class GfxContext;
struct skysprite {
std::string texture;
float phase;
float distance;
bool emissive;
};
class Skybox {
std::unique_ptr<Framebuffer> fbo;
uint size;
Shader* shader;
bool ready = false;
FastRandom random;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Batch3D> batch3d;
std::vector<skysprite> sprites;
void drawStars(float angle, float opacity);
void drawBackground(Camera* camera, Assets* assets, int width, int height);
public:
Skybox(uint size, Shader* shader);
~Skybox();
void draw(
const GfxContext& pctx,
Camera* camera,
Assets* assets,
float daytime,
float fog
);
void refresh(const GfxContext& pctx, float t, float mie, uint quality);
void bind() const;
void unbind() const;
bool isReady() const {
return ready;
}
};
#endif // FRONTEND_GRAPHICS_SKYBOX_H_