marking it much faster

This commit is contained in:
Dynamitos
2025-01-25 18:05:47 +01:00
parent 86b6d678f6
commit 463e3d925e
18 changed files with 287 additions and 148 deletions
+4
View File
@@ -6,4 +6,8 @@ target_sources(RayTracer
ModelLoader.h
ModelLoader.cpp
Ray.h
Texture.h
Texture.cpp
TextureLoader.h
TextureLoader.cpp
)
+23 -2
View File
@@ -1,6 +1,26 @@
#include "Model.h"
std::optional<IntersectionInfo> Model::intersect(const Ray ray)
void Model::transform(glm::mat4 matrix)
{
for (auto& pos : positions)
{
pos = glm::vec3(matrix * glm::vec4(pos, 1));
}
boundingBox.transform(matrix);
for (int i = 0; i < indices.size(); i+=3)
{
auto e0 = positions[indices[i + 1]] - positions[indices[i + 0]];
auto e1 = positions[indices[i + 2]] - positions[indices[i + 0]];
es.push_back(e0);
es.push_back(e1);
faceNormals.push_back(glm::cross(e0, e1));
}
}
std::optional<IntersectionInfo> Model::intersect(const Ray ray) const
{
std::optional<IntersectionInfo> intersection = {};
float distance = 0;
@@ -50,4 +70,5 @@ std::optional<IntersectionInfo> Model::intersect(const Ray ray)
}
return intersection;
}
}
+8 -7
View File
@@ -1,8 +1,8 @@
#pragma once
#include "Minimal.h"
#include "scene/AABB.h"
#include <vector>
#include <optional>
#include <vector>
// material infos
// shading parameter
@@ -19,11 +19,12 @@ struct IntersectionInfo
class Model
{
public:
AABB boundingBox;
std::vector<glm::vec3> positions;
std::vector<uint32_t> indices;
std::vector<glm::vec3> es;
std::vector<glm::vec3> faceNormals;
std::optional<IntersectionInfo> intersect(Ray ray);
AABB boundingBox;
std::vector<glm::vec3> positions;
std::vector<uint32_t> indices;
std::vector<glm::vec3> es;
std::vector<glm::vec3> faceNormals;
void transform(glm::mat4 matrix);
std::optional<IntersectionInfo> intersect(Ray ray) const;
};
DECLARE_REF(Model)
-6
View File
@@ -28,12 +28,6 @@ std::vector<PModel> ModelLoader::loadModel(std::string_view filename)
model->indices.push_back(face.mIndices[1]);
model->indices.push_back(face.mIndices[2]);
auto e0 = model->positions[face.mIndices[1]] - model->positions[face.mIndices[0]];
auto e1 = model->positions[face.mIndices[2]] - model->positions[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));
+8
View File
@@ -0,0 +1,8 @@
#include "Texture.h"
glm::vec3 Texture::sample(glm::vec2 texCoords) const
{
uint32_t x = texCoords.x * width;
uint32_t y = texCoords.y * height;
return textureData[y * width + x];
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "Minimal.h"
#include <glm/glm.hpp>
#include <ktx.h>
#include <vector>
class Texture
{
public:
glm::vec3 sample(glm::vec2 texCoords) const;
std::vector<glm::vec3> textureData;
int width;
int height;
};
DECLARE_REF(Texture)
+19
View File
@@ -0,0 +1,19 @@
#include "TextureLoader.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
PTexture TextureLoader::loadTexture(std::string_view filename)
{
int x, y, n;
auto* data = stbi_load(filename.data(), &x, &y, &n, 3);
std::vector<glm::vec3> texData(x * y);
for (uint32_t i = 0; i < texData.size(); ++i)
{
texData[i] = glm::vec3(data[i * 3 + 0] / 256.f, data[i * 3 + 1] / 256.f, data[i * 3 + 2] / 256.f);
}
auto result = std::make_unique<Texture>();
result->textureData = std::move(texData);
result->width = x;
result->height = y;
return result;
}
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#include <string_view>
#include "Texture.h"
class TextureLoader
{
public:
static PTexture loadTexture(std::string_view filename);
private:
};