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.

geotypes.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "geotypes.h"
  23. namespace segl {
  24. Sphere::Sphere(Punkt3D _pos, float _radius) {
  25. pos = _pos;
  26. radius = _radius;
  27. }
  28. Sphere::Sphere() {
  29. radius = 1.0f;
  30. }
  31. void Sphere::set(Punkt3D _pos, float _radius) {
  32. pos = _pos;
  33. radius = _radius;
  34. }
  35. bool Sphere::collision(const Sphere &s) const {
  36. return ((pos-s.pos)*(pos-s.pos))<((radius+s.radius)*(radius+s.radius));
  37. }
  38. bool Sphere::collision(const Ray &r) const {
  39. return true;
  40. }
  41. bool Sphere::collision(const Box & b) const {
  42. return true;
  43. }
  44. bool Sphere::collision(const Plane &p) const {
  45. return true;
  46. }
  47. bool Sphere::inSphere(Punkt3D p) const {
  48. return abs(pos-p)<=radius;
  49. }
  50. Punkt3D Sphere::getPos() const {
  51. return pos;
  52. }
  53. Ray::Ray() {
  54. dir.set(0.0f, 1.0f, 0.0f);
  55. }
  56. Ray::Ray(Punkt3D _pos, Punkt3D _dir) {
  57. set(_pos, _dir);
  58. }
  59. void Ray::set(Punkt3D _pos, Punkt3D _dir) {
  60. pos = _pos;
  61. dir = _dir;
  62. }
  63. Punkt3D Ray::get(float x) {
  64. return pos + dir*x;
  65. }
  66. // TODO: Heavy Testing
  67. bool Ray::onRay(Punkt3D p, int rnd) {
  68. float r1 = 0.0f, r2 = 0.0f, r3 = 0.0f;
  69. short fcount = 0;
  70. bool g1=true, g2=true, g3=true;
  71. Punkt3D f = p-pos;
  72. if(dir.x==0.0f) {
  73. if(f.x!=0.0f)
  74. return false;
  75. g1 = false;
  76. fcount++;
  77. } else
  78. r1 = f.x / dir.x;
  79. if(dir.y==0.0f) {
  80. if(f.y!=0.0f)
  81. return false;
  82. g2 = false;
  83. fcount++;
  84. } else
  85. r2 = f.y / dir.y;
  86. if(dir.z==0.0f) {
  87. if(f.z!=0.0f)
  88. return false;
  89. g2 = false;
  90. fcount++;
  91. } else
  92. r2 = f.z / dir.z;
  93. if(fcount>=2)
  94. return true;
  95. if(rnd>=0) {
  96. // TODO:Implement rounding
  97. // r1 = round(r1, rnd);
  98. // r2 = round(r2, rnd);
  99. // r3 = round(r3, rnd);
  100. }
  101. if(g1)
  102. return (r2 == r3);
  103. else if(g2)
  104. return (r1 == r3);
  105. else if(g3)
  106. return (r1 == r2);
  107. else
  108. return (r1 == r2 == r3);
  109. }
  110. float Ray::dist(Punkt3D p) {
  111. return abs(p - get( getParam(p) ));
  112. }
  113. float Ray::getParam(Punkt3D p, bool onray) {
  114. if(onray) {
  115. if(!onRay(p))
  116. return 0.0f;
  117. }
  118. return -((pos-p)*dir) / (dir*dir);
  119. }
  120. bool Ray::collision(const Sphere &s) const {
  121. return s.collision(*this);
  122. }
  123. bool Ray::collision(const Ray &r) const {
  124. return true;
  125. }
  126. bool Ray::collision(const Box & b) const {
  127. return true;
  128. }
  129. bool Ray::collision(const Plane &p) const {
  130. return true;
  131. }
  132. Box::Box() {
  133. max.set(1.0f, 1.0f, 1.0f);
  134. }
  135. Box::Box(Punkt3D _min, Punkt3D _max) {
  136. min = _min;
  137. max = _max;
  138. }
  139. bool Box::collision(const Sphere &s) const {
  140. return s.collision(*this);
  141. }
  142. bool Box::collision(const Ray &r) const {
  143. return r.collision(*this);
  144. }
  145. bool Box::collision(const Box & b) const {
  146. return true;
  147. }
  148. bool Box::collision(const Plane &p) const {
  149. return true;
  150. }
  151. Plane::Plane() {
  152. norm.set(0.0f, 1.0f, 0.0f);
  153. }
  154. Plane::Plane(Punkt3D _pos, Punkt3D _norm) {
  155. pos = _pos;
  156. norm = _norm;
  157. }
  158. Plane::Plane(float x, float y, float z, float a) {
  159. // TODO: Implementation (if not too lazy)
  160. norm.set(x, y, z);
  161. norm.normalize();
  162. }
  163. bool Plane::collision(const Sphere &s) const {
  164. return s.collision(*this);
  165. }
  166. bool Plane::collision(const Ray &r) const {
  167. return r.collision(*this);
  168. }
  169. bool Plane::collision(const Box & b) const {
  170. return b.collision(*this);
  171. }
  172. bool Plane::collision(const Plane &p) const {
  173. return true;
  174. }
  175. float Plane::dist(Punkt3D p) const {
  176. }
  177. } // namespace segl