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.

rotationsmatrix.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "rotationsmatrix.h"
  23. namespace segl {
  24. Rotationsmatrix::Rotationsmatrix() : Matrix(3,3) {
  25. set(x_axis, 0.0f);
  26. }
  27. Rotationsmatrix::Rotationsmatrix(axis _ax, float _deg) : Matrix(3,3) {
  28. set(_ax, _deg);
  29. }
  30. Rotationsmatrix::Rotationsmatrix(Punkt3D _rotvec, float _deg) : Matrix(3,3) {
  31. set(_rotvec, _deg);
  32. }
  33. void Rotationsmatrix::set(Punkt3D _rotvec, float _deg) {
  34. rotvec = _rotvec;
  35. rotvec.normalize();
  36. deg = _deg;
  37. initRot();
  38. }
  39. void Rotationsmatrix::set(axis _ax, float _deg) {
  40. deg = _deg;
  41. switch(_ax) {
  42. case x_axis:
  43. rotvec.set(1.0f, 0.0f, 0.0f);
  44. break;
  45. case y_axis:
  46. rotvec.set(0.0f, 1.0f, 0.0f);
  47. break;
  48. case z_axis:
  49. rotvec.set(0.0f, 0.0f, 1.0f);
  50. break;
  51. }
  52. initRot();
  53. }
  54. void Rotationsmatrix::set(float _deg) {
  55. deg = _deg;
  56. // rotvec.print();
  57. initRot();
  58. }
  59. void Rotationsmatrix::initRot() {
  60. c[0][0] = scos(deg) + pow(rotvec.x,2.0f) * (1 - scos(deg));
  61. c[0][1] = rotvec.x * rotvec.y * (1 - scos(deg)) - rotvec.z * ssin(deg);
  62. c[0][2] = rotvec.x * rotvec.z * (1 - scos(deg)) + rotvec.y * ssin(deg);
  63. c[1][0] = rotvec.y * rotvec.x * (1 - scos(deg)) + rotvec.z * ssin(deg);
  64. c[1][1] = scos(deg) + pow(rotvec.y,2.0f) * (1 - scos(deg));
  65. c[1][2] = rotvec.y * rotvec.z * (1 - scos(deg)) - rotvec.x * ssin(deg);
  66. c[2][0] = rotvec.z * rotvec.x * (1 - scos(deg)) - rotvec.y * ssin(deg);
  67. c[2][1] = rotvec.z * rotvec.y * (1 - scos(deg)) + rotvec.x * ssin(deg);
  68. c[2][2] = scos(deg) + pow(rotvec.z,2.0f) * (1 - scos(deg));
  69. }
  70. Punkt3D Rotationsmatrix::operator*(const Punkt3D &p) {
  71. Matrix erg(3,1);
  72. Punkt3D erg2;
  73. erg = (((Matrix*)(this))->operator*(Matrix(p)));
  74. const float **mat = erg.getMatrix();
  75. erg2.set(mat[0][0], mat[1][0], mat[2][0]);
  76. return erg2;
  77. }
  78. Rotationsmatrix Rotationsmatrix::operator*(const Rotationsmatrix &p) {
  79. Matrix m(3, 3);
  80. Rotationsmatrix rotmat;
  81. m = ((Matrix)(*this) * (Matrix)p);
  82. const float **mat = m.getMatrix();
  83. rotmat.c[0][0] = mat[0][0];
  84. rotmat.c[0][1] = mat[0][1];
  85. rotmat.c[0][2] = mat[0][2];
  86. rotmat.c[1][0] = mat[1][0];
  87. rotmat.c[1][1] = mat[1][1];
  88. rotmat.c[1][2] = mat[1][2];
  89. rotmat.c[2][0] = mat[2][0];
  90. rotmat.c[2][1] = mat[2][1];
  91. rotmat.c[2][2] = mat[2][2];
  92. return rotmat;
  93. }
  94. Rotationsmatrix::~Rotationsmatrix() {
  95. }
  96. } // namespace segl