Lagrange
Loading...
Searching...
No Matches
io.h
1/*
2 * Copyright 2021 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/Logger.h>
15#include <lagrange/ui/Entity.h>
16#include <lagrange/ui/api.h>
17#include <lagrange/ui/components/MeshData.h>
18#include <lagrange/ui/types/Texture.h>
19#include <lagrange/ui/utils/io_assimp.h>
20
21#include <lagrange/io/load_mesh_ext.h>
22
23#include <sstream>
24
25namespace lagrange {
26namespace ui {
27
28LA_UI_API std::shared_ptr<Texture> load_texture(
29 const fs::path& path,
30 const Texture::Params& params = Texture::Params());
31
37LA_UI_API std::shared_ptr<Material>
38convert_material(Registry& r, const fs::path& base_dir, const tinyobj::material_t& tinymat);
39
40
43template <typename MeshType>
44Entity load_obj(
45 Registry& r,
46 const fs::path& path,
47 const io::MeshLoaderParams& params = io::MeshLoaderParams())
48{
49 if (path.extension() != ".obj") {
50 lagrange::logger().error("wrong file format '{}' for load_obj", path.extension().string());
51 return NullEntity;
52 }
53
54 // Override to load as one mesh
55 auto p = params;
56 p.as_one_mesh = true;
57 p.load_materials = false;
58 p.triangulate = true;
59
60 auto res = lagrange::io::load_mesh_ext<MeshType>(path, params);
61 if (!res.success) return NullEntity;
62
63 auto mesh = lagrange::to_shared_ptr(std::move(res.meshes.front()));
64 auto e = register_mesh<MeshType>(r, std::dynamic_pointer_cast<MeshType>(mesh));
65 ui::set_name(r, e, path.filename().string());
66
67 return e;
68}
69
73template <typename MeshType>
74std::pair<Entity, std::vector<std::shared_ptr<Material>>> load_obj_with_materials(
75 Registry& r,
76 const fs::path& path,
77 const io::MeshLoaderParams& params = io::MeshLoaderParams())
78{
79 if (path.extension() != ".obj") {
80 lagrange::logger().error("wrong file format '{}' for load_obj", path.extension().string());
81 return {NullEntity, {}};
82 }
83
84 // Override to load as one mesh
85 auto p = params;
86 p.as_one_mesh = true;
87 p.load_materials = true;
88 p.triangulate = true;
89
90 auto res = lagrange::io::load_mesh_ext<MeshType>(path, params);
91 if (!res.success) return {NullEntity, {}};
92
93 auto mesh = lagrange::to_shared_ptr(std::move(res.meshes.front()));
94 auto e = register_mesh<MeshType>(r, std::dynamic_pointer_cast<MeshType>(mesh));
95 ui::set_name(r, e, path.filename().string());
96
97 std::vector<std::shared_ptr<Material>> mats;
98 auto base_dir = path;
99 base_dir.remove_filename();
100
101 for (auto& tinymat : res.materials) {
102 mats.push_back(convert_material(r, base_dir, tinymat));
103 }
104
105 return {e, mats};
106}
107
108} // namespace ui
109} // namespace lagrange
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:40
Lagrange UI Viewer and mini 3D engine.
Definition AcceleratedPicking.h:23
std::pair< Entity, std::vector< std::shared_ptr< Material > > > load_obj_with_materials(Registry &r, const fs::path &path, const io::MeshLoaderParams &params=io::MeshLoaderParams())
Loads obj as a single mesh and materials.
Definition io.h:74
Entity load_obj(Registry &r, const fs::path &path, const io::MeshLoaderParams &params=io::MeshLoaderParams())
Loads obj as a single mesh.
Definition io.h:44
LA_UI_API std::shared_ptr< Material > convert_material(Registry &r, const fs::path &base_dir, const tinyobj::material_t &tinymat)
Converts tinyobj's material_t to UI's Material.
Definition io.cpp:97
Main namespace for Lagrange.
std::shared_ptr< T > to_shared_ptr(std::unique_ptr< T > &&ptr)
Helper for automatic type deduction for unique_ptr to shared_ptr conversion.
Definition common.h:88
Definition Texture.h:45