otr works for object picking

This commit is contained in:
MegaMech
2025-03-01 13:41:32 -07:00
parent 3bb80b8464
commit 842042d515
7 changed files with 129 additions and 103 deletions
+10 -2
View File
@@ -16,6 +16,7 @@ extern "C" {
#include "audio/external.h"
#include "menus.h"
#include "common_data.h"
#include "mario_raceway_data.h"
}
World::World() {}
@@ -124,9 +125,16 @@ struct Actor* World::AddBaseActor(s16 actorType) {
Actors.push_back(new AActor());
AActor* actor = Actors.back();
if (actorType == ACTOR_ITEM_BOX) {
actor->Model = (Gfx*)LOAD_ASSET_RAW(itemBoxQuestionMarkModel);
switch(actorType) {
case ACTOR_ITEM_BOX:
actor->Model = (Gfx*)LOAD_ASSET_RAW(D_0D003090);
break;
case ACTOR_PIRANHA_PLANT:
actor->Model = (Gfx*)LOAD_ASSET_RAW(d_course_mario_raceway_dl_piranha_plant);
break;
}
gEditor.AddObject((FVector*) &actor->Pos, actor->Model, 1.0f, CollisionType::VTX_INTERSECT, 0.0f);
// Skip C++ vtable to access variables in C
return reinterpret_cast<struct Actor*>(reinterpret_cast<char*>(Actors.back()) + sizeof(void*));
+12
View File
@@ -14,15 +14,27 @@ void GenerateCollisionMesh(GameObject& object, Gfx* model, float scale) {
i++;
lo = ptr->words.w0;
hi = ptr->words.w1;
printf("w0 0x%llX 0x%llX\n", lo, hi);
opcode = (EDITOR_GFX_GET_OPCODE(lo) >> 24);
switch(opcode) {
case G_DL:
GenerateCollisionMesh(object, (Gfx*)hi, scale);
break;
case G_DL_OTR_HASH:
ptr++;
GenerateCollisionMesh(object, (Gfx*)ResourceGetDataByCrc(((uint64_t)(ptr->words.w0 << 32)) + ptr->words.w1), scale);
break;
case G_VTX:
vtx = (Vtx*)ptr->words.w1;
break;
case G_VTX_OTR_HASH: {
const uintptr_t offset = hi;
ptr++;
vtx = (Vtx*)ResourceGetDataByCrc(((uint64_t)(ptr->words.w0 << 32)) + ptr->words.w1);
break;
}
case G_TRI1: {
if (vtx == NULL) {
ptr++;
+3 -10
View File
@@ -35,7 +35,7 @@ Editor::Editor() {
void Editor::Load() {
eObjectPicker.Load();
for (auto& object : eGameObjects) {
GenerateCollisionMesh(object, object.Model, 1.0f);
//GenerateCollisionMesh(object, object.Model, 1.0f);
}
}
@@ -84,6 +84,7 @@ void Editor::Tick() {
void Editor::Draw() {
eObjectPicker.Draw();
DrawObj();
}
void Editor::AddObject(FVector* pos, Gfx* model, float scale, CollisionType collision, float boundingBoxSize) {
@@ -91,16 +92,8 @@ void Editor::AddObject(FVector* pos, Gfx* model, float scale, CollisionType coll
if (model != NULL) {
eGameObjects.push_back({pos, model, {}, collision, boundingBoxSize});
GenerateCollisionMesh(eGameObjects.back(), model, scale);
printf("\n");
} else { // to bounding box or sphere collision
eGameObjects.push_back({pos, model, {}, CollisionType::BOUNDING_BOX, 2.0f});
}
}
void Editor::DrawObj(float length) {
Mat4 mtx;
Vec3f pos2 = { _ray[0], _ray[1], _ray[2] };
mtxf_translate(mtx, pos2);
mtxf_scale(mtx, 0.03);
render_set_position(mtx, 0);
gSPDisplayList(gDisplayListHead++, Editor::box_Cube_mesh);
}
-88
View File
@@ -27,92 +27,4 @@ private:
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(),
};
};
+99 -1
View File
@@ -190,7 +190,7 @@ void Clear(MtxF* mf) {
mf->yw = 0.0f;
mf->zw = 0.0f;
}
FVector DebugPoss = {0, 0, 0};
bool IntersectRayTriangle(const Ray& ray, const Triangle& tri, const FVector& objectPos, float& t) {
const float EPSILON = 1e-6f;
@@ -199,6 +199,8 @@ bool IntersectRayTriangle(const Ray& ray, const Triangle& tri, const FVector& ob
FVector v1 = tri.v1 + objectPos;
FVector v2 = tri.v2 + objectPos;
DebugPoss = v0;
FVector edge1 = v1 - v0;
FVector edge2 = v2 - v0;
FVector h = ray.Direction.Cross(edge2);
@@ -223,6 +225,15 @@ bool IntersectRayTriangle(const Ray& ray, const Triangle& tri, const FVector& ob
t = f * edge2.Dot(q);
return t > EPSILON;
}
extern Gfx box_Cube_mesh[];
void DrawObj() {
Mat4 mtx;
Vec3f pos2 = { DebugPoss.x, DebugPoss.y, DebugPoss.z };
mtxf_translate(mtx, pos2);
mtxf_scale(mtx, 0.03);
render_set_position(mtx, 0);
gSPDisplayList(gDisplayListHead++, box_Cube_mesh);
}
bool FindClosestObject(const Ray& ray, const std::vector<GameObject>& objects, GameObject& outObject, float& outDistance) {
@@ -248,3 +259,90 @@ bool FindClosestObject(const Ray& ray, const std::vector<GameObject>& objects, G
return false;
}
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(),
};
+1
View File
@@ -45,6 +45,7 @@ FVector4 MultiplyMatrixVector(float matrix[4][4], float vector[4]);
s32 Inverse(MtxF* src, MtxF* dest);
void Copy(MtxF* src, MtxF* dest);
void Clear(MtxF* mf);
void DrawObj();
bool IntersectRayTriangle(const Ray& ray, const Triangle& tri, const FVector& objectPos, float& t);
+4 -2
View File
@@ -113,14 +113,16 @@ void ObjectPicker::FindObject(Ray ray, std::vector<GameObject>& objects) {
if (boundingBox == 0.0f) {
boundingBox = 2.0f;
}
float t;
switch(object.Collision) {
case CollisionType::VTX_INTERSECT:
printf("vtx object\n");
for (const auto& tri : object.Triangles) {
float t;
if (IntersectRayTriangle(ray, tri, *object.Pos, t)) {
printf("\nSELECTED OBJECT\n\n");
_selected = &object;
found = true;
}
}
break;
@@ -134,7 +136,7 @@ void ObjectPicker::FindObject(Ray ray, std::vector<GameObject>& objects) {
Vec3f boxMax = { object.Pos->x + boundingBox * max,
object.Pos->y + boundingBox * max,
object.Pos->z + boundingBox * max };
float t;
if (QueryCollisionRayActor(&ray.Origin.x, &ray.Direction.x, boxMin, boxMax, &t)) {
// if (actor == _selected) {
// _selected = nullptr;