210 likes | 320 Views
Preserving Normals. Lecture 28 Wed, Nov 12, 2003. The Effect of a Transformation on a Normal. What happens to a normal vector under a linear transformation? A translation? A rotation? A scaling? Other?. v. n. v. n. Rotations and Translations.
E N D
Preserving Normals Lecture 28 Wed, Nov 12, 2003
The Effect of a Transformation on a Normal • What happens to a normal vector under a linear transformation? • A translation? • A rotation? • A scaling? • Other?
v n v n Rotations and Translations • It seems intuitive that if u and v are orthogonal, then their images will be orthogonal under both translations and rotations, since these are rigid motions.
n n v v Rotations, Translations, and Scalings • However, under scalings, their images will, in general, not be orthogonal.
n v n v Rotations, Translations, and Scalings • Nor will they be orthogonal under a shear transformation.
The Effect on a Normal • Let v be a vector lying in the tangent plane. • Let n be a unit normal vector at that point. n v
The Effect on a Normal • Then n v = 0. • Equivalently, if we regard n and v as 3 1 matrices, then nTv = 0, where nT is the transpose of n and the operation is matrix multiplication.
The Effect on a Normal • Let M be a linear transformation. • Then M maps v to v’ = Mv and M maps n to n’ = Mn. • In general, v’ and n’ will not be orthogonal. • That is because (n’)Tv’ = (Mn)T(Mv) = (nTMT)(Mv) = nT(MTM)v 0. (?)
Transforming a Normal • So if Mn is not orthogonal to v’, then what vector will be orthogonal to v’? • Let’s try n’’ = (M-1)Tn. • Then n’’Tv’ is (n’’)Tv’ = ((M-1)Tn)T(Mv) = (nTM-1)(Mv) = nT(M-1M)v = nTv = 0.
Transforming a Normal • This demonstrates that if the surface points are transformed by matrix M, then the surface normals should be transformed by the matrix (M-1)T, the transpose of the inverse, in order to remain normal to the surface.
The Case of Translations • The case of translations is very simple since the matrix does not change the normal vector in the first place. =
The Case of Translations • In the case of translations, we know that Mn = n and Mv = v. • It follows that Mn and Mv are orthogonal since (Mn)T(Mv) = nTv = 0.
The Case of Rotations • In the case of rotations, the transpose of the inverse of the matrix is the same matrix again! (I.e., M-1 = MT.) • Such a matrix is said to be orthonormal. • Therefore, (Mn)T(Mv) = (nTMT)Mv = nT(MTM)v = nT(I)v = nTv = 0.
Scalings • Let M be the matrix of the scaling Scale(sx, sy, sz). • Then (M-1)T = M =
Scalings • Under the scaling Scale(sx, sy, sz), the point P = (x, y, z) is transformed into the point P’ = MP = (sxx, syy, szz). • But the normal vector n = (nx, ny, nz) must be transformed into the normal vector n’ = (M-1)Tn = (nx/sx, ny/sy, nz/sz).
Other Transformations • For other transformations, we must apply the transpose of the inverse matrix to the normal vectors to produce normals to the transformed surface.
Consequences for Programming • OpenGL applies transformation matrices to both the vertices but it applies the transpose of the inverse to the normals. • One consequence is that, if we compute the transformed normals ourselves, then we must be sure to apply the transpose of the inverse and renormalize.
Consequences for Programming • However, • In translations, it is not necessary to do anything. • In rotations, we may apply the very same matrix.
Renormalizing Vectors in OpenGL • Luckily, OpenGL will automatically recalculate normals if we ask it to. • We should write the statement glEnable(GL_NORMALIZE); • This statement forces OpenGL to renormalize all surface normals after transformations, throughout the program.
Caution • This function call is expensive since unit normals have already been computed and now they must be recomputed. • It should be used only if the transformations include scalings or non-standard transformations. • It would be more efficient to compute the correct normals from the start, if the situation allows for that.
Example: Scaling in the Mesh Class • NormScaler.cpp • mesh2.cpp • vector3.cpp