Files
davinci2/source/console/ui/ui_main.cpp
T

196 lines
6.6 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"
#include "standard/dvu_standard_material.h"
#include "asset/dvc_material.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;
extern int32_t g_mousePosX;
extern int32_t g_mousePosY;
extern davinci::fVector4 g_mouseColor;
extern int32_t g_debugMaterialID;
typedef std::map<std::string, davinci::MaterialPtr> DebugMaterialMap;
DebugMaterialMap g_debugMaterialMap;
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));
const char* materialItems[] = {
"none",
"classic:phong",
"debug:solid_color",
"debug:base_color_texture",
"debug:normal_texture",
"debug:texture_coordinates_0",
"debug:geometry_normal"
};
ImGui::Combo("DebugMaterial", (int*)(&g_debugMaterialID), materialItems, IM_ARRAYSIZE(materialItems));
if (ImGui::Button("Render"))
{
davinci::RenderPipe pipe(g_device);
davinci::UniformBuffer& uniformBuffer = pipe.getUniformBuffer();
uniformBuffer.updateStandardAttribute(g_scene);
//find Light node
uniformBuffer.setAttribute("ambient_light:color", davinci::fVector3(0.1f, 0.1f, 0.1f));
davinci::SceneNodeConstPtr lightNode = g_scene.getNodeWithComponent<davinci::LightComponent>();
if (lightNode)
{
uniformBuffer.setAttribute("light0:position", lightNode->getWorldPosition());
uniformBuffer.setAttribute("light0:color", davinci::fVector3::WHITE);
}
pipe.setAntiAliasingTech(g_aaTech);
if (g_debugMaterialID == 0) {
pipe.setDebugMaterial(nullptr);
}
else
{
DebugMaterialMap::iterator it = g_debugMaterialMap.find(materialItems[g_debugMaterialID]);
if(it==g_debugMaterialMap.end())
{
davinci::MaterialPtr debugMaterial = davinci_utils::create_standard_material(&g_device, materialItems[g_debugMaterialID]);
if (debugMaterial)
{
debugMaterial->setupMaterial();
g_debugMaterialMap[materialItems[g_debugMaterialID]] = debugMaterial;
pipe.setDebugMaterial(debugMaterial);
}
}
else
{
pipe.setDebugMaterial(it->second);
}
}
g_scene.buildRenderableQueue(pipe);
g_renderTarget.clear();
pipe.process(g_renderTarget);
//davinci_utils::dump_render_target("d:\\rendertarget", g_renderTarget);
}
ImGui::Separator();
ImVec2 mousePos = ImGui::GetMousePos();
ImGui::Text("MousePosition: %d,%d", g_mousePosX, g_mousePosY);
ImGui::Text("MouseColor: %.3f,%.3f,%.3f,%.3f", g_mouseColor.x, g_mouseColor.y, g_mouseColor.z, g_mouseColor.w);
ImGui::End();
// Rendering
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void cleanMainUI()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
}