Skip to main content
gamedev.net
🔒 Locked

Normals

Started by Tipotas688 5 replies 800+ views
swiftcoder
swiftcoder
Quote:
Original post by Tipotas688
Ok, 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)
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
Tipotas688
Tipotas688
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
ahlywog
ahlywog
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?
swiftcoder
swiftcoder
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)
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
Tipotas688
Tipotas688
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(???)
swiftcoder
swiftcoder
Yes, for each glVertex*(), you must specify a glNormal*(). Since you are drawing in immediate mode, you could use face normals (one normal per triangle), which you can compute from the vertices as you render. If you want vertex normals, you need to generate them all, before rendering.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Topic Locked

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

Sign in to reply to this topic.