T5 Position and Orient the Camera (Milestone 3)

T5.1 Use Field-of-View angle (camera.h)

Now we are going to use the fov angle to control the camera zooming.

In camera.h, add a private memboer vfov.

In initialise(), calculate the viewport height based on vfov

class camera {
public:
    double aspect_ratio = 1.0;  // Ratio of image width over height
    int    image_width  = 100;  // Rendered image width in pixel count
    int    samples_per_pixel = 10; 
    int    max_depth = 10;   // Maximum number of ray bounces into scene

    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    double vfov = 90;  // Vertical view angle (field of view)
    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
private:
    // ....
    
    void initialize() {
        image_height = int(image_width / aspect_ratio);
        image_height = (image_height < 1) ? 1 : image_height;

        pixel_samples_scale = 1.0 / samples_per_pixel;

        center = point3(0, 0, 0);

        // Determine viewport dimensions.
        auto focal_length = 1.0;
        
        //auto viewport_height = 2.0;
        // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        auto theta = degrees_to_radians(vfov);
        auto h = std::tan(theta/2);
        auto viewport_height = 2 * h * focal_length;
        // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        auto viewport_width = viewport_height * (double(image_width)/image_height);
        //...
   }
   //...
};

Test in main() (main.cpp)

in main(), comment out those glass and metal sphere materials and objects, use the following simple scene in main() to test the viewing angle.

Build and Run, You are expected to see the following output:

The following is the output when setting fov to 100

T5.2 Position and Orient the Camera (camera.h)

Add lookfrom, lookat, vup, and u, v, w

Now we are going to set the camera frame using the camera position (lookfrom), the looking center (lookat), and the up vector (vup), in the same way as we are doing with glm::lookAt().

The difference is that we are going to calculate the camera frame vectors u, v, w, using vector cross product by ourselves.

First, add lookfrom, lookat, and vup in class camera's public members, after fov.

In the private section, add u, v, w as the camera's major axes.

Revise initialise() to calculate u, v, w of the camera frame from lookfrom, lookat, vup

Revise main() to test camera settings

Restore to the previous glass and metal sphere scene and set camera parameters

It looks a bit noisy

If we change the vfov of the camera to 20 in main(), you are going to see the following:

T5.3 Defocus

choosing random points from the defocus disk

add random_in_unit_disk() at the end of vec3.h

add defocus members in class camera

Add a public method defocus_disk_sample() in class camera

revise camera's initialise()

revise camera's get_ray() to change the ray origin a bit

Set camera defocus parameters in main()

Build and Run

If successful, you are going to see outputs similar to the following:

It looks darker because I am using Gamma correction.

Last updated