Model is loaded and added, started to implement ray generation

This commit is contained in:
Gamemaker1998
2025-01-24 23:21:14 +01:00
parent 8af00e2510
commit 8317ccaaba
5 changed files with 37 additions and 15 deletions
+2
View File
@@ -2,6 +2,7 @@
#include "scene/Scene.h" #include "scene/Scene.h"
#include "util/ModelLoader.h" #include "util/ModelLoader.h"
#include "window/Window.h" #include "window/Window.h"
#include "util/ModelLoader.h"
int main() int main()
{ {
@@ -17,6 +18,7 @@ int main()
.height = 1080, .height = 1080,
.numSamples = 10000, .numSamples = 10000,
}); });
while (true) while (true)
{ {
window.update(scene.getImage()); window.update(scene.getImage());
+5
View File
@@ -3,6 +3,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <algorithm> #include <algorithm>
#include "util/Ray.h" #include "util/Ray.h"
#include <iostream>
struct AABB struct AABB
{ {
@@ -45,9 +46,13 @@ struct AABB
} }
bool intersects(Ray ray, float tmin, float tmax) bool intersects(Ray ray, float tmin, float tmax)
{ {
std::cout << "Hello1" << std::endl;
glm::vec3 invD = 1.0f / ray.direction; glm::vec3 invD = 1.0f / ray.direction;
std::cout << "Hello2: " << min.x << " " << min.y << " " << min.z << " " << max.x << " " << max.y << " " << max.z << " " << std::endl;
glm::vec3 t0s = glm::vec3(min - ray.origin) * invD; glm::vec3 t0s = glm::vec3(min - ray.origin) * invD;
std::cout << "Hello3" << std::endl;
glm::vec3 t1s = glm::vec3(max - ray.origin) * invD; glm::vec3 t1s = glm::vec3(max - ray.origin) * invD;
std::cout << "Hello4" << std::endl;
glm::vec3 tsmaller = glm::min(t0s, t1s); glm::vec3 tsmaller = glm::min(t0s, t1s);
glm::vec3 tbigger = glm::max(t0s, t1s); glm::vec3 tbigger = glm::max(t0s, t1s);
+9 -8
View File
@@ -1,6 +1,7 @@
#include "BVH.h" #include "BVH.h"
#include <algorithm> #include <algorithm>
#include <ranges> #include <ranges>
#include <iostream>
void BVH::addModel(PModel model, glm::mat4 transform) void BVH::addModel(PModel model, glm::mat4 transform)
{ {
@@ -10,10 +11,10 @@ void BVH::addModel(PModel model, glm::mat4 transform)
void BVH::addModels(std::vector<PModel> _models, glm::mat4 transform) void BVH::addModels(std::vector<PModel> _models, glm::mat4 transform)
{ {
for (int i = 0; i < _models.size(); ++i) for (auto& _model : _models)
{ {
_models[i]->boundingBox.transform(transform); _model->boundingBox.transform(transform);
models.push_back(std::move(_models[i])); models.push_back(std::move(_model));
} }
} }
@@ -60,13 +61,13 @@ std::optional<IntersectionInfo> BVH::traceRay(Ray ray)
{ {
auto results = generateIntersections(hierarchy, ray); auto results = generateIntersections(hierarchy, ray);
float closestT = std::numeric_limits<float>::max(); float closestT = std::numeric_limits<float>::max();
IntersectionInfo info; std::optional<IntersectionInfo> info = {};
for (uint32_t i = 0; i < results.size(); ++i) for (auto& result : results)
{ {
if (results[i].t < closestT) if (result.t < closestT)
{ {
closestT = results[i].t; closestT = result.t;
info = results[i]; info = result;
} }
} }
if (closestT < std::numeric_limits<float>::max()) if (closestT < std::numeric_limits<float>::max())
+17 -3
View File
@@ -1,8 +1,16 @@
#include "Scene.h" #include "Scene.h"
#include "util/ModelLoader.h"
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
Scene::Scene() {} Scene::Scene()
{
bvh.addModels(ModelLoader::loadModel("../cube.fbx"),
glm::mat4(glm::vec4(1.0f, 0.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 1.0f, 0.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 1.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)));
}
Scene::~Scene() {} Scene::~Scene() {}
@@ -35,8 +43,14 @@ void Scene::render(Camera cam, RenderParameter params)
{ {
for (int h = 0; h < params.height; ++h) for (int h = 0; h < params.height; ++h)
{ {
// Ray r = Ray(); Ray r = Ray{
// bvh.traceRay(r); .origin = glm::vec3(0.0f),
.direction = glm::vec3(0.0f, 0.0f, 1.0f)
};
bvh.traceRay(r);
std::cout << "HELLO" << std::endl;
accumulator[w + h * params.width] += accumulator[w + h * params.width] +=
glm::vec3(w / float(params.width * params.numSamples), h / float(params.height * params.numSamples), 0); glm::vec3(w / float(params.width * params.numSamples), h / float(params.height * params.numSamples), 0);
} }
+2 -2
View File
@@ -28,8 +28,8 @@ std::vector<PModel> ModelLoader::loadModel(std::string_view filename)
model->indices.push_back(face.mIndices[1]); model->indices.push_back(face.mIndices[1]);
model->indices.push_back(face.mIndices[2]); model->indices.push_back(face.mIndices[2]);
auto e0 = model->positions[face.mIndices[1] - face.mIndices[0]]; auto e0 = model->positions[face.mIndices[1]] - model->positions[face.mIndices[0]];
auto e1 = model->positions[face.mIndices[2] - face.mIndices[0]]; auto e1 = model->positions[face.mIndices[2]] - model->positions[face.mIndices[0]];
model->es.push_back(e0); model->es.push_back(e0);
model->es.push_back(e1); model->es.push_back(e1);