diff --git a/private/sdl_gpu_test/7_3d_scene/app.cpp b/private/sdl_gpu_test/7_3d_scene/app.cpp index 403c755..c4bf8ff 100644 --- a/private/sdl_gpu_test/7_3d_scene/app.cpp +++ b/private/sdl_gpu_test/7_3d_scene/app.cpp @@ -1,9 +1,12 @@ #include "./app.hpp" +#include + #include #include #include +#include #include "../scene/mesh.hpp" #include "../sdlpp/keyboard.hpp" @@ -14,8 +17,8 @@ namespace sdl_gpu_test { namespace { -inline constexpr float PITCH_MIN = -89.f; -inline constexpr float PITCH_MAX = 89.f; +inline constexpr float PITCH_MIN = -0.49f * std::numbers::pi_v; +inline constexpr float PITCH_MAX = 0.49f * std::numbers::pi_v; inline constexpr Uint16 AXIS_DEADZONE = 5000; } @@ -101,8 +104,8 @@ void ThreeDSceneApp::update(const AppUpdateArgs& args) mSceneRenderer.render({ .camera = { .position = mPosition, - .pitch = glm::radians(mPitch), - .yaw = glm::radians(mYaw) + .pitch = mPitch, + .yaw = mYaw }, .cmdBuffer = &cmdBuffer, .targetTexture = &swapchainTexture, @@ -141,8 +144,8 @@ void ThreeDSceneApp::handleMouseMotionEvent(const sdlpp::MouseMotionEvent& event { mWidgetTree.notifyMouseMoved(event); - mYaw -= 0.5f * event.xrel; - mPitch -= 0.5f * event.yrel; + mYaw -= 0.002f * event.xrel; + mPitch -= 0.002f * event.yrel; } void ThreeDSceneApp::handleMouseButtonEvent(const sdlpp::MouseButtonEvent& event) @@ -152,30 +155,65 @@ void ThreeDSceneApp::handleMouseButtonEvent(const sdlpp::MouseButtonEvent& event void ThreeDSceneApp::processInput(const AppUpdateArgs& args) { + glm::vec2 movement = {0.f, 0.f}; + const std::span keyboardState = sdlpp::getKeyboardState(); if (mGamepad) { Sint16 axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::RIGHTX); if (std::abs(axisValue) > AXIS_DEADZONE) { - mYaw -= 0.005f * args.tickSeconds * static_cast(axisValue); + mYaw -= 0.0001f * args.tickSeconds * static_cast(axisValue); } axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::RIGHTY); if (std::abs(axisValue) > AXIS_DEADZONE) { - mPitch -= 0.005f * args.tickSeconds * static_cast(axisValue); + mPitch -= 0.0001f * args.tickSeconds * static_cast(axisValue); } axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::LEFTY); if (std::abs(axisValue) > AXIS_DEADZONE) { - mPosition.x += 0.0005f * args.tickSeconds * std::sin(mYaw) * static_cast(axisValue); - mPosition.z += 0.0005f * args.tickSeconds * std::cos(mYaw) * static_cast(axisValue); + movement.y -= static_cast(axisValue) / static_cast(std::numeric_limits::max()); + } + + axisValue = mGamepad.getAxis(sdlpp::GamepadAxis::LEFTX); + if (std::abs(axisValue) > AXIS_DEADZONE) + { + movement.x += static_cast(axisValue) / static_cast(std::numeric_limits::max()); } } - while (mYaw >= 360.f) { mYaw -= 360.f; } - while (mYaw < 0.f) { mYaw += 360.f; } + if (keyboardState[SDL_SCANCODE_W]) + { + movement.y += 1.f; + } + if (keyboardState[SDL_SCANCODE_S]) + { + movement.y -= 1.f; + } + if (keyboardState[SDL_SCANCODE_A]) + { + movement.x -= 1.f; + } + if (keyboardState[SDL_SCANCODE_D]) + { + movement.x += 1.f; + } + + if (glm::length2(movement) > 1.f) + { + movement = glm::normalize(movement); + } + + mPosition.x -= 2.f * args.tickSeconds * std::sin(mYaw) * movement.y; + mPosition.z -= 2.f * args.tickSeconds * std::cos(mYaw) * movement.y; + + mPosition.x += 2.f * args.tickSeconds * std::cos(mYaw) * movement.x; + mPosition.z -= 2.f * args.tickSeconds * std::sin(mYaw) * movement.x; + + while (mYaw >= 2.f * std::numbers::pi) { mYaw -= 2.f * std::numbers::pi_v; } + while (mYaw < 0.f) { mYaw += 2.f * std::numbers::pi_v; } mPitch = std::clamp(mPitch, PITCH_MIN, PITCH_MAX); } } diff --git a/private/sdl_gpu_test/SModule b/private/sdl_gpu_test/SModule index d3d2430..7843eac 100644 --- a/private/sdl_gpu_test/SModule +++ b/private/sdl_gpu_test/SModule @@ -30,7 +30,7 @@ src_files = Split(""" shader_files = env.Glob("#assets/shaders/glsl/*.frag") \ + env.Glob("#assets/shaders/glsl/*.vert") -env.Append(CPPDEFINES = ['GLM_FORCE_DEPTH_ZERO_TO_ONE', 'GLM_FORCE_RADIANS']) +env.Append(CPPDEFINES = ['GLM_FORCE_DEPTH_ZERO_TO_ONE', 'GLM_FORCE_RADIANS', 'GLM_ENABLE_EXPERIMENTAL']) prog_app = env.UnityProgram( target = env['BIN_DIR'] + '/sdl_gpu_test', source = src_files, diff --git a/private/sdl_gpu_test/scene/scene_renderer.cpp b/private/sdl_gpu_test/scene/scene_renderer.cpp index 81cee72..6e28706 100644 --- a/private/sdl_gpu_test/scene/scene_renderer.cpp +++ b/private/sdl_gpu_test/scene/scene_renderer.cpp @@ -79,7 +79,7 @@ void SceneRenderer::render(const SceneRendererRenderArgs& args) VertexShaderParameters vertexShaderParameters = { // note that we are transforming the world, but are passed the camera transform, so invert it .worldToView = glm::translate( - glm::yawPitchRoll(-args.camera.yaw, -args.camera.pitch, -args.camera.roll), + glm::transpose(glm::yawPitchRoll(args.camera.yaw, args.camera.pitch, args.camera.roll)), -args.camera.position ), .viewToClip = glm::perspectiveFov( diff --git a/private/sdl_gpu_test/scene/transform3d.hpp b/private/sdl_gpu_test/scene/transform3d.hpp index 18fc6b3..c0653c7 100644 --- a/private/sdl_gpu_test/scene/transform3d.hpp +++ b/private/sdl_gpu_test/scene/transform3d.hpp @@ -4,13 +4,11 @@ #if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_TRANSFORM3D_HPP_INCLUDED) #define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_TRANSFORM3D_HPP_INCLUDED 1 -#define GLM_ENABLE_EXPERIMENTAL 1 #include #include #include #include #include -#undef GLM_ENABLE_EXPERIMENTAL namespace sdl_gpu_test {