Skip to content

--savemesh argument in example 12 #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 4 additions & 1 deletion 12_MeshLoaders/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ endif()
# TODO; Arek I removed `NBL_EXECUTABLE_PROJECT_CREATION_PCH_TARGET` from the last parameter here, doesn't this macro have 4 arguments anyway !?
nbl_create_executable_project("" "" "${NBL_INCLUDE_SERACH_DIRECTORIES}" "${NBL_LIBRARIES}")
# TODO: Arek temporarily disabled cause I haven't figured out how to make this target yet
# LINK_BUILTIN_RESOURCES_TO_TARGET(${EXECUTABLE_NAME} nblExamplesGeometrySpirvBRD)
# LINK_BUILTIN_RESOURCES_TO_TARGET(${EXECUTABLE_NAME} nblExamplesGeometrySpirvBRD)

add_dependencies(${EXECUTABLE_NAME} argparse)
target_include_directories(${EXECUTABLE_NAME} PUBLIC $<TARGET_PROPERTY:argparse,INTERFACE_INCLUDE_DIRECTORIES>)
56 changes: 33 additions & 23 deletions 12_MeshLoaders/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#include "../3rdparty/argparse/include/argparse/argparse.hpp"
#include "argparse/argparse.hpp"
#include "common.hpp"

#include "../3rdparty/portable-file-dialogs/portable-file-dialogs.h"
Expand Down Expand Up @@ -46,7 +46,7 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
}

if (parser["--savemesh"] == true)
m_saveMeshOnExit = true;
m_saveGeomOnExit = true;

m_semaphore = m_device->createSemaphore(m_realFrameIx);
if (!m_semaphore)
Expand Down Expand Up @@ -197,22 +197,9 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc

inline bool onAppTerminated() override
{
if (m_saveMeshOnExit)
if (m_saveGeomOnExit)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check for current geom

{
// make save path
static const auto prefix = std::filesystem::absolute("saved/");

if (!std::filesystem::exists(prefix))
m_system->createDirectory(prefix);

auto savePath = (prefix / path(m_modelPath).filename()).generic_string();

m_logger->log("Saving mesh to %S", ILogger::ELL_INFO, savePath.c_str());
// TODO (Yas): learn how to get out the geometry from renderer and transform it into IAsset

auto& asset = m_currentBundle.getContents()[0];
IAssetWriter::SAssetWriteParams params{ asset.get() };
m_assetMgr->writeAsset(savePath, params);
writeGeometry();
}

if (!device_base_t::onAppTerminated())
Expand Down Expand Up @@ -286,6 +273,9 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
m_modelPath = file.result()[0];
}

if (m_saveGeomOnExit && m_currentGeom)
writeGeometry();

// free up
m_renderer->m_instances.clear();
m_renderer->clearGeometries({.semaphore=m_semaphore.get(),.value=m_realFrameIx});
Expand All @@ -294,16 +284,16 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
//! load the geometry
IAssetLoader::SAssetLoadParams params = {};
params.logger = m_logger.get();
m_currentBundle = m_assetMgr->getAsset(m_modelPath,params);
if (m_currentBundle.getContents().empty())
auto asset = m_assetMgr->getAsset(m_modelPath,params);
if (asset.getContents().empty())
return false;

//
core::vector<smart_refctd_ptr<const ICPUPolygonGeometry>> geometries;
switch (m_currentBundle.getAssetType())
switch (asset.getAssetType())
{
case IAsset::E_TYPE::ET_GEOMETRY:
for (const auto& item : m_currentBundle.getContents())
for (const auto& item : asset.getContents())
if (auto polyGeo=IAsset::castDown<ICPUPolygonGeometry>(item); polyGeo)
geometries.push_back(polyGeo);
break;
Expand All @@ -314,6 +304,8 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
if (geometries.empty())
return false;

m_currentGeom = geometries[0];

using aabb_t = hlsl::shapes::AABB<3,double>;
auto printAABB = [&](const aabb_t& aabb, const char* extraMsg="")->void
{
Expand Down Expand Up @@ -446,6 +438,24 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
return true;
}

void writeGeometry()
{
// make save path
static const auto prefix = std::filesystem::absolute("saved/");

if (!std::filesystem::exists(prefix))
m_system->createDirectory(prefix);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path is wrong, see the 4 CWD members of the apocalypse in the basic framework app


auto savePath = (prefix / path(m_modelPath).filename()).generic_string();

m_logger->log("Saving mesh to %S", ILogger::ELL_INFO, savePath.c_str());

// should I do a const cast here?
const IAsset* asset = m_currentGeom.get();
IAssetWriter::SAssetWriteParams params{ const_cast<IAsset*>(asset) };
m_assetMgr->writeAsset(savePath, params);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after writing out once, the `m_currentGeom needs to be nulled out

}

// Maximum frames which can be simultaneously submitted, used to cycle through our per-frame resources like command buffers
constexpr static inline uint32_t MaxFramesInFlight = 3u;
//
Expand All @@ -462,10 +472,10 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
// mutables
std::string m_modelPath;

SAssetBundle m_currentBundle;
smart_refctd_ptr<const ICPUPolygonGeometry> m_currentGeom;

std::string m_saveFileName; // NOTE: no extension
bool m_saveMeshOnExit;
bool m_saveGeomOnExit;
};

NBL_MAIN_FUNC(MeshLoadersApp)