Files
davinci2/source/console/ui/ui_main.cpp
T
2025-10-26 20:07:32 +08:00

145 lines
5.0 KiB
C++

#include "ui_main.h"
#include <SDL3/SDL.h>
#include <SDL3/SDL_opengl.h>
#include <imgui/imgui.h>
#include "imgui/imgui_impl_sdl3.h"
#include "imgui/imgui_impl_opengl3.h"
#include "scene/dvu_load_gltf.h"
#include "pipe/dvc_render_pipe.h"
#include "device/dvc_device.h"
#include "device/dvc_render_target.h"
#include "shader/dvc_shader_interface.h"
#include "buffer/dvu_dump_buffer.h"
#include "device/dvc_uniform_buffer.h"
#include "scene/dvc_scene_node.h"
#include "scene/component/dvc_light_component.h"
#include "../adapter/adapter_texture.h"
SDL_Window* main_window;
extern davinci::Device g_device;
extern davinci::Scene g_scene;
extern davinci::RenderTarget g_renderTarget;
extern davinci::AntiAliasingTech g_aaTech;
bool initMainUI(SDL_Window* window, SDL_GLContext gl_context, const char* glsl_version)
{
main_window = window;
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
//ImGui::StyleColorsDark();
ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
ImGui_ImplSDL3_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
//io.Fonts->AddFontDefault();
io.Fonts->AddFontFromFileTTF("./font/FiraCode-Regular.ttf", 18.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != nullptr);
return true;
}
void processUIEvent(const SDL_Event* event)
{
ImGui_ImplSDL3_ProcessEvent(event);
}
void OpenFileDialogFileCallback(void* userdata, const char* const* filelist, int filter)
{
if (filelist == nullptr || *filelist == nullptr) return;
g_device.clear();
g_scene.clear();
const char* szFileName = *filelist;
if (!davinci_utils::load_scene_from_gltf(szFileName, g_device, g_scene))
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Open glf scene file error", main_window);
}
}
void drawMainUI()
{
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Davinci"); // Create a window called "Hello, world!" and append into it.
if (ImGui::Button("OpenFile..."))
{
const SDL_DialogFileFilter filters[] = {
{ "gltf files(*.gltf;*.glb)", "gltf;glb" },
{ "All files(*.*)", "*" }
};
SDL_ShowOpenFileDialog(OpenFileDialogFileCallback, nullptr, main_window, filters, sizeof(filters)/sizeof(SDL_DialogFileFilter), nullptr, false);
}
if (ImGui::Button("Export"))
{
davinci_utils::export_mesh_to_obj(g_device, g_scene);
}
const char* aaItem[] = { "None", "MSAA_2X2", "MSAA_4X4"};
ImGui::Combo("AntiAliasing", (int*)(&g_aaTech), aaItem, IM_ARRAYSIZE(aaItem));
if (ImGui::Button("Render"))
{
davinci::RenderPipe pipe(g_device);
davinci::UniformBuffer& uniformBuffer = pipe.getUniformBuffer();
uniformBuffer.updateStandardAttribute(g_scene);
//find Light node
davinci::SceneNodeConstPtr lightNode = g_scene.getNodeWithComponent<davinci::LightComponent>();
if (lightNode)
{
uniformBuffer.setAttribute("light0:position", lightNode->getWorldPosition());
}
pipe.setAntiAliasingTech(g_aaTech);
g_scene.buildRenderableQueue(pipe);
g_renderTarget.clear();
pipe.process(g_renderTarget);
davinci_utils::dump_render_target("d:\\rendertarget", g_renderTarget);
}
ImGui::End();
// Rendering
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void cleanMainUI()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
}