Skip to content

Commit

Permalink
Merge pull request #61 from hotstreams/detached5
Browse files Browse the repository at this point in the history
Detached5
  • Loading branch information
hotstreams committed Jun 12, 2024
2 parents 72056e6 + 6914749 commit 59ac777
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 27 deletions.
1 change: 1 addition & 0 deletions include/limitless/instances/skeletal_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace Limitless {
* throws no_such_animation if not found
*/
SkeletalInstance& play(const std::string& name);
SkeletalInstance& play(uint32_t index);

/**
* Pauses current animation
Expand Down
2 changes: 1 addition & 1 deletion include/limitless/renderer/renderer_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Limitless {
/**
* Cascade shadow maps
*/
bool cascade_shadow_maps = false;
bool cascade_shadow_maps = true;
glm::uvec2 csm_resolution = {1024 * 4, 1024 * 4 };
uint8_t csm_split_count = 3; // [2; 4]

Expand Down
3 changes: 3 additions & 0 deletions shaders/pipeline/composite.frag
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ void main() {
color = texture(lightened, uv).rgb + bloom_color;

color = toneMapping(color, tone_mapping_exposure);
//
// float gamma = 2.2;
// color = pow(color, vec3(1.0 / gamma));
}
2 changes: 1 addition & 1 deletion shaders/terrain/terrain.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ vec3 StochasticTexture(vec2 uv, float index, sampler2DArray s)
void calculateTerrain(inout MaterialContext mctx) {
vec2 terrain_uv = (getVertexPosition().xz / 25.6) * 256.0;

vec3 variation = MacroContrast(MacroVariation());
// vec3 variation = MacroContrast(MacroVariation());

vec3 diffuse = StochasticTexture(terrain_uv, getVertexTileCurrent(), _terrain_diffuse_texture);
vec3 normal = StochasticTexture(terrain_uv, getVertexTileCurrent(), _terrain_normal_texture);
Expand Down
8 changes: 7 additions & 1 deletion src/limitless/core/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void Context::unregisterContext() noexcept {

void Limitless::swap(Context& lhs, Context& rhs) noexcept {
using std::swap;

/*
* we do NOT register nullptr GLFWWindow at Context default constructor (it is private, so GLFWWindow pointer gets nullptr)
* so have to check that it is present here
Expand All @@ -80,6 +79,13 @@ void Limitless::swap(Context& lhs, Context& rhs) noexcept {
swap(lhs.window, rhs.window);
swap(lhs.monitor, rhs.monitor);
swap(lhs.size, rhs.size);

swap(lhs.framebuffer_callback, rhs.framebuffer_callback);
swap(lhs.mouseclick_callback, rhs.mouseclick_callback);
swap(lhs.mousemove_callback, rhs.mousemove_callback);
swap(lhs.scroll_callback, rhs.scroll_callback);
swap(lhs.char_callback, rhs.char_callback);
swap(lhs.key_callback, rhs.key_callback);
}

Context::Context(Context&& rhs) noexcept : ContextState(std::move(rhs)) {
Expand Down
13 changes: 11 additions & 2 deletions src/limitless/instances/skeletal_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ void SkeletalInstance::update(Context& context, const Camera& camera) {
ModelInstance::update(context, camera);
}

SkeletalInstance &SkeletalInstance::play(uint32_t index) {
const auto& skeletal = dynamic_cast<SkeletalModel&>(*model);
const auto& animations = skeletal.getAnimations();

animation = &animations.at(index);
animation_duration = std::chrono::seconds(0);
last_time = std::chrono::time_point<std::chrono::steady_clock>();

return *this;
}

SkeletalInstance& SkeletalInstance::play(const std::string& name) {
const auto& skeletal = dynamic_cast<SkeletalModel&>(*model);
const auto& animations = skeletal.getAnimations();
Expand Down Expand Up @@ -213,5 +224,3 @@ const std::vector<Bone>& SkeletalInstance::getAllBones() const noexcept {
const auto& bones = skeletal.getBones();
return bones;
}


60 changes: 38 additions & 22 deletions src/limitless/loaders/gltf_model_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ loadMeshes(
const std::string& model_name,
size_t mesh_index,
const std::vector<std::shared_ptr<ms::Material>>& materials,
const cgltf_data& data
const cgltf_data& data,
const ModelLoaderFlags& flags
) {
auto base_mesh_name =
std::string(mesh.name ? mesh.name : generateMeshName(model_name, mesh_index));
Expand Down Expand Up @@ -387,10 +388,12 @@ loadMeshes(
+ std::to_string(attribute.data->component_type) + " for bone weights"};
}
break;

case cgltf_attribute_type_color: {
// copyFromAccessor<glm::vec4>(*attribute.data);
break;
}
default:
throw ModelLoadError {
"unsupported attribute type " + std::to_string(attribute.type)};
throw ModelLoadError {"unsupported attribute type " + std::to_string(attribute.type)};
break;
}
}
Expand Down Expand Up @@ -419,13 +422,18 @@ loadMeshes(
+ " and UVs"};
}


vertices.reserve(positions.size());
for (size_t i = 0; i < positions.size(); ++i) {
auto uv = flags.isPresent(Limitless::ModelLoaderOption::FlipUV) ? uvs[i] : glm::vec2(uvs[i].x, 1.0 - uvs[i].y);

vertices.emplace_back(VertexNormalTangent {
positions[i],
normals[i],
glm::vec3 {tangents[i]}, // TODO: handle tangent basis handedness in W.
uvs[i]});
// gltf 2.0 spec: uv origin in top left corner
// OpenGL uv origin in bottom left
uv});
}

if (!skin) {
Expand Down Expand Up @@ -655,7 +663,8 @@ static std::shared_ptr<ms::Material> loadMaterial(
const fs::path& base_path,
const cgltf_material& material,
const std::string& model_name,
size_t material_index
size_t material_index,
const ModelLoaderFlags& flags
) {
ms::Material::Builder builder = ms::Material::builder();
const auto material_name = model_name + (material.name
Expand All @@ -674,13 +683,19 @@ static std::shared_ptr<ms::Material> loadMaterial(
case cgltf_alpha_mode_blend:
builder.blending(ms::Blending::Translucent);
break;
case cgltf_alpha_mode_mask:
builder.blending(ms::Blending::Opaque);
//TODO: add to material built-in
builder.custom("alpha_cutoff", material.alpha_cutoff);
builder.fragment("if (mctx.diffuse.a <= alpha_cutoff) discard;");
break;
default:
throw ModelLoadError {"alpha mode " + std::to_string(material.alpha_mode) + " not supported"};
}

if (!material.has_pbr_metallic_roughness) {
throw ModelLoadError {"missing PBR metallic roughness"};
}
// if (!material.has_pbr_metallic_roughness) {
// throw ModelLoadError {"missing PBR metallic roughness"};
// }

auto bytesFromBase64 = [](const char* cstr) -> std::vector<uint8_t> {
auto decode_base64_char = [](char c) -> uint8_t {
Expand Down Expand Up @@ -907,13 +922,14 @@ static std::vector<std::shared_ptr<ms::Material>> loadMaterials(
Assets& assets,
const InstanceTypes& instance_types,
const fs::path& path,
const cgltf_data& src
const cgltf_data& src,
const ModelLoaderFlags& flags
) {
std::vector<std::shared_ptr<ms::Material>> materials;

for (size_t i = 0; i < src.materials_count; ++i) {
materials.emplace_back(loadMaterial(
assets, instance_types, path.parent_path(), src.materials[i], model_name, i
assets, instance_types, path.parent_path(), src.materials[i], model_name, i, flags
));
}

Expand Down Expand Up @@ -958,7 +974,7 @@ static void fixMissingMaterials(
}

static SkeletalModel* loadSkeletalModel(
Assets& assets, const fs::path& path, const cgltf_data& src, const std::string& model_name
Assets& assets, const fs::path& path, const cgltf_data& src, const std::string& model_name, const ModelLoaderFlags& flags
) {
std::vector<Bone> bones;
std::unordered_map<const cgltf_node*, size_t> bone_indice_map;
Expand Down Expand Up @@ -1021,7 +1037,7 @@ static SkeletalModel* loadSkeletalModel(
auto bone_indices_tree = makeBoneIndiceTrees(root_nodes, bone_map);

const InstanceTypes instance_types {InstanceType::Skeletal};
auto loaded_materials = loadMaterials(model_name, assets, instance_types, path, src);
auto loaded_materials = loadMaterials(model_name, assets, instance_types, path, src, flags);

std::vector<std::shared_ptr<AbstractMesh>> meshes;
std::vector<std::shared_ptr<ms::Material>> mesh_materials;
Expand All @@ -1031,7 +1047,7 @@ static SkeletalModel* loadSkeletalModel(

if (node.mesh) {
auto [more_meshes, more_mesh_materials] = loadMeshes(
node, *node.mesh, node.skin, model_name, meshes.size(), loaded_materials, src
node, *node.mesh, node.skin, model_name, meshes.size(), loaded_materials, src, flags
);
meshes.insert(meshes.end(), more_meshes.begin(), more_meshes.end());
mesh_materials.insert(
Expand Down Expand Up @@ -1059,20 +1075,20 @@ static SkeletalModel* loadSkeletalModel(
}

static Model* loadPlainModel(
Assets& assets, const fs::path& path, const cgltf_data& src, const std::string& model_name
Assets& assets, const fs::path& path, const cgltf_data& src, const std::string& model_name, const ModelLoaderFlags& flags
) {
std::vector<std::shared_ptr<AbstractMesh>> meshes;
std::vector<std::shared_ptr<ms::Material>> mesh_materials;
const InstanceTypes instance_types {InstanceType::Model};

auto loaded_materials = loadMaterials(model_name, assets, instance_types, path, src);
auto loaded_materials = loadMaterials(model_name, assets, instance_types, path, src, flags);

for (size_t i = 0; i < src.nodes_count; ++i) {
const cgltf_node& node = src.nodes[i];

if (node.mesh) {
auto [more_meshes, more_mesh_materials] = loadMeshes(
node, *node.mesh, nullptr, model_name, meshes.size(), loaded_materials, src
node, *node.mesh, nullptr, model_name, meshes.size(), loaded_materials, src, flags
);
meshes.insert(meshes.end(), more_meshes.begin(), more_meshes.end());
mesh_materials.insert(
Expand All @@ -1087,18 +1103,18 @@ static Model* loadPlainModel(
}

static std::shared_ptr<AbstractModel>
loadModel(Assets& assets, const fs::path& path, const cgltf_data& src) {
loadModel(Assets& assets, const fs::path& path, const cgltf_data& src, const ModelLoaderFlags& flags) {
auto model_name = path.stem().string();

if (src.skins_count > 0) {
return std::shared_ptr<AbstractModel>(loadSkeletalModel(assets, path, src, model_name));
return std::shared_ptr<AbstractModel>(loadSkeletalModel(assets, path, src, model_name, flags));
} else {
return std::shared_ptr<AbstractModel>(loadPlainModel(assets, path, src, model_name));
return std::shared_ptr<AbstractModel>(loadPlainModel(assets, path, src, model_name, flags));
}
}

std::shared_ptr<AbstractModel>
GltfModelLoader::loadModel(Assets& assets, const fs::path& path, [[maybe_unused]] const ModelLoaderFlags& flags) {
GltfModelLoader::loadModel(Assets& assets, const fs::path& path, const ModelLoaderFlags& flags) {
cgltf_options opts = cgltf_options {
cgltf_file_type_invalid, // autodetect
0, // auto json token count
Expand Down Expand Up @@ -1126,5 +1142,5 @@ GltfModelLoader::loadModel(Assets& assets, const fs::path& path, [[maybe_unused]
throw ModelLoadError {"no scene"};
}

return ::loadModel(assets, path, *out_data);
return ::loadModel(assets, path, *out_data, flags);
}
3 changes: 3 additions & 0 deletions src/limitless/pipeline/deferred/translucent_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdexcept>
#include <limitless/pipeline/pipeline.hpp>
#include <limitless/pipeline/deferred/deferred_lighting_pass.hpp>
#include <limitless/pipeline/deferred/deferred_framebuffer_pass.hpp>

using namespace Limitless;

Expand Down Expand Up @@ -72,4 +73,6 @@ std::shared_ptr<Texture> TranslucentPass::getResult() {

void TranslucentPass::onFramebufferChange(glm::uvec2 size) {
framebuffer.onFramebufferChange(size);

framebuffer << TextureAttachment{FramebufferAttachment::Depth, pipeline.get<DeferredFramebufferPass>().getDepth()};
}

0 comments on commit 59ac777

Please sign in to comment.