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.1KB

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