483 lines
12 KiB
C++
Executable File
483 lines
12 KiB
C++
Executable File
|
|
/*
|
|
*#include "viewer/instance.hpp"
|
|
#include "util/logger.hpp"
|
|
#include <condition_variable>
|
|
#include <thread>
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
|
|
#include "util/string_lookup.hpp"
|
|
#include "viewer/asset_loader.hpp"
|
|
#include "viewer/asset_types.hpp"
|
|
#include "viewer/dynamic_shader_program_loading.hpp"
|
|
#include "util/logger.hpp"
|
|
void controller_task(
|
|
int args_count, char* args[],
|
|
viewer::instance* z3d,
|
|
std::mutex* gl_resource_lock,
|
|
std::mutex* progress_lock,
|
|
std::string* progress_title,
|
|
float* progress_ratio
|
|
) {
|
|
|
|
using namespace std::chrono_literals;
|
|
std::this_thread::sleep_for(0.2s);
|
|
|
|
using loader_type = viewer::asset_loader;
|
|
|
|
auto loader = loader_type{};
|
|
|
|
|
|
auto default_material = dynamic_material_data{};
|
|
std::ignore = default_material.initialized_surface_properties(); // TODO ...
|
|
|
|
constexpr auto enabled_mesh_components = (
|
|
mesh_vertex_components::flags::position |
|
|
mesh_vertex_components::flags::tex_coord |
|
|
mesh_vertex_components::flags::normal |
|
|
mesh_vertex_components::flags::color |
|
|
mesh_vertex_components::flags::reflectance
|
|
);
|
|
|
|
constexpr auto enabled_material_components = (
|
|
material_component::flags::texture |
|
|
material_component::flags::surface_properties |
|
|
material_component::flags::transparency
|
|
);
|
|
|
|
// TODO Add point cloud component selection to loaders or remove it all together
|
|
constexpr auto enabled_point_cloud_components = (
|
|
point_cloud_vertex_components::flags::position |
|
|
point_cloud_vertex_components::flags::normal |
|
|
point_cloud_vertex_components::flags::color |
|
|
point_cloud_vertex_components::flags::reflectance
|
|
);
|
|
if (const auto e = loader.init(
|
|
enabled_mesh_components,
|
|
enabled_material_components,
|
|
enabled_point_cloud_components,
|
|
default_material
|
|
)) {
|
|
ztu::logger::error(
|
|
"Error while initializing resource loader: [%] %",
|
|
e.category().name(),
|
|
e.message()
|
|
);
|
|
return;
|
|
}
|
|
|
|
viewer::dynamic_shader_program_loading::load_directory(
|
|
loader,
|
|
*z3d,
|
|
*gl_resource_lock,
|
|
*progress_lock,
|
|
*progress_title,
|
|
*progress_ratio,
|
|
std::filesystem::current_path() / ".." / "shaders"
|
|
);
|
|
|
|
progress_lock->lock();
|
|
*progress_title = "Assigning shaders...";
|
|
*progress_ratio = 0.9f;
|
|
progress_lock->unlock();
|
|
|
|
progress_lock->lock();
|
|
*progress_title = "Starting...";
|
|
*progress_ratio = 1.0f;
|
|
progress_lock->unlock();
|
|
|
|
progress_lock->lock();
|
|
*progress_ratio = std::numeric_limits<float>::max();
|
|
progress_lock->unlock();
|
|
|
|
|
|
std::vector<viewer::instance::asset_id> asset_ids;
|
|
auto dynamic_mesh_handles = std::vector<std::pair<
|
|
loader_type::dynamic_mesh_handle_type,
|
|
loader_type::dynamic_material_handle_type
|
|
>>{};
|
|
|
|
auto dynamic_point_cloud_handles = std::vector<loader_type::dynamic_point_cloud_handle_type>{};
|
|
|
|
for (int i = 1; i + 2 <= args_count; i += 2)
|
|
{
|
|
|
|
auto format = std::string{ args[i] };
|
|
auto filename = std::filesystem::path{ args[i + 1] };
|
|
|
|
if (const auto e = loader.load_asset(
|
|
format, filename, dynamic_mesh_handles, dynamic_point_cloud_handles
|
|
)) {
|
|
ztu::logger::error(
|
|
"Error while loading file %: [%] %",
|
|
filename,
|
|
e.category().name(),
|
|
e.message()
|
|
);
|
|
}
|
|
|
|
for (const auto& [ dynamic_mesh_handle, dynamic_material_handle ] : dynamic_mesh_handles)
|
|
{
|
|
const auto& [ mesh_handle, bounding_box, mesh_components ] = dynamic_mesh_handle;
|
|
const auto& [ material_handle, material_components ] = dynamic_material_handle;
|
|
|
|
ztu::logger::debug("mesh components: %", static_cast<unsigned int>(mesh_components));
|
|
|
|
ztu::logger::debug("material components: %", static_cast<unsigned int>(material_components));
|
|
|
|
gl_resource_lock->lock();
|
|
const auto asset_id = z3d->add_mesh(
|
|
mesh_handle, bounding_box, mesh_components,
|
|
material_handle, material_components
|
|
);
|
|
gl_resource_lock->unlock();
|
|
ztu::logger::debug("Added mesh to z3d");
|
|
|
|
if (asset_id)
|
|
{
|
|
asset_ids.push_back(*asset_id);
|
|
}
|
|
else
|
|
{
|
|
ztu::logger::warn(
|
|
"Ignored mesh as its layout is not supported by z3d."
|
|
);
|
|
}
|
|
}
|
|
|
|
for (const auto& [ point_cloud_handle, bounding_box, point_cloud_components ] : dynamic_point_cloud_handles)
|
|
{
|
|
gl_resource_lock->lock();
|
|
const auto asset_id = z3d->add_point_cloud(
|
|
point_cloud_handle, bounding_box, point_cloud_components
|
|
);
|
|
gl_resource_lock->unlock();
|
|
ztu::logger::debug("Added poitn cloud to z3d");
|
|
|
|
if (asset_id)
|
|
{
|
|
asset_ids.push_back(*asset_id);
|
|
}
|
|
else
|
|
{
|
|
ztu::logger::warn(
|
|
"Ignored Point cloud, as its layout is not supported by z3d."
|
|
);
|
|
}
|
|
}
|
|
|
|
dynamic_mesh_handles.clear();
|
|
dynamic_point_cloud_handles.clear();
|
|
}
|
|
|
|
if (not asset_ids.empty())
|
|
{
|
|
// TODO resource lock does not help with update
|
|
gl_resource_lock->lock();
|
|
z3d->look_at(asset_ids.front());
|
|
gl_resource_lock->unlock();
|
|
}
|
|
|
|
// TODO fix
|
|
std::this_thread::sleep_for(20h);
|
|
}
|
|
|
|
|
|
void controller_task(
|
|
int args_count, char* args[],
|
|
viewer::instance* z3d,
|
|
viewer::asset_loader& loader,
|
|
std::mutex* gl_resource_lock,
|
|
std::mutex* progress_lock,
|
|
std::string* progress_title,
|
|
float* progress_ratio
|
|
) {
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
auto default_material = dynamic_material_data{};
|
|
std::ignore = default_material.initialized_surface_properties(); // TODO ...
|
|
|
|
constexpr auto enabled_mesh_components = (
|
|
mesh_vertex_components::flags::position |
|
|
mesh_vertex_components::flags::tex_coord |
|
|
mesh_vertex_components::flags::normal |
|
|
mesh_vertex_components::flags::color |
|
|
mesh_vertex_components::flags::reflectance
|
|
);
|
|
|
|
constexpr auto enabled_material_components = (
|
|
material_component::flags::texture |
|
|
material_component::flags::surface_properties |
|
|
material_component::flags::transparency
|
|
);
|
|
|
|
// TODO Add point cloud component selection to loaders or remove it all together
|
|
constexpr auto enabled_point_cloud_components = (
|
|
point_cloud_vertex_components::flags::position |
|
|
point_cloud_vertex_components::flags::normal |
|
|
point_cloud_vertex_components::flags::color |
|
|
point_cloud_vertex_components::flags::reflectance
|
|
);
|
|
if (const auto e = loader.init(
|
|
enabled_mesh_components,
|
|
enabled_material_components,
|
|
enabled_point_cloud_components,
|
|
default_material
|
|
)) {
|
|
ztu::logger::error(
|
|
"Error while initializing resource loader: [%] %",
|
|
e.category().name(),
|
|
e.message()
|
|
);
|
|
return;
|
|
}
|
|
|
|
viewer::dynamic_shader_program_loading::load_directory(
|
|
loader,
|
|
*z3d,
|
|
*gl_resource_lock,
|
|
*progress_lock,
|
|
*progress_title,
|
|
*progress_ratio,
|
|
std::filesystem::current_path() / ".." / "shaders"
|
|
);
|
|
|
|
progress_lock->lock();
|
|
*progress_title = "Assigning shaders...";
|
|
*progress_ratio = 0.9f;
|
|
progress_lock->unlock();
|
|
|
|
progress_lock->lock();
|
|
*progress_title = "Starting...";
|
|
*progress_ratio = 1.0f;
|
|
progress_lock->unlock();
|
|
|
|
progress_lock->lock();
|
|
*progress_ratio = std::numeric_limits<float>::max();
|
|
progress_lock->unlock();
|
|
|
|
using loader_type = viewer::asset_loader;
|
|
|
|
std::vector<viewer::instance::asset_id> asset_ids;
|
|
auto dynamic_mesh_handles = std::vector<std::pair<
|
|
loader_type::dynamic_mesh_handle_type,
|
|
loader_type::dynamic_material_handle_type
|
|
>>{};
|
|
|
|
auto dynamic_point_cloud_handles = std::vector<loader_type::dynamic_point_cloud_handle_type>{};
|
|
|
|
for (int i = 1; i + 2 <= args_count; i += 2)
|
|
{
|
|
|
|
auto format = std::string{ args[i] };
|
|
auto filename = std::filesystem::path{ args[i + 1] };
|
|
|
|
if (const auto e = loader.load_asset(
|
|
format, filename, dynamic_mesh_handles, dynamic_point_cloud_handles
|
|
)) {
|
|
ztu::logger::error(
|
|
"Error while loading file %: [%] %",
|
|
filename,
|
|
e.category().name(),
|
|
e.message()
|
|
);
|
|
}
|
|
|
|
for (const auto& [ dynamic_mesh_handle, dynamic_material_handle ] : dynamic_mesh_handles)
|
|
{
|
|
const auto& [ mesh_handle, bounding_box, mesh_components ] = dynamic_mesh_handle;
|
|
const auto& [ material_handle, material_components ] = dynamic_material_handle;
|
|
|
|
//ztu::logger::debug("mesh components: %", static_cast<unsigned int>(mesh_components));
|
|
//ztu::logger::debug("material components: %", static_cast<unsigned int>(material_components));
|
|
|
|
gl_resource_lock->lock();
|
|
const auto asset_id = z3d->add_mesh(
|
|
mesh_handle, bounding_box, mesh_components,
|
|
material_handle, material_components
|
|
);
|
|
gl_resource_lock->unlock();
|
|
ztu::logger::debug("Added mesh to z3d");
|
|
|
|
if (asset_id)
|
|
{
|
|
asset_ids.push_back(*asset_id);
|
|
}
|
|
else
|
|
{
|
|
ztu::logger::warn(
|
|
"Ignored mesh as its layout is not supported by z3d."
|
|
);
|
|
}
|
|
}
|
|
|
|
for (const auto& [ point_cloud_handle, bounding_box, point_cloud_components ] : dynamic_point_cloud_handles)
|
|
{
|
|
gl_resource_lock->lock();
|
|
const auto asset_id = z3d->add_point_cloud(
|
|
point_cloud_handle, bounding_box, point_cloud_components
|
|
);
|
|
gl_resource_lock->unlock();
|
|
ztu::logger::debug("Added poitn cloud to z3d");
|
|
|
|
if (asset_id)
|
|
{
|
|
asset_ids.push_back(*asset_id);
|
|
}
|
|
else
|
|
{
|
|
ztu::logger::warn(
|
|
"Ignored Point cloud, as its layout is not supported by z3d."
|
|
);
|
|
}
|
|
}
|
|
|
|
dynamic_mesh_handles.clear();
|
|
dynamic_point_cloud_handles.clear();
|
|
}
|
|
|
|
if (not asset_ids.empty())
|
|
{
|
|
// TODO resource lock does not help with update
|
|
gl_resource_lock->lock();
|
|
z3d->look_at(asset_ids.front());
|
|
gl_resource_lock->unlock();
|
|
}
|
|
}
|
|
|
|
int main(int args_count, char* args[]) {
|
|
|
|
// Clion struggles interleaving the error stream, so we just don't use it...
|
|
ztu::logger::global_context().err = &std::cout;
|
|
ztu::logger::global_dynamic_config().flags &= ~ztu::logger::flag::time;
|
|
|
|
auto z3d = viewer::instance{};
|
|
|
|
if (const auto e = z3d.init("Z3D"))
|
|
{
|
|
ztu::logger::error(
|
|
"Failed to initialize viewer: [%] %",
|
|
e.category().name(),
|
|
e.message()
|
|
);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
//glEnable(GL_DEBUG_OUTPUT);
|
|
glDebugMessageCallback(
|
|
[](
|
|
GLenum source,
|
|
GLenum type,
|
|
GLuint id,
|
|
GLenum severity,
|
|
GLsizei length,
|
|
const GLchar* message,
|
|
const void* userParam
|
|
) {
|
|
ztu::logger::error("[%] % type: %%, severity: %%, msg: %",
|
|
"OpenGL",
|
|
(type == GL_DEBUG_TYPE_ERROR ? "ERROR" : ""),
|
|
std::hex, type,
|
|
std::hex, severity,
|
|
message
|
|
);
|
|
},
|
|
nullptr
|
|
);
|
|
|
|
std::mutex gl_resource_lock;
|
|
std::mutex progress_lock;
|
|
std::string progress_title = "Initializing...";
|
|
float progress_ratio = 0.0f;
|
|
|
|
auto controller_thread = std::thread(
|
|
controller_task,
|
|
args_count,
|
|
args,
|
|
&z3d,
|
|
&gl_resource_lock,
|
|
&progress_lock,
|
|
&progress_title,
|
|
&progress_ratio
|
|
);
|
|
|
|
auto loader = viewer::asset_loader{};
|
|
|
|
controller_task(
|
|
args_count,
|
|
args,
|
|
&z3d,
|
|
loader,
|
|
&gl_resource_lock,
|
|
&progress_lock,
|
|
&progress_title,
|
|
&progress_ratio
|
|
);
|
|
|
|
z3d.run_progress(
|
|
progress_lock,
|
|
progress_title,
|
|
progress_ratio
|
|
);
|
|
|
|
z3d.size(1280, 720);
|
|
|
|
|
|
z3d.run(gl_resource_lock, 60.0f);
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
*/
|
|
|
|
|
|
|
|
#include "assets/file_parsers/obj_parser.hpp"
|
|
#include "assets/file_parsers/mtl_parser.hpp"
|
|
#include <iostream>
|
|
|
|
int main() {
|
|
assets::path_id_lookups lookups{};
|
|
assets::data_stores stores{};
|
|
|
|
lookups.meshes.try_emplace("/home/zy4n/Downloads/test.obj");
|
|
|
|
assets::obj_parser obj_parser{};
|
|
assets::mtl_parser mtl_parser{};
|
|
|
|
const auto time = [](auto title, auto&& f)
|
|
{
|
|
namespace chr = std::chrono;
|
|
using clock = chr::high_resolution_clock;
|
|
using floating_ms = chr::duration<double, std::milli>;
|
|
|
|
const auto begin = clock::now();
|
|
|
|
f();
|
|
|
|
const auto end = clock::now();
|
|
|
|
const auto ms = chr::duration_cast<floating_ms>(end - begin).count();
|
|
|
|
std::cout << title << ": " << ms << "ms" << std::endl;
|
|
};
|
|
|
|
time("obj prefetch", [&] {
|
|
std::ignore = obj_parser.prefetch(lookups);
|
|
});
|
|
|
|
time("mtl prefetch", [&] {
|
|
std::ignore = mtl_parser.prefetch(lookups);
|
|
});
|
|
|
|
time("mtl parsing", [&] {
|
|
std::ignore = mtl_parser.load(lookups, stores);
|
|
});
|
|
|
|
time("obj parsing", [&] {
|
|
std::ignore = obj_parser.load(lookups, stores);
|
|
});
|
|
|
|
} |