aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2021-09-01 00:18:04 +0200
committerladevieq <ladevie.quentin@gmail.com>2021-09-01 00:18:04 +0200
commitbf512216323f3df55627421bb0187ed364d58340 (patch)
tree2ae215cdf1bacf925efb7a52975d49351274cf30
parentd071fc5c947689eca7726803a420013c519acdd9 (diff)
White furnace test on gltf scenes & load normals
-rw-r--r--include/bvh-node.hpp126
-rw-r--r--include/gltf.hpp16
-rw-r--r--include/material.hpp3
-rw-r--r--include/scene.hpp6
-rw-r--r--include/triangle.hpp1
-rw-r--r--shaders/compute.comp103
-rw-r--r--src/gltf.cpp60
-rw-r--r--src/main.cpp8
-rw-r--r--src/scene.cpp161
9 files changed, 272 insertions, 212 deletions
diff --git a/include/bvh-node.hpp b/include/bvh-node.hpp
index 5a46273..99b6d62 100644
--- a/include/bvh-node.hpp
+++ b/include/bvh-node.hpp
@@ -30,61 +30,21 @@ struct temp_node : public bvh_node {
class bvh {
public:
- bvh(std::vector<sphere>& spheres, std::vector<packed_bvh_node>& packed_nodes)
- :spheres(spheres) {
- packed_nodes.resize(2 * spheres.size() - 1);
- temp_nodes = std::vector<temp_node>(2 * spheres.size() - 1);
- leafs = std::vector<temp_node>(spheres.size());
+ // bvh(std::vector<sphere>& spheres, std::vector<packed_bvh_node>& packed_nodes)
+ // :spheres(spheres) {
+ // packed_nodes.resize(2 * spheres.size() - 1);
+ // temp_nodes = std::vector<temp_node>(2 * spheres.size() - 1);
+ // leafs = std::vector<temp_node>(spheres.size());
- for (uint32_t id = 0; id < spheres.size(); id++) {
- leafs[id].bounding_box = aabb(spheres[id].center, spheres[id].radius);
- leafs[id].primitive_id = id;
- }
-
- temp_nodes[0] = temp_node();
-
- temp_nodes[0].bounding_box = compute_bounds(0, spheres.size());
- subdivide(0, 0, spheres.size());
-
- set_depth_first_order();
-
- for (uint32_t id = 0; id < temp_nodes.size(); id++) {
- auto old_node = temp_nodes[id];
- auto &node = packed_nodes[old_node.df_id];
-
- node.primitive_id = old_node.primitive_id;
-
- node.min[0] = old_node.bounding_box.minimum.v[0];
- node.min[1] = old_node.bounding_box.minimum.v[1];
- node.min[2] = old_node.bounding_box.minimum.v[2];
-
- node.max[0] = old_node.bounding_box.maximum.v[0];
- node.max[1] = old_node.bounding_box.maximum.v[1];
- node.max[2] = old_node.bounding_box.maximum.v[2];
-
- if (old_node.next_id != -1) {
- node.next_id = temp_nodes[old_node.next_id].df_id;
- }
- }
-
- // exporter();
- }
-
- // bvh(std::vector<triangle>& triangles, std::vector<packed_bvh_node>& packed_nodes)
- // :triangles(triangles) {
- // packed_nodes.resize(2 * triangles.size() - 1);
- // temp_nodes = std::vector<temp_node>(2 * triangles.size() - 1);
- // leafs = std::vector<temp_node>(triangles.size());
-
- // for (uint32_t id = 0; id < triangles.size(); id++) {
- // leafs[id].bounding_box = aabb(triangles[id]);
+ // for (uint32_t id = 0; id < spheres.size(); id++) {
+ // leafs[id].bounding_box = aabb(spheres[id].center, spheres[id].radius);
// leafs[id].primitive_id = id;
// }
// temp_nodes[0] = temp_node();
- // temp_nodes[0].bounding_box = compute_bounds(0, triangles.size());
- // subdivide(0, 0, triangles.size());
+ // temp_nodes[0].bounding_box = compute_bounds(0, spheres.size());
+ // subdivide(0, 0, spheres.size());
// set_depth_first_order();
@@ -110,6 +70,46 @@ public:
// // exporter();
// }
+ bvh(std::vector<triangle>& triangles, std::vector<packed_bvh_node>& packed_nodes)
+ :triangles(triangles) {
+ packed_nodes.resize(2 * triangles.size() - 1);
+ temp_nodes = std::vector<temp_node>(2 * triangles.size() - 1);
+ leafs = std::vector<temp_node>(triangles.size());
+
+ for (uint32_t id = 0; id < triangles.size(); id++) {
+ leafs[id].bounding_box = aabb(triangles[id]);
+ leafs[id].primitive_id = id;
+ }
+
+ temp_nodes[0] = temp_node();
+
+ temp_nodes[0].bounding_box = compute_bounds(0, triangles.size());
+ subdivide(0, 0, triangles.size());
+
+ set_depth_first_order();
+
+ for (uint32_t id = 0; id < temp_nodes.size(); id++) {
+ auto old_node = temp_nodes[id];
+ auto &node = packed_nodes[old_node.df_id];
+
+ node.primitive_id = old_node.primitive_id;
+
+ node.min[0] = old_node.bounding_box.minimum.v[0];
+ node.min[1] = old_node.bounding_box.minimum.v[1];
+ node.min[2] = old_node.bounding_box.minimum.v[2];
+
+ node.max[0] = old_node.bounding_box.maximum.v[0];
+ node.max[1] = old_node.bounding_box.maximum.v[1];
+ node.max[2] = old_node.bounding_box.maximum.v[2];
+
+ if (old_node.next_id != -1) {
+ node.next_id = temp_nodes[old_node.next_id].df_id;
+ }
+ }
+
+ // exporter();
+ }
+
//-------------------------
// BVH exporter for graphviz
//-------------------------
@@ -166,8 +166,8 @@ public:
std::vector<temp_node> temp_nodes;
std::vector<temp_node> leafs;
- std::vector<sphere>& spheres;
- // std::vector<triangle>& triangles;
+ // std::vector<sphere>& spheres;
+ std::vector<triangle>& triangles;
uint32_t next_node_id = 0;
@@ -207,12 +207,12 @@ private:
if (count <= 4) {
// Subdivide the node by splitting the set in two equal parts
std::sort(leafs.begin() + begin, leafs.begin() + end,
- [&](const bvh_node& a, const bvh_node& b) {
- return spheres[a.primitive_id].center[split_axis] < spheres[b.primitive_id].center[split_axis];
- }
// [&](const bvh_node& a, const bvh_node& b) {
- // return triangles[a.primitive_id].center()[split_axis] < triangles[b.primitive_id].center()[split_axis];
+ // return spheres[a.primitive_id].center[split_axis] < spheres[b.primitive_id].center[split_axis];
// }
+ [&](const bvh_node& a, const bvh_node& b) {
+ return triangles[a.primitive_id].center()[split_axis] < triangles[b.primitive_id].center()[split_axis];
+ }
);
temp_nodes[left_id].bounding_box = compute_bounds(begin, begin + (count / 2));
@@ -233,18 +233,18 @@ private:
aabb centroid_bounds;
for (size_t i = begin; i < end; i++) {
- // auto primitive = triangles[leafs[i].primitive_id];
- // centroid_bounds.union_with(primitive.center());
- auto primitive = spheres[leafs[i].primitive_id];
- centroid_bounds.union_with(primitive.center);
+ auto primitive = triangles[leafs[i].primitive_id];
+ centroid_bounds.union_with(primitive.center());
+ // auto primitive = spheres[leafs[i].primitive_id];
+ // centroid_bounds.union_with(primitive.center);
}
float split_axis_size = centroid_bounds.maximum[split_axis] - centroid_bounds.minimum[split_axis];
for (size_t i = begin; i < end; i++) {
- // auto primitive = triangles[leafs[i].primitive_id];
- // auto normalized_offset = (primitive.center()[split_axis] - centroid_bounds.minimum[split_axis]) / split_axis_size;
- auto primitive = spheres[leafs[i].primitive_id];
- auto normalized_offset = (primitive.center[split_axis] - centroid_bounds.minimum[split_axis]) / split_axis_size;
+ auto primitive = triangles[leafs[i].primitive_id];
+ auto normalized_offset = (primitive.center()[split_axis] - centroid_bounds.minimum[split_axis]) / split_axis_size;
+ // auto primitive = spheres[leafs[i].primitive_id];
+ // auto normalized_offset = (primitive.center[split_axis] - centroid_bounds.minimum[split_axis]) / split_axis_size;
uint32_t bucket_index = normalized_offset * buckets_count;
if (bucket_index == buckets_count) {
bucket_index -= 1;
diff --git a/include/gltf.hpp b/include/gltf.hpp
index 7008e8a..6633811 100644
--- a/include/gltf.hpp
+++ b/include/gltf.hpp
@@ -9,12 +9,14 @@
using json = nlohmann::json;
-#include <vec3.hpp>
+#include "material.hpp"
+#include "vec3.hpp"
struct mesh_part {
- std::vector<uint16_t> indices;
- std::vector<vec3> positions;
- // std::vector<vec3> normals;
+ std::vector<uint16_t> indices;
+ std::vector<vec3> positions;
+ std::vector<vec3> normals;
+ material mat;
};
struct mesh {
@@ -34,9 +36,11 @@ private:
static void load_node(json &gltf_json, uint32_t index, node &parent, std::vector<mesh> &meshes);
- static std::vector<mesh> load_meshes(json &gltf_json, std::vector<std::vector<uint8_t>> &buffers_content);
+ static std::vector<mesh> load_meshes(json &gltf_json, std::vector<std::vector<uint8_t>> &buffers_content, std::vector<material> &materials);
- static mesh_part load_primitive(json &gltf_json, json &primitive, std::vector<std::vector<uint8_t>> &buffers_content);
+ static std::vector<material> load_materials(json &gltf_json);
+
+ static mesh_part load_primitive(json &gltf_json, json &primitive, std::vector<std::vector<uint8_t>> &buffers_content, std::vector<material> &materials);
};
#endif
diff --git a/include/material.hpp b/include/material.hpp
index 4853279..ae42179 100644
--- a/include/material.hpp
+++ b/include/material.hpp
@@ -4,8 +4,7 @@
#include "vec3.hpp"
struct material {
- color albedo;
- color emissive;
+ color base_color;
float metalness = 0.f;
float roughness = 1.f;
uint32_t padding[2];
diff --git a/include/scene.hpp b/include/scene.hpp
index 4eed567..cb0ea88 100644
--- a/include/scene.hpp
+++ b/include/scene.hpp
@@ -29,7 +29,7 @@ class scene {
: cam(cam), width(width), height(height) {}
};
- void random_scene();
+ // void random_scene();
public:
scene(camera cam, uint32_t width, uint32_t height);
@@ -37,8 +37,8 @@ public:
metadata meta;
// Geometry & BVH
- std::vector<sphere> spheres;
- // std::vector<triangle> triangles;
+ // std::vector<sphere> spheres;
+ std::vector<triangle> triangles;
std::vector<packed_bvh_node> packed_nodes;
};
diff --git a/include/triangle.hpp b/include/triangle.hpp
index 9993efe..456a238 100644
--- a/include/triangle.hpp
+++ b/include/triangle.hpp
@@ -3,6 +3,7 @@
struct vertex {
vec3 position;
+ vec3 normal;
};
struct triangle {
diff --git a/shaders/compute.comp b/shaders/compute.comp
index 7454589..0d5c525 100644
--- a/shaders/compute.comp
+++ b/shaders/compute.comp
@@ -25,8 +25,7 @@ const uint DIFFUSE = 1;
const uint SPECULAR = 2;
struct material {
- vec4 albedo;
- vec4 emissive;
+ vec4 base_color;
float metalness;
float roughness;
uint[2] padding;
@@ -35,7 +34,8 @@ struct material {
struct hit_info {
float t;
vec3 point;
- vec3 normal;
+ vec3 geometry_normal;
+ vec3 shading_normal;
material mat;
};
@@ -48,6 +48,7 @@ struct sphere {
struct vertex {
vec4 position;
+ vec4 normal;
};
struct triangle {
@@ -93,8 +94,8 @@ layout(buffer_reference) readonly buffer scene_metadata {
};
layout(buffer_reference) readonly buffer primitives_array {
- sphere[] spheres;
- // triangle triangles[];
+ // sphere[] spheres;
+ triangle triangles[];
};
layout(buffer_reference) readonly buffer nodes_array {
@@ -232,12 +233,12 @@ bool hit_sphere(sphere s, ray r, out hit_info info) {
}
info.t = root;
- info.point = at(r, info.t);
+ // info.point = at(r, info.t);
- vec3 outward_normal = (info.point - s.position.xyz) / s.radius;
- bool front_face = dot(outward_normal, r.direction) < 0.0;
- info.normal = front_face ? outward_normal : -outward_normal;
- info.mat = s.mat;
+ // vec3 outward_normal = (info.point - s.position.xyz) / s.radius;
+ // bool front_face = dot(outward_normal, r.direction) < 0.0;
+ // info.normal = front_face ? outward_normal : -outward_normal;
+ // info.mat = s.mat;
return true;
}
@@ -259,7 +260,7 @@ bool hit_triangle(triangle tri, ray r, out hit_info info) {
}
info.t = t;
- info.normal = normal;
+ info.geometry_normal = normalize(normal);
return true;
}
@@ -272,16 +273,16 @@ bool hit_node(ray r, out hit_info info) {
while(id != -1) {
// Leafs
if (bufs.bvh.nodes[id].primitive_id != -1) {
- if (hit_sphere(bufs.geometry.spheres[bufs.bvh.nodes[id].primitive_id], r, temp_info)) {
- info = temp_info;
- r.max_t = temp_info.t;
- hit = true;
- }
- // if (hit_triangle(bufs.geometry.triangles[bufs.bvh.nodes[id].primitive_id], r, temp_info)) {
+ // if (hit_sphere(bufs.geometry.spheres[bufs.bvh.nodes[id].primitive_id], r, temp_info)) {
// info = temp_info;
// r.max_t = temp_info.t;
// hit = true;
// }
+ if (hit_triangle(bufs.geometry.triangles[bufs.bvh.nodes[id].primitive_id], r, temp_info)) {
+ info = temp_info;
+ r.max_t = temp_info.t;
+ hit = true;
+ }
id = bufs.bvh.nodes[id].next_id;
} else if (hit_aabb(bufs.bvh.nodes[id].min, bufs.bvh.nodes[id].max, r)) {
id++;
@@ -294,12 +295,12 @@ bool hit_node(ray r, out hit_info info) {
return hit;
}
-vec3 albedo_to_specular_F0(vec3 albedo, float metalness) {
- return mix(vec3(MIN_DIELECTRICS_F0), albedo, metalness);
+vec3 base_color_to_specular_F0(vec3 base_color, float metalness) {
+ return mix(vec3(MIN_DIELECTRICS_F0), base_color, metalness);
}
-vec3 albedo_to_diffuse_reflectance(vec3 albedo, float metalness) {
- return albedo * (1.0f - metalness);
+vec3 base_color_to_diffuse_reflectance(vec3 base_color, float metalness) {
+ return base_color * (1.0f - metalness);
}
// Schlick's approximation to Fresnel term
@@ -325,12 +326,12 @@ float shadowed_F90(vec3 F0) {
return min(1.f, t * luminance(F0));
}
-float get_brdf_probability(material mat, vec3 view, vec3 shadingNormal) {
+float get_brdf_probability(material mat, vec3 view, vec3 shading_normal) {
// Evaluate Fresnel term using the shading normal
// Note: we use the shading normal instead of the microfacet normal (half-vector) for Fresnel term here. That's suboptimal for rough surfaces at grazing angles, but half-vector is yet unknown at this point
- vec3 specular_F0 = vec3(luminance(albedo_to_specular_F0(mat.albedo.rgb, mat.metalness)));
- float diffuse_reflectance = luminance(albedo_to_diffuse_reflectance(mat.albedo.rgb, mat.metalness));
- float fresnel = clamp(luminance(eval_fresnel(specular_F0, shadowed_F90(specular_F0), max(0.0f, dot(view, shadingNormal)))), 0.0, 1.0);
+ vec3 specular_F0 = vec3(luminance(base_color_to_specular_F0(mat.base_color.rgb, mat.metalness)));
+ float diffuse_reflectance = luminance(base_color_to_diffuse_reflectance(mat.base_color.rgb, mat.metalness));
+ float fresnel = clamp(luminance(eval_fresnel(specular_F0, shadowed_F90(specular_F0), max(0.0f, dot(view, shading_normal)))), 0.0, 1.0);
// Approximate relative contribution of BRDFs using the Fresnel term
float specular = fresnel;
@@ -352,34 +353,68 @@ vec3 ray_color(ray r, uint seed) {
if (!hit_node(r, info))
return throughput * vec3(1.0);
- // uint brdf_type;
+ vec3 v = -r.direction;
+
+ if (dot(info.geometry_normal, v) < 0.f) info.geometry_normal = -info.geometry_normal;
+
+ // if (dot(info.geometry_normal, info.shading_normal) < 0.0f) info.shading_normal = -info.shading_normal;
+
+ uint brdf_type;
// if (info.mat.metalness == 1.f && info.mat.metalness == 0.f) {
// brdf_type = SPECULAR;
// } else {
- // float brdf_probability = get_brdf_probability(info.mat, -r.direction, );
+ // float brdf_probability = get_brdf_probability(info.mat, -r.direction, info.shading_normal);
// if (rand(seed) < brdf_probability) {
// brdf_type = SPECULAR;
+ // throughput /= brdf_probability;
// } else {
// brdf_type = DIFFUSE;
+ // throughput /= (1.0 - brdf_probability);
// }
// }
+ brdf_type = DIFFUSE;
+
+ // Ignore ray coming from below the hemisphere
+ // if (dot(info.shading_normal, v) <= 0.f) break;
+
// Move to tangent space
- vec4 rotation_to_z = rotation_to_z_axis(info.normal);
+ vec4 rotation_to_z = rotation_to_z_axis(info.geometry_normal);
vec3 view_local = rotate_point(rotation_to_z, -r.direction);
vec3 normal_local = vec3(0.0, 0.0, 1.0);
- vec3 ray_dir_local = sample_hemisphere(seed);
+ vec3 ray_dir_local;
+ vec3 brdf_weight;
+
+ if (brdf_type == DIFFUSE) {
+ ray_dir_local = sample_hemisphere(seed);
- float NdotL = dot(normal_local, ray_dir_local);
- float pdf = NdotL / PI;
- vec3 brdf = albedo_to_diffuse_reflectance(info.mat.albedo.xyz, info.mat.metalness) / PI;
- throughput *= brdf / pdf;
+ float NdotL = dot(normal_local, ray_dir_local);
+ float pdf = NdotL / PI;
+ // vec3 brdf = base_color_to_diffuse_reflectance(info.mat.base_color.xyz, info.mat.metalness) / PI;
+ vec3 brdf = (vec3(1.0) * NdotL) / PI;
+ brdf_weight = brdf / pdf;
+ } else if (brdf_type == SPECULAR) {
+ // alpha = info.mat.roughness * info.mat.roughness;
+
+ // vec3 half_local;
+ // if (alpha == 0.0f) {
+ // // Fast path for zero roughness (perfect reflection), also prevents NaNs appearing due to divisions by zeroes
+ // half_local = vec3(0.0f, 0.0f, 1.0f);
+ // } else {
+ // // For non-zero roughness, this calls VNDF sampling for GG-X distribution or Walter's sampling for Beckmann distribution
+ // half_local = sampleSpecularHalfVector(view_local, vec2(alpha));
+ // }
+ }
+
+ throughput *= brdf_weight;
// Move to global space
vec4 rotation_from_z = invert_rotation(rotation_to_z);
vec3 ray_dir = rotate_point(rotation_from_z, ray_dir_local);
- r = ray(info.point, normalize(info.normal + ray_dir), 0.001f, 1e15);
+ if (dot(info.geometry_normal, ray_dir) <= 0.0f) break;
+
+ r = ray(info.point, ray_dir, 0.001f, 1e15);
// Russian Roulette
// As the throughput gets smaller, the ray is more likely to get terminated early.
diff --git a/src/gltf.cpp b/src/gltf.cpp
index 6b39ff4..66147dc 100644
--- a/src/gltf.cpp
+++ b/src/gltf.cpp
@@ -24,8 +24,10 @@ node gltf::load(std::filesystem::path &path, std::string &filename) {
buffer_index++;
}
+ // auto materials = load_materials(gltf_json);
+ std::vector<material> materials;
- auto meshes = load_meshes(gltf_json, buffers_content);
+ auto meshes = load_meshes(gltf_json, buffers_content, materials);
node root_node;
auto &root_children = scene["nodes"];
@@ -57,7 +59,7 @@ void gltf::load_node(json &gltf_json, uint32_t index, node &parent, std::vector<
}
}
-std::vector<mesh> gltf::load_meshes(json &gltf_json, std::vector<std::vector<uint8_t>> &buffers_content) {
+std::vector<mesh> gltf::load_meshes(json &gltf_json, std::vector<std::vector<uint8_t>> &buffers_content, std::vector<material> &materials) {
auto &gltf_meshes = gltf_json["meshes"];
auto meshes_count = gltf_meshes.size();
std::vector<mesh> meshes { meshes_count };
@@ -68,14 +70,35 @@ std::vector<mesh> gltf::load_meshes(json &gltf_json, std::vector<std::vector<uin
auto &mesh = meshes[mesh_index];
for (auto& primitive: primitives) {
- mesh.parts.emplace_back(load_primitive(gltf_json, primitive, buffers_content));
+ mesh.parts.emplace_back(load_primitive(gltf_json, primitive, buffers_content, materials));
}
}
return std::move(meshes);
}
-mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector<std::vector<uint8_t>> &buffers_content) {
+std::vector<material> gltf::load_materials(json &gltf_json) {
+ auto &gltf_materials = gltf_json["materials"];
+ auto materials_count = gltf_materials.size();
+ std::vector<material> materials { materials_count };
+
+ for(size_t material_index = 0; material_index < materials_count; material_index++) {
+ auto& gltf_material = gltf_materials[material_index];
+ auto& material = materials[material_index];
+
+ auto base_color = gltf_material["baseColorFactor"];
+ material.base_color.v[0] = base_color[0];
+ material.base_color.v[1] = base_color[1];
+ material.base_color.v[2] = base_color[2];
+ material.base_color.v[3] = base_color[3];
+ material.metalness = gltf_material["matellicFactor"].get<float>();
+ material.roughness = gltf_material["roughnessFactor"].get<float>();
+ }
+
+ return std::move(materials);
+}
+
+mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector<std::vector<uint8_t>> &buffers_content, std::vector<material> &materials) {
auto part = mesh_part();
auto &accessors = gltf_json["accessors"];
@@ -116,22 +139,27 @@ mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector<std
}
}
- // {
- // auto normals_accessor_index = primitive["attributes"]["NORMAL"].get<uint32_t>();
- // auto normals_view_index = accessors[normals_accessor_index]["bufferView"].get<uint32_t>();
- // auto normals_view = gltf_json["bufferViews"][normals_view_index];
+ {
+ auto normals_accessor_index = primitive["attributes"]["NORMAL"].get<uint32_t>();
+ auto normals_view_index = accessors[normals_accessor_index]["bufferView"].get<uint32_t>();
+ auto &normals_view = gltf_json["bufferViews"][normals_view_index];
+
+ auto normals_buffer_index = normals_view["buffer"].get<uint32_t>();
+ auto normals_offset = normals_view["byteOffset"].get<size_t>();
+ auto normals_length = normals_view["byteLength"].get<size_t>();
- // auto normals_buffer_index = normals_view["buffer"].get<uint32_t>();
- // auto normals_offset = normals_view["byteOffset"].get<size_t>();
- // auto normals_length = normals_view["byteLength"].get<size_t>();
+ auto &normals_buffer = buffers_content[normals_buffer_index];
+ float* normals_start = (float*)(normals_buffer.data() + normals_offset);
- // auto normals_buffer = buffers_content[normals_buffer_index];
- // void* normals_start = normals_buffer.data() + normals_offset;
+ size_t normals_count = normals_length / (sizeof(float) * 3);
+ part.normals.resize(normals_count);
- // mesh_part.normals.resize(normals_length / (sizeof(float) * 3));
+ for (size_t i = 0; i < normals_count; i++) {
+ part.normals[i] = vec3(normals_start[i * 3], normals_start[i * 3 + 1], normals_start[i * 3 + 2]);
+ }
+ }
- // std::memcpy(mesh_part.normals.data(), normals_start, normals_length);
- // }
+ // part.mat = materials[primitive["material"].get<uint32_t>()];
return std::move(part);
}
diff --git a/src/main.cpp b/src/main.cpp
index 9d3297d..c47aaeb 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -62,13 +62,13 @@ int main() {
auto cam = camera(position, target, v_fov, (float)width / (float)height, aperture, focus_distance);
auto main_scene = scene(cam, width, height);
auto can_render = true;
- // vkrenderer renderer { wnd, sizeof(main_scene.meta), sizeof(main_scene.triangles[0]) * main_scene.triangles.size(), sizeof(main_scene.packed_nodes[0]) * main_scene.packed_nodes.size() };
- vkrenderer renderer { wnd, sizeof(main_scene.meta), sizeof(main_scene.spheres[0]) * main_scene.spheres.size(), sizeof(main_scene.packed_nodes[0]) * main_scene.packed_nodes.size() };
+ vkrenderer renderer { wnd, sizeof(main_scene.meta), sizeof(main_scene.triangles[0]) * main_scene.triangles.size(), sizeof(main_scene.packed_nodes[0]) * main_scene.packed_nodes.size() };
+ // vkrenderer renderer { wnd, sizeof(main_scene.meta), sizeof(main_scene.spheres[0]) * main_scene.spheres.size(), sizeof(main_scene.packed_nodes[0]) * main_scene.packed_nodes.size() };
// Upload scene content
std::memcpy(renderer.scene_buffer_ptr(), &main_scene.meta, sizeof(main_scene.meta));
- // renderer.update_geometry_buffer(main_scene.triangles.data());
- renderer.update_geometry_buffer(main_scene.spheres.data());
+ renderer.update_geometry_buffer(main_scene.triangles.data());
+ // renderer.update_geometry_buffer(main_scene.spheres.data());
renderer.update_bvh_buffer(main_scene.packed_nodes.data());
ImGuiIO& io = ImGui::GetIO();
diff --git a/src/scene.cpp b/src/scene.cpp
index cf22415..a94c603 100644
--- a/src/scene.cpp
+++ b/src/scene.cpp
@@ -6,105 +6,98 @@
#include "utils.hpp"
#include "gltf.hpp"
-void scene::random_scene() {
- material ground_material = { { 0.5, 0.5, 0.5 } };
- spheres.emplace_back(vec3 { 0, -1000, 0 }, ground_material, 1000);
-
- int32_t spheres_count = 11;
- for (int a = -spheres_count; a < spheres_count; a++) {
- for (int b = -spheres_count; b < spheres_count; b++) {
- auto choose_mat = randd();
- point3 center(a + 0.9 * randd(), 0.2, b + 0.9 * randd());
-
- if ((center - point3{ 4, 0.2, 0 }).length() > 0.9) {
- material sphere_material = {};
-
- if (choose_mat < 0.8) {
- // diffuse
- sphere_material.albedo = color::random() * color::random();
- } else if (choose_mat < 0.95) {
- // metal
- sphere_material.albedo = color::random(0.5, 1);
- sphere_material.metalness = randd(0, 0.5);
- sphere_material.roughness = 0.f;
- } else {
- // glass
- // sphere_material.ior = 1.5;
- sphere_material.metalness = 1.f;
- sphere_material.roughness = 0.f;
- }
-
- spheres.emplace_back(center, sphere_material, 0.2);
- }
- }
- }
-
- material material1 = {};
- // material1.ior = 1.5;
- spheres.emplace_back(vec3 { 0, 1, 0 }, material1, 1.0);
-
- material material2 = {};
- material2.albedo = color{ 0.4, 0.2, 0.1 };
- spheres.emplace_back(vec3 { -4, 1, 0 }, material2, 1.0);
-
- material material3 = {};
- material3.albedo = color{ 0.7, 0.6, 0.5 };
- material3.roughness = 0.0;
- spheres.emplace_back(vec3 { 4, 1, 0 }, material3, 1.0);
-
- material material4 = {};
- material4.albedo = color{ 0.0, 0.0, 0.0 };
- material4.emissive = color{ 1.0, 1.0, 1.0 } * 1000.0;
- material4.roughness = 0.0;
- spheres.emplace_back(vec3 { 2, 1.5, 2 }, material4, 0.5);
-
- spheres_count = spheres.size();
-}
+// void scene::random_scene() {
+// material ground_material = { { 0.5, 0.5, 0.5 } };
+// spheres.emplace_back(vec3 { 0, -1000, 0 }, ground_material, 1000);
+//
+// int32_t spheres_count = 11;
+// for (int a = -spheres_count; a < spheres_count; a++) {
+// for (int b = -spheres_count; b < spheres_count; b++) {
+// auto choose_mat = randd();
+// point3 center(a + 0.9 * randd(), 0.2, b + 0.9 * randd());
+//
+// if ((center - point3{ 4, 0.2, 0 }).length() > 0.9) {
+// material sphere_material = {};
+//
+// if (choose_mat < 0.8) {
+// // diffuse
+// sphere_material.base_color = color::random() * color::random();
+// } else if (choose_mat < 0.95) {
+// // metal
+// sphere_material.base_color = color::random(0.5, 1);
+// sphere_material.metalness = randd(0, 0.5);
+// sphere_material.roughness = 0.f;
+// } else {
+// // glass
+// // sphere_material.ior = 1.5;
+// sphere_material.metalness = 1.f;
+// sphere_material.roughness = 0.f;
+// }
+//
+// spheres.emplace_back(center, sphere_material, 0.2);
+// }
+// }
+// }
+//
+// material material1 = {};
+// // material1.ior = 1.5;
+// spheres.emplace_back(vec3 { 0, 1, 0 }, material1, 1.0);
+//
+// material material2 = {};
+// material2.base_color = color{ 0.4, 0.2, 0.1 };
+// spheres.emplace_back(vec3 { -4, 1, 0 }, material2, 1.0);
+//
+// material material3 = {};
+// material3.base_color = color{ 0.7, 0.6, 0.5 };
+// material3.roughness = 0.0;
+// spheres.emplace_back(vec3 { 4, 1, 0 }, material3, 1.0);
+//
+// spheres_count = spheres.size();
+// }
scene::scene(camera cam, uint32_t width, uint32_t height)
:meta(cam, width, height){
- // auto bistro_interior_path = std::filesystem::path("../models/BistroInterior");
- // auto bistro_interior_filename = std::string("BistroInterior.gltf");
- // auto root_node = gltf::load(bistro_interior_path, bistro_interior_filename);
+ auto bistro_interior_path = std::filesystem::path("../models/BistroInterior");
+ auto bistro_interior_filename = std::string("BistroInterior.gltf");
+ auto root_node = gltf::load(bistro_interior_path, bistro_interior_filename);
// auto sponza_path = std::filesystem::path("../models/sponza");
// auto sponza_filename = std::string("Sponza.gltf");
// auto root_node = gltf::load(sponza_path, sponza_filename);
- // std::queue<node> nodes_to_load {};
- // nodes_to_load.push(root_node);
+ std::queue<node> nodes_to_load {};
+ nodes_to_load.push(root_node);
- // while(nodes_to_load.size() != 0) {
- // auto &node = nodes_to_load.front();
+ while(nodes_to_load.size() != 0) {
+ auto &node = nodes_to_load.front();
- // if (node.mesh) {
- // auto &mesh = node.mesh.value();
- // for (auto& mesh_part: mesh.parts) {
- // for (size_t i = 0; i < mesh_part.indices.size(); i += 3) {
- // auto i1 = mesh_part.indices[i];
- // auto i2 = mesh_part.indices[i + 1];
- // auto i3 = mesh_part.indices[i + 2];
+ if (node.mesh) {
+ auto &mesh = node.mesh.value();
+ for (auto& mesh_part: mesh.parts) {
+ for (size_t i = 0; i < mesh_part.indices.size(); i += 3) {
+ auto i1 = mesh_part.indices[i];
+ auto i2 = mesh_part.indices[i + 1];
+ auto i3 = mesh_part.indices[i + 2];
- // auto v1 = vertex { .position = mesh_part.positions[i1] };
- // auto v2 = vertex { .position = mesh_part.positions[i2] };
- // auto v3 = vertex { .position = mesh_part.positions[i3] };
+ auto v1 = vertex { .position = mesh_part.positions[i1], .normal = mesh_part.normals[i1] };
+ auto v2 = vertex { .position = mesh_part.positions[i2], .normal = mesh_part.normals[i2] };
+ auto v3 = vertex { .position = mesh_part.positions[i3], .normal = mesh_part.normals[i3] };
- // triangles.emplace_back(v1, v2, v3);
- // }
- // }
- // }
+ triangles.emplace_back(v1, v2, v3);
+ }
+ }
+ }
- // for (auto &node: node.children) {
- // nodes_to_load.push(node);
- // }
+ for (auto &node: node.children) {
+ nodes_to_load.push(node);
+ }
- // nodes_to_load.pop();
- // }
+ nodes_to_load.pop();
+ }
- random_scene();
+ // random_scene();
- // bvh builder(triangles, packed_nodes);
- bvh builder(spheres, packed_nodes);
- // builder.exporter();
+ bvh builder(triangles, packed_nodes);
+ // bvh builder(spheres, packed_nodes);
}