goat3dgfx

view src/geom.cc @ 21:7c593721547f

integrated support for the multiple animation system of libanim
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Dec 2013 11:59:32 +0200
parents 1873dfd13f2d
children 0ac499409edd
line source
1 #include <algorithm>
2 #include <float.h>
3 #include "geom.h"
4 #include "logger.h"
6 using namespace goatgfx;
8 GeomObject::~GeomObject()
9 {
10 }
13 Sphere::Sphere()
14 {
15 radius = 1.0;
16 }
18 Sphere::Sphere(const Vector3 &cent, float radius)
19 : center(cent)
20 {
21 this->radius = radius;
22 }
24 void Sphere::set_union(const GeomObject *obj1, const GeomObject *obj2)
25 {
26 const Sphere *sph1 = dynamic_cast<const Sphere*>(obj1);
27 const Sphere *sph2 = dynamic_cast<const Sphere*>(obj2);
29 if(!sph1 || !sph2) {
30 error_log("Sphere::set_union: arguments must be spheres");
31 return;
32 }
34 float dist = (sph1->center - sph2->center).length();
35 float surf_dist = dist - (sph1->radius + sph2->radius);
36 float d1 = sph1->radius + surf_dist / 2.0;
37 float d2 = sph2->radius + surf_dist / 2.0;
38 float t = d1 / (d1 + d2);
40 if(t < 0.0) t = 0.0;
41 if(t > 1.0) t = 1.0;
43 center = sph1->center * t + sph2->center * (1.0 - t);
44 radius = std::max(dist * t + sph2->radius, dist * (1.0f - t) + sph1->radius);
45 }
47 void Sphere::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
48 {
49 error_log("Sphere::intersection undefined\n");
50 }
52 bool Sphere::intersect(const Ray &ray, HitPoint *hit) const
53 {
54 float a = dot_product(ray.dir, ray.dir);
55 float b = 2.0 * ray.dir.x * (ray.origin.x - center.x) +
56 2.0 * ray.dir.y * (ray.origin.y - center.y) +
57 2.0 * ray.dir.z * (ray.origin.z - center.z);
58 float c = dot_product(ray.origin, ray.origin) + dot_product(center, center) -
59 2.0 * dot_product(ray.origin, center) - radius * radius;
61 float discr = b * b - 4.0 * a * c;
62 if(discr < 1e-4) {
63 return false;
64 }
66 float sqrt_discr = sqrt(discr);
67 float t0 = (-b + sqrt_discr) / (2.0 * a);
68 float t1 = (-b - sqrt_discr) / (2.0 * a);
70 if(t0 < 1e-4)
71 t0 = t1;
72 if(t1 < 1e-4)
73 t1 = t0;
75 float t = t0 < t1 ? t0 : t1;
76 if(t < 1e-4) {
77 return false;
78 }
80 // fill the HitPoint structure
81 if(hit) {
82 hit->obj = this;
83 hit->dist = t;
84 hit->pos = ray.origin + ray.dir * t;
85 hit->normal = (hit->pos - center) / radius;
86 }
87 return true;
88 }
91 AABox::AABox()
92 {
93 }
95 AABox::AABox(const Vector3 &vmin, const Vector3 &vmax)
96 : min(vmin), max(vmax)
97 {
98 }
100 void AABox::set_union(const GeomObject *obj1, const GeomObject *obj2)
101 {
102 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
103 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
105 if(!box1 || !box2) {
106 error_log("AABox::set_union: arguments must be AABoxes too\n");
107 return;
108 }
110 min.x = std::min(box1->min.x, box2->min.x);
111 min.y = std::min(box1->min.y, box2->min.y);
112 min.z = std::min(box1->min.z, box2->min.z);
114 max.x = std::max(box1->max.x, box2->max.x);
115 max.y = std::max(box1->max.y, box2->max.y);
116 max.z = std::max(box1->max.z, box2->max.z);
117 }
119 void AABox::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
120 {
121 const AABox *box1 = dynamic_cast<const AABox*>(obj1);
122 const AABox *box2 = dynamic_cast<const AABox*>(obj2);
124 if(!box1 || !box2) {
125 error_log("AABox::set_intersection: arguments must be AABoxes too\n");
126 return;
127 }
129 for(int i=0; i<3; i++) {
130 min[i] = std::max(box1->min[i], box2->min[i]);
131 max[i] = std::min(box1->max[i], box2->max[i]);
133 if(max[i] < min[i]) {
134 max[i] = min[i];
135 }
136 }
137 }
139 bool AABox::intersect(const Ray &ray, HitPoint *hit) const
140 {
141 Vector3 param[2] = {min, max};
142 Vector3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
143 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
145 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
146 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
147 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
148 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
150 if(tmin > tymax || tymin > tmax) {
151 return false;
152 }
153 if(tymin > tmin) {
154 tmin = tymin;
155 }
156 if(tymax < tmax) {
157 tmax = tymax;
158 }
160 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
161 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
163 if(tmin > tzmax || tzmin > tmax) {
164 return false;
165 }
166 if(tzmin > tmin) {
167 tmin = tzmin;
168 }
169 if(tzmax < tmax) {
170 tmax = tzmax;
171 }
173 float t = tmin < 1e-4 ? tmax : tmin;
174 if(t >= 1e-4) {
176 if(hit) {
177 hit->obj = this;
178 hit->dist = t;
179 hit->pos = ray.origin + ray.dir * t;
181 float min_dist = FLT_MAX;
182 Vector3 offs = min + (max - min) / 2.0;
183 Vector3 local_hit = hit->pos - offs;
185 static const Vector3 axis[] = {
186 Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)
187 };
188 //int tcidx[][2] = {{2, 1}, {0, 2}, {0, 1}};
190 for(int i=0; i<3; i++) {
191 float dist = fabs((max[i] - offs[i]) - fabs(local_hit[i]));
192 if(dist < min_dist) {
193 min_dist = dist;
194 hit->normal = axis[i] * (local_hit[i] < 0.0 ? 1.0 : -1.0);
195 //hit->texcoord = Vector2(hit->pos[tcidx[i][0]], hit->pos[tcidx[i][1]]);
196 }
197 }
198 }
199 return true;
200 }
201 return false;
203 }
205 Plane::Plane()
206 : normal(0.0, 1.0, 0.0)
207 {
208 }
210 Plane::Plane(const Vector3 &p, const Vector3 &norm)
211 : pt(p)
212 {
213 normal = norm.normalized();
214 }
216 Plane::Plane(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3)
217 : pt(p1)
218 {
219 normal = cross_product(p2 - p1, p3 - p1).normalized();
220 }
222 Plane::Plane(const Vector3 &normal, float dist)
223 {
224 this->normal = normal.normalized();
225 pt = this->normal * dist;
226 }
228 void Plane::set_union(const GeomObject *obj1, const GeomObject *obj2)
229 {
230 error_log("Plane::set_union undefined\n");
231 }
233 void Plane::set_intersection(const GeomObject *obj1, const GeomObject *obj2)
234 {
235 error_log("Plane::set_intersection undefined\n");
236 }
238 bool Plane::intersect(const Ray &ray, HitPoint *hit) const
239 {
240 float ndotdir = dot_product(normal, ray.dir);
241 if(fabs(ndotdir) < 1e-4) {
242 return false;
243 }
245 if(hit) {
246 Vector3 ptdir = pt - ray.origin;
247 float t = dot_product(normal, ptdir) / ndotdir;
249 hit->pos = ray.origin + ray.dir * t;
250 hit->normal = normal;
251 hit->obj = this;
252 }
253 return true;
254 }