implemented lookat camera
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 28 Mar 2018 06:58:31 +0000 (09:58 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 28 Mar 2018 06:58:31 +0000 (09:58 +0300)
scene
src/csgimpl.h
src/csgray.c

diff --git a/scene b/scene
index 6cf673d..ed67f5c 100644 (file)
--- a/scene
+++ b/scene
@@ -1,7 +1,8 @@
 # vi:set ts=4 sts=4 sw=4 ai:
 # vi:set ts=4 sts=4 sw=4 ai:
+# test scene for the CSG raytracer
 csgray_scene {
        viewer {
 csgray_scene {
        viewer {
-               position = [0, 0, 5]
+               position = [0, 1, 5]
                target = [0, 0, 0]
                fov = 50
        }
                target = [0, 0, 0]
                fov = 50
        }
index 19cd43a..8b65783 100644 (file)
@@ -63,7 +63,10 @@ union csg_object {
 struct camera {
        float x, y, z;
        float tx, ty, tz;
 struct camera {
        float x, y, z;
        float tx, ty, tz;
+       float ux, uy, uz;
        float fov;
        float fov;
+
+       float xform[16];
 };
 
 #endif /* CSGIMPL_H_ */
 };
 
 #endif /* CSGIMPL_H_ */
index 150f5fb..c9d2ed2 100644 (file)
@@ -44,12 +44,30 @@ void csg_destroy(void)
 
 void csg_view(float x, float y, float z, float tx, float ty, float tz)
 {
 
 void csg_view(float x, float y, float z, float tx, float ty, float tz)
 {
+       float dir[3];
+       float len;
+
        cam.x = x;
        cam.y = y;
        cam.z = z;
        cam.tx = tx;
        cam.ty = ty;
        cam.tz = tz;
        cam.x = x;
        cam.y = y;
        cam.z = z;
        cam.tx = tx;
        cam.ty = ty;
        cam.tz = tz;
+
+       dir[0] = tx - x;
+       dir[1] = ty - y;
+       dir[2] = tz - z;
+       len = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
+
+       if(1.0f - fabs(ty - y) / len < 1e-6f) {
+               cam.ux = cam.uy = 0.0f;
+               cam.uz = -1.0f;
+       } else {
+               cam.ux = cam.uz = 0.0f;
+               cam.uy = 1.0f;
+       }
+
+       mat4_lookat(cam.xform, x, y, z, tx, ty, tz, cam.ux, cam.uy, cam.uz);
 }
 
 void csg_fov(float fov)
 }
 
 void csg_fov(float fov)
@@ -370,14 +388,15 @@ void csg_render_image(float *pixels, int width, int height)
 
 static void calc_primary_ray(struct ray *ray, int x, int y, int w, int h, float aspect)
 {
 
 static void calc_primary_ray(struct ray *ray, int x, int y, int w, int h, float aspect)
 {
-       /* TODO */
        ray->dx = aspect * ((float)x / (float)w * 2.0f - 1.0f);
        ray->dy = 1.0f - (float)y / (float)h * 2.0f;
        ray->dz = -1.0f / tan(cam.fov * 0.5f);
 
        ray->dx = aspect * ((float)x / (float)w * 2.0f - 1.0f);
        ray->dy = 1.0f - (float)y / (float)h * 2.0f;
        ray->dz = -1.0f / tan(cam.fov * 0.5f);
 
-       ray->x = cam.x;
-       ray->y = cam.y;
-       ray->z = cam.z;
+       ray->x = 0;
+       ray->y = 0;
+       ray->z = 0;
+
+       xform_ray(ray, cam.xform);
 }
 
 static int ray_trace(struct ray *ray, float *col)
 }
 
 static int ray_trace(struct ray *ray, float *col)
@@ -423,7 +442,7 @@ static void shade(float *col, struct ray *ray, struct hit *hit)
                sray.dy = ldir[1];
                sray.dz = ldir[2];
 
                sray.dy = ldir[1];
                sray.dz = ldir[2];
 
-               if(!find_intersection(&sray, &tmphit) || tmphit.t > 1.0f) {
+               if(!find_intersection(&sray, &tmphit) || tmphit.t < 0.000001 || tmphit.t > 1.0f) {
                        if((len = sqrt(ldir[0] * ldir[0] + ldir[1] * ldir[1] + ldir[2] * ldir[2])) != 0.0f) {
                                float s = 1.0f / len;
                                ldir[0] *= s;
                        if((len = sqrt(ldir[0] * ldir[0] + ldir[1] * ldir[1] + ldir[2] * ldir[2])) != 0.0f) {
                                float s = 1.0f / len;
                                ldir[0] *= s;