2024-05-01 03:27:30 -04:00
|
|
|
#pragma once
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_GRAPHICS_RAYCAST_H
|
|
|
|
#define BLT_GRAPHICS_RAYCAST_H
|
|
|
|
|
|
|
|
#include <blt/math/vectors.h>
|
|
|
|
#include <blt/math/matrix.h>
|
2024-05-01 20:56:47 -04:00
|
|
|
#include <type_traits>
|
2024-05-01 03:27:30 -04:00
|
|
|
|
|
|
|
namespace blt::gfx
|
|
|
|
{
|
2024-05-01 20:56:47 -04:00
|
|
|
template<typename... Args>
|
|
|
|
inline const bool not_float_v = (std::is_arithmetic_v<Args> && ...) && (!std::is_same_v<float, Args> && ...);
|
|
|
|
|
2024-05-01 03:27:30 -04:00
|
|
|
blt::vec3 calculateRay3D(float mx, float my, float width, float height, const blt::mat4x4& view, const blt::mat4x4& proj);
|
2024-05-01 20:56:47 -04:00
|
|
|
|
2024-05-01 03:27:30 -04:00
|
|
|
blt::vec3 calculateRay3D(float width, float height, const blt::mat4x4& view, const blt::mat4x4& proj);
|
2024-05-01 20:56:47 -04:00
|
|
|
|
2024-05-01 12:46:26 -04:00
|
|
|
blt::vec3 calculateRay2D(float mx, float my, float width, float height, const blt::vec3 scale, const blt::mat4x4& view, const blt::mat4x4& proj);
|
2024-05-01 20:56:47 -04:00
|
|
|
|
2024-05-01 12:46:26 -04:00
|
|
|
blt::vec3 calculateRay2D(float width, float height, const blt::vec3 scale, const blt::mat4x4& view, const blt::mat4x4& proj);
|
2024-05-01 03:27:30 -04:00
|
|
|
|
2024-05-01 20:56:47 -04:00
|
|
|
template<typename T, typename G, std::enable_if_t<not_float_v<T, G>, bool> = true>
|
|
|
|
inline blt::vec3 calculateRay2D(T width, G height, const blt::vec3& scale, const blt::mat4x4& view, const blt::mat4x4& proj)
|
|
|
|
{
|
|
|
|
return calculateRay2D(static_cast<float>(width), static_cast<float>(height), scale, view, proj);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename G, typename V, typename N, std::enable_if_t<not_float_v<T, G, V, N>, bool> = true>
|
|
|
|
inline blt::vec3 calculateRay2D(T mx, G my, V width, N height, const blt::vec3& scale, const blt::mat4x4& view, const blt::mat4x4& proj)
|
|
|
|
{
|
|
|
|
return calculateRay2D(static_cast<float>(mx), static_cast<float>(my),
|
|
|
|
static_cast<float>(width), static_cast<float>(height), scale, view, proj);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename G, std::enable_if_t<not_float_v<T, G>, bool> = true>
|
|
|
|
inline blt::vec3 calculateRay3D(T width, G height, const blt::mat4x4& view, const blt::mat4x4& proj)
|
|
|
|
{
|
|
|
|
return calculateRay3D(static_cast<float>(width), static_cast<float>(height), view, proj);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename G, typename V, typename N, std::enable_if_t<not_float_v<T, G, V, N>, bool> = true>
|
|
|
|
inline blt::vec3 calculateRay3D(T mx, G my, V width, N height, const blt::mat4x4& view, const blt::mat4x4& proj)
|
|
|
|
{
|
|
|
|
return calculateRay3D(static_cast<float>(mx), static_cast<float>(my), static_cast<float>(width), static_cast<float>(height), view, proj);
|
|
|
|
}
|
|
|
|
|
2024-05-01 03:27:30 -04:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
blt::vec3 toWorldCoords(const blt::vec4& eyeCoords, const blt::mat4x4& view);
|
|
|
|
|
|
|
|
blt::vec4 toEyeCoords(const blt::vec4& clipCoords, const blt::mat4x4& proj);
|
|
|
|
|
|
|
|
inline blt::vec2 getNormalisedDeviceCoordinates(float mx, float my, float width, float height)
|
|
|
|
{
|
|
|
|
float x = (2.0f * mx) / width - 1.0f;
|
|
|
|
float y = (2.0f * my) / height - 1.0f;
|
|
|
|
return blt::vec2(x, y);
|
|
|
|
}
|
|
|
|
}
|
2024-05-01 20:56:47 -04:00
|
|
|
|
2024-05-01 03:27:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_GRAPHICS_RAYCAST_H
|