# T1 Set up Your Project and Output an Image

### Tuortial

{% embed url="<https://raytracing.github.io/books/RayTracingInOneWeekend.html#outputanimage>" %}

### Project Setup

Create a project folder, such as proj\_ray01

Set up one directory, **src**

### Adding main.cpp

Create main.cpp under **src/**

**Put the content in the tutorial into main.cpp to output a test PPM image**

```cpp
// main.cpp
#include <iostream>

int main() {
    // Image
    int image_width = 256;
    int image_height = 256;

    // Render
    std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";

    for (int j = 0; j < image_height; j++) {
        for (int i = 0; i < image_width; i++) {
            auto r = double(i) / (image_width-1);
            auto g = double(j) / (image_height-1);
            auto b = 0.0;

            int ir = int(255.999 * r);
            int ig = int(255.999 * g);
            int ib = int(255.999 * b);

            std::cout << ir << ' ' << ig << ' ' << ib << '\n';
        }
    }
}
```

### Adding CMakeLists.txt

In the root folder, create CMakeLists.txt, put the following into it

```cmake
cmake_minimum_required(VERSION 3.26.0)
project(proj_ray1)

# we are going to use smart pointers
set(CMAKE_CXX_STANDARD 17)

# specify the executable name and 
# add source files to our exectuable programs
add_executable(ray01 src/main.cpp)

# specify include directories
# We are not using external libraries, so don't need it at the moment
# target_include_directories(ray01 PRIVATE include )
```

### Adding .gitignore (optional)

If you like to use Git for the version control of your project, you can copy the .gitignore file from your projects in Semester 1. Here I am uploading mine:

{% file src="<https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FWKRwKdvmg8AV3iVHL1OO%2F.gitignore?alt=media&token=8a0ebf61-279e-4572-a3c4-6f14b0c6956a>" %}

Put .gitignore in your root folder.

### Build Your Project

Use Visual Studio Code or Visual Studio to open your project folder and build your project.

If successful, it will generate an executable named "ray01"

### Run Your Program

Find your program under **build\Debug**

Run your program in a terminal, such as Visual Studio Code terminal or Powershell.

Using the redirection symbol > to redirect the ouputs to a named file as follows (ray01 is the name of my executable, you need to use yours)

```powershell
./ray01 > image.ppm
```

Avoid running it directly from Visual Studio or Visual Studio Code as that will ouput the pixel values to the console.&#x20;

### View image.ppm

image.ppm is in a text format, as follows:

```
P3
256 256
255
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
11 0 0
12 0 0
...
```

#### Use an viewer to view the image

You can use an online viewer such as

{% embed url="<https://www.cs.rhodes.edu/welshc/COMP141_F16/ppmReader.html>" %}

You will see something like the following:

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FplGXmpRUiu2IvpcnUkH9%2Fe361e097-cb2c-4355-919b-f881df9b9d27.png?alt=media&#x26;token=bae3497c-ed99-4751-becb-1c13cbd14c6b" alt="" width="227"><figcaption></figcaption></figure>

### Adding a progress indicator in the loop (optional)

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FuqLuv2hrf7Jhbk0RQfj0%2F85eb1fec-9099-4398-a367-616b69e3242b.png?alt=media&#x26;token=c32fd758-38cf-45e7-bd90-cfdce19c87f2" alt=""><figcaption></figcaption></figure>
