Skip to content

Commit

Permalink
[#1] Started on BVH
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenz Reithmayr committed Jun 28, 2024
1 parent 6a4b0e0 commit 155e3d4
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 79 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
cmake_minimum_required(VERSION 3.23)
set(CMAKE_POLICY_DEFAULT_CMP0135 NEW)

project(cannoli)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -fconcepts -pthread -DAABB_INT")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -fconcepts -pthread -DAABB_INT")

# === PROJECT SETUP ===

Expand All @@ -20,6 +21,7 @@ add_library(${PROJECT_NAME} SHARED
src/metal_brdf.cpp
src/mesh.cpp
src/aabb.cpp
src/bvh.cpp

# Header Files
include/utils.h
Expand All @@ -40,7 +42,9 @@ add_library(${PROJECT_NAME} SHARED
include/metal_brdf.h
include/mesh.h
include/obj_file_loader.h
include/aabb.h)
include/aabb.h
include/bvh.h
)

# === DEPENDENCIES ===

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

### "Leave the gun, take the cannoli" - Goodfellas, starring Ray Liotta. Ray...get it?

##

![img.png](img.png)
##
This is (rather, is going to be) my implementation of
the [RTiOWE](https://raytracing.github.io/books/RayTracingInOneWeekend.html#overview) ray tracer.

It follows RTiOWE in
spirit, but the architecture is going to be substantially different, in order to implement more advanced features down
spirit, but the architecture is going to be substantially different in order to implement more advanced features down
the line.

Heavy inspiration is drawn from [Manta Ray](https://github.com/ange-yaghi/manta-ray) by Anghe Yaghi.
Expand Down
Binary file added img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions include/bvh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef CANNOLI_SRC_BVH_H_
#define CANNOLI_SRC_BVH_H_

#include "aabb.h"

namespace cannoli {
class BVHNode {
public:
BVHNode(AABB &boundingBox, std::vector<int> &triangles);

protected:
AABB m_boundingBox;
BVHNode *m_childA{nullptr};
BVHNode *m_childB{nullptr};
std::vector<int> m_associatedTriangleIndices;
};

class BVH {
public:
BVH() = default;

protected:
std::vector<BVHNode> m_nodes;
};
} // namespace cannoli
#endif //CANNOLI_SRC_BVH_H_

2 changes: 1 addition & 1 deletion include/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Canvas {
m_height = static_cast<int>(m_width / m_aspectRatio);
}

void setAspectRation(float aspect_ratio) {
void setAspectRatio(float aspect_ratio) {
m_aspectRatio = aspect_ratio;
}

Expand Down
22 changes: 14 additions & 8 deletions include/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "material.h"
#include "object.h"
#include "aabb.h"
#include "bvh.h"

#include <string>
#include <memory>
Expand All @@ -27,7 +28,7 @@ class Mesh {
}

[[nodiscard]] int getFaceCount() const {
return m_faceCount;
return m_triangleCount;
}

[[nodiscard]] std::shared_ptr<Material> getMaterial() const {
Expand All @@ -38,14 +39,19 @@ class Mesh {
return std::make_shared<AABB>(m_aabb);
}

bool computeTriangleIntersection(LightRay &ray,
const float &t_min,
const float &t_max,
HitRecord &hit_record,
int triangle_nr);
[[nodiscard]] std::shared_ptr<BVH> getBBVH() const {
return std::make_shared<BVH>(m_bvh);
}

bool computeTriangleIntersection(
LightRay &ray,
const float &t_min,
const float &t_max,
HitRecord &hit_record,
int triangle_nr);
LightRay computeSurfaceInteraction(const LightRay &ray, const HitRecord &hit_record);
void computeAABB();
void constructBVH();

private:
std::vector<cannoli::Vec3f> m_vertices;
Expand All @@ -54,9 +60,9 @@ class Mesh {
std::shared_ptr<Material> m_meshMaterial;

private:
int m_faceCount = 0;
int m_triangleCount = 0;
AABB m_aabb;

BVH m_bvh;
};
} // namespace cannoli
#endif //CANNOLI_SRC_MESH_H_
85 changes: 55 additions & 30 deletions include/obj_file_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// OBJ_Loader.h - A Single Header OBJ Model Loader

#pragma once

// Iostream - STD I/O Library
#include <iostream>

// Vector - STD Vector/Array Library
#include <numbers>
#include <vector>

// String - STD String Library
Expand Down Expand Up @@ -549,9 +549,11 @@ class Loader {
while (1) {
tempMesh.MeshName = meshname + "_" + std::to_string(i);

for (auto &m : LoadedMeshes)
if (m.MeshName == tempMesh.MeshName)
for (auto &m : LoadedMeshes) {
if (m.MeshName == tempMesh.MeshName) {
continue;
}
}
break;
}

Expand Down Expand Up @@ -788,12 +790,15 @@ class Loader {
if (tVerts.size() == 3) {
// Create a triangle from pCur, pPrev, pNext
for (int j = 0; j < int(tVerts.size()); j++) {
if (iVerts[j].Position == pCur.Position)
if (iVerts[j].Position == pCur.Position) {
oIndices.push_back(j);
if (iVerts[j].Position == pPrev.Position)
}
if (iVerts[j].Position == pPrev.Position) {
oIndices.push_back(j);
if (iVerts[j].Position == pNext.Position)
}
if (iVerts[j].Position == pNext.Position) {
oIndices.push_back(j);
}
}

tVerts.clear();
Expand All @@ -802,12 +807,15 @@ class Loader {
if (tVerts.size() == 4) {
// Create a triangle from pCur, pPrev, pNext
for (int j = 0; j < int(iVerts.size()); j++) {
if (iVerts[j].Position == pCur.Position)
if (iVerts[j].Position == pCur.Position) {
oIndices.push_back(j);
if (iVerts[j].Position == pPrev.Position)
}
if (iVerts[j].Position == pPrev.Position) {
oIndices.push_back(j);
if (iVerts[j].Position == pNext.Position)
}
if (iVerts[j].Position == pNext.Position) {
oIndices.push_back(j);
}
}

Vector3 tempVec;
Expand All @@ -821,23 +829,28 @@ class Loader {

// Create a triangle from pCur, pPrev, pNext
for (int j = 0; j < int(iVerts.size()); j++) {
if (iVerts[j].Position == pPrev.Position)
if (iVerts[j].Position == pPrev.Position) {
oIndices.push_back(j);
if (iVerts[j].Position == pNext.Position)
}
if (iVerts[j].Position == pNext.Position) {
oIndices.push_back(j);
if (iVerts[j].Position == tempVec)
}
if (iVerts[j].Position == tempVec) {
oIndices.push_back(j);
}
}

tVerts.clear();
break;
}

// If Vertex is not an interior vertex
float angle = math::AngleBetweenV3(pPrev.Position - pCur.Position, pNext.Position - pCur.Position)
* (180 / 3.14159265359);
if (angle <= 0 && angle >= 180)
float angle = NAN;
angle = math::AngleBetweenV3(pPrev.Position - pCur.Position, pNext.Position - pCur.Position)
* (180 / std::numbers::pi);
if (angle <= 0 && angle >= 180) {
continue;
}

// If any vertices are within this triangle
bool inTri = false;
Expand All @@ -849,17 +862,21 @@ class Loader {
break;
}
}
if (inTri)
if (inTri) {
continue;
}

// Create a triangle from pCur, pPrev, pNext
for (int j = 0; j < int(iVerts.size()); j++) {
if (iVerts[j].Position == pCur.Position)
if (iVerts[j].Position == pCur.Position) {
oIndices.push_back(j);
if (iVerts[j].Position == pPrev.Position)
}
if (iVerts[j].Position == pPrev.Position) {
oIndices.push_back(j);
if (iVerts[j].Position == pNext.Position)
}
if (iVerts[j].Position == pNext.Position) {
oIndices.push_back(j);
}
}

// Delete pCur from the list
Expand All @@ -876,26 +893,30 @@ class Loader {
}

// if no triangles were created
if (oIndices.size() == 0)
if (oIndices.size() == 0) {
break;
}

// if no more vertices
if (tVerts.size() == 0)
if (tVerts.size() == 0) {
break;
}
}
}

// Load Materials from .mtl file
bool LoadMaterials(std::string path) {
bool LoadMaterials(const std::string& path) {
// If the file is not a material file return false
if (path.substr(path.size() - 4, path.size()) != ".mtl")
if (path.substr(path.size() - 4, path.size()) != ".mtl") {
return false;
}

std::ifstream file(path);

// If the file is not found return false
if (!file.is_open())
if (!file.is_open()) {
return false;
}

Material tempMaterial;

Expand Down Expand Up @@ -935,8 +956,9 @@ class Loader {
std::vector<std::string> temp;
algorithm::split(algorithm::tail(curline), temp, " ");

if (temp.size() != 3)
if (temp.size() != 3) {
continue;
}

tempMaterial.Ka.X = std::stof(temp[0]);
tempMaterial.Ka.Y = std::stof(temp[1]);
Expand All @@ -947,8 +969,9 @@ class Loader {
std::vector<std::string> temp;
algorithm::split(algorithm::tail(curline), temp, " ");

if (temp.size() != 3)
if (temp.size() != 3) {
continue;
}

tempMaterial.Kd.X = std::stof(temp[0]);
tempMaterial.Kd.Y = std::stof(temp[1]);
Expand All @@ -959,8 +982,9 @@ class Loader {
std::vector<std::string> temp;
algorithm::split(algorithm::tail(curline), temp, " ");

if (temp.size() != 3)
if (temp.size() != 3) {
continue;
}

tempMaterial.Ks.X = std::stof(temp[0]);
tempMaterial.Ks.Y = std::stof(temp[1]);
Expand Down Expand Up @@ -1016,11 +1040,12 @@ class Loader {

// Test to see if anything was loaded
// If not return false
if (LoadedMaterials.empty())
if (LoadedMaterials.empty()) {
return false;
// If so return true
else
} else {
return true;
}
}
};
}
} // namespace objl
Loading

0 comments on commit 155e3d4

Please sign in to comment.