From 5c0995b850c16f9c7bde507032c680a9c060781a Mon Sep 17 00:00:00 2001 From: kugimasa Date: Sat, 26 Aug 2023 21:15:05 +0900 Subject: [PATCH] Finalize scene. - Implement command line argument. --- resources/shader/compute-sample.wgsl | 17 ++---- src/include/renderer.h | 4 +- src/include/scene.h | 5 +- src/main.cpp | 15 +++++- src/objects/cornell_box.cpp | 18 ++++--- src/render.cpp | 4 +- src/scene.cpp | 77 +++++++++++++++++----------- 7 files changed, 78 insertions(+), 62 deletions(-) diff --git a/resources/shader/compute-sample.wgsl b/resources/shader/compute-sample.wgsl index c700677..007223c 100644 --- a/resources/shader/compute-sample.wgsl +++ b/resources/shader/compute-sample.wgsl @@ -76,7 +76,6 @@ struct SphereLights { l6 : Sphere, l7 : Sphere, l8 : Sphere, - l9 : Sphere, } fn fabs(x: f32) -> f32 { @@ -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); @@ -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; @@ -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) { @@ -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); } @@ -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); @@ -274,8 +266,7 @@ 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) + @@ -283,8 +274,7 @@ fn mixture_pdf(hit: HitInfo, dir: vec3f) -> f32 { 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; } @@ -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; } diff --git a/src/include/renderer.h b/src/include/renderer.h index d083d98..3ff71b2 100644 --- a/src/include/renderer.h +++ b/src/include/renderer.h @@ -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); @@ -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; diff --git a/src/include/scene.h b/src/include/scene.h index 28dc682..c561340 100644 --- a/src/include/scene.h +++ b/src/include/scene.h @@ -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(); diff --git a/src/main.cpp b/src/main.cpp index 01508c9..ec2f0d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; @@ -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; } diff --git a/src/objects/cornell_box.cpp b/src/objects/cornell_box.cpp index ef43d8f..f2d882e 100644 --- a/src/objects/cornell_box.cpp +++ b/src/objects/cornell_box.cpp @@ -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 &quads) { diff --git a/src/render.cpp b/src/render.cpp index 596988e..1eaf207 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -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(); diff --git a/src/scene.cpp b/src/scene.cpp index 23f77de..5acf72e 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -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); @@ -337,26 +340,39 @@ 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); @@ -364,14 +380,13 @@ void Scene::UpdateSphereLights(Queue &queue, float t) { 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)); }