diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..eb8f4c8 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,38 @@ +name: Run Tests + +on: + pull_request: + branches: [ "main" ] + +jobs: + tests: + runs-on: ubuntu-latest + steps: + + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: SDL Deps + run: | + sudo apt-get install libgl1-mesa-dev build-essential git make \ + pkg-config cmake ninja-build gnome-desktop-testing libasound2-dev libpulse-dev \ + libaudio-dev libfribidi-dev libjack-dev libsndio-dev libx11-dev libxext-dev \ + libxrandr-dev libxcursor-dev libxfixes-dev libxi-dev libxss-dev libxtst-dev \ + libxkbcommon-dev libdrm-dev libgbm-dev libgl1-mesa-dev libgles2-mesa-dev \ + libegl1-mesa-dev libdbus-1-dev libibus-1.0-dev libudev-dev libthai-dev \ + libpipewire-0.3-dev libwayland-dev libdecor-0-dev liburing-dev + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. + run: cmake --preset debug + + - name: Build + # Build your program with the given configuration + run: cmake --build --preset tests-debug + + - name: Test + working-directory: ${{github.workspace}}/build/debug + # Execute tests + run: ./tests diff --git a/.gitignore b/.gitignore index 5c5bb4b..e289eb6 100644 --- a/.gitignore +++ b/.gitignore @@ -12,5 +12,6 @@ native Debug Release +ccache .cache # .zed \ No newline at end of file diff --git a/events.cpp b/events.cpp index eada31b..1421e1e 100644 --- a/events.cpp +++ b/events.cpp @@ -13,44 +13,6 @@ using namespace mym; -Vec2 getPointerClickInClipSpace(const int mouse_x, const int mouse_y, const int canvas_width, const int canvas_height) { - // Convert from window coordinates to normalized device coordinates (clip space) - const float x = static_cast(mouse_x) / static_cast(canvas_width) * 2.0f - 1.0f; - const float y = static_cast(mouse_y) / static_cast(canvas_height) * -2.0f + 1.0f; - return { x, y }; -} - -Ray getWorldRayFromClipSpaceAndCamera( - Vec2 clipSpacePoint, - Camera camera) { - - float x = clipSpacePoint.x; - float y = clipSpacePoint.y; - - Vec3 nearPoint = {x, y, -1.f}; - Vec3 farPoint = {x, y, 1}; - - const Mat4 viewMatrix = inverse(camera.transform); - const Mat4 projectionMatrix = getProjectionMatrix(camera); - const Mat4 viewProjInverse = inverse(multiplied(projectionMatrix, viewMatrix)); - - const Vec3 worldNear = positionMultiplied(nearPoint, viewProjInverse); - const Vec3 worldFar = positionMultiplied(farPoint, viewProjInverse); - - auto rayOrigin = worldNear; - - const Vec3 rayDirection = subtractVectors(worldFar, worldNear); - - auto rayDirNorm = normalize(rayDirection); - - Ray worldRay = { - .origin = rayOrigin, - .direction = rayDirNorm - }; - - return worldRay; -} - void processEvents(WindowState& window, Camera& camera, InputState& input, Scene& scene) { ZoneScoped; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 7f2b544..77b4dd0 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -5,8 +5,18 @@ project(NativeRendererLib) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # find_package(OpenGL REQUIRED) +set(SDL_ALSA OFF) +set(SDL_ALSA_SHARED OFF) +set(SDL_RENDER_GPU OFF) +set(SDL_VULKAN OFF) add_subdirectory(../third_party/SDL SDL3) + +set(ASSIMP_BUILD_TESTS OFF) +set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT OFF) +set(ASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT OFF) +set(ASSIMP_BUILD_GLTF_IMPORTER ON) add_subdirectory(../third_party/assimp assimp) + add_subdirectory(../mym mym) # lib (include is for templates) diff --git a/lib/include/raycast.h b/lib/include/raycast.h index 153bd14..9efda59 100644 --- a/lib/include/raycast.h +++ b/lib/include/raycast.h @@ -55,4 +55,8 @@ void sortBySceneDepth( Camera camera ); +Vec2 getPointerClickInClipSpace(const int mouse_x, const int mouse_y, const int canvas_width, const int canvas_height); + +Ray getWorldRayFromClipSpaceAndCamera(Vec2 clipSpacePoint, Camera camera); + #endif \ No newline at end of file diff --git a/lib/raycast.cpp b/lib/raycast.cpp index 9c0a843..0afbd73 100644 --- a/lib/raycast.cpp +++ b/lib/raycast.cpp @@ -7,6 +7,43 @@ #include #include "mystl.hpp" +Vec2 getPointerClickInClipSpace(const int mouse_x, const int mouse_y, const int canvas_width, const int canvas_height) { + // Convert from window coordinates to normalized device coordinates (clip space) + const float x = static_cast(mouse_x) / static_cast(canvas_width) * 2.0f - 1.0f; + const float y = static_cast(mouse_y) / static_cast(canvas_height) * -2.0f + 1.0f; + return { x, y }; +} + +Ray getWorldRayFromClipSpaceAndCamera( + Vec2 clipSpacePoint, + Camera camera) { + + float x = clipSpacePoint.x; + float y = clipSpacePoint.y; + + Vec3 nearPoint = {x, y, -1.f}; + Vec3 farPoint = {x, y, 1}; + + const Mat4 viewMatrix = inverse(camera.transform); + const Mat4 projectionMatrix = getProjectionMatrix(camera); + const Mat4 viewProjInverse = inverse(multiplied(projectionMatrix, viewMatrix)); + + const Vec3 worldNear = positionMultiplied(nearPoint, viewProjInverse); + const Vec3 worldFar = positionMultiplied(farPoint, viewProjInverse); + + auto rayOrigin = worldNear; + + const Vec3 rayDirection = subtractVectors(worldFar, worldNear); + + auto rayDirNorm = normalize(rayDirection); + + Ray worldRay = { + .origin = rayOrigin, + .direction = rayDirNorm + }; + + return worldRay; +} Vec3Result rayIntersectsTriangle(Ray ray, Triangle triangle) { diff --git a/lib/scene.cpp b/lib/scene.cpp index 62fd093..1cc3c44 100644 --- a/lib/scene.cpp +++ b/lib/scene.cpp @@ -1,8 +1,5 @@ #include "scene.h" #include "mat4.h" -#include "camera.h" - - size_t sceneNodeCounter = 0;