overhauled physics engine

This commit is contained in:
Dynamitos
2023-01-21 18:43:21 +01:00
parent 3c7346cf7b
commit 2208ab438a
164 changed files with 22606 additions and 928 deletions
+95 -44
View File
@@ -2,18 +2,20 @@
#include "MeshAsset.h"
#include "FontAsset.h"
#include "TextureAsset.h"
#include "MaterialAsset.h"
#include "FontLoader.h"
#include "TextureLoader.h"
#include "MaterialLoader.h"
#include "MeshLoader.h"
#include "Material/MaterialAsset.h"
#include "Graphics/Mesh.h"
#include "Graphics/Graphics.h"
#include "Window/WindowManager.h"
#include "MeshAsset.h"
#include <nlohmann/json.hpp>
#include <iostream>
using namespace Seele;
using json = nlohmann::json;
AssetRegistry::~AssetRegistry()
{
@@ -25,6 +27,11 @@ void AssetRegistry::init(const std::string& rootFolder)
}
void AssetRegistry::importFile(const std::string &filePath)
{
importFile(filePath, "");
}
void AssetRegistry::importFile(const std::string &filePath, const std::string& importPath)
{
std::filesystem::path fsPath = std::filesystem::path(filePath);
std::string extension = fsPath.extension().string();
@@ -32,43 +39,78 @@ void AssetRegistry::importFile(const std::string &filePath)
|| extension.compare(".obj") == 0
|| extension.compare(".blend") == 0)
{
get().importMesh(fsPath);
get().importMesh(fsPath, importPath);
}
if (extension.compare(".png") == 0
|| extension.compare(".jpg") == 0)
{
get().importTexture(fsPath);
get().importTexture(fsPath, importPath);
}
if(extension.compare(".ttf") == 0)
{
get().importFont(fsPath);
get().importFont(fsPath, importPath);
}
if (extension.compare(".asset") == 0)
{
get().importMaterial(fsPath);
get().importMaterial(fsPath, importPath);
}
}
PMeshAsset AssetRegistry::findMesh(const std::string &filePath)
{
auto it = get().meshes.find(filePath);
assert(it != get().meshes.end());
AssetFolder& folder = get().assetRoot;
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
if(slashLoc != -1)
{
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
fileName = filePath.substr(slashLoc+1, filePath.size());
}
auto it = folder.meshes.find(fileName);
assert(it != folder.meshes.end());
return it->second;
}
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
{
return get().textures[filePath];
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
if(slashLoc != -1)
{
AssetFolder& folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
fileName = filePath.substr(slashLoc+1, filePath.size());
return folder.textures[fileName];
}
else
{
return get().assetRoot.textures[fileName];
}
}
PFontAsset AssetRegistry::findFont(const std::string& name)
PFontAsset AssetRegistry::findFont(const std::string& filePath)
{
return get().fonts[name];
AssetFolder& folder = get().assetRoot;
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
if(slashLoc != -1)
{
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
fileName = filePath.substr(slashLoc+1, filePath.size());
}
return folder.fonts[fileName];
}
PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
{
return get().materials[filePath];
AssetFolder& folder = get().assetRoot;
std::string fileName = filePath;
size_t slashLoc = filePath.rfind("/");
if(slashLoc != -1)
{
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
fileName = filePath.substr(slashLoc+1, filePath.size());
}
return folder.materials[fileName];
}
std::ofstream AssetRegistry::createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode)
@@ -106,56 +148,65 @@ std::string AssetRegistry::getRootFolder()
return get().rootFolder.generic_string();
}
void AssetRegistry::importMesh(const std::filesystem::path &filePath)
void AssetRegistry::importMesh(const std::filesystem::path &filePath, const std::string& importPath)
{
meshLoader->importAsset(filePath);
meshLoader->importAsset(filePath, importPath);
}
void AssetRegistry::importTexture(const std::filesystem::path &filePath)
void AssetRegistry::importTexture(const std::filesystem::path &filePath, const std::string& importPath)
{
textureLoader->importAsset(filePath);
textureLoader->importAsset(filePath, importPath);
}
void AssetRegistry::importFont(const std::filesystem::path& filePath)
void AssetRegistry::importFont(const std::filesystem::path& filePath, const std::string& importPath)
{
fontLoader->importAsset(filePath);
fontLoader->importAsset(filePath, importPath);
}
void AssetRegistry::importMaterial(const std::filesystem::path &filePath)
void AssetRegistry::importMaterial(const std::filesystem::path &filePath, const std::string& importPath)
{
materialLoader->importAsset(filePath);
materialLoader->importAsset(filePath, importPath);
}
void AssetRegistry::registerMesh(PMeshAsset mesh)
void AssetRegistry::registerMesh(PMeshAsset mesh, const std::string& importPath)
{
PMeshAsset existingMesh = meshes[mesh->getFileName()];
if(existingMesh != nullptr)
AssetFolder& folder = getOrCreateFolder(importPath);
folder.meshes[mesh->getFileName()] = mesh;
}
void AssetRegistry::registerTexture(PTextureAsset texture, const std::string& importPath)
{
AssetFolder& folder = getOrCreateFolder(importPath);
folder.textures[texture->getFileName()] = texture;
}
void AssetRegistry::registerFont(PFontAsset font, const std::string& importPath)
{
AssetFolder& folder = getOrCreateFolder(importPath);
folder.fonts[font->getFileName()] = font;
}
void AssetRegistry::registerMaterial(PMaterialAsset material, const std::string& importPath)
{
AssetFolder& folder = getOrCreateFolder(importPath);
folder.materials[material->getFileName()] = material;
}
AssetRegistry::AssetFolder& AssetRegistry::getOrCreateFolder(std::string fullPath)
{
AssetFolder& result = assetRoot;
while(!fullPath.empty())
{
auto newMeshes = mesh->getMeshes();
for(uint32 i = 0; i < newMeshes.size(); ++i)
size_t slashLoc = fullPath.find("/");
if(slashLoc == -1)
{
existingMesh->addMesh(newMeshes[i]);
return result.children[fullPath];
}
std::string folderName = fullPath.substr(0, slashLoc);
result = result.children[folderName];
fullPath = fullPath.substr(slashLoc+1, fullPath.size());
}
else
{
meshes[mesh->getFileName()] = mesh;
}
}
void AssetRegistry::registerTexture(PTextureAsset texture)
{
textures[texture->getFileName()] = texture;
}
void AssetRegistry::registerFont(PFontAsset font)
{
fonts[font->getFileName()] = font;
}
void AssetRegistry::registerMaterial(PMaterialAsset material)
{
materials[material->getFileName()] = material;
return result;
}
std::ofstream AssetRegistry::internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode)
+22 -14
View File
@@ -1,7 +1,6 @@
#pragma once
#include "MinimalEngine.h"
#include "Asset.h"
#include "Material/MaterialAsset.h"
#include <string>
#include <map>
@@ -25,6 +24,7 @@ public:
static std::string getRootFolder();
static void importFile(const std::string& filePath);
static void importFile(const std::string& filePath, const std::string& importPath);
static PMeshAsset findMesh(const std::string& filePath);
static PTextureAsset findTexture(const std::string& filePath);
@@ -34,30 +34,38 @@ public:
static std::ofstream createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::out);
static std::ifstream createReadStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::in);
private:
struct AssetFolder
{
std::map<std::string, AssetFolder> children;
//Todo: Seele::Map doesn't really work with strings for some reason, so just use std::map for now
std::map<std::string, PTextureAsset> textures;
std::map<std::string, PFontAsset> fonts;
std::map<std::string, PMeshAsset> meshes;
std::map<std::string, PMaterialAsset> materials;
};
static AssetRegistry& get();
AssetRegistry();
void init(const std::filesystem::path& rootFolder, Gfx::PGraphics graphics);
void importMesh(const std::filesystem::path& filePath);
void importTexture(const std::filesystem::path& filePath);
void importFont(const std::filesystem::path& filePath);
void importMaterial(const std::filesystem::path& filePath);
void importMesh(const std::filesystem::path& filePath, const std::string& importPath);
void importTexture(const std::filesystem::path& filePath, const std::string& importPath);
void importFont(const std::filesystem::path& filePath, const std::string& importPath);
void importMaterial(const std::filesystem::path& filePath, const std::string& importPath);
void registerMesh(PMeshAsset mesh);
void registerTexture(PTextureAsset texture);
void registerFont(PFontAsset font);
void registerMaterial(PMaterialAsset material);
void registerMesh(PMeshAsset mesh, const std::string& importPath);
void registerTexture(PTextureAsset texture, const std::string& importPath);
void registerFont(PFontAsset font, const std::string& importPath);
void registerMaterial(PMaterialAsset material, const std::string& importPath);
AssetFolder& getOrCreateFolder(std::string foldername);
std::ofstream internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::out);
std::ifstream internalCreateReadStream(const std::string& relaitvePath, std::ios_base::openmode openmode = std::ios::in);
std::filesystem::path rootFolder;
//Todo: Seele::Map doesn't really work with strings for some reason, so just use std::map for now
std::map<std::string, PTextureAsset> textures;
std::map<std::string, PFontAsset> fonts;
std::map<std::string, PMeshAsset> meshes;
std::map<std::string, PMaterialAsset> materials;
AssetFolder assetRoot;
UPTextureLoader textureLoader;
UPFontLoader fontLoader;
UPMeshLoader meshLoader;
+6
View File
@@ -8,6 +8,10 @@ target_sources(Engine
FontAsset.cpp
FontLoader.h
FontLoader.cpp
MaterialAsset.h
MaterialAsset.cpp
MaterialInstanceAsset.h
MaterialInstanceAsset.cpp
MaterialLoader.h
MaterialLoader.cpp
MeshAsset.h
@@ -26,6 +30,8 @@ target_sources(Engine
AssetRegistry.h
FontAsset.h
FontLoader.h
MaterialAsset.h
MaterialInstanceAsset.h
MaterialLoader.h
MeshAsset.h
MeshLoader.h
+2 -2
View File
@@ -52,8 +52,8 @@ void FontAsset::load()
continue;
}
Glyph& glyph = glyphs[c];
glyph.size = Math::IVector2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
glyph.bearing = Math::IVector2(face->glyph->bitmap_left, face->glyph->bitmap_top);
glyph.size = IVector2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
glyph.bearing = IVector2(face->glyph->bitmap_left, face->glyph->bitmap_top);
glyph.advance = face->glyph->advance.x;
TextureCreateInfo imageData;
imageData.format = Gfx::SE_FORMAT_R8_UINT;
+2 -2
View File
@@ -17,8 +17,8 @@ public:
struct Glyph
{
Gfx::PTexture2D texture;
Math::IVector2 size;
Math::IVector2 bearing;
IVector2 size;
IVector2 bearing;
uint32 advance;
};
const std::map<uint32, Glyph> getGlyphData() const { return glyphs; }
+2 -3
View File
@@ -15,7 +15,7 @@ FontLoader::~FontLoader()
{
}
void FontLoader::importAsset(const std::filesystem::path& filePath)
void FontLoader::importAsset(const std::filesystem::path& filePath, const std::string& importPath)
{
std::filesystem::path assetPath = filePath.filename();
assetPath.replace_extension("asset");
@@ -23,12 +23,11 @@ void FontLoader::importAsset(const std::filesystem::path& filePath)
std::error_code code;
std::filesystem::copy_file(filePath, asset->getFullPath(), code);
asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerFont(asset);
AssetRegistry::get().registerFont(asset, importPath);
import(filePath, asset);
}
void FontLoader::import(std::filesystem::path path, PFontAsset asset)
{
asset->load();
AssetRegistry::get().registerFont(asset);
}
+1 -1
View File
@@ -12,7 +12,7 @@ class FontLoader
public:
FontLoader(Gfx::PGraphics graphic);
~FontLoader();
void importAsset(const std::filesystem::path& filePath);
void importAsset(const std::filesystem::path& filePath, const std::string& importPath);
private:
void import(std::filesystem::path path, PFontAsset asset);
Gfx::PGraphics graphics;
+41
View File
@@ -0,0 +1,41 @@
#include "MaterialAsset.h"
#include "Material/Material.h"
using namespace Seele;
MaterialAsset::MaterialAsset()
{
}
MaterialAsset::MaterialAsset(const std::string& directory, const std::string& name)
: Asset(directory, name)
{
}
MaterialAsset::MaterialAsset(const std::filesystem::path& fullPath)
: Asset(fullPath)
{
}
MaterialAsset::~MaterialAsset()
{
}
void MaterialAsset::save()
{
}
void MaterialAsset::load()
{
}
void MaterialAsset::beginFrame()
{
}
void MaterialAsset::endFrame()
{
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include "Asset.h"
namespace Seele
{
DECLARE_REF(Material)
class MaterialAsset : public Asset
{
public:
MaterialAsset();
MaterialAsset(const std::string &directory, const std::string &name);
MaterialAsset(const std::filesystem::path &fullPath);
virtual ~MaterialAsset();
virtual void beginFrame();
virtual void endFrame();
virtual void save() override;
virtual void load() override;
PMaterial getMaterial() const { return material; }
private:
PMaterial material;
friend class MaterialLoader;
};
DEFINE_REF(MaterialAsset)
} // namespace Seele
@@ -0,0 +1,32 @@
#include "MaterialInstanceAsset.h"
#include "Material/MaterialInstance.h"
#include "Material/Material.h"
using namespace Seele;
MaterialInstanceAsset::MaterialInstanceAsset()
{
}
MaterialInstanceAsset::MaterialInstanceAsset(const std::string& directory, const std::string& name)
: Asset(directory, name)
{
}
MaterialInstanceAsset::MaterialInstanceAsset(const std::filesystem::path& fullPath)
: Asset(fullPath)
{
}
MaterialInstanceAsset::~MaterialInstanceAsset()
{
}
void MaterialInstanceAsset::save()
{
}
void MaterialInstanceAsset::load()
{
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include "Asset.h"
#include "Material/MaterialInstance.h"
namespace Seele
{
class MaterialInstanceAsset : public Asset
{
public:
MaterialInstanceAsset();
MaterialInstanceAsset(const std::string &directory, const std::string &name);
MaterialInstanceAsset(const std::filesystem::path &fullPath);
virtual ~MaterialInstanceAsset();
virtual void beginFrame();
virtual void endFrame();
virtual void save() override;
virtual void load() override;
private:
PMaterialInstance material;
};
DEFINE_REF(MaterialInstanceAsset)
} // namespace Seele
+114 -10
View File
@@ -1,37 +1,141 @@
#include "MaterialLoader.h"
#include "Graphics/Graphics.h"
#include "Material/MaterialAsset.h"
#include "MaterialAsset.h"
#include "AssetRegistry.h"
#include "Material/Material.h"
#include "Material/BRDF.h"
#include "Window/WindowManager.h"
#include "Material/ShaderExpression.h"
#include "TextureAsset.h"
#include <nlohmann/json.hpp>
using namespace Seele;
using json = nlohmann::json;
MaterialLoader::MaterialLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
placeholderMaterial = new MaterialAsset(std::filesystem::absolute("./shaders/Placeholder.asset"));
placeholderMaterial->load();
graphics->getShaderCompiler()->registerMaterial(placeholderMaterial);
importAsset(std::filesystem::absolute("./shaders/Placeholder.asset"), "");
}
MaterialLoader::~MaterialLoader()
{
}
void MaterialLoader::importAsset(const std::filesystem::path& name)
void MaterialLoader::importAsset(const std::filesystem::path& name, const std::string& importPath)
{
std::filesystem::path assetPath = name.filename();
assetPath.replace_extension("asset");
PMaterialAsset asset = new MaterialAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerMaterial(asset);
AssetRegistry::get().registerMaterial(asset, importPath);
import(name, asset);
}
void MaterialLoader::import(std::filesystem::path, PMaterialAsset asset)
void MaterialLoader::import(std::filesystem::path name, PMaterialAsset asset)
{
asset->load();
graphics->getShaderCompiler()->registerMaterial(asset);
AssetRegistry::get().registerMaterial(asset);
auto stream = std::ifstream(name.c_str());
json j;
stream >> j;
std::string materialName = j["name"].get<std::string>() + "Material";
Gfx::PDescriptorLayout layout = WindowManager::getGraphics()->createDescriptorLayout(materialName + "Layout");
//Shader file needs to conform to the slang standard, which prohibits _
materialName.erase(std::remove(materialName.begin(), materialName.end(), '_'), materialName.end());
materialName.erase(std::remove(materialName.begin(), materialName.end(), '.'), materialName.end());
std::ofstream codeStream("./shaders/generated/"+materialName+".slang");
std::string profile = j["profile"].get<std::string>();
codeStream << "import Material;" << std::endl;
codeStream << "import BRDF;" << std::endl;
codeStream << "import MaterialParameter;" << std::endl << std::endl;
codeStream << "struct " << materialName << " : IMaterial {" << std::endl;
uint32 uniformBufferOffset = 0;
uint32 bindingCounter = 0; // Uniform buffers are always binding 0
uint32 uniformBinding = -1;
Array<PShaderParameter> parameters;
for(auto param : j["params"].items())
{
std::string type = param.value()["type"].get<std::string>();
auto defaultValue = param.value().find("default");
// TODO: ALIGNMENT RULES
if(type.compare("float") == 0)
{
PFloatParameter p = new FloatParameter(param.key(), uniformBufferOffset, 0);
codeStream << "\tlayout(offset = " << uniformBufferOffset << ")";
if(uniformBinding == -1)
{
layout->addDescriptorBinding(bindingCounter, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uniformBinding = bindingCounter++;
}
uniformBufferOffset += 4;
if(defaultValue != param.value().end())
{
p->data = std::stof(defaultValue.value().get<std::string>());
}
parameters.add(p);
}
// TODO: ALIGNMENT RULES
else if(type.compare("float3") == 0)
{
PVectorParameter p = new VectorParameter(param.key(), uniformBufferOffset, 0);
codeStream << "\tlayout(offset = " << uniformBufferOffset << ")";
if(uniformBinding == -1)
{
layout->addDescriptorBinding(bindingCounter, Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uniformBinding = bindingCounter++;
}
uniformBufferOffset += 12;
if(defaultValue != param.value().end())
{
p->data = parseVector(defaultValue.value().get<std::string>().c_str());
}
parameters.add(p);
}
else if(type.compare("Texture2D") == 0)
{
PTextureParameter p = new TextureParameter(param.key(), 0, bindingCounter);
layout->addDescriptorBinding(bindingCounter++, Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
if(defaultValue != param.value().end())
{
std::string defaultString = defaultValue.value().get<std::string>();
p->data = AssetRegistry::findTexture(defaultString);
}
if(p->data == nullptr)
{
p->data = AssetRegistry::findTexture(""); // this will return placeholder texture
}
parameters.add(p);
}
else if(type.compare("SamplerState") == 0)
{
PSamplerParameter p = new SamplerParameter(param.key(), 0, bindingCounter);
layout->addDescriptorBinding(bindingCounter++, Gfx::SE_DESCRIPTOR_TYPE_SAMPLER);
p->data = WindowManager::getGraphics()->createSamplerState({});
parameters.add(p);
}
else
{
std::cout << "Error unsupported parameter type" << std::endl;
}
codeStream << "\t" << type << " " << param.key() << ";\n";
}
uint32 uniformDataSize = uniformBufferOffset;
BRDF* brdf = BRDF::getBRDFByName(profile);
brdf->generateMaterialCode(codeStream, j["code"]);
codeStream << "};";
codeStream.close();
layout->create();
asset->material = new Material(
std::move(parameters),
std::move(layout),
uniformDataSize,
uniformBinding,
materialName
);
graphics->getShaderCompiler()->registerMaterial(asset->material);
asset->setStatus(Asset::Status::Ready);
////co_return;
}
+1 -1
View File
@@ -12,7 +12,7 @@ class MaterialLoader
public:
MaterialLoader(Gfx::PGraphics graphic);
~MaterialLoader();
void importAsset(const std::filesystem::path& name);
void importAsset(const std::filesystem::path& name, const std::string& importPath);
PMaterialAsset getPlaceHolderMaterial();
private:
void import(std::filesystem::path filePath, PMaterialAsset asset);
+1
View File
@@ -1,6 +1,7 @@
#include "MeshAsset.h"
#include "Graphics/Mesh.h"
#include "Graphics/VertexShaderInput.h"
#include "Material/MaterialInterface.h"
using namespace Seele;
+4 -2
View File
@@ -1,10 +1,11 @@
#pragma once
#include "Asset.h"
#include "Component/Collider.h"
namespace Seele
{
DECLARE_REF(Mesh)
DECLARE_REF(MaterialAsset)
DECLARE_REF(MaterialInterface)
class MeshAsset : public Asset
{
public:
@@ -17,8 +18,9 @@ public:
void addMesh(PMesh mesh);
const Array<PMesh> getMeshes();
//Workaround while no editor
Array<PMaterialAsset> referencedMaterials;
Array<PMaterialInterface> referencedMaterials;
Array<PMesh> meshes;
Component::Collider physicsMesh;
};
DEFINE_REF(MeshAsset)
} // namespace Seele
+43 -30
View File
@@ -5,6 +5,7 @@
#include "Graphics/Mesh.h"
#include "Graphics/StaticMeshVertexInput.h"
#include "AssetRegistry.h"
#include "MaterialAsset.h"
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
@@ -26,17 +27,17 @@ MeshLoader::~MeshLoader()
{
}
void MeshLoader::importAsset(const std::filesystem::path &path)
void MeshLoader::importAsset(const std::filesystem::path &path, const std::string& importPath)
{
std::filesystem::path assetPath = path.filename();
assetPath.replace_extension("asset");
PMeshAsset asset = new MeshAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerMesh(asset);
AssetRegistry::get().registerMesh(asset, importPath);
import(path, asset);
}
void MeshLoader::loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials)
void MeshLoader::loadMaterials(const aiScene* scene, Array<PMaterial>& globalMaterials)
{
using json = nlohmann::json;
for(uint32 i = 0; i < scene->mNumMaterials; ++i)
@@ -92,15 +93,13 @@ void MeshLoader::loadMaterials(const aiScene* scene, Array<PMaterialAsset>& glob
outMatFile.close();
std::cout << "writing json to " << outMatFilename << std::endl;
PMaterialAsset result = new MaterialAsset(outMatFilename);
result->load();
graphics->getShaderCompiler()->registerMaterial(result);
AssetRegistry::get().registerMaterial(result);
PMaterialAsset asset = AssetRegistry::findMaterial(result->getFileName());
globalMaterials[i] = asset;
AssetRegistry::importFile(AssetRegistry::getRootFolder() + "/" + outMatFilename);
PMaterialAsset asset = AssetRegistry::findMaterial(matCode["name"].get<std::string>());
globalMaterials[i] = asset->getMaterial();
}
}
void findMeshRoots(aiNode *node, List<aiNode *> &meshNodes)
{
if (node->mNumMeshes > 0)
@@ -115,61 +114,70 @@ void findMeshRoots(aiNode *node, List<aiNode *> &meshNodes)
}
VertexStreamComponent createVertexStream(uint32 size, aiVector3D* sourceData, Gfx::PGraphics graphics)
{
Array<Math::Vector> buffer(size);
Array<Vector> buffer(size);
for(uint32 i = 0; i < size; ++i)
{
buffer[i] = Math::Vector(sourceData[i].x, sourceData[i].y, sourceData[i].z);
}
buffer[i] = Vector(sourceData[i].x, sourceData[i].y, sourceData[i].z);
}
VertexBufferCreateInfo vbInfo;
vbInfo.numVertices = size;
vbInfo.vertexSize = sizeof(Math::Vector);
vbInfo.vertexSize = sizeof(Vector);
vbInfo.resourceData.data = (uint8 *)buffer.data();
vbInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
vbInfo.resourceData.size = sizeof(Math::Vector) * buffer.size();
vbInfo.resourceData.size = sizeof(Vector) * buffer.size();
Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
vertexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS);
return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32B32_SFLOAT);
}
VertexStreamComponent createVertexStream(uint32 size, aiVector2D* sourceData, Gfx::PGraphics graphics)
{
Array<Math::Vector2> buffer(size);
Array<Vector2> buffer(size);
for(uint32 i = 0; i < size; ++i)
{
buffer[i] = Math::Vector2(sourceData[i].x, sourceData[i].y);
buffer[i] = Vector2(sourceData[i].x, sourceData[i].y);
}
VertexBufferCreateInfo vbInfo;
vbInfo.numVertices = size;
vbInfo.vertexSize = sizeof(Math::Vector2);
vbInfo.vertexSize = sizeof(Vector2);
vbInfo.resourceData.data = (uint8 *)buffer.data();
vbInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
vbInfo.resourceData.size = sizeof(Math::Vector2) * buffer.size();
vbInfo.resourceData.size = sizeof(Vector2) * buffer.size();
Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
vertexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS);
return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32_SFLOAT);
}
VertexStreamComponent createVertexStream(uint32 size, aiColor4D* sourceData, Gfx::PGraphics graphics)
{
Array<Math::Vector4> buffer(size);
Array<Vector4> buffer(size);
for(uint32 i = 0; i < size; ++i)
{
buffer[i] = Math::Vector4(sourceData[i].r, sourceData[i].g, sourceData[i].b, sourceData[i].a);
buffer[i] = Vector4(sourceData[i].r, sourceData[i].g, sourceData[i].b, sourceData[i].a);
}
VertexBufferCreateInfo vbInfo;
vbInfo.numVertices = size;
vbInfo.vertexSize = sizeof(Math::Vector4);
vbInfo.vertexSize = sizeof(Vector4);
vbInfo.resourceData.data = (uint8 *)buffer.data();
vbInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
vbInfo.resourceData.size = sizeof(Math::Vector4) * buffer.size();
vbInfo.resourceData.size = sizeof(Vector4) * buffer.size();
Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
vertexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS);
return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32_SFLOAT);
return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32B32A32_SFLOAT);
}
void MeshLoader::loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, const Array<PMaterialAsset>& materials)
void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterial>& materials, Array<PMesh>& globalMeshes, Component::Collider& collider)
{
for (uint32 meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex)
{
aiMesh *mesh = scene->mMeshes[meshIndex];
PMaterialAsset material = materials[mesh->mMaterialIndex];
collider.boundingbox.adjust(Vector(mesh->mAABB.mMin.x, mesh->mAABB.mMin.y, mesh->mAABB.mMin.z));
collider.boundingbox.adjust(Vector(mesh->mAABB.mMax.x, mesh->mAABB.mMax.y, mesh->mAABB.mMax.z));
//! \todo duplicate from createVertexStream, clean up
Array<Vector> vertices(mesh->mNumVertices);
for(uint32 i = 0; i < mesh->mNumVertices; ++i)
{
vertices[i] = Vector(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
}
PStaticMeshVertexInput vertexShaderInput = new StaticMeshVertexInput(std::string(mesh->mName.C_Str()));
StaticMeshDataType data;
data.positionStream = createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics);
@@ -205,6 +213,9 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMesh
indices[faceIndex * 3 + 1] = mesh->mFaces[faceIndex].mIndices[1];
indices[faceIndex * 3 + 2] = mesh->mFaces[faceIndex].mIndices[2];
}
collider.physicsMesh.addCollider(vertices, indices, Matrix4(1.0f));
IndexBufferCreateInfo idxInfo;
idxInfo.indexType = Gfx::SE_INDEX_TYPE_UINT32;
idxInfo.resourceData.data = (uint8 *)indices.data();
@@ -259,22 +270,23 @@ void MeshLoader::import(std::filesystem::path path, PMeshAsset meshAsset)
std::cout << "Starting to import "<<path << std::endl;
meshAsset->setStatus(Asset::Status::Loading);
Assimp::Importer importer;
importer.ReadFile(path.string().c_str(),
importer.ReadFile(path.string().c_str(), (uint32)(
aiProcess_FlipUVs |
aiProcess_Triangulate |
aiProcess_SortByPType |
aiProcess_GenBoundingBoxes |
aiProcess_GenSmoothNormals |
aiProcess_GenUVCoords |
aiProcess_FindDegenerates);
aiProcess_FindDegenerates));
const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace);
Array<PMaterialAsset> globalMaterials(scene->mNumMaterials);
Array<PMaterial> globalMaterials(scene->mNumMaterials);
loadTextures(scene, path.parent_path());
loadMaterials(scene, globalMaterials);
Array<PMesh> globalMeshes(scene->mNumMeshes);
loadGlobalMeshes(scene, globalMeshes, globalMaterials);
Component::Collider collider;
loadGlobalMeshes(scene, globalMaterials, globalMeshes, collider);
List<aiNode *> meshNodes;
findMeshRoots(scene->mRootNode, meshNodes);
@@ -286,6 +298,7 @@ void MeshLoader::import(std::filesystem::path path, PMeshAsset meshAsset)
meshAsset->addMesh(globalMeshes[meshNode->mMeshes[i]]);
}
}
meshAsset->physicsMesh = std::move(collider);
meshAsset->setStatus(Asset::Status::Ready);
meshAsset->save();
std::cout << "Finished loading " << path << std::endl;
+5 -4
View File
@@ -1,6 +1,7 @@
#pragma once
#include "MinimalEngine.h"
#include "Containers/List.h"
#include "Component/Collider.h"
#include <filesystem>
struct aiScene;
@@ -9,18 +10,18 @@ namespace Seele
{
DECLARE_REF(Mesh)
DECLARE_REF(MeshAsset)
DECLARE_REF(MaterialAsset)
DECLARE_REF(Material)
DECLARE_NAME_REF(Gfx, Graphics)
class MeshLoader
{
public:
MeshLoader(Gfx::PGraphics graphic);
~MeshLoader();
void importAsset(const std::filesystem::path& filePath);
void importAsset(const std::filesystem::path& filePath, const std::string& importPath);
private:
void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials);
void loadMaterials(const aiScene* scene, Array<PMaterial>& globalMaterials);
void loadTextures(const aiScene* scene, const std::filesystem::path& meshPath);
void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, const Array<PMaterialAsset>& materials);
void loadGlobalMeshes(const aiScene* scene, const Array<PMaterial>& materials, Array<PMesh>& globalMeshes, Component::Collider& collider);
void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels);
void import(std::filesystem::path path, PMeshAsset meshAsset);
+1
View File
@@ -1,3 +1,4 @@
#pragma once
#include "Asset.h"
namespace Seele
+3 -3
View File
@@ -16,21 +16,21 @@ TextureLoader::TextureLoader(Gfx::PGraphics graphics)
{
placeholderAsset = new TextureAsset(std::filesystem::absolute("./textures/placeholder.ktx"));
placeholderAsset->load();
AssetRegistry::get().textures[""] = placeholderAsset;
AssetRegistry::get().assetRoot.textures[""] = placeholderAsset;
}
TextureLoader::~TextureLoader()
{
}
void TextureLoader::importAsset(const std::filesystem::path& path)
void TextureLoader::importAsset(const std::filesystem::path& path, const std::string& importPath)
{
std::filesystem::path assetPath = path.filename();
assetPath.replace_extension("asset");
PTextureAsset asset = new TextureAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
asset->setTexture(placeholderAsset->getTexture());
AssetRegistry::get().registerTexture(asset);
AssetRegistry::get().registerTexture(asset, importPath);
import(path, asset);
}
+1 -1
View File
@@ -13,7 +13,7 @@ class TextureLoader
public:
TextureLoader(Gfx::PGraphics graphic);
~TextureLoader();
void importAsset(const std::filesystem::path& filePath);
void importAsset(const std::filesystem::path& filePath, const std::string& importPath);
PTextureAsset getPlaceholderTexture();
private:
void import(std::filesystem::path path, PTextureAsset asset);