Skip to content

Commit

Permalink
add STB third party, add asset concept, image texture
Browse files Browse the repository at this point in the history
  • Loading branch information
PoneyUHC committed Aug 29, 2024
1 parent e2f94a1 commit bce9c73
Show file tree
Hide file tree
Showing 16 changed files with 8,286 additions and 5 deletions.
7 changes: 5 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

- Book 2 section 4 : texture mapping
- make buildchain crossplatform with CMake
- add more commong image type exporter (PNG)
- test to replace RNG for grid sampling in antialiasing
- test to replace RNG for grid sampling in antialiasing
- centralize asset loading in a resource manager
- find a good name for the project
- have a format describing scenes instead of code description
- add log system
Binary file added assets/2k_earth.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/2k_moon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/2k_sun.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/8k_stars_milky_way.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/assets/IAsset.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#pragma once

#include <filesystem>
#include <string>


class IAsset {

public:

virtual ~IAsset() {};
virtual std::string GetPath() const = 0;
virtual int Load() = 0;

};
59 changes: 59 additions & 0 deletions src/assets/image_asset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

#include "image_asset.hpp"

#include "utils.hpp"

#define STB_IMAGE_IMPLEMENTATION
#include "third_party/stb_image.h"

#include <memory>
#include <filesystem>
#include <iostream>


using namespace std;
namespace fs = std::filesystem;


int ImageAsset::Load()
{
clog << "Loading " << m_filepath << " ..." << endl;
fs::path path(m_filepath);

if(!fs::exists(m_filepath)){
cerr << "\t" << m_filepath << ": file not found" << endl;
return false;
}

int n = m_bytes_per_pixel;

float* fdata;
fdata = stbi_loadf(path.c_str(), &m_width, &m_height, &n, m_bytes_per_pixel);
if(fdata == NULL){
cerr << "\t" << m_filepath << ": error while loading file" << endl;
cerr << "\t" << stbi_failure_reason() << endl;
return false;
}

m_fdata = unique_ptr<float[]>(fdata);

int total_bytes = m_width * m_height * m_bytes_per_pixel;
m_bdata = make_unique<uint8_t[]>(total_bytes);

#pragma omp parallel for
for(int i=0; i<total_bytes; ++i){
m_bdata[i] = rgb_normalized_to_8bits(m_fdata[i]);
}

clog << "Loaded " << m_filepath << " successfully" << endl;
return true;
}


float* ImageAsset::GetPixelData(int x, int y)
{
if(m_fdata == nullptr){
return MAGENTA;
}
return m_fdata.get() + y*m_bytes_per_pixel*m_width + x*m_bytes_per_pixel;
}
31 changes: 31 additions & 0 deletions src/assets/image_asset.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#pragma once

#include "assets/IAsset.hpp"


class ImageAsset : public IAsset {

private:

std::string m_filepath;
int m_width = 0;
int m_height = 0;
int m_bytes_per_pixel = 3;

std::unique_ptr<float[]> m_fdata;
std::unique_ptr<uint8_t[]> m_bdata;


public:

ImageAsset(std::string filepath): m_filepath{filepath} {};

int Load() override;
std::string GetPath() const override { return m_filepath; }

int Width() const { return (m_fdata == nullptr) ? 0 : m_width; }
int Height() const { return (m_fdata == nullptr) ? 0 : m_height; }
float* GetPixelData(int x, int y);

};
20 changes: 19 additions & 1 deletion src/geometry/sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,25 @@ bool Sphere::Hit(const Ray& ray, const Interval& interval, HitRecord& outRecord)
outRecord.hit_point = ray.At(root);
Vec3 outwardNormal = (outRecord.hit_point - m_center) / m_radius;
outRecord.SetFaceNormal(ray, outwardNormal);
get_sphere_uv(outwardNormal, outRecord.u, outRecord.v);
outRecord.material = m_material;

return true;
}
}


void Sphere::get_sphere_uv(const Point3 &point, double &u, double &v)
{
// p: a given point on the sphere of radius one, centered at the origin.
// u: returned value [0,1] of angle around the Y axis from X=-1.
// v: returned value [0,1] of angle from Y=-1 to Y=+1.
// <1 0 0> yields <0.50 0.50> <-1 0 0> yields <0.00 0.50>
// <0 1 0> yields <0.50 1.00> < 0 -1 0> yields <0.50 0.00>
// <0 0 1> yields <0.25 0.50> < 0 0 -1> yields <0.75 0.50>

double theta = std::acos(-point[1]);
double phi = std::atan2(-point[2], point[0]) + M_PI;

u = phi / (2*M_PI);
v = theta / M_PI;
}
1 change: 1 addition & 0 deletions src/geometry/sphere.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ class Sphere : public IHittable {

AABB GetAABB() const override { return m_aabb; }

static void get_sphere_uv(const Point3& point, double& u, double& v);
};
8 changes: 6 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "scenes/IScene.hpp"
#include "scenes/sparsed_spheres_scene.hpp"
#include "scenes/checkered_spheres_scene.hpp"
#include "scenes/solar_system_scene.hpp"
#include "renderer/camera.hpp"
#include "export/png_exporter.hpp"

Expand All @@ -27,13 +28,16 @@ int main(int argc, char *argv[]){

unique_ptr<IScene> scene;

switch(2){
case 1:
switch(3){
case 1:
scene = make_unique<SparsedSpheresScene>();
break;
case 2:
scene = make_unique<CheckeredSpheresScene>();
break;
case 3:
scene = make_unique<SolarSystemScene>();
break;
default:
cout << "Problem in scene selection" << endl;
return 1;
Expand Down
97 changes: 97 additions & 0 deletions src/scenes/solar_system_scene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

#include "solar_system_scene.hpp"

#include "geometry/hittable_list.hpp"
#include "geometry/hittable_list.hpp"
#include "geometry/sphere.hpp"
#include "material/lambertian.hpp"
#include "assets/image_asset.hpp"
#include "texture/image_texture.hpp"
#include "renderer/camera.hpp"
#include "renderer/pathtracing_renderer.hpp"

#include <memory>


using namespace std;


shared_ptr<Camera> SolarSystemScene::InitCamera()
{
CameraParams camera_params;
camera_params.aspect_ratio = 16.0 / 9.0;
camera_params.image_width = m_params.render_width;
camera_params.vfov = 40.0;
camera_params.lookfrom = Point3(0,0,12);
camera_params.lookat = Point3(0,0,0);
camera_params.vup = Vec3(0,1,0);
camera_params.defocus_angle = 0.0;
camera_params.focus_dist = 10.0;

return make_shared<Camera>(std::move(camera_params));
}


shared_ptr<HittableList> SolarSystemScene::InitObjects()
{
auto hittable_list = make_shared<HittableList>();

auto earth_asset = make_shared<ImageAsset>("assets/2k_earth.jpg");
earth_asset->Load();
auto moon_asset = make_shared<ImageAsset>("assets/2k_moon.jpg");
moon_asset->Load();
auto sun_asset = make_shared<ImageAsset>("assets/2k_sun.jpg");
sun_asset->Load();
auto stars_asset = make_shared<ImageAsset>("assets/8k_stars_milky_way.jpg");
stars_asset->Load();

auto earth_texture = make_shared<ImageTexture>(earth_asset);
auto earth_surface = make_shared<Lambertian>(earth_texture);

auto moon_texture = make_shared<ImageTexture>(moon_asset);
auto moon_surface = make_shared<Lambertian>(moon_texture);

auto sun_texture = make_shared<ImageTexture>(sun_asset);
auto sun_surface = make_shared<Lambertian>(sun_texture);

auto stars_texture = make_shared<ImageTexture>(stars_asset);
auto stars_surface = make_shared<Lambertian>(stars_texture);

auto earth = make_shared<Sphere>(Point3(-3, 0, 0), 2, earth_surface);
auto moon = make_shared<Sphere>(Point3(-4.5, 1, 3), 0.3, moon_surface);
auto sun = make_shared<Sphere>(Point3(10, 3, -10), 8, sun_surface);
auto stars = make_shared<Sphere>(Point3(0,0,-100), 70, stars_surface);

hittable_list->AddObject(earth);
hittable_list->AddObject(moon);
hittable_list->AddObject(sun);
hittable_list->AddObject(stars);

return hittable_list;
}


shared_ptr<PathTracingRenderer> SolarSystemScene::InitRenderer()
{
PathTracingRendererParams params;
params.aa_sample_per_pixel = 300;
params.max_depth = 50;

return make_shared<PathTracingRenderer>(m_camera, m_objets, move(params));
}


void SolarSystemScene::Build(SceneParams &&params)
{
m_params = params;
m_camera = InitCamera();
m_objets = InitObjects();
m_renderer = InitRenderer();
}


shared_ptr<RGBColor[]> SolarSystemScene::Render()
{
m_renderer->Render();
return m_renderer->GetBuffer();
}
25 changes: 25 additions & 0 deletions src/scenes/solar_system_scene.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#pragma once

#include "scenes/IScene.hpp"


class Camera;
class HittableList;
class PathTracingRenderer;

class SolarSystemScene: public IScene {

private:

std::shared_ptr<Camera> InitCamera();
std::shared_ptr<HittableList> InitObjects();
std::shared_ptr<PathTracingRenderer> InitRenderer();


public:

void Build(SceneParams&& params) override;
std::shared_ptr<RGBColor[]> Render() override;

};
17 changes: 17 additions & 0 deletions src/texture/image_texture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include "image_texture.hpp"

#include <algorithm>


RGBColor ImageTexture::GetColor(double u, double v, const Point3 &point) const
{
u = std::clamp(0.0, 1.0, u);
v = 1.0 - std::clamp(0.0, 1.0, v);

int i = int(u * m_image->Width());
int j = int(v * m_image->Height());
float* pixel_data = m_image->GetPixelData(i, j);

return RGBColor(pixel_data[0], pixel_data[1], pixel_data[2]);
}
22 changes: 22 additions & 0 deletions src/texture/image_texture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#pragma once

#include "texture/ITexture.hpp"
#include "assets/image_asset.hpp"
#include <memory>


class ImageTexture : public ITexture {

private:

std::shared_ptr<ImageAsset> m_image;


public:

ImageTexture(std::shared_ptr<ImageAsset> image): m_image{image} {};

RGBColor GetColor(double u, double v, const Point3& point) const override;

};
Loading

0 comments on commit bce9c73

Please sign in to comment.