Fixed scene loadingstuff, and Asset manager models loader

This commit is contained in:
OusmBlueNinja 2024-12-26 23:31:12 -06:00
parent 181f46e5a8
commit 4876a6c2dd
11 changed files with 13720 additions and 70 deletions

View File

@ -0,0 +1,167 @@
# Blender MTL File: 'FiatUNO.blend'
# Material Count: 15
newmtl Body
Ns 94.117647
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .
newmtl Fari
Ns 94.117647
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .
newmtl Finestrini
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .
newmtl Finestrini.001
Ns 94.117647
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .
newmtl GrigliaAnt
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .
newmtl GrigliaPost
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .
newmtl Parabrezza
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd Parabrezza.png
newmtl Ruote
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd Ruote Diffuse Color.png
newmtl Ruote.001
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd Ruote Diffuse Color.png
newmtl Ruote.002
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd Ruote Diffuse Color.png
newmtl Ruote.005
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd Ruote Diffuse Color.png
newmtl StopDX
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .
newmtl StopSX.001
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .
newmtl Targa
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd C:\\Users\\DeveLion\\Desktop\\Targa Diffuse Color.png
newmtl VetroPost
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd .

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[Window][DockSpace] [Window][DockSpace]
Pos=0,0 Pos=0,0
Size=1920,1177 Size=1280,720
Collapsed=0 Collapsed=0
[Window][Debug##Default] [Window][Debug##Default]
@ -9,26 +9,26 @@ Size=400,400
Collapsed=0 Collapsed=0
[Window][Inspector] [Window][Inspector]
Pos=1588,27 Pos=948,27
Size=324,587 Size=324,448
Collapsed=0 Collapsed=0
DockId=0x00000005,0 DockId=0x00000005,0
[Window][OpenGL Output] [Window][OpenGL Output]
Pos=374,27 Pos=374,27
Size=1212,770 Size=572,313
Collapsed=0 Collapsed=0
DockId=0x00000003,0 DockId=0x00000003,0
[Window][Performance] [Window][Performance]
Pos=1588,616 Pos=948,477
Size=324,553 Size=324,235
Collapsed=0 Collapsed=0
DockId=0x00000006,0 DockId=0x00000006,0
[Window][Logger] [Window][Logger]
Pos=374,799 Pos=374,342
Size=1212,370 Size=572,370
Collapsed=0 Collapsed=0
DockId=0x00000004,0 DockId=0x00000004,0
@ -40,20 +40,20 @@ DockId=0x00000007,0
[Window][Scene Window] [Window][Scene Window]
Pos=8,27 Pos=8,27
Size=364,1142 Size=364,685
Collapsed=0 Collapsed=0
DockId=0x00000009,0 DockId=0x00000009,0
[Docking][Data] [Docking][Data]
DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,27 Size=1904,1142 Split=X Selected=0xF7365A5A DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,27 Size=1264,685 Split=X Selected=0xF7365A5A
DockNode ID=0x00000009 Parent=0x14621557 SizeRef=364,1142 HiddenTabBar=1 Selected=0x3DC5AC3F DockNode ID=0x00000009 Parent=0x14621557 SizeRef=364,1142 HiddenTabBar=1 Selected=0x3DC5AC3F
DockNode ID=0x0000000A Parent=0x14621557 SizeRef=1538,1142 Split=X DockNode ID=0x0000000A Parent=0x14621557 SizeRef=1538,1142 Split=X
DockNode ID=0x00000007 Parent=0x0000000A SizeRef=357,1142 Selected=0x7737E8B2 DockNode ID=0x00000007 Parent=0x0000000A SizeRef=357,1142 Selected=0x7737E8B2
DockNode ID=0x00000008 Parent=0x0000000A SizeRef=1545,1142 Split=X DockNode ID=0x00000008 Parent=0x0000000A SizeRef=1545,1142 Split=X
DockNode ID=0x00000001 Parent=0x00000008 SizeRef=1211,1142 Split=Y Selected=0xF7365A5A DockNode ID=0x00000001 Parent=0x00000008 SizeRef=1212,1142 Split=Y Selected=0xF7365A5A
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1578,770 CentralNode=1 HiddenTabBar=1 Selected=0xF7365A5A DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1578,770 CentralNode=1 HiddenTabBar=1 Selected=0xF7365A5A
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1578,370 HiddenTabBar=1 Selected=0x9DD4E196 DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1578,370 HiddenTabBar=1 Selected=0x9DD4E196
DockNode ID=0x00000002 Parent=0x00000008 SizeRef=324,1142 Split=Y Selected=0x36DC96AB DockNode ID=0x00000002 Parent=0x00000008 SizeRef=324,1142 Split=Y Selected=0x36DC96AB
DockNode ID=0x00000005 Parent=0x00000002 SizeRef=324,587 HiddenTabBar=1 Selected=0x36DC96AB DockNode ID=0x00000005 Parent=0x00000002 SizeRef=324,748 HiddenTabBar=1 Selected=0x36DC96AB
DockNode ID=0x00000006 Parent=0x00000002 SizeRef=324,553 HiddenTabBar=1 Selected=0x726D8899 DockNode ID=0x00000006 Parent=0x00000002 SizeRef=324,392 HiddenTabBar=1 Selected=0x726D8899

23
scenes/Car.scene Normal file
View File

@ -0,0 +1,23 @@
Entities:
- ID: 0
Name: Default
Components:
Mesh:
vao: 2
indexCount: 15810
textureID: 1
Transform:
Position: [0, 1.39999998, 0]
Rotation: [-180, -115.599998, 0]
Scale: [1, 1, 1]
- ID: 1
Name: New GameObject 1
Components:
Mesh:
vao: 1
indexCount: 36
textureID: 2
Transform:
Position: [-4.5999999, -2.29999995, -5.4000001]
Rotation: [0, 0, 0]
Scale: [1, 1, 1]

12
scenes/Default.scene Normal file
View File

@ -0,0 +1,12 @@
Entities:
- ID: 0
Name: Default
Components:
Mesh:
vao: 1
indexCount: 36
textureID: 1
Transform:
Position: [0, 0, 0]
Rotation: [0, 0.5, 0]
Scale: [1, 1, 1]

View File

@ -1,14 +1,14 @@
Entities: Entities:
- ID: 0 - ID: 0
Name: Default Name: Carrrrr Yeaaa
Components: Components:
Mesh: Mesh:
vao: 1 vao: 2
indexCount: 36 indexCount: 200000
textureID: 1 textureID: 1
Transform: Transform:
Position: [0, 2.79999995, -12.6000004] Position: [0, 2.79999995, -12.6000004]
Rotation: [0, 0.5, -20.6000004] Rotation: [149.699997, -137.899994, -39.2999992]
Scale: [1, 1, 1] Scale: [1, 1, 1]
- ID: 1 - ID: 1
Name: Cube Yay Name: Cube Yay
@ -22,7 +22,7 @@ Entities:
Rotation: [-9, -18.6000004, -28.1000004] Rotation: [-9, -18.6000004, -28.1000004]
Scale: [1, 1, 1] Scale: [1, 1, 1]
- ID: 2 - ID: 2
Name: Cube Yay Name: Cube Yay 2
Components: Components:
Mesh: Mesh:
vao: 1 vao: 1
@ -33,7 +33,7 @@ Entities:
Rotation: [-86.3000031, 0, -66] Rotation: [-86.3000031, 0, -66]
Scale: [1, 1, 1] Scale: [1, 1, 1]
- ID: 3 - ID: 3
Name: Cube Yay Name: Cube Yay 3
Components: Components:
Mesh: Mesh:
vao: 1 vao: 1
@ -44,7 +44,7 @@ Entities:
Rotation: [-23.5, 15.8999996, -59.9000015] Rotation: [-23.5, 15.8999996, -59.9000015]
Scale: [1, 1, 1] Scale: [1, 1, 1]
- ID: 4 - ID: 4
Name: Cube Yay Name: Cube Yay 4
Components: Components:
Mesh: Mesh:
vao: 1 vao: 1
@ -55,7 +55,7 @@ Entities:
Rotation: [-17.2999992, -16.1000004, -19.2999992] Rotation: [-17.2999992, -16.1000004, -19.2999992]
Scale: [1, 1, 1] Scale: [1, 1, 1]
- ID: 5 - ID: 5
Name: Cube Yay Name: Cube Yay 5
Components: Components:
Transform: Transform:
Position: [-6.80000019, 2.70000005, -13.8000002] Position: [-6.80000019, 2.70000005, -13.8000002]
@ -66,7 +66,7 @@ Entities:
indexCount: 36 indexCount: 36
textureID: 6 textureID: 6
- ID: 6 - ID: 6
Name: Cube Yay Name: Cube Yay 6
Components: Components:
Mesh: Mesh:
vao: 1 vao: 1
@ -77,7 +77,7 @@ Entities:
Rotation: [15.8000002, -18.2000008, -11.1000004] Rotation: [15.8000002, -18.2000008, -11.1000004]
Scale: [1, 1, 1] Scale: [1, 1, 1]
- ID: 7 - ID: 7
Name: Cube Yay Name: Cube Yay 7
Components: Components:
Mesh: Mesh:
vao: 1 vao: 1
@ -88,7 +88,7 @@ Entities:
Rotation: [-16.1000004, -15.8999996, -35] Rotation: [-16.1000004, -15.8999996, -35]
Scale: [1, 1, 1] Scale: [1, 1, 1]
- ID: 8 - ID: 8
Name: Cube Yay Name: Cube Yay 8
Components: Components:
Mesh: Mesh:
vao: 1 vao: 1
@ -99,7 +99,7 @@ Entities:
Rotation: [22.5, -34.2999992, 0] Rotation: [22.5, -34.2999992, 0]
Scale: [1, 1, 1] Scale: [1, 1, 1]
- ID: 9 - ID: 9
Name: Cube Yay Name: Cube Yay 9
Components: Components:
Mesh: Mesh:
vao: 1 vao: 1

View File

@ -175,6 +175,18 @@ void MyEngine::Run()
g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/default.png"); g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/default.png");
g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/lush_grass.png"); g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/lush_grass.png");
// Load a model
void* modelPtr = g_AssetManager.loadAsset(AssetType::MODEL, "assets/models/LowPolyFiatUNO.obj");
if (modelPtr == nullptr)
{
DEBUG_PRINT("Failed to load model.");
}
else
{
Model* model = reinterpret_cast<Model*>(modelPtr);
DEBUG_PRINT("Model loaded successfully with %lld vertices and %lld indices.", model->vertices.size(), model->indices.size());
}
g_GameObjects.push_back(newGameObject); g_GameObjects.push_back(newGameObject);
DEBUG_PRINT("Put componenent into Global Componenets Subsystem"); DEBUG_PRINT("Put componenent into Global Componenets Subsystem");
@ -324,14 +336,14 @@ void MyEngine::ShowDockSpace()
if (ImGui::MenuItem("Save")) if (ImGui::MenuItem("Save"))
{ {
m_LoggerWindow->AddLog("Saveing Scene", ImVec4(0.3f, 1.0f, 0.3f, 1.0f)); m_LoggerWindow->AddLog("Saveing Scene", ImVec4(0.3f, 1.0f, 0.3f, 1.0f));
g_SceneManager.SaveScene(g_GameObjects, "./Default.scene"); g_SceneManager.SaveScene(g_GameObjects, "./scenes/Default.scene");
} }
if (ImGui::MenuItem("Load")) if (ImGui::MenuItem("Load"))
{ {
m_LoggerWindow->AddLog("Loading Scene", ImVec4(0.3f, 1.0f, 0.3f, 1.0f)); m_LoggerWindow->AddLog("Loading Scene", ImVec4(0.3f, 1.0f, 0.3f, 1.0f));
g_SceneManager.LoadScene(g_GameObjects, "./Default.scene"); g_SceneManager.LoadScene(g_GameObjects, "./scenes/Default.scene");
} }

View File

@ -1,23 +1,37 @@
#include "Engine/AssetManager.h" #include "Engine/AssetManager.h"
#include <iostream> #include <iostream>
#include <fstream>
// Include your Shader class #include <sstream>
#include "Rendering/Shader.h" #include <cstring>
#include "Windows/LoggerWindow.h"
// Include OpenGL loader (GLEW) for texture creation
#include <GL/glew.h> #include <GL/glew.h>
#include <vector>
// For texture loading
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h" #include "stb/stb_image.h"
#include "imgui.h"
#include "Windows/LoggerWindow.h"
#include "Rendering/Shader.h"
#include "gcml.h"
#include <filesystem>
int LoaddedAssets = 0; int LoaddedAssets = 0;
extern LoggerWindow *g_LoggerWindow; extern LoggerWindow *g_LoggerWindow;
void* AssetManager::loadAsset(AssetType type, const std::string& path)
std::string getDirectoryPath(const std::string& fullPath) {
std::filesystem::path pathObj(fullPath);
std::filesystem::path dir = pathObj.parent_path();
return dir.string();
}
void *AssetManager::loadAsset(AssetType type, const std::string &path)
{ {
// 1) Create a unique key for cache lookup // 1) Create a unique key for cache lookup
std::string key = generateKey(type, path); std::string key = generateKey(type, path);
@ -31,10 +45,13 @@ void* AssetManager::loadAsset(AssetType type, const std::string& path)
} }
// 3) Not loaded yet, load from disk // 3) Not loaded yet, load from disk
void* assetData = loadAssetFromDisk(type, path); void *assetData = loadAssetFromDisk(type, path);
if (!assetData) if (!assetData)
{ {
std::cerr << "[AssetManager] Failed to load asset: " << path << std::endl;
DEBUG_PRINT("[AssetManager] Failed to load asset: %s", path.c_str());
g_LoggerWindow->AddLog("[AsseetManager] Failed to load asset: %s", ImVec4(1.0f, 0.0f, 0.0f, 1.0f), path.c_str());
return nullptr; return nullptr;
} }
@ -51,12 +68,12 @@ void* AssetManager::loadAsset(AssetType type, const std::string& path)
return assetData; return assetData;
} }
std::string AssetManager::generateKey(AssetType type, const std::string& path) std::string AssetManager::generateKey(AssetType type, const std::string &path)
{ {
return std::to_string(static_cast<int>(type)) + ":" + path; return std::to_string(static_cast<int>(type)) + ":" + path;
} }
void* AssetManager::loadAssetFromDisk(AssetType type, const std::string& path) void *AssetManager::loadAssetFromDisk(AssetType type, const std::string &path)
{ {
switch (type) switch (type)
{ {
@ -68,7 +85,7 @@ void* AssetManager::loadAssetFromDisk(AssetType type, const std::string& path)
std::cout << "[AssetManager] Loading TEXTURE from: " << path << std::endl; std::cout << "[AssetManager] Loading TEXTURE from: " << path << std::endl;
int width, height, channels; int width, height, channels;
unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 0); unsigned char *data = stbi_load(path.c_str(), &width, &height, &channels, 0);
if (!data) if (!data)
{ {
std::cerr << "[AssetManager] stb_image failed for: " << path << std::endl; std::cerr << "[AssetManager] stb_image failed for: " << path << std::endl;
@ -76,8 +93,10 @@ void* AssetManager::loadAssetFromDisk(AssetType type, const std::string& path)
} }
GLenum format = GL_RGBA; GLenum format = GL_RGBA;
if (channels == 1) format = GL_RED; if (channels == 1)
else if (channels == 3) format = GL_RGB; format = GL_RED;
else if (channels == 3)
format = GL_RGB;
// if channels == 4, already GL_RGBA // if channels == 4, already GL_RGBA
GLuint texID = 0; GLuint texID = 0;
@ -99,7 +118,7 @@ void* AssetManager::loadAssetFromDisk(AssetType type, const std::string& path)
stbi_image_free(data); stbi_image_free(data);
// Return as void* // Return as void*
return reinterpret_cast<void*>(static_cast<uintptr_t>(texID)); return reinterpret_cast<void *>(static_cast<uintptr_t>(texID));
} }
case AssetType::SHADER: case AssetType::SHADER:
@ -107,12 +126,12 @@ void* AssetManager::loadAssetFromDisk(AssetType type, const std::string& path)
// -------------------------------------------- // --------------------------------------------
// Load a shader using your existing "Shader" class // Load a shader using your existing "Shader" class
// -------------------------------------------- // --------------------------------------------
// Example usage: path = "shaders/UnlitMaterial" => // Example usage: path = "shaders/UnlitMaterial" =>
// loads "shaders/UnlitMaterial.vert" and "shaders/UnlitMaterial.frag" // loads "shaders/UnlitMaterial.vert" and "shaders/UnlitMaterial.frag"
std::cout << "[AssetManager] Loading SHADER from: " << path << std::endl; std::cout << "[AssetManager] Loading SHADER from: " << path << std::endl;
// Create a new Shader object on the heap // Create a new Shader object on the heap
Shader* newShader = new Shader(); Shader *newShader = new Shader();
// Build actual paths from the base path // Build actual paths from the base path
std::string vertPath = path + ".vert"; std::string vertPath = path + ".vert";
@ -128,14 +147,295 @@ void* AssetManager::loadAssetFromDisk(AssetType type, const std::string& path)
} }
// Return as void* // Return as void*
return reinterpret_cast<void*>(newShader); return reinterpret_cast<void *>(newShader);
} }
case AssetType::SOUND: case AssetType::SOUND:
{ {
std::cout << "[AssetManager] Loading SOUND from: " << path << std::endl; std::cout << "[AssetManager] Loading SOUND from: " << path << std::endl;
// Stub or real code to load .wav / .ogg // Stub or real code to load .wav / .ogg
return (void*)0xAAAA8888; // placeholder return (void *)0xAAAA8888; // placeholder
}
case AssetType::MODEL:
{
// --------------------------------------------
// Load an OBJ model
// --------------------------------------------
std::cout << "[AssetManager] Loading MODEL from: " << path << std::endl;
std::ifstream objFile(path);
if (!objFile.is_open())
{
DEBUG_PRINT("[AssetManager] Failed to open OBJ file: %s\n", path.c_str());
return nullptr;
}
std::vector<float> temp_positions;
std::vector<float> temp_texCoords;
std::vector<float> temp_normals;
std::vector<unsigned int> vertexIndices, texCoordIndices, normalIndices;
std::string directory;
size_t lastSlash = path.find_last_of("/\\");
if (lastSlash != std::string::npos)
directory = path.substr(0, lastSlash + 1);
else
directory = "";
DEBUG_PRINT("[AssetManager] Asset Directory: %s", directory.c_str());
std::string line;
std::string mtlFileName;
while (std::getline(objFile, line))
{
std::istringstream iss(line);
std::string prefix;
iss >> prefix;
if (prefix == "v")
{
float x, y, z;
iss >> x >> y >> z;
temp_positions.push_back(x);
temp_positions.push_back(y);
temp_positions.push_back(z);
}
else if (prefix == "vt")
{
float u, v;
iss >> u >> v;
temp_texCoords.push_back(u);
temp_texCoords.push_back(v);
}
else if (prefix == "vn")
{
float nx, ny, nz;
iss >> nx >> ny >> nz;
temp_normals.push_back(nx);
temp_normals.push_back(ny);
temp_normals.push_back(nz);
}
else if (prefix == "f")
{
std::string vertexStr;
std::vector<std::tuple<unsigned int, unsigned int, unsigned int>> faceVertices;
while (iss >> vertexStr)
{
unsigned int vIdx = 0, tIdx = 0, nIdx = 0;
size_t firstSlash = vertexStr.find('/');
size_t secondSlash = vertexStr.find('/', firstSlash + 1);
if (firstSlash == std::string::npos)
{
// Format: f v1 v2 v3
vIdx = std::stoi(vertexStr);
}
else if (secondSlash == std::string::npos)
{
// Format: f v1/vt1 v2/vt2 v3/vt3
vIdx = std::stoi(vertexStr.substr(0, firstSlash));
tIdx = std::stoi(vertexStr.substr(firstSlash + 1));
}
else if (secondSlash > firstSlash + 1)
{
// Format: f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3
vIdx = std::stoi(vertexStr.substr(0, firstSlash));
tIdx = std::stoi(vertexStr.substr(firstSlash + 1, secondSlash - firstSlash - 1));
nIdx = std::stoi(vertexStr.substr(secondSlash + 1));
}
else
{
// Format: f v1//vn1 v2//vn2 v3//vn3
vIdx = std::stoi(vertexStr.substr(0, firstSlash));
nIdx = std::stoi(vertexStr.substr(secondSlash + 1));
}
faceVertices.emplace_back(vIdx, tIdx, nIdx);
}
// Triangulate if the face has more than 3 vertices (optional)
for (size_t i = 1; i + 1 < faceVertices.size(); ++i)
{
vertexIndices.push_back(std::get<0>(faceVertices[0]));
texCoordIndices.push_back(std::get<1>(faceVertices[0]));
normalIndices.push_back(std::get<2>(faceVertices[0]));
vertexIndices.push_back(std::get<0>(faceVertices[i]));
texCoordIndices.push_back(std::get<1>(faceVertices[i]));
normalIndices.push_back(std::get<2>(faceVertices[i]));
vertexIndices.push_back(std::get<0>(faceVertices[i + 1]));
texCoordIndices.push_back(std::get<1>(faceVertices[i + 1]));
normalIndices.push_back(std::get<2>(faceVertices[i + 1]));
}
}
else if (prefix == "mtllib")
{
iss >> mtlFileName;
}
}
objFile.close();
// Load MTL file if specified
std::string texturePath;
if (!mtlFileName.empty())
{
std::ifstream mtlFile(directory + mtlFileName);
if (mtlFile.is_open())
{
while (std::getline(mtlFile, line))
{
std::istringstream mtlIss(line);
std::string mtlPrefix;
mtlIss >> mtlPrefix;
if (mtlPrefix == "map_Kd")
{
mtlIss >> texturePath;
break; // Assuming only one texture map for simplicity
}
}
mtlFile.close();
}
else
{
DEBUG_PRINT("[AssetManager] Failed to open MTL file: %s", mtlFileName.c_str());
}
}
if (texturePath.empty())
{
DEBUG_PRINT("[AssetManager] No texture found for OBJ: %s", path.c_str());
}
else
{
DEBUG_PRINT("[AssetManager] Texture for OBJ: %s%s", directory.c_str(), texturePath.c_str());
}
// Create Model object
Model *model = new Model();
// Populate vertices
std::unordered_map<std::string, unsigned int> uniqueVertices;
for (size_t i = 0; i < vertexIndices.size(); ++i)
{
std::ostringstream keyStream;
keyStream << vertexIndices[i] << "/" << texCoordIndices[i] << "/" << normalIndices[i];
std::string key = keyStream.str();
if (uniqueVertices.find(key) == uniqueVertices.end())
{
Vertex vertex;
// OBJ indices are 1-based
vertex.position[0] = temp_positions[(vertexIndices[i] - 1) * 3];
vertex.position[1] = temp_positions[(vertexIndices[i] - 1) * 3 + 1];
vertex.position[2] = temp_positions[(vertexIndices[i] - 1) * 3 + 2];
if (!temp_texCoords.empty())
{
vertex.texCoord[0] = temp_texCoords[(texCoordIndices[i] - 1) * 2];
vertex.texCoord[1] = temp_texCoords[(texCoordIndices[i] - 1) * 2 + 1];
}
else
{
vertex.texCoord[0] = 0.0f;
vertex.texCoord[1] = 0.0f;
}
if (!temp_normals.empty())
{
vertex.normal[0] = temp_normals[(normalIndices[i] - 1) * 3];
vertex.normal[1] = temp_normals[(normalIndices[i] - 1) * 3 + 1];
vertex.normal[2] = temp_normals[(normalIndices[i] - 1) * 3 + 2];
}
else
{
vertex.normal[0] = 0.0f;
vertex.normal[1] = 0.0f;
vertex.normal[2] = 0.0f;
}
model->vertices.push_back(vertex);
unsigned int newIndex = static_cast<unsigned int>(model->vertices.size() - 1);
uniqueVertices[key] = newIndex;
model->indices.push_back(newIndex);
}
else
{
model->indices.push_back(uniqueVertices[key]);
}
}
// Generate OpenGL buffers
glGenVertexArrays(1, &model->vao);
glGenBuffers(1, &model->vbo);
glGenBuffers(1, &model->ebo);
glBindVertexArray(model->vao);
glBindBuffer(GL_ARRAY_BUFFER, model->vbo);
glBufferData(GL_ARRAY_BUFFER, model->vertices.size() * sizeof(Vertex), model->vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, model->indices.size() * sizeof(unsigned int), model->indices.data(), GL_STATIC_DRAW);
// Vertex positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
// Texture coordinates
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)(3 * sizeof(float)));
// Normals
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)(5 * sizeof(float)));
glBindVertexArray(0);
// Load texture if available
if (!texturePath.empty())
{
int width, height, channels;
unsigned char *data = stbi_load((directory + texturePath).c_str(), &width, &height, &channels, 0);
if (!data)
{
DEBUG_PRINT("[AssetManager] stb_image failed to load texture: %s", (directory + texturePath).c_str());
}
else
{
GLenum format = GL_RGBA;
if (channels == 1)
format = GL_RED;
else if (channels == 3)
format = GL_RGB;
// if channels == 4, already GL_RGBA
glGenTextures(1, &model->textureID);
glBindTexture(GL_TEXTURE_2D, model->textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
// Set texture params
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
}
}
else
{
model->textureID = 0; // No texture
}
// Return the Model pointer as void*
return reinterpret_cast<void *>(model);
} }
default: default:

View File

@ -2,6 +2,8 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <GL/glew.h>
#include <vector>
// Forward-declare your Shader class // Forward-declare your Shader class
class Shader; class Shader;
@ -12,7 +14,7 @@ enum class AssetType
TEXTURE, TEXTURE,
SHADER, SHADER,
SOUND, SOUND,
// Add more as you need MODEL,
}; };
// A simple struct to hold the generic pointer // A simple struct to hold the generic pointer
@ -21,6 +23,25 @@ struct GenericAsset
void* data = nullptr; void* data = nullptr;
}; };
struct Vertex {
float position[3];
float texCoord[2];
float normal[3];
};
struct Model {
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
GLuint vao;
GLuint vbo;
GLuint ebo;
GLuint textureID;
Model()
: vao(0), vbo(0), ebo(0), textureID(0) {}
};
// The main AssetManager // The main AssetManager
class AssetManager class AssetManager
{ {

View File

@ -5,9 +5,17 @@
#include "./Componenets/Mesh.h" #include "./Componenets/Mesh.h"
#include "./Componenets/GameObject.h" #include "./Componenets/GameObject.h"
#include "imgui.h"
#include "./Windows/LoggerWindow.h"
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
#include <fstream> #include <fstream>
#include <filesystem>
extern LoggerWindow *g_LoggerWindow;
@ -27,6 +35,14 @@ void SceneManager::SaveScene(const std::vector<std::shared_ptr<GameObject>> &gam
void SceneManager::LoadScene(std::vector<std::shared_ptr<GameObject>> &gameobjects, const std::string &filename) void SceneManager::LoadScene(std::vector<std::shared_ptr<GameObject>> &gameobjects, const std::string &filename)
{ {
if (!std::filesystem::exists(filename) || !std::filesystem::is_regular_file(filename)) {
g_LoggerWindow->AddLog("Error: File not found: %s", ImVec4(1.0f,0.0f,0.0f,1.0f), filename.c_str());
return;
}
YAML::Node sceneNode = YAML::LoadFile(filename); YAML::Node sceneNode = YAML::LoadFile(filename);
gameobjects.clear(); gameobjects.clear();

View File

@ -32,18 +32,23 @@ void InspectorWindow::Show()
// =========================== // ===========================
// 1) TRANSFORM // 1) TRANSFORM
// =========================== // ===========================
// Color the Transform header
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 0.4f, 1.0f));
bool transformOpen = ImGui::CollapsingHeader("Transform##Main", ImGuiTreeNodeFlags_DefaultOpen);
ImGui::PopStyleColor();
if (transformOpen && g_SelectedObject) //! Funny: I did not put a null check here and it broke everything. std::shared_ptr<TransformComponent> transform = g_SelectedObject->GetComponent<TransformComponent>();
std::shared_ptr<MeshComponent> mesh = g_SelectedObject->GetComponent<MeshComponent>();
// Color the Transform header
if (transform && g_SelectedObject) //! Funny: I did not put a null check here and it broke everything.
{ {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
bool transformOpen = ImGui::CollapsingHeader("Transform##Main", ImGuiTreeNodeFlags_DefaultOpen);
ImGui::PopStyleColor();
// Transform* transform = &g_SelectedObject->transform; // Transform* transform = &g_SelectedObject->transform;
std::shared_ptr<TransformComponent> transform = g_SelectedObject->GetComponent<TransformComponent>();
// printf("%p\n", &transform); // printf("%p\n", &transform);
if (transform) if (transformOpen)
{ {
if (ImGui::IsItemHovered()) if (ImGui::IsItemHovered())
@ -244,39 +249,36 @@ void InspectorWindow::Show()
ImGui::Spacing(); ImGui::Spacing();
ImGui::Separator(); ImGui::Separator();
} }
else
{
ImGui::Text("Error, Null Component");
}
} }
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 0.4f, 1.0f));
bool meshOpen = ImGui::CollapsingHeader("Mesh##Main", ImGuiTreeNodeFlags_DefaultOpen);
ImGui::PopStyleColor();
if (meshOpen && g_SelectedObject) //! Funny: I did not put a null check here and it broke everything. if (mesh && g_SelectedObject) //! Funny: I did not put a null check here and it broke everything.
{ {
// Transform* transform = &g_SelectedObject->transform; // Transform* transform = &g_SelectedObject->transform;
std::shared_ptr<MeshComponent> mesh = g_SelectedObject->GetComponent<MeshComponent>();
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
bool meshOpen = ImGui::CollapsingHeader("Mesh##Main", ImGuiTreeNodeFlags_DefaultOpen);
ImGui::PopStyleColor();
// printf("%p\n", &transform); // printf("%p\n", &transform);
if (mesh) if (meshOpen)
{ {
int vao = static_cast<int>(mesh->vao); int vao = static_cast<int>(mesh->vao);
if (ImGui::DragInt("vao", &vao, 1, 0, 1024)) if (ImGui::InputInt("vao", &vao, 1, 0))
{ {
mesh->vao = static_cast<GLuint>(vao); mesh->vao = static_cast<GLuint>(vao);
} }
int indexCount = static_cast<int>(mesh->indexCount); int indexCount = static_cast<int>(mesh->indexCount);
if (ImGui::DragInt("indexCount", &indexCount, 1, 0, 1024)) if (ImGui::InputInt("indexCount", &indexCount, 1, 0))
{ {
mesh->indexCount = static_cast<GLuint>(indexCount); mesh->indexCount = static_cast<GLuint>(indexCount);
} }
int textureID = static_cast<int>(mesh->textureID); int textureID = static_cast<int>(mesh->textureID);
if (ImGui::DragInt("textureID", &textureID, 1, 0, 1024)) if (ImGui::InputInt("textureID", &textureID, 1, 0))
{ {
mesh->textureID = static_cast<GLuint>(textureID); mesh->textureID = static_cast<GLuint>(textureID);
} }