fixed 3d Rendering pipline, updated asset manager v2, and a\added inspector window

This commit is contained in:
OusmBlueNinja 2024-12-25 17:35:38 -06:00
parent ceb1f81f1b
commit 6be1be5a51
11 changed files with 490 additions and 191 deletions

View File

@ -14,5 +14,5 @@ void main()
// Multiply the texture by our uniform color.
// If you want a pure color (no texture), just do:
// FragColor = uColor;
FragColor = texColor * uColor;
FragColor = texColor * texColor;
}

View File

@ -20,6 +20,8 @@
#include "Windows/RenderWindow.h"
#include "Windows/PerformanceWindow.h"
#include "Windows/LoggerWindow.h"
#include "Windows/InspectorWindow.h"
@ -27,6 +29,8 @@
AssetManager g_AssetManager;
LoggerWindow *g_LoggerWindow;
bool MyEngine::Init(int width, int height, const std::string& title)
{
DEBUG_PRINT("[START] Engine Init");
@ -87,11 +91,16 @@ bool MyEngine::Init(int width, int height, const std::string& title)
m_RenderWindow = std::make_unique<RenderWindow>();
m_PerformanceWindow = std::make_unique<PerformanceWindow>();
m_LoggerWindow = std::make_unique<LoggerWindow>();
m_InspectorWindow = std::make_unique<InspectorWindow>();
// Some initial logs
m_LoggerWindow->AddLog("Engine initialized.");
m_LoggerWindow->AddLog("Welcome to Tesseract Engine!");
g_LoggerWindow = m_LoggerWindow.get();
m_Running = true;
m_LastTime = glfwGetTime();
DEBUG_PRINT("[OK] Engine Init ");
@ -126,6 +135,11 @@ void MyEngine::Run()
// Show main DockSpace
ShowDockSpace();
static Transform myTransform;
static Script myScript;
m_InspectorWindow->Show(myTransform, myScript);
// Show our windows
m_RenderWindow->Show(); // The spinning triangle as ImGui::Image
m_PerformanceWindow->Show(m_Fps, m_Ms); // FPS & ms

View File

@ -7,6 +7,8 @@
#include "Windows/PerformanceWindow.h"
#include "Windows/LoggerWindow.h"
#include "Engine/AssetManager.h"
#include "Windows/InspectorWindow.h"
//#define DEBUG
#include "gcml.h"
@ -40,6 +42,8 @@ private:
std::unique_ptr<RenderWindow> m_RenderWindow;
std::unique_ptr<PerformanceWindow> m_PerformanceWindow;
std::unique_ptr<LoggerWindow> m_LoggerWindow;
std::unique_ptr<InspectorWindow> m_InspectorWindow;
// For FPS calculation
float m_Fps = 0.0f;

View File

@ -1,24 +1,36 @@
#include "AssetManager.h"
#include "Engine/AssetManager.h"
#include <iostream>
#include <GL/glew.h> // or <GL/glew.h>, whichever you use
// Include your Shader class
#include "Rendering/Shader.h"
#include "Windows/LoggerWindow.h"
// Include OpenGL loader (GLEW) for texture creation
#include <GL/glew.h>
// For texture loading
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
int LoaddedAssets = 0;
extern LoggerWindow *g_LoggerWindow;
void* AssetManager::loadAsset(AssetType type, const std::string& path)
{
// 1) Generate a unique key for this type+path
// 1) Create a unique key for cache lookup
std::string key = generateKey(type, path);
// 2) Check if this asset is already loaded/cached
// 2) Check if its already loaded
auto it = m_AssetMap.find(key);
if (it != m_AssetMap.end())
{
// Already loaded, return existing data
// Return existing pointer
return it->second.data;
}
// 3) Not loaded yet, so load from disk
// 3) Not loaded yet, load from disk
void* assetData = loadAssetFromDisk(type, path);
if (!assetData)
{
@ -26,18 +38,21 @@ void* AssetManager::loadAsset(AssetType type, const std::string& path)
return nullptr;
}
// 4) Cache the result in m_AssetMap
// 4) Store in cache
GenericAsset newAsset;
newAsset.data = assetData;
m_AssetMap[key] = newAsset;
// 5) Return the new asset pointer
LoaddedAssets += 1;
g_LoggerWindow->AddLog("Loadded Asset: %s", path.c_str());
// 5) Return pointer
return assetData;
}
std::string AssetManager::generateKey(AssetType type, const std::string& path)
{
// Combine the numeric type and path into a single string key
return std::to_string(static_cast<int>(type)) + ":" + path;
}
@ -45,75 +60,88 @@ void* AssetManager::loadAssetFromDisk(AssetType type, const std::string& path)
{
switch (type)
{
case AssetType::TEXTURE:
case AssetType::TEXTURE:
{
// --------------------------------------------
// Load a texture with stb_image
// --------------------------------------------
std::cout << "[AssetManager] Loading TEXTURE from: " << path << std::endl;
int width, height, channels;
unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 0);
if (!data)
{
// ----------------------------------------------------------
// REAL CODE: load an image file (PNG, JPG, etc.) with stb_image
// and upload it as an OpenGL texture.
// ----------------------------------------------------------
std::cout << "[AssetManager] Loading TEXTURE from " << path << std::endl;
// 1) Load pixels using stb_image
int width, height, channels;
unsigned char* data = stbi_load(path.c_str(), &width, &height, &channels, 0);
if (!data)
{
std::cerr << "[AssetManager] stb_image failed to load: " << path << std::endl;
return nullptr;
}
// 2) Determine the pixel format
GLenum format = GL_RGBA; // default
if (channels == 1)
format = GL_RED;
else if (channels == 3)
format = GL_RGB;
// if channels == 4, already RGBA
// 3) Generate and bind a texture
GLuint textureID = 0;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
// 4) Upload the data to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format,
GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
// 5) Set texture parameters (filtering, wrapping, etc.)
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);
// 6) Unbind and free the stb_image data
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
// Return the texture ID, but cast it to `void*`
// so it fits our `GenericAsset.data` usage.
return reinterpret_cast<void*>(static_cast<uintptr_t>(textureID));
}
case AssetType::SHADER:
{
std::cout << "[AssetManager] Loading SHADER from " << path << std::endl;
// Stub: in real code, read/compile .vert/.frag, return program ID
return (void*)0x87654321;
}
case AssetType::SOUND:
{
std::cout << "[AssetManager] Loading SOUND from " << path << std::endl;
// Stub: load .wav or .ogg file, return pointer to sound data
return (void*)0xAAAA8888;
}
default:
{
std::cerr << "[AssetManager] Unknown asset type!\n";
std::cerr << "[AssetManager] stb_image failed for: " << path << std::endl;
return nullptr;
}
GLenum format = GL_RGBA;
if (channels == 1) format = GL_RED;
else if (channels == 3) format = GL_RGB;
// if channels == 4, already GL_RGBA
GLuint texID = 0;
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
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);
// Cleanup
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
// Return as void*
return reinterpret_cast<void*>(static_cast<uintptr_t>(texID));
}
case AssetType::SHADER:
{
// --------------------------------------------
// Load a shader using your existing "Shader" class
// --------------------------------------------
// Example usage: path = "shaders/UnlitMaterial" =>
// loads "shaders/UnlitMaterial.vert" and "shaders/UnlitMaterial.frag"
std::cout << "[AssetManager] Loading SHADER from: " << path << std::endl;
// Create a new Shader object on the heap
Shader* newShader = new Shader();
// Build actual paths from the base path
std::string vertPath = path + ".vert";
std::string fragPath = path + ".frag";
// Attempt to load
if (!newShader->Load(vertPath, fragPath))
{
std::cerr << "[AssetManager] Could not load shader: "
<< vertPath << " / " << fragPath << std::endl;
delete newShader; // Cleanup
return nullptr;
}
// Return as void*
return reinterpret_cast<void*>(newShader);
}
case AssetType::SOUND:
{
std::cout << "[AssetManager] Loading SOUND from: " << path << std::endl;
// Stub or real code to load .wav / .ogg
return (void*)0xAAAA8888; // placeholder
}
default:
{
std::cerr << "[AssetManager] Unknown asset type for: " << path << std::endl;
return nullptr;
}
}
}

View File

@ -3,52 +3,45 @@
#include <string>
#include <unordered_map>
// Forward-declare your Shader class
class Shader;
// --------------------------------------------------------------------------
// 1. Define an enum for various asset types
// --------------------------------------------------------------------------
// Define types of assets
enum class AssetType
{
TEXTURE,
SHADER,
SOUND,
// Add more as needed
// Add more as you need
};
// --------------------------------------------------------------------------
// 2. A placeholder "GenericAsset" struct
//
// In a real system, you might have a base class (e.g., class Asset)
// with derived classes, or store typed data in union/variant.
// --------------------------------------------------------------------------
// A simple struct to hold the generic pointer
struct GenericAsset
{
void* data = nullptr; // Points to the actual loaded asset data
// In a real engine, you'd store more metadata here.
void* data = nullptr;
};
// --------------------------------------------------------------------------
// 3. The AssetManager class
// --------------------------------------------------------------------------
// The main AssetManager
class AssetManager
{
public:
// Constructor / Destructor
AssetManager() = default;
~AssetManager() = default;
// loadAsset() returns a pointer to the loaded asset data.
// In a real engine, you might return a typed pointer or a handle.
// Load an asset from disk (texture, shader, etc.)
// Returns a void* pointer to the loaded resource.
// - For TEXTURE, cast to (GLuint)
// - For SHADER, cast to (Shader*)
// - For SOUND, cast to whatever you store
void* loadAsset(AssetType type, const std::string& path);
private:
// Map from "type+path" -> GenericAsset
// Cache of already loaded assets: key = "type + path"
std::unordered_map<std::string, GenericAsset> m_AssetMap;
// Generate a unique key for each asset based on type + path
// Generate the unique key
std::string generateKey(AssetType type, const std::string& path);
// Actually load asset data from disk, specialized by AssetType
// Actual loading from disk
void* loadAssetFromDisk(AssetType type, const std::string& path);
};

View File

@ -0,0 +1,222 @@
#include "InspectorWindow.h"
#include <cstdio> // for printf, if needed
#include <cstring> // for strcpy, if needed
void InspectorWindow::Show(Transform &transform, Script &script)
{
// We can push additional style for a more Unity/Godot-like inspector
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(12, 12));
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(6, 4));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(8, 8));
// Optional: a slight color for window background
// (If you want to override the themes default)
// ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.12f, 0.12f, 0.12f, 1.f));
if (ImGui::Begin("Inspector"))
{
// Title or header-like text
{
// A mild accent color for the header text
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.9f, 0.85f, 0.2f, 1.f));
ImGui::TextUnformatted("Selected Object Inspector");
ImGui::PopStyleColor();
}
ImGui::Separator();
ImGui::Spacing();
// ----------------
// TRANSFORM
// ----------------
{
// A bit of color or bold for the header
// Inside your InspectorWindow::Show(...) method,
// specifically within the Transform section:
if (ImGui::CollapsingHeader("Transform", ImGuiTreeNodeFlags_DefaultOpen))
{
// Provide a quick tooltip/hint on hover
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::TextUnformatted("Controls the object's Position, Rotation, and Scale.");
ImGui::EndTooltip();
}
// ------------------------------------------
// Position
// ------------------------------------------
if (ImGui::TreeNodeEx("Position", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_SpanAvailWidth))
{
// We'll do a custom layout with color-coded X, Y, Z labels
ImGui::TextColored(ImVec4(1.0f, 1.0f, 1.0f, 1.0f), "Position:");
ImGui::Spacing();
// Colors for each axis: X=red, Y=green, Z=blue
ImVec4 colX(1.0f, 0.4f, 0.4f, 1.0f);
ImVec4 colY(0.4f, 1.0f, 0.4f, 1.0f);
ImVec4 colZ(0.4f, 0.4f, 1.0f, 1.0f);
// Axis labels for reference
const char *axisLabels[3] = {"X", "Y", "Z"};
ImVec4 axisColors[3] = {colX, colY, colZ};
float *pos = transform.position;
// We'll lay them out on one line with spacing
ImGui::PushID("PositionFields");
for (int i = 0; i < 3; i++)
{
// Color-coded label
ImGui::TextColored(axisColors[i], "%s", axisLabels[i]);
ImGui::SameLine();
// We push another ID so each DragFloat is unique
ImGui::PushID(i);
ImGui::SetNextItemWidth(60.0f); // or -1 for full stretch
ImGui::DragFloat("##Pos", &pos[i], 0.1f);
ImGui::PopID();
// Small spacing between each axis
if (i < 2)
{
ImGui::SameLine(0, 15);
}
}
ImGui::PopID();
ImGui::TreePop();
}
ImGui::Spacing();
// ------------------------------------------
// Rotation
// ------------------------------------------
if (ImGui::TreeNodeEx("Rotation", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_SpanAvailWidth))
{
ImGui::TextColored(ImVec4(1.0f, 1.0f, 1.0f, 1.0f), "Rotation:");
ImGui::Spacing();
ImVec4 colX(1.0f, 0.4f, 0.4f, 1.0f);
ImVec4 colY(0.4f, 1.0f, 0.4f, 1.0f);
ImVec4 colZ(0.4f, 0.4f, 1.0f, 1.0f);
const char *axisLabels[3] = {"X", "Y", "Z"};
ImVec4 axisColors[3] = {colX, colY, colZ};
float *rot = transform.rotation;
ImGui::PushID("RotationFields");
for (int i = 0; i < 3; i++)
{
ImGui::TextColored(axisColors[i], "%s", axisLabels[i]);
ImGui::SameLine();
ImGui::PushID(i);
ImGui::SetNextItemWidth(60.0f);
ImGui::DragFloat("##Rot", &rot[i], 0.1f);
ImGui::PopID();
if (i < 2)
{
ImGui::SameLine(0, 15);
}
}
ImGui::PopID();
ImGui::TreePop();
}
ImGui::Spacing();
// ------------------------------------------
// Scale
// ------------------------------------------
if (ImGui::TreeNodeEx("Scale", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_SpanAvailWidth))
{
ImGui::TextColored(ImVec4(1.0f, 1.0f, 1.0f, 1.0f), "Scale:");
ImGui::Spacing();
ImVec4 colX(1.0f, 0.4f, 0.4f, 1.0f);
ImVec4 colY(0.4f, 1.0f, 0.4f, 1.0f);
ImVec4 colZ(0.4f, 0.4f, 1.0f, 1.0f);
const char *axisLabels[3] = {"X", "Y", "Z"};
ImVec4 axisColors[3] = {colX, colY, colZ};
float *scl = transform.scale;
ImGui::PushID("ScaleFields");
for (int i = 0; i < 3; i++)
{
ImGui::TextColored(axisColors[i], "%s", axisLabels[i]);
ImGui::SameLine();
ImGui::PushID(i);
ImGui::SetNextItemWidth(60.0f);
ImGui::DragFloat("##Scale", &scl[i], 0.1f);
ImGui::PopID();
if (i < 2)
{
ImGui::SameLine(0, 15);
}
}
ImGui::PopID();
ImGui::TreePop();
}
ImGui::Spacing();
ImGui::Separator();
}
ImGui::Spacing();
}
// ----------------
// SCRIPT
// ----------------
{
if (ImGui::CollapsingHeader("Script##Inspector", ImGuiTreeNodeFlags_DefaultOpen))
{
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::TextUnformatted("Attach a script (logic) to this object.");
ImGui::EndTooltip();
}
// Script Name
ImGui::TextColored(ImVec4(0.9f, 0.9f, 0.9f, 1.0f), "Script Name:");
ImGui::SameLine();
{
// We'll allocate a buffer to edit the script name
char buffer[128];
// Copy the current name into the buffer
std::snprintf(buffer, sizeof(buffer), "%s", script.scriptName.c_str());
// Provide an input text
ImGui::SetNextItemWidth(-1);
if (ImGui::InputText("##ScriptName", buffer, sizeof(buffer)))
{
script.scriptName = buffer;
}
}
// Enabled?
ImGui::Checkbox("Enabled##ScriptEnabled", &script.enabled);
// In a real engine, you might display all public fields of the script here
// e.g., float moveSpeed, int health, etc.
ImGui::Spacing();
ImGui::Separator();
}
}
}
ImGui::End();
// Pop the potential override color if used
// ImGui::PopStyleColor();
// Restore style
ImGui::PopStyleVar(3);
}

View File

@ -0,0 +1,37 @@
#pragma once
#include <string>
#include <glm/glm.hpp> // or <glm/vec3.hpp> if you prefer
#include "imgui.h"
// Example struct for a Transform component
struct Transform
{
float position[3] = {0.0f, 0.0f, 0.0f};
float rotation[3] = {0.0f, 0.0f, 0.0f};
float scale[3] = {1.0f, 1.0f, 1.0f};
};
// Example struct for a Script component
struct Script
{
std::string scriptName = "MyBehavior.lua";
bool enabled = true;
};
// The Inspector window class
class InspectorWindow
{
public:
// Constructor / Destructor
InspectorWindow() = default;
~InspectorWindow() = default;
// Show the window (call each frame)
// Pass references to your components, so any changes get applied to them.
void Show(Transform& transform, Script& script);
private:
// You can store additional state or styling here if needed
// e.g. bool m_SomeInternalFlag = false;
};

View File

@ -2,6 +2,8 @@
#include "imgui.h"
#include <algorithm> // for std::move, etc.
extern int LoaddedAssets;
// Initialize static members
int PerformanceWindow::m_OpenGLCallCount = 0;
int PerformanceWindow::m_TriangleCount = 0;
@ -77,6 +79,9 @@ void PerformanceWindow::Show(float fps, float ms)
0, nullptr, 0.0f, 5000.0f, ImVec2(0, 50));
ImGui::Separator();
ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "Assets: %d", LoaddedAssets);
ImGui::End();
ImGui::PopStyleVar();
}

View File

@ -1,56 +1,56 @@
// RenderWindow.cpp
#include "RenderWindow.h"
#include <GL/glew.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "imgui.h"
// Include your AssetManager header
// Include your AssetManager & Shader headers
#include "Engine/AssetManager.h"
#include "Rendering/Shader.h"
// Assume we have an extern or global asset manager declared somewhere
// e.g. in "Engine.cpp": AssetManager g_AssetManager;
// and in "AssetManager.h": extern AssetManager g_AssetManager;
// Extern reference to our global (or extern) asset manager
extern AssetManager g_AssetManager;
// --------------------------------------------------
// Cube vertex data and indices (unchanged)
// --------------------------------------------------
// Example cube data (position + UVs)
static float g_CubeVertices[] =
{
// FRONT (z=+1)
-1.f, -1.f, 1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 1.f, 0.f, 1.f,
-1.f, -1.f, 1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 1.f, 0.f, 1.f,
// BACK (z=-1)
-1.f, -1.f, -1.f, 1.f, 0.f,
1.f, -1.f, -1.f, 0.f, 0.f,
1.f, 1.f, -1.f, 0.f, 1.f,
-1.f, 1.f, -1.f, 1.f, 1.f,
-1.f, -1.f, -1.f, 1.f, 0.f,
1.f, -1.f, -1.f, 0.f, 0.f,
1.f, 1.f, -1.f, 0.f, 1.f,
-1.f, 1.f, -1.f, 1.f, 1.f,
// LEFT (x=-1)
-1.f, -1.f, -1.f, 0.f, 0.f,
-1.f, -1.f, 1.f, 1.f, 0.f,
-1.f, 1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, -1.f, 0.f, 1.f,
-1.f, -1.f, -1.f, 0.f, 0.f,
-1.f, -1.f, 1.f, 1.f, 0.f,
-1.f, 1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, -1.f, 0.f, 1.f,
// RIGHT (x=+1)
1.f, -1.f, -1.f, 1.f, 0.f,
1.f, -1.f, 1.f, 0.f, 0.f,
1.f, 1.f, 1.f, 0.f, 1.f,
1.f, 1.f, -1.f, 1.f, 1.f,
1.f, -1.f, -1.f, 1.f, 0.f,
1.f, -1.f, 1.f, 0.f, 0.f,
1.f, 1.f, 1.f, 0.f, 1.f,
1.f, 1.f, -1.f, 1.f, 1.f,
// TOP (y=+1)
-1.f, 1.f, -1.f, 0.f, 0.f,
1.f, 1.f, -1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 1.f, 0.f, 1.f,
-1.f, 1.f, -1.f, 0.f, 0.f,
1.f, 1.f, -1.f, 1.f, 0.f,
1.f, 1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 1.f, 0.f, 1.f,
// BOTTOM (y=-1)
-1.f, -1.f, -1.f, 1.f, 0.f,
1.f, -1.f, -1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 0.f, 1.f,
-1.f, -1.f, 1.f, 1.f, 1.f,
-1.f, -1.f, -1.f, 1.f, 0.f,
1.f, -1.f, -1.f, 0.f, 0.f,
1.f, -1.f, 1.f, 0.f, 1.f,
-1.f, -1.f, 1.f, 1.f, 1.f,
};
static unsigned int g_CubeIndices[] =
@ -69,9 +69,6 @@ static unsigned int g_CubeIndices[] =
20,21,22, 22,23,20
};
// --------------------------------------------------
// Show() - ImGui window to display the FBO
// --------------------------------------------------
void RenderWindow::Show()
{
ImGui::Begin("OpenGL Output");
@ -86,6 +83,7 @@ void RenderWindow::Show()
m_Initialized = true;
}
// If there's space, render to the FBO, then show it as an ImGui image
if (w > 0 && h > 0)
{
if (w != m_LastWidth || h != m_LastHeight)
@ -106,34 +104,39 @@ void RenderWindow::Show()
ImGui::End();
}
// --------------------------------------------------
// InitGLResources() - Setup VAO/VBO/EBO and load texture
// --------------------------------------------------
void RenderWindow::InitGLResources()
{
// 1) Load & compile our unlit texture shader (with UV support)
if (!m_Shader.Load("shaders/UnlitMaterial.vert", "shaders/UnlitMaterial.frag"))
// ----------------------------------------------------
// 1) Load SHADER from the asset manager
// ----------------------------------------------------
{
fprintf(stderr, "[RenderWindow] Failed to load unlit material shader.\n");
return;
void* shaderAsset = g_AssetManager.loadAsset(AssetType::SHADER, "assets/shaders/UnlitMaterial");
if (!shaderAsset)
{
fprintf(stderr, "[RenderWindow] Failed to load shader via AssetManager.\n");
return;
}
// Cast back to your Shader class
m_ShaderPtr = static_cast<Shader*>(shaderAsset);
}
// 2) Create a VAO for the cube
// ----------------------------------------------------
// 2) Create VAO/VBO/EBO for the cube
// ----------------------------------------------------
glGenVertexArrays(1, &m_VAO);
glBindVertexArray(m_VAO);
// 3) Create a VBO
glGenBuffers(1, &m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_CubeVertices), g_CubeVertices, GL_STATIC_DRAW);
// 4) Create an EBO
glGenBuffers(1, &m_EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(g_CubeIndices), g_CubeIndices, GL_STATIC_DRAW);
// 5) Setup vertex attribs
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
// Position = location 0, UV = location 1
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
@ -142,28 +145,26 @@ void RenderWindow::InitGLResources()
glBindVertexArray(0);
// 6) Load the texture via AssetManager
// This call returns a void*, which we cast to a GLuint
// ----------------------------------------------------
// 3) Load TEXTURE from the asset manager
// ----------------------------------------------------
{
void* assetPtr = g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/default.png");
if (!assetPtr)
void* texAsset = g_AssetManager.loadAsset(AssetType::TEXTURE, "assets/textures/default.png");
if (!texAsset)
{
fprintf(stderr, "[RenderWindow] Failed to load texture via AssetManager.\n");
fprintf(stderr, "[RenderWindow] Failed to load texture.\n");
}
else
{
// Convert void* to GLuint
m_TextureID = static_cast<GLuint>(reinterpret_cast<uintptr_t>(assetPtr));
// Cast from void* to GLuint
m_TextureID = static_cast<GLuint>(reinterpret_cast<uintptr_t>(texAsset));
}
}
}
// --------------------------------------------------
// RenderSceneToFBO() - Offscreen render of the spinning cube
// --------------------------------------------------
void RenderWindow::RenderSceneToFBO()
{
m_RotationAngle += 0.5f; // degrees per frame
m_RotationAngle += 0.5f; // spin per frame
m_FBO.Bind();
glViewport(0, 0, m_LastWidth, m_LastHeight);
@ -173,37 +174,41 @@ void RenderWindow::RenderSceneToFBO()
glClearColor(0.1f, 0.15f, 0.2f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use our unlit texture shader
m_Shader.Use();
// Use our loaded shader
if (m_ShaderPtr)
m_ShaderPtr->Use();
else
return; // No shader? Can't render
// Build MVP
glm::mat4 model = glm::rotate(glm::mat4(1.0f),
glm::radians(m_RotationAngle),
glm::vec3(1.f, 1.f, 0.f));
glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0.f, 0.f, -5.f));
float aspect = (m_LastHeight != 0) ? (float)m_LastWidth / (float)m_LastHeight : 1.0f;
glm::mat4 proj = glm::perspective(glm::radians(45.f), aspect, 0.1f, 100.f);
glm::mat4 mvp = proj * view * model;
glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0.f, 0.f, -5.f));
float aspect = (m_LastHeight != 0) ? (float)m_LastWidth / (float)m_LastHeight : 1.0f;
glm::mat4 proj = glm::perspective(glm::radians(45.f), aspect, 0.1f, 100.f);
glm::mat4 mvp = proj * view * model;
// Pass MVP
GLint mvpLoc = glGetUniformLocation(m_Shader.GetProgramID(), "uMVP");
// Pass MVP to the shader
GLuint programID = m_ShaderPtr->GetProgramID();
GLint mvpLoc = glGetUniformLocation(programID, "uMVP");
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(mvp));
// Texture uniform -> unit 0
GLint texLoc = glGetUniformLocation(m_Shader.GetProgramID(), "uTexture");
glUniform1i(texLoc, 0);
// Bind the texture from asset manager
// Bind the texture to unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_TextureID);
// Draw
// If your shader has a uniform sampler2D named "uTexture"
GLint texLoc = glGetUniformLocation(programID, "uTexture");
glUniform1i(texLoc, 0);
// Draw the cube
glBindVertexArray(m_VAO);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
// Cleanup
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
FBO::Unbind();
m_FBO.Unbind();
}

View File

@ -1,33 +1,23 @@
#pragma once
#include "../Rendering/FBO.h"
#include "../Rendering/Shader.h"
#include <glm/glm.hpp>
#include "../Engine/AssetManager.h"
// Forward-declare a texture loading function if you have it in a separate file.
// extern unsigned int LoadTexture(const char* filePath);
#include "Rendering/Shader.h" //
class RenderWindow
{
public:
void Show(); // Called each frame to draw the ImGui window with the rendered texture
void Show();
private:
// Initialize VAO/VBO/EBO, load shaders/textures, etc.
void InitGLResources();
// Actually render the spinning cube into the FBO each frame
void RenderSceneToFBO();
// Offscreen render target
FBO m_FBO;
// Our unlit shader
Shader m_Shader;
// Keep track if we've initialized (so we only do so once)
// Keep track if we've initialized
bool m_Initialized = false;
// GL objects for the cube
@ -36,12 +26,13 @@ private:
unsigned int m_EBO = 0;
// Spin
float m_RotationAngle = 0.0f;
float m_RotationAngle = 0.f;
int m_LastWidth = 0;
int m_LastHeight = 0;
// Track last known size (to recreate FBO if user resizes ImGui window)
int m_LastWidth = 0;
int m_LastHeight = 0;
// The loaded texture
unsigned int m_TextureID = 0;
// The loaded texture handle
unsigned int m_TextureID = 0;
// The loaded shader program (via AssetManager)
Shader* m_ShaderPtr = nullptr;
};