From 32632cacdb316d7f2d3b6ecdb634f0ef9aebadf0 Mon Sep 17 00:00:00 2001 From: MegaMech Date: Wed, 14 May 2025 18:30:32 -0600 Subject: Impl new intro (#193) * Update menus * Update CMakeLists.txt * Add Ship * Impl hm ship actors * Update HM course * Impl new intro * Finish intro scene * Rename * Start Editor Work * raycast works * Fix ScreenRayTrace in widescreen * Basic Actor Picking * wip * Editor use vtx collision * gizmo work * otr works for object picking * Impl objects for editor * actor init * Update * Add all axis move (freemove) * Docking Windows works here * Setup imgui layout for editor * Editor Snap to Ground works * Basic Scene Explorer Works * Editor get actor names * Impl editor object names * impl Editor Play and Pause buttons * Editor translate works while paused * Fix freecam lighting * Added adjustable track properties to editor * Editor matrix, icons, rotation, impl light * Setup Track Properties 1 * Editor tooling wip * Load modded o2rs * Don't enable hud if editor is enabled * Updates * SceneManager nearly working * Fix mario kart 64 intro logo sizing * Fix Rotator * Finish new matrix translation code * Cleanup headers * Cleanup * Cleanup 2 * Cleanup 3 * Prevent divize by zero crash * Add visible circle for translate in all axis * Editor scaling/rot works properly now * Scale All_Axis evenly * Fixes to includes to work on Linux. * Removed overfilled arguments in gfx_create_framebuffer() * Added missing function definitions to Game.h * Editor sun face the camera * Add rotation model to gizmo * Add new handles * Failed attempt at transforming collision * Impl water volume * Import fast64 paths * water surface * Scene Setup 1 * Custom Track O2R almost working needs testing * Custom Track Load path O2r * Render custom track. Wip collision * Add missing function * Debug Spawning Custom O2R Track * Import courses working now * Fix memory leak * Remove New Track Button * Engine.cpp more consistent with sf64 * Fix Editor Enable Button * Editor Accurate mouse click drag objects * Editor selects closest object and cleanup * Gizmo rot and scale collision working * Remove constexpr from IRotator * Impl properties for location/rot/scale * Better Properties display, swap rot handles * Fix content browser dock and editor now disabled by default * Remove GameInfoWindow, Multiplayer Button, and FPS Slider * Disable Editor when its disabled * Add new logo to hm intro * Fix pause menu item box cursor * Remove minimap from Course::from_json and to_json * Impl Import Minimap * Fix custom minimap rendering * minimap uses extension .png * Refactor minimap * Freecam only for player 1 * GrandPrix Balloons work in custom track * Track Id is now std::string and outside of Props * Moved editor assets to be included in ship.o2r * Fixed GenerateO2R to package the correct folder and save to the correct filename * Linux specific changes. * Added "#include " that required them * Changed how the "ship.o2r" file is loaded to allow it to load the file from within appimages. * Changed the Linuxdeploy version to avoid errors later when the Github Actions creates appimages(same fix applied to other ports.) * Revert "Moved editor assets to be included in ship.o2r" This reverts commit 05704c01f761baa67a83e357d9765f5c6b5e3fdb. * Added back files(this time without LUS changes) * Changed workflow file to use correct filename for assets file. * Missed a few spots in the workflow file. * Added .desktop file and made corrections to the main workflow. * Added the rest of upstream CMakeLists.txt * disabled USE_NETWORKING * New InverseMatrix * Renamed both .o2r files to be more accurate to its contents. * Reverted CmakeList.txt --------- Co-authored-by: MegaMech <7255464+MegaMech@users.noreply.github.com> Co-authored-by: sitton76 <58642183+sitton76@users.noreply.github.com> --- src/engine/editor/Collision.cpp | 188 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 src/engine/editor/Collision.cpp (limited to 'src/engine/editor/Collision.cpp') diff --git a/src/engine/editor/Collision.cpp b/src/engine/editor/Collision.cpp new file mode 100644 index 000000000..bf62d6273 --- /dev/null +++ b/src/engine/editor/Collision.cpp @@ -0,0 +1,188 @@ +#include "Collision.h" + +#include +#include +#include "Matrix.h" + +extern "C" { +#include "main.h" +#include "other_textures.h" +} + +namespace Editor { + void GenerateCollisionMesh(GameObject* object, Gfx* model, float scale) { + int8_t opcode; + uintptr_t lo; + uintptr_t hi; + + Gfx* ptr = model; + Vtx* vtx = NULL; + size_t i = 0; + bool run = true; + while (run) { + i++; + lo = ptr->words.w0; + hi = ptr->words.w1; + + 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_DL_OTR_FILEPATH: + // printf("otr filepath: %s\n", (const char*)hi); + GenerateCollisionMesh(object, (Gfx*)ResourceGetDataByName((const char*)hi), scale); + break; + case G_VTX: + vtx = (Vtx*)ptr->words.w1; + break; + case G_VTX_OTR_HASH: { + ptr++; + vtx = (Vtx*)ResourceGetDataByCrc(((uint64_t)(ptr->words.w0 << 32)) + ptr->words.w1); + break; + } + case G_VTX_OTR_FILEPATH: { + const char* filePath = (const char*)hi; + ptr++; + size_t vtxDataOff = ptr->words.w1 & 0xFFFF; + vtx = ( (Vtx*)ResourceGetDataByName(filePath) ) + vtxDataOff; + break; + } + case G_TRI1: { + if (vtx == NULL) { + ptr++; + continue; + } + uint32_t v1 = ((hi & 0x00FF0000) >> 16) / 2; + uint32_t v2 = ((hi & 0x0000FF00) >> 8) / 2; + uint32_t v3 = (hi & 0x000000FF) / 2; + + FVector p1 = FVector(vtx[v1].v.ob[0], vtx[v1].v.ob[1], vtx[v1].v.ob[2]); + FVector p2 = FVector(vtx[v2].v.ob[0], vtx[v2].v.ob[1], vtx[v2].v.ob[2]); + FVector p3 = FVector(vtx[v3].v.ob[0], vtx[v3].v.ob[1], vtx[v3].v.ob[2]); + + object->Triangles.push_back({p1, p2, p3}); + break; + } + case G_TRI1_OTR: { + if (vtx == NULL) { + ptr++; + continue; + } + + // The shift values here are different than the above. No idea why. But it has to be this way. + uint32_t v1 = (lo & 0x0000FFFF); + uint32_t v2 = (hi >> 16); + uint32_t v3 = (hi & 0x0000FFFF); + + FVector p1 = FVector(vtx[v1].v.ob[0], vtx[v1].v.ob[1], vtx[v1].v.ob[2]); + FVector p2 = FVector(vtx[v2].v.ob[0], vtx[v2].v.ob[1], vtx[v2].v.ob[2]); + FVector p3 = FVector(vtx[v3].v.ob[0], vtx[v3].v.ob[1], vtx[v3].v.ob[2]); + + object->Triangles.push_back({p1, p2, p3}); + + break; + } + case G_TRI2: { + if (vtx == NULL) { + ptr++; + continue; + } + uint32_t v1 = ((lo & 0x00FF0000) >> 16) / 2; + uint32_t v2 = ((lo & 0x0000FF00) >> 8) / 2; + uint32_t v3 = (lo & 0x000000FF) / 2; + + // This is actually triangle 2; vert 1,2,3. + uint32_t v4 = ((hi & 0x00FF0000) >> 16) / 2; + uint32_t v5 = ((hi & 0x0000FF00) >> 8) / 2; + uint32_t v6 = (hi & 0x000000FF) / 2; + + FVector p1 = FVector(vtx[v1].v.ob[0], vtx[v1].v.ob[1], vtx[v1].v.ob[2]); + FVector p2 = FVector(vtx[v2].v.ob[0], vtx[v2].v.ob[1], vtx[v2].v.ob[2]); + FVector p3 = FVector(vtx[v3].v.ob[0], vtx[v3].v.ob[1], vtx[v3].v.ob[2]); + + FVector p4 = FVector(vtx[v4].v.ob[0], vtx[v4].v.ob[1], vtx[v4].v.ob[2]); + FVector p5 = FVector(vtx[v5].v.ob[0], vtx[v5].v.ob[1], vtx[v5].v.ob[2]); + FVector p6 = FVector(vtx[v6].v.ob[0], vtx[v6].v.ob[1], vtx[v6].v.ob[2]); + + object->Triangles.push_back({p1, p2, p3}); + object->Triangles.push_back({p4, p5, p6}); + break; + } + case G_QUAD: { + if (vtx == NULL) { + ptr++; + continue; + } + uint32_t v1 = ((hi & 0x00FF0000) >> 16) / 2; + uint32_t v2 = ((hi & 0x0000FF00) >> 8) / 2; + uint32_t v3 = (hi & 0x000000FF) / 2; + uint32_t v4 = ((hi & 0xFF000000) >> 24) / 2; + + FVector p1 = FVector(vtx[v1].v.ob[0], vtx[v1].v.ob[1], vtx[v1].v.ob[2]); + FVector p2 = FVector(vtx[v2].v.ob[0], vtx[v2].v.ob[1], vtx[v2].v.ob[2]); + FVector p3 = FVector(vtx[v3].v.ob[0], vtx[v3].v.ob[1], vtx[v3].v.ob[2]); + FVector p4 = FVector(vtx[v4].v.ob[0], vtx[v4].v.ob[1], vtx[v4].v.ob[2]); + + object->Triangles.push_back({p1, p2, p3}); + object->Triangles.push_back({p1, p3, p4}); + break; + } + case G_ENDDL: + run = false; + break; + } + + ptr++; + } + } + + std::unordered_map> gDebugObjVtxCache; + + // Render a collision model + void DebugCollision(GameObject* obj, FVector pos, IRotator rot, FVector scale, const std::vector& triangles) { + if (obj == NULL || triangles.empty()) { + return; + } + + gSPTexture(gDisplayListHead++, 0xFFFF, 0xFFFF, 0, G_TX_RENDERTILE, G_OFF); + + auto& vtxBuffer = gDebugObjVtxCache[obj]; + if (vtxBuffer.empty()) { + for (const auto& tri : triangles) { + vtxBuffer.push_back({(s16)tri.v0.x, (s16)tri.v0.y, (s16)tri.v0.z, 0, {0, 0}, {255, 255, 255, 255}}); + vtxBuffer.push_back({(s16)tri.v1.x, (s16)tri.v1.y, (s16)tri.v1.z, 0, {0, 0}, {255, 255, 255, 255}}); + vtxBuffer.push_back({(s16)tri.v2.x, (s16)tri.v2.y, (s16)tri.v2.z, 0, {0, 0}, {255, 255, 255, 255}}); + } + } + + // Setup matrix and render state + Mat4 mtx; + ApplyMatrixTransformations(mtx, pos, rot, scale); + Editor_AddMatrix(mtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW); + + gSPSetGeometryMode(gDisplayListHead++, G_FOG); + gDPPipeSync(gDisplayListHead++); + gDPSetCombineMode(gDisplayListHead++, G_CC_PRIMITIVE, G_CC_PRIMITIVE); + gSPTexture(gDisplayListHead++, 0, 0, 0, G_TX_RENDERTILE, G_OFF); + gDPSetRenderMode(gDisplayListHead++, G_RM_FOG_SHADE_A, G_RM_AA_ZB_OPA_SURF2); + uint32_t hash = (uint32_t)((uintptr_t)obj ^ ((uintptr_t)obj >> 16)); + u8 r = (hash >> 16) & 0xFF; + u8 g = (hash >> 8) & 0xFF; + u8 b = hash & 0xFF; + + gDPSetPrimColor(gDisplayListHead++, 0, 0, r, g, b, 255); + + // Render triangles in batches of 3 + for (size_t i = 0; i + 2 < vtxBuffer.size(); i += 3) { + gSPVertex(gDisplayListHead++, (uintptr_t)&vtxBuffer[i], 3, 0); + gSP1Triangle(gDisplayListHead++, 0, 1, 2, 0); + } + + gSPSetGeometryMode(gDisplayListHead++, G_CULL_BACK | G_ZBUFFER); + } +} -- cgit v1.2.3