Moved model file loading logic to the editor code base

This commit is contained in:
Nigel Barink 2023-05-06 21:06:49 +02:00
parent 89f5b1497f
commit 28927d9a4e
18 changed files with 260 additions and 233 deletions

View File

@ -2,6 +2,7 @@
#include "../../YoggieEngine/src/YoggieEngine.h"
#include <filesystem>
#include <string>
#include "uuid.h"
enum class ASSET_TYPE {
Unknown = -1,
@ -13,34 +14,12 @@ enum class ASSET_TYPE {
class Asset {
public:
Asset(const char* name): name(name) {}
uuid::v4::UUID id = uuid::v4::UUID::New();
virtual ASSET_TYPE GetType() { return ASSET_TYPE::Unknown; }
const char* GetName() const { return name.c_str(); }
bool isFolder = false ;
protected:
std::string name;
};
class MeshAsset : Asset {
public:
MeshAsset(YoggieEngine::Mesh mesh) : Asset("New MeshAsset"), mesh(mesh) {}
ASSET_TYPE GetType() override { return ASSET_TYPE::Mesh; }
private:
YoggieEngine::Mesh& mesh;
};
class TextureAsset : Asset {
public:
TextureAsset (YoggieEngine::Texture texture): Asset("New TextureAsset"), texture(texture) {}
ASSET_TYPE GetType() override { return ASSET_TYPE::Texture; }
private:
YoggieEngine::Texture& texture;
};
class MaterialAsset : Asset {
public:
MaterialAsset () : Asset("New MaterialAsset"){}
ASSET_TYPE GetType() override { return ASSET_TYPE::Material; }
private:
};

View File

@ -5,24 +5,22 @@
class AssetFinder : EditorWindow {
public:
AssetFinder() : EditorWindow("Assets") {}
AssetFinder() : EditorWindow("Assets"),
folderIcon ("rsc/FolderIcon.png"),
assetIcon ("rsc/AssetIcon.png")
{}
void Draw() override {
ImGui::DragInt("IconSize", &iconSize, 1, 30, 90);
ImGui::DragInt("Maximum Columns", &maxColumns, 1, 1, 6);
if (ImGui::BeginTable("##resources", 3))
{
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.f, 0.f, 0.f, 0.f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.f, 0.f, 0.f, 0.f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f));
YoggieEngine::Texture folderIcon = YoggieEngine::Texture("rsc/folderIcon.png");
YoggieEngine::Texture assetIcon = YoggieEngine::Texture("rsc/assetIcon.png");
/*
int row = 0;
int column = 0;
for (auto& asset : AssetManager::assets) {
@ -52,6 +50,7 @@ public:
column++;
}
*/
ImGui::PopStyleColor(3);
@ -64,5 +63,10 @@ public:
private:
int iconSize = 60;
int maxColumns = 3;
YoggieEngine::Texture folderIcon;
YoggieEngine::Texture assetIcon;
};

View File

@ -0,0 +1,9 @@
#pragma once
#include <filesystem>
#include "../Asset.h"
class AssetLoader {
public:
virtual Asset LoadAsset(std::filesystem::path& path) = 0;
// virtual void PackageAsset(Asset& asset ) = 0;
};

View File

@ -0,0 +1,102 @@
#include "ModelLoader.h"
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
void ProcessVertices(aiMesh* mesh, std::vector<YoggieEngine::Vertex>& out_vertices) {
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
YoggieEngine::Vertex v{};
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
v.vertices = vector;
if (mesh->mTextureCoords[0]) {
glm::vec2 texCoord;
texCoord.x = mesh->mTextureCoords[0][i].x;
texCoord.y = mesh->mTextureCoords[0][i].y;
v.uv = texCoord;
}
out_vertices.push_back(v);
}
}
void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices) {
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
if (face.mNumIndices < 3)
continue;
for (unsigned int j = 0; j < face.mNumIndices; j++) {
out_indices.push_back(face.mIndices[j]);
}
}
}
YoggieEngine::Mesh processMesh(aiMesh* mesh, const aiScene* scene) {
std::vector<unsigned int> indices;
std::vector<YoggieEngine::Vertex> vertices;
ProcessVertices(mesh, vertices);
ProcessIndices(mesh, indices);
YoggieEngine::Mesh result;
result.vertices = vertices;
result.elements = indices;
return result;
}
std::vector<YoggieEngine::Mesh> processNode(aiNode* node, const aiScene* scene) {
std::vector<YoggieEngine::Mesh> meshes = std::vector<YoggieEngine::Mesh>();
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
auto m2 = processNode(node->mChildren[i], scene);
for (auto m : m2) {
meshes.push_back(m);
}
}
return meshes;
}
Asset ModelLoader::LoadAsset(std::filesystem::path& path) {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path.string(), aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode;
spdlog::debug("Loading meshes!");
auto meshes = processNode(currentNode, scene);
return Asset("Mesh");
}

View File

@ -0,0 +1,10 @@
#pragma once
#include "AssetLoader.h"
class ModelLoader : AssetLoader {
Asset LoadAsset(std::filesystem::path& path);
};

View File

@ -1,42 +1,34 @@
#include "AssetManager.h"
#include "../../YoggieEngine/src/AssetManager/ModelImporter.h"
#include <iostream>
#include <fstream>
#include <spdlog/spdlog.h>
std::vector<Asset> AssetManager::assets;
std::filesystem::path AssetManager::currentPath;
#include "AssetLoaders/ModelLoader.h"
/*
* this is still a very naive approach to the asset manager
*/
void AssetManager::Init()
{
assets = std::vector<Asset>();
currentPath = std::filesystem::path(".");
}
void AssetManager::BuildAssetView()
{
if (currentPath.empty()) {
return;
}
for (auto& dir_entry : std::filesystem::directory_iterator(currentPath))
{
auto asset = Asset(dir_entry.path().filename().string().c_str());
if (dir_entry.is_directory()) {
asset.isFolder = true;
}
assets.push_back(asset);
}
// Assets = std::map<uuid::v4::UUID&, Asset>();
modelLoader = ModelLoader();
}
void AssetManager::setAssetPath(std::filesystem::path path)
{
currentPath = path;
void AssetManager::RegisterAsset(Asset& asset)
{
// Assets.try_emplace(asset.id, asset);
}
void AssetManager::UnregisterAsset(Asset& asset) {
// auto old = Assets[asset.id];
// Assets[asset.id] = NULL;
}
YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath)
{
YoggieEngine::Mesh* imported = nullptr;
@ -100,7 +92,8 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path
YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder)
{
auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string());
/*
* auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string());
YoggieEngine::Mesh* exportMesh = model->renderable->mesh;
std::filesystem::path MeshFileName = assetFolder / srcPath.filename().replace_extension(".mesh");
std::cout << "Save path: " << MeshFileName << std::endl;
@ -145,4 +138,6 @@ YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::pa
return model->renderable;
*/
return nullptr;
}

View File

@ -1,20 +1,23 @@
#pragma once
#include <vector>
#include "Asset.h"
#include "AssetLoaders/ModelLoader.h"
#include "uuid.h"
class AssetManager {
public:
static void Init();
static void BuildAssetView();
static void setAssetPath(std::filesystem::path path);
void Init();
void RegisterAsset(Asset& asset);
void UnregisterAsset(Asset& asset);
static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath);
static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder);
static std::vector<Asset> assets ;
private:
static std::filesystem::path currentPath;
private:
std::map<uuid::v4::UUID , Asset> Assets;
ModelLoader modelLoader;
};

View File

@ -0,0 +1,63 @@
/*
*
* SOURCE: https://github.com/rkg82/uuid-v4/blob/main/uuid/v4/uuid.h
*
*/
#ifndef __UUID__
#define __UUID__
#include <random>
#include <string>
namespace uuid::v4
{
// Encaasulate the genaeration of a Version 4 UUID object
// A Version 4 UUID is a universally unique identifier that is generated using random numbers.
class UUID
{
public:
// Factory method for creating UUID object.
static UUID New()
{
UUID uuid;
std::random_device rd;
std::mt19937 engine{ rd() };
std::uniform_int_distribution<int> dist{ 0, 256 }; //Limits of the interval
for (int index = 0; index < 16; ++index)
{
uuid._data[index] = (unsigned char)dist(engine);
}
uuid._data[6] = ((uuid._data[6] & 0x0f) | 0x40); // Version 4
uuid._data[8] = ((uuid._data[8] & 0x3f) | 0x80); // Variant is 10
return uuid;
}
// Returns UUID as formatted string
std::string String()
{
// Formats to "0065e7d7-418c-4da4-b4d6-b54b6cf7466a"
char buffer[256] = { 0 };
std::snprintf(buffer, 255,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
_data[0], _data[1], _data[2], _data[3],
_data[4], _data[5],
_data[6], _data[7],
_data[8], _data[9],
_data[10], _data[11], _data[12], _data[13], _data[14], _data[15]);
std::string uuid = buffer;
return uuid;
}
private:
UUID() {}
unsigned char _data[16] = { 0 };
};
};
#endif // #ifndef __UUID__

View File

@ -19,8 +19,8 @@ void MainMenuBar::ApplicationMenu(Project& project) {
switch (result) {
case(NFD_OKAY):
Project::LoadProject(path, project);
AssetManager::setAssetPath(project.GetProjectDirectory());
AssetManager::BuildAssetView();
//AssetManager::setAssetPath(project.GetProjectDirectory());
//AssetManager::BuildAssetView();
break;
case(NFD_CANCEL):
break;

View File

@ -12,6 +12,7 @@
#include "PropertyPanels/Inspector.h"
#include "Project/ProjectInfo.h"
#include "Runtime/RuntimeControls.h"
#include "AssetManagement/uuid.h"
#include "Project/Settings.h"
#include "Console.h"
@ -26,21 +27,19 @@ public:
std::string path = (std::filesystem::current_path()).string();
project.setProjectDirectory(path);
AssetManager::Init();
AssetManager::setAssetPath(project.GetProjectDirectory());
AssetManager::BuildAssetView();
LoadLastOrEmptyProject();
ProjectInfo projectInfo(project);
Viewport sceneview = Viewport(scene);
//ProjectInfo projectInfo(project);
RuntimeControls rc = RuntimeControls();
Viewport sceneview = Viewport(scene);
SceneExplorer explorer(Selected, scene);
Inspector inspector = Inspector(Selected);
Settings settings = Settings();
// AssetFinder assetsView = AssetFinder();
Console console = Console();
//Settings settings = Settings();
AssetFinder assetsView = AssetFinder();
//Console console = Console();
Selected = YoggieEngine::Entity((entt::entity) -1, &scene);
double previous = glfwGetTime();
@ -77,16 +76,18 @@ public:
}
projectInfo.Update();
//projectInfo.Update();
sceneview.Update();
rc.Update();
explorer.Update();
settings.Update();
//settings.Update();
inspector.Update();
console.Update();
//console.Update();
ImGui::ShowDemoWindow();
ImGui::ShowMetricsWindow();
assetsView.Draw();
//ImGui::ShowDemoWindow();
//ImGui::ShowMetricsWindow();
GuiEnd();

View File

@ -71,8 +71,8 @@ project "YoggieEngine"
files {
"./src/**.cpp",
"./src/**.h"
"src/**.cpp",
"src/**.h"
}
prebuildcommands

View File

@ -59,9 +59,12 @@ namespace YoggieEngine {
ImGui::NewFrame();
ImGuizmo::BeginFrame();
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport());
}
void Application::GuiEnd() {
ImGui::EndFrame();

View File

@ -1,102 +0,0 @@
#include <YoggieEngine.h>
#include "ModelImporter.h"
namespace YoggieEngine {
SceneObject* ModelImporter::Import(const std::string path)
{
SceneObject* root = new SceneObject(std::string(path), nullptr);
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode;
std::vector<Mesh> meshes = processNode(currentNode, scene);
std::cout << "[DEBUG]: Loaded " << meshes.size() << " meshes!" << std::endl;
// create a renderable (per mesh ?? )
root->renderable = new Renderable();
root->renderable->mesh = new Mesh(meshes[0]);
return root;
}
std::vector<Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene)
{
std::vector<Mesh> meshes;
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
auto m2 = processNode(node->mChildren[i], scene);
for (auto m : m2) {
meshes.push_back(m);
}
}
return meshes;
}
Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) {
std::vector<unsigned int> indices;
std::vector<Vertex> vertices;
ProcessVertices(mesh, vertices);
ProcessIndices(mesh, indices);
Mesh result;
result.vertices = vertices;
result.elements = indices;
return result;
}
void ProcessVertices(aiMesh* mesh, std::vector<Vertex>& out_vertices) {
// Process vertices
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
Vertex v{};
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
v.vertices = vector;
if (mesh->mTextureCoords[0]) {
glm::vec2 texCoord;
texCoord.x = mesh->mTextureCoords[0][i].x;
texCoord.y = mesh->mTextureCoords[0][i].y;
v.uv = texCoord;
}
out_vertices.push_back(v);
}
}
void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices) {
// Process Indices
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
if (face.mNumIndices < 3)
continue;
for (unsigned int j = 0; j < face.mNumIndices; j++) {
out_indices.push_back(face.mIndices[j]);
}
}
}
}

View File

@ -1,29 +0,0 @@
#pragma once
#include "../Graphics/Primitives/Mesh.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <string>
#include "../Scene/TransformTree/SceneNodeTypes.h"
namespace YoggieEngine {
void ProcessVertices(aiMesh* mesh, std::vector<Vertex>& out_vertices);
void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices);
class ModelImporter {
public:
SceneObject* Import(const std::string path);
private:
static Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene);
static std::vector<Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene);
};
}

View File

@ -27,7 +27,7 @@ namespace YoggieEngine {
}
else {
spdlog::error("Failed to load image (%s)", texturePath);
spdlog::error("Failed to load image ({0})", texturePath);
}
stbi_image_free(data);

View File

@ -435,17 +435,6 @@ void Renderer::Render(Scene& scene)
BlendingPass();
//PostProcessing();
// Lighting pass
/*
auto lights = scene.getReg().view<LightComponent>();
lights.each([&](auto entity, LightComponent& light) {
renderComponent.shader.setUniformVec3("lighting.color", light.Color);
renderComponent.shader.setUniformFloat("lighting.strength", light.Strength);
});
*/
commands.clear();
glBindFramebuffer(GL_FRAMEBUFFER, 0);

View File

@ -25,7 +25,7 @@ void main()
float Specular = texture(gColorSpec, TexCoords).a;
// calculate lighting as usual
vec3 lighting = Albedo * 0.1; // Hard-coded ambient light
vec3 lighting = Albedo * 0.0; // Hard-coded ambient light
vec3 viewDir = normalize(viewPos - FragPos);
for( int i = 0; i < NR_LIGHTS; ++i)
{

View File

@ -40,8 +40,8 @@ group("Other")
group("Libraries")
include('../ImGui')
include("../ImGuizmo")
include("../yaml-cpp")
include('ImGui')
include("ImGuizmo")
include("yaml-cpp")