Adding normals
This commit is contained in:
parent
cfb4c0cfcc
commit
97f79a6483
6 changed files with 882 additions and 878 deletions
|
|
@ -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