Skip to content

Commit

Permalink
read mesh normal from file
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleTheFu committed Oct 3, 2024
1 parent 687d06a commit af57b4b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
15 changes: 12 additions & 3 deletions geometry/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,27 @@ Mesh::Mesh(const std::string fileName, const Vector3 pos, float scale, Material
float x_a = scene->mMeshes[0]->mVertices[face.mIndices[0]].x;
float y_a = scene->mMeshes[0]->mVertices[face.mIndices[0]].y;
float z_a = scene->mMeshes[0]->mVertices[face.mIndices[0]].z;
float n_x_a = scene->mMeshes[0]->mNormals[face.mIndices[0]].x;
float n_y_a = scene->mMeshes[0]->mNormals[face.mIndices[0]].y;
float n_z_a = scene->mMeshes[0]->mNormals[face.mIndices[0]].z;

float x_b = scene->mMeshes[0]->mVertices[face.mIndices[1]].x;
float y_b = scene->mMeshes[0]->mVertices[face.mIndices[1]].y;
float z_b = scene->mMeshes[0]->mVertices[face.mIndices[1]].z;
float n_x_b = scene->mMeshes[0]->mNormals[face.mIndices[1]].x;
float n_y_b = scene->mMeshes[0]->mNormals[face.mIndices[1]].y;
float n_z_b = scene->mMeshes[0]->mNormals[face.mIndices[1]].z;

float x_c = scene->mMeshes[0]->mVertices[face.mIndices[2]].x;
float y_c = scene->mMeshes[0]->mVertices[face.mIndices[2]].y;
float z_c = scene->mMeshes[0]->mVertices[face.mIndices[2]].z;
float n_x_c = scene->mMeshes[0]->mNormals[face.mIndices[2]].x;
float n_y_c = scene->mMeshes[0]->mNormals[face.mIndices[2]].y;
float n_z_c = scene->mMeshes[0]->mNormals[face.mIndices[2]].z;

TriVertex va(x_a * scale, y_a * scale, z_a * scale);
TriVertex vb(x_b * scale, y_b * scale, z_b * scale);
TriVertex vc(x_c * scale, y_c * scale, z_c * scale);
TriVertex va(x_a * scale, y_a * scale, z_a * scale, n_x_a, n_y_a, n_z_a);
TriVertex vb(x_b * scale, y_b * scale, z_b * scale, n_x_b, n_y_b, n_z_b);
TriVertex vc(x_c * scale, y_c * scale, z_c * scale, n_x_c, n_y_c, n_z_c);

Tri *tri = new Tri(va, vb, vc, pos, pMtrl);

Expand Down
5 changes: 4 additions & 1 deletion geometry/tri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ bool Tri::hit(const Ray &ray, HitRecord &record, Light *pLight) const

Vector3 Tri::getLocalNormal(bool reverse) const
{
Vector3 n = m_ab.cross(-m_ca);
Vector3 n = m_a.normal + m_b.normal + m_c.normal;
n.normalize();

// Vector3 n = m_ab.cross(-m_ca);

if(reverse)
{
Expand Down
2 changes: 2 additions & 0 deletions geometry/tri.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class TriVertex
public:
TriVertex() {};
TriVertex(float x, float y, float z) : pos(x, y, z) {};
TriVertex(float x, float y, float z, float nx, float ny, float nz) : pos(x, y, z), normal(nx, ny, nz) {}

Vector3 pos;
Vector3 normal;
};

class Tri : public Geometry
Expand Down

0 comments on commit af57b4b

Please sign in to comment.