2727
2828
2929#include " ArcBall.hpp"
30- #include < iostream >
30+ #include < glm/gtx/vec_swizzle.hpp >
3131
3232namespace spire {
3333
3434// ------------------------------------------------------------------------------
35- ArcBall::ArcBall (const glm::vec3& center, glm:: float_t radius, bool inverted, const glm::mat4& screenToTCS) :
35+ ArcBall::ArcBall (const glm::vec3& center, float radius, bool inverted, const glm::mat4& screenToTCS) :
3636 mScreenToTCS (screenToTCS),
3737 mCenter (center),
3838 mRadius (radius),
@@ -51,13 +51,9 @@ namespace spire {
5151// ------------------------------------------------------------------------------
5252glm::vec3 ArcBall::mouseOnSphere (const glm::vec3& tscMouse)
5353{
54- glm::vec3 ballMouse;
54+ glm::vec3 ballMouse ( xy (tscMouse - mCenter ) / mRadius , 0.0 ) ;
5555
56- // (m - C) / R
57- ballMouse.x = (tscMouse.x - mCenter .x ) / mRadius ;
58- ballMouse.y = (tscMouse.y - mCenter .y ) / mRadius ;
59-
60- glm::float_t mag_sq = glm::dot (ballMouse, ballMouse);
56+ float mag_sq = glm::dot (ballMouse, ballMouse);
6157 if (mag_sq > 1.0 )
6258 {
6359 // Since we are outside of the sphere, map to the visible boundary of the sphere.
@@ -79,26 +75,24 @@ glm::vec3 ArcBall::mouseOnSphere(const glm::vec3& tscMouse)
7975void ArcBall::beginDrag (const glm::vec2& msc)
8076{
8177 mQDown = mQNow ;
82- mVSphereDown = mouseOnSphere ((mScreenToTCS * glm::vec4 (msc, 0 .0f , 1.0 )). xyz ( ));
78+ mVSphereDown = mouseOnSphere (xyz (mScreenToTCS * glm::vec4 (msc, 0 .0f , 1 .0f ) ));
8379}
8480
8581// ------------------------------------------------------------------------------
8682void ArcBall::drag (const glm::vec2& msc)
8783{
88- glm::vec3 mVSphereNow = mouseOnSphere ((mScreenToTCS * glm::vec4 (msc, 0.0 , 1.0 )). xyz ( ));
84+ glm::vec3 mVSphereNow = mouseOnSphere (xyz (mScreenToTCS * glm::vec4 (msc, 0 .0f , 1 .0f ) ));
8985
9086 // Construct a quaternion from two points on the unit sphere.
9187 glm::quat mQDrag = quatFromUnitSphere (mVSphereDown , mVSphereNow );
9288 mQNow = mQDrag * mQDown ;
93- if (glm::dot (mVSphereDown , mVSphereNow ) < 0.0 )
94- beginDrag (msc);
9589}
9690
9791// ------------------------------------------------------------------------------
9892void ArcBall::setLocationOnSphere (glm::vec3 location, glm::vec3 up)
9993{
10094 glm::mat4 mMatNow = glm::lookAt (location, glm::vec3 (0 .0f ), up);
101- mQNow = glm::quat_cast (mMatNow );
95+ mQNow = glm::quat_cast (mMatNow );
10296}
10397
10498// ------------------------------------------------------------------------------
@@ -107,7 +101,7 @@ glm::quat ArcBall::quatFromUnitSphere(const glm::vec3& from, const glm::vec3& to
107101 // TODO: check if cross is 0 before normalize. Crashes on Windows
108102 auto c = glm::cross (from, to);
109103 if (c == glm::vec3 (0 , 0 , 0 ))
110- return {} ;
104+ return glm::quat ( 1.0 , 0.0 , 0.0 , 0.0 ) ;
111105
112106 glm::vec3 axis = glm::normalize (c);
113107
0 commit comments