Quick start to coding - 012

Pierre Marchand

Team-project POEMS, Inria, ENSTA and IPP

Organisation

Important information

  • Bring your laptop.
  • Configure your Internet connexion via eduroam (Infos Paris-SaclayInfos ENSTA).
  • Make sure you have an account at ENSTA (contact me otherwise).

Schedule

Date Room Location
Wed. 11/09 (14:00-17:00) 2151 ENSTA
Fri. 13/09 (14:00-17:00) 2151 ENSTA
Wed. 18/09 (14:00-17:00) 2151 ENSTA
Fri. 20/09 (14:00-17:00) 2151 ENSTA
Wed. 25/09 (14:00-17:00) 2151 ENSTA
Fri. 27/09 (14:00-17:00) 2151 ENSTA
About this course

Goal

  • Tools to help you code (1 or 2 lectures)
  • Basics of modern C++ (4-5 lectures)

Remarks

  • This is course is not graded → 🎉🎉🎉
  • This course is very short → It is only a starting point from which you can start learning
  • Third time this course is given → Constructive comments are welcome
Learning content

Everything is available on my webpage:

You can find

  • these slides (see Teaching)
    • you can open them in a browser to follow this course
  • Computer tools (see Project, or these slides)
    • contains introductions to useful/necessary tools for coding
    • pointers to other resources are also given
    • available as a website or a pdf file
  • C++ Quick Start (see Teaching, or these slides)
    • quick introduction to C++
    • pointers to other resources are also given
    • available as a website or a pdf file
Setup requirements

Unix-like systems? (Linux, macOS, ...)

→ you are good to go 👍👍👍

Windows?

  • Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11

    → Install WSL 2

  • a x64 system with Version 1903 or later, with Build 18362 or later

    → Install manually WSL 2

  • Otherwise, I mention other alternatives here.

Remark: To check your version and build number, select Windows logo key + R, type winver, select OK.

Source code editor

VS Code

You can use any source code editor you want, in doubts, I recommend VS Code

  • free
  • cross-platform
  • multipurpose
  • extensible via extensions

See here for more information.

Advices

  • Take the time to learn how to use it (see here for references)
  • On Windows, use VS Code with the extension Remote WSL

Computer tools

Use this document

Basic tools

Before learning about C++, or any programming languages, it is important to introduce the following tools:

  • bash
  • git
  • SSH

Motivation

  • The tools we will see are useful with all languages (Python, C++, LaTeX, ...)
  • These are transversal skills that you can put forward and develop in your future career.
Terminal/bash

Why?

  • Alternative to GUI which allows scripting and uses less resources
  • Potentially more efficient than manipulating graphical elements
  • Necessary to access remote servers (i.e, supercomputers)

Filesystem

Files are organized in a hierarchical tree structure.

To your terminal!

hierarchical tree structure

git

Decentralized version control system for source code

Several use cases:

  • with : versioning,
  • with with : versioning and backup,
  • with with : versioning, backup and synchronization,
  • with with : versioning, backup, synchronization and collaborative work.

To your terminal!

SSH

The Secure Shell (SSH) protocol is a network protocol that allows secure access to systems running an SSH server over a network (e.g. the Internet)

SSH is widely used, examples of applications are

  • accessing severs remotely (student workspace hosted by the school, supercomputers, git servers, remote workstation, ...) with or without password,
  • transferring or syncing files,
  • mounting a directory on a remote server as a local filesystem.

To your terminal!

C++ Quick Start

Use this document

What is C++?

C++ was first developed as an extension to C, adding for example classes, but it evolved since then:

C++98 | C++11 | C++14 | C++17 | C++20 | C++23 | (C++26)

Properties of C++

  • C++ is a compiled language (like C and fortran): you need a compiler
  • C++ is statically typed (the compiler checks the types of every value and if all the operations are valid)
  • Each standard of the language is composed by two main components :

Modern C++

We will rely on features and best practices from C++17 and the C++ standard library.

A project to learn and practice

You don't learn how to program just by watching, you need to practice!

What I suggest is to iteratively

The plan

Next slides describe

  • necessary sections from C++ Quick Start to read,
  • steps to move forward with your project.

Create an image

We first need to be able to write an image from RGB data.

  • Using the PPM image format, create a file from your main function containing an arbitrary image. Start with one color, and then try to make something colorful.
    • Use this VSCode extension to visualize such images.
    • Define the variable image_width and image_height.
  • Put your previous code in a function:

    void render_image(double aspect_ratio, int image_width, std::string image_path)
    
    • Use separate compilation, you should have the following files: main.cpp calling render_image, render_images.hpp and render_images.cpp containing respectively the declaration and definition of the function.
Utility classes for geometry

Required: Array, Classes

  • Define a class vec3 with

    • member data: std::array<3,double>
    • member functions: accessors for coordinates, operator-, operator=-, operator=+, operator=*, operator=/, length and length_squared.
    • free functions using this class: operator-, operator+,operator*, operator/, dot, cross and unit_vector.
  • Test everything! You can add another .cpp file with a main function to test your code.

  • Define the following function and refactor render_image:

    void write_color(std::ostream& out, const color& pixel_color)
    

    You can now create aliases to make your code more readable with:

    • using color = vec3; and using point3 = vec3;
    • color will have three components between 0 and 1 for simplification. You need to compute RGB components appropriately in write_color.
Rays
  • Define a class ray with

    • member data: a point3 and a vec3 (origin and direction)
    • member function to access data members, and a function to get the position along the ray
    point3 ray::at(double t)
    

    which returns the origin for t=0t=0 and direction for t=1t=1.

Viewport
  • We will send rays from the point3 camera(0,0,0) to the "viewport", the window to our virtual environment. This viewport will be discretized by the pixels of our screen.
    • Define viewport_height the height of the viewport (with an arbitrary number), and deduce the viewport_width using the "real" ratio between the number of pixel in width and height. Define also focal_length (distance between the camera and the viewport).
    • Define a point3 object for the top left pixel, vec3 objects for (0,Δu,0)(0,\Delta u, 0), (0,Δv,0)(0,-\Delta v, 0),

  • Refactor render_image using a loop over the pixels of the viewport, so you can compute the ray between the current pixel center and the camera. You can use all the operators defined in the class vec3!
Sending rays
  • In render_image, we gave an arbitrary color so far. Instead, define and use the following function

    color ray_color(const ray& r)
    

    where the return color will blend white and blue depending on the height:

    (1-a) * color(1.0,1.0,1.0) + a * color(0.5,0.7,1.0)
    

    where a=0.5 * (unit_vector(r.direction()).y() + 1.0). Remark it goes to 0 when looking down, to 1 when looking up.

aspect_ratio=16./9., image_width=400, focal_length=1., viewport_height=2

A first image with an obstacle
  • write a function that return True if a ray intersects a sphere:

    bool hit_sphere(const point3& center, double radius, const ray& r)
    
  • change the function ray_color to return red when it hits the sphere.

center=point3(0, 0, -1), radius=0.5

  • change the function hit_sphere to return the position of the intersection on the ray, and use it in ray_color to display normals on the sphere by mapping its component to (0,1).
    • Normals will always be unit length.

What is an obstacle?

Required: Polymorphism

  • When sending rays, we need to record the position and the normal to the object it hits. Define a class hit_record which contains three data members: point3 p, vec3 normal and double t which gives the position along the ray.

  • write an abstract class hittable with a pure virtual function:

    virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const = 0;
    

    It will return true if ray hits the obstacle with tminttmaxt_{min} \leq t \leq t_{max}.

  • hittable defines the interface obstacles need to satisfy. Write a class sphere deriving from hittable, its function hit should be similar to the previous hit_sphere function.

  • For the actual computation of the normal, we choose to always have normals pointing outward of the obstacle. In hittable_record

    • add a boolean front_face, which will be true of the ray hits the obstacle from outside, and false otherwise.
    • add a method void set_face_normal(const ray& r, const vec3& outward_normal) that will set the direction of normal and front_face. This function needs to be used in sphere now.
First use of an abstract obstacle

Required: Pointers

  • To test hittable and hit_record, change ray_color to use them instead of hit_sphere. To do so and use polymorphism, you will need to create a std::shared_ptr<hittable>.

  • You should still get the same image as previously.

List of obstacles
  • Define the class hittable_list inheriting from hittable with

    • data members: std::vector<std::shared_ptr<hittable>> objects
    • member functions:
    void add(std::shared_ptr<hittable> object)
    bool hit(const ray &r, double ray_tmin, double ray_tmax, hit_record &rec) const override 
    

    where hit will loop over all the hittable objects to find the closest hit by r.

  • Rewrite ray_color so that it takes a const hittable& as an input argument.

  • Create an hittable_list with

    Sphere at (0,0,1)(0, 0, -1) with a radius of 0.50.5, and (0,100.5,1)(0, -100.5, -1) with a radius of 100100.

Last words

To go further

  • The project is taken from Ray Tracing in One Weekend, and it goes way further that what we did. In this course, we stop here, but if you want to finish the projet, take a look!

Tips for the future

  • Take the time to look for information (documentation and stackoverflow helps!)
  • Read the output of the compiler
  • Use git (especially for projects)

Good luck 💪

- Say that they are all available via a browser. - Wait for them to have it open.

.abs-layout.width-20.right-5.top-30[![VS Code](https://upload.wikimedia.org/wikipedia/commons/9/9a/Visual_Studio_Code_1.35_icon.svg)]

- Explain the terminology (cli/shell, prompt and terminal) - Quickly mention variables

version control system: source code is usually **plain text** -> `.py`, `.tex`, `.marshmallow`...

git scales ! from your article/proto code to Linux kernel and Windows