From: John Tsiombikas Date: Wed, 28 Mar 2018 06:58:31 +0000 (+0300) Subject: implemented lookat camera X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=csgray;a=commitdiff_plain;h=aab39eb873290ce4b268fe4ae303def6bf932bc0;ds=sidebyside implemented lookat camera --- diff --git a/scene b/scene index 6cf673d..ed67f5c 100644 --- a/scene +++ b/scene @@ -1,7 +1,8 @@ # vi:set ts=4 sts=4 sw=4 ai: +# test scene for the CSG raytracer csgray_scene { viewer { - position = [0, 0, 5] + position = [0, 1, 5] target = [0, 0, 0] fov = 50 } diff --git a/src/csgimpl.h b/src/csgimpl.h index 19cd43a..8b65783 100644 --- a/src/csgimpl.h +++ b/src/csgimpl.h @@ -63,7 +63,10 @@ union csg_object { struct camera { float x, y, z; float tx, ty, tz; + float ux, uy, uz; float fov; + + float xform[16]; }; #endif /* CSGIMPL_H_ */ diff --git a/src/csgray.c b/src/csgray.c index 150f5fb..c9d2ed2 100644 --- a/src/csgray.c +++ b/src/csgray.c @@ -44,12 +44,30 @@ void csg_destroy(void) 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; + + 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) @@ -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) { - /* 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->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) @@ -423,7 +442,7 @@ static void shade(float *col, struct ray *ray, struct hit *hit) 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;