Advertisement
Guest User

Untitled

a guest
Oct 16th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. /** KempoApi: The Turloc Toolkit *****************************/
  2. /** * * **/
  3. /** ** ** Filename: ArcBall.cpp **/
  4. /** ** Version: Common **/
  5. /** ** **/
  6. /** **/
  7. /** Arcball class for mouse manipulation. **/
  8. /** **/
  9. /** **/
  10. /** **/
  11. /** **/
  12. /** (C) 1999-2003 Tatewake.com **/
  13. /** History: **/
  14. /** 08/17/2003 - (TJG) - Creation **/
  15. /** 09/23/2003 - (TJG) - Bug fix and optimization **/
  16. /** 09/25/2003 - (TJG) - Version for NeHe Basecode users **/
  17. /** **/
  18. /*************************************************************/
  19.  
  20. #include <windows.h> // Header File For Windows
  21. #include <gl\gl.h> // Header File For The OpenGL32 Library
  22. #include <gl\glu.h> // Header File For The GLu32 Library
  23. #include <glaux.h> // Header File For The GLaux Library
  24.  
  25. #include "math.h" // Needed for sqrtf
  26.  
  27. #include "ArcBall.h" // ArcBall header
  28.  
  29. //Arcball sphere constants:
  30. //Diameter is 2.0f
  31. //Radius is 1.0f
  32. //Radius squared is 1.0f
  33.  
  34. void ArcBall_t::_mapToSphere(const Point2fT* NewPt, Vector3fT* NewVec) const
  35. {
  36. Point2fT TempPt;
  37. GLfloat length;
  38.  
  39. //Copy paramter into temp point
  40. TempPt = *NewPt;
  41.  
  42. //Adjust point coords and scale down to range of [-1 ... 1]
  43. TempPt.s.X = (TempPt.s.X * this->AdjustWidth) - 1.0f;
  44. TempPt.s.Y = 1.0f - (TempPt.s.Y * this->AdjustHeight);
  45.  
  46. //Compute the square of the length of the vector to the point from the center
  47. length = (TempPt.s.X * TempPt.s.X) + (TempPt.s.Y * TempPt.s.Y);
  48.  
  49. //If the point is mapped outside of the sphere... (length > radius squared)
  50. if (length > 1.0f)
  51. {
  52. GLfloat norm;
  53.  
  54. //Compute a normalizing factor (radius / sqrt(length))
  55. norm = 1.0f / FuncSqrt(length);
  56.  
  57. //Return the "normalized" vector, a point on the sphere
  58. NewVec->s.X = TempPt.s.X * norm;
  59. NewVec->s.Y = TempPt.s.Y * norm;
  60. NewVec->s.Z = 0.0f;
  61. }
  62. else //Else it's on the inside
  63. {
  64. //Return a vector to a point mapped inside the sphere sqrt(radius squared - length)
  65. NewVec->s.X = TempPt.s.X;
  66. NewVec->s.Y = TempPt.s.Y;
  67. NewVec->s.Z = FuncSqrt(1.0f - length);
  68. }
  69. }
  70.  
  71. //Create/Destroy
  72. ArcBall_t::ArcBall_t(GLfloat NewWidth, GLfloat NewHeight)
  73. {
  74. //Clear initial values
  75. this->StVec.s.X =
  76. this->StVec.s.Y =
  77. this->StVec.s.Z =
  78.  
  79. this->EnVec.s.X =
  80. this->EnVec.s.Y =
  81. this->EnVec.s.Z = 0.0f;
  82.  
  83. //Set initial bounds
  84. this->setBounds(NewWidth, NewHeight);
  85. }
  86.  
  87. //Mouse down
  88. void ArcBall_t::click(const Point2fT* NewPt)
  89. {
  90. //Map the point to the sphere
  91. this->_mapToSphere(NewPt, &this->StVec);
  92. }
  93.  
  94. //Mouse drag, calculate rotation
  95. void ArcBall_t::drag(const Point2fT* NewPt, Quat4fT* NewRot)
  96. {
  97. //Map the point to the sphere
  98. this->_mapToSphere(NewPt, &this->EnVec);
  99.  
  100. //Return the quaternion equivalent to the rotation
  101. if (NewRot)
  102. {
  103. Vector3fT Perp;
  104.  
  105. //Compute the vector perpendicular to the begin and end vectors
  106. Vector3fCross(&Perp, &this->StVec, &this->EnVec);
  107.  
  108. //Compute the length of the perpendicular vector
  109. if (Vector3fLength(&Perp) > Epsilon) //if its non-zero
  110. {
  111. //We're ok, so return the perpendicular vector as the transform after all
  112. NewRot->s.X = Perp.s.X;
  113. NewRot->s.Y = Perp.s.Y;
  114. NewRot->s.Z = Perp.s.Z;
  115. //In the quaternion values, w is cosine (theta / 2), where theta is rotation angle
  116. NewRot->s.W= Vector3fDot(&this->StVec, &this->EnVec);
  117. }
  118. else //if its zero
  119. {
  120. //The begin and end vectors coincide, so return an identity transform
  121. NewRot->s.X =
  122. NewRot->s.Y =
  123. NewRot->s.Z =
  124. NewRot->s.W = 0.0f;
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement