normal for each triangle vertex or...
If you have a drawing with lots of small 3D triangles (thousands of them). And you want to give it lighting. Would it be better to compute normals for each triangle vertex or would it be better to generate and use one normal vector for all vertices of a single triangle. What difference does it make or should make in choosing between the two?
i am getting unexpected/weird results when i am computing normals for each triangle vertex, so was thinking to switch to the other possibility coz maybe while trying to generate normals for all 3 vertices i am calculating them all wrong. generating one single normal for a triangle using its vertices is easier and is bound to be correct too ie pointing up from the triangle''s face.
any ideas?
Hi
Vertex Normals give you a better per-vertex-lighting
you have to calculate the normals as
[edited by - Luminous Blue on June 30, 2003 8:15:34 AM]
[edited by - Luminous Blue on June 30, 2003 8:16:23 AM]
[edited by - Luminous Blue on June 30, 2003 8:17:49 AM]
Vertex Normals give you a better per-vertex-lighting
you have to calculate the normals as
pTempNormals = new Vector3D [pObj->numOfFaces]; // Generate Face Normals as Uni normal (not Normalize) for (i=0; i < pObj->numOfFaces; i++) { GetFaceNormal(&pVertices[pObj->VertsIndex], &pFaces[pObj->FacesIndex + i], &pTempNormals[i], TRUE); } for (i=0; i < pObj->numOfVerts; i++) { for (j=0; j < pObj->numOfFaces; j++) { if (pFaces[pObj->FacesIndex + j].vertex1 == i || pFaces[pObj->FacesIndex + j].vertex2 == i || pFaces[pObj->FacesIndex + j].vertex3 == i) { // Add the Face normal to sum myVectorMath.Add( &vSum, &pTempNormals[j], &vSum); shared++; } } if(shared != 0) { // Get the normal by dividing the sum by the shared.// We negate the shared so it has the normals pointing out. tmpShared = 1.0f / float(shared); tmpNormal.x = vSum.x * tmpShared; tmpNormal.y = vSum.y * tmpShared; tmpNormal.z = vSum.z * tmpShared; // Normalize the normal for the final vertex normal myVectorMath.Normalize( &tmpNormal ); pVerticesNormals[pObj->VertsIndex + i] = tmpNormal; } vSum = vZero; // Reset the sum shared = 0; // Reset the shared } delete [] pTempNormals;
[edited by - Luminous Blue on June 30, 2003 8:15:34 AM]
[edited by - Luminous Blue on June 30, 2003 8:16:23 AM]
[edited by - Luminous Blue on June 30, 2003 8:17:49 AM]
Luminous blue thanks but it went over my head... i didnt get whts going on in ur code. could u plz instead explain me how are we supposed to go about it?
thanks again.
thanks again.
Ok i try
1. Calc face normals
Calculate two vectors from the three points
v1 = vertex1 - vertex2
v2 = vertex2 - vertex3
Take the cross product of the two vectors to get the normal vector
vn = (v1 * v2) - (v1 * v2);
Do not Normalize vn !!!
-> not !!
l = sqrt( (vnx * vnx) + (vny * vny) + (vnz * vnz)
vn = vn / l
<-
2.
you have to check each vertex if they share any surrounding face
__x_____
__/\_b__/
_/__\__/
/__a_\/
vertex -> x <-
share face -> a <- and -> b <-
if so
add the normal from face X (a) to the temp vector
add 1 to a counter
....
....
go on with next face until end
if any face share the current vertex
take the temp vector and divided by the counter
finally normalize the temp vector and you have the
vertex normal for the current vertex
go on with the next vertex until end
------- psuedo code --------
vector sum = 0.0f
int share=0
loop through all your vertex's (i)
{
loop through all your face's (j)
{
note: if face index (j) = vertex index (i)
if vertex share the current face then
{
add the face normal to a vector called sum
incase the shared counter
}
}
if there is any shared ( share != 0 )
{
vertexnormal = sum / share
normalize vertexnormal
you got it !
}
}
sorry no more time yet it's late hope it helps
LB
[edited by - Luminous Blue on June 30, 2003 6:41:37 PM]
1. Calc face normals
Calculate two vectors from the three points
v1 = vertex1 - vertex2
v2 = vertex2 - vertex3
Take the cross product of the two vectors to get the normal vector
vn = (v1 * v2) - (v1 * v2);
Do not Normalize vn !!!
-> not !!
l = sqrt( (vnx * vnx) + (vny * vny) + (vnz * vnz)
vn = vn / l
<-
2.
you have to check each vertex if they share any surrounding face
__x_____
__/\_b__/
_/__\__/
/__a_\/
vertex -> x <-
share face -> a <- and -> b <-
if so
add the normal from face X (a) to the temp vector
add 1 to a counter
....
....
go on with next face until end
if any face share the current vertex
take the temp vector and divided by the counter
finally normalize the temp vector and you have the
vertex normal for the current vertex
go on with the next vertex until end
------- psuedo code --------
vector sum = 0.0f
int share=0
loop through all your vertex's (i)
{
loop through all your face's (j)
{
note: if face index (j) = vertex index (i)
if vertex share the current face then
{
add the face normal to a vector called sum
incase the shared counter
}
}
if there is any shared ( share != 0 )
{
vertexnormal = sum / share
normalize vertexnormal
you got it !
}
}
sorry no more time yet it's late hope it helps
LB
[edited by - Luminous Blue on June 30, 2003 6:41:37 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement