views:

231

answers:

5

Hello, I'm trying to apply a transform to a 3D object in a STL File (without creating a structured mesh object). Here is how I proceed: I read the normals and faces information one by one in the STL file, apply my transform to each vertex and to the face normal and write back the new computed values in another STL file. The vertex are OK in the generated file but my normals are wrong. It seems that I can't just apply my transform to the normal as I do for the vertice. How is that possible??

+1  A: 

Transforming a vector is different than transforming a point -- you can't apply the transformation, only the rotations.

Jeff Kotula
+2  A: 

You should look at transforming normals.

And actually, Jeff, you're only partly correct. For a vector, you're right. But for a normal, which is a bit different in meaning, you have to transform by the upper 3x3, but inversed, and then transposed.

Matt Cruikshank
To Jeff's credit, you should note that what he said is valid for rigid transformations, such as rotations (which seems to be what he was talking about). And jumar did not tell what he meant exactly by "transforming"...
Camille
Yes -- for rotations (and reflections), the transpose *is* the inverse, so the inverse-transpose is just the original matrix.
comingstorm
Yup - it's anisotropic scaling that kills your normals - that's why the inverse transpose.
Matt Cruikshank
A: 

You need to apply the inverse-transpose of your matrix to the normals, instead of using the original matrix.

Also, you need to treat w-coordinate of the normal as 0 (not 1 as with points) when transforming it.

comingstorm
+2  A: 

You can apply pretty much the same transformation for both but keep these two things in mind:

  • Normals are directions, so the position part of a 4x4 matrix shouldn't be applied. To avoid applying it you can either format the vector as Vector(x,y,z,0) before multiplying with the matrix, or use a dedicated TransformVector() function to avoid the instructions that will end up multiplying with zero.
  • If the matrix you apply contains a scale, your normal will be scaled as well, meaning that, if you do the typical N.L lighting dot product your result will be brighter or darker than it should be. Usually you'd want to re-normalize after applying the transform, or make sure the transform doesn't de-normalize the normal (which is what the inverse-transpose of the matrix is for.)
Rodrigo Lopez
+1  A: 

Quote Rodrigo Lopez: Normals are directions, so the position part of a 4x4 matrix shouldn't really be applied though renormalization will fix it anyway.

renormalization will not fix it: suppose the normal is (1,0,0) then translate it with (-2,0,0) => the normal will be (-1,0,0) which is normalized and is wrong, because the normal should stay the same.

Emile Vrijdags