mirror of
https://github.com/orange-cpp/omath.git
synced 2026-08-07 21:42:05 +00:00
Auto stash before checking out "origin/main"
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua_engines_detail.hpp"
|
||||
#include <omath/engines/cry_engine/camera.hpp>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
void register_cry_engine(sol::table& omath_table)
|
||||
{
|
||||
register_engine<omath::cry_engine::Camera, omath::cry_engine::PitchAngle, omath::cry_engine::ViewAngles>(
|
||||
omath_table, "cry");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua_engines_detail.hpp"
|
||||
#include <omath/engines/frostbite_engine/camera.hpp>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
void register_frostbite_engine(sol::table& omath_table)
|
||||
{
|
||||
register_engine<omath::frostbite_engine::Camera, omath::frostbite_engine::PitchAngle,
|
||||
omath::frostbite_engine::ViewAngles>(omath_table, "frostbite");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua_engines_detail.hpp"
|
||||
#include <omath/engines/iw_engine/camera.hpp>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
void register_iw_engine(sol::table& omath_table)
|
||||
{
|
||||
register_engine<omath::iw_engine::Camera, omath::iw_engine::PitchAngle, omath::iw_engine::ViewAngles>(
|
||||
omath_table, "iw");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua_engines_detail.hpp"
|
||||
#include <omath/engines/opengl_engine/camera.hpp>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
void register_opengl_engine(sol::table& omath_table)
|
||||
{
|
||||
register_engine<omath::opengl_engine::Camera, omath::opengl_engine::PitchAngle,
|
||||
omath::opengl_engine::ViewAngles>(omath_table, "opengl");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua_engines_detail.hpp"
|
||||
#include <omath/engines/rage_engine/camera.hpp>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
void register_rage_engine(sol::table& omath_table)
|
||||
{
|
||||
register_engine<omath::rage_engine::Camera, omath::rage_engine::PitchAngle, omath::rage_engine::ViewAngles>(
|
||||
omath_table, "rage");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua_engines_detail.hpp"
|
||||
#include <omath/engines/source_engine/camera.hpp>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
void register_source_engine(sol::table& omath_table)
|
||||
{
|
||||
register_engine<omath::source_engine::Camera, omath::source_engine::PitchAngle,
|
||||
omath::source_engine::ViewAngles>(omath_table, "source");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua_engines_detail.hpp"
|
||||
#include <omath/engines/unity_engine/camera.hpp>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
void register_unity_engine(sol::table& omath_table)
|
||||
{
|
||||
register_engine<omath::unity_engine::Camera, omath::unity_engine::PitchAngle,
|
||||
omath::unity_engine::ViewAngles>(omath_table, "unity");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "lua_engines_detail.hpp"
|
||||
#include <omath/engines/unreal_engine/camera.hpp>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
void register_unreal_engine(sol::table& omath_table)
|
||||
{
|
||||
register_engine<omath::unreal_engine::Camera, omath::unreal_engine::PitchAngle,
|
||||
omath::unreal_engine::ViewAngles, double>(omath_table, "unreal");
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
#endif
|
||||
+48
-242
@@ -2,269 +2,75 @@
|
||||
// Created by orange on 07.03.2026.
|
||||
//
|
||||
#ifdef OMATH_ENABLE_LUA
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include "omath/omath.hpp"
|
||||
#include "omath/projection/error_codes.hpp"
|
||||
#include <omath/engines/cry_engine/camera.hpp>
|
||||
#include <omath/engines/frostbite_engine/camera.hpp>
|
||||
#include <omath/engines/iw_engine/camera.hpp>
|
||||
#include <omath/engines/opengl_engine/camera.hpp>
|
||||
#include <omath/engines/rage_engine/camera.hpp>
|
||||
#include <omath/engines/source_engine/camera.hpp>
|
||||
#include <omath/engines/unity_engine/camera.hpp>
|
||||
#include <omath/engines/unreal_engine/camera.hpp>
|
||||
#include <sol/sol.hpp>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace
|
||||
{
|
||||
// ---- Canonical shared C++ type aliases ----------------------------------
|
||||
// Each unique template instantiation must be registered exactly once.
|
||||
|
||||
using PitchAngle90 = omath::Angle<float, -90.f, 90.f, omath::AngleFlags::Clamped>;
|
||||
using PitchAngle89 = omath::Angle<float, -89.f, 89.f, omath::AngleFlags::Clamped>;
|
||||
using SharedYawRoll = omath::Angle<float, -180.f, 180.f, omath::AngleFlags::Normalized>;
|
||||
using SharedFoV = omath::Angle<float, 0.f, 180.f, omath::AngleFlags::Clamped>;
|
||||
using ViewAngles90 = omath::ViewAngles<PitchAngle90, SharedYawRoll, SharedYawRoll>;
|
||||
using ViewAngles89 = omath::ViewAngles<PitchAngle89, SharedYawRoll, SharedYawRoll>;
|
||||
|
||||
std::string projection_error_to_string(omath::projection::Error e)
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case omath::projection::Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS:
|
||||
return "world position is out of screen bounds";
|
||||
case omath::projection::Error::INV_VIEW_PROJ_MAT_DET_EQ_ZERO:
|
||||
return "inverse view-projection matrix determinant is zero";
|
||||
case omath::projection::Error::PERSPECTIVE_DIVIDER_LESS_EQ_ZERO:
|
||||
return "perspective divider is less or equal to zero";
|
||||
}
|
||||
return "unknown error";
|
||||
}
|
||||
|
||||
template<class AngleType>
|
||||
void register_angle(sol::table& table, const char* name)
|
||||
{
|
||||
table.new_usertype<AngleType>(
|
||||
name, sol::no_constructor, "from_degrees", &AngleType::from_degrees, "from_radians",
|
||||
&AngleType::from_radians, "as_degrees", &AngleType::as_degrees, "as_radians", &AngleType::as_radians,
|
||||
"sin", &AngleType::sin, "cos", &AngleType::cos, "tan", &AngleType::tan, "cot", &AngleType::cot,
|
||||
sol::meta_function::addition, [](const AngleType& a, const AngleType& b)
|
||||
{ return AngleType::from_degrees(a.as_degrees() + b.as_degrees()); }, sol::meta_function::subtraction,
|
||||
[](const AngleType& a, const AngleType& b)
|
||||
{ return AngleType::from_degrees(a.as_degrees() - b.as_degrees()); }, sol::meta_function::unary_minus,
|
||||
[](const AngleType& a) { return AngleType::from_degrees(-a.as_degrees()); },
|
||||
sol::meta_function::equal_to, [](const AngleType& a, const AngleType& b) { return a == b; },
|
||||
sol::meta_function::to_string, [](const AngleType& a) { return std::format("{}deg", a.as_degrees()); });
|
||||
}
|
||||
|
||||
// Set aliases in an engine subtable pointing to the already-registered shared types
|
||||
template<class PitchAngleType, class ViewAnglesType>
|
||||
void set_engine_aliases(sol::table& engine_table, sol::table& types)
|
||||
{
|
||||
if constexpr (std::is_same_v<PitchAngleType, PitchAngle90>)
|
||||
engine_table["PitchAngle"] = types["PitchAngle90"];
|
||||
else
|
||||
engine_table["PitchAngle"] = types["PitchAngle89"];
|
||||
|
||||
engine_table["YawAngle"] = types["YawRoll"];
|
||||
engine_table["RollAngle"] = types["YawRoll"];
|
||||
engine_table["FieldOfView"] = types["FieldOfView"];
|
||||
engine_table["ViewPort"] = types["ViewPort"];
|
||||
|
||||
if constexpr (std::is_same_v<ViewAnglesType, ViewAngles90>)
|
||||
engine_table["ViewAngles"] = types["ViewAngles90"];
|
||||
else
|
||||
engine_table["ViewAngles"] = types["ViewAngles89"];
|
||||
}
|
||||
|
||||
// Register an engine: alias shared types, register unique Camera
|
||||
template<class EngineTraits, class ArithmeticType = float>
|
||||
requires std::is_arithmetic_v<ArithmeticType>
|
||||
void register_engine(sol::table& omath_table, const char* subtable_name)
|
||||
{
|
||||
using PitchAngle = typename EngineTraits::PitchAngle;
|
||||
using ViewAngles = typename EngineTraits::ViewAngles;
|
||||
using Camera = typename EngineTraits::Camera;
|
||||
using Mat4X4 = std::remove_cvref_t<decltype(std::declval<const Camera&>().get_view_matrix())>;
|
||||
|
||||
auto engine_table = omath_table[subtable_name].get_or_create<sol::table>();
|
||||
auto types = omath_table["_types"].get<sol::table>();
|
||||
|
||||
set_engine_aliases<PitchAngle, ViewAngles>(engine_table, types);
|
||||
|
||||
auto camera_type = engine_table.new_usertype<Camera>(
|
||||
"Camera",
|
||||
sol::constructors<Camera(const omath::Vector3<ArithmeticType>&, const ViewAngles&,
|
||||
const omath::projection::ViewPort&, const omath::projection::FieldOfView&,
|
||||
ArithmeticType, ArithmeticType)>());
|
||||
|
||||
camera_type["look_at"] = &Camera::look_at;
|
||||
camera_type["get_forward"] = &Camera::get_forward;
|
||||
camera_type["get_right"] = &Camera::get_right;
|
||||
camera_type["get_up"] = &Camera::get_up;
|
||||
camera_type["get_origin"] = &Camera::get_origin;
|
||||
camera_type["get_view_angles"] = &Camera::get_view_angles;
|
||||
camera_type["get_near_plane"] = &Camera::get_near_plane;
|
||||
camera_type["get_far_plane"] = &Camera::get_far_plane;
|
||||
camera_type["get_field_of_view"] = &Camera::get_field_of_view;
|
||||
camera_type["set_origin"] = &Camera::set_origin;
|
||||
camera_type["set_view_angles"] = &Camera::set_view_angles;
|
||||
camera_type["set_view_port"] = &Camera::set_view_port;
|
||||
camera_type["set_field_of_view"] = &Camera::set_field_of_view;
|
||||
camera_type["set_near_plane"] = &Camera::set_near_plane;
|
||||
camera_type["set_far_plane"] = &Camera::set_far_plane;
|
||||
|
||||
camera_type["get_view_matrix"] = [](const Camera& cam) -> Mat4X4
|
||||
{
|
||||
return cam.get_view_matrix();
|
||||
};
|
||||
camera_type["get_projection_matrix"] = [](const Camera& cam) -> Mat4X4
|
||||
{
|
||||
return cam.get_projection_matrix();
|
||||
};
|
||||
camera_type["get_view_projection_matrix"] = [](const Camera& cam) -> Mat4X4
|
||||
{
|
||||
return cam.get_view_projection_matrix();
|
||||
};
|
||||
camera_type["extract_projection_params"] = [](const Mat4X4& projection_matrix)
|
||||
{
|
||||
const auto params = Camera::extract_projection_params(projection_matrix);
|
||||
return std::make_tuple(params.fov, params.aspect_ratio);
|
||||
};
|
||||
camera_type["calc_view_angles_from_view_matrix"] = &Camera::calc_view_angles_from_view_matrix;
|
||||
camera_type["calc_origin_from_view_matrix"] = &Camera::calc_origin_from_view_matrix;
|
||||
|
||||
camera_type["world_to_screen"] = [](const Camera& cam, const omath::Vector3<ArithmeticType>& pos)
|
||||
-> std::tuple<sol::optional<omath::Vector3<ArithmeticType>>, sol::optional<std::string>>
|
||||
{
|
||||
auto result = cam.world_to_screen(pos);
|
||||
if (result)
|
||||
return {*result, sol::nullopt};
|
||||
return {sol::nullopt, projection_error_to_string(result.error())};
|
||||
};
|
||||
|
||||
camera_type["screen_to_world"] = [](const Camera& cam, const omath::Vector3<ArithmeticType>& pos)
|
||||
-> std::tuple<sol::optional<omath::Vector3<ArithmeticType>>, sol::optional<std::string>>
|
||||
{
|
||||
auto result = cam.screen_to_world(pos);
|
||||
if (result)
|
||||
return {*result, sol::nullopt};
|
||||
return {sol::nullopt, projection_error_to_string(result.error())};
|
||||
};
|
||||
}
|
||||
|
||||
// ---- Engine trait structs -----------------------------------------------
|
||||
|
||||
struct OpenGLEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::opengl_engine::PitchAngle;
|
||||
using ViewAngles = omath::opengl_engine::ViewAngles;
|
||||
using Camera = omath::opengl_engine::Camera;
|
||||
};
|
||||
struct FrostbiteEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::frostbite_engine::PitchAngle;
|
||||
using ViewAngles = omath::frostbite_engine::ViewAngles;
|
||||
using Camera = omath::frostbite_engine::Camera;
|
||||
};
|
||||
struct IWEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::iw_engine::PitchAngle;
|
||||
using ViewAngles = omath::iw_engine::ViewAngles;
|
||||
using Camera = omath::iw_engine::Camera;
|
||||
};
|
||||
struct SourceEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::source_engine::PitchAngle;
|
||||
using ViewAngles = omath::source_engine::ViewAngles;
|
||||
using Camera = omath::source_engine::Camera;
|
||||
};
|
||||
struct RageEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::rage_engine::PitchAngle;
|
||||
using ViewAngles = omath::rage_engine::ViewAngles;
|
||||
using Camera = omath::rage_engine::Camera;
|
||||
};
|
||||
struct UnityEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::unity_engine::PitchAngle;
|
||||
using ViewAngles = omath::unity_engine::ViewAngles;
|
||||
using Camera = omath::unity_engine::Camera;
|
||||
};
|
||||
struct UnrealEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::unreal_engine::PitchAngle;
|
||||
using ViewAngles = omath::unreal_engine::ViewAngles;
|
||||
using Camera = omath::unreal_engine::Camera;
|
||||
};
|
||||
struct CryEngineTraits
|
||||
{
|
||||
using PitchAngle = omath::cry_engine::PitchAngle;
|
||||
using ViewAngles = omath::cry_engine::ViewAngles;
|
||||
using Camera = omath::cry_engine::Camera;
|
||||
};
|
||||
} // namespace
|
||||
#include "lua_engines_detail.hpp"
|
||||
|
||||
namespace omath::lua
|
||||
{
|
||||
void LuaInterpreter::register_shared_types(sol::table& omath_table)
|
||||
{
|
||||
auto t = omath_table["_types"].get_or_create<sol::table>();
|
||||
auto types = omath_table["_types"].get_or_create<sol::table>();
|
||||
|
||||
register_angle<PitchAngle90>(t, "PitchAngle90");
|
||||
register_angle<PitchAngle89>(t, "PitchAngle89");
|
||||
register_angle<SharedYawRoll>(t, "YawRoll");
|
||||
register_angle<SharedFoV>(t, "FieldOfView");
|
||||
detail::register_angle<detail::PitchAngle90>(types, "PitchAngle90");
|
||||
detail::register_angle<detail::PitchAngle89>(types, "PitchAngle89");
|
||||
detail::register_angle<detail::SharedYawRoll>(types, "YawRoll");
|
||||
detail::register_angle<detail::SharedFoV>(types, "FieldOfView");
|
||||
|
||||
t.new_usertype<omath::projection::ViewPort>(
|
||||
"ViewPort", sol::factories([](float w, float h) { return omath::projection::ViewPort{w, h}; }), "width",
|
||||
sol::property([](const omath::projection::ViewPort& vp) { return vp.m_width; },
|
||||
[](omath::projection::ViewPort& vp, float val) { vp.m_width = val; }),
|
||||
types.new_usertype<projection::ViewPort>(
|
||||
"ViewPort",
|
||||
sol::factories([](float width, float height) { return projection::ViewPort{width, height}; }),
|
||||
"width",
|
||||
sol::property([](const projection::ViewPort& view_port) { return view_port.m_width; },
|
||||
[](projection::ViewPort& view_port, float value) { view_port.m_width = value; }),
|
||||
"height",
|
||||
sol::property([](const omath::projection::ViewPort& vp) { return vp.m_height; },
|
||||
[](omath::projection::ViewPort& vp, float val) { vp.m_height = val; }),
|
||||
"aspect_ratio", &omath::projection::ViewPort::aspect_ratio);
|
||||
sol::property([](const projection::ViewPort& view_port) { return view_port.m_height; },
|
||||
[](projection::ViewPort& view_port, float value) { view_port.m_height = value; }),
|
||||
"aspect_ratio", &projection::ViewPort::aspect_ratio);
|
||||
|
||||
t.new_usertype<ViewAngles90>(
|
||||
types.new_usertype<detail::ViewAngles90>(
|
||||
"ViewAngles90",
|
||||
sol::factories([](PitchAngle90 p, SharedYawRoll y, SharedYawRoll r) { return ViewAngles90{p, y, r}; }),
|
||||
sol::factories([](detail::PitchAngle90 pitch, detail::SharedYawRoll yaw, detail::SharedYawRoll roll)
|
||||
{ return detail::ViewAngles90{pitch, yaw, roll}; }),
|
||||
"pitch",
|
||||
sol::property([](const ViewAngles90& va) { return va.pitch; },
|
||||
[](ViewAngles90& va, const PitchAngle90& val) { va.pitch = val; }),
|
||||
sol::property([](const detail::ViewAngles90& view_angles) { return view_angles.pitch; },
|
||||
[](detail::ViewAngles90& view_angles, const detail::PitchAngle90& value)
|
||||
{ view_angles.pitch = value; }),
|
||||
"yaw",
|
||||
sol::property([](const ViewAngles90& va) { return va.yaw; },
|
||||
[](ViewAngles90& va, const SharedYawRoll& val) { va.yaw = val; }),
|
||||
sol::property([](const detail::ViewAngles90& view_angles) { return view_angles.yaw; },
|
||||
[](detail::ViewAngles90& view_angles, const detail::SharedYawRoll& value)
|
||||
{ view_angles.yaw = value; }),
|
||||
"roll",
|
||||
sol::property([](const ViewAngles90& va) { return va.roll; },
|
||||
[](ViewAngles90& va, const SharedYawRoll& val) { va.roll = val; }));
|
||||
sol::property([](const detail::ViewAngles90& view_angles) { return view_angles.roll; },
|
||||
[](detail::ViewAngles90& view_angles, const detail::SharedYawRoll& value)
|
||||
{ view_angles.roll = value; }));
|
||||
|
||||
t.new_usertype<ViewAngles89>(
|
||||
types.new_usertype<detail::ViewAngles89>(
|
||||
"ViewAngles89",
|
||||
sol::factories([](PitchAngle89 p, SharedYawRoll y, SharedYawRoll r) { return ViewAngles89{p, y, r}; }),
|
||||
sol::factories([](detail::PitchAngle89 pitch, detail::SharedYawRoll yaw, detail::SharedYawRoll roll)
|
||||
{ return detail::ViewAngles89{pitch, yaw, roll}; }),
|
||||
"pitch",
|
||||
sol::property([](const ViewAngles89& va) { return va.pitch; },
|
||||
[](ViewAngles89& va, const PitchAngle89& val) { va.pitch = val; }),
|
||||
sol::property([](const detail::ViewAngles89& view_angles) { return view_angles.pitch; },
|
||||
[](detail::ViewAngles89& view_angles, const detail::PitchAngle89& value)
|
||||
{ view_angles.pitch = value; }),
|
||||
"yaw",
|
||||
sol::property([](const ViewAngles89& va) { return va.yaw; },
|
||||
[](ViewAngles89& va, const SharedYawRoll& val) { va.yaw = val; }),
|
||||
sol::property([](const detail::ViewAngles89& view_angles) { return view_angles.yaw; },
|
||||
[](detail::ViewAngles89& view_angles, const detail::SharedYawRoll& value)
|
||||
{ view_angles.yaw = value; }),
|
||||
"roll",
|
||||
sol::property([](const ViewAngles89& va) { return va.roll; },
|
||||
[](ViewAngles89& va, const SharedYawRoll& val) { va.roll = val; }));
|
||||
sol::property([](const detail::ViewAngles89& view_angles) { return view_angles.roll; },
|
||||
[](detail::ViewAngles89& view_angles, const detail::SharedYawRoll& value)
|
||||
{ view_angles.roll = value; }));
|
||||
}
|
||||
|
||||
void LuaInterpreter::register_engines(sol::table& omath_table)
|
||||
{
|
||||
register_engine<OpenGLEngineTraits>(omath_table, "opengl");
|
||||
register_engine<FrostbiteEngineTraits>(omath_table, "frostbite");
|
||||
register_engine<IWEngineTraits>(omath_table, "iw");
|
||||
register_engine<SourceEngineTraits>(omath_table, "source");
|
||||
register_engine<RageEngineTraits>(omath_table, "rage");
|
||||
register_engine<UnityEngineTraits>(omath_table, "unity");
|
||||
register_engine<UnrealEngineTraits, double>(omath_table, "unreal");
|
||||
register_engine<CryEngineTraits>(omath_table, "cry");
|
||||
detail::register_opengl_engine(omath_table);
|
||||
detail::register_frostbite_engine(omath_table);
|
||||
detail::register_iw_engine(omath_table);
|
||||
detail::register_source_engine(omath_table);
|
||||
detail::register_rage_engine(omath_table);
|
||||
detail::register_unity_engine(omath_table);
|
||||
detail::register_unreal_engine(omath_table);
|
||||
detail::register_cry_engine(omath_table);
|
||||
}
|
||||
} // namespace omath::lua::detail
|
||||
} // namespace omath::lua
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
#pragma once
|
||||
|
||||
#include "omath/lua/lua.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
#include "omath/projection/error_codes.hpp"
|
||||
#include "omath/trigonometry/view_angles.hpp"
|
||||
#include <sol/sol.hpp>
|
||||
#include <format>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace omath::lua::detail
|
||||
{
|
||||
using PitchAngle90 = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
|
||||
using PitchAngle89 = Angle<float, -89.f, 89.f, AngleFlags::Clamped>;
|
||||
using SharedYawRoll = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
using SharedFoV = Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
|
||||
using ViewAngles90 = ViewAngles<PitchAngle90, SharedYawRoll, SharedYawRoll>;
|
||||
using ViewAngles89 = ViewAngles<PitchAngle89, SharedYawRoll, SharedYawRoll>;
|
||||
|
||||
inline std::string projection_error_to_string(projection::Error error)
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
case projection::Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS:
|
||||
return "world position is out of screen bounds";
|
||||
case projection::Error::INV_VIEW_PROJ_MAT_DET_EQ_ZERO:
|
||||
return "inverse view-projection matrix determinant is zero";
|
||||
case projection::Error::PERSPECTIVE_DIVIDER_LESS_EQ_ZERO:
|
||||
return "perspective divider is less or equal to zero";
|
||||
}
|
||||
return "unknown error";
|
||||
}
|
||||
|
||||
template<class AngleType>
|
||||
void register_angle(sol::table& table, const char* name)
|
||||
{
|
||||
table.new_usertype<AngleType>(
|
||||
name, sol::no_constructor, "from_degrees", &AngleType::from_degrees, "from_radians",
|
||||
&AngleType::from_radians, "as_degrees", &AngleType::as_degrees, "as_radians", &AngleType::as_radians,
|
||||
"sin", &AngleType::sin, "cos", &AngleType::cos, "tan", &AngleType::tan, "cot", &AngleType::cot,
|
||||
sol::meta_function::addition, [](const AngleType& a, const AngleType& b)
|
||||
{ return AngleType::from_degrees(a.as_degrees() + b.as_degrees()); }, sol::meta_function::subtraction,
|
||||
[](const AngleType& a, const AngleType& b)
|
||||
{ return AngleType::from_degrees(a.as_degrees() - b.as_degrees()); }, sol::meta_function::unary_minus,
|
||||
[](const AngleType& a) { return AngleType::from_degrees(-a.as_degrees()); },
|
||||
sol::meta_function::equal_to, [](const AngleType& a, const AngleType& b) { return a == b; },
|
||||
sol::meta_function::to_string, [](const AngleType& a) { return std::format("{}deg", a.as_degrees()); });
|
||||
}
|
||||
|
||||
template<class PitchAngleType, class ViewAnglesType>
|
||||
void set_engine_aliases(sol::table& engine_table, sol::table& types)
|
||||
{
|
||||
if constexpr (std::is_same_v<PitchAngleType, PitchAngle90>)
|
||||
engine_table["PitchAngle"] = types["PitchAngle90"];
|
||||
else
|
||||
engine_table["PitchAngle"] = types["PitchAngle89"];
|
||||
|
||||
engine_table["YawAngle"] = types["YawRoll"];
|
||||
engine_table["RollAngle"] = types["YawRoll"];
|
||||
engine_table["FieldOfView"] = types["FieldOfView"];
|
||||
engine_table["ViewPort"] = types["ViewPort"];
|
||||
|
||||
if constexpr (std::is_same_v<ViewAnglesType, ViewAngles90>)
|
||||
engine_table["ViewAngles"] = types["ViewAngles90"];
|
||||
else
|
||||
engine_table["ViewAngles"] = types["ViewAngles89"];
|
||||
}
|
||||
|
||||
template<class Camera, class PitchAngleType, class ViewAnglesType, class ArithmeticType = float>
|
||||
requires std::is_arithmetic_v<ArithmeticType>
|
||||
void register_engine(sol::table& omath_table, const char* subtable_name)
|
||||
{
|
||||
using Mat4X4 = std::remove_cvref_t<decltype(std::declval<const Camera&>().get_view_matrix())>;
|
||||
|
||||
auto engine_table = omath_table[subtable_name].get_or_create<sol::table>();
|
||||
auto types = omath_table["_types"].get<sol::table>();
|
||||
|
||||
set_engine_aliases<PitchAngleType, ViewAnglesType>(engine_table, types);
|
||||
|
||||
auto camera_type = engine_table.new_usertype<Camera>(
|
||||
"Camera",
|
||||
sol::constructors<Camera(const Vector3<ArithmeticType>&, const ViewAnglesType&,
|
||||
const projection::ViewPort&, const projection::FieldOfView&, ArithmeticType,
|
||||
ArithmeticType)>());
|
||||
|
||||
camera_type["look_at"] = &Camera::look_at;
|
||||
camera_type["get_forward"] = &Camera::get_forward;
|
||||
camera_type["get_right"] = &Camera::get_right;
|
||||
camera_type["get_up"] = &Camera::get_up;
|
||||
camera_type["get_origin"] = &Camera::get_origin;
|
||||
camera_type["get_view_angles"] = &Camera::get_view_angles;
|
||||
camera_type["get_near_plane"] = &Camera::get_near_plane;
|
||||
camera_type["get_far_plane"] = &Camera::get_far_plane;
|
||||
camera_type["get_field_of_view"] = &Camera::get_field_of_view;
|
||||
camera_type["set_origin"] = &Camera::set_origin;
|
||||
camera_type["set_view_angles"] = &Camera::set_view_angles;
|
||||
camera_type["set_view_port"] = &Camera::set_view_port;
|
||||
camera_type["set_field_of_view"] = &Camera::set_field_of_view;
|
||||
camera_type["set_near_plane"] = &Camera::set_near_plane;
|
||||
camera_type["set_far_plane"] = &Camera::set_far_plane;
|
||||
camera_type["get_view_matrix"] = [](const Camera& camera) -> Mat4X4 { return camera.get_view_matrix(); };
|
||||
camera_type["get_projection_matrix"] = [](const Camera& camera) -> Mat4X4
|
||||
{ return camera.get_projection_matrix(); };
|
||||
camera_type["get_view_projection_matrix"] = [](const Camera& camera) -> Mat4X4
|
||||
{ return camera.get_view_projection_matrix(); };
|
||||
camera_type["extract_projection_params"] = [](const Mat4X4& projection_matrix)
|
||||
{
|
||||
const auto params = Camera::extract_projection_params(projection_matrix);
|
||||
return std::make_tuple(params.fov, params.aspect_ratio);
|
||||
};
|
||||
camera_type["calc_view_angles_from_view_matrix"] = &Camera::calc_view_angles_from_view_matrix;
|
||||
camera_type["calc_origin_from_view_matrix"] = &Camera::calc_origin_from_view_matrix;
|
||||
camera_type["world_to_screen"] = [](const Camera& camera, const Vector3<ArithmeticType>& position)
|
||||
-> std::tuple<sol::optional<Vector3<ArithmeticType>>, sol::optional<std::string>>
|
||||
{
|
||||
auto result = camera.world_to_screen(position);
|
||||
if (result)
|
||||
return {*result, sol::nullopt};
|
||||
return {sol::nullopt, projection_error_to_string(result.error())};
|
||||
};
|
||||
camera_type["screen_to_world"] = [](const Camera& camera, const Vector3<ArithmeticType>& position)
|
||||
-> std::tuple<sol::optional<Vector3<ArithmeticType>>, sol::optional<std::string>>
|
||||
{
|
||||
auto result = camera.screen_to_world(position);
|
||||
if (result)
|
||||
return {*result, sol::nullopt};
|
||||
return {sol::nullopt, projection_error_to_string(result.error())};
|
||||
};
|
||||
}
|
||||
|
||||
void register_opengl_engine(sol::table& omath_table);
|
||||
void register_frostbite_engine(sol::table& omath_table);
|
||||
void register_iw_engine(sol::table& omath_table);
|
||||
void register_source_engine(sol::table& omath_table);
|
||||
void register_rage_engine(sol::table& omath_table);
|
||||
void register_unity_engine(sol::table& omath_table);
|
||||
void register_unreal_engine(sol::table& omath_table);
|
||||
void register_cry_engine(sol::table& omath_table);
|
||||
} // namespace omath::lua::detail
|
||||
Reference in New Issue
Block a user