Skip to content

Commit

Permalink
Finalize scene.
Browse files Browse the repository at this point in the history
- Implement command line argument.
  • Loading branch information
kugimasa committed Aug 26, 2023
1 parent 81c1576 commit 5c0995b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 62 deletions.
17 changes: 3 additions & 14 deletions resources/shader/compute-sample.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ struct SphereLights {
l6 : Sphere,
l7 : Sphere,
l8 : Sphere,
l9 : Sphere,
}

fn fabs(x: f32) -> f32 {
Expand Down Expand Up @@ -178,7 +177,6 @@ fn sample_from_light(hit: HitInfo) -> vec3f {
let l6_dist = distance(hit.pos, sphere_lights.l6.center);
let l7_dist = distance(hit.pos, sphere_lights.l7.center);
let l8_dist = distance(hit.pos, sphere_lights.l8.center);
let l9_dist = distance(hit.pos, sphere_lights.l9.center);
let l1_w = select(0.0, 1.0 / (l1_dist * l1_dist), sphere_lights.l1.emissive > 0.0f);
let l2_w = select(0.0, 1.0 / (l2_dist * l2_dist), sphere_lights.l2.emissive > 0.0f);
let l3_w = select(0.0, 1.0 / (l3_dist * l3_dist), sphere_lights.l3.emissive > 0.0f);
Expand All @@ -187,8 +185,7 @@ fn sample_from_light(hit: HitInfo) -> vec3f {
let l6_w = select(0.0, 1.0 / (l6_dist * l6_dist), sphere_lights.l6.emissive > 0.0f);
let l7_w = select(0.0, 1.0 / (l7_dist * l7_dist), sphere_lights.l7.emissive > 0.0f);
let l8_w = select(0.0, 1.0 / (l8_dist * l8_dist), sphere_lights.l8.emissive > 0.0f);
let l9_w = select(0.0, 1.0 / (l9_dist * l9_dist), sphere_lights.l9.emissive > 0.0f);
let sum = l1_w + l2_w + l3_w + l4_w + l5_w + l6_w + l7_w + l8_w + l9_w;
let sum = l1_w + l2_w + l3_w + l4_w + l5_w + l6_w + l7_w + l8_w;
let l1_t = l1_w / sum;
let l2_t = l1_t + l2_w / sum;
let l3_t = l2_t + l3_w / sum;
Expand All @@ -197,7 +194,6 @@ fn sample_from_light(hit: HitInfo) -> vec3f {
let l6_t = l5_t + l6_w / sum;
let l7_t = l6_t + l7_w / sum;
let l8_t = l7_t + l8_w / sum;
let l9_t = l8_t + l9_w / sum;
let rand = rand();
var sphere = sphere_lights.l1;
if (l1_t < rand && rand <= l2_t) {
Expand All @@ -221,9 +217,6 @@ fn sample_from_light(hit: HitInfo) -> vec3f {
if (l7_t < rand && rand <= l8_t) {
sphere = sphere_lights.l8;
}
if (l8_t < rand && rand <= l9_t) {
sphere = sphere_lights.l9;
}
return sample_from_sphere(sphere, hit.pos);
}

Expand Down Expand Up @@ -265,7 +258,6 @@ fn mixture_pdf(hit: HitInfo, dir: vec3f) -> f32 {
let l6_dist = distance(hit.pos, sphere_lights.l6.center);
let l7_dist = distance(hit.pos, sphere_lights.l7.center);
let l8_dist = distance(hit.pos, sphere_lights.l8.center);
let l9_dist = distance(hit.pos, sphere_lights.l9.center);
let l1_w = select(0.0, 1.0 / (l1_dist * l1_dist), sphere_lights.l1.emissive > 0.0f);
let l2_w = select(0.0, 1.0 / (l2_dist * l2_dist), sphere_lights.l2.emissive > 0.0f);
let l3_w = select(0.0, 1.0 / (l3_dist * l3_dist), sphere_lights.l3.emissive > 0.0f);
Expand All @@ -274,17 +266,15 @@ fn mixture_pdf(hit: HitInfo, dir: vec3f) -> f32 {
let l6_w = select(0.0, 1.0 / (l6_dist * l6_dist), sphere_lights.l6.emissive > 0.0f);
let l7_w = select(0.0, 1.0 / (l7_dist * l7_dist), sphere_lights.l7.emissive > 0.0f);
let l8_w = select(0.0, 1.0 / (l8_dist * l8_dist), sphere_lights.l8.emissive > 0.0f);
let l9_w = select(0.0, 1.0 / (l9_dist * l9_dist), sphere_lights.l9.emissive > 0.0f);
let sum = l1_w + l2_w + l3_w + l4_w + l5_w + l6_w + l7_w + l8_w + l9_w;
let sum = l1_w + l2_w + l3_w + l4_w + l5_w + l6_w + l7_w + l8_w;
let light_pdf = l1_w * sphere_pdf(hit, sphere_lights.l1, dir) +
l2_w * sphere_pdf(hit, sphere_lights.l2, dir) +
l3_w * sphere_pdf(hit, sphere_lights.l3, dir) +
l4_w * sphere_pdf(hit, sphere_lights.l4, dir) +
l5_w * sphere_pdf(hit, sphere_lights.l5, dir) +
l6_w * sphere_pdf(hit, sphere_lights.l6, dir) +
l7_w * sphere_pdf(hit, sphere_lights.l7, dir) +
l8_w * sphere_pdf(hit, sphere_lights.l8, dir) +
l9_w * sphere_pdf(hit, sphere_lights.l9, dir);
l8_w * sphere_pdf(hit, sphere_lights.l8, dir);
return 0.5 * cosine_pdf(hit, dir) + 0.5 * light_pdf / sum;
}

Expand Down Expand Up @@ -382,7 +372,6 @@ fn sample_hit(r: Ray) -> HitInfo {
hit = intersect_sphere(r, sphere_lights.l6, hit);
hit = intersect_sphere(r, sphere_lights.l7, hit);
hit = intersect_sphere(r, sphere_lights.l8, hit);
hit = intersect_sphere(r, sphere_lights.l9, hit);
return hit;
}

Expand Down
4 changes: 2 additions & 2 deletions src/include/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Renderer {
public:
bool OnInit(bool hasWindow);

bool OnCompute();
bool OnCompute(uint32_t start_frame, uint32_t end_frame);

bool OnRender(uint32_t frame);

Expand Down Expand Up @@ -42,7 +42,7 @@ class Renderer {
private:
static const uint32_t WIDTH = 640;
static const uint32_t HEIGHT = 480;
static const uint32_t MAX_FRAME = 300;
static const uint32_t MAX_FRAME = 600;
Camera camera_{};
Scene scene_{};
bool hasWindow_ = false;
Expand Down
5 changes: 2 additions & 3 deletions src/include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ class Scene {
Sphere l6_;
Sphere l7_;
Sphere l8_;
Sphere l9_;

SphereLights(Sphere l1, Sphere l2, Sphere l3, Sphere l4, Sphere l5, Sphere l6, Sphere l7, Sphere l8, Sphere l9)
: l1_(l1), l2_(l2), l3_(l3), l4_(l4), l5_(l5), l6_(l6), l7_(l7), l8_(l8), l9_(l9) {};
SphereLights(Sphere l1, Sphere l2, Sphere l3, Sphere l4, Sphere l5, Sphere l6, Sphere l7, Sphere l8)
: l1_(l1), l2_(l2), l3_(l3), l4_(l4), l5_(l5), l6_(l6), l7_(l7), l8_(l8) {};
};

void Release();
Expand Down
15 changes: 13 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "renderer.h"

int main() {
int main(int argc, char *argv[]) {
Print(PrintInfoType::Portracer, "Starting Portracer (_)=---=(_)");
Renderer renderer;
bool hasWindow = false;
Expand All @@ -13,8 +13,19 @@ int main() {
}
}

uint32_t start_frame = 1;
uint32_t end_frame = 600;
// コマンドライン入力形式
// ./Portracer.exe --frame [start] [end]
if (argc == 4) {
if (strcmp(argv[1], "--frame") == 0) {
start_frame = (uint32_t) atoi(argv[2]);
end_frame = (uint32_t) atoi(argv[3]);
}
}

// ComputePipeline
if (!renderer.OnCompute()) {
if (!renderer.OnCompute(start_frame, end_frame)) {
Error(PrintInfoType::Portracer, "(_)=--.. Something went wrong");
return 1;
}
Expand Down
18 changes: 10 additions & 8 deletions src/objects/cornell_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,36 @@ CornellBox::CornellBox(Point3 center, Vec3 scale) {
auto s_x = scale_.X();
auto s_y = scale_.Y();
auto s_z = scale_.Z();
auto red = Color3(0.65, 0.05, 0.05);
auto white = Color3(0.73, 0.73, 0.73);
auto green = Color3(0.12, 0.45, 0.15);
auto left_red = Color3(1.0f, 0.2f, 0.2f);
auto far_green = Color3(0.2f, 1.0f, 0.2f);
auto right_blue = Color3(0.2f, 0.2f, 1.0f);
auto top_orange = Color3(1.0f, 0.5f, 0.0f);
auto bottom_teal = Color3(0.2f, 0.8f, 0.8f);
/// Top
auto pos = Vec3(p_x - s_x / 2.0f, p_y + s_y / 2.0f, p_z - s_z / 2.0f);
auto right = Vec3(s_x, 0, 0);
auto up = Vec3(0, 0, s_z);
quads_.emplace_back(pos, right, up, white);
quads_.emplace_back(pos, right, up, top_orange);
/// Bottom
pos = Vec3(p_x - s_x / 2.0f, p_y - s_y / 2.0f, p_z + s_z / 2.0f);
right = Vec3(s_x, 0, 0);
up = Vec3(0, 0, -s_z);
quads_.emplace_back(pos, right, up, white);
quads_.emplace_back(pos, right, up, bottom_teal);
/// Right
pos = Vec3(p_x + s_x / 2.0f, p_y - s_y / 2.0f, p_z - s_z / 2.0f);
right = Vec3(0, 0, s_z);
up = Vec3(0, s_y, 0);
quads_.emplace_back(pos, right, up, green);
quads_.emplace_back(pos, right, up, right_blue);
/// Left
pos = Vec3(p_x - s_x / 2.0f, p_y - s_y / 2.0f, p_z + s_z / 2.0f);
right = Vec3(0, 0, -s_z);
up = Vec3(0, s_y, 0);
quads_.emplace_back(pos, right, up, red);
quads_.emplace_back(pos, right, up, left_red);
/// Far
pos = Vec3(p_x - s_x / 2.0f, p_y - s_y / 2.0f, p_z - s_z / 2.0f);
right = Vec3(s_x, 0, 0);
up = Vec3(0, s_y, 0);
quads_.emplace_back(pos, right, up, white);
quads_.emplace_back(pos, right, up, far_green);
}

void CornellBox::PushToQuads(std::vector<Quad> &quads) {
Expand Down
4 changes: 2 additions & 2 deletions src/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,14 @@ void Renderer::InitBindGroup() {
}

/// \brief Compute pass
bool Renderer::OnCompute() {
bool Renderer::OnCompute(uint32_t start_frame, uint32_t end_frame) {
Print(PrintInfoType::Portracer, "Running compute pass ...");
auto success = false;
// chrono変数
std::chrono::system_clock::time_point start, end;
// 時間計測開始
start = std::chrono::system_clock::now();
for (uint32_t i = 0; i < MAX_FRAME; ++i) {
for (uint32_t i = start_frame - 1; i < end_frame; ++i) {
success = OnRender(i);
}
queue_.release();
Expand Down
77 changes: 46 additions & 31 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ Scene::Scene(Device &device) {
cb.PushToQuads(quads_);

/// Sphereの追加
spheres_.emplace_back(Point3(-1.0, 0, -9), 0.3, Color3(0.8, 0.8, 0.8));
// 最終位置
float end_pos = 128;
spheres_.emplace_back(Point3(0.0, 2.0, -(30 + end_pos)), 0.3, Color3(0.8, 0.8, 0.8));
spheres_.emplace_back(Point3(0.0, 2.0, -(60 + end_pos)), 0.3, Color3(0.8, 0.8, 0.8));
spheres_.emplace_back(Point3(0.0, 2.0, -(90 + end_pos)), 0.3, Color3(0.8, 0.8, 0.8));
spheres_.emplace_back(Point3(0.0, 2.0, -(120 + end_pos)), 0.3, Color3(0.8, 0.8, 0.8));
/// バッファのバインド
spheres_.emplace_back(Point3(-1.0, 0, -10), 0.3, Color3(0.8, 0.8, 0.8));
spheres_.emplace_back(Point3(2.0, 0.5, -20), 0.3, Color3(0.8, 0.2, 0.4));
spheres_.emplace_back(Point3(1.0, -0.5, -30), 0.3, Color3(0.4, 0.8, 0.1));
spheres_.emplace_back(Point3(-1.5, 1.5, -40), 0.3, Color3(0.5, 0.2, 0.7));
spheres_.emplace_back(Point3(0.75, 1.8, -50), 0.3, Color3(0.1, 0.6, 0.3));
spheres_.emplace_back(Point3(1.65, -0.3, -60), 0.3, Color3(0.3, 0.2, 0.1));
spheres_.emplace_back(Point3(0.3, 0.5, -70), 0.3, Color3(0.4, 0.5, 0.4));
spheres_.emplace_back(Point3(-1.0, -0.5, -80), 0.3, Color3(0.7, 0.1, 0.7));
spheres_.emplace_back(Point3(1.0, -1.8, -90), 0.3, Color3(0.1, 0.3, 0.2));
spheres_.emplace_back(Point3(-1.8, 0.75, -100), 0.3, Color3(0.8, 0.6, 0.2));
spheres_.emplace_back(Point3(0.75, 0.5, -110), 0.3, Color3(0.2, 0.4, 0.5));
InitBindGroupLayout(device);
InitBuffers(device);
InitBindGroup(device);
Expand Down Expand Up @@ -337,41 +340,53 @@ void Scene::UpdateSphereLights(Queue &queue, float t) {
float cam_pos = t < 0.2f ? 8.0f * EaseInQuart(t / 0.2f) : 8.0f * EaseInQuart((t - 0.2f) / 0.8f + 1.0f);
float dist_from_cam = 5.0f;
float move_dist = cam_pos + dist_from_cam;
bool is_on = t < 0.95f;
bool l1_on = is_on && move_dist > 15 - dist_from_cam;
bool l2_on = is_on && move_dist > 30 - dist_from_cam;
bool l3_on = is_on && move_dist > 45 - dist_from_cam;
bool l4_on = is_on && move_dist > 60 - dist_from_cam;
bool l5_on = is_on && move_dist > 75 - dist_from_cam;
bool l6_on = is_on && move_dist > 90 - dist_from_cam;
bool l7_on = is_on && move_dist > 105 - dist_from_cam;
bool l8_on = is_on && move_dist > 120 - dist_from_cam;
Color3 light_col = Color3(1000, 1000, 1000);
bool l1_on = move_dist > 15 - dist_from_cam;
bool l2_on = move_dist > 30 - dist_from_cam;
bool l3_on = move_dist > 45 - dist_from_cam;
bool l4_on = move_dist > 60 - dist_from_cam;
bool l5_on = move_dist > 75 - dist_from_cam;
bool l6_on = move_dist > 90 - dist_from_cam;
bool l7_on = move_dist > 105 - dist_from_cam;
float light_power = t < 0.15f ? Lerp(0.0, 1000.0, t / 0.15f) : t < 0.95f ? 1000.0f : Lerp(1000.0f, 0.0f, EaseOutCubic((t - 0.95f) / 0.05f));
Color3 light_off_col = Color3(0.2, 0.2, 0.2);
Color3 move_light_col = Color3(2000, 2000, 2000);
Color3 col1 = l1_on ? light_col + Color3(250, 0, 0) : light_off_col;
Color3 col2 = l2_on ? light_col + Color3(0, 250, 0) : light_off_col;
Color3 col3 = l3_on ? light_col + Color3(0, 0, 250) : light_off_col;
Color3 col4 = l4_on ? light_col + Color3(250, 250, 0) : light_off_col;
Color3 col5 = l5_on ? light_col + Color3(0, 250, 250) : light_off_col;
Color3 col6 = l6_on ? light_col + Color3(250, 0, 250) : light_off_col;
Color3 col7 = l7_on ? light_col + Color3(500, 0, 250) : light_off_col;
Color3 col8 = l8_on ? light_col + Color3(250, 0, 500) : light_off_col;
Color3 move_light_col = light_power * Color3(1.0f, 1.0f, 1.0f);
Color3 col1 = l1_on ? light_power * Color3(255.0f / 255.0f, 0.0f, 0.0f) : light_off_col;
Color3 col2 = l2_on ? light_power * Color3(255.0f / 255.0f, 127.0f / 255.0f, 0.0f) : light_off_col;
Color3 col3 = l3_on ? light_power * Color3(255.0f / 255.0f, 255.0f / 255.0f, 0.0f) : light_off_col;
Color3 col4 = l4_on ? light_power * Color3(0.0f, 255.0f / 255.0f, 0.0f) : light_off_col;
Color3 col5 = l5_on ? light_power * Color3(0.0f, 0.0f, 255.0f / 255.0f) : light_off_col;
Color3 col6 = l6_on ? light_power * Color3(75.0f / 255.0f, 0.0f, 130.0f / 255.0f) : light_off_col;
Color3 col7 = l7_on ? light_power * Color3(148.0f / 255.0f, 0.0f, 211.0f / 255.0f) : light_off_col;
// 移動ライトの色を変化
if (l7_on) {
move_light_col = col7;
} else if (l6_on) {
move_light_col = col6;
} else if (l5_on) {
move_light_col = col5;
} else if (l4_on) {
move_light_col = col4;
} else if (l3_on) {
move_light_col = col3;
} else if (l2_on) {
move_light_col = col2;
} else if (l1_on) {
move_light_col = col1;
}
Sphere l1(Point3(0.0, 2.0, -15), 0.3, col1, l1_on);
Sphere l2(Point3(0.0, 2.0, -30), 0.3, col2, l2_on);
Sphere l3(Point3(0.0, 2.0, -45), 0.3, col3, l3_on);
Sphere l4(Point3(0.0, 2.0, -60), 0.3, col4, l4_on);
Sphere l5(Point3(0.0, 2.0, -75), 0.3, col5, l5_on);
Sphere l6(Point3(0.0, 2.0, -90), 0.3, col6, l6_on);
Sphere l7(Point3(0.0, 2.0, -105), 0.3, col7, l7_on);
Sphere l8(Point3(0.0, 2.0, -120), 0.3, col8, l8_on);
// float end_pos = 8.0f * EaseInQuart((0.95f - 0.2f) / 0.8f + 1.0f);
// float theta = (is_on ? cam_pos / 12.0f : Lerp(end_pos / 12.0f, 360.0f, (t - 0.95f) / 0.05f)) * (float) (M_PI * 2.0f);
float end_cam_pos = 8.0f * EaseInQuart((0.95f - 0.2f) / 0.8f + 1.0f);
float end_theta = Lerp(end_cam_pos / 12.0f * (float) (M_PI * 2.0f), 10.0f * (float) (M_PI * 2.0f), EaseOutCubic((t - 0.95f) / 0.05f));
float theta = cam_pos / 12.0f * (float) (M_PI * 2.0f);
float x = cos(theta);
float y = sin(theta);
Point3 origin = Vec3(x, y, 0.01f - move_dist);
Sphere move_l(origin, 0.1, move_light_col, true);
SphereLights lights(l1, l2, l3, l4, l5, l6, l7, l8, move_l);
SphereLights lights(l1, l2, l3, l4, l5, l6, l7, move_l);
queue.writeBuffer(sphere_light_buffer_, 0, &lights, sizeof(SphereLights));
}

0 comments on commit 5c0995b

Please sign in to comment.