Started Working on an assetsystem for loading more complex models
This commit is contained in:
		@ -8,18 +8,49 @@ enum class ASSET_TYPE {
 | 
			
		||||
	Unknown = -1,
 | 
			
		||||
	Mesh,
 | 
			
		||||
	Texture,
 | 
			
		||||
	Material
 | 
			
		||||
	Material,
 | 
			
		||||
	Shader,
 | 
			
		||||
	Model,
 | 
			
		||||
	File
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
	virtual ASSET_TYPE GetType() { return detectAssetType(); }
 | 
			
		||||
	
 | 
			
		||||
	const char* GetName() const { return name.c_str(); }
 | 
			
		||||
	void setName(std::string& name) { name = name.c_str(); }
 | 
			
		||||
	void setFilPath(std::string& path) { file = std::filesystem::path(path); }
 | 
			
		||||
 | 
			
		||||
	std::string getId() { return Id.String(); }
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
protected:
 | 
			
		||||
	uuid::v4::UUID Id = uuid::v4::UUID::New();
 | 
			
		||||
 | 
			
		||||
	std::string name;
 | 
			
		||||
	std::filesystem::path file;
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	ASSET_TYPE detectAssetType() {
 | 
			
		||||
		auto ext = (file.extension()).string();
 | 
			
		||||
		if (ext == ".obj" || ext == ".gltf" || ext == ".fbx" || ext == ".stl") {
 | 
			
		||||
			return ASSET_TYPE::Model;
 | 
			
		||||
		}
 | 
			
		||||
		else if (ext == ".yproj") {
 | 
			
		||||
			return ASSET_TYPE::File;
 | 
			
		||||
		}
 | 
			
		||||
		else if (ext == ".vs" || ext == ".fs") {
 | 
			
		||||
			return ASSET_TYPE::File;
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			std::cout << "unknown file!" << std::endl;
 | 
			
		||||
			return ASSET_TYPE::Unknown;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
@ -1,16 +1,51 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "../../YoggieEngine/src/YoggieEngine.h"
 | 
			
		||||
#include "EditorWindow.h"
 | 
			
		||||
#include "AssetManagement/AssetManager.h"
 | 
			
		||||
#include "AssetManagement/AssetRegistry.h"
 | 
			
		||||
 | 
			
		||||
class AssetFinder : EditorWindow {
 | 
			
		||||
const char* hidden_extensions [] {
 | 
			
		||||
    ".exe",
 | 
			
		||||
    ".pdb",
 | 
			
		||||
    ".idb",
 | 
			
		||||
    ".dll",
 | 
			
		||||
    ".ini"
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class AssetFinder : public EditorWindow {
 | 
			
		||||
public:
 | 
			
		||||
    AssetFinder() : EditorWindow("Assets"),
 | 
			
		||||
                    folderIcon ("rsc/FolderIcon.png"),
 | 
			
		||||
                    assetIcon ("rsc/AssetIcon.png")
 | 
			
		||||
    {}
 | 
			
		||||
    AssetFinder () : EditorWindow("Assets") {}
 | 
			
		||||
    AssetFinder(const std::filesystem::path& projectdirectory) : EditorWindow("Assets")
 | 
			
		||||
    {
 | 
			
		||||
        for (auto& dir_entry : std::filesystem::directory_iterator(projectdirectory)) {
 | 
			
		||||
            auto filepath = dir_entry.path();
 | 
			
		||||
 | 
			
		||||
            if (dir_entry.is_directory() || dir_entry.is_symlink() || dir_entry.is_socket())
 | 
			
		||||
                continue;
 | 
			
		||||
 | 
			
		||||
            bool has_hidden_extension = false;
 | 
			
		||||
            for (auto hide : hidden_extensions) {
 | 
			
		||||
                if (filepath.extension() == hide)
 | 
			
		||||
                {
 | 
			
		||||
                    has_hidden_extension = true;
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (has_hidden_extension)
 | 
			
		||||
                continue;
 | 
			
		||||
 | 
			
		||||
            Asset asset(filepath.filename().string().c_str());
 | 
			
		||||
            asset.setFilPath(filepath.string());
 | 
			
		||||
            spdlog::info("Created asset: {0}", asset.GetName());
 | 
			
		||||
            files.push_back(asset);
 | 
			
		||||
        
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void Draw() override {
 | 
			
		||||
 | 
			
		||||
        //assetIcon = YoggieEngine::Texture("rsc/AssetIcon.png");
 | 
			
		||||
        ImGui::DragInt("IconSize", &iconSize, 1, 30, 90);
 | 
			
		||||
        ImGui::DragInt("Maximum Columns", &maxColumns, 1, 1, 6);
 | 
			
		||||
        if (ImGui::BeginTable("##resources", 3))
 | 
			
		||||
@ -20,10 +55,10 @@ public:
 | 
			
		||||
            ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            /*
 | 
			
		||||
            
 | 
			
		||||
            int row = 0;
 | 
			
		||||
            int column = 0;
 | 
			
		||||
            for (auto& asset : AssetManager::assets) {
 | 
			
		||||
            for (auto& asset : files ) {
 | 
			
		||||
                if (column % 3 == 0) {
 | 
			
		||||
                    ImGui::TableNextRow();
 | 
			
		||||
                    column = 0;
 | 
			
		||||
@ -32,27 +67,18 @@ public:
 | 
			
		||||
 | 
			
		||||
                ImGui::TableSetColumnIndex(column);
 | 
			
		||||
 | 
			
		||||
                if (asset.isFolder) {
 | 
			
		||||
                    ImGui::ImageButton(
 | 
			
		||||
                        (ImTextureID)folderIcon.GetID(),
 | 
			
		||||
                        ImVec2{ (float)iconSize,(float)iconSize });
 | 
			
		||||
                    ImGui::Text(asset.GetName(), row);
 | 
			
		||||
                ImGui::ImageButton(
 | 
			
		||||
                    (ImTextureID)assetIcon.GetID(),
 | 
			
		||||
                    ImVec2{ (float)iconSize, (float)iconSize });
 | 
			
		||||
                ImGui::Text(asset.GetName(), row);
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
                else {
 | 
			
		||||
                    ImGui::ImageButton(
 | 
			
		||||
                        (ImTextureID)assetIcon.GetID(),
 | 
			
		||||
                        ImVec2{ (float)iconSize, (float)iconSize });
 | 
			
		||||
                    ImGui::Text(asset.GetName(), row);
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                column++;
 | 
			
		||||
            }
 | 
			
		||||
            */
 | 
			
		||||
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            
 | 
			
		||||
            ImGui::PopStyleColor(3);
 | 
			
		||||
            ImGui::EndTable();
 | 
			
		||||
            const GLuint textures[2]{ assetIcon.GetID(), folderIcon.GetID() };
 | 
			
		||||
@ -62,6 +88,8 @@ public:
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    std::vector <Asset> files = std::vector<Asset>();
 | 
			
		||||
 | 
			
		||||
    int iconSize = 60;
 | 
			
		||||
    int maxColumns = 3;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -85,16 +85,23 @@ std::vector<YoggieEngine::Mesh> processNode(aiNode* node, const aiScene* scene)
 | 
			
		||||
 | 
			
		||||
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!");
 | 
			
		||||
	
 | 
			
		||||
	std::cout << "Loading meshes!" << std::endl;
 | 
			
		||||
 | 
			
		||||
	auto meshes = processNode(currentNode, scene);
 | 
			
		||||
 | 
			
		||||
	std::cout << "Model file contained " << meshes.size() << " meshes" << std::endl;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	return Asset("Mesh");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2,9 +2,7 @@
 | 
			
		||||
#include "AssetLoader.h"
 | 
			
		||||
 | 
			
		||||
class  ModelLoader : AssetLoader {
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
	Asset LoadAsset(std::filesystem::path& path);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
@ -1,35 +1,31 @@
 | 
			
		||||
#include "AssetManager.h"
 | 
			
		||||
#include "AssetRegistry.h"
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <spdlog/spdlog.h>
 | 
			
		||||
#include "AssetLoaders/ModelLoader.h"
 | 
			
		||||
/*
 | 
			
		||||
* this is still a very naive approach to the asset manager
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
void AssetManager::Init() 
 | 
			
		||||
AssetRegistry::AssetRegistry() 
 | 
			
		||||
{
 | 
			
		||||
//	Assets =  std::map<uuid::v4::UUID&, Asset>();
 | 
			
		||||
	modelLoader = ModelLoader();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AssetManager::RegisterAsset(Asset& asset) 
 | 
			
		||||
 | 
			
		||||
void AssetRegistry::RegisterAsset(Asset& asset) 
 | 
			
		||||
{
 | 
			
		||||
//	Assets.try_emplace(asset.id, asset);
 | 
			
		||||
	Assets.try_emplace(asset.getId() , asset);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void AssetManager::UnregisterAsset(Asset& asset) {
 | 
			
		||||
//	auto old = Assets[asset.id];
 | 
			
		||||
//	Assets[asset.id] = NULL;
 | 
			
		||||
void AssetRegistry::UnregisterAsset(Asset& asset) {
 | 
			
		||||
	Assets.erase(asset.getId());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path assetPath)
 | 
			
		||||
YoggieEngine::Mesh* AssetRegistry::LoadFromAssetFile(const std::filesystem::path assetPath)
 | 
			
		||||
{
 | 
			
		||||
	YoggieEngine::Mesh* imported = nullptr;
 | 
			
		||||
 | 
			
		||||
@ -89,7 +85,7 @@ YoggieEngine::Mesh* AssetManager::LoadFromAssetFile(const std::filesystem::path
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
YoggieEngine::Renderable* AssetManager::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder)
 | 
			
		||||
YoggieEngine::Renderable* AssetRegistry::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
@ -1,23 +1,26 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include "Asset.h"
 | 
			
		||||
#include "AssetLoaders/ModelLoader.h"
 | 
			
		||||
#include "uuid.h"
 | 
			
		||||
 | 
			
		||||
class AssetManager {
 | 
			
		||||
class AssetRegistry {
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
	void Init();
 | 
			
		||||
	
 | 
			
		||||
	AssetRegistry();
 | 
			
		||||
	//AssetRegistry(AssetPack);
 | 
			
		||||
 | 
			
		||||
	void RegisterAsset(Asset& asset);
 | 
			
		||||
	void UnregisterAsset(Asset& asset);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	void Update();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath);
 | 
			
		||||
	static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder);
 | 
			
		||||
	
 | 
			
		||||
private: 
 | 
			
		||||
	std::map<uuid::v4::UUID , Asset> Assets;
 | 
			
		||||
	ModelLoader modelLoader;
 | 
			
		||||
 | 
			
		||||
	int unique_number = 0;
 | 
			
		||||
	std::map<std::string , Asset> Assets = std::map<std::string, Asset> ();
 | 
			
		||||
};
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
#include "MainMenuBar.h"
 | 
			
		||||
#include <nfd.h>
 | 
			
		||||
#include "AssetManagement/AssetManager.h"
 | 
			
		||||
#include "AssetManagement/AssetRegistry.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -19,8 +19,8 @@ void MainMenuBar::ApplicationMenu(Project& project) {
 | 
			
		||||
            switch (result) {
 | 
			
		||||
            case(NFD_OKAY):
 | 
			
		||||
                Project::LoadProject(path, project);
 | 
			
		||||
                //AssetManager::setAssetPath(project.GetProjectDirectory());
 | 
			
		||||
                //AssetManager::BuildAssetView();
 | 
			
		||||
                //AssetRegistry::setAssetPath(project.GetProjectDirectory());
 | 
			
		||||
                //AssetRegistry::BuildAssetView();
 | 
			
		||||
                break;
 | 
			
		||||
            case(NFD_CANCEL):
 | 
			
		||||
                break;
 | 
			
		||||
@ -116,7 +116,7 @@ void MainMenuBar::SceneMenu(Project& project, YoggieEngine::Scene& scene) {
 | 
			
		||||
            case(NFD_OKAY):
 | 
			
		||||
                // Import Model
 | 
			
		||||
 | 
			
		||||
                AssetManager::LoadFromSource(
 | 
			
		||||
                AssetRegistry::LoadFromSource(
 | 
			
		||||
                    path,
 | 
			
		||||
                    "build/Debug/Assets"//project.get()->GetProjectDirectory() / "Assets"
 | 
			
		||||
                );
 | 
			
		||||
@ -136,7 +136,7 @@ void MainMenuBar::SceneMenu(Project& project, YoggieEngine::Scene& scene) {
 | 
			
		||||
            switch (result) {
 | 
			
		||||
            case(NFD_OKAY):
 | 
			
		||||
            {
 | 
			
		||||
                YoggieEngine::Mesh* importedMesh = AssetManager::LoadFromAssetFile(path);
 | 
			
		||||
                YoggieEngine::Mesh* importedMesh = AssetRegistry::LoadFromAssetFile(path);
 | 
			
		||||
                if (importedMesh != nullptr)
 | 
			
		||||
                {
 | 
			
		||||
                    auto full_name = std::filesystem::path(path);
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user