# T4 Casting Rays Through Pixels

Now we are going to calculate the directions based on pixel locations and casting our rays. Here we are not going to use the formula in the lecture but directly calculate the ray direction based on the viewport size and position.

Tutorial

{% embed url="<https://raytracing.github.io/books/RayTracingInOneWeekend.html#rays,asimplecamera,andbackground/sendingraysintothescene>" %}

### Working on main.cpp

### Setting up the Camera and Viewport

Add include header files at the beginning of main.cpp

```cpp
#include "color.h"
#include "ray.h"
#include "vec3.h"
```

Now add contents in main()

#### Cacluate Image dimensions

remove the previous hard coded image\_with and height

```cpp
    // Image
    auto aspect_ratio = 16.0 / 9.0;
    int image_width = 400;

    // Calculate the image height, and ensure that it's at least 1.
    int image_height = int(image_width / aspect_ratio);
    image_height = (image_height < 1) ? 1 : image_height;
```

#### Add Camera Position and Viewport Size

```cpp
    // Camera
    auto focal_length = 1.0;  // distance to the viewport plane
    auto camera_center = point3(0, 0, 0);
    
    // viewport size
    auto viewport_height = 2.0;
    auto viewport_width = viewport_height * (double(image_width)/image_height);
```

#### Calculate Viewport Horizontal and Vertical Vectors

```cpp
    // Calculate the vectors across the horizontal and down the vertical viewport edges.
    auto viewport_u = vec3(viewport_width, 0, 0);
    auto viewport_v = vec3(0, -viewport_height, 0);

    // Calculate the horizontal and vertical delta vectors from pixel to pixel.
    auto pixel_delta_u = viewport_u / image_width;
    auto pixel_delta_v = viewport_v / image_height;
```

#### Calculate the Upper Left point of the Viewport

based on the camera position, the distance from the camera to the viewport plane and the u v directions of the viewport.

```cpp
    // Calculate the location of the upper left pixel.
    auto viewport_upper_left = camera_center
                             - vec3(0, 0, focal_length) 
                             - viewport_u/2 - viewport_v/2;
    auto pixel00_loc = viewport_upper_left + 0.5 * (pixel_delta_u + pixel_delta_v);
```

### Calculate the Ray direction

Now we've got the u v vector pixel, so that we can calcuate the position of each pixel on the viewport. Together with the camera position, we can know the direction of the ray.

Adding the following in the double for loop

```cpp
for (int j = 0; j < image_height; j++) {
        std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
        for (int i = 0; i < image_width; i++) {
            // calculate the pixel position in the world space
            auto pixel_center = pixel00_loc + (i * pixel_delta_u) + (j * pixel_delta_v);
            // calculate the ray direction using the camera position
            auto ray_direction = pixel_center - camera_center;
            // build the ray
            ray r(camera_center, ray_direction);

            color pixel_color = ray_color(r);
            write_color(std::cout, pixel_color);
        }
    }
```

We also added a **ray\_color** function to calcuate he ray color, now we need to define that.

### Calculate ray\_color

Write a ray\_color function before main()

```cpp
#include "color.h"
#include "ray.h"
#include "vec3.h"

#include <iostream>

color ray_color(const ray& r) {
    vec3 unit_direction = unit_vector(r.direction());
    auto a = 0.25*(unit_direction.y() + unit_direction.x() + 2.0);
    return (1.0 - a)*color(1.0, 0.0, 0.0) + a * color(0.0, 0.0, 1.0);
}
```

This function is going to generate a red to blue gradient color from lower left corner to upper right corner (You can try with your own colours, the official tutorial simulates a blue sky background)&#x20;

### Build and Run your program

```powershell
./ray01.exe > image2.ppm
```

You are going to see&#x20;

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FEm9nLgOyIsY7AYtJ4JJ3%2F4b653c2b-9217-4132-a195-f5f6f8449ea3.png?alt=media&#x26;token=d563f264-f62b-48eb-90e5-472b56fa57e6" alt="" width="315"><figcaption></figcaption></figure>
