diff --git a/.clang-format b/.clang-format index c7e273c..d824563 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,5 @@ BasedOnStyle: LLVM -IndentWidth: 4 +IndentWidth: 2 ColumnLimit: 140 DerivePointerAlignment: false PointerAlignment: Left diff --git a/src/util/Model.cpp b/src/util/Model.cpp index 59833a9..710009f 100644 --- a/src/util/Model.cpp +++ b/src/util/Model.cpp @@ -1,6 +1,53 @@ #include "Model.h" -std::optional Model::intersect(Ray ray) +std::optional Model::intersect(const Ray ray) { - return {}; + std::optional intersection = {}; + float distance = 0; + + for(size_t posIndex=0, eIndex=0, normalIndex=0; posIndex 1) + continue; + if(resultVector.y < 0 || resultVector.y > 1) + continue; + if(resultVector.z < 0 || resultVector.z > 1) + continue; + + if(std::abs(1.0f - (resultVector.y + resultVector.z + b3)) > 1e-7) + continue; + if(resultVector.x < 1e-6) + continue; + + if(!intersection.has_value() || resultVector.x < distance) + { + intersection = IntersectionInfo { + .position = ray.origin + ray.direction * resultVector.x, + .normal = n, + .albedo = glm::vec3(0.7f, 0.7f, 0.7f), + .emissive = glm::vec3(0.0f, 0.0f, 0.0f) + }; + distance = resultVector.x; + } + } + + return intersection; } \ No newline at end of file diff --git a/src/util/Model.h b/src/util/Model.h index e599f24..83160e9 100644 --- a/src/util/Model.h +++ b/src/util/Model.h @@ -22,6 +22,8 @@ public: AABB boundingBox; std::vector positions; std::vector indices; + std::vector es; + std::vector faceNormals; std::optional intersect(Ray ray); }; DECLARE_REF(Model) \ No newline at end of file diff --git a/src/util/ModelLoader.cpp b/src/util/ModelLoader.cpp index b96b618..c2330f8 100644 --- a/src/util/ModelLoader.cpp +++ b/src/util/ModelLoader.cpp @@ -27,6 +27,13 @@ std::vector ModelLoader::loadModel(std::string_view filename) model->indices.push_back(face.mIndices[0]); model->indices.push_back(face.mIndices[1]); model->indices.push_back(face.mIndices[2]); + + auto e0 = model->positions[face.mIndices[1] - face.mIndices[0]]; + auto e1 = model->positions[face.mIndices[2] - face.mIndices[0]]; + model->es.push_back(e0); + model->es.push_back(e1); + + model->faceNormals.push_back(glm::cross(e0, e1)); } model->boundingBox = aabb; result.push_back(std::move(model));