Skip to content

Commit

Permalink
[ChunkRenderer] Now using 'u_mipLevel' uniform to specify max mip level.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Oct 29, 2023
1 parent 87d5531 commit 8f20c78
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
3 changes: 2 additions & 1 deletion resources/shaders/game.fs.sc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ uniform vec4 u_renderDistance;
uniform vec4 u_fogColor;
uniform vec4 u_skyColor;
uniform vec4 u_sunlightIntensity;
uniform vec4 u_mipLevel;

// Get light color
vec4 light(vec4 position, vec4 normal, vec4 color, vec3 lightColor, vec4 lightPosition, float ambientIntensity, float diffuseIntensity) {
Expand Down Expand Up @@ -50,7 +51,7 @@ void main() {
// Get current pixel color and apply multiplier on grayscale textures
vec4 color = v_color0;
if (v_texcoord0.x > -0.99 && v_texcoord0.y > -0.99) {
color = texture2D(u_tex, v_texcoord0);
color = texture2DLod(u_tex, v_texcoord0, clamp(mix(u_mipLevel.x, u_mipLevel.y, v_dist / u_renderDistance.x), u_mipLevel.x, u_mipLevel.y));
if (blockFace > -1 && color.r == color.g && color.g == color.b) {
color *= v_color0;
}
Expand Down
18 changes: 13 additions & 5 deletions source/client/graphics/ChunkRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@
ChunkRenderer::ChunkRenderer(const TextureAtlas &textureAtlas) : m_textureAtlas(textureAtlas) {
m_renderDistance = bgfx::createUniform("u_renderDistance", bgfx::UniformType::Vec4);
m_fogColor = bgfx::createUniform("u_fogColor", bgfx::UniformType::Vec4);
m_mipLevel = bgfx::createUniform("u_mipLevel", bgfx::UniformType::Vec4);
}

ChunkRenderer::~ChunkRenderer() {
if (bgfx::isValid(m_mipLevel)) {
bgfx::destroy(m_mipLevel);
m_mipLevel.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_fogColor)) {
bgfx::destroy(m_fogColor);
m_fogColor.idx = bgfx::kInvalidHandle;
Expand Down Expand Up @@ -306,11 +312,11 @@ void ChunkRenderer::drawChunks(RenderTarget &target, RenderStates states, const
}

for (u8 layer = 0 ; layer < ChunkMeshLayer::Count ; ++layer) {
#ifdef OM_NOT_IMPLEMENTED_GL_TEXTURE
// Disable mipmaps for specific layers
glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL,
(layer == ChunkMeshLayer::NoMipMap || layer == ChunkMeshLayer::Flora) ? 0 : Config::mipmapLevels));
#endif // OM_NOT_IMPLEMENTED_GL_TEXTURE
float mipLevel[4] = {0.f,
(layer == ChunkMeshLayer::NoMipMap || layer == ChunkMeshLayer::Flora)
? 0.f : (float)Config::mipmapLevels,
0.f, 0.f
};

if (layer == ChunkMeshLayer::Flora || layer == ChunkMeshLayer::Liquid)
states.isCullFaceEnabled = false;
Expand All @@ -321,6 +327,8 @@ void ChunkRenderer::drawChunks(RenderTarget &target, RenderStates states, const
std::size_t verticesCount = std::get<0>(it)->getVerticesCount(layer);
if (verticesCount == 0) continue;

bgfx::setUniform(m_mipLevel, mipLevel);

target.beginDrawing(states);

bgfx::setTransform(std::get<1>(it).getRawMatrix());
Expand Down
1 change: 1 addition & 0 deletions source/client/graphics/ChunkRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ChunkRenderer {

bgfx::UniformHandle m_renderDistance = BGFX_INVALID_HANDLE;
bgfx::UniformHandle m_fogColor = BGFX_INVALID_HANDLE;
bgfx::UniformHandle m_mipLevel = BGFX_INVALID_HANDLE;
};

#endif // CHUNKRENDERER_HPP_

0 comments on commit 8f20c78

Please sign in to comment.