initial commit
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
#include "frontend/ContentGfxCache.hpp"
|
||||
|
||||
const glm::vec3 BlocksRenderer::SUN_VECTOR(0.528265f, 0.833149f, -0.163704f);
|
||||
const float DIRECTIONAL_LIGHT_FACTOR = 0.3f;
|
||||
const float DIRECTIONAL_LIGHT_FACTOR = 0.2f;
|
||||
|
||||
BlocksRenderer::BlocksRenderer(
|
||||
size_t capacity,
|
||||
@@ -39,13 +39,17 @@ BlocksRenderer::~BlocksRenderer() {
|
||||
|
||||
/// Basic vertex add method
|
||||
void BlocksRenderer::vertex(
|
||||
const glm::vec3& coord, float u, float v, const glm::vec4& light
|
||||
const glm::vec3& coord, float u, float v, const glm::vec4& light, const glm::vec3& normal
|
||||
) {
|
||||
|
||||
vertexBuffer[vertexCount].position = coord;
|
||||
|
||||
vertexBuffer[vertexCount].uv = {u,v};
|
||||
|
||||
vertexBuffer[vertexCount].normal[0] = static_cast<uint8_t>(normal.r * 127 + 128);
|
||||
vertexBuffer[vertexCount].normal[1] = static_cast<uint8_t>(normal.g * 127 + 128);
|
||||
vertexBuffer[vertexCount].normal[2] = static_cast<uint8_t>(normal.b * 127 + 128);
|
||||
|
||||
vertexBuffer[vertexCount].color[0] = static_cast<uint8_t>(light.r * 255);
|
||||
vertexBuffer[vertexCount].color[1] = static_cast<uint8_t>(light.g * 255);
|
||||
vertexBuffer[vertexCount].color[2] = static_cast<uint8_t>(light.b * 255);
|
||||
@@ -82,10 +86,10 @@ void BlocksRenderer::face(
|
||||
auto Y = axisY * h;
|
||||
auto 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);
|
||||
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, lights[0] * tint, axisZ);
|
||||
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, lights[1] * tint, axisZ);
|
||||
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, lights[2] * tint, axisZ);
|
||||
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, lights[3] * tint, axisZ);
|
||||
index(0, 1, 3, 1, 2, 3);
|
||||
}
|
||||
|
||||
@@ -103,7 +107,7 @@ void BlocksRenderer::vertexAO(
|
||||
axisX,
|
||||
axisY
|
||||
);
|
||||
vertex(coord, u, v, light * tint);
|
||||
vertex(coord, u, v, light * tint, axisZ);
|
||||
}
|
||||
|
||||
void BlocksRenderer::faceAO(
|
||||
@@ -134,11 +138,12 @@ void BlocksRenderer::faceAO(
|
||||
vertexAO(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, axisX, axisY, axisZ);
|
||||
vertexAO(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, axisX, axisY, axisZ);
|
||||
} else {
|
||||
auto axisZ = glm::normalize(Z);
|
||||
glm::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);
|
||||
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, axisZ);
|
||||
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, axisZ);
|
||||
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, axisZ);
|
||||
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, axisZ);
|
||||
}
|
||||
index(0, 1, 2, 0, 2, 3);
|
||||
}
|
||||
@@ -163,10 +168,10 @@ void BlocksRenderer::face(
|
||||
d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR;
|
||||
tint *= d;
|
||||
}
|
||||
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);
|
||||
vertex(coord + (-X - Y + Z) * s, region.u1, region.v1, tint, Z);
|
||||
vertex(coord + ( X - Y + Z) * s, region.u2, region.v1, tint, Z);
|
||||
vertex(coord + ( X + Y + Z) * s, region.u2, region.v2, tint, Z);
|
||||
vertex(coord + (-X + Y + Z) * s, region.u1, region.v2, tint, Z);
|
||||
index(0, 1, 2, 0, 2, 3);
|
||||
}
|
||||
|
||||
@@ -337,7 +342,8 @@ void BlocksRenderer::blockCustomModel(
|
||||
coord + vcoord.x * X + vcoord.y * Y + vcoord.z * Z,
|
||||
vertex.uv.x,
|
||||
vertex.uv.y,
|
||||
glm::vec4(d, d, d, d) * aoColor
|
||||
glm::vec4(d, d, d, d) * aoColor,
|
||||
n
|
||||
);
|
||||
indexBuffer[indexCount++] = vertexOffset++;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ class BlocksRenderer {
|
||||
|
||||
SortingMeshData sortingMesh;
|
||||
|
||||
void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light);
|
||||
void vertex(const glm::vec3& coord, float u, float v, const glm::vec4& light, const glm::vec3& normal);
|
||||
void index(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f);
|
||||
|
||||
void vertexAO(
|
||||
|
||||
@@ -143,7 +143,7 @@ void ChunksRenderer::update() {
|
||||
}
|
||||
|
||||
const Mesh<ChunkVertex>* ChunksRenderer::retrieveChunk(
|
||||
size_t index, const Camera& camera, Shader& shader, bool culling
|
||||
size_t index, const Camera& camera, bool culling
|
||||
) {
|
||||
auto chunk = chunks.getChunks()[index];
|
||||
if (chunk == nullptr) {
|
||||
@@ -219,7 +219,7 @@ void ChunksRenderer::drawChunks(
|
||||
// TODO: minimize draw calls number
|
||||
for (int i = indices.size()-1; i >= 0; i--) {
|
||||
auto& chunk = chunks.getChunks()[indices[i].index];
|
||||
auto mesh = retrieveChunk(indices[i].index, camera, shader, culling);
|
||||
auto mesh = retrieveChunk(indices[i].index, camera, culling);
|
||||
|
||||
if (mesh) {
|
||||
glm::vec3 coord(
|
||||
|
||||
@@ -50,7 +50,7 @@ class ChunksRenderer {
|
||||
std::vector<ChunksSortEntry> indices;
|
||||
util::ThreadPool<std::shared_ptr<Chunk>, RendererResult> threadPool;
|
||||
const Mesh<ChunkVertex>* retrieveChunk(
|
||||
size_t index, const Camera& camera, Shader& shader, bool culling
|
||||
size_t index, const Camera& camera, bool culling
|
||||
);
|
||||
public:
|
||||
ChunksRenderer(
|
||||
|
||||
@@ -136,8 +136,8 @@ void Skybox::draw(
|
||||
batch3d->sprite(pos, glm::vec3(0, 0, 1),
|
||||
up, 1, 1, UVRegion(), tint);
|
||||
}
|
||||
|
||||
drawStars(angle, opacity);
|
||||
batch3d->flush();
|
||||
//drawStars(angle, opacity);
|
||||
}
|
||||
|
||||
void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality) {
|
||||
|
||||
@@ -41,6 +41,8 @@
|
||||
#include "graphics/core/Shader.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "graphics/core/Font.hpp"
|
||||
#include "graphics/core/ShadowMap.hpp"
|
||||
#include "graphics/core/GBuffer.hpp"
|
||||
#include "BlockWrapsRenderer.hpp"
|
||||
#include "ParticlesRenderer.hpp"
|
||||
#include "PrecipitationRenderer.hpp"
|
||||
@@ -100,6 +102,10 @@ WorldRenderer::WorldRenderer(
|
||||
settings.graphics.skyboxResolution.get(),
|
||||
assets->require<Shader>("skybox_gen")
|
||||
);
|
||||
|
||||
shadowMap = std::make_unique<ShadowMap>(1024);
|
||||
|
||||
shadowCamera = std::make_unique<Camera>();
|
||||
}
|
||||
|
||||
WorldRenderer::~WorldRenderer() = default;
|
||||
@@ -119,6 +125,7 @@ void WorldRenderer::setupWorldShader(
|
||||
shader.uniform1f("u_fogFactor", fogFactor);
|
||||
shader.uniform1f("u_fogCurve", settings.graphics.fogCurve.get());
|
||||
shader.uniform1i("u_debugLights", lightsDebug);
|
||||
shader.uniform1i("u_debugNormals", false);
|
||||
shader.uniform1f("u_weatherFogOpacity", weather.fogOpacity());
|
||||
shader.uniform1f("u_weatherFogDencity", weather.fogDencity());
|
||||
shader.uniform1f("u_weatherFogCurve", weather.fogCurve());
|
||||
@@ -126,6 +133,15 @@ void WorldRenderer::setupWorldShader(
|
||||
shader.uniform2f("u_lightDir", skybox->getLightDir());
|
||||
shader.uniform3f("u_cameraPos", camera.position);
|
||||
shader.uniform1i("u_cubemap", 1);
|
||||
shader.uniform1i("u_enableShadows", gbufferPipeline);
|
||||
|
||||
if (gbufferPipeline) {
|
||||
shader.uniformMatrix("u_shadowsMatrix", shadowCamera->getProjView());
|
||||
glActiveTexture(GL_TEXTURE4);
|
||||
shader.uniform1i("u_shadows", 4);
|
||||
glBindTexture(GL_TEXTURE_2D, shadowMap->getDepthMap());
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
|
||||
auto indices = level.content.getIndices();
|
||||
// Light emission when an emissive item is chosen
|
||||
@@ -354,10 +370,52 @@ void WorldRenderer::draw(
|
||||
|
||||
const auto& assets = *engine.getAssets();
|
||||
auto& linesShader = assets.require<Shader>("lines");
|
||||
auto& shadowsShader = assets.require<Shader>("shadows");
|
||||
|
||||
if (gbufferPipeline) {
|
||||
float shadowMapScale = 0.05f;
|
||||
float shadowMapSize = shadowMap->getResolution() * shadowMapScale;
|
||||
*shadowCamera = Camera(camera.position, shadowMapSize);
|
||||
shadowCamera->near = 0.5f;
|
||||
shadowCamera->far = 600.0f;
|
||||
shadowCamera->perspective = false;
|
||||
shadowCamera->setAspectRatio(1.0f);
|
||||
shadowCamera->rotate(glm::radians(-65.0f), glm::radians(-35.0f), glm::radians(-35.0f));
|
||||
//shadowCamera.perspective = false;
|
||||
/*shadowCamera.rotation = //glm::inverse(
|
||||
glm::lookAt({}, glm::normalize(shadowCamera.position-camera.position), glm::vec3(0, 1, 0));
|
||||
//);*/
|
||||
shadowCamera->updateVectors();
|
||||
//shadowCamera->position += camera.dir * shadowMapSize * 0.5f;
|
||||
shadowCamera->position -= shadowCamera->front * 100.0f;
|
||||
shadowCamera->position -= shadowCamera->right * (shadowMap->getResolution() * shadowMapScale) * 0.5f;
|
||||
shadowCamera->position -= shadowCamera->up * (shadowMap->getResolution() * shadowMapScale) * 0.5f;
|
||||
shadowCamera->position = glm::floor(shadowCamera->position);
|
||||
{
|
||||
frustumCulling->update(shadowCamera->getProjView());
|
||||
auto sctx = pctx.sub();
|
||||
sctx.setDepthTest(true);
|
||||
sctx.setCullFace(true);
|
||||
sctx.setViewport({shadowMap->getResolution(), shadowMap->getResolution()});
|
||||
shadowMap->bind();
|
||||
setupWorldShader(shadowsShader, *shadowCamera, settings, 0.0f);
|
||||
chunks->drawChunks(*shadowCamera, shadowsShader);
|
||||
shadowMap->unbind();
|
||||
}
|
||||
}
|
||||
|
||||
/* World render scope with diegetic HUD included */ {
|
||||
DrawContext wctx = pctx.sub();
|
||||
postProcessing.use(wctx);
|
||||
if (gbufferPipeline) {
|
||||
if (gbuffer == nullptr) {
|
||||
gbuffer = std::make_unique<GBuffer>(vp.x, vp.y);
|
||||
} else {
|
||||
gbuffer->resize(vp.x, vp.y);
|
||||
}
|
||||
wctx.setFramebuffer(gbuffer.get());
|
||||
} else {
|
||||
postProcessing.use(wctx);
|
||||
}
|
||||
|
||||
display::clearDepth();
|
||||
|
||||
@@ -370,25 +428,25 @@ void WorldRenderer::draw(
|
||||
ctx.setCullFace(true);
|
||||
renderLevel(ctx, camera, settings, uiDelta, pause, hudVisible);
|
||||
// Debug lines
|
||||
if (hudVisible) {
|
||||
if (debug) {
|
||||
guides->renderDebugLines(
|
||||
ctx, camera, *lineBatch, linesShader, showChunkBorders
|
||||
);
|
||||
}
|
||||
if (player.currentCamera == player.fpCamera) {
|
||||
renderHands(camera, delta);
|
||||
}
|
||||
}
|
||||
// if (hudVisible) {
|
||||
// if (debug) {
|
||||
// guides->renderDebugLines(
|
||||
// ctx, camera, *lineBatch, linesShader, showChunkBorders
|
||||
// );
|
||||
// }
|
||||
// if (player.currentCamera == player.fpCamera) {
|
||||
// renderHands(camera, delta);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
{
|
||||
DrawContext ctx = wctx.sub();
|
||||
texts->render(ctx, camera, settings, hudVisible, true);
|
||||
}
|
||||
renderBlockOverlay(wctx);
|
||||
// {
|
||||
// DrawContext ctx = wctx.sub();
|
||||
// texts->render(ctx, camera, settings, hudVisible, true);
|
||||
// }
|
||||
//renderBlockOverlay(wctx);
|
||||
}
|
||||
|
||||
postProcessing.render(pctx, assets, timer);
|
||||
postProcessing.render(pctx, assets, timer, camera, shadowMap->getDepthMap());
|
||||
}
|
||||
|
||||
void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {
|
||||
|
||||
@@ -30,6 +30,8 @@ class PostProcessing;
|
||||
class DrawContext;
|
||||
class ModelBatch;
|
||||
class Assets;
|
||||
class ShadowMap;
|
||||
class GBuffer;
|
||||
struct EngineSettings;
|
||||
|
||||
class WorldRenderer {
|
||||
@@ -44,12 +46,18 @@ class WorldRenderer {
|
||||
std::unique_ptr<GuidesRenderer> guides;
|
||||
std::unique_ptr<ChunksRenderer> chunks;
|
||||
std::unique_ptr<Skybox> skybox;
|
||||
std::unique_ptr<ShadowMap> shadowMap;
|
||||
Weather weather {};
|
||||
|
||||
std::unique_ptr<Camera> shadowCamera;
|
||||
|
||||
float timer = 0.0f;
|
||||
bool debug = false;
|
||||
bool lightsDebug = false;
|
||||
|
||||
std::unique_ptr<GBuffer> gbuffer;
|
||||
bool gbufferPipeline = false;
|
||||
|
||||
/// @brief Render block selection lines
|
||||
void renderBlockSelection();
|
||||
|
||||
|
||||
@@ -14,11 +14,13 @@ struct ChunkVertex {
|
||||
glm::vec3 position;
|
||||
glm::vec2 uv;
|
||||
std::array<uint8_t, 4> color;
|
||||
std::array<uint8_t, 4> normal;
|
||||
|
||||
static constexpr VertexAttribute ATTRIBUTES[] = {
|
||||
{VertexAttribute::Type::FLOAT, false, 3},
|
||||
{VertexAttribute::Type::FLOAT, false, 2},
|
||||
{VertexAttribute::Type::UNSIGNED_BYTE, true, 4},
|
||||
{VertexAttribute::Type::UNSIGNED_BYTE, true, 4},
|
||||
{{}, 0}};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user