Skip to content

Commit

Permalink
Shaders don't use blockID anymore. Fixes #48.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Feb 19, 2020
1 parent a7ecdf1 commit cb356b4
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 77 deletions.
21 changes: 10 additions & 11 deletions client/source/gui/InventoryCube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,21 @@ void InventoryCube::updateVertexBuffer(const Block &block) {
blockTexCoords.x + blockTexCoords.width, blockTexCoords.y,
blockTexCoords.x, blockTexCoords.y
};
for(u8 j = 0 ; j < 4 ; j++) {
vertices[j + i * 4].texCoord[0] = faceTexCoords[j * 2];
vertices[j + i * 4].texCoord[1] = faceTexCoords[j * 2 + 1];

for(u8 j = 0 ; j < 4 ; j++) {
vertices[j + i * 4].coord3d[0] = vertices[j + i * 4].coord3d[0] * block.boundingBox().width + block.boundingBox().x;
vertices[j + i * 4].coord3d[1] = vertices[j + i * 4].coord3d[1] * block.boundingBox().height + block.boundingBox().y;
vertices[j + i * 4].coord3d[2] = vertices[j + i * 4].coord3d[2] * block.boundingBox().depth + block.boundingBox().z;
}
}

gk::Color color = gk::Color::White;
for (u8 i = 0 ; i < 6 * 4 ; ++i) {
vertices[i].color[0] = color.r;
vertices[i].color[1] = color.g;
vertices[i].color[2] = color.b;
vertices[i].color[3] = color.a;
vertices[j + i * 4].texCoord[0] = faceTexCoords[j * 2];
vertices[j + i * 4].texCoord[1] = faceTexCoords[j * 2 + 1];

const gk::Color &colorMultiplier = block.colorMultiplier();
vertices[j + i * 4].color[0] = colorMultiplier.r;
vertices[j + i * 4].color[1] = colorMultiplier.g;
vertices[j + i * 4].color[2] = colorMultiplier.b;
vertices[j + i * 4].color[3] = colorMultiplier.a;
}
}

gk::VertexBuffer::bind(&m_vbo);
Expand Down
7 changes: 4 additions & 3 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ void GameState::initShaders() {
}

void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
gk::Shader::bind(&m_shader);
m_shader.setUniform("u_time", gk::GameClock::getTicks());
gk::Shader::bind(nullptr);
// FIXME: This uniform is not used anymore since water/leaves effects are disabled
// gk::Shader::bind(&m_shader);
// m_shader.setUniform("u_time", gk::GameClock::getTicks());
// gk::Shader::bind(nullptr);

states.shader = &m_shader;

Expand Down
22 changes: 10 additions & 12 deletions client/source/world/ChunkBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,11 @@ inline void ChunkBuilder::addFace(u8 x, u8 y, u8 z, u8 i, const ClientChunk &chu
vertices[j].normal[1] = normal.y;
vertices[j].normal[2] = normal.z;

vertices[j].color[0] = 1.0;
vertices[j].color[1] = 1.0;
vertices[j].color[2] = 1.0;
vertices[j].color[3] = 1.0;
const gk::Color colorMultiplier = block->colorMultiplier();
vertices[j].color[0] = colorMultiplier.r;
vertices[j].color[1] = colorMultiplier.g;
vertices[j].color[2] = colorMultiplier.b;
vertices[j].color[3] = colorMultiplier.a;

vertices[j].texCoord[0] = faceTexCoords[j * 2];
vertices[j].texCoord[1] = faceTexCoords[j * 2 + 1];
Expand All @@ -206,8 +207,6 @@ inline void ChunkBuilder::addFace(u8 x, u8 y, u8 z, u8 i, const ClientChunk &chu
surroundingBlockPos[0], surroundingBlockPos[1], surroundingBlockPos[2]);

vertices[j].ambientOcclusion = getAmbientOcclusion(x, y, z, i, j, chunk);

vertices[j].blockType = block->id();
}

auto addVertex = [&](u8 j) {
Expand Down Expand Up @@ -262,10 +261,11 @@ inline void ChunkBuilder::addCross(u8 x, u8 y, u8 z, const ClientChunk &chunk, c
vertices[j].normal[1] = normal.y;
vertices[j].normal[2] = normal.z;

vertices[j].color[0] = 1.0;
vertices[j].color[1] = 1.0;
vertices[j].color[2] = 1.0;
vertices[j].color[3] = 1.0;
const gk::Color colorMultiplier = block->colorMultiplier();
vertices[j].color[0] = colorMultiplier.r;
vertices[j].color[1] = colorMultiplier.g;
vertices[j].color[2] = colorMultiplier.b;
vertices[j].color[3] = colorMultiplier.a;

vertices[j].texCoord[0] = faceTexCoords[j * 2];
vertices[j].texCoord[1] = faceTexCoords[j * 2 + 1];
Expand All @@ -274,8 +274,6 @@ inline void ChunkBuilder::addCross(u8 x, u8 y, u8 z, const ClientChunk &chunk, c
vertices[j].lightValue[1] = chunk.lightmap().getTorchlight(x, y, z);

vertices[j].ambientOcclusion = 5;

vertices[j].blockType = block->id();
}

m_vertices[Layer::Flora].emplace_back(vertices[0]);
Expand Down
6 changes: 6 additions & 0 deletions common/include/world/Block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <gk/core/Box.hpp>
#include <gk/core/IntTypes.hpp>
#include <gk/graphics/Color.hpp>

#include "ItemStack.hpp"
#include "ISerializable.hpp"
Expand Down Expand Up @@ -93,6 +94,9 @@ class Block : public ISerializable {
bool isLightSource() const { return m_isLightSource; }
void setLightSource(bool isLightSource) { m_isLightSource = isLightSource; }

const gk::Color &colorMultiplier() const { return m_colorMultiplier; }
void setColorMultiplier(const gk::Color &colorMultiplier) { m_colorMultiplier = colorMultiplier; }

protected:
glm::vec4 getTexCoordsFromID(int textureID) const;

Expand All @@ -119,6 +123,8 @@ class Block : public ISerializable {
bool m_isOpaque = true;

bool m_isLightSource = false;

gk::Color m_colorMultiplier = gk::Color::White;
};

#endif // BLOCK_HPP_
6 changes: 4 additions & 2 deletions common/source/world/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void Block::serialize(sf::Packet &packet) const {
<< m_hardness << m_harvestRequirements << m_itemDrop << m_itemDropAmount << m_tiles
<< m_boundingBox.x << m_boundingBox.y << m_boundingBox.z
<< m_boundingBox.width << m_boundingBox.height << m_boundingBox.depth
<< m_isLightSource << m_canUpdate << m_canBeActivated;
<< m_isLightSource << m_canUpdate << m_canBeActivated
<< m_colorMultiplier.r << m_colorMultiplier.g << m_colorMultiplier.b << m_colorMultiplier.a;
}

void Block::deserialize(sf::Packet &packet) {
Expand All @@ -55,7 +56,8 @@ void Block::deserialize(sf::Packet &packet) {
>> m_harvestRequirements >> m_itemDrop >> m_itemDropAmount >> m_tiles
>> m_boundingBox.x >> m_boundingBox.y >> m_boundingBox.z
>> m_boundingBox.width >> m_boundingBox.height >> m_boundingBox.depth
>> m_isLightSource >> m_canUpdate >> m_canBeActivated;
>> m_isLightSource >> m_canUpdate >> m_canBeActivated
>> m_colorMultiplier.r >> m_colorMultiplier.g >> m_colorMultiplier.b >> m_colorMultiplier.a;

m_id = id;
m_drawType = BlockDrawType(drawType);
Expand Down
4 changes: 4 additions & 0 deletions mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ mod:block {
id = "grass",
name = "Grass",
tiles = {"grass_block_top.png", "dirt.png", "grass_block_side.png"},
color_multiplier = {129, 191, 91, 255},

item_drop = {
id = mod:id()..":dirt",
amount = 1
Expand All @@ -49,6 +51,7 @@ mod:block {
id = "leaves",
name = "Leaves",
tiles = "oak_leaves.png",
color_multiplier = {106, 173, 51, 255},
hardness = 0.5,
draw_type = 2, -- FIXME: Use string instead
-- is_opaque = false, -- FIXME
Expand Down Expand Up @@ -83,6 +86,7 @@ mod:block {
id = "water",
name = "Water",
tiles = "water.png",
color_multiplier = {51, 115, 255, 217},
draw_type = 3, -- FIXME: Use string instead
is_opaque = false,
}
Expand Down
10 changes: 7 additions & 3 deletions resources/shaders/basic.f.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#version 120

varying vec4 v_color;
varying vec4 v_coord3d;
varying vec2 v_texCoord;
varying vec4 v_color;
varying float v_faceValue;

uniform sampler2D u_tex;
Expand All @@ -12,8 +13,11 @@ void main() {
vec4 color = v_color;

if (v_texCoord.x != -1 && v_texCoord.y != -1) {
vec4 texColor = texture2D(u_tex, v_texCoord);
color = vec4(texColor.rgb - (1 - color.rgb), min(texColor.a, color.a));
color = texture2D(u_tex, v_texCoord);

if (color.r == color.g && color.g == color.b) {
color *= v_color;
}
}

// if (u_renderType == -1) {
Expand Down
4 changes: 2 additions & 2 deletions resources/shaders/basic.v.glsl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#version 120

attribute vec4 color;
attribute vec4 coord3d;
attribute vec2 texCoord;
attribute vec4 color;

varying vec4 v_color;
varying vec4 v_coord3d;
varying vec2 v_texCoord;
varying vec4 v_color;
varying float v_faceValue;

uniform mat4 u_modelMatrix;
Expand Down
10 changes: 2 additions & 8 deletions resources/shaders/color.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
varying vec4 v_color;
varying vec2 v_texCoord;

varying float v_blockID;

uniform sampler2D u_tex;

vec4 getColor() {
vec4 color = v_color;

// if(color == vec4(0, 0, 0, 1)) {
if (v_texCoord.x != -1 && v_texCoord.y != -1) {
vec4 texColor = texture2D(u_tex, v_texCoord);
color = vec4(texColor.rgb - (1 - color.rgb), min(texColor.a, color.a));
return texture2D(u_tex, v_texCoord);
}

return color;
return v_color;
}
24 changes: 6 additions & 18 deletions resources/shaders/game.f.glsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#version 120

varying vec4 v_coord3d;
varying vec4 v_color;
varying vec2 v_lightValue;
varying float v_ambientOcclusion;

varying float v_blockFace;
varying float v_blockID;
varying float v_dist;

uniform int u_renderDistance;
Expand All @@ -22,32 +22,20 @@ vec4 fog(vec4 color, float fogCoord, float fogStart, float fogEnd);
void main() {
// Needed to prevent bad interpolation on some systems
// Refer to #23 for more informations
float blockID = floor(v_blockID + 0.5);
float blockFace = floor(v_blockFace + 0.5);
float lightCheck = floor(v_lightValue.x + 0.5);

// Discard if the pixel is too far away
if(blockID != -1. && v_dist > u_renderDistance) discard;
if(v_blockFace != -1. && v_dist > u_renderDistance) discard;

// Get current pixel color and apply multiplier on grayscale textures
vec4 color = getColor();
if (blockID == 8.) { // Water
color.a = 0.85;
color += vec4(-0.8, -0.4, 0.2, 0);
}
else if (blockID == 4.) { // Leaves
color += vec4(-0.5, -0.15, -0.4, 0);
/* if (v_dist > 20 && color.a == 0) */
/* color.a = 0.5; */
}
else if (blockID == 3.) { // Grass
if (color.r == color.g && color.r == color.b) {
color += vec4(-0.3, -0.1, -0.25, 0);
/* color += vec4(-0.4, -0.15, -0.3, 0); */
}
if (v_blockFace != -1 && color != v_color && color.r == color.g && color.g == color.b) {
color *= v_color;
}

// Very cheap "transparency": don't draw pixels with a low alpha value
if(color.a < 0.3 && blockID != -1.) discard;
if(color.a < 0.3 && v_blockFace != -1.) discard;

// FIXME: FINISH THIS WITH PROPER CODE AND SUN BASIC DISPLAY
// int maxTime = 5 * 1000;
Expand Down
36 changes: 18 additions & 18 deletions resources/shaders/game.v.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,44 @@ attribute vec4 coord3d;
attribute vec3 normal;
attribute vec2 texCoord;
attribute vec2 lightValue;
attribute float blockType;
attribute float ambientOcclusion;

varying vec4 v_color;
varying vec4 v_coord3d;
varying vec4 v_color;
varying vec4 v_normal;
varying vec2 v_texCoord;
varying vec2 v_lightValue;
varying float v_ambientOcclusion;

varying float v_blockFace;
varying float v_blockID;
varying float v_dist;

uniform mat4 u_modelMatrix;
uniform mat4 u_projectionMatrix;
uniform mat4 u_viewMatrix;

uniform int u_renderDistance;
uniform int u_time;
// uniform int u_time;

void main() {
vec3 finalPos = coord3d.xyz;
if (blockType == 8) { // Water
// finalPos.y += sin((u_time / 1000.0 + mod(finalPos.x, 16)) * 1.75) / 10.0;
// finalPos.y += cos((u_time / 1000.0 + mod(finalPos.z, 16)) * 1.75) / 10.0;
// finalPos.y -= 0.25;
// finalPos.y -= 1.0 / 16.0;
finalPos.y += sin(u_time / 1000.0) / 16.0 - 0.125;
}
else if (blockType == 4) { // Leaves
finalPos.xz += sin((u_time / 1000.0 + finalPos.x) * 2) / 30.0;
finalPos.xz += cos((u_time / 1000.0 + finalPos.z) * 2) / 30.0;
}
// FIXME: This code won't work anymore because of 'blockType' attibute removal
// One way to get those effects back would be to add another attribute 'effect'
// with values: 'NONE', 'LIQUID', 'LEAVES'
// vec3 finalPos = coord3d.xyz;
// if (blockType == 8) { // Water
// // finalPos.y += sin((u_time / 1000.0 + mod(finalPos.x, 16)) * 1.75) / 10.0;
// // finalPos.y += cos((u_time / 1000.0 + mod(finalPos.z, 16)) * 1.75) / 10.0;
// // finalPos.y -= 0.25;
// // finalPos.y -= 1.0 / 16.0;
// finalPos.y += sin(u_time / 1000.0) / 16.0 - 0.125;
// }
// else if (blockType == 4) { // Leaves
// finalPos.xz += sin((u_time / 1000.0 + finalPos.x) * 2) / 30.0;
// finalPos.xz += cos((u_time / 1000.0 + finalPos.z) * 2) / 30.0;
// }

// Used for lighting
v_coord3d = u_modelMatrix * vec4(finalPos, 1.0);
v_coord3d = u_modelMatrix * vec4(coord3d.xyz, 1.0);
v_normal = vec4(normal, 1.0);

v_color = color;
Expand All @@ -56,7 +57,6 @@ void main() {
}

v_blockFace = coord3d.w;
v_blockID = blockType;

// Distance from eye
v_dist = length(u_viewMatrix * v_coord3d);
Expand Down
10 changes: 10 additions & 0 deletions server/source/lua/LuaMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ void LuaMod::registerBlock(const sol::table &table) {
block.setItemDrop(dropID, dropAmount);
}

sol::optional<sol::table> colorMultiplier = table["color_multiplier"];
if (colorMultiplier != sol::nullopt) {
block.setColorMultiplier(gk::Color{
colorMultiplier.value().get<u8>(1),
colorMultiplier.value().get<u8>(2),
colorMultiplier.value().get<u8>(3),
colorMultiplier.value().get<u8>(4)
});
}

Registry::getInstance().registerItem(block.tiles(), stringID, label).setIsBlock(true);
}

Expand Down

0 comments on commit cb356b4

Please sign in to comment.