dos port underway
[retroray] / src / rt.c
index e94ac19..f917836 100644 (file)
--- a/src/rt.c
+++ b/src/rt.c
@@ -17,8 +17,20 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
 #include "rt.h"
 
+int ray_object(const cgm_ray *ray, const struct object *obj, struct rayhit *hit)
+{
+       struct csghit csghit;
 
-int ray_object(const cgm_ray *ray, const struct object *obj, struct csghit *hit)
+       if(!ray_object_csg(ray, obj, &csghit)) {
+               return 0;
+       }
+       if(hit) {
+               *hit = csghit.ivlist[0].a;
+       }
+       return 1;
+}
+
+int ray_object_csg(const cgm_ray *ray, const struct object *obj, struct csghit *hit)
 {
        int i, res;
        cgm_ray localray = *ray;
@@ -48,7 +60,7 @@ int ray_object(const cgm_ray *ray, const struct object *obj, struct csghit *hit)
 int ray_sphere(const cgm_ray *ray, const struct sphere *sph, struct csghit *hit)
 {
        int i;
-       float a, b, c, d, sqrt_d, t1, t2, invrad;
+       float a, b, c, d, sqrt_d, t1, t2;/*, invrad;*/
        struct rayhit *rhptr;
 
        a = cgm_vdot(&ray->dir, &ray->dir);
@@ -83,14 +95,16 @@ int ray_sphere(const cgm_ray *ray, const struct sphere *sph, struct csghit *hit)
                hit->ivcount = 1;
                hit->ivlist[0].a.t = t1;
                hit->ivlist[0].b.t = t2;
-               invrad = 1.0f / sph->rad;
+               /*invrad = 1.0f / sph->rad;*/
 
                rhptr = &hit->ivlist[0].a;
                for(i=0; i<2; i++) {
                        cgm_raypos(&rhptr->pos, ray, rhptr->t);
-                       rhptr->norm.x = rhptr->pos.x * invrad;
+                       rhptr->norm = rhptr->pos;
+                       cgm_vnormalize(&rhptr->norm);
+                       /*rhptr->norm.x = rhptr->pos.x * invrad;
                        rhptr->norm.y = rhptr->pos.y * invrad;
-                       rhptr->norm.z = rhptr->pos.z * invrad;
+                       rhptr->norm.z = rhptr->pos.z * invrad;*/
                        rhptr->uv.x = (atan2(rhptr->norm.z, rhptr->norm.x) + CGM_PI) / (2.0 * CGM_PI);
                        rhptr->uv.y = acos(rhptr->norm.y) / CGM_PI;
                        rhptr->obj = (struct object*)sph;
@@ -104,3 +118,22 @@ int ray_csg(const cgm_ray *ray, const struct csgnode *csg, struct csghit *hit)
 {
        return 0;
 }
+
+float ray_object_dist(const cgm_ray *ray, const struct object *obj)
+{
+       /*struct rayhit hit;*/
+       cgm_vec3 norm, pvec;
+
+       /*if(ray_object(ray, obj, &hit)) {
+               return cgm_vdist(&hit.pos, &ray->origin);
+       }*/
+
+       /* if we can't hit the object for some reason, fallback to computing the
+        * distance of obj->pos from the plane perpendicular to the ray at the origin
+        */
+       norm = ray->dir;
+       /*cgm_vnormalize(&norm);*/
+       pvec = obj->pos;
+       cgm_vsub(&pvec, &ray->origin);
+       return cgm_vdot(&pvec, &norm);
+}