This commit is contained in:
MegaMech
2025-02-18 09:04:45 -07:00
5 changed files with 354 additions and 3 deletions
+1 -1
View File
@@ -118,4 +118,4 @@ struct IPathSpan {
#endif // __cplusplus
};
#endif // CORE_MATH_H
#endif // CORE_MATH_H
+234
View File
@@ -0,0 +1,234 @@
#include <libultraship.h>
#include <libultra/gbi.h>
#include "../CoreMath.h"
#include <libultra/types.h>
#include "../World.h"
#include "Editor.h"
#include "port/Engine.h"
#include <controller/controldevice/controller/mapping/keyboard/KeyboardScancodes.h>
#include <window/Window.h>
#include "engine/actors/Ship.h"
#include "port/Game.h"
extern "C" {
#include "common_structs.h"
#include "main.h"
#include "defines.h"
#include "actors.h"
#include "math_util.h"
#include "math_util_2.h"
#include "camera.h"
}
Editor::Editor() {
}
void Editor::Tick() {
auto wnd = GameEngine::Instance->context->GetWindow();
if (wnd->GetMouseState(Ship::LUS_MOUSE_BTN_LEFT)) {
MouseClick();
_draw = true;
} else {
_draw = false;
}
}
void Editor::MouseClick() {
auto wnd = GameEngine::Instance->context->GetWindow();
Camera* camera = &cameras[0];
if (!_spawned) {
_spawned = true;
object = reinterpret_cast<AShip*>(gWorldInstance.AddActor(new AShip(FVector(0, 0, 0), AShip::Skin::SHIP2)));
}
Ship::Coords mouse = wnd->GetMousePos();
// Get screen dimensions
uint32_t width = wnd->GetWidth();
uint32_t height = wnd->GetHeight();
// Convert mouse to NDS screen coordinates
float x = (2.0f * mouse.x) / width - 1.0f; // Normalized X: -1 to 1
float y = 1.0f - (2.0f * -mouse.y) / height; // Normalized Y: -1 to 1
float z = 1.0f; // z is typically 1.0 for the near plane
float rayClip[4] = {x, y, z, 1.0f};
// printf("rayClip %f %f %f\n", x, y, z);
Mat4 inversePerspMtx;
Mat4 perspMtx;
guMtxL2F(perspMtx, &gGfxPools->mtxPersp[0]);
Mat4 lookAtMtx;
guMtxL2F(lookAtMtx, &gGfxPools->mtxLookAt[0]);
Mat4 combinedMtx;
mtxf_multiplication(combinedMtx, lookAtMtx, perspMtx);
Mat4 inverseViewMtx;
float invRayWor[4];
Editor::Inverse((MtxF*)&combinedMtx[0][0], (MtxF*)&inverseViewMtx[0][0]);
MultiplyMatrixVector(inverseViewMtx, rayClip, invRayWor);
Vec3f direction = {invRayWor[0], invRayWor[1], invRayWor[2]};
float rayLength = 400.0f;
float nearPlaneOffset = 9.0f;
Vec3f rayStart;
rayStart[0] = camera->pos[0];// + direction[0] * nearPlaneOffset;
rayStart[1] = camera->pos[1];// + direction[1] * nearPlaneOffset;
rayStart[2] = camera->pos[2];// + direction[2] * nearPlaneOffset;
Vec3f rayEnd;
rayEnd[0] = (rayStart[0] + direction[0] * 400.0); //* (rayLength + ray_wor[0]);
rayEnd[1] = (rayStart[1] + direction[1] * 400.0); //* (rayLength + ray_wor[1]);
rayEnd[2] = (rayStart[2] + direction[2] * 400.0); // * (rayLength + ray_wor[2]);
_ray[0] = direction[0];
_ray[1] = direction[1];
_ray[2] = direction[2];
printf("cam pos %f, ray*len %f, cam+ray*len %f, brckt %f\n", camera->pos[0], _ray[0] * 300, camera->pos[0] + _ray[0] * 300, camera->pos[0] + (_ray[0] * 300));
// printf("pos %f %f %f dir %f %f %f\n", camera->pos[0], camera->pos[1], camera->pos[2], rayLength * ray_wor[0], rayLength * ray_wor[1], rayLength * ray_wor[2]);
//printf("3 %f %f %f\n", direction[0], direction[1], direction[2]);
object->Pos.x = rayEnd[0];
object->Pos.y = rayEnd[1];
object->Pos.z = rayEnd[2];
}
void Editor::Draw() {
if (_draw) {
// DrawObj(0);
//DrawObj(5);
//DrawObj(100);
//DrawObj(500);
//DrawObj(1000);
DrawObj(2000);
}
}
void Editor::DrawObj(float length) {
Mat4 mtx;
Vec3f pos2 = { cameras[0].pos[0] + _ray[0] * length, cameras[0].pos[1] + _ray[1] * length,
cameras[0].pos[2] + _ray[2] * length };
mtxf_translate(mtx, pos2);
mtxf_scale(mtx, 0.1);
render_set_position(mtx, 0);
gSPDisplayList(gDisplayListHead++, Editor::box_Cube_mesh);
}
void Editor::MultiplyMatrixVector(float matrix[4][4], float vector[4], float result[4]) {
for (int i = 0; i < 4; i++) {
result[i] = 0.0f;
for (int j = 0; j < 4; j++) {
result[i] += matrix[i][j] * vector[j];
}
}
}
s32 Editor::Inverse(MtxF* src, MtxF* dest) {
MtxF mfCopy;
s32 i;
s32 pad;
f32 temp2;
f32 temp1;
s32 thisCol;
s32 thisRow;
Editor::Copy(src, &mfCopy);
Editor::Clear(dest);
for (thisCol = 0; thisCol < 4; thisCol++) {
thisRow = thisCol;
while ((thisRow < 4) && (fabsf(mfCopy.mf[thisCol][thisRow]) < 0.0005f)) {
thisRow++;
}
if (thisRow == 4) {
// Reaching row = 4 means the column is either all 0 or a duplicate column.
// Therefore src is a singular matrix (0 determinant).
// osSyncPrintf(VT_COL(YELLOW, BLACK));
// osSyncPrintf("Skin_Matrix_InverseMatrix():逆行列つくれません\n");
// osSyncPrintf(VT_RST);
return 2;
}
if (thisRow != thisCol) {
// Diagonal element mf[thisCol][thisCol] is zero.
// Swap the rows thisCol and thisRow.
for (i = 0; i < 4; i++) {
temp1 = mfCopy.mf[i][thisRow];
mfCopy.mf[i][thisRow] = mfCopy.mf[i][thisCol];
mfCopy.mf[i][thisCol] = temp1;
temp2 = dest->mf[i][thisRow];
dest->mf[i][thisRow] = dest->mf[i][thisCol];
dest->mf[i][thisCol] = temp2;
}
}
// Scale this whole row such that the diagonal element is 1.
temp1 = mfCopy.mf[thisCol][thisCol];
for (i = 0; i < 4; i++) {
mfCopy.mf[i][thisCol] /= temp1;
dest->mf[i][thisCol] /= temp1;
}
for (thisRow = 0; thisRow < 4; thisRow++) {
if (thisRow != thisCol) {
temp1 = mfCopy.mf[thisCol][thisRow];
for (i = 0; i < 4; i++) {
mfCopy.mf[i][thisRow] -= mfCopy.mf[i][thisCol] * temp1;
dest->mf[i][thisRow] -= dest->mf[i][thisCol] * temp1;
}
}
}
}
return 0;
}
void Editor::Copy(MtxF* src, MtxF* dest) {
dest->xx = src->xx;
dest->yx = src->yx;
dest->zx = src->zx;
dest->wx = src->wx;
dest->xy = src->xy;
dest->yy = src->yy;
dest->zy = src->zy;
dest->wy = src->wy;
dest->xz = src->xz;
dest->yz = src->yz;
dest->zz = src->zz;
dest->wz = src->wz;
dest->xw = src->xw;
dest->yw = src->yw;
dest->zw = src->zw;
dest->ww = src->ww;
}
void Editor::Clear(MtxF* mf) {
mf->xx = 1.0f;
mf->yy = 1.0f;
mf->zz = 1.0f;
mf->ww = 1.0f;
mf->yx = 0.0f;
mf->zx = 0.0f;
mf->wx = 0.0f;
mf->xy = 0.0f;
mf->zy = 0.0f;
mf->wy = 0.0f;
mf->xz = 0.0f;
mf->yz = 0.0f;
mf->wz = 0.0f;
mf->xw = 0.0f;
mf->yw = 0.0f;
mf->zw = 0.0f;
}
+114
View File
@@ -0,0 +1,114 @@
#pragma once
#include <libultraship.h>
#include <libultra/gbi.h>
#include "engine/objects/Flagpole.h"
class AShip;
class Editor {
public:
Editor();
void Tick();
void Draw();
void MouseClick();
void MultiplyMatrixVector(float matrix[4][4], float vector[4], float result[4]);
private:
bool _spawned = false;
bool _draw = false;
AShip* object;
Vec3f _ray;
s32 Inverse(MtxF* src, MtxF* dest);
void Copy(MtxF* src, MtxF* dest);
void Clear(MtxF* mf);
void DrawObj(float length);
Vtx box_Cube_mesh_vtx_cull[8] = {
{{ {-46, -46, -46}, 0, {0, 0}, {0, 0, 0, 0} }},
{{ {-46, -46, 46}, 0, {0, 0}, {0, 0, 0, 0} }},
{{ {-46, 46, 46}, 0, {0, 0}, {0, 0, 0, 0} }},
{{ {-46, 46, -46}, 0, {0, 0}, {0, 0, 0, 0} }},
{{ {46, -46, -46}, 0, {0, 0}, {0, 0, 0, 0} }},
{{ {46, -46, 46}, 0, {0, 0}, {0, 0, 0, 0} }},
{{ {46, 46, 46}, 0, {0, 0}, {0, 0, 0, 0} }},
{{ {46, 46, -46}, 0, {0, 0}, {0, 0, 0, 0} }},
};
Vtx box_Cube_mesh_vtx_0[24] = {
{{ {-46, -46, 46}, 0, {368, 1008}, {129, 0, 0, 255} }},
{{ {-46, 46, 46}, 0, {624, 1008}, {129, 0, 0, 255} }},
{{ {-46, 46, -46}, 0, {624, 752}, {129, 0, 0, 255} }},
{{ {-46, -46, -46}, 0, {368, 752}, {129, 0, 0, 255} }},
{{ {-46, -46, -46}, 0, {368, 752}, {0, 0, 129, 255} }},
{{ {-46, 46, -46}, 0, {624, 752}, {0, 0, 129, 255} }},
{{ {46, 46, -46}, 0, {624, 496}, {0, 0, 129, 255} }},
{{ {46, -46, -46}, 0, {368, 496}, {0, 0, 129, 255} }},
{{ {46, -46, -46}, 0, {368, 496}, {127, 0, 0, 255} }},
{{ {46, 46, -46}, 0, {624, 496}, {127, 0, 0, 255} }},
{{ {46, 46, 46}, 0, {624, 240}, {127, 0, 0, 255} }},
{{ {46, -46, 46}, 0, {368, 240}, {127, 0, 0, 255} }},
{{ {46, -46, 46}, 0, {368, 240}, {0, 0, 127, 255} }},
{{ {46, 46, 46}, 0, {624, 240}, {0, 0, 127, 255} }},
{{ {-46, 46, 46}, 0, {624, -16}, {0, 0, 127, 255} }},
{{ {-46, -46, 46}, 0, {368, -16}, {0, 0, 127, 255} }},
{{ {-46, -46, -46}, 0, {112, 496}, {0, 129, 0, 255} }},
{{ {46, -46, -46}, 0, {368, 496}, {0, 129, 0, 255} }},
{{ {46, -46, 46}, 0, {368, 240}, {0, 129, 0, 255} }},
{{ {-46, -46, 46}, 0, {112, 240}, {0, 129, 0, 255} }},
{{ {46, 46, -46}, 0, {624, 496}, {0, 127, 0, 255} }},
{{ {-46, 46, -46}, 0, {880, 496}, {0, 127, 0, 255} }},
{{ {-46, 46, 46}, 0, {880, 240}, {0, 127, 0, 255} }},
{{ {46, 46, 46}, 0, {624, 240}, {0, 127, 0, 255} }},
};
Gfx box_Cube_mesh_tri_0[8] = {
gsSPVertex(box_Cube_mesh_vtx_0 + 0, 24, 0),
gsSP2Triangles(0, 1, 2, 0, 0, 2, 3, 0),
gsSP2Triangles(4, 5, 6, 0, 4, 6, 7, 0),
gsSP2Triangles(8, 9, 10, 0, 8, 10, 11, 0),
gsSP2Triangles(12, 13, 14, 0, 12, 14, 15, 0),
gsSP2Triangles(16, 17, 18, 0, 16, 18, 19, 0),
gsSP2Triangles(20, 21, 22, 0, 20, 22, 23, 0),
gsSPEndDisplayList(),
};
Gfx mat_box_f3dlite_material[6] = {
gsSPClearGeometryMode(G_CLIPPING),
gsDPPipeSync(),
gsDPSetCombineLERP(0, 0, 0, SHADE, 0, 0, 0, ENVIRONMENT, 0, 0, 0, SHADE, 0, 0, 0, ENVIRONMENT),
gsDPSetAlphaDither(G_AD_NOISE),
gsSPTexture(65535, 65535, 0, 0, 1),
gsSPEndDisplayList(),
};
Gfx mat_revert_box_f3dlite_material[4] = {
gsSPSetGeometryMode(G_CLIPPING),
gsDPPipeSync(),
gsDPSetAlphaDither(G_AD_DISABLE),
gsSPEndDisplayList(),
};
Gfx box_Cube_mesh[13] = {
gsSPClearGeometryMode(G_LIGHTING),
gsSPVertex(box_Cube_mesh_vtx_cull + 0, 8, 0),
gsSPSetGeometryMode(G_LIGHTING),
gsSPCullDisplayList(0, 7),
gsSPDisplayList(mat_box_f3dlite_material),
gsSPDisplayList(box_Cube_mesh_tri_0),
gsSPDisplayList(mat_revert_box_f3dlite_material),
gsDPPipeSync(),
gsSPSetGeometryMode(G_LIGHTING),
gsSPClearGeometryMode(G_TEXTURE_GEN),
gsDPSetCombineLERP(0, 0, 0, SHADE, 0, 0, 0, ENVIRONMENT, 0, 0, 0, SHADE, 0, 0, 0, ENVIRONMENT),
gsSPTexture(65535, 65535, 0, 0, 0),
gsSPEndDisplayList(),
};
};
-2
View File
@@ -40,8 +40,6 @@ u32 fTargetPlayer = false;
u32 fMode; // freecam mode should probably be an enum
u32 fModeInit = false;
int rightMouseButtonDown = 0; // Track if right mouse button is held down
u32 bFreecamUseController = false;
/**
+5
View File
@@ -44,6 +44,7 @@
#include "Smoke.h"
#include "engine/HM_Intro.h"
#include "engine/editor/Editor.h"
extern "C" {
#include "main.h"
@@ -99,6 +100,8 @@ ModelLoader gModelLoader;
HarbourMastersIntro gMenuIntro;
Editor gEditor;
s32 gTrophyIndex = NULL;
void CustomEngineInit() {
@@ -388,6 +391,7 @@ void CM_TickObjects() {
if (gWorldInstance.CurrentCourse) {
gWorldInstance.TickObjects();
}
gEditor.Tick();
}
// A couple objects such as lakitu are ticked inside of process_game_tick which support 60fps.
@@ -402,6 +406,7 @@ void CM_DrawObjects(s32 cameraId) {
if (gWorldInstance.CurrentCourse) {
gWorldInstance.DrawObjects(cameraId);
}
gEditor.Draw();
}
void CM_TickParticles() {