Add ImGui
This commit is contained in:
@@ -70,6 +70,31 @@ void DebugRender::DrawLine(const glm::vec3& from, const glm::vec3& to, const glm
|
||||
m_lines.push_back(line);
|
||||
}
|
||||
|
||||
void DebugRender::DrawBoundingBox(const BoundingBox& box, const glm::vec3& color)
|
||||
{
|
||||
glm::vec3 p0 = glm::vec3(box.m_min.x, box.m_min.y, box.m_min.z);
|
||||
glm::vec3 p1 = glm::vec3(box.m_max.x, box.m_max.y, box.m_max.z);
|
||||
glm::vec3 p2 = glm::vec3(box.m_min.x, box.m_min.y, box.m_max.z);
|
||||
glm::vec3 p3 = glm::vec3(box.m_min.x, box.m_max.y, box.m_min.z);
|
||||
glm::vec3 p4 = glm::vec3(box.m_min.x, box.m_max.y, box.m_max.z);
|
||||
glm::vec3 p5 = glm::vec3(box.m_max.x, box.m_min.y, box.m_min.z);
|
||||
glm::vec3 p6 = glm::vec3(box.m_max.x, box.m_min.y, box.m_max.z);
|
||||
glm::vec3 p7 = glm::vec3(box.m_max.x, box.m_max.y, box.m_min.z);
|
||||
|
||||
DrawLine(p0, p2, color);
|
||||
DrawLine(p0, p3, color);
|
||||
DrawLine(p0, p5, color);
|
||||
DrawLine(p1, p4, color);
|
||||
DrawLine(p1, p6, color);
|
||||
DrawLine(p1, p7, color);
|
||||
DrawLine(p2, p4, color);
|
||||
DrawLine(p2, p6, color);
|
||||
DrawLine(p3, p4, color);
|
||||
DrawLine(p3, p7, color);
|
||||
DrawLine(p5, p6, color);
|
||||
DrawLine(p5, p7, color);
|
||||
}
|
||||
|
||||
void DebugRender::RenderFrame()
|
||||
{
|
||||
if (!g_drawDebug)
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#define DEBUGRENDER_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "boundingbox.h"
|
||||
#include "render_shared.h"
|
||||
|
||||
const int kMaxDebugVBSize = 1024 * 1024 * 2;
|
||||
@@ -20,6 +22,7 @@ public:
|
||||
|
||||
void DrawAxis(const glm::vec3& vec);
|
||||
void DrawLine(const glm::vec3& from, const glm::vec3& to, const glm::vec3& color);
|
||||
void DrawBoundingBox(const BoundingBox& box, const glm::vec3& color);
|
||||
|
||||
void RenderFrame();
|
||||
|
||||
|
||||
65
src/render/imguimanager.cpp
Normal file
65
src/render/imguimanager.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "engine.h"
|
||||
#include "render.h"
|
||||
#include "imguimanager.h"
|
||||
#include <imgui_impl_sdl3.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
|
||||
ImGuiManager g_ImGuiManager;
|
||||
|
||||
void ImGuiManager::Init()
|
||||
{
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
(void)io;
|
||||
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
// Initialize backend
|
||||
|
||||
ImGui_ImplSDL3_InitForOpenGL(GetEngine()->GetWindow(), g_pRender->GetGLContext());
|
||||
|
||||
ImGui_ImplOpenGL3_Init();
|
||||
|
||||
m_ready = true;
|
||||
}
|
||||
|
||||
void ImGuiManager::Shutdown()
|
||||
{
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
|
||||
ImGui_ImplSDL3_Shutdown();
|
||||
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
|
||||
void ImGuiManager::BeginFrame()
|
||||
{
|
||||
ImGui_ImplSDL3_NewFrame();
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
|
||||
ImGui::NewFrame();
|
||||
}
|
||||
|
||||
void ImGuiManager::EndFrame()
|
||||
{
|
||||
Draw();
|
||||
}
|
||||
|
||||
void ImGuiManager::PollEvents(SDL_Event* _p_event)
|
||||
{
|
||||
SDL_assert(_p_event);
|
||||
|
||||
if (!m_ready)
|
||||
return;
|
||||
|
||||
ImGui_ImplSDL3_ProcessEvent(_p_event);
|
||||
}
|
||||
|
||||
void ImGuiManager::Draw()
|
||||
{
|
||||
ImGui::Render();
|
||||
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
}
|
||||
28
src/render/imguimanager.h
Normal file
28
src/render/imguimanager.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef IMGUI_MGR_H
|
||||
#define IMGUI_MGR_H
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include "core.h"
|
||||
|
||||
class ImGuiManager
|
||||
{
|
||||
public:
|
||||
void Init();
|
||||
void Shutdown();
|
||||
|
||||
void BeginFrame();
|
||||
void EndFrame();
|
||||
|
||||
void PollEvents(SDL_Event* _p_event);
|
||||
|
||||
private:
|
||||
void Draw();
|
||||
|
||||
private:
|
||||
bool m_ready = false;
|
||||
};
|
||||
|
||||
extern ImGuiManager g_ImGuiManager;
|
||||
|
||||
#endif // !IMGUI_MGR_H
|
||||
@@ -225,6 +225,9 @@ void Model::LoadObj(const char* filename)
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
m_boundingBox.m_min = glm::vec3(FLT_MAX);
|
||||
m_boundingBox.m_max = glm::vec3(FLT_MIN);
|
||||
|
||||
// Combine in to the one array
|
||||
std::vector<StaticMeshVertex> vertices;
|
||||
@@ -246,8 +249,8 @@ void Model::LoadObj(const char* filename)
|
||||
vtx.texcoord = uv;
|
||||
vertices.push_back(vtx);
|
||||
|
||||
//m_boundingBox.min = glm::min(m_boundingBox.min, vertex);
|
||||
//m_boundingBox.max = glm::max(m_boundingBox.max, vertex);
|
||||
m_boundingBox.m_min = glm::min(m_boundingBox.m_min, vertex);
|
||||
m_boundingBox.m_max = glm::max(m_boundingBox.m_max, vertex);
|
||||
}
|
||||
|
||||
m_Vertices = vertices;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "boundingbox.h"
|
||||
#include "render_shared.h"
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
@@ -35,12 +36,12 @@ public:
|
||||
|
||||
void Draw(const glm::mat4& model, bool isTransparent = false);
|
||||
|
||||
//BoundingBox GetBoundingBox() { return m_boundingBox; }
|
||||
const BoundingBox& GetBoundingBox() { return m_boundingBox; }
|
||||
|
||||
private:
|
||||
std::vector<StaticMeshVertex> m_Vertices;
|
||||
ModelData_t m_data;
|
||||
//BoundingBox m_boundingBox;
|
||||
BoundingBox m_boundingBox;
|
||||
Texture2D* m_AlbedoTexture;
|
||||
};
|
||||
|
||||
|
||||
@@ -7,11 +7,15 @@
|
||||
#include "modelsystem.h"
|
||||
#include "debugrender.h"
|
||||
#include "ifilesystem.h"
|
||||
#include "camera.h"
|
||||
#include "imguimanager.h"
|
||||
|
||||
#include <pugixml.hpp>
|
||||
|
||||
static GLuint g_VAO = 0;
|
||||
|
||||
static int g_NumModels = 0;
|
||||
|
||||
// TEMP
|
||||
glm::vec3 g_viewOrigin;
|
||||
glm::vec3 g_viewOrient;
|
||||
@@ -139,6 +143,9 @@ void Render::Init(SDL_Window* pWindow)
|
||||
g_pDebugRender = new DebugRender();
|
||||
g_pDebugRender->Initialize();
|
||||
|
||||
// Create imgui manager
|
||||
g_ImGuiManager.Init();
|
||||
|
||||
// Create stretched picture filenameBuffer
|
||||
m_pStretchedPicVBuf = g_pRenderDevice->CreateVertexBuffer(nullptr, MAX_STRETCH_VX, true);
|
||||
|
||||
@@ -162,6 +169,8 @@ void Render::Shutdown()
|
||||
delete m_pStretchedPicVBuf;
|
||||
m_pStretchedPicVBuf = nullptr;
|
||||
|
||||
g_ImGuiManager.Shutdown();
|
||||
|
||||
g_pDebugRender->Shutdown();
|
||||
delete g_pDebugRender;
|
||||
g_pDebugRender = nullptr;
|
||||
@@ -187,19 +196,46 @@ void Render::Shutdown()
|
||||
m_pGLContext = nullptr;
|
||||
}
|
||||
|
||||
void Render::RenderScene()
|
||||
{
|
||||
void Render::RenderScene() {
|
||||
if (m_sceneModel) {
|
||||
Camera* camera = g_cameraManager.GetActiveCamera();
|
||||
if (camera) {
|
||||
glm::mat4 viewProj = m_ProjectionMatrix * m_ViewMatrix;
|
||||
viewProj = glm::transpose(viewProj);
|
||||
|
||||
camera->GetFrustum().Build(viewProj);
|
||||
|
||||
if (camera->GetFrustum().CullBoundingBox(m_sceneModel->GetBoundingBox()))
|
||||
return;
|
||||
}
|
||||
|
||||
static glm::mat4 s_identity = glm::mat4(1.0f);
|
||||
m_sceneModel->Draw(s_identity);
|
||||
|
||||
g_NumModels++;
|
||||
|
||||
g_pDebugRender->DrawBoundingBox(m_sceneModel->GetBoundingBox(), glm::vec3(1.0f));
|
||||
}
|
||||
|
||||
g_pDebugRender->RenderFrame();
|
||||
}
|
||||
|
||||
void Render::RenderStats()
|
||||
{
|
||||
char buffer[256];
|
||||
|
||||
snprintf(buffer, sizeof(buffer), "Scene: %s", m_sceneName.c_str());
|
||||
ImGui::GetForegroundDrawList()->AddText(ImVec2(0.0f, 0.0f), 0xffffffff, buffer);
|
||||
snprintf(buffer, sizeof(buffer), "numModels: %d", g_NumModels);
|
||||
ImGui::GetForegroundDrawList()->AddText(ImVec2(0.0f, 15.0f), 0xffffffff, buffer);
|
||||
}
|
||||
|
||||
void Render::Present(bool vsync)
|
||||
{
|
||||
SDL_GL_SwapWindow(m_pWindow);
|
||||
|
||||
// reset stats
|
||||
g_NumModels = 0;
|
||||
}
|
||||
|
||||
void Render::ResetStates()
|
||||
@@ -242,13 +278,15 @@ void Render::LoadSceneXML(const char* filename)
|
||||
pugi::xml_parse_result result = doc.load_buffer(filedata, length);
|
||||
delete[] filedata;
|
||||
if (!result) {
|
||||
Core::Error("Render::LoadSceneXML: Error while reading level description file '%s'\nError: %s:%i",
|
||||
Core::Error("SceneManager::LoadScene: Error while reading level description file '%s'\nError: %s:%i",
|
||||
filenameBuffer, result.description(), result.offset);
|
||||
}
|
||||
|
||||
pugi::xml_node root = doc.document_element();
|
||||
const char* scenefilename = root.child("SceneFile").attribute("filename").value();
|
||||
|
||||
m_sceneName = scenefilename;
|
||||
|
||||
sprintf(filenameBuffer, "data/levels/%s/%s", filename, scenefilename);
|
||||
|
||||
if (!GetFileSystem()->IsExist(filenameBuffer)) {
|
||||
|
||||
@@ -19,6 +19,7 @@ public:
|
||||
void Shutdown();
|
||||
|
||||
void RenderScene();
|
||||
void RenderStats();
|
||||
|
||||
void Present(bool vsync = false);
|
||||
|
||||
@@ -32,6 +33,8 @@ public:
|
||||
|
||||
void LoadSceneXML(const char* filename);
|
||||
|
||||
SDL_GLContext GetGLContext() { return m_pGLContext; }
|
||||
|
||||
private:
|
||||
glm::mat4 m_ViewMatrix;
|
||||
glm::mat4 m_ProjectionMatrix;
|
||||
|
||||
Reference in New Issue
Block a user