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

@ -12,89 +12,89 @@ TrigCache::TrigCache() : mP1(0), mP2(0), mP3(0) {}
TrigCache::TrigCache(ualni v1, ualni v2, ualni v3) : mP1(v1), mP2(v2), mP3(v3) {} TrigCache::TrigCache(ualni v1, ualni v2, ualni v3) : mP1(v1), mP2(v2), mP3(v3) {}
TrigCache::TrigCache(const TrigCache& in) { TrigCache::TrigCache(const TrigCache& in) {
mP1 = in.mP1; mP1 = in.mP1;
mP2 = in.mP2; mP2 = in.mP2;
mP3 = in.mP3; mP3 = in.mP3;
mEdgeP1P2 = in.mEdgeP1P2; mEdgeP1P2 = in.mEdgeP1P2;
mEdgeP1P3 = in.mEdgeP1P3; mEdgeP1P3 = in.mEdgeP1P3;
mOrigin = in.mOrigin; mOrigin = in.mOrigin;
mNormal = in.mNormal; mNormal = in.mNormal;
} }
void TrigCache::updateCache(const Buffer<Vec3F>& points) { void TrigCache::updateCache(const Buffer<Vec3F>& points) {
mOrigin = points[mP1]; mOrigin = points[mP1];
mEdgeP1P2 = points[mP2] - points[mP1]; mEdgeP1P2 = points[mP2] - points[mP1];
mEdgeP1P3 = points[mP3] - points[mP1]; mEdgeP1P3 = points[mP3] - points[mP1];
mNormal = (points[mP2] - points[mP1]).cross(points[mP3] - points[mP1]).unitV(); mNormal = (points[mP2] - points[mP1]).cross(points[mP3] - points[mP1]).unitV();
} }
bool TrigCache::castRay(const Ray& ray) const { bool TrigCache::castRay(const Ray& ray) const {
static Vec3F h, s, q; static Vec3F h, s, q;
static halnf a, f, u, v; static halnf a, f, u, v;
static halnf t; static halnf t;
h = ray.dir.cross(mEdgeP1P3); h = ray.dir.cross(mEdgeP1P3);
a = mEdgeP1P2.dot(h); a = mEdgeP1P2.dot(h);
if (a > -EPSILON && a < EPSILON) { if (a > -EPSILON && a < EPSILON) {
return false; return false;
} }
f = 1.f / a; f = 1.f / a;
s = ray.pos - mOrigin; s = ray.pos - mOrigin;
u = f * s.dot(h); u = f * s.dot(h);
if (u < 0.0 || u > 1.0) { if (u < 0.0 || u > 1.0) {
return false; return false;
} }
q = s.cross(mEdgeP1P2); q = s.cross(mEdgeP1P2);
v = f * ray.dir.dot(q); v = f * ray.dir.dot(q);
if (v < 0.f || u + v > 1.f) { if (v < 0.f || u + v > 1.f) {
return false; return false;
} }
t = f * mEdgeP1P3.dot(q); t = f * mEdgeP1P3.dot(q);
if (t > EPSILON) { if (t > EPSILON) {
gHitPos = ray.pos + ray.dir * t; gHitPos = ray.pos + ray.dir * t;
return true; return true;
} else { } else {
return false; return false;
} }
} }
const Vec3F& TrigCache::getHitPos() { return gHitPos; } 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); TransformedPoints.reserve(Source->Points.size());
mTrigCaches.append(newTrig); 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);
}
} }
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);
}
}
const Buffer<TrigCache>& Topology::getTrigs() const { return mTrigCaches; }

File diff suppressed because it is too large Load diff

View file

@ -1,49 +1,53 @@
#pragma once #pragma once
#include "Camera.hpp"
#include "Buffer.hpp" #include "Buffer.hpp"
#include "Camera.hpp"
namespace tp { namespace tp {
class TrigCache { class TrigCache {
static Vec3F gHitPos; static Vec3F gHitPos;
public: public:
ualni mP1, mP2, mP3; ualni mP1, mP2, mP3;
Vec3F mEdgeP1P2, mEdgeP1P3; Vec3F mEdgeP1P2, mEdgeP1P3;
Vec3F mNormal; Vec3F mNormal;
Vec3F mOrigin; Vec3F mOrigin;
public: public:
TrigCache(); TrigCache();
TrigCache(const TrigCache& in); TrigCache(const TrigCache& in);
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:
void updateCache(const Buffer<Vec3F>& points); void updateCache(const Buffer<Vec3F>& points);
[[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;