Skip to content

Commit

Permalink
fix populating triangles, bxdfs, lights, primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
yumcyaWiz committed Nov 21, 2021
1 parent be2f5ee commit 96d5a80
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <filesystem>
#include <memory>
#include <optional>
#include <vector>

#include "core.h"
Expand Down Expand Up @@ -48,6 +49,8 @@ class Scene {
std::vector<float> normals;
std::vector<float> texcoords;

std::vector<std::optional<tinyobj::material_t>> materials;

// triangles
// NOTE: per face
std::vector<Triangle> triangles;
Expand Down Expand Up @@ -120,7 +123,6 @@ class Scene {
const auto& materials = reader.GetMaterials();

// loop over shapes
// populate mesh data, shapes, bxdfs, lights, primitives
for (size_t s = 0; s < shapes.size(); ++s) {
size_t index_offset = 0;
// loop over faces
Expand Down Expand Up @@ -183,6 +185,7 @@ class Scene {
texcoords.push_back(Vec2f(0, 1));
}

// populate vertices, indices, normals, texcoords
for (int i = 0; i < 3; ++i) {
this->vertices.push_back(vertices[i][0]);
this->vertices.push_back(vertices[i][1]);
Expand All @@ -198,40 +201,51 @@ class Scene {
this->indices.push_back(this->indices.size());
}

// create triangle
this->triangles.emplace_back(this->vertices.data(),
this->indices.data(), this->normals.data(),
this->texcoords.data(), f);

// add bxdf
// TODO: remove duplicate
// populate materials
const int materialID = shapes[s].mesh.material_ids[f];
std::optional<tinyobj::material_t> material = std::nullopt;
if (materialID != -1) {
const tinyobj::material_t& m = materials[materialID];
this->bxdfs.push_back(createBxDF(m));
}
// default material
else {
this->bxdfs.push_back(createDefaultBxDF());
material = materials[materialID];
}
this->materials.push_back(material);

// add light
std::shared_ptr<Light> light = nullptr;
if (materialID != -1) {
const tinyobj::material_t& m = materials[materialID];
light =
createAreaLight(m, &this->triangles[this->triangles.size() - 1]);
if (light != nullptr) {
lights.push_back(light);
}
}
index_offset += fv;
}
}

// add primitive
primitives.emplace_back(&this->triangles[this->triangles.size() - 1],
this->bxdfs[this->bxdfs.size() - 1], light);
// populate triangles, bxdfs, lights, primitives
for (size_t faceID = 0; faceID < nFaces(); ++faceID) {
// add triangle
this->triangles.emplace_back(this->vertices.data(), this->indices.data(),
this->normals.data(), this->texcoords.data(),
faceID);

// add bxdf
// TODO: remove duplicate
const auto material = this->materials[faceID];
if (material) {
const tinyobj::material_t& m = material.value();
this->bxdfs.push_back(createBxDF(m));
}
// default material
else {
this->bxdfs.push_back(createDefaultBxDF());
}

index_offset += fv;
// add light
std::shared_ptr<Light> light = nullptr;
if (material) {
const tinyobj::material_t& m = material.value();
light =
createAreaLight(m, &this->triangles[this->triangles.size() - 1]);
if (light != nullptr) {
lights.push_back(light);
}
}

// add primitive
primitives.emplace_back(&this->triangles[this->triangles.size() - 1],
this->bxdfs[this->bxdfs.size() - 1], light);
}

spdlog::info("[Scene] vertices: {}", nVertices());
Expand Down

0 comments on commit 96d5a80

Please sign in to comment.