Skip to content

Commit

Permalink
Refactor renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
uis246 committed Nov 26, 2020
1 parent af23271 commit edfefd1
Show file tree
Hide file tree
Showing 21 changed files with 334 additions and 367 deletions.
2 changes: 1 addition & 1 deletion cwd/assets/altcraft/shaders/face.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"MinLightLevel",
"GlobalTime"
]
}
}
50 changes: 23 additions & 27 deletions cwd/assets/altcraft/shaders/frag/face.fs
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
#version 330 core
precision lowp float;

in vec2 UvPosition;
flat in uint Layer;

in VS_OUT {
vec2 UvPosition;
vec3 Texture;
vec3 Color;
vec2 Light;
flat float Light;
flat vec3 Color;
} fs_in;

uniform sampler2DArray textureAtlas;
uniform float DayTime;
uniform float MinLightLevel;


vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

void main() {
vec4 color = texture(textureAtlas,fs_in.Texture);
if (color.a < 0.3)
discard;
vec4 color = texture(textureAtlas, vec3(UvPosition, Layer));
if (color.a < 0.3)
discard;

vec3 hsvColor = rgb2hsv(color.xyz);
hsvColor+=fs_in.Color;
vec3 hsvColor = rgb2hsv(color.xyz);
hsvColor+=fs_in.Color;
color = vec4(hsv2rgb(hsvColor),1);

float light = fs_in.Light.x / 15.0;
float skyLight = (fs_in.Light.y / 15.0) * DayTime;

float faceLight = clamp(light + skyLight,MinLightLevel,1.0);

color = vec4(color.rgb * faceLight, color.a);
color.rgb *= fs_in.Light;
gl_FragColor = color;
}
}
85 changes: 47 additions & 38 deletions cwd/assets/altcraft/shaders/vert/face.vs
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
#version 330 core
precision mediump float;
precision mediump int;

layout (location = 0) in vec3 position;
layout (location = 2) in vec2 UvCoordinates;
layout (location = 7) in vec4 Texture;
layout (location = 8) in mat4 model;
layout (location = 12) in vec3 color;
layout (location = 13) in vec2 light;
layout (location = 14) in float TextureLayer;
layout (location = 15) in float TextureFrames;
uniform float GlobalTime;
uniform float DayTime;
uniform float MinLightLevel;
uniform mat4 projView;

//Per quad info

//xx yy ww hh 4*2=8
//p h l f 4*1=4 //12
//T(L1){Uu} (L2){Vv} 2*2=4 //16

layout(location = 0) in uvec2 qinfo;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec3 positions[4];
//3
//4
//5
layout(location = 6) in uvec4 utex;
layout(location = 7) in uvec4 phlf;

out vec2 UvPosition;
flat out uint Layer;

out VS_OUT {
vec2 UvPosition;
vec3 Texture;
vec3 Color;
vec2 Light;
flat float Light;
flat vec3 Color;
} vs_out;

uniform float GlobalTime;
uniform mat4 projView;
//Intel SNB: VS vec4 shader: 55 instructions. 0 loops. 196 cycles. 0:0 spills:fills, 1 sends. Compacted 880 to 864 bytes (2%)

vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) {
float x = TextureAtlasCoords.x;
float y = TextureAtlasCoords.y;
// float w = TextureAtlasCoords.z;
float h = TextureAtlasCoords.w;
vec2 transformed = vec2(x, 1 - y - h) + UvCoords * TextureAtlasCoords.zw;
return vec3(transformed.x, transformed.y, Layer);
}
void main() {
gl_Position = projView * vec4(positions[gl_VertexID], 1.0);

vec4 subUV = vec4(
uvec4(qinfo, qinfo >> uint(5)) & uint(0x1F)
) / 16.0;

vec4 tex = vec4(utex) / 1024.0;
float frames = float(phlf.w);
tex.w /= frames;
tex.y += trunc(mod(GlobalTime * 4.0f, frames)) * tex.w;

tex.xy += subUV.xy * tex.zw;
tex.zw = (subUV.zw-subUV.xy) * tex.zw;

UvPosition = tex.xy + tex.zw*uv;
Layer = phlf.z;

void main()
{
vec4 sourcePosition = vec4(position,1.0f);
gl_Position = projView * model * sourcePosition;

vec4 texturePos = Texture;
float frameHeight = texturePos.w / TextureFrames;
float currentFrame = mod(GlobalTime * 4.0f, TextureFrames);
currentFrame = trunc(currentFrame);
texturePos.w = frameHeight;
texturePos.y = texturePos.y + currentFrame * frameHeight;

vs_out.UvPosition = UvCoordinates;
vs_out.Texture = TransformTextureCoord(texturePos,UvCoordinates,TextureLayer);
vs_out.Color = color;
vs_out.Light = light;
vec2 light = vec2((qinfo >> uint(10)) & uint(0xF)) / 15.0;
vs_out.Light = clamp(light.x + (light.y * DayTime), MinLightLevel, 1.0);
vs_out.Color = vec3(0.275, 0.63, 0.1) * (qinfo.x>>14);
}
61 changes: 26 additions & 35 deletions src/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void LoadTextures() {
data.data = std::move(textureAsset->textureData);
data.width = textureAsset->realWidth;
data.height = textureAsset->realHeight;
data.frames = textureAsset->frames;
textureData.push_back(data);
textureAsset->id = id++;
});
Expand Down Expand Up @@ -257,55 +258,45 @@ void ParseBlockModels() {
break;
}
parsedFace.transform = faceTransform;
TextureCoord texture;
unsigned int textureFrames = 1;
uint16_t textureId, frames;
textureName = face.second.texture;
if (model.Textures.empty()) {
texture = AssetManager::GetTexture("minecraft/textures/error");
}
else {
AssetTreeNode *node = AssetManager::GetAssetByAssetName("/minecraft/textures/error");
AssetTexture *assetTexture = nullptr;
if (node && node->type == AssetTreeNode::ASSET_TEXTURE)
assetTexture = reinterpret_cast<AssetTexture*>(node->asset.get());
textureId = assetTexture->id;
frames = assetTexture->frames;
} else {
while (textureName[0] == '#') {
textureName.erase(0, 1);
auto textureIt = model.Textures.find(textureName);
textureName = textureIt != model.Textures.end() ? textureIt->second : "minecraft/textures/error";
textureName = textureIt != model.Textures.end() ? textureIt->second : "error";
}
textureName.insert(0, "/minecraft/textures/");
AssetTreeNode *node = AssetManager::GetAssetByAssetName(textureName);
AssetTexture *assetTexture = nullptr;
if (node && node->type == AssetTreeNode::ASSET_TEXTURE)
assetTexture = reinterpret_cast<AssetTexture*>(node->asset.get());

texture = atlas->GetTexture(assetTexture->id);
textureFrames = assetTexture->frames;

if (!(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,16,0,16 }) && !(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,0,0,0 })
&& !(face.second.uv == BlockModel::ElementData::FaceData::Uv{ 0,0,16,16 })) {
double x = face.second.uv.x1;
double y = face.second.uv.x1;
double w = face.second.uv.x2 - face.second.uv.x1;
double h = face.second.uv.y2 - face.second.uv.y1;
x /= 16.0;
y /= 16.0;
w /= 16.0;
h /= 16.0;
double X = texture.x;
double Y = texture.y;
double W = texture.w;
double H = texture.h;

texture.x = X + x * W;
texture.y = Y + y * H;
texture.w = w * W;
texture.h = h * H;
}
textureId = assetTexture->id;
frames = assetTexture->frames;
}
parsedFace.texture = glm::vec4{ texture.x,texture.y,texture.w,texture.h };
parsedFace.layer = texture.layer;
parsedFace.frames = textureFrames;
TextureCoord tc = atlas->GetTexture(textureId);
parsedFace.x = tc.pixelX;
parsedFace.y = tc.pixelY;
parsedFace.w = tc.pixelW;
parsedFace.h = tc.pixelH;
parsedFace.layer = tc.layer;

parsedFace.frames = frames;
parsedFace.textureId = textureId;
parsedFace.Uu = (face.second.uv.x2<<5)|face.second.uv.x1;
parsedFace.Vv = (face.second.uv.y2<<5)|face.second.uv.y1;
if (face.second.tintIndex)
parsedFace.color = glm::vec3(0.275, 0.63, 0.1);
parsedFace.tint = true;
else
parsedFace.color = glm::vec3(0, 0, 0);
parsedFace.tint = false;

model.parsedFaces.push_back(parsedFace);
}
Expand Down Expand Up @@ -411,7 +402,7 @@ AssetTreeNode *AssetManager::GetAssetByAssetName(const std::string & assetName)
return node;
}

GLuint AssetManager::GetTextureAtlasId()
GLuint AssetManager::GetTextureAtlasId() noexcept
{
return atlas->GetRawTextureId();
}
Expand Down
11 changes: 6 additions & 5 deletions src/AssetManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ static const Vector FaceDirectionVector[] = {
struct ParsedFace {
FaceDirection visibility;
glm::mat4 transform;
glm::vec4 texture;
float layer;
float frames;
glm::vec3 color;
uint16_t textureId;
uint16_t Uu, Vv;//10-bit
uint16_t x, y, w, h;
uint8_t layer, frames;
bool tint;
};

struct BlockFaces {
Expand Down Expand Up @@ -197,7 +198,7 @@ namespace AssetManager {
Asset *GetAssetPtr(const std::string &assetName);
AssetTreeNode *GetAssetByAssetName(const std::string &assetName);

GLuint GetTextureAtlasId();
GLuint GetTextureAtlasId() noexcept;

TextureCoord GetTexture(const std::string &assetName);
}
3 changes: 1 addition & 2 deletions src/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ void Framebuffer::Activate() {

void Framebuffer::RenderTo(Framebuffer &target) {
OPTICK_EVENT();
glBindFramebuffer(GL_FRAMEBUFFER, target.fbo);
glViewport(0, 0, target.width, target.height);
target.Clear();
AssetTreeNode *fbo = AssetManager::GetAssetByAssetName("/altcraft/shaders/fbo");
if (fbo->type == AssetTreeNode::ASSET_SHADER)
reinterpret_cast<AssetShader*>(fbo->asset.get())->shader->Activate();
Expand Down
2 changes: 1 addition & 1 deletion src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void InitEvents() {
}
if (message[0] == '!')
message = message.substr(1);
auto packet = std::static_pointer_cast<Packet>(std::make_shared<PacketChatMessageSB>(message));
auto packet = std::static_pointer_cast<PacketSB>(std::make_shared<PacketChatMessageSB>(message));
PUSH_EVENT("SendPacket",packet);
});

Expand Down
2 changes: 0 additions & 2 deletions src/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ void GameState::UpdatePacket(std::shared_ptr<PacketCB> ptr) {
auto packet = std::static_pointer_cast<PacketEntityRelativeMove>(ptr);
Entity &entity = world.GetEntity(packet->EntityId);
entity.pos = entity.pos + Entity::DecodeDeltaPos(packet->DeltaX, packet->DeltaY, packet->DeltaZ);
if (entity.entityId != 0)
LOG(INFO) << "M: " << packet->EntityId;
break;
}

Expand Down
7 changes: 2 additions & 5 deletions src/ModLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,6 @@ void ModLoader::ParseAssetBlockModel(AssetTreeNode &node) noexcept {
nlohmann::json modelData = nlohmann::json::parse(node.data);
BlockModel model;

if (node.name == "button") {
int a = 15;
a++;
}

if (modelData.find("parent") != modelData.end()) {
std::string parentName = modelData["parent"].get<std::string>();
parentName = parentName.substr(parentName.find('/') + 1);
Expand Down Expand Up @@ -266,6 +261,8 @@ void ModLoader::ParseAssetBlockModel(AssetTreeNode &node) noexcept {
uv.x2 = face["uv"][2];
uv.y2 = face["uv"][3];
faceData.uv = uv;
} else {
faceData.uv = {0, 0, 16, 16};
}

FaceDirection cullface = FaceDirection::none;
Expand Down
Loading

0 comments on commit edfefd1

Please sign in to comment.