diff options
author | Tavian Barnes <tavianator@gmail.com> | 2010-10-20 18:52:59 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2010-10-20 18:52:59 -0400 |
commit | 59481d4e4421cc7d0610d49c6dbec720de1f11ea (patch) | |
tree | 7d72d30229073c3997b5fe89faa657e2430f8102 /libdimension | |
parent | 13f34392c72f99134342b5a58b14c02e07f0e67b (diff) | |
download | dimension-59481d4e4421cc7d0610d49c6dbec720de1f11ea.tar.xz |
Add cone support to libdimension.
Diffstat (limited to 'libdimension')
-rw-r--r-- | libdimension/cylinder.c | 58 | ||||
-rw-r--r-- | libdimension/dimension/objects.h | 4 |
2 files changed, 45 insertions, 17 deletions
diff --git a/libdimension/cylinder.c b/libdimension/cylinder.c index 159d520..62c1476 100644 --- a/libdimension/cylinder.c +++ b/libdimension/cylinder.c @@ -32,17 +32,29 @@ static bool dmnsn_cylinder_intersection_fn(const dmnsn_object *cylinder, static bool dmnsn_cylinder_inside_fn(const dmnsn_object *cylinder, dmnsn_vector point); +/* Payload type */ +typedef struct dmnsn_cylinder_payload { + double r1, r2; + bool open; +} dmnsn_cylinder_payload; + /* Allocate a new cylinder object */ dmnsn_object * -dmnsn_new_cylinder(bool open) +dmnsn_new_cylinder(double r1, double r2, bool open) { dmnsn_object *cylinder = dmnsn_new_object(); cylinder->intersection_fn = &dmnsn_cylinder_intersection_fn; cylinder->inside_fn = &dmnsn_cylinder_inside_fn; cylinder->bounding_box.min = dmnsn_new_vector(-1.0, -1.0, -1.0); cylinder->bounding_box.max = dmnsn_new_vector(1.0, 1.0, 1.0); - /* (bool)cyliner->ptr == open */ - cylinder->ptr = open ? cylinder : NULL; + + dmnsn_cylinder_payload *payload + = dmnsn_malloc(sizeof(dmnsn_cylinder_payload)); + payload->r1 = r1; + payload->r2 = r2; + payload->open = open; + cylinder->ptr = payload; + cylinder->free_fn = &dmnsn_free; return cylinder; } @@ -52,12 +64,17 @@ dmnsn_cylinder_intersection_fn(const dmnsn_object *cylinder, dmnsn_line line, dmnsn_intersection *intersection) { dmnsn_line l = dmnsn_transform_line(cylinder->trans_inv, line); + dmnsn_cylinder_payload *payload = cylinder->ptr; + double r1 = payload->r1, r2 = payload->r2; - /* Solve (x0 + nx*t)^2 + (z0 + nz*t)^2 == 1 */ + /* Solve (x0 + nx*t)^2 + (z0 + nz*t)^2 + == (((r2 - r1)*(y0 + ny*t) + r1 + r2)/2)^2 */ double a, b, c, t; - a = l.n.x*l.n.x + l.n.z*l.n.z; - b = 2.0*(l.n.x*l.x0.x + l.n.z*l.x0.z); - c = l.x0.x*l.x0.x + l.x0.z*l.x0.z - 1.0; + a = l.n.x*l.n.x + l.n.z*l.n.z - l.n.y*l.n.y*(r2 - r1)*(r2 - r1)/4.0; + b = 2.0*(l.n.x*l.x0.x + l.n.z*l.x0.z) + - l.n.y*(r2 - r1)*(l.x0.y*(r2 - r1) + r2 + r1)/2.0; + c = l.x0.x*l.x0.x + l.x0.z*l.x0.z + - (l.x0.y*(r2 - r1) + r2 + r1)*(l.x0.y*(r2 - r1) + r2 + r1)/4; if (b*b - 4.0*a*c >= 0.0) { t = (-b - sqrt(b*b - 4.0*a*c))/(2.0*a); @@ -68,31 +85,40 @@ dmnsn_cylinder_intersection_fn(const dmnsn_object *cylinder, dmnsn_line line, p = dmnsn_line_point(l, t); } - if (!cylinder->ptr && l.n.y) { + if (!payload->open && l.n.y) { /* Test for cap intersections */ double tcap1 = (-1.0 - l.x0.y)/l.n.y; double tcap2 = (+1.0 - l.x0.y)/l.n.y; - double tcap; + double tcap, r; dmnsn_vector norm; if (tcap1 < tcap2) { tcap = tcap1; + r = r1; norm = dmnsn_new_vector(0.0, -1.0, 0.0); } else { tcap = tcap2; + r = r2; norm = dmnsn_new_vector(0.0, 1.0, 0.0); } dmnsn_vector pcap = dmnsn_line_point(l, tcap); - if (tcap < 0.0 || pcap.x*pcap.x + pcap.z*pcap.z >= 1.0) { - tcap = dmnsn_max(tcap1, tcap2); + if (tcap < 0.0 || pcap.x*pcap.x + pcap.z*pcap.z >= r*r) { + if (tcap2 <= tcap1) { + tcap = tcap1; + r = r1; + norm = dmnsn_new_vector(0.0, -1.0, 0.0); + } else { + tcap = tcap2; + r = r2; + norm = dmnsn_new_vector(0.0, 1.0, 0.0); + } pcap = dmnsn_line_point(l, tcap); - norm = dmnsn_vector_negate(norm); } if (tcap >= 0.0 && (tcap < t || p.y <= -1.0 || p.y >= 1.0) - && pcap.x*pcap.x + pcap.z*pcap.z < 1.0) + && pcap.x*pcap.x + pcap.z*pcap.z < r*r) { intersection->ray = line; intersection->t = tcap; @@ -104,10 +130,12 @@ dmnsn_cylinder_intersection_fn(const dmnsn_object *cylinder, dmnsn_line line, } if (t >= 0.0 && p.y > -1.0 && p.y < 1.0) { - p.y = 0; + dmnsn_vector norm = dmnsn_vector_normalize( + dmnsn_new_vector(p.x, -(r2 - r1)*sqrt(p.x*p.x + p.z*p.z)/2.0, p.z) + ); intersection->ray = line; intersection->t = t; - intersection->normal = dmnsn_transform_normal(cylinder->trans, p); + intersection->normal = dmnsn_transform_normal(cylinder->trans, norm); intersection->texture = cylinder->texture; intersection->interior = cylinder->interior; return true; diff --git a/libdimension/dimension/objects.h b/libdimension/dimension/objects.h index 8f30eae..91c9127 100644 --- a/libdimension/dimension/objects.h +++ b/libdimension/dimension/objects.h @@ -36,7 +36,7 @@ dmnsn_object *dmnsn_new_sphere(void); /* A cube, axis-aligned, from (-1, -1, -1) to (1, 1, 1) */ dmnsn_object *dmnsn_new_cube(void); -/* A cylinder, of radius 1, from y = -1 to y = 1 */ -dmnsn_object *dmnsn_new_cylinder(bool open); +/* A cylinder/cone, from r = r1 at y = -1, to r = r2 at y = 1 */ +dmnsn_object *dmnsn_new_cylinder(double r1, double r2, bool open); #endif /* DIMENSION_OBJECTS_H */ |