-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add STB third party, add asset concept, image texture
- Loading branch information
Showing
16 changed files
with
8,286 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 &¶ms) | ||
{ | ||
m_params = params; | ||
m_camera = InitCamera(); | ||
m_objets = InitObjects(); | ||
m_renderer = InitRenderer(); | ||
} | ||
|
||
|
||
shared_ptr<RGBColor[]> SolarSystemScene::Render() | ||
{ | ||
m_renderer->Render(); | ||
return m_renderer->GetBuffer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
}; |
Oops, something went wrong.