Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
OpenXrHelper.h
1#pragma once
2
3#include <glm/gtx/quaternion.hpp>
4#include <openxr/openxr.h>
5#include <stdexcept>
6#include <string>
7
8namespace Engine
9{
14 {
15 public:
19 typedef struct posef
20 {
21 glm::quat orientation{};
22 glm::vec3 position{};
24
36 template <typename T>
37 static void
38 LoadXrFunction( XrInstance xrInstance, const std::string & functionName, T & functionPointer )
39 {
40 XrResult result = xrGetInstanceProcAddr(
41 xrInstance, functionName.c_str(), reinterpret_cast<PFN_xrVoidFunction *>( &functionPointer )
42 );
43 if ( result != XR_SUCCESS )
44 {
45 functionPointer = nullptr;
46 throw std::runtime_error(
47 "Failed to load " + functionName + " with error code: " + std::to_string( result )
48 );
49 }
50
51 if ( !functionPointer )
52 {
53 functionPointer = nullptr;
54 throw std::runtime_error( "Failed to load " + functionName + ". The function was a nullptr" );
55 }
56 }
57
66 static XrPosef makeIdentityPose();
67
78 static glm::mat4 poseToMatrix( const XrPosef & pose );
79
87 static glm::mat4 poseToMatrix( const OpenXrHelper::posef & pose );
88
89 // janhsimons math functions (found undocumented)
90 // Adjustments to naming have been made. Like changing all variables from being named src to
91 // something more descriptive.
92
103 static glm::quat xrQuaternionfToGlmQuat( const XrQuaternionf & xrQuaternion );
104
112 static XrQuaternionf glmQuatToXrQuaternionf( const glm::quat & glmQuaternion );
113
124 static glm::vec3 xrVector3fToGlmVec3( const XrVector3f & xrVector3 );
125
136 static XrVector3f glmVec3ToXrVector3f( const glm::vec3 & glmVector3 );
137
149 static float dot( const glm::quat & a, const glm::quat & b );
150
163 static glm::quat lerpMix( const glm::quat & from, const glm::quat & to, float progress );
164
177 static glm::vec3 lerpMix( const glm::vec3 & from, const glm::vec3 & to, float progress );
178
193 static float lerpMix( float from, float to, float progress );
194
206 static float dot( const glm::vec3 & a, const glm::vec3 & b );
213 static glm::vec3 crossProductVector3( const glm::vec3 & vector_a, const glm::vec3 & vector_b );
214
222 static float clampf( float num, float left, float right );
223
229 static float lengthSquared( const glm::vec3 & vec );
230
236 static float length( const glm::vec3 & vec );
237
243 static glm::vec3 normalize( const glm::vec3 & vec );
244
251 static float distanceSquared( const glm::vec3 & vec1, const glm::vec3 & vec2 );
252
259 static float distance( const glm::vec3 & vec1, const glm::vec3 & vec2 );
260
268 static glm::quat slerp( const glm::quat & start, const glm::quat & end, float percent );
269
277 static glm::vec3 slerp( const glm::vec3 & start, const glm::vec3 & end, float percent );
278
286 static float vectorAngleAroundNormal( const glm::vec3 & vec1, const glm::vec3 & vec2, const glm::vec3 & norm );
287
294 static glm::mat4 rotationAroundPoint( glm::vec3 point, glm::mat4 rotationMatrix );
295
302 static void quaternionToAngleAxis( const glm::quat & quat, float & angle, glm::vec3 & axis );
303
310 static glm::quat quaternionFromAngleAxis( const float & angle, const glm::vec3 & axis );
311
317 static posef xrPosefToGlmPosef( const XrPosef & xrPosef );
318
326 static glm::mat4 createProjectionMatrix( XrFovf fov, float nearClip, float farClip );
327
333 static std::string sessionStateToString( XrSessionState state );
334
340 static const char * GetXrStructureTypeName( XrStructureType type );
341 };
342} // namespace Engine
Conversion and math helpers used by the OpenXR integration.
static float vectorAngleAroundNormal(const glm::vec3 &vec1, const glm::vec3 &vec2, const glm::vec3 &norm)
Computes the signed angle between two vectors around a normal.
static glm::quat xrQuaternionfToGlmQuat(const XrQuaternionf &xrQuaternion)
Turns the OpenXR quaternion into an OpenXR quaternion.
static glm::mat4 createProjectionMatrix(XrFovf fov, float nearClip, float farClip)
Creates a reverse-Z projection matrix from OpenXR field-of-view angles.
static void LoadXrFunction(XrInstance xrInstance, const std::string &functionName, T &functionPointer)
Loads a function from openxr which is not loaded at compile time. Retrieves function pointer.
static glm::vec3 normalize(const glm::vec3 &vec)
Normalizes a vector.
static float dot(const glm::vec3 &a, const glm::vec3 &b)
The dot product between two glm vector3's.
static float distanceSquared(const glm::vec3 &vec1, const glm::vec3 &vec2)
Computes squared distance between two vectors.
static glm::vec3 crossProductVector3(const glm::vec3 &vector_a, const glm::vec3 &vector_b)
Computes the cross product of two vectors.
static float lerpMix(float from, float to, float progress)
Linearly interpolate between two float values.
static float lengthSquared(const glm::vec3 &vec)
Computes squared vector length.
static const char * GetXrStructureTypeName(XrStructureType type)
Converts an OpenXR structure type to its symbolic name.
static float clampf(float num, float left, float right)
Clamps a float to an inclusive range.
static void quaternionToAngleAxis(const glm::quat &quat, float &angle, glm::vec3 &axis)
Converts a quaternion to angle-axis form.
static float distance(const glm::vec3 &vec1, const glm::vec3 &vec2)
Computes distance between two vectors.
static glm::vec3 xrVector3fToGlmVec3(const XrVector3f &xrVector3)
Converts an OpenXR vector3 to a glm vector3.
static glm::quat slerp(const glm::quat &start, const glm::quat &end, float percent)
Spherically interpolates between two quaternions.
static glm::mat4 rotationAroundPoint(glm::vec3 point, glm::mat4 rotationMatrix)
Builds a transform that applies a rotation around a point.
static glm::mat4 poseToMatrix(const OpenXrHelper::posef &pose)
Pose of glm components to glm::mat4.
static float length(const glm::vec3 &vec)
Computes vector length.
static XrPosef makeIdentityPose()
Makes identity pose.
static glm::vec3 slerp(const glm::vec3 &start, const glm::vec3 &end, float percent)
Spherically interpolates between two direction vectors.
static float dot(const glm::quat &a, const glm::quat &b)
Calculates the dot product between two glm quaternions.
struct Engine::OpenXrHelper::posef posef
A glm based posef struct.
static posef xrPosefToGlmPosef(const XrPosef &xrPosef)
Converts an OpenXR pose to a GLM-backed pose.
static XrVector3f glmVec3ToXrVector3f(const glm::vec3 &glmVector3)
Converts a glm::vec3 to a XrVector3f.
static std::string sessionStateToString(XrSessionState state)
Converts an OpenXR session state to its symbolic name.
static glm::vec3 lerpMix(const glm::vec3 &from, const glm::vec3 &to, float progress)
Linearly interpolate between two glm vector3's.
static glm::quat lerpMix(const glm::quat &from, const glm::quat &to, float progress)
Linearly interpolate between two quaternions.
static glm::quat quaternionFromAngleAxis(const float &angle, const glm::vec3 &axis)
Creates a quaternion from angle-axis form.
static XrQuaternionf glmQuatToXrQuaternionf(const glm::quat &glmQuaternion)
Glm quaternion to xr quaternionf.
static glm::mat4 poseToMatrix(const XrPosef &pose)
Converts an XrPosef to a glm::mat4.
A glm based posef struct.
glm::quat orientation
Pose orientation in GLM quaternion layout.
glm::vec3 position
Pose position in engine world units.