Skip to content

Commit

Permalink
Fix #4767: Bad lighting when re-exporting models with a non-identity …
Browse files Browse the repository at this point in the history
…rotation transform.
  • Loading branch information
codereader committed Apr 30, 2018
1 parent 6286c2c commit f026e57
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions plugins/model/ModelExporterBase.h
Expand Up @@ -33,6 +33,8 @@ class ModelExporterBase :
{
Surface& surface = ensureSurface(incoming.getActiveMaterial());

Matrix4 invTranspTransform = localToWorld.getFullInverse().getTransposed();

try
{
const IIndexedModelSurface& indexedSurf = dynamic_cast<const IIndexedModelSurface&>(incoming);
Expand All @@ -53,12 +55,13 @@ class ModelExporterBase :
// Transform vertices before inserting them
for (const auto& meshVertex : vertices)
{
// Copy-construct based on the incoming meshVertex
surface.vertices.emplace_back(meshVertex);

// Transform the copied vertex
Vertex3f& vertex = surface.vertices.back().vertex;
vertex = localToWorld.transformPoint(vertex);
// Copy-construct based on the incoming meshVertex, transform the vertex.
// Transform the normal using the inverse transpose
// We discard the tangent and bitangent vectors here, none of the exporters is using them.
surface.vertices.emplace_back(
localToWorld.transformPoint(meshVertex.vertex),
invTranspTransform.transformPoint(meshVertex.normal).getNormalised(),
meshVertex.texcoord);
}

surface.indices.reserve(surface.indices.size() + indices.size());
Expand Down Expand Up @@ -90,6 +93,11 @@ class ModelExporterBase :
poly.b.vertex = localToWorld.transformPoint(poly.b.vertex);
poly.c.vertex = localToWorld.transformPoint(poly.c.vertex);

// Transform the normal using the inverse transpose
poly.a.normal = invTranspTransform.transformPoint(poly.a.normal).getNormalised();
poly.b.normal = invTranspTransform.transformPoint(poly.b.normal).getNormalised();
poly.c.normal = invTranspTransform.transformPoint(poly.c.normal).getNormalised();

surface.vertices.push_back(poly.a);
surface.vertices.push_back(poly.b);
surface.vertices.push_back(poly.c);
Expand Down

0 comments on commit f026e57

Please sign in to comment.