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.

punkt3d.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "punkt3d.h"
  23. namespace segl {
  24. Punkt3D::Punkt3D() {
  25. x = y = z = 0.0f;
  26. }
  27. Punkt3D::Punkt3D(float _x, float _y, float _z) {
  28. set(_x, _y, _z);
  29. }
  30. void Punkt3D::set(float _x, float _y, float _z) {
  31. x = _x;
  32. y = _y;
  33. z = _z;
  34. }
  35. float Punkt3D::abs() const {
  36. return sqrt(x*x+y*y+z*z);
  37. }
  38. Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) const {
  39. Punkt3D erg;
  40. erg.x = y*b.z - z*b.y;
  41. erg.y = z*b.x - x*b.z;
  42. erg.z = x*b.y - y*b.x;
  43. return erg;
  44. }
  45. void Punkt3D::normalize() {
  46. float a = abs();
  47. x /= a;
  48. y /= a;
  49. z /= a;
  50. }
  51. Punkt3D Punkt3D::getNormalized() const {
  52. Punkt3D ret(*this);
  53. ret.normalize();
  54. return ret;
  55. }
  56. bool Punkt3D::isNormalized() const {
  57. return (abs()==1.0f);
  58. }
  59. float Punkt3D::calcAngle(Punkt3D b) const {
  60. if(abs()*b.abs()==0.0f)
  61. return 0.0f;
  62. return rad2deg(std::acos(((*this)*b)/(abs()*b.abs())));
  63. }
  64. Punkt3D Punkt3D::getOrtographic() const {
  65. Punkt3D erg;
  66. if(!x) {
  67. erg.x = 0.0f;
  68. erg.y = z;
  69. erg.z = -y;
  70. } else if(!y) {
  71. erg.x = z;
  72. erg.y = 0;
  73. erg.z = -x;
  74. } else {
  75. // z, oder genereller fall
  76. erg.x = y;
  77. erg.y = -x;
  78. erg.z = 0.0f;
  79. }
  80. return erg;
  81. }
  82. Punkt3D Punkt3D::getOrtographic2() const {
  83. Punkt3D nullvec;
  84. Punkt3D erg;
  85. erg.set(y, -x, 0.0f);
  86. if(erg!=nullvec)
  87. return erg;
  88. erg.set(z, 0.0f, -x);
  89. if(erg!=nullvec)
  90. return erg;
  91. erg.set(0.0f, z, -y);
  92. return erg;
  93. }
  94. void Punkt3D::print(std::string coordname) const {
  95. if(coordname!="")
  96. coordname.append(" ");
  97. std::cout << coordname << "Coord: (" << x << ", " << y << ", " << z << ")" << std::endl;
  98. }
  99. Punkt3D Punkt3D::operator+(const Punkt3D &b) const {
  100. Punkt3D c;
  101. c.x = x + b.x;
  102. c.y = y + b.y;
  103. c.z = z + b.z;
  104. return c;
  105. }
  106. Punkt3D Punkt3D::operator-(const Punkt3D &b) const {
  107. Punkt3D c;
  108. c.x = x - b.x;
  109. c.y = y - b.y;
  110. c.z = z - b.z;
  111. return c;
  112. }
  113. Punkt3D& Punkt3D::operator+=(const Punkt3D &b) {
  114. x += b.x;
  115. y += b.y;
  116. z += b.z;
  117. return *this;
  118. }
  119. Punkt3D& Punkt3D::operator-=(const Punkt3D &b) {
  120. x -= b.x;
  121. y -= b.y;
  122. z -= b.z;
  123. return *this;
  124. }
  125. Punkt3D Punkt3D::operator+(const float &_m) const {
  126. return Punkt3D(x+_m, y+_m, z+_m);
  127. }
  128. Punkt3D Punkt3D::operator-(const float &_m) const {
  129. return Punkt3D(x-_m, y-_m, z-_m);
  130. }
  131. Punkt3D Punkt3D::operator*(const float &_m) const {
  132. return Punkt3D(x*_m, y*_m, z*_m);
  133. }
  134. Punkt3D Punkt3D::operator/(const float &_m) const {
  135. return Punkt3D(x/_m, y/_m, z/_m);
  136. }
  137. Punkt3D& Punkt3D::operator+=(const float &_m) {
  138. x += _m;
  139. y += _m;
  140. z += _m;
  141. return *this;
  142. }
  143. Punkt3D& Punkt3D::operator-=(const float &_m) {
  144. x -= _m;
  145. y -= _m;
  146. z -= _m;
  147. return *this;
  148. }
  149. Punkt3D& Punkt3D::operator*=(const float &_m) {
  150. x *= _m;
  151. y *= _m;
  152. z *= _m;
  153. return *this;
  154. }
  155. Punkt3D& Punkt3D::operator/=(const float &_m) {
  156. x /= _m;
  157. y /= _m;
  158. z /= _m;
  159. return *this;
  160. }
  161. float Punkt3D::operator*(const Punkt3D& _m) const {
  162. return x * _m.x + y * _m.y + z * _m.z;
  163. }
  164. Punkt3D Punkt3D::operator-() const {
  165. return Punkt3D(-x, -y, -z);
  166. }
  167. bool Punkt3D::operator==(const Punkt3D& b) const {
  168. return ( x==b.x && y==b.y && z==b.z);
  169. }
  170. bool Punkt3D::operator!=(const Punkt3D& b) const {
  171. return !(*this==b);
  172. }
  173. // Freunde
  174. segl::Punkt3D operator+(const float& _m, const segl::Punkt3D& b) {
  175. return segl::Punkt3D(b.x+_m, b.y+_m, b.z+_m);
  176. }
  177. segl::Punkt3D operator-(const float& _m, const segl::Punkt3D& b) {
  178. return segl::Punkt3D(b.x-_m, b.y-_m, b.z-_m);
  179. }
  180. segl::Punkt3D operator*(const float& _m, const segl::Punkt3D& b) {
  181. return segl::Punkt3D(b.x*_m, b.y*_m, b.z*_m);
  182. }
  183. segl::Punkt3D operator/(const float& _m, const segl::Punkt3D& b) {
  184. return segl::Punkt3D(b.x/_m, b.y/_m, b.z/_m);
  185. }
  186. std::ostream &operator<<(std::ostream &ostr, const segl::Punkt3D &r) {
  187. return ostr << "(" << r.x << ", " << r.y << ", " << r.z << ")";
  188. }
  189. } // namespace segl
  190. float abs(const segl::Punkt3D &p) {
  191. return p.abs();
  192. }
  193. // OpenGL Funktionen f�r Punkt3D
  194. void glVertex3f(const segl::Punkt3D &p) {
  195. glVertex3f(p.x, p.y, p.z);
  196. }
  197. void glTranslatef(const segl::Punkt3D &p) {
  198. glTranslatef(p.x, p.y, p.z);
  199. }
  200. void glNormal3f(const segl::Punkt3D &p) {
  201. glNormal3f(p.x, p.y, p.z);
  202. }
  203. void glRotatef(const float &deg, const segl::Punkt3D &vec) {
  204. glRotatef(deg, vec.x, vec.y, vec.z);
  205. }
  206. // Funktionen mit richtgen bezeichnern
  207. void glVertexP3D(const segl::Punkt3D &p) {
  208. glVertex3f(p.x, p.y, p.z);
  209. }
  210. void glTranslateP3D(const segl::Punkt3D &p) {
  211. glTranslatef(p.x, p.y, p.z);
  212. }
  213. void glNormalP3D(const segl::Punkt3D &p) {
  214. glNormal3f(p.x, p.y, p.z);
  215. }
  216. void glRotateP3D(const float &deg, const segl::Punkt3D &vec) {
  217. glRotatef(deg, vec.x, vec.y, vec.z);
  218. }
  219. void gluLookAt(const segl::Punkt3D &pos, const segl::Punkt3D &viewport, const segl::Punkt3D &normal) {
  220. gluLookAt( pos.x, pos.y, pos.z,
  221. viewport.x, viewport.y, viewport.z,
  222. normal.x, normal.y, normal.z );
  223. }