Try reformatting asset code with clang-format
This commit is contained in:
parent
e2d29f34f2
commit
069318dc0b
5 changed files with 173 additions and 192 deletions
|
@ -1,18 +1,18 @@
|
||||||
set(SRC
|
set(SRC
|
||||||
include/asset_types.hpp
|
include/asset_types.hpp
|
||||||
include/asset.hpp
|
include/asset.hpp
|
||||||
include/assetptr.hpp
|
include/assetptr.hpp
|
||||||
|
|
||||||
src/asset.cpp)
|
src/asset.cpp)
|
||||||
|
|
||||||
add_library(Asset STATIC ${SRC})
|
add_library(Asset STATIC ${SRC})
|
||||||
target_include_directories(Asset PUBLIC include)
|
target_include_directories(Asset PUBLIC include)
|
||||||
target_link_libraries(Asset
|
target_link_libraries(Asset
|
||||||
PUBLIC
|
PUBLIC
|
||||||
Math
|
Math
|
||||||
Renderer
|
Renderer
|
||||||
PRIVATE
|
PRIVATE
|
||||||
stb
|
stb
|
||||||
Log
|
Log
|
||||||
Core)
|
Core)
|
||||||
set_engine_properties(Asset)
|
set_engine_properties(Asset)
|
||||||
|
|
|
@ -1,28 +1,23 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <array>
|
|
||||||
|
|
||||||
#include "file.hpp"
|
|
||||||
#include "assetptr.hpp"
|
|
||||||
#include "asset_types.hpp"
|
#include "asset_types.hpp"
|
||||||
|
#include "assetptr.hpp"
|
||||||
|
#include "file.hpp"
|
||||||
#include "string_utils.hpp"
|
#include "string_utils.hpp"
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> std::unique_ptr<T> load_asset(const prism::path& p);
|
||||||
std::unique_ptr<T> load_asset(const prism::path& p);
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> bool can_load_asset(const prism::path& p);
|
||||||
bool can_load_asset(const prism::path& p);
|
|
||||||
|
|
||||||
template<class AssetType>
|
template<class AssetType> using AssetStore = std::unordered_map<prism::path, std::unique_ptr<AssetType>>;
|
||||||
using AssetStore = std::unordered_map<prism::path, std::unique_ptr<AssetType>>;
|
|
||||||
|
|
||||||
template<class... Assets>
|
template<class... Assets> class AssetPool : public AssetStore<Assets>... {
|
||||||
class AssetPool : public AssetStore<Assets>... {
|
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
template<typename T> AssetPtr<T> add() {
|
||||||
AssetPtr<T> add() {
|
|
||||||
const auto p = prism::path();
|
const auto p = prism::path();
|
||||||
auto reference_block = get_reference_block(p);
|
auto reference_block = get_reference_block(p);
|
||||||
|
|
||||||
|
@ -31,15 +26,11 @@ public:
|
||||||
return AssetPtr<T>(AssetStore<T>::at(p).get(), reference_block);
|
return AssetPtr<T>(AssetStore<T>::at(p).get(), reference_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> AssetPtr<T> get(const prism::path path) { return fetch<T>(path, get_reference_block(path)); }
|
||||||
AssetPtr<T> get(const prism::path path) {
|
|
||||||
return fetch<T>(path, get_reference_block(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> std::vector<T*> get_all() {
|
||||||
std::vector<T*> get_all() {
|
|
||||||
std::vector<T*> assets;
|
std::vector<T*> assets;
|
||||||
for(auto iter = AssetStore<T>::begin(); iter != AssetStore<T>::end(); iter++) {
|
for (auto iter = AssetStore<T>::begin(); iter != AssetStore<T>::end(); iter++) {
|
||||||
auto& [p, asset] = *iter;
|
auto& [p, asset] = *iter;
|
||||||
|
|
||||||
assets.push_back(asset.get());
|
assets.push_back(asset.get());
|
||||||
|
@ -48,9 +39,8 @@ public:
|
||||||
return assets;
|
return assets;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> AssetPtr<T> fetch(const prism::path path, ReferenceBlock* reference_block) {
|
||||||
AssetPtr<T> fetch(const prism::path path, ReferenceBlock* reference_block) {
|
if (!AssetStore<T>::count(path))
|
||||||
if(!AssetStore<T>::count(path))
|
|
||||||
AssetStore<T>::try_emplace(path, load_asset<T>(path));
|
AssetStore<T>::try_emplace(path, load_asset<T>(path));
|
||||||
|
|
||||||
return AssetPtr<T>(AssetStore<T>::at(path).get(), reference_block);
|
return AssetPtr<T>(AssetStore<T>::at(path).get(), reference_block);
|
||||||
|
@ -66,10 +56,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void perform_cleanup() {
|
void perform_cleanup() {
|
||||||
for(auto iter = reference_blocks.begin(); iter != reference_blocks.end();) {
|
for (auto iter = reference_blocks.begin(); iter != reference_blocks.end();) {
|
||||||
auto& [path, block] = *iter;
|
auto& [path, block] = *iter;
|
||||||
|
|
||||||
if(block->references == 0) {
|
if (block->references == 0) {
|
||||||
((delete_asset<Assets>(path)), ...);
|
((delete_asset<Assets>(path)), ...);
|
||||||
|
|
||||||
iter = reference_blocks.erase(iter);
|
iter = reference_blocks.erase(iter);
|
||||||
|
@ -83,16 +73,15 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ReferenceBlock* get_reference_block(const prism::path& path) {
|
ReferenceBlock* get_reference_block(const prism::path& path) {
|
||||||
if(!reference_blocks.count(path))
|
if (!reference_blocks.count(path))
|
||||||
reference_blocks.try_emplace(path, std::make_unique<ReferenceBlock>());
|
reference_blocks.try_emplace(path, std::make_unique<ReferenceBlock>());
|
||||||
|
|
||||||
return reference_blocks[path].get();
|
return reference_blocks[path].get();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) {
|
||||||
void load_asset_generic(const prism::path& path, Asset*& at, ReferenceBlock*& block) {
|
if (can_load_asset<T>(path)) {
|
||||||
if(can_load_asset<T>(path)) {
|
if (!AssetStore<T>::count(path))
|
||||||
if(!AssetStore<T>::count(path))
|
|
||||||
AssetStore<T>::try_emplace(path, load_asset<T>(path));
|
AssetStore<T>::try_emplace(path, load_asset<T>(path));
|
||||||
|
|
||||||
at = AssetStore<T>::at(path).get();
|
at = AssetStore<T>::at(path).get();
|
||||||
|
@ -100,10 +89,9 @@ private:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> void delete_asset(const prism::path path) {
|
||||||
void delete_asset(const prism::path path) {
|
|
||||||
auto iter = AssetStore<T>::find(path);
|
auto iter = AssetStore<T>::find(path);
|
||||||
if(iter != AssetStore<T>::end()) {
|
if (iter != AssetStore<T>::end()) {
|
||||||
auto& [_, asset] = *iter;
|
auto& [_, asset] = *iter;
|
||||||
|
|
||||||
asset.reset();
|
asset.reset();
|
||||||
|
@ -123,24 +111,22 @@ std::unique_ptr<Texture> load_texture(const prism::path& path);
|
||||||
|
|
||||||
void save_material(Material* material, const prism::path& path);
|
void save_material(Material* material, const prism::path& path);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> std::unique_ptr<T> load_asset(const prism::path& path) {
|
||||||
std::unique_ptr<T> load_asset(const prism::path& path) {
|
|
||||||
if constexpr (std::is_same_v<T, Mesh>) {
|
if constexpr (std::is_same_v<T, Mesh>) {
|
||||||
return load_mesh(path);
|
return load_mesh(path);
|
||||||
} else if constexpr(std::is_same_v<T, Material>) {
|
} else if constexpr (std::is_same_v<T, Material>) {
|
||||||
return load_material(path);
|
return load_material(path);
|
||||||
} else if constexpr(std::is_same_v<T, Texture>) {
|
} else if constexpr (std::is_same_v<T, Texture>) {
|
||||||
return load_texture(path);
|
return load_texture(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T> bool can_load_asset(const prism::path& path) {
|
||||||
bool can_load_asset(const prism::path& path) {
|
if constexpr (std::is_same_v<T, Mesh>) {
|
||||||
if constexpr(std::is_same_v<T, Mesh>) {
|
|
||||||
return path.extension() == ".model";
|
return path.extension() == ".model";
|
||||||
} else if constexpr(std::is_same_v<T, Material>) {
|
} else if constexpr (std::is_same_v<T, Material>) {
|
||||||
return path.extension() == ".material";
|
return path.extension() == ".material";
|
||||||
} else if constexpr(std::is_same_v<T, Texture>) {
|
} else if constexpr (std::is_same_v<T, Texture>) {
|
||||||
return path.extension() == ".png";
|
return path.extension() == ".png";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
#include "aabb.hpp"
|
||||||
#include "assetptr.hpp"
|
#include "assetptr.hpp"
|
||||||
#include "math.hpp"
|
#include "math.hpp"
|
||||||
#include "aabb.hpp"
|
|
||||||
#include "utility.hpp"
|
#include "utility.hpp"
|
||||||
|
|
||||||
class GFXBuffer;
|
class GFXBuffer;
|
||||||
|
@ -75,8 +75,8 @@ struct Bone {
|
||||||
|
|
||||||
class Mesh : public Asset {
|
class Mesh : public Asset {
|
||||||
public:
|
public:
|
||||||
// meshes are rendered in parts if we cannot batch it in one call, i.e. a mesh
|
// meshes are rendered in parts if we cannot batch it in one call, i.e. a
|
||||||
// with multiple materials with different textures, etc
|
// mesh with multiple materials with different textures, etc
|
||||||
struct Part {
|
struct Part {
|
||||||
std::string name;
|
std::string name;
|
||||||
prism::aabb bounding_box;
|
prism::aabb bounding_box;
|
||||||
|
@ -84,7 +84,8 @@ public:
|
||||||
GFXBuffer* bone_batrix_buffer = nullptr;
|
GFXBuffer* bone_batrix_buffer = nullptr;
|
||||||
std::vector<Matrix4x4> offset_matrices;
|
std::vector<Matrix4x4> offset_matrices;
|
||||||
|
|
||||||
uint32_t index_offset = 0, index_count = 0;;
|
uint32_t index_offset = 0, index_count = 0;
|
||||||
|
;
|
||||||
int32_t vertex_offset = 0;
|
int32_t vertex_offset = 0;
|
||||||
int32_t material_override = -1;
|
int32_t material_override = -1;
|
||||||
std::string material_hint;
|
std::string material_hint;
|
||||||
|
|
|
@ -12,24 +12,21 @@ public:
|
||||||
std::string path;
|
std::string path;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T> struct AssetPtr {
|
||||||
struct AssetPtr {
|
|
||||||
AssetPtr() = default;
|
AssetPtr() = default;
|
||||||
|
|
||||||
AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) {
|
AssetPtr(T* ptr, ReferenceBlock* block) : handle(ptr), block(block) { block->references++; }
|
||||||
block->references++;
|
|
||||||
}
|
|
||||||
|
|
||||||
AssetPtr(const AssetPtr &rhs) {
|
AssetPtr(const AssetPtr& rhs) {
|
||||||
handle = rhs.handle;
|
handle = rhs.handle;
|
||||||
block = rhs.block;
|
block = rhs.block;
|
||||||
|
|
||||||
if(block != nullptr)
|
if (block != nullptr)
|
||||||
block->references++;
|
block->references++;
|
||||||
}
|
}
|
||||||
|
|
||||||
AssetPtr& operator=(const AssetPtr& rhs) {
|
AssetPtr& operator=(const AssetPtr& rhs) {
|
||||||
if(&rhs != this) {
|
if (&rhs != this) {
|
||||||
handle = rhs.handle;
|
handle = rhs.handle;
|
||||||
block = rhs.block;
|
block = rhs.block;
|
||||||
|
|
||||||
|
@ -41,12 +38,12 @@ struct AssetPtr {
|
||||||
}
|
}
|
||||||
|
|
||||||
~AssetPtr() {
|
~AssetPtr() {
|
||||||
if(block != nullptr)
|
if (block != nullptr)
|
||||||
block->references--;
|
block->references--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
if(block != nullptr)
|
if (block != nullptr)
|
||||||
block->references--;
|
block->references--;
|
||||||
|
|
||||||
block = nullptr;
|
block = nullptr;
|
||||||
|
@ -56,23 +53,13 @@ struct AssetPtr {
|
||||||
T* handle = nullptr;
|
T* handle = nullptr;
|
||||||
ReferenceBlock* block = nullptr;
|
ReferenceBlock* block = nullptr;
|
||||||
|
|
||||||
explicit operator bool() const{
|
explicit operator bool() const { return handle != nullptr; }
|
||||||
return handle != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
T* operator->() {
|
T* operator->() { return handle; }
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
T* operator->() const {
|
T* operator->() const { return handle; }
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
T* operator*() {
|
T* operator*() { return handle; }
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
T* operator*() const {
|
T* operator*() const { return handle; }
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
#include "asset.hpp"
|
#include "asset.hpp"
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <map>
|
||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
|
|
||||||
#include "log.hpp"
|
#include "assertions.hpp"
|
||||||
#include "engine.hpp"
|
#include "engine.hpp"
|
||||||
#include "gfx.hpp"
|
#include "gfx.hpp"
|
||||||
#include "json_conversions.hpp"
|
|
||||||
#include "gfx_commandbuffer.hpp"
|
#include "gfx_commandbuffer.hpp"
|
||||||
#include "assertions.hpp"
|
|
||||||
#include "renderer.hpp"
|
|
||||||
#include "input.hpp"
|
|
||||||
#include "physics.hpp"
|
|
||||||
#include "imgui_backend.hpp"
|
#include "imgui_backend.hpp"
|
||||||
|
#include "input.hpp"
|
||||||
|
#include "json_conversions.hpp"
|
||||||
|
#include "log.hpp"
|
||||||
|
#include "physics.hpp"
|
||||||
|
#include "renderer.hpp"
|
||||||
|
|
||||||
std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
Expects(!path.empty());
|
Expects(!path.empty());
|
||||||
|
|
||||||
auto file = prism::open_file(path, true);
|
auto file = prism::open_file(path, true);
|
||||||
if(!file.has_value()) {
|
if (!file.has_value()) {
|
||||||
prism::log("Failed to load mesh from {}!", path.string());
|
prism::log("Failed to load mesh from {}!", path.string());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
int version = 0;
|
int version = 0;
|
||||||
file->read(&version);
|
file->read(&version);
|
||||||
|
|
||||||
if(version == 5 || version == 6 || version == 7) {
|
if (version == 5 || version == 6 || version == 7) {
|
||||||
} else {
|
} else {
|
||||||
prism::log("{} failed the mesh version check! reported version = {}", path.string(), version);
|
prism::log("{} failed the mesh version check! reported version = {}", path.string(), version);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -50,9 +50,10 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
Expects(numVertices > 0);
|
Expects(numVertices > 0);
|
||||||
|
|
||||||
const auto read_buffer = [&f = file.value(), numVertices](unsigned int size) -> GFXBuffer* {
|
const auto read_buffer = [&f = file.value(), numVertices](unsigned int size) -> GFXBuffer* {
|
||||||
auto buffer = engine->get_gfx()->create_buffer(nullptr, size * static_cast<unsigned int>(numVertices), false, GFXBufferUsage::Vertex);
|
auto buffer = engine->get_gfx()->create_buffer(
|
||||||
|
nullptr, size * static_cast<unsigned int>(numVertices), false, GFXBufferUsage::Vertex);
|
||||||
auto buffer_ptr = reinterpret_cast<unsigned char*>(engine->get_gfx()->get_buffer_contents(buffer));
|
auto buffer_ptr = reinterpret_cast<unsigned char*>(engine->get_gfx()->get_buffer_contents(buffer));
|
||||||
if(buffer_ptr != nullptr) {
|
if (buffer_ptr != nullptr) {
|
||||||
f.read(buffer_ptr, size * static_cast<unsigned int>(numVertices));
|
f.read(buffer_ptr, size * static_cast<unsigned int>(numVertices));
|
||||||
|
|
||||||
engine->get_gfx()->release_buffer_contents(buffer, buffer_ptr);
|
engine->get_gfx()->release_buffer_contents(buffer, buffer_ptr);
|
||||||
|
@ -70,7 +71,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
mesh->tangent_buffer = read_buffer(sizeof(prism::float3));
|
mesh->tangent_buffer = read_buffer(sizeof(prism::float3));
|
||||||
mesh->bitangent_buffer = read_buffer(sizeof(prism::float3));
|
mesh->bitangent_buffer = read_buffer(sizeof(prism::float3));
|
||||||
|
|
||||||
if(mesh_type == MeshType::Skinned)
|
if (mesh_type == MeshType::Skinned)
|
||||||
mesh->bone_buffer = read_buffer(sizeof(BoneVertexData));
|
mesh->bone_buffer = read_buffer(sizeof(BoneVertexData));
|
||||||
|
|
||||||
int numIndices = 0;
|
int numIndices = 0;
|
||||||
|
@ -78,9 +79,10 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
|
|
||||||
Expects(numIndices > 0);
|
Expects(numIndices > 0);
|
||||||
|
|
||||||
mesh->index_buffer = engine->get_gfx()->create_buffer(nullptr, sizeof(uint32_t) * numIndices, false, GFXBufferUsage::Index);
|
mesh->index_buffer =
|
||||||
|
engine->get_gfx()->create_buffer(nullptr, sizeof(uint32_t) * numIndices, false, GFXBufferUsage::Index);
|
||||||
auto index_ptr = reinterpret_cast<uint32_t*>(engine->get_gfx()->get_buffer_contents(mesh->index_buffer));
|
auto index_ptr = reinterpret_cast<uint32_t*>(engine->get_gfx()->get_buffer_contents(mesh->index_buffer));
|
||||||
if(index_ptr != nullptr) {
|
if (index_ptr != nullptr) {
|
||||||
file->read(index_ptr, sizeof(uint32_t) * numIndices);
|
file->read(index_ptr, sizeof(uint32_t) * numIndices);
|
||||||
} else {
|
} else {
|
||||||
file->seek(sizeof(uint32_t) * numIndices);
|
file->seek(sizeof(uint32_t) * numIndices);
|
||||||
|
@ -93,7 +95,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
|
|
||||||
mesh->bones.reserve(bone_len);
|
mesh->bones.reserve(bone_len);
|
||||||
|
|
||||||
if(bone_len > 0) {
|
if (bone_len > 0) {
|
||||||
file->read(&mesh->global_inverse_transformation);
|
file->read(&mesh->global_inverse_transformation);
|
||||||
|
|
||||||
std::map<std::string, uint32_t> boneMapping;
|
std::map<std::string, uint32_t> boneMapping;
|
||||||
|
@ -114,7 +116,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
prism::float3 scl;
|
prism::float3 scl;
|
||||||
file->read(&scl);
|
file->read(&scl);
|
||||||
|
|
||||||
if(!boneMapping.count(bone)) {
|
if (!boneMapping.count(bone)) {
|
||||||
Bone b;
|
Bone b;
|
||||||
b.index = mesh->bones.size();
|
b.index = mesh->bones.size();
|
||||||
b.name = bone;
|
b.name = bone;
|
||||||
|
@ -123,7 +125,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
b.rotation = rot;
|
b.rotation = rot;
|
||||||
b.scale = scl;
|
b.scale = scl;
|
||||||
|
|
||||||
if(parent != "none" && !parent.empty())
|
if (parent != "none" && !parent.empty())
|
||||||
parentQueue[b.index] = parent;
|
parentQueue[b.index] = parent;
|
||||||
|
|
||||||
mesh->bones.push_back(b);
|
mesh->bones.push_back(b);
|
||||||
|
@ -132,15 +134,15 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& [index, parentName] : parentQueue) {
|
for (auto& [index, parentName] : parentQueue) {
|
||||||
for(auto& bone : mesh->bones) {
|
for (auto& bone : mesh->bones) {
|
||||||
if(bone.name == parentName)
|
if (bone.name == parentName)
|
||||||
mesh->bones[index].parent = &bone;
|
mesh->bones[index].parent = &bone;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& bone : mesh->bones) {
|
for (auto& bone : mesh->bones) {
|
||||||
if(bone.parent == nullptr)
|
if (bone.parent == nullptr)
|
||||||
mesh->root_bone = &bone;
|
mesh->root_bone = &bone;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,14 +155,14 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
mesh->parts.resize(numMeshes);
|
mesh->parts.resize(numMeshes);
|
||||||
|
|
||||||
uint32_t vertexOffset = 0, indexOffset = 0;
|
uint32_t vertexOffset = 0, indexOffset = 0;
|
||||||
for(int i = 0; i < numMeshes; i++) {
|
for (int i = 0; i < numMeshes; i++) {
|
||||||
auto& p = mesh->parts[i];
|
auto& p = mesh->parts[i];
|
||||||
p.vertex_offset = vertexOffset;
|
p.vertex_offset = vertexOffset;
|
||||||
p.index_offset = indexOffset;
|
p.index_offset = indexOffset;
|
||||||
|
|
||||||
file->read_string(p.name);
|
file->read_string(p.name);
|
||||||
|
|
||||||
if(version >= 6) {
|
if (version >= 6) {
|
||||||
file->read(&p.bounding_box);
|
file->read(&p.bounding_box);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,9 +174,10 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
int numBones = 0;
|
int numBones = 0;
|
||||||
file->read(&numBones);
|
file->read(&numBones);
|
||||||
|
|
||||||
p.bone_batrix_buffer = engine->get_gfx()->create_buffer(nullptr, sizeof(Matrix4x4) * 128, true, GFXBufferUsage::Storage);
|
p.bone_batrix_buffer =
|
||||||
|
engine->get_gfx()->create_buffer(nullptr, sizeof(Matrix4x4) * 128, true, GFXBufferUsage::Storage);
|
||||||
|
|
||||||
if(numBones > 0) {
|
if (numBones > 0) {
|
||||||
p.offset_matrices.resize(numBones);
|
p.offset_matrices.resize(numBones);
|
||||||
|
|
||||||
file->read(p.offset_matrices.data(), sizeof(Matrix4x4) * numBones);
|
file->read(p.offset_matrices.data(), sizeof(Matrix4x4) * numBones);
|
||||||
|
@ -182,7 +185,7 @@ std::unique_ptr<Mesh> load_mesh(const prism::path& path) {
|
||||||
|
|
||||||
file->read(&p.material_override);
|
file->read(&p.material_override);
|
||||||
|
|
||||||
if(version == 7) {
|
if (version == 7) {
|
||||||
file->read_string(p.material_hint);
|
file->read_string(p.material_hint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +202,7 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
|
||||||
Expects(!path.empty());
|
Expects(!path.empty());
|
||||||
|
|
||||||
auto file = prism::open_file(path, true);
|
auto file = prism::open_file(path, true);
|
||||||
if(!file.has_value()) {
|
if (!file.has_value()) {
|
||||||
prism::log("Failed to load texture from {}!", path.string());
|
prism::log("Failed to load texture from {}!", path.string());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -210,8 +213,9 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
|
||||||
file->read_all();
|
file->read_all();
|
||||||
|
|
||||||
int width, height, channels;
|
int width, height, channels;
|
||||||
unsigned char* data = stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4);
|
unsigned char* data =
|
||||||
if(!data) {
|
stbi_load_from_memory(file->cast_data<unsigned char>(), file->size(), &width, &height, &channels, 4);
|
||||||
|
if (!data) {
|
||||||
prism::log("Failed to load texture from {}!", path.string());
|
prism::log("Failed to load texture from {}!", path.string());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -229,16 +233,19 @@ std::unique_ptr<Texture> load_texture(const prism::path& path) {
|
||||||
createInfo.width = width;
|
createInfo.width = width;
|
||||||
createInfo.height = height;
|
createInfo.height = height;
|
||||||
createInfo.format = GFXPixelFormat::R8G8B8A8_UNORM;
|
createInfo.format = GFXPixelFormat::R8G8B8A8_UNORM;
|
||||||
createInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst | GFXTextureUsage::TransferSrc; // src and dst are needed for copy tex data -> image and mipmap gen (from image data) respectively
|
createInfo.usage = GFXTextureUsage::Sampled | GFXTextureUsage::TransferDst |
|
||||||
|
GFXTextureUsage::TransferSrc; // src and dst are needed for copy tex
|
||||||
|
// data -> image and mipmap gen (from
|
||||||
|
// image data) respectively
|
||||||
|
|
||||||
if(should_generate_mipmaps)
|
if (should_generate_mipmaps)
|
||||||
createInfo.mip_count = std::floor(std::log2(std::max(width, height))) + 1;
|
createInfo.mip_count = std::floor(std::log2(std::max(width, height))) + 1;
|
||||||
|
|
||||||
texture->handle = engine->get_gfx()->create_texture(createInfo);
|
texture->handle = engine->get_gfx()->create_texture(createInfo);
|
||||||
|
|
||||||
engine->get_gfx()->copy_texture(texture->handle, data, width * height * 4);
|
engine->get_gfx()->copy_texture(texture->handle, data, width * height * 4);
|
||||||
|
|
||||||
if(createInfo.mip_count > 1) {
|
if (createInfo.mip_count > 1) {
|
||||||
GFXCommandBuffer* cmd_buf = engine->get_gfx()->acquire_command_buffer(false);
|
GFXCommandBuffer* cmd_buf = engine->get_gfx()->acquire_command_buffer(false);
|
||||||
|
|
||||||
cmd_buf->generate_mipmaps(texture->handle, createInfo.mip_count);
|
cmd_buf->generate_mipmaps(texture->handle, createInfo.mip_count);
|
||||||
|
@ -255,7 +262,7 @@ std::unique_ptr<Material> load_material(const prism::path& path) {
|
||||||
Expects(!path.empty());
|
Expects(!path.empty());
|
||||||
|
|
||||||
auto file = prism::open_file(path);
|
auto file = prism::open_file(path);
|
||||||
if(!file.has_value()) {
|
if (!file.has_value()) {
|
||||||
prism::log("Failed to load material from {}!", path.string());
|
prism::log("Failed to load material from {}!", path.string());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -266,7 +273,7 @@ std::unique_ptr<Material> load_material(const prism::path& path) {
|
||||||
auto mat = std::make_unique<Material>();
|
auto mat = std::make_unique<Material>();
|
||||||
mat->path = path.string();
|
mat->path = path.string();
|
||||||
|
|
||||||
if(!j.count("version") || j["version"] != 3) {
|
if (!j.count("version") || j["version"] != 3) {
|
||||||
prism::log("Material {} failed the version check!", path.string());
|
prism::log("Material {} failed the version check!", path.string());
|
||||||
return mat;
|
return mat;
|
||||||
}
|
}
|
||||||
|
@ -277,7 +284,7 @@ std::unique_ptr<Material> load_material(const prism::path& path) {
|
||||||
p.float_value = property["float_value"];
|
p.float_value = property["float_value"];
|
||||||
p.type = property["type"];
|
p.type = property["type"];
|
||||||
|
|
||||||
if(!property["asset_value"].get<std::string>().empty()) {
|
if (!property["asset_value"].get<std::string>().empty()) {
|
||||||
p.value_tex = assetm->get<Texture>(prism::game_domain / property["asset_value"].get<std::string>());
|
p.value_tex = assetm->get<Texture>(prism::game_domain / property["asset_value"].get<std::string>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,7 +293,7 @@ std::unique_ptr<Material> load_material(const prism::path& path) {
|
||||||
|
|
||||||
mat->colorProperty = loadProperty(j["color"]);
|
mat->colorProperty = loadProperty(j["color"]);
|
||||||
|
|
||||||
if(j.contains("normal"))
|
if (j.contains("normal"))
|
||||||
mat->normalProperty = loadProperty(j["normal"]);
|
mat->normalProperty = loadProperty(j["normal"]);
|
||||||
|
|
||||||
return mat;
|
return mat;
|
||||||
|
|
Reference in a new issue