Added gizmos

This commit is contained in:
KP 2024-07-30 22:28:17 -05:00
parent 5ebd128f5b
commit 0c52643b11
5 changed files with 2315 additions and 50 deletions

File diff suppressed because it is too large Load diff

View file

@ -226,7 +226,7 @@ void Editor::UpdateModeNormal()
using namespace kp3d; using namespace kp3d;
// Raycast through everything on the map // Raycast through everything on the map
if (sandbox->IsMouseButtonDown(kp3d::MOUSE_BUTTON_LEFT)) if (sandbox->IsMouseButtonDown(kp3d::MOUSE_BUTTON_LEFT) && !editing_gizmo)
{ {
struct Target struct Target
{ {
@ -304,6 +304,9 @@ void Editor::UpdateModeNormal()
const auto& info = std::any_cast<kp3d::BatchSectorInfo>(kp3d::editor_hovered_batch[0]->userdata); const auto& info = std::any_cast<kp3d::BatchSectorInfo>(kp3d::editor_hovered_batch[0]->userdata);
if (info.wall) if (info.wall)
{ {
// KP3D_LOG_INFO("poop j");
can_wall_update = true;
wall_update.walls_to_update.clear();
// Find where this wall is in the sector graph. // Find where this wall is in the sector graph.
// We need the following info: // We need the following info:
// - The index of this wall and the one it's connected to (i.e. the wall whose end point is the start of this one) // - The index of this wall and the one it's connected to (i.e. the wall whose end point is the start of this one)
@ -312,18 +315,37 @@ void Editor::UpdateModeNormal()
{ {
for (int i = 0; i < sp->walls.size(); i++) for (int i = 0; i < sp->walls.size(); i++)
{ {
const Wall& wall = sp->walls[i]; Wall& wall = sp->walls[i];
const Wall& neighbor = sp->walls[(i - 1) % sp->walls.size()]; Wall& neighbor = sp->walls[(i - 1) % sp->walls.size()];
if (&wall == info.wall) if (&wall == info.wall)
{ {
wall_update.gizmo_pos = {
info.wall->start.x,
(info.sector->floor.base_height + info.sector->ceiling.base_height) * 0.5f,
-info.wall->start.y};
wall_update.main_wall = info.wall;
wall_update.walls_to_update.emplace(&neighbor, WP_END);
} }
} }
} }
for (const auto& sp: sandbox->map.sectors)
{
if (sp.get() == info.sector)
continue;
for (int i = 0; i < sp->walls.size(); i++)
{
}
}
}
else
{
can_wall_update = false;
} }
} }
catch (std::bad_any_cast& e) catch (std::bad_any_cast& e)
{ {
can_wall_update = false;
KP3D_LOG_ERROR("Bad any cast: {}", e.what()); KP3D_LOG_ERROR("Bad any cast: {}", e.what());
} }
} }
@ -331,6 +353,68 @@ void Editor::UpdateModeNormal()
void Editor::RenderModeNormal() void Editor::RenderModeNormal()
{ {
if (!can_wall_update)
return;
ImGuizmo::Enable(true);
ImGuizmo::SetRect(0, 0, sandbox->GetWidth(), sandbox->GetHeight());
ImGuizmo::SetDrawlist(ImGui::GetBackgroundDrawList());
auto p = &wall_update.gizmo_pos;
kp3d::Transform dummy;
dummy.translation = *p;
enum { M, V, P };
std::array<kp3d::Mat4, 3> mvp = dummy.GetMvp(sandbox->camera, sandbox->projection);
float tmp[16];
for (int i = 0; i < 16; i++)
tmp[i] = mvp[M].mat[i];
bool v = ImGuizmo::Manipulate(
mvp[V].mat.data(),
mvp[P].mat.data(),
static_cast<ImGuizmo::OPERATION>(ImGuizmo::TRANSLATE_X | ImGuizmo::TRANSLATE_Z),
ImGuizmo::WORLD,
tmp,
nullptr,
nullptr,
nullptr,
nullptr
);
editing_gizmo = v;
float translation[3];
float rotation[3];
float scale[3];
ImGuizmo::DecomposeMatrixToComponents(tmp, translation, rotation, scale);
p->x = translation[0];
p->y = translation[1];
p->z = translation[2];
wall_update.main_wall->start.x = translation[0];
wall_update.main_wall->start.y = -translation[2];
for (const auto& [wall, pos]: wall_update.walls_to_update)
{
if (pos == WP_START)
{
wall->start.x = translation[0];
wall->start.y = -translation[2];
}
else if (pos == WP_END)
{
wall->end.x = translation[0];
wall->end.y = -translation[2];
}
}
if (v)
{
sandbox->map.Rebuild(kp3d::GEN_NORMALS);
}
} }
void Editor::RenderUI() void Editor::RenderUI()
@ -459,7 +543,6 @@ void Editor::RenderUIInfo()
{ {
case MODE_NORMAL: mode_str = "Normal"; break; case MODE_NORMAL: mode_str = "Normal"; break;
case MODE_BUILD: mode_str = "Build"; break; case MODE_BUILD: mode_str = "Build"; break;
case MODE_SECTOR_EDIT: mode_str = "Sector Edit"; break;
} }
ImGui::Text("Mode: %s\n", mode_str.c_str()); ImGui::Text("Mode: %s\n", mode_str.c_str());
ImGui::Separator(); ImGui::Separator();

View file

@ -1,9 +1,12 @@
#pragma once #pragma once
#include <map>
#include "KP3D_Texture.h" #include "KP3D_Texture.h"
#include "KP3D_Math.h" #include "KP3D_Math.h"
#include "KP3D_IOEvents.h" #include "KP3D_IOEvents.h"
#include "KP3D_Material.h" #include "KP3D_Material.h"
#include "KP3D_Map.h"
enum EditMode enum EditMode
{ {
@ -20,6 +23,19 @@ enum MaterialType
MAT_MIDDLE_TEX MAT_MIDDLE_TEX
}; };
enum WallPoint
{
WP_START,
WP_END
};
struct WallUpdate
{
kp3d::Wall* main_wall;
std::map<kp3d::Wall*, WallPoint> walls_to_update;
kp3d::Vec3 gizmo_pos;
};
class Editor class Editor
{ {
public: public:
@ -61,6 +77,10 @@ private:
bool should_show_material_modal = false; bool should_show_material_modal = false;
char m_tex_filter_buf[512]; char m_tex_filter_buf[512];
bool anything_hovered = false; bool anything_hovered = false;
bool editing_gizmo = false;
std::vector<const kp3d::Material**> m_materials_to_update; std::vector<const kp3d::Material**> m_materials_to_update;
WallUpdate wall_update;
bool can_wall_update = false;
}; };

View file

@ -20,7 +20,7 @@ Sandbox::Sandbox(const std::string& path):
tex.Load("logo.png"); tex.Load("logo.png");
crosshair.Load(".kp3d/crosshair.png"); crosshair.Load(".kp3d/crosshair.png");
m_projection.InitPerspective(70.0f, (float) GetWidth() / (float) GetHeight()); projection.InitPerspective(70.0f, (float) GetWidth() / (float) GetHeight());
map.Init(); map.Init();
using namespace ksi; using namespace ksi;
@ -127,7 +127,7 @@ void Sandbox::Render()
if (m_mode == MODE_GAME || m_mode == MODE_EDIT) if (m_mode == MODE_GAME || m_mode == MODE_EDIT)
{ {
// 3D scene // 3D scene
kp3d::Renderer3D::Begin(camera, m_projection); kp3d::Renderer3D::Begin(camera, projection);
map.Render(); map.Render();
if (m_mode == MODE_EDIT) if (m_mode == MODE_EDIT)
editor.RenderMap(); editor.RenderMap();

View file

@ -33,10 +33,10 @@ public:
Editor editor; Editor editor;
kp3d::Camera camera; kp3d::Camera camera;
kp3d::Texture crosshair; kp3d::Texture crosshair;
kp3d::Mat4 projection;
private: private:
// KP3D essentials // KP3D essentials
kp3d::Mat4 m_projection;
int m_mode; int m_mode;
}; };