aboutsummaryrefslogtreecommitdiffstats
path: root/src
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 /src
parentd071fc5c947689eca7726803a420013c519acdd9 (diff)
White furnace test on gltf scenes & load normals
Diffstat (limited to 'src')
-rw-r--r--src/gltf.cpp60
-rw-r--r--src/main.cpp8
-rw-r--r--src/scene.cpp161
3 files changed, 125 insertions, 104 deletions
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);
}