Skip to main content
GameDev.net gamedev.net
🔒 Locked

Normal interpolation

Started by nail85 Jul 21, 2002 at 3:47 AM 2 replies 300+ views
Original Post
nail85
nail85
I have a triangle. All points and all normals are known. The question: How to compute normal in the point "p" whithin this triangle, using normal seeds in triangle vertices. I know the question is simple, but my own alg. doesn''t work properly and I can''t find any article with solution.
McRam
McRam
Add all face-normals connected with that point together and normalize the product. Take care that the vector-length is >0 while normalizing.

McRam
Cedric
Cedric
quote:

Add all face-normals connected with that point together and normalize the product. Take care that the vector-length is >0 while normalizing.


I think that''s not what he was asking for... And besides, this question belongs more in the Maths-Phys forum or the Graphics programming forum.

Anyway, I think interpolating normals accross a triangle is the basic idea behind Phong shading. Maybe could you Google it?

My idea would be to make a weighted sum of the normals, proportionnally to the distance between the point and each of the vertex, then normalizing. Unfortunately, this method will probably be quite slow if you do it often. Maybe could you use the squared distance?

Cédric
nail85
nail85
Thanks,

But I''ve solve this problem earlier than you replied.
Actualy i asked a question about phong lighting (for lightmaps) Nobody gives correct reply: First method computes normal in vertex, second doesn''t work (imagine quad side of cylinder with 2 triangles in it - in the center of quad normal seeds will be differ for first and second triangles).

Right solution is:
inline NVector3 GetTriInterpolatedNormal(const NTriangle& tri,
const NVector& n1,
const NVector& n2,
const NVector& e1,
const NVector& e2,
const NVector& p)
{
// Convert ''p'' vector to triangle coordinates
const NVector v = p - GetPoint(tri,0);

// Alpha and betta are coefficients for decomposin vector p in
// alpha*e1 + beta*e2
// Computing process calculates roots of the system:
// ( alpha*e1.x + beta*e2.x = x
// < alpha*e1.y + beta*e2.y = y
// ( alpha*e1.z + beta*e2.z = z
float alpha=0, beta=0;
if ( e2.x != 0 ) {
if ( e1.z*e2.x - e1.x*e2.z != 0.0f )
alpha = (v.z*e2.x - v.x*e2.z) / (e1.z*e2.x - e1.x*e2.z);
else
alpha = (v.y*e2.x - v.x*e2.y) / (e1.y*e2.x - e1.x*e2.y);
beta = (v.x - alpha*e1.x) / e2.x;
}
else if ( e2.y != 0 ) {
if ( e1.z*e2.y - e1.y*e2.z != 0.0f )
alpha = (v.z*e2.y - v.y*e2.z) / (e1.z*e2.y - e1.y*e2.z);
else
alpha = (v.x*e2.y - v.y*e2.x) / (e1.x*e2.y - e1.y*e2.x);
beta = (v.y - alpha*e1.y) / e2.y;
}
else if ( e2.z != 0 ) {
if ( e1.x*e2.z - e1.z*e2.y != 0.0f)
alpha = (v.y*e2.z - v.z*e2.y) / (e1.y*e2.z - e1.z*e2.y);
else
alpha = (v.x*e2.z - v.z*e2.x) / (e1.x*e2.z - e1.z*e2.x);
beta = (v.z - alpha*e1.z) / e2.z;
}

return (GetNormal(tri,0) + n1*alpha + n2*beta).Normalize();
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.