Adding normals
This commit is contained in:
parent
a84cfabd60
commit
368ae81251
6 changed files with 882 additions and 878 deletions
|
|
@ -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(const TrigCache& in) {
|
||||
mP1 = in.mP1;
|
||||
mP2 = in.mP2;
|
||||
mP3 = in.mP3;
|
||||
mP1 = in.mP1;
|
||||
mP2 = in.mP2;
|
||||
mP3 = in.mP3;
|
||||
|
||||
mEdgeP1P2 = in.mEdgeP1P2;
|
||||
mEdgeP1P3 = in.mEdgeP1P3;
|
||||
mEdgeP1P2 = in.mEdgeP1P2;
|
||||
mEdgeP1P3 = in.mEdgeP1P3;
|
||||
|
||||
mOrigin = in.mOrigin;
|
||||
mNormal = in.mNormal;
|
||||
mOrigin = in.mOrigin;
|
||||
mNormal = in.mNormal;
|
||||
}
|
||||
|
||||
void TrigCache::updateCache(const Buffer<Vec3F>& points) {
|
||||
mOrigin = points[mP1];
|
||||
mEdgeP1P2 = points[mP2] - points[mP1];
|
||||
mEdgeP1P3 = points[mP3] - points[mP1];
|
||||
mNormal = (points[mP2] - points[mP1]).cross(points[mP3] - points[mP1]).unitV();
|
||||
mOrigin = points[mP1];
|
||||
mEdgeP1P2 = points[mP2] - points[mP1];
|
||||
mEdgeP1P3 = points[mP3] - points[mP1];
|
||||
mNormal = (points[mP2] - points[mP1]).cross(points[mP3] - points[mP1]).unitV();
|
||||
}
|
||||
|
||||
bool TrigCache::castRay(const Ray& ray) const {
|
||||
static Vec3F h, s, q;
|
||||
static halnf a, f, u, v;
|
||||
static halnf t;
|
||||
static Vec3F h, s, q;
|
||||
static halnf a, f, u, v;
|
||||
static halnf t;
|
||||
|
||||
h = ray.dir.cross(mEdgeP1P3);
|
||||
a = mEdgeP1P2.dot(h);
|
||||
h = ray.dir.cross(mEdgeP1P3);
|
||||
a = mEdgeP1P2.dot(h);
|
||||
|
||||
if (a > -EPSILON && a < EPSILON) {
|
||||
return false;
|
||||
}
|
||||
if (a > -EPSILON && a < EPSILON) {
|
||||
return false;
|
||||
}
|
||||
|
||||
f = 1.f / a;
|
||||
s = ray.pos - mOrigin;
|
||||
u = f * s.dot(h);
|
||||
f = 1.f / a;
|
||||
s = ray.pos - mOrigin;
|
||||
u = f * s.dot(h);
|
||||
|
||||
if (u < 0.0 || u > 1.0) {
|
||||
return false;
|
||||
}
|
||||
if (u < 0.0 || u > 1.0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
q = s.cross(mEdgeP1P2);
|
||||
v = f * ray.dir.dot(q);
|
||||
q = s.cross(mEdgeP1P2);
|
||||
v = f * ray.dir.dot(q);
|
||||
|
||||
if (v < 0.f || u + v > 1.f) {
|
||||
return false;
|
||||
}
|
||||
if (v < 0.f || u + v > 1.f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
t = f * mEdgeP1P3.dot(q);
|
||||
if (t > EPSILON) {
|
||||
gHitPos = ray.pos + ray.dir * t;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
t = f * mEdgeP1P3.dot(q);
|
||||
if (t > EPSILON) {
|
||||
gHitPos = ray.pos + ray.dir * t;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const Vec3F& TrigCache::getHitPos() { return gHitPos; }
|
||||
|
||||
const Vec3F& TrigCache::getNormal() const { return mNormal; }
|
||||
|
||||
void Topology::addTrig(const Vec3F& v1, const Vec3F& v2, const Vec3F& v3) {
|
||||
auto trigIdx = mPoints.size();
|
||||
void TopologyCache::updateCache() {
|
||||
TransformedNormals.clear();
|
||||
TransformedPoints.clear();
|
||||
TrigCaches.clear();
|
||||
|
||||
mPoints.append(v1);
|
||||
mPoints.append(v2);
|
||||
mPoints.append(v3);
|
||||
if (!Source) {
|
||||
return;
|
||||
}
|
||||
|
||||
TrigCache newTrig(trigIdx, trigIdx + 1, trigIdx + 2);
|
||||
mTrigCaches.append(newTrig);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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; }
|
||||
|
||||
|
|
|
|||
1480
Math/public/Mat.hpp
1480
Math/public/Mat.hpp
File diff suppressed because it is too large
Load diff
|
|
@ -1,49 +1,53 @@
|
|||
#pragma once
|
||||
|
||||
#include "Camera.hpp"
|
||||
#include "Buffer.hpp"
|
||||
#include "Camera.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class TrigCache {
|
||||
static Vec3F gHitPos;
|
||||
class TrigCache {
|
||||
static Vec3F gHitPos;
|
||||
|
||||
public:
|
||||
ualni mP1, mP2, mP3;
|
||||
Vec3F mEdgeP1P2, mEdgeP1P3;
|
||||
Vec3F mNormal;
|
||||
Vec3F mOrigin;
|
||||
public:
|
||||
ualni mP1, mP2, mP3;
|
||||
Vec3F mEdgeP1P2, mEdgeP1P3;
|
||||
Vec3F mNormal;
|
||||
Vec3F mOrigin;
|
||||
|
||||
public:
|
||||
TrigCache();
|
||||
TrigCache(const TrigCache& in);
|
||||
TrigCache(ualni v1, ualni v2, ualni v3);
|
||||
public:
|
||||
TrigCache();
|
||||
TrigCache(const TrigCache& in);
|
||||
TrigCache(ualni v1, ualni v2, ualni v3);
|
||||
|
||||
public:
|
||||
static const Vec3F& getHitPos() ;
|
||||
[[nodiscard]] const Vec3F& getNormal() const;
|
||||
public:
|
||||
static const Vec3F& getHitPos();
|
||||
[[nodiscard]] const Vec3F& getNormal() const;
|
||||
|
||||
public:
|
||||
void updateCache(const Buffer<Vec3F>& points);
|
||||
[[nodiscard]] bool castRay(const Ray& ray) const;
|
||||
};
|
||||
public:
|
||||
void updateCache(const Buffer<Vec3F>& points);
|
||||
[[nodiscard]] bool castRay(const Ray& ray) const;
|
||||
};
|
||||
|
||||
class Topology {
|
||||
mat3f mBasis;
|
||||
Vec3F mOrigin;
|
||||
struct Topology {
|
||||
Vec3F Origin = {0, 0, 0};
|
||||
mat3f Basis = {
|
||||
{1, 0, 0},
|
||||
{0, 1, 0},
|
||||
{0, 0, 1}
|
||||
};
|
||||
|
||||
Buffer<Vec3F> mPoints;
|
||||
Buffer<Vec3F> mPointsTransformed;
|
||||
Buffer<TrigCache> mTrigCaches;
|
||||
Buffer<Vec3F> Points;
|
||||
Buffer<Vec3F> Normals;
|
||||
Buffer<Vec3I> Indexes;
|
||||
};
|
||||
|
||||
public:
|
||||
Topology() = default;
|
||||
~Topology() = default;
|
||||
struct TopologyCache {
|
||||
const Topology* Source = nullptr;
|
||||
|
||||
public:
|
||||
void addTrig(const Vec3F& v1, const Vec3F& v2, const Vec3F& v3);
|
||||
void transformPoint(Vec3F& vert);
|
||||
void updateTransformed();
|
||||
[[nodiscard]] const Buffer<TrigCache>& getTrigs() const;
|
||||
};
|
||||
Buffer<Vec3F> TransformedPoints;
|
||||
Buffer<Vec3F> TransformedNormals;
|
||||
Buffer<TrigCache> TrigCaches;
|
||||
|
||||
void updateCache();
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,21 +19,28 @@ bool loadMeshes(tp::Scene& scene, const tp::String& objetsPath) {
|
|||
}
|
||||
|
||||
for (auto& curMesh : Loader.LoadedMeshes) {
|
||||
scene.mObjects.append(Topology());
|
||||
scene.mObjects.append(Object());
|
||||
|
||||
auto object = &scene.mObjects.last();
|
||||
|
||||
for (int j = 0; j < curMesh.Indices.size(); j += 3) {
|
||||
unsigned int idx1 = curMesh.Indices[j];
|
||||
unsigned int idx2 = curMesh.Indices[j + 1];
|
||||
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);
|
||||
for (auto& vertex : curMesh.Vertices) {
|
||||
object->mTopology.Points.append(Vec3F {vertex.Position.X, vertex.Position.Y, vertex.Position.Z});
|
||||
object->mTopology.Normals.append(Vec3F {vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z});
|
||||
}
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ void RayTracer::castRay(const Ray& ray, RayCastData& out, alnf far) {
|
|||
far *= far;
|
||||
|
||||
for (auto obj : mScene->mObjects) {
|
||||
for (auto trig : obj->getTrigs()) {
|
||||
for (auto trig : obj->mCache.TrigCaches) {
|
||||
if (trig->castRay(ray)) {
|
||||
// printf("Hit\n");
|
||||
|
||||
|
|
@ -77,15 +77,46 @@ void RayTracer::render(const Scene& scene, RayTracer::RenderBuffer& buff) {
|
|||
castRay(ray, castData, mScene->mCamera.getFar());
|
||||
|
||||
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();
|
||||
|
||||
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);
|
||||
|
||||
// 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 {
|
||||
buff.set({i, j}, RGBA(0.f, 0.f, 0.f, 0.f));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,10 +12,16 @@ namespace tp {
|
|||
|
||||
extern ModuleManifest gModuleRayTracer;
|
||||
|
||||
class Scene {
|
||||
class Object {
|
||||
public:
|
||||
typedef Topology Object;
|
||||
Object() = default;
|
||||
|
||||
public:
|
||||
Topology mTopology;
|
||||
TopologyCache mCache;
|
||||
};
|
||||
|
||||
class Scene {
|
||||
public:
|
||||
Scene() = default;
|
||||
|
||||
|
|
@ -40,7 +46,7 @@ namespace tp {
|
|||
|
||||
private:
|
||||
struct RayCastData {
|
||||
const Scene::Object* obj = nullptr;
|
||||
const Object* obj = nullptr;
|
||||
TrigCache* trig = nullptr;
|
||||
Vec3F hitPos = {0, 0, 0};
|
||||
bool hit = false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue