libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moArcBall.cpp
Go to the documentation of this file.
1 
18 /*************************************************************/
19 
20 #include "moTypes.h"
21 #include <stdio.h>
22 
23 #include "math.h" // Needed for sqrtf
24 
25 #include "moArcBall.h" // ArcBall header
26 
27 //Arcball sphere constants:
28 //Diameter is 2.0f
29 //Radius is 1.0f
30 //Radius squared is 1.0f
31 
32 void ArcBall_t::_mapToSphere(const Point2fT* NewPt, Vector3fT* NewVec) const
33 {
34  Point2fT TempPt;
35  GLfloat length;
36 
37  //Copy paramter into temp point
38  TempPt = *NewPt;
39 
40  //Adjust point coords and scale down to range of [-1 ... 1]
41  TempPt.s.X = (TempPt.s.X * this->AdjustWidth) - 1.0f;
42  TempPt.s.Y = 1.0f -(TempPt.s.Y * this->AdjustHeight);
43 
44  //Compute the square of the length of the vector to the point from the center
45  length =(TempPt.s.X * TempPt.s.X) +(TempPt.s.Y * TempPt.s.Y);
46 
47  //If the point is mapped outside of the sphere...(length > radius squared)
48  if(length > 1.0f)
49  {
50  GLfloat norm;
51 
52  //Compute a normalizing factor(radius / sqrt(length))
53  norm = 1.0f / FuncSqrt(length);
54 
55  //Return the "normalized" vector, a point on the sphere
56  NewVec->s.X = TempPt.s.X * norm;
57  NewVec->s.Y = TempPt.s.Y * norm;
58  NewVec->s.Z = 0.0f;
59  }
60  else //Else it's on the inside
61  {
62  //Return a vector to a point mapped inside the sphere sqrt(radius squared - length)
63  NewVec->s.X = TempPt.s.X;
64  NewVec->s.Y = TempPt.s.Y;
65  NewVec->s.Z = FuncSqrt(1.0f - length);
66  }
67 }
68 
69 //Create/Destroy
71 {
72  this->StVec.s.X =
73  this->StVec.s.Y =
74  this->StVec.s.Z =
75 
76  this->EnVec.s.X =
77  this->EnVec.s.Y =
78  this->EnVec.s.Z = 0.0f;
79 
80  this->AdjustWidth =
81  this->AdjustHeight = 0.0f;
82 }
83 
84 ArcBall_t::ArcBall_t(GLfloat NewWidth, GLfloat NewHeight)
85 {
86  //Clear initial values
87  this->StVec.s.X =
88  this->StVec.s.Y =
89  this->StVec.s.Z =
90 
91  this->EnVec.s.X =
92  this->EnVec.s.Y =
93  this->EnVec.s.Z = 0.0f;
94 
95  //Set initial bounds
96  this->setBounds(NewWidth, NewHeight);
97 }
98 
99 //Mouse down
100 void ArcBall_t::click(const Point2fT* NewPt)
101 {
102  //Map the point to the sphere
103  this->_mapToSphere(NewPt, &this->StVec);
104 }
105 
106 //Mouse drag, calculate rotation
107 void ArcBall_t::drag(const Point2fT* NewPt, Quat4fT* NewRot)
108 {
109  //Map the point to the sphere
110  this->_mapToSphere(NewPt, &this->EnVec);
111 
112  //Return the quaternion equivalent to the rotation
113  if(NewRot)
114  {
115  Vector3fT Perp;
116 
117  //Compute the vector perpendicular to the begin and end vectors
118  Vector3fCross(&Perp, &this->StVec, &this->EnVec);
119 
120  //Compute the length of the perpendicular vector
121  if(Vector3fLength(&Perp) > Epsilon) //if its non-zero
122  {
123  //We're ok, so return the perpendicular vector as the transform after all
124  NewRot->s.X = Perp.s.X;
125  NewRot->s.Y = Perp.s.Y;
126  NewRot->s.Z = Perp.s.Z;
127  //In the quaternion values, w is cosine(theta / 2), where theta is rotation angle
128  NewRot->s.W= Vector3fDot(&this->StVec, &this->EnVec);
129  }
130  else //if its zero
131  {
132  //The begin and end vectors coincide, so return an identity transform
133  NewRot->s.X =
134  NewRot->s.Y =
135  NewRot->s.Z =
136  NewRot->s.W = 0.0f;
137  }
138  }
139 }
#define Quat4fT
Definition: moArcBall.h:108
void setBounds(GLfloat NewWidth, GLfloat NewHeight)
Definition: moArcBall.h:453
GLfloat AdjustWidth
Definition: moArcBall.h:471
#define Vector3fT
Definition: moArcBall.h:111
GLfloat AdjustHeight
Definition: moArcBall.h:472
Vector3fT EnVec
Definition: moArcBall.h:470
#define FuncSqrt
Definition: moArcBall.h:114
#define Epsilon
Definition: moArcBall.h:118
#define Point2fT
Definition: moArcBall.h:106
void drag(const Point2fT *NewPt, Quat4fT *NewRot)
Definition: moArcBall.cpp:107
void _mapToSphere(const Point2fT *NewPt, Vector3fT *NewVec) const
Definition: moArcBall.cpp:32
void click(const Point2fT *NewPt)
Definition: moArcBall.cpp:100
Vector3fT StVec
Definition: moArcBall.h:469