T2 Texture Mapping (MS2)

Create class texture

create texture.h where you are writing your abstract texture class.

It only defines a virtual method value() which returns a colour value.

We also define a class solid_color to implemented texture. It only has a private member albedo and the value() method returns that colour.

#ifndef TEXTURE_H
#define TEXTURE_H

class texture {
  public:
    virtual ~texture() = default;
    virtual color value(double u, double v, const point3& p) const = 0;
};

class solid_color : public texture {
  public:
    solid_color(const color& albedo) : albedo(albedo) {}
    solid_color(double red, double green, double blue) : solid_color(color(red,green,blue)) {}

    color value(double u, double v, const point3& p) const override {
        return albedo;
    }

  private:
    color albedo;
};

#endif

Add u, v in hit_record (hittable.h)

We need the (u,v) coordinates to retrieve colour from the texture.

Checker Texture

add class checker_texture in texture.h to implement the abstract texture classs

Add texture to the lambertian class in material.h

change the lambertian class in material.h to use the texture.

Use texture to replace albedo colour, revise the constructor of lambertian to use the texture.

Revise the scatter method to use colour from the texture.

Modify the scene in main.cpp to use the textured lambertian material

Revise main.cpp

Add texture.h

Use the complex scene at the end of Book 1 ( Lab B03), replace the ground material with our new material.

Build and Run

Set a lower image resolution for testing.

Build your programme into the Release version.

If successfully built, run your program:

With powershell, you can use Measure-Command to know the running time of your programme.

On Linux, you can use the time command.

The ouput of Measure-Command looks like the following, so that I can know it takes almost 1 minute and 39 seconds to run the programme.

The final result is the following, it looks darder because I skipped Gamma correction in the first book.

Last updated