• 13
• 18
• 19
• 27
• 10

# Normals

This topic is 3397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I ve created a 3D model using a ply file and now I need to calculate its normals..

##### Share on other sites
Quote:
 Original post by Tipotas688Ok, I ve created a 3D model using a ply file and now I need to calculate its normals.
For each triangle, take the cross product of 2 edges. For a triangle with vertices a, b and c this would be: CrossProduct(c - a, c - b), although you may have to correct the order depending on your triangle winding. This is the normal vector for the triangle.

Unfortunately, OpenGL expects vertex normals instead, so for each vertex you need to add together the normals of each triangle that contains the vertex, and normalise the result. For a vertex used by 3 triangles, d, e and f this would be: Normalise(d + e + f)

##### Share on other sites
You mean I will have to calculate:

CrossProduct :

u = < a , b , c > and v = < d , e , f >

w = u x v = < a , b , c > x < d , e , f > = < x , y , z >

x = b*f - c*e , y = c*d - a*f and z = a*e - b*d

d = CrossProduct(c1 - a1, c1 - b1)
e = CrossProduct(c2 - a2, c2 - b2)
f = CrossProduct(c3 - a3, c3 - b3)

Normalise(d + e + f).

But I will need to Normalise(x,y,z)

I did not understand how I will do all that :P

##### Share on other sites
def find_surface_normal(p1x, p1y, p1z,
p2x, p2y, p2z,
p3x, p3y, p3z):
ax = p2x - p1x
ay = p2y - p1y
az = p2z - p1z

bx = p3x - p1x
by = p3y - p1y
bz = p3z - p1z

nx = (ay * bz) - (az * by)
ny = (az * bx) - (ax * bz)
nz = (ax * by) - (ay * bx)

l = sqrt(nx * nx + ny * ny + nz * nz)
nx /= l
ny /= l
nz /= l
return nx, ny, nz

Thats what I do for my surface normals. Or are you looking for vertex?

##### Share on other sites
First we need triangle normals:
def triangle_normal(v1, v2, v3):	return CrossProduct(v1 - v2, v1 - v3)

And then we use those to produce vertex normals:
def vertex_normal(triangle_normals):	v = vec3()	for n in triangle_normals:		v += n	return normalise(v)

##### Share on other sites
I understand those but I dont understand what I will use in these equations

I have :

glVertex3d(vertex x1,vertex y1,vertex z1)
glVertex3d(vertex x2,vertex y2,vertex z2)
glVertex3d(vertex x3,vertex y3,vertex z3)

as defined by the faces of the ply file.

glNormal3d(???)
glNormal3d(???)
glNormal3d(???)