Skip to content

Commit

Permalink
Add trigger drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Mar 15, 2024
1 parent 317fa3f commit 56e0bb4
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ This will visualize all portals which are between levels.

This will draw all signals (triggers) in the current level.

### Draw triggers

This will draw all trigger volumes and planes in the current level. This feature only exists in Underworld.

## Debug

The debug menu has some restored event debug features.
Expand Down
15 changes: 15 additions & 0 deletions src/cdc/math/Math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,20 @@ cdc::Vector3 cdc::Mul3x3(Matrix* matA, Vector3* vecB)
_mm_mul_ps(matA->col1.vec128, _mm_shuffle_ps(vecB->vec128, vecB->vec128, _MM_SHUFFLE(1, 1, 1, 1))),
_mm_mul_ps(matA->col2.vec128, _mm_shuffle_ps(vecB->vec128, vecB->vec128, _MM_SHUFFLE(2, 2, 2, 2)))));

return result;
}

cdc::Vector3 cdc::Mul3x4(Matrix* matA, Vector3* vecB)
{
cdc::Vector3 result;

result.vec128 = _mm_add_ps(
_mm_mul_ps(matA->col0.vec128, _mm_shuffle_ps(vecB->vec128, vecB->vec128, _MM_SHUFFLE(0, 0, 0, 0))),
_mm_add_ps(
_mm_mul_ps(matA->col1.vec128, _mm_shuffle_ps(vecB->vec128, vecB->vec128, _MM_SHUFFLE(1, 1, 1, 1))),
_mm_add_ps(
_mm_mul_ps(matA->col2.vec128, _mm_shuffle_ps(vecB->vec128, vecB->vec128, _MM_SHUFFLE(2, 2, 2, 2))),
matA->col3.vec128)));

return result;
}
1 change: 1 addition & 0 deletions src/cdc/math/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
namespace cdc
{
Vector3 Mul3x3(Matrix* matA, Vector3* vecB);
Vector3 Mul3x4(Matrix* matA, Vector3* vecB);
}
8 changes: 8 additions & 0 deletions src/cdc/math/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ void cdc::Matrix::Build(Euler* euler)
// Too lazy to implement myself
auto addr = GET_ADDRESS(0x000000, 0x000000, 0x49DD70);

Hooking::ThisCall(addr, this, euler);
}

void cdc::Matrix::Build_XYZOrder(cdc::Euler* euler)
{
// Too lazy to implement myself
auto addr = GET_ADDRESS(0x000000, 0x000000, 0x49D500);

Hooking::ThisCall(addr, this, euler);
}
1 change: 1 addition & 0 deletions src/cdc/math/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ namespace cdc
Vector col3;

void Build(Euler* euler);
void Build_XYZOrder(cdc::Euler* euler);
};
}
37 changes: 37 additions & 0 deletions src/level/Trigger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "instance/Instance.h"

enum TriggerShape
{
TriggerShape_Box = 1,
TriggerShape_Sphere = 2
};

struct IntroDataTrigger : IntroData
{
char pad1[44];

TriggerShape shape;
float LocalXExtent;
float LocalYExtent;
float LocalZExtent;
float Radius;
};

struct NsInstance
{
char pad1[32];

Instance* m_instance;
};

struct NsTriggerPlaneBase : NsInstance
{
unsigned int m_flags;
};

struct NsTriggerVolumeBase : NsInstance
{
unsigned int m_flags;
};
109 changes: 109 additions & 0 deletions src/modules/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "instance/Instances.h"
#include "instance/Enemy.h"
#include "level/Markup.h"
#include "level/Trigger.h"

#include "cdc/math/Math.h"

template <typename T>
static inline cdc::Vector3 GetVertice(unsigned int vertice, Mesh* mesh, cdc::Vector* offset)
Expand Down Expand Up @@ -56,6 +59,7 @@ void Draw::OnMenu()
ImGui::MenuItem("Draw collision", nullptr, &m_drawCollision);
ImGui::MenuItem("Draw portals", nullptr, &m_drawPortals);
ImGui::MenuItem("Draw signals", nullptr, &m_drawSignals);
ImGui::MenuItem("Draw triggers", nullptr, &m_drawTriggers);

ImGui::EndMenu();
}
Expand All @@ -76,6 +80,11 @@ void Draw::OnFrame()
DrawMarkUp();
}

if (m_drawTriggers)
{
DrawTriggers();
}

if (level)
{
if (m_drawCollision)
Expand Down Expand Up @@ -430,6 +439,106 @@ void Draw::DrawSignals(Level* level)
#endif
}

void Draw::DrawTriggers()
{
#ifdef TR8
// Draw all trigger planes
auto numPlanes = *(int*)0xDBA21C;
auto planes = (NsTriggerPlaneBase**)0xDA61C8;

for (int i = 0; i < numPlanes; i++)
{
auto plane = planes[i];
auto instance = plane->m_instance;
auto data = (IntroDataTrigger*)instance->intro->data;

if (data)
{
auto extendX = cdc::Vector3{};
extendX.x += data->LocalXExtent;
extendX.z += data->LocalZExtent;

auto extendY = cdc::Vector3{};
extendY.x -= data->LocalXExtent;
extendY.z -= data->LocalZExtent;

cdc::Matrix mat;
mat.Build_XYZOrder(&instance->rotation);
mat.col3 = instance->position;

auto x = cdc::Mul3x4(&mat, &extendX);
auto y = cdc::Mul3x4(&mat, &extendY);

DrawPlane(&x, &y, RGBA(255, 0, 0, 10));
}
}

// Draw all trigger volumes
auto numVolumes = *(int*)0xDBA220;
auto volumes = (NsTriggerVolumeBase**)0xDB6BE0;

for (int i = 0; i < numVolumes; i++)
{
auto volume = volumes[i];
auto instance = volume->m_instance;
auto data = (IntroDataTrigger*)instance->intro->data;

if (data)
{
if(data->shape == TriggerShape_Box)
{
auto extendX = cdc::Vector3{};
extendX.x += data->LocalXExtent;
extendX.y += data->LocalYExtent;
extendX.z += data->LocalZExtent;

auto extendY = cdc::Vector3{};
extendY.x -= data->LocalXExtent;
extendY.y -= data->LocalYExtent;
extendY.z -= data->LocalZExtent;

auto extendZ = cdc::Vector3{};
extendZ.x -= data->LocalXExtent;
extendZ.y += data->LocalYExtent;

auto extendW = cdc::Vector3{};
extendW.x += data->LocalXExtent;
extendW.y -= data->LocalYExtent;

cdc::Matrix mat;
mat.Build_XYZOrder(&instance->rotation);
mat.col3 = instance->position;

auto x = cdc::Mul3x4(&mat, &extendX);
auto y = cdc::Mul3x4(&mat, &extendY);
auto z = cdc::Mul3x4(&mat, &extendZ);
auto w = cdc::Mul3x4(&mat, &extendW);

DrawBoundingBox(&x, &y, &z, &w, RGB(255, 0, 0));
DrawBox(&x, &y, &z, &w, RGBA(255, 0, 0, 10));
}
else if (data->shape == TriggerShape_Sphere)
{
auto x = instance->position;
auto y = instance->position;

auto radius = data->Radius;

x.x += radius;
x.y += radius;
x.z += radius;

y.x -= radius;
y.y -= radius;
y.z -= radius;

DrawBoundingBox(&x, &y, RGB(255, 0, 0));
}
}
}
#endif
}

std::string Draw::FlagsToString(unsigned int flags)
{
std::string result;
Expand Down
2 changes: 2 additions & 0 deletions src/modules/Draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Draw : public Module
bool m_drawCollision = false;
bool m_drawPortals = false;
bool m_drawSignals = false;
bool m_drawTriggers = false;

// Instance options
bool m_drawIntro = false;
Expand All @@ -38,6 +39,7 @@ class Draw : public Module
void DrawCollision(TerrainGroup* group);
void DrawPortals(Level* level);
void DrawSignals(Level* level);
void DrawTriggers();

static std::string FlagsToString(unsigned int flags);

Expand Down
50 changes: 50 additions & 0 deletions src/render/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,54 @@ void DrawBoundingBox(cdc::Vector3* v0, cdc::Vector3* v1, int color)
DrawLine(&b2, &a2, color);
DrawLine(&b3, &a3, color);
DrawLine(&b4, &a4, color);
}

void DrawBoundingBox(cdc::Vector3* v0, cdc::Vector3* v1, cdc::Vector3* v2, cdc::Vector3* v3, int color)
{
// Yes this is terrible
auto a1 = cdc::Vector3{ v0->x, v0->y, v0->z };
auto a2 = cdc::Vector3{ v2->x, v2->y, v0->z };
auto a3 = cdc::Vector3{ v1->x, v1->y, v0->z };
auto a4 = cdc::Vector3{ v3->x, v3->y, v0->z };

auto b1 = cdc::Vector3{ v0->x, v0->y, v1->z };
auto b2 = cdc::Vector3{ v2->x, v2->y, v1->z };
auto b3 = cdc::Vector3{ v1->x, v1->y, v1->z };
auto b4 = cdc::Vector3{ v3->x, v3->y, v1->z };

DrawLine(&a1, &a2, color);
DrawLine(&a2, &a3, color);
DrawLine(&a3, &a4, color);
DrawLine(&a4, &a1, color);

DrawLine(&b1, &b2, color);
DrawLine(&b2, &b3, color);
DrawLine(&b3, &b4, color);
DrawLine(&b4, &b1, color);

DrawLine(&b1, &a1, color);
DrawLine(&b2, &a2, color);
DrawLine(&b3, &a3, color);
DrawLine(&b4, &a4, color);
}

void DrawBox(cdc::Vector3* v0, cdc::Vector3* v1, cdc::Vector3* v2, cdc::Vector3* v3, int color)
{
auto a1 = cdc::Vector3{ v0->x, v0->y, v0->z };
auto a2 = cdc::Vector3{ v2->x, v2->y, v0->z };
auto a3 = cdc::Vector3{ v1->x, v1->y, v0->z };
auto a4 = cdc::Vector3{ v3->x, v3->y, v0->z };

auto b1 = cdc::Vector3{ v0->x, v0->y, v1->z };
auto b2 = cdc::Vector3{ v2->x, v2->y, v1->z };
auto b3 = cdc::Vector3{ v1->x, v1->y, v1->z };
auto b4 = cdc::Vector3{ v3->x, v3->y, v1->z };

DrawPlane(&a1, &b2, color);
DrawPlane(&a2, &b3, color);
DrawPlane(&a3, &b4, color);
DrawPlane(&a4, &b1, color);

DrawPlane(&a1, &a3, color);
DrawPlane(&b1, &b3, color);
}
5 changes: 4 additions & 1 deletion src/render/Draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ void DRAW_DrawLines(LINEVERTEX* verts, int numlines);
void DrawTriangle(cdc::Vector3* v0, cdc::Vector3* v1, cdc::Vector3* v2, int color);
void DrawPlane(cdc::Vector3* v0, cdc::Vector3* v1, int color);
void DrawLine(cdc::Vector3* v0, cdc::Vector3* v1, int color);
void DrawBoundingBox(cdc::Vector3* v0, cdc::Vector3* v1, int color);
void DrawBoundingBox(cdc::Vector3* v0, cdc::Vector3* v1, int color);
void DrawBoundingBox(cdc::Vector3* v0, cdc::Vector3* v1, cdc::Vector3* v2, cdc::Vector3* v3, int color);

void DrawBox(cdc::Vector3* v0, cdc::Vector3* v1, cdc::Vector3* v2, cdc::Vector3* v3, int color);

0 comments on commit 56e0bb4

Please sign in to comment.