Doubles (#14)
Added a compile time option to use double precision body positions. Most math remains single precision with minimal performance loss. `BOX3D_DOUBLE_PRECISION` Improved accuracy of body inertia computation when shapes are far from the body origin. WARNING: several APIs changed but migration should be straight forward if doubles are not enabled.
This commit is contained in:
parent
a252f03b19
commit
09c65b08ea
115 changed files with 7591 additions and 5295 deletions
|
|
@ -1,9 +1,8 @@
|
|||
#include "camera.h"
|
||||
|
||||
#include "gfx/picking.h"
|
||||
#include "sokol_app.h"
|
||||
|
||||
#include "box3d/math_functions.h"
|
||||
#include "sokol_app.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
|
|
@ -52,14 +51,15 @@ static void RebuildBasisAndView( Camera& c )
|
|||
const b3Vec3 upUnnorm = b3Sub( worldUp, b3MulSV( b3Dot( worldUp, forward ), forward ) );
|
||||
const b3Vec3 up = b3Normalize( upUnnorm );
|
||||
const b3Vec3 right = b3Normalize( b3Cross( up, forward ) );
|
||||
const b3Vec3 position = { c.m_pivot.x + forward.x * c.m_radius, c.m_pivot.y + forward.y * c.m_radius,
|
||||
c.m_pivot.z + forward.z * c.m_radius };
|
||||
|
||||
c.m_position = position;
|
||||
// Eye in world space is the draw origin. Building the view in that relative frame puts the eye
|
||||
// at the origin, so the view matrix carries no translation and stays exact far from the origin.
|
||||
c.m_worldEye = b3OffsetPos( c.m_pivot, b3MulSV( c.m_radius, forward ) );
|
||||
c.m_position = b3Vec3_zero;
|
||||
c.m_right = right;
|
||||
c.m_up = up;
|
||||
c.m_forward = forward;
|
||||
MakeViewAndInverse( &c.m_view, &c.m_viewInv, right, up, forward, position );
|
||||
MakeViewAndInverse( &c.m_view, &c.m_viewInv, right, up, forward, c.m_position );
|
||||
}
|
||||
|
||||
Camera::Camera()
|
||||
|
|
@ -75,6 +75,7 @@ Camera::Camera()
|
|||
, m_height( 0 )
|
||||
, m_thirdPerson( false )
|
||||
, m_position{ 0.0f, 0.0f, 0.0f }
|
||||
, m_worldEye{ 0.0f, 0.0f, 0.0f }
|
||||
, m_right{ 1.0f, 0.0f, 0.0f }
|
||||
, m_up{ 0.0f, 1.0f, 0.0f }
|
||||
, m_forward{ 0.0f, 0.0f, 1.0f }
|
||||
|
|
@ -103,7 +104,7 @@ void Camera::Frame( b3AABB aabb, float aspect, float padding )
|
|||
b3Vec3 center = b3AABB_Center( aabb );
|
||||
b3Vec3 ext = b3AABB_Extents( aabb ); // half-extents
|
||||
float r = sqrtf( ext.x * ext.x + ext.y * ext.y + ext.z * ext.z );
|
||||
m_pivot = center;
|
||||
m_pivot = b3ToPos( center );
|
||||
if ( r < 1.0e-6f )
|
||||
{
|
||||
// Point-like; preserve current radius, just retarget. The cached
|
||||
|
|
@ -140,10 +141,8 @@ void Camera::SetOrbit( float yawRadians, float pitchRadians, float radius )
|
|||
RebuildBasisAndView( *this );
|
||||
}
|
||||
|
||||
void Camera::SetView( float yawDegrees, float pitchDegrees, float radius, b3Vec3 pivot )
|
||||
void Camera::SetView( float yawDegrees, float pitchDegrees, float radius, b3Pos pivot )
|
||||
{
|
||||
// Box3D feeds degrees; render3d stores radians. Pivot first so SetOrbit's
|
||||
// basis rebuild uses the new pivot.
|
||||
SetPivot( pivot );
|
||||
SetOrbit( yawDegrees * DEG_TO_RAD, pitchDegrees * DEG_TO_RAD, radius );
|
||||
}
|
||||
|
|
@ -160,14 +159,47 @@ b3Vec3 Camera::Position() const
|
|||
|
||||
PickRay Camera::BuildPickRay( float x, float y ) const
|
||||
{
|
||||
// Defined zero ray so callers never read uninitialized values when the
|
||||
// camera has no size yet or the unprojection is degenerate.
|
||||
PickRay ray;
|
||||
if ( !::BuildPickRay( x, y, &ray.origin, &ray.translation ) )
|
||||
ray.origin = b3Pos_zero;
|
||||
ray.translation = b3Vec3_zero;
|
||||
|
||||
if ( m_width <= 0 || m_height <= 0 )
|
||||
{
|
||||
// Camera state not latched yet, or degenerate matrices: defined zero
|
||||
// ray so callers never read uninitialized values.
|
||||
ray.origin = m_position;
|
||||
ray.translation = b3Vec3_zero;
|
||||
return ray;
|
||||
}
|
||||
|
||||
// Pixel -> NDC. Origin top-left, Y down. NDC has Y up, X right.
|
||||
const float ndcX = ( 2.0f * x / (float)m_width ) - 1.0f;
|
||||
const float ndcY = 1.0f - ( 2.0f * y / (float)m_height );
|
||||
|
||||
// Reverse-Z: near maps to clip-Z = +w, far to 0. Unproject both clip points
|
||||
// through inv(P*V) = viewInv * projInv, no runtime matrix inversion. The
|
||||
// matrices are eye-relative, so the recovered points are too.
|
||||
const Mat4 invVP = MulMM4( m_viewInv, m_projInv );
|
||||
|
||||
const Vec4 clipN = MakeVec4( ndcX, ndcY, 1.0f, 1.0f );
|
||||
const Vec4 clipF = MakeVec4( ndcX, ndcY, 0.0f, 1.0f );
|
||||
|
||||
const Vec4 wN = MulMV4( invVP, clipN );
|
||||
const Vec4 wF = MulMV4( invVP, clipF );
|
||||
|
||||
if ( wN.w == 0.0f || wF.w == 0.0f )
|
||||
{
|
||||
return ray;
|
||||
}
|
||||
|
||||
const float invWN = 1.0f / wN.w;
|
||||
const float invWF = 1.0f / wF.w;
|
||||
|
||||
b3Vec3 nearWorld = { wN.x * invWN, wN.y * invWN, wN.z * invWN };
|
||||
b3Vec3 farWorld = { wF.x * invWF, wF.y * invWF, wF.z * invWF };
|
||||
|
||||
// Lift the near point to absolute world with the double eye. Translation is
|
||||
// a delta so it stays eye-relative and needs no offset.
|
||||
ray.origin = b3OffsetPos( m_worldEye, nearWorld );
|
||||
ray.translation = b3Sub( farWorld, nearWorld );
|
||||
return ray;
|
||||
}
|
||||
|
||||
|
|
@ -321,13 +353,12 @@ void Camera::Update( float dt, int width, int height )
|
|||
{
|
||||
// Snapshot eye BEFORE rotating so yaw/pitch pivot around the eye
|
||||
// (FPS) instead of around m_pivot (orbit). After rotation we
|
||||
// back-derive m_pivot from this preserved eye so Position()
|
||||
// returns the same point regardless of look angle.
|
||||
const b3Vec3 eyeBefore = Position();
|
||||
// back-derive m_pivot from this preserved eye so the eye stays put
|
||||
// regardless of look angle.
|
||||
const b3Pos eyeBefore = b3OffsetPos( m_pivot, b3MulSV( m_radius, ForwardFromAngles( m_yaw, m_pitch ) ) );
|
||||
|
||||
// FPS look: drag right -> yaw decreases (turn head right, scene
|
||||
// shifts left); drag down -> pitch increases (look down). Box3D's
|
||||
// signs - opposite of render3d's orbit pitch sign on purpose.
|
||||
// shifts left); drag down -> pitch increases (look down).
|
||||
if ( m_orbitDX != 0.0f || m_orbitDY != 0.0f )
|
||||
{
|
||||
m_yaw -= m_orbitDX * FLY_LOOK_SENS;
|
||||
|
|
@ -351,7 +382,7 @@ void Camera::Update( float dt, int width, int height )
|
|||
if ( m_aDown )
|
||||
wasdR -= 1.0f;
|
||||
|
||||
b3Vec3 eye = eyeBefore;
|
||||
b3Pos eye = eyeBefore;
|
||||
if ( wasdF != 0.0f || wasdR != 0.0f )
|
||||
{
|
||||
// Right = normalize(worldUp x forward), matching Box3D's
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ struct sapp_event;
|
|||
// translation spans near -> far. Feeds straight into b3World_CastRay*.
|
||||
struct PickRay
|
||||
{
|
||||
b3Vec3 origin;
|
||||
b3Pos origin;
|
||||
b3Vec3 translation;
|
||||
};
|
||||
|
||||
|
|
@ -57,14 +57,17 @@ public:
|
|||
{
|
||||
return m_view;
|
||||
}
|
||||
|
||||
Mat4 ViewInverse() const
|
||||
{
|
||||
return m_viewInv;
|
||||
}
|
||||
|
||||
Mat4 Proj() const
|
||||
{
|
||||
return m_proj;
|
||||
}
|
||||
|
||||
Mat4 ProjInverse() const
|
||||
{
|
||||
return m_projInv;
|
||||
|
|
@ -72,20 +75,22 @@ public:
|
|||
|
||||
b3Vec3 Position() const;
|
||||
|
||||
// Cached basis accessors (Box3D parity). Forward is +view-Z (pivot -> eye),
|
||||
// so the look direction is -GetForward().
|
||||
// Forward is +view-Z (pivot -> eye), so the look direction is -GetForward().
|
||||
b3Vec3 GetPosition() const
|
||||
{
|
||||
return m_position;
|
||||
}
|
||||
|
||||
b3Vec3 GetForward() const
|
||||
{
|
||||
return m_forward;
|
||||
}
|
||||
|
||||
b3Vec3 GetRight() const
|
||||
{
|
||||
return m_right;
|
||||
}
|
||||
|
||||
b3Vec3 GetUp() const
|
||||
{
|
||||
return m_up;
|
||||
|
|
@ -97,14 +102,14 @@ public:
|
|||
|
||||
// Box3D's sample signature: angles in degrees, pivot folded in. Mirrors
|
||||
// the ~140 sample call sites exactly so they need no edits.
|
||||
void SetView( float yawDegrees, float pitchDegrees, float radius, b3Vec3 pivot = b3Vec3_zero );
|
||||
void SetView( float yawDegrees, float pitchDegrees, float radius, b3Pos pivot );
|
||||
|
||||
void SetPivot( b3Vec3 pivot )
|
||||
void SetPivot( b3Pos pivot )
|
||||
{
|
||||
m_pivot = pivot;
|
||||
}
|
||||
// Forwarder for existing render3d call sites.
|
||||
void SetTarget( b3Vec3 target )
|
||||
|
||||
void SetTarget( b3Pos target )
|
||||
{
|
||||
m_pivot = target;
|
||||
}
|
||||
|
|
@ -132,13 +137,13 @@ public:
|
|||
// point for sample code that mutates m_pivot directly (third-person follow).
|
||||
void UpdateTransform();
|
||||
|
||||
// World-space pick ray from a framebuffer-pixel coordinate, using the
|
||||
// latched camera state. Wraps the free function in gfx/picking.h; hands
|
||||
// back a zero-translation ray (origin at the eye) if that state is not
|
||||
// ready yet, so callers never read uninitialized values.
|
||||
// World-space pick ray from a framebuffer-pixel coordinate. Unprojects
|
||||
// through the camera's own eye-relative matrices and framebuffer size, then
|
||||
// lifts the origin to absolute world with the double eye. Hands back a zero
|
||||
// ray if the camera has no size yet, so callers never read uninitialized values.
|
||||
PickRay BuildPickRay( float x, float y ) const;
|
||||
|
||||
b3Vec3 m_pivot;
|
||||
b3Pos m_pivot; // look-at point, world space. Double precision so it stays exact far from origin.
|
||||
float m_yaw; // radians, around Y
|
||||
float m_pitch; // radians, around camera-frame X
|
||||
float m_radius; // meters from pivot
|
||||
|
|
@ -155,14 +160,19 @@ public:
|
|||
int m_width;
|
||||
int m_height;
|
||||
|
||||
// Locks input to wheel-zoom; the sample drives m_pivot to a followed body
|
||||
// Locks input to wheel-zoom. The sample drives m_pivot to a followed body
|
||||
// and calls UpdateTransform. Eye placement stays the orbit formula.
|
||||
bool m_thirdPerson;
|
||||
|
||||
// Cached basis (Box3D-style: forward is +view-Z, i.e. pivot->eye), refreshed
|
||||
// Cached basis: forward is +view-Z, i.e. pivot->eye) refreshed
|
||||
// alongside m_view / m_viewInv whenever yaw/pitch/pivot/radius change.
|
||||
// These let consumers (picking, shadow frustum) skip re-inverting m_view.
|
||||
//
|
||||
// The view is built in the eye-relative frame, so m_position (the eye in that
|
||||
// frame) is always zero and the view matrix is translation free. m_worldEye is
|
||||
// the eye in world space, the draw origin the sample renders and picks against.
|
||||
b3Vec3 m_position;
|
||||
b3Pos m_worldEye;
|
||||
b3Vec3 m_right;
|
||||
b3Vec3 m_up;
|
||||
b3Vec3 m_forward;
|
||||
|
|
|
|||
Loading…
Reference in a new issue