summaryrefslogtreecommitdiff
path: root/src/camera.c
blob: 68376b8cbf1c322fc08653a14f76c8f256456e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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;
}