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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 (r.dist(pos) <= radius);
  40. }
  41. bool Sphere::collision(const Box & b) const {
  42. return true;
  43. }
  44. bool Sphere::collision(const Plane &p) const {
  45. return p.dist(pos) <= radius;
  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. int Sphere::getIntersectionParam(const Ray &ray, float *param1, float *param2) {
  54. int numerg;
  55. // ax^2 + bx + c = 0
  56. float a = ray.dir * ray.dir;
  57. float b = 2 * ray.dir * (ray.pos - pos);
  58. float c = ray.pos * ray.pos - 2 * ray.pos * pos + pos * pos - radius * radius;
  59. float p = b/a;
  60. float q = c/a;
  61. if((p*p)/4.0f<q) {
  62. numerg = 0;
  63. } else if((p*p)/4.0f==q) {
  64. numerg = 1;
  65. if(param1!=0) {
  66. *param1 = -p/2.0f;
  67. }
  68. } else {
  69. numerg = 2;
  70. if(param1!=0 || param2!=0) {
  71. if(param1!=0) {
  72. *param1 = -p/2.0f + sqrt(((p*p)/4.0f)-q);
  73. }
  74. if(param2!=0) {
  75. *param2 = -p/2.0f - sqrt(((p*p)/4.0f)-q);
  76. }
  77. }
  78. }
  79. return numerg;
  80. }
  81. int Sphere::getIntersectionPoints(const Ray &ray, segl::Punkt3D *a, segl::Punkt3D *b) {
  82. float pa, pb;
  83. int numerg = getIntersectionParam(ray, &pa, &pb);
  84. if(a)
  85. *a = ray.get(pa);
  86. if(b)
  87. *b = ray.get(pb);
  88. return numerg;
  89. }
  90. Ray::Ray() {
  91. dir.set(0.0f, 1.0f, 0.0f);
  92. }
  93. Ray::Ray(const Punkt3D &_pos, const Punkt3D &_dir) {
  94. set(_pos, _dir);
  95. }
  96. void Ray::set(const Punkt3D &_pos, const Punkt3D &_dir) {
  97. pos = _pos;
  98. dir = _dir;
  99. }
  100. void Ray::setFromPoints(const Punkt3D &a, const Punkt3D &b) {
  101. pos = a;
  102. dir = b - a;
  103. }
  104. Punkt3D Ray::get(float x) const {
  105. return pos + dir*x;
  106. }
  107. // TODO: Heavy Testing
  108. bool Ray::onRay(Punkt3D p, int rnd) const {
  109. float r1 = 0.0f, r2 = 0.0f, r3 = 0.0f;
  110. short fcount = 0;
  111. bool g1=true, g2=true, g3=true;
  112. Punkt3D f = p-pos;
  113. if(dir.x==0.0f) {
  114. if(f.x!=0.0f)
  115. return false;
  116. g1 = false;
  117. fcount++;
  118. } else
  119. r1 = f.x / dir.x;
  120. if(dir.y==0.0f) {
  121. if(f.y!=0.0f)
  122. return false;
  123. g2 = false;
  124. fcount++;
  125. } else
  126. r2 = f.y / dir.y;
  127. if(dir.z==0.0f) {
  128. if(f.z!=0.0f)
  129. return false;
  130. g2 = false;
  131. fcount++;
  132. } else
  133. r2 = f.z / dir.z;
  134. if(fcount>=2)
  135. return true;
  136. if(rnd>=0) {
  137. // TODO:Implement rounding
  138. // r1 = round(r1, rnd);
  139. // r2 = round(r2, rnd);
  140. // r3 = round(r3, rnd);
  141. }
  142. if(g1)
  143. return (r2 == r3);
  144. else if(g2)
  145. return (r1 == r3);
  146. else if(g3)
  147. return (r1 == r2);
  148. else
  149. return ((r1 == r2) == r3);
  150. }
  151. float Ray::dist(Punkt3D p) const {
  152. return abs(p - get( getParam(p) ));
  153. }
  154. float Ray::getParam(Punkt3D p, bool onray) const {
  155. if(onray) {
  156. if(!onRay(p))
  157. return 0.0f;
  158. }
  159. return -((pos-p)*dir) / (dir*dir);
  160. }
  161. bool Ray::collision(const Sphere &s) const {
  162. return s.collision(*this);
  163. }
  164. bool Ray::collision(const Ray &r) const {
  165. return true;
  166. }
  167. bool Ray::collision(const Box & b) const {
  168. return true;
  169. }
  170. bool Ray::collision(const Plane &p) const {
  171. return true;
  172. }
  173. Box::Box() {
  174. max.set(1.0f, 1.0f, 1.0f);
  175. }
  176. Box::Box(Punkt3D _min, Punkt3D _max) {
  177. min = _min;
  178. max = _max;
  179. }
  180. bool Box::collision(const Sphere &s) const {
  181. return s.collision(*this);
  182. }
  183. bool Box::collision(const Ray &r) const {
  184. return r.collision(*this);
  185. }
  186. bool Box::collision(const Box & b) const {
  187. return true;
  188. }
  189. bool Box::collision(const Plane &p) const {
  190. return true;
  191. }
  192. Plane::Plane() {
  193. norm.set(0.0f, 1.0f, 0.0f);
  194. }
  195. Plane::Plane(Punkt3D _pos, Punkt3D _norm) {
  196. pos = _pos;
  197. norm = _norm;
  198. }
  199. Plane::Plane(float x, float y, float z, float a) {
  200. // TODO: Implementation (if not too lazy)
  201. norm.set(x, y, z);
  202. norm.normalize();
  203. }
  204. bool Plane::collision(const Sphere &s) const {
  205. return s.collision(*this);
  206. }
  207. bool Plane::collision(const Ray &r) const {
  208. return r.collision(*this);
  209. }
  210. bool Plane::collision(const Box & b) const {
  211. return b.collision(*this);
  212. }
  213. bool Plane::collision(const Plane &p) const {
  214. return true;
  215. }
  216. bool Plane::onPlane(const Punkt3D &p) const {
  217. return dist(p) == 0.0f;
  218. }
  219. bool Plane::getIntersectionPoint(const Ray &r, Punkt3D *p) const {
  220. if(r.dir.calcAngle(norm) && !onPlane(r.pos))
  221. return false;
  222. if(!p)
  223. return true;
  224. float param = (pos*norm - r.pos*norm) / (r.dir*norm);
  225. *p = r.get(param);
  226. return true;
  227. }
  228. float Plane::dist(Punkt3D p) const {
  229. return 0.0f;
  230. }
  231. } // namespace segl