diff options
Diffstat (limited to 'src/camera.c')
-rw-r--r-- | src/camera.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/camera.c b/src/camera.c new file mode 100644 index 0000000..68376b8 --- /dev/null +++ b/src/camera.c @@ -0,0 +1,37 @@ +#include "camera.h" + +struct camera +camera_init(const int image_width, const int image_height, + const float focal_length) +{ + struct camera cam; + float viewport_height, viewport_width; + + cam.centre.s[0] = 0.0f; + cam.centre.s[1] = 0.0f; + cam.centre.s[2] = 0.0f; + + viewport_height = 2.0f; + viewport_width = + viewport_height * ((float)image_width / (float)image_height); + + cam.pixel_delta_u.s[0] = viewport_width / (float)image_width; + cam.pixel_delta_u.s[1] = 0.0f; + cam.pixel_delta_u.s[2] = 0.0f; + + cam.pixel_delta_v.s[0] = 0.0f; + cam.pixel_delta_v.s[1] = -viewport_height / (float)image_height; + cam.pixel_delta_v.s[2] = 0.0f; + + cam.corner00.s[0] = + cam.centre.s[0] - viewport_width / 2.0f + + 0.5f * (cam.pixel_delta_u.s[0] + cam.pixel_delta_v.s[0]); + cam.corner00.s[1] = + cam.centre.s[1] + viewport_height / 2.0f + + 0.5f * (cam.pixel_delta_u.s[1] + cam.pixel_delta_v.s[1]); + cam.corner00.s[2] = + cam.centre.s[2] - focal_length + + 0.5f * (cam.pixel_delta_u.s[2] + cam.pixel_delta_v.s[2]); + + return cam; +} |