Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

quaternion.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* libsegl - Sebas Extended GL Library
  2. * Collection of Opengl/3D-Math helpers
  3. *
  4. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  5. * http://www.seba-geek.de
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "quaternion.h"
  23. namespace segl {
  24. Quaternion::Quaternion() {
  25. w = 1.0f,
  26. x = y = z = 0.0f;
  27. }
  28. Quaternion::Quaternion(float _x, float _y, float _z, float _w) {
  29. x = _x;
  30. y = _y;
  31. z = _z;
  32. w = _w;
  33. }
  34. Quaternion::Quaternion(float deg, Punkt3D p) {
  35. set(deg, p);
  36. }
  37. Quaternion::Quaternion(float _x, float _y, float _z) {
  38. set(_x, _y, _z);
  39. }
  40. void Quaternion::set(float rotx, float roty, float rotz) {
  41. rotx = deg2rad(rotx);
  42. roty = deg2rad(roty);
  43. rotz = deg2rad(rotz);
  44. float sinx = sin(rotx);
  45. float siny = sin(roty);
  46. float sinz = sin(rotz);
  47. float cosx = cos(rotx);
  48. float cosy = cos(roty);
  49. float cosz = cos(rotz);
  50. x = sinz * cosx * cosy - cosz * sinx * siny;
  51. y = cosz * sinx * cosy + sinz * cosx * siny;
  52. z = cosz * cosx * siny - sinz * sinx * cosy;
  53. w = cosz * cosx * cosy - sinz * sinx * siny;
  54. normalize();
  55. }
  56. void Quaternion::set(float deg, Punkt3D p) {
  57. float angle = deg2rad(0.5f*deg);
  58. float sinangle = sin(angle);
  59. x = p.x * sinangle;
  60. y = p.y * sinangle;
  61. z = p.z * sinangle;
  62. w = cos(angle);
  63. normalize();
  64. }
  65. void Quaternion::normalize() {
  66. float len = x*x+y*y+z*z+w*w;
  67. if(std::fabs(len-1.0f)>0.00001f) {
  68. len = sqrt(len);
  69. x /= len;
  70. y /= len;
  71. z /= len;
  72. w /= len;
  73. }
  74. }
  75. Quaternion Quaternion::getConjugate() {
  76. return Quaternion(-x, -y, -z, w);
  77. }
  78. // Rotationsmatrix Quaternion::getRotMat() {
  79. //
  80. // }
  81. Punkt3D Quaternion::rotP3D(const Punkt3D &p) {
  82. Quaternion q(p.x, p.y, p.z, 0.0f);
  83. Quaternion erg = *this * q * this->getConjugate();
  84. return Punkt3D(erg.x, erg.y, erg.z);
  85. }
  86. Quaternion Quaternion::operator*(const Quaternion &q) {
  87. return Quaternion(w*q.x - x*q.w + y*q.z - z*q.y,
  88. w*q.y - y*q.w + z*q.x - x*q.z,
  89. w*q.z - z*q.w + x*q.y - y*q.x,
  90. w*q.w - x*q.x - y*q.y - z*q.z);
  91. }
  92. // Quaternion::Quaternion() {
  93. // w = 1.0f;
  94. // x = y = z = 0.0f;
  95. // }
  96. //
  97. // void Quaternion::createFromRotation(float deg, float _x, float _y, float _z) {
  98. // float tmpres = sin(deg2rad(deg) / 2.0f);
  99. //
  100. // w = cos(deg2rad(deg) / 2.0f);
  101. // x = _x * tmpres;
  102. // y = _y * tmpres;
  103. // z = _z * tmpres;
  104. // }
  105. //
  106. // Quaternion Quaternion::operator*(const Quaternion &q) {
  107. // Quaternion ret;
  108. //
  109. // ret.w = w*q.w - x*q.x - y*q.y - z*q.z;
  110. // ret.x = w*q.x + x*q.w + y*q.z - z*q.y;
  111. // ret.y = w*q.y + y*q.w + z*q.x - x*q.z;
  112. // ret.z = w*q.z + z*q.w + x*q.y - y*q.x;
  113. //
  114. // return ret;
  115. // }
  116. //
  117. // void Quaternion::createGlMatrix(GLfloat *matrix) {
  118. // if(!matrix)
  119. // return;
  120. //
  121. // matrix[0] = 1.0f - 2.0f * ( y*y + z*z);
  122. // matrix[1] = 2.0f * ( x*y + z*w);
  123. // matrix[2] = 2.0f * ( x*z - y*w);
  124. // matrix[3] = 0.0f;
  125. //
  126. // matrix[4] = 2.0f * ( x*y - z*w);
  127. // matrix[5] = 1.0f - 2.0f * ( x*x + z*z);
  128. // matrix[6] = 2.0f * ( z*y + x*w);
  129. // matrix[7] = 0.0f;
  130. //
  131. // matrix[8] = 2.0f * ( x*z + y*w);
  132. // matrix[9] = 2.0f * ( y*z - x*w);
  133. // matrix[10] = 1.0f - 2.0f * ( x*x + y*y);
  134. // matrix[11] = 0.0f;
  135. //
  136. // matrix[12] = 0.0f;
  137. // matrix[13] = 0.0f;
  138. // matrix[14] = 0.0f;
  139. // matrix[15] = 1.0f;
  140. //
  141. // }
  142. //
  143. // Punkt3D Quaternion::getDirectionVector() {
  144. // GLfloat matrix[16];
  145. // createGlMatrix(matrix);
  146. //
  147. // return getDirectionVector(matrix);
  148. // }
  149. //
  150. // Punkt3D Quaternion::getDirectionVector(GLfloat *matrix) {
  151. // if(!matrix)
  152. // return Punkt3D();
  153. // Punkt3D tmp;
  154. //
  155. // tmp.x = matrix[8];
  156. // tmp.y = matrix[9];
  157. // tmp.z = matrix[10];
  158. //
  159. // return tmp;
  160. // }
  161. //
  162. // void glMultMatrixf(Quaternion q) {
  163. // GLfloat matrix[16];
  164. // q.createGlMatrix(matrix);
  165. // glMultMatrixf(matrix);
  166. // }
  167. } // namespace segl