Lagrange
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/api.h>
16#include <lagrange/ui/Entity.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
36LA_UI_API std::shared_ptr<Material>
37convert_material(Registry& r, const fs::path& base_dir, const tinyobj::material_t& tinymat);
38
39
42template <typename MeshType>
43Entity load_obj(
44 Registry& r,
45 const fs::path& path,
46 const io::MeshLoaderParams& params = io::MeshLoaderParams())
47{
48 if (path.extension() != ".obj") {
49 lagrange::logger().error("wrong file format '{}' for load_obj", path.extension().string());
50 return NullEntity;
51 }
52
53 // Override to load as one mesh
54 auto p = params;
55 p.as_one_mesh = true;
56 p.load_materials = false;
57 p.triangulate = true;
58
59 auto res = lagrange::io::load_mesh_ext<MeshType>(path, params);
60 if (!res.success) return NullEntity;
61
62 auto mesh = lagrange::to_shared_ptr(std::move(res.meshes.front()));
63 auto e = register_mesh<MeshType>(r, std::dynamic_pointer_cast<MeshType>(mesh));
64 ui::set_name(r, e, path.filename().string());
65
66 return e;
67}
68
72template <typename MeshType>
73std::pair<Entity, std::vector<std::shared_ptr<Material>>> load_obj_with_materials(
74 Registry& r,
75 const fs::path& path,
76 const io::MeshLoaderParams& params = io::MeshLoaderParams())
77{
78 if (path.extension() != ".obj") {
79 lagrange::logger().error("wrong file format '{}' for load_obj", path.extension().string());
80 return {NullEntity, {}};
81 }
82
83 // Override to load as one mesh
84 auto p = params;
85 p.as_one_mesh = true;
86 p.load_materials = true;
87 p.triangulate = true;
88
89 auto res = lagrange::io::load_mesh_ext<MeshType>(path, params);
90 if (!res.success) return {NullEntity, {}};
91
92 auto mesh = lagrange::to_shared_ptr(std::move(res.meshes.front()));
93 auto e = register_mesh<MeshType>(r, std::dynamic_pointer_cast<MeshType>(mesh));
94 ui::set_name(r, e, path.filename().string());
95
96 std::vector<std::shared_ptr<Material>> mats;
97 auto base_dir = path;
98 base_dir.remove_filename();
99
100 for (auto& tinymat : res.materials) {
101 mats.push_back(convert_material(r, base_dir, tinymat));
102 }
103
104 return {e, mats};
105}
106
107} // namespace ui
108} // 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:22
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:73
Entity load_obj(Registry &r, const fs::path &path, const io::MeshLoaderParams &params=io::MeshLoaderParams())
Loads obj as a single mesh.
Definition: io.h:43
LA_UI_API std::shared_ptr< Material > convert_material(Registry &r, const fs::path &base_dir, const tinyobj::material_t &tinymat)
Convertrs tinyobj's material_t to UI's Material.
Definition: io.cpp:97
Main namespace for Lagrange.
Definition: AABBIGL.h:30
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:84