Adding normals

This commit is contained in:
IlyaShurupov 2023-10-17 20:51:25 +03:00
parent cfb4c0cfcc
commit 97f79a6483
6 changed files with 882 additions and 878 deletions

View file

@ -70,31 +70,31 @@ const Vec3F& TrigCache::getHitPos() { return gHitPos; }
const Vec3F& TrigCache::getNormal() const { return mNormal; } const Vec3F& TrigCache::getNormal() const { return mNormal; }
void Topology::addTrig(const Vec3F& v1, const Vec3F& v2, const Vec3F& v3) { void TopologyCache::updateCache() {
auto trigIdx = mPoints.size(); TransformedNormals.clear();
TransformedPoints.clear();
TrigCaches.clear();
mPoints.append(v1); if (!Source) {
mPoints.append(v2); return;
mPoints.append(v3);
TrigCache newTrig(trigIdx, trigIdx + 1, trigIdx + 2);
mTrigCaches.append(newTrig);
}
void Topology::transformPoint(Vec3F& vert) {
mBasis.transform(vert);
vert += mOrigin;
}
void Topology::updateTransformed() {
mPointsTransformed = mPoints;
for (auto idx : Range(mPointsTransformed.size())) {
transformPoint(mPointsTransformed[idx]);
} }
for (auto idx : Range(mTrigCaches.size())) {
mTrigCaches[idx].updateCache(mPointsTransformed); TransformedPoints.reserve(Source->Points.size());
for (auto idx : Range(TransformedPoints.size())) {
TransformedPoints[idx] = Source->Basis.transform(Source->Points[idx]);
TransformedPoints[idx] += Source->Origin;
}
TransformedNormals.reserve(Source->Normals.size());
for (auto idx : Range(TransformedNormals.size())) {
TransformedNormals[idx] = Source->Basis.transform(Source->Normals[idx]);
}
TrigCaches.reserve(Source->Indexes.size());
for (auto idx : Range(TrigCaches.size())) {
TrigCaches[idx].mP1 = Source->Indexes[idx].x;
TrigCaches[idx].mP2 = Source->Indexes[idx].y;
TrigCaches[idx].mP3 = Source->Indexes[idx].z;
TrigCaches[idx].updateCache(TransformedPoints);
} }
} }
const Buffer<TrigCache>& Topology::getTrigs() const { return mTrigCaches; }

View file

@ -14,13 +14,9 @@ namespace tp {
public: public:
Mat() = default; Mat() = default;
explicit Mat(const Type& val) { explicit Mat(const Type& val) { operator=(val); }
operator=(val);
}
Mat(const Mat& in) { Mat(const Mat& in) { operator=(in); }
operator=(in);
}
MVec& operator[](alni i) { MVec& operator[](alni i) {
DEBUG_ASSERT(i < tNColoumns && i >= 0) DEBUG_ASSERT(i < tNColoumns && i >= 0)
@ -33,7 +29,9 @@ namespace tp {
} }
Mat& operator=(const Mat& in) { Mat& operator=(const Mat& in) {
if (&in == this) return *this; if (&in == this) {
return *this;
}
memCopy(this, &in, sizeof(Mat<Type, tNRows, tNColoumns>)); memCopy(this, &in, sizeof(Mat<Type, tNRows, tNColoumns>));
return *this; return *this;
} }
@ -220,7 +218,7 @@ namespace tp {
} }
Mat& transpose() { Mat& transpose() {
static_assert( tNRows == tNColoumns); static_assert(tNRows == tNColoumns);
for (halni i = 0; i < tNColoumns; i++) { for (halni i = 0; i < tNColoumns; i++) {
for (halni j = i + 1; j < tNColoumns; j++) { for (halni j = i + 1; j < tNColoumns; j++) {
swap((*this)[i][j], (*this)[j][i]); swap((*this)[i][j], (*this)[j][i]);
@ -229,13 +227,9 @@ namespace tp {
return *this; return *this;
} }
Mat operator*(const Mat& in) const { Mat operator*(const Mat& in) const { return transform(in); }
return transform(in);
}
MVec operator*(const MVec& in) const { MVec operator*(const MVec& in) const { return transform(in); }
return transform(in);
}
Mat<Type, tNRows - 1, tNRows - 1> minor(halni p, halni q) { Mat<Type, tNRows - 1, tNRows - 1> minor(halni p, halni q) {
Mat<Type, tNRows - 1, tNRows - 1> out; Mat<Type, tNRows - 1, tNRows - 1> out;
@ -255,7 +249,7 @@ namespace tp {
} }
Type det() { Type det() {
static_assert( tNRows == tNColoumns); static_assert(tNRows == tNColoumns);
Type out = 0; Type out = 0;
@ -275,7 +269,7 @@ namespace tp {
} }
Mat cofactors() { Mat cofactors() {
static_assert( tNRows == tNColoumns); static_assert(tNRows == tNColoumns);
Mat out; Mat out;
for (int i = 0; i < tNRows; i++) { for (int i = 0; i < tNRows; i++) {
for (int j = 0; j < tNRows; j++) { for (int j = 0; j < tNRows; j++) {
@ -305,12 +299,9 @@ namespace tp {
MVec j; MVec j;
public: public:
Mat() = default; Mat() = default;
explicit Mat(const Type& val) { explicit Mat(const Type& val) { operator=(val); }
operator=(val);
}
Mat(const MVec& pi, const MVec& pj) { Mat(const MVec& pi, const MVec& pj) {
i.x = pi.x; i.x = pi.x;
@ -326,9 +317,7 @@ namespace tp {
j.y = jy; j.y = jy;
} }
Mat(const Mat& in) { Mat(const Mat& in) { operator=(in); }
operator=(in);
}
MVec& operator[](alni idx) { MVec& operator[](alni idx) {
DEBUG_ASSERT(idx < 2 && idx >= 0) DEBUG_ASSERT(idx < 2 && idx >= 0)
@ -371,9 +360,7 @@ namespace tp {
return *this; return *this;
} }
Mat operator-() { Mat operator-() { return Mat(-i.x, -i.y, -j.x, -j.y); }
return Mat(-i.x, -i.y, -j.x, -j.y);
}
Mat& operator+=(const Mat& in) { Mat& operator+=(const Mat& in) {
i.x += in.i.x; i.x += in.i.x;
@ -478,9 +465,7 @@ namespace tp {
} }
// Matrix Properties // Matrix Properties
MVec transform(const MVec& in) const { MVec transform(const MVec& in) const { return MVec(i.x * in.x + i.y * in.y, j.x * in.y + j.y * in.x); }
return MVec(i.x * in.x + i.y * in.y, j.x * in.y + j.y * in.x);
}
Mat transform(const Mat& in) const { Mat transform(const Mat& in) const {
Mat out; Mat out;
@ -491,22 +476,16 @@ namespace tp {
return out; return out;
} }
Mat operator*(const Mat& in) { Mat operator*(const Mat& in) { return transform(in); }
return transform(in);
}
Mat& transpose() { Mat& transpose() {
swap(j.x, i.y); swap(j.x, i.y);
return *this; return *this;
} }
Type det() { Type det() { return i.x * j.y - i.y * j.x; }
return i.x * j.y - i.y * j.x;
}
Mat cofactors() { Mat cofactors() { return Mat(j.y, -i.y, -j.x, i.x); }
return Mat(j.y, -i.y, -j.x, i.x);
}
Mat inv() { Mat inv() {
Type detV = det(); Type detV = det();
@ -574,10 +553,15 @@ namespace tp {
// create on stack // create on stack
Mat operator+(const Mat& in) { return Mat(I + in.I, J + in.J, K + in.K); } Mat operator+(const Mat& in) { return Mat(I + in.I, J + in.J, K + in.K); }
Mat operator-(const Mat& in) { return Mat(I - in.I, J - in.J, K - in.K); } Mat operator-(const Mat& in) { return Mat(I - in.I, J - in.J, K - in.K); }
Mat operator+(Type val) { return Mat(I + val, J + val, K + val); } Mat operator+(Type val) { return Mat(I + val, J + val, K + val); }
Mat operator-(Type val) { return Mat(I - val, J - val, K - val); } Mat operator-(Type val) { return Mat(I - val, J - val, K - val); }
Mat operator*(Type val) { return Mat(I * val, J * val, K * val); } Mat operator*(Type val) { return Mat(I * val, J * val, K * val); }
Mat operator/(Type val) { return Mat(I / val, J / val, K / val); } Mat operator/(Type val) { return Mat(I / val, J / val, K / val); }
// write // write
@ -631,29 +615,17 @@ namespace tp {
} }
// Matrix transformation // Matrix transformation
vec transform(const vec& in) { vec transform(const vec& in) const { return vec(I.x * in.x + I.y * in.y + I.z * in.z, J.x * in.x + J.y * in.y + J.z * in.z, K.x * in.x + K.y * in.y + K.z * in.z); }
return vec(
I.x * in.x + I.y * in.y + I.z * in.z,
J.x * in.x + J.y * in.y + J.z * in.z,
K.x * in.x + K.y * in.y + K.z * in.z
);
}
Mat transform(const Mat& in) { Mat transform(const Mat& in) {
return Mat( return Mat({(in.I.x * I.x + in.I.y * J.x + in.I.z * K.x), (in.I.x * I.y + in.I.y * J.y + in.I.z * K.y), (in.I.x * I.z + in.I.y * J.z + in.I.z * K.z)},
{(in.I.x * I.x + in.I.y * J.x + in.I.z * K.x), (in.I.x * I.y + in.I.y * J.y + in.I.z * K.y), (in.I.x * I.z + in.I.y * J.z + in.I.z * K.z)},
{(in.J.x * I.x + in.J.y * J.x + in.J.z * K.x), (in.J.x * I.y + in.J.y * J.y + in.J.z * K.y), (in.J.x * I.z + in.J.y * J.z + in.J.z * K.z)}, {(in.J.x * I.x + in.J.y * J.x + in.J.z * K.x), (in.J.x * I.y + in.J.y * J.y + in.J.z * K.y), (in.J.x * I.z + in.J.y * J.z + in.J.z * K.z)},
{(in.K.x * I.x + in.K.y * J.x + in.K.z * K.x), (in.K.x * I.y + in.K.y * J.y + in.K.z * K.y), (in.K.x * I.z + in.K.y * J.z + in.K.z * K.z)} {(in.K.x * I.x + in.K.y * J.x + in.K.z * K.x), (in.K.x * I.y + in.K.y * J.y + in.K.z * K.y), (in.K.x * I.z + in.K.y * J.z + in.K.z * K.z)});
);
} }
vec operator*(const vec& in) { vec operator*(const vec& in) { return transform(in); }
return transform(in);
}
Mat operator*(const Mat& in) { Mat operator*(const Mat& in) { return transform(in); }
return transform(in);
}
Mat<Type, 2, 2> minor(halni, halni) { Mat<Type, 2, 2> minor(halni, halni) {
Mat<Type, 2, 2> out; Mat<Type, 2, 2> out;
@ -667,33 +639,23 @@ namespace tp {
return *this; return *this;
} }
Type det() { Type det() { return (+I.x * (J.y * K.z - J.z * K.y) - I.y * (J.x * K.z - J.z * K.x) + I.z * (J.x * K.y - J.y * K.x)); }
return (
+I.x * (J.y * K.z - J.z * K.y)
- I.y * (J.x * K.z - J.z * K.x)
+ I.z * (J.x * K.y - J.y * K.x)
);
}
Mat cofactors() { Mat cofactors() {
return Mat( return Mat(vec(+(J.y * K.z - J.z * K.y), -(I.y * K.z - I.z * K.y), +(I.y * J.z - I.z * J.y)),
vec(+(J.y * K.z - J.z * K.y), -(I.y * K.z - I.z * K.y), +(I.y * J.z - I.z * J.y)),
vec(-(J.x * K.z - J.z * K.x), +(I.x * K.z - I.z * K.x), -(I.x * J.z - I.z * J.x)), vec(-(J.x * K.z - J.z * K.x), +(I.x * K.z - I.z * K.x), -(I.x * J.z - I.z * J.x)),
vec(+(J.x * K.y - J.y * K.x), -(I.x * K.y - I.y * K.x), +(I.x * J.y - I.y * J.x)) vec(+(J.x * K.y - J.y * K.x), -(I.x * K.y - I.y * K.x), +(I.x * J.y - I.y * J.x)));
);
} }
Mat inv() { Mat inv() { return cofactors() /= det(); }
return cofactors() /= det();
}
Mat rotatorX(alnf angle) { Mat rotatorX(alnf angle) {
alnf cosA = (alnf) cos(angle); alnf cosA = (alnf) cos(angle);
alnf sinA = (alnf) sin(angle); alnf sinA = (alnf) sin(angle);
return { return {
{1, 0, 0}, {1, 0, 0 },
{0, cosA, -sinA}, {0, cosA, -sinA},
{0, sinA, cosA} {0, sinA, cosA }
}; };
} }
@ -702,7 +664,7 @@ namespace tp {
alnf sinA = (alnf) sin(angle); alnf sinA = (alnf) sin(angle);
return { return {
{cosA, 0, sinA}, {cosA, 0, sinA},
{0, 1, 0}, {0, 1, 0 },
{-sinA, 0, cosA} {-sinA, 0, cosA}
}; };
} }
@ -741,28 +703,22 @@ namespace tp {
} }
}; };
template <typename Type > template <typename Type>
using Mat4 = Mat<Type, 4, 4>; using Mat4 = Mat<Type, 4, 4>;
using Mat4F = Mat4<halnf>; using Mat4F = Mat4<halnf>;
using Mat4I = Mat4<halni>; using Mat4I = Mat4<halni>;
template <typename Type > template <typename Type>
class Mat< Type, 0, 0 > { class Mat<Type, 0, 0> {
typedef Vec<Type, 1> MVec; typedef Vec<Type, 1> MVec;
MVec dummy; MVec dummy;
public: public:
MVec& operator[](alni) { MVec& operator[](alni) { return dummy; }
return dummy;
}
const MVec& operator[](alni) const { const MVec& operator[](alni) const { return dummy; }
return dummy;
}
Type det() { Type det() { return Type(); }
return Type();
}
}; };
} }

View file

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "Camera.hpp"
#include "Buffer.hpp" #include "Buffer.hpp"
#include "Camera.hpp"
namespace tp { namespace tp {
@ -20,7 +20,7 @@ namespace tp {
TrigCache(ualni v1, ualni v2, ualni v3); TrigCache(ualni v1, ualni v2, ualni v3);
public: public:
static const Vec3F& getHitPos() ; static const Vec3F& getHitPos();
[[nodiscard]] const Vec3F& getNormal() const; [[nodiscard]] const Vec3F& getNormal() const;
public: public:
@ -28,22 +28,26 @@ namespace tp {
[[nodiscard]] bool castRay(const Ray& ray) const; [[nodiscard]] bool castRay(const Ray& ray) const;
}; };
class Topology { struct Topology {
mat3f mBasis; Vec3F Origin = {0, 0, 0};
Vec3F mOrigin; mat3f Basis = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
};
Buffer<Vec3F> mPoints; Buffer<Vec3F> Points;
Buffer<Vec3F> mPointsTransformed; Buffer<Vec3F> Normals;
Buffer<TrigCache> mTrigCaches; Buffer<Vec3I> Indexes;
};
public: struct TopologyCache {
Topology() = default; const Topology* Source = nullptr;
~Topology() = default;
public: Buffer<Vec3F> TransformedPoints;
void addTrig(const Vec3F& v1, const Vec3F& v2, const Vec3F& v3); Buffer<Vec3F> TransformedNormals;
void transformPoint(Vec3F& vert); Buffer<TrigCache> TrigCaches;
void updateTransformed();
[[nodiscard]] const Buffer<TrigCache>& getTrigs() const; void updateCache();
}; };
} }

View file

@ -19,21 +19,28 @@ bool loadMeshes(tp::Scene& scene, const tp::String& objetsPath) {
} }
for (auto& curMesh : Loader.LoadedMeshes) { for (auto& curMesh : Loader.LoadedMeshes) {
scene.mObjects.append(Topology()); scene.mObjects.append(Object());
auto object = &scene.mObjects.last(); auto object = &scene.mObjects.last();
for (int j = 0; j < curMesh.Indices.size(); j += 3) { for (auto& vertex : curMesh.Vertices) {
unsigned int idx1 = curMesh.Indices[j]; object->mTopology.Points.append(Vec3F {vertex.Position.X, vertex.Position.Y, vertex.Position.Z});
unsigned int idx2 = curMesh.Indices[j + 1]; object->mTopology.Normals.append(Vec3F {vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z});
unsigned int idx3 = curMesh.Indices[j + 2];
Vec3F v1 = {curMesh.Vertices[idx1].Position.X, curMesh.Vertices[idx1].Position.Y, curMesh.Vertices[idx1].Position.Z};
Vec3F v2 = {curMesh.Vertices[idx2].Position.X, curMesh.Vertices[idx2].Position.Y, curMesh.Vertices[idx2].Position.Z};
Vec3F v3 = {curMesh.Vertices[idx3].Position.X, curMesh.Vertices[idx3].Position.Y, curMesh.Vertices[idx3].Position.Z};
object->addTrig(v1, v2, v3);
} }
object->updateTransformed();
for (int j = 0; j < curMesh.Indices.size(); j += 3) {
int idx1 = (int) curMesh.Indices[j];
int idx2 = (int) curMesh.Indices[j + 1];
int idx3 = (int) curMesh.Indices[j + 2];
object->mTopology.Indexes.append(Vec3I {idx1, idx2, idx3});
}
if (object->mTopology.Normals.size() != object->mTopology.Points.size()) {
printf("Logic error loading normals\n");
}
object->mCache.Source = &object->mTopology;
object->mCache.updateCache();
} }
return scene.mObjects.size(); return scene.mObjects.size();

View file

@ -22,7 +22,7 @@ void RayTracer::castRay(const Ray& ray, RayCastData& out, alnf far) {
far *= far; far *= far;
for (auto obj : mScene->mObjects) { for (auto obj : mScene->mObjects) {
for (auto trig : obj->getTrigs()) { for (auto trig : obj->mCache.TrigCaches) {
if (trig->castRay(ray)) { if (trig->castRay(ray)) {
// printf("Hit\n"); // printf("Hit\n");
@ -77,15 +77,46 @@ void RayTracer::render(const Scene& scene, RayTracer::RenderBuffer& buff) {
castRay(ray, castData, mScene->mCamera.getFar()); castRay(ray, castData, mScene->mCamera.getFar());
if (castData.hit) { if (castData.hit) {
auto normal = castData.trig->getNormal(); Vec3F normal = castData.trig->getNormal();
if (1) {
const auto& points = castData.obj->mCache.TransformedPoints;
const auto& normals = castData.obj->mCache.TransformedNormals;
const auto trig = castData.trig;
const auto& n1 = normals[trig->mP1];
const auto& n2 = normals[trig->mP2];
const auto& n3 = normals[trig->mP3];
auto v0 = points[trig->mP1];
auto v1 = points[trig->mP2];
auto v2 = points[trig->mP3];
// Calculate barycentric coordinates
Vec3F barycentric;
// Calculate the area of the triangle
auto areaABC = (halnf) (v1 - v0).cross(v2 - v0).length();
auto areaPBC = (halnf) (v1 - castData.hitPos).cross(v2 - castData.hitPos).length();
auto areaPCA = (halnf) (v2 - castData.hitPos).cross(v0 - castData.hitPos).length();
// Calculate the barycentric coordinates
barycentric.x = areaPBC / areaABC;
barycentric.y = areaPCA / areaABC;
barycentric.z = 1.0f - barycentric.x - barycentric.y;
// Interpolate the normal using barycentric coordinates
normal = n1 * barycentric.x + n2 * barycentric.y + n3 * barycentric.z;
}
normal.normalize(); normal.normalize();
RGBA col = {normal.x * 0.5f + 0.5f, normal.y * 0.5f + 0.5f, normal.z * 0.5f + 0.5f, 1.f}; RGBA col = {normal.x * 0.5f + 0.5f, normal.y * 0.5f + 0.5f, normal.z * 0.5f + 0.5f, 1.f};
auto depth = 1 - clamp((halnf) (castData.hitPos - ray.pos).length(), 0.f, mScene->mCamera.getFar()) / mScene->mCamera.getFar();
// buff.set({i, j}, {depth, depth, depth, 1.f});
buff.set({i, j}, col); buff.set({i, j}, col);
// auto depth = 1 - clamp((halnf) (castData.hitPos - ray.pos).length(), 0.f, mScene->mCamera.getFar()) / mScene->mCamera.getFar();
// buff.set({i, j}, {depth, depth, depth, 1.f});
} else { } else {
buff.set({i, j}, RGBA(0.f, 0.f, 0.f, 0.f)); buff.set({i, j}, RGBA(0.f, 0.f, 0.f, 0.f));
} }

View file

@ -12,10 +12,16 @@ namespace tp {
extern ModuleManifest gModuleRayTracer; extern ModuleManifest gModuleRayTracer;
class Scene { class Object {
public: public:
typedef Topology Object; Object() = default;
public:
Topology mTopology;
TopologyCache mCache;
};
class Scene {
public: public:
Scene() = default; Scene() = default;
@ -40,7 +46,7 @@ namespace tp {
private: private:
struct RayCastData { struct RayCastData {
const Scene::Object* obj = nullptr; const Object* obj = nullptr;
TrigCache* trig = nullptr; TrigCache* trig = nullptr;
Vec3F hitPos = {0, 0, 0}; Vec3F hitPos = {0, 0, 0};
bool hit = false; bool hit = false;