Camera view geometry
This commit is contained in:
parent
0a0080ce80
commit
b8bdd91e40
2 changed files with 23 additions and 20 deletions
|
@ -5,29 +5,31 @@
|
||||||
|
|
||||||
class camera {
|
class camera {
|
||||||
public:
|
public:
|
||||||
camera() {
|
camera(double vfov /*vertical field-of-view in degrees*/, double aspect_ratio) {
|
||||||
auto aspect_ratio = 16.0 / 9.0;
|
|
||||||
auto viewport_height = 2.0;
|
|
||||||
auto viewport_width = aspect_ratio * viewport_height;
|
|
||||||
auto focal_length = 1.0;
|
|
||||||
|
|
||||||
origin = point3(0, 0, 0);
|
auto theta = degrees_to_radians(vfov);
|
||||||
horizontal = vec3(viewport_width, 0.0, 0.0);
|
auto h = tan(theta / 2);
|
||||||
vertical = vec3(0.0, viewport_height, 0.0);
|
auto viewport_height = 2.0 * h;
|
||||||
lower_left_corner =
|
auto viewport_width = aspect_ratio * viewport_height;
|
||||||
origin - horizontal / 2 - vertical / 2 - vec3(0, 0, focal_length);
|
auto focal_length = 1.0;
|
||||||
}
|
|
||||||
|
|
||||||
ray get_ray(double u, double v) const {
|
origin = point3(0, 0, 0);
|
||||||
return ray(origin,
|
horizontal = vec3(viewport_width, 0.0, 0.0);
|
||||||
lower_left_corner + u * horizontal + v * vertical - origin);
|
vertical = vec3(0.0, viewport_height, 0.0);
|
||||||
}
|
lower_left_corner =
|
||||||
|
origin - horizontal / 2 - vertical / 2 - vec3(0, 0, focal_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
ray get_ray(double u, double v) const {
|
||||||
|
return ray(origin,
|
||||||
|
lower_left_corner + u * horizontal + v * vertical - origin);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
point3 origin;
|
point3 origin;
|
||||||
point3 lower_left_corner;
|
point3 lower_left_corner;
|
||||||
vec3 horizontal;
|
vec3 horizontal;
|
||||||
vec3 vertical;
|
vec3 vertical;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CAMERA_H_
|
#endif // CAMERA_H_
|
||||||
|
|
|
@ -54,11 +54,12 @@ int main() {
|
||||||
make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
|
make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
|
||||||
world.add(make_shared<sphere>(point3(0.0, 0.0, -1.0), 0.5, material_center));
|
world.add(make_shared<sphere>(point3(0.0, 0.0, -1.0), 0.5, material_center));
|
||||||
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, material_left));
|
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, material_left));
|
||||||
|
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), -0.4, material_left));
|
||||||
world.add(make_shared<sphere>(point3(1.0, 0.0, -1.0), 0.5, material_right));
|
world.add(make_shared<sphere>(point3(1.0, 0.0, -1.0), 0.5, material_right));
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
|
|
||||||
camera cam;
|
camera cam(90, aspect_ratio);
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue