From 3ce1acc633011bd35755814fa43dd25cfc05a449 Mon Sep 17 00:00:00 2001 From: Asuro Date: Sat, 29 Mar 2025 22:27:34 +0100 Subject: [PATCH] wip card clicking --- assets/blender/tablet.blend | 3 ++ assets/textures/w straight.png | 3 ++ src/game/Global.cpp | 6 +++ src/game/Global.h | 27 ++++++++++ src/game/Input.cpp | 5 ++ src/game/Input.h | 1 + src/game/Instance.h | 4 +- src/game/Level.cpp | 92 +++++++++++++++++++++++++------- src/game/data/puzzles/0.pzl | 2 +- src/game/data/static/puzzle.dat | Bin 1808 -> 1808 bytes src/models/tablet.glb | 3 ++ src/textures/w straight.ktx | 3 ++ 12 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 assets/blender/tablet.blend create mode 100644 assets/textures/w straight.png create mode 100644 src/models/tablet.glb create mode 100644 src/textures/w straight.ktx diff --git a/assets/blender/tablet.blend b/assets/blender/tablet.blend new file mode 100644 index 0000000..86acfa7 --- /dev/null +++ b/assets/blender/tablet.blend @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9eaa1529a51ec918937646f594007cdf65ffb01f78ba2b575b5ce216f8c59096 +size 870165 diff --git a/assets/textures/w straight.png b/assets/textures/w straight.png new file mode 100644 index 0000000..4533c28 --- /dev/null +++ b/assets/textures/w straight.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eadbbab5dbe448a8aaefbc3ec9d8af1786b76e1e301dfca0b8bc72ebad5b739d +size 12000 diff --git a/src/game/Global.cpp b/src/game/Global.cpp index aa119cd..747a26c 100644 --- a/src/game/Global.cpp +++ b/src/game/Global.cpp @@ -171,3 +171,9 @@ Mat4 Mat4::Transpose() bx::mtxTranspose(result.M, M); return result; } +Vec4 Mat4::Mul(const Vec4& vec) +{ + Vec4 out; + bx::vec4MulMtx(&out.x, &vec.x, &M[0]); + return out; +} diff --git a/src/game/Global.h b/src/game/Global.h index c4a79f8..47370bc 100644 --- a/src/game/Global.h +++ b/src/game/Global.h @@ -21,6 +21,19 @@ struct Vec2 return lhs; } + Vec2& operator+=(const float& rhs) + { + x += rhs; + y += rhs; + return *this; + } + + friend Vec2 operator+(Vec2 lhs, const float& rhs) + { + lhs += rhs; + return lhs; + } + Vec2& operator-=(const Vec2& rhs) { x -= rhs.x; @@ -34,6 +47,19 @@ struct Vec2 return lhs; } + Vec2& operator-=(const float& rhs) + { + x -= rhs; + y -= rhs; + return *this; + } + + friend Vec2 operator-(Vec2 lhs, const float& rhs) + { + lhs -= rhs; + return lhs; + } + Vec2& operator*=(const float rhs) { x *= rhs; @@ -180,6 +206,7 @@ struct Mat4 Mat4 Inverse(); Mat4 Transpose(); + Vec4 Mul(const Vec4& vec); }; inline int32_t SetFlags(int32_t in, int32_t flags) diff --git a/src/game/Input.cpp b/src/game/Input.cpp index 11175ea..5f668b3 100644 --- a/src/game/Input.cpp +++ b/src/game/Input.cpp @@ -56,4 +56,9 @@ namespace Game if (!IsMouseAllowed()) return {}; return {GetShared().Window.MouseDeltaX, GetShared().Window.MouseDeltaY}; } + Vec2 GetMousePos() + { + ImVec2 pos = ImGui::GetMousePos(); + return {pos.x, pos.y}; + } } // namespace Game diff --git a/src/game/Input.h b/src/game/Input.h index 70bf3d6..290b750 100644 --- a/src/game/Input.h +++ b/src/game/Input.h @@ -394,4 +394,5 @@ namespace Game bool GetMouseButtonPressedNow(MouseButton button); bool GetMouseButtonReleasedNow(MouseButton button); Vec2 GetMouseMovement(); + Vec2 GetMousePos(); } // namespace Game diff --git a/src/game/Instance.h b/src/game/Instance.h index f61a7cb..9a05fbb 100644 --- a/src/game/Instance.h +++ b/src/game/Instance.h @@ -28,12 +28,14 @@ namespace Game struct PlayerData { + Transform PlayerCamTransform; Transform FreeflyCamTransform; + Mat4 Projection; + Mat4 ProjectionInverse; float FreeflyXRot = 0.0f; float FreeflyYRot = 0.0f; float WalkXRot = 0.0f; float WalkYRot = 0.0f; - Transform PlayerCamTransform; CameraMode CameraM = CameraMode::Freefly; InputMode InputM = InputMode::Game; float MouseSensitivity = 1.0f; diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 9b11f5f..ea08d10 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -58,6 +58,8 @@ namespace Game { sampler = textures[TextureHandle.TextureIdx].SamplerHandle; tex = textures[TextureHandle.TextureIdx].RenderHandle; + texInfo[0] = textures[TextureHandle.TextureIdx].Info.width; + texInfo[1] = textures[TextureHandle.TextureIdx].Info.height; } bgfx::setTexture(0, sampler, tex); @@ -93,8 +95,6 @@ namespace Game needReset |= PuzzleTiles.Setup(storagePtr, needReset); needReset |= UIQuads.Setup(storagePtr, needReset); - Tests.IsEnabled = false; - Puzzle::Setup(); bx::Error err; @@ -250,27 +250,27 @@ namespace Game { ZoneScopedN("Level Render"); auto& shared = GetShared(); + auto& player = GetInstance().Player; - float proj[16]; - bx::mtxProj(proj, + bx::mtxProj(&player.Projection.M[0], 75.0f, float(shared.Window.WindowWidth) / float(shared.Window.WindowHeight), 0.1f, 1000.0f, bgfx::getCaps()->homogeneousDepth); + bx::mtxInverse(&player.ProjectionInverse.M[0], &player.Projection.M[0]); - auto& player = GetInstance().Player; bool isFreefly = player.CameraM == CameraMode::Freefly; Cubes.Get(PlayerOutsideViewCube).EData.Visible = isFreefly; if (isFreefly) { player.FreeflyCamTransform.UpdateMatrix(); - bgfx::setViewTransform(viewId, player.FreeflyCamTransform.MI.M, proj); + bgfx::setViewTransform(viewId, player.FreeflyCamTransform.MI.M, &player.Projection.M[0]); } else { player.PlayerCamTransform.UpdateMatrix(); - bgfx::setViewTransform(viewId, player.PlayerCamTransform.MI.M, proj); + bgfx::setViewTransform(viewId, player.PlayerCamTransform.MI.M, &player.Projection.M[0]); } bgfx::touch(viewId); @@ -297,9 +297,7 @@ namespace Game void TestEntity::Setup() { EData.MaterialHandle = EMaterial::Default; - EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/zurg.gltf"); - - EData.Transform.Position = {0.0f, 0.0f, 0.0f}; + EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/cube.gltf"); } void WorldPuzzle::Setup() @@ -325,6 +323,7 @@ namespace Game void WorldPuzzle::Update() { Level& level = GetInstance().GameLevel; + auto& window = GetShared().Window; auto& staticCards = Puzzle::GetStaticPuzzleData().Cards; Transform& camTransform = GetInstance().Player.PlayerCamTransform; @@ -335,24 +334,42 @@ namespace Game boardTransform.Rotation = camTransform.Rotation; Vec3 fw = {camTransform.M.M[8], camTransform.M.M[9], camTransform.M.M[10]}; Vec3 pos = cameraPos; - pos += fw; + pos += fw * 10.0f; boardTransform.SetPosition(pos); + bool clicked = GetMouseButtonPressedNow(MouseButton::Left); + Vec2 mousePos = GetMousePos(); + mousePos.x = mousePos.x / window.WindowWidth; + mousePos.y = mousePos.y / window.WindowHeight; + mousePos *= 2.0f; + mousePos -= 1.0f; + Vec4 mousePosView = {mousePos.x, -mousePos.y, 0.0f, 1.0f}; + Vec4 mousePosCam = GetInstance().Player.ProjectionInverse.Mul(mousePosView); + mousePosCam.x /= mousePosCam.w; + mousePosCam.y /= mousePosCam.w; + mousePosCam.z /= mousePosCam.w; + mousePosCam.w = 1.0f; + Vec4 mousePosWorld = camTransform.M.Mul(mousePosCam); + + auto& t = level.Tests.Get({0}); + t.EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/cube.gltf"); + t.EData.Transform.Position = {mousePosWorld.x, mousePosWorld.y, mousePosWorld.z}; + t.EData.Transform.Scale = {0.01f, 0.01f, 0.01f}; + for (int8_t y = 0; y < Data.HeightTiles / Puzzle::Config::CardSize; ++y) { for (int8_t x = 0; x < Data.WidthTiles / Puzzle::Config::CardSize; ++x) { - Generated::PlacedPuzzleCard& card = Data.PlacedCards[y * Puzzle::Config::MaxPuzzleSizeCards + x]; - auto& tile = level.PuzzleTiles.Get(TileHandles[y * Puzzle::Config::MaxPuzzleSizeCards + x]); - auto& quad = level.UIQuads.Get(UIPlacedCards[y * Puzzle::Config::MaxPuzzleSizeCards + x]); + int32_t cardIdx = y * Puzzle::Config::MaxPuzzleSizeCards + x; + Generated::PlacedPuzzleCard& card = Data.PlacedCards[cardIdx]; + auto& tile = level.PuzzleTiles.Get(TileHandles[cardIdx]); + auto& quad = level.UIQuads.Get(UIPlacedCards[cardIdx]); bool IsValid = Puzzle::IsValid(card.RefCard); - tile.EData.Visible = true; - quad.EData.Visible = IsValid; + tile.EData.Visible = true; tile.EData.ModelH = IsValid ? staticCards[card.RefCard.Idx].ModelHandle : staticCards[0].ModelHandle; - quad.EData.TextureHandle = - IsValid ? staticCards[card.RefCard.Idx].BoardTextureHandle : Generated::TextureHandle{}; + Vec3 cardPos = { (float)card.Position.X * Puzzle::Config::CardScaleWorld, -5.0f, @@ -365,10 +382,45 @@ namespace Game tile.EData.Transform.SetPosition(cardPos); bx::mtxRotateY(tile.EData.Transform.Rotation.M, card.Rotation * bx::kPi * 0.5f); + quad.EData.Visible = IsValid; + quad.EData.TextureHandle = + IsValid ? staticCards[card.RefCard.Idx].BoardTextureHandle : Generated::TextureHandle{}; + quad.EData.Transform = boardTransform; - quad.EData.Transform.TranslateLocal(Vec3{(float)card.Position.X, (float)card.Position.Y, 0.0f} * 0.21f); - quad.EData.Transform.Scale = {0.1f, 0.1f, 0.1f}; + quad.EData.Transform.TranslateLocal(Vec3{(float)card.Position.X, (float)card.Position.Y, 0.0f} * 3.0f); + quad.EData.Transform.Scale = {1.0f, 1.0f, 1.0f}; + // quad.EData.Transform.Scale = {0.1f, 0.1f, 0.1f}; quad.EData.Transform.Rotate(Vec3{bx::kPi * 0.5f, 0.0f, bx::kPi}); + + if (clicked && IsValid && x == 1 && y == 1) + { + quad.EData.Transform.UpdateMatrix(); + // boardTransform.UpdateMatrix(); + Vec4 posInQuad = quad.EData.Transform.MI.Mul(mousePosWorld); + // Vec4 posInQuad = boardTransform.MI.Mul(mousePosWorld); + // if (posInQuad.x >= -1.0f && posInQuad.x <= 1.0f && posInQuad.z >= -1.0f && posInQuad.z <= 1.0f) + { + LOG("---"); + LOG("%.03f %.03f: Click", mousePos.x, mousePos.y); + LOG("%.03f %.03f %.03f %.03f: Cam", mousePosCam.x, mousePosCam.y, mousePosCam.z, mousePosCam.w); + LOG("%.03f %.03f %.03f %.03f: World", + mousePosWorld.x, + mousePosWorld.y, + mousePosWorld.z, + mousePosWorld.w); + LOG("%.03f %.03f %.03f %.03f: Card (%u %u)", + posInQuad.x, + posInQuad.y, + posInQuad.z, + posInQuad.w, + x, + y); + LOG("%.03f %.03f %.03f: Player", + camTransform.Position.x, + camTransform.Position.y, + camTransform.Position.z); + } + } } } } diff --git a/src/game/data/puzzles/0.pzl b/src/game/data/puzzles/0.pzl index f4536c7..3731831 100644 --- a/src/game/data/puzzles/0.pzl +++ b/src/game/data/puzzles/0.pzl @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:73d48d31ff063d48c57887ef7463fa9fdc458200915a6b07451d5f0f0c242a32 +oid sha256:5129bc993aa646404d766c424d8693bafc96457d4c055883c48fdaa830cd5dd5 size 5820 diff --git a/src/game/data/static/puzzle.dat b/src/game/data/static/puzzle.dat index 40483b06825218f18d87e17dcda41d1e789d4e2e..930e59b335e04e4b70e7da3c406774af3270904d 100644 GIT binary patch delta 26 icmbQhH-T@04G$N?gV|P0ObmOXe9SgBOkkVXU;zMYo(O;d delta 26 icmbQhH-T@04G$;7gV|P0j0~UJFPzxeFoA7ig9QM1#tHWT diff --git a/src/models/tablet.glb b/src/models/tablet.glb new file mode 100644 index 0000000..ddbe0e5 --- /dev/null +++ b/src/models/tablet.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:22207711195047f5aaba2545b1f8713f37fee9e2b59f4ae42d4be9da9d7b6b12 +size 7048 diff --git a/src/textures/w straight.ktx b/src/textures/w straight.ktx new file mode 100644 index 0000000..f505d25 --- /dev/null +++ b/src/textures/w straight.ktx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dab0fcd7ecb89dfe1ec2faeb8e1222dace3330c1494463ebf7a8c0dea7d7c894 +size 1048644