Sign in to follow this  
calidev234

Computing XFile mesh normals

Recommended Posts

Does anybody know how to compute mesh normals? I'm trying to make an automated procedure to take in mesh files and computes the mesh normal. The automated normal creator doesn't work like I need it to. I've included a basic box and need normals to be perpendicular to the main sides. If you have any idea could you please include the calculations. Thank you very much, TestBox.x ------------------------------------ xof 0303txt 0032 # Created By Joshua Mesh v0{ 8; 268.00; 365.00; 0.00;, 852.00; 363.00; 0.00;, 267.00; 354.00; 0.00;, 851.00; 352.00; 0.00;, 268.00; 365.00; 200.00;, 852.00; 363.00; 200.00;, 267.00; 354.00; 200.00;, 851.00; 352.00; 200.00;; 12; 3; 0,1,3;, 3; 0,3,2;, 3; 0,5,1;, 3; 0,4,5;, 3; 0,2,6;, 3; 0,6,4;, 3; 4,7,5;, 3; 4,6,7;, 3; 1,7,3;, 3; 1,5,7;, 3; 2,3,7;, 3; 2,7,6;; MeshTextureCoords { 8; 0.0;2.22;, 1.11;2.22;, 1.11;2.22;, 0.0;2.22;, 0.0;0.0;, 1.11;0.0;, 1.11;0.0;, 0.0;0.0;; } MeshMaterialList { 3; 12; 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2;; Material { 0.753;0.565;0.341;0.000;; 0.00; 0.00;0.00;0.00;; 0.00;0.00;0.00;; } Material { 0.000;0.004;0.000;0.000;; 0.00; 0.00;0.00;0.00;; 0.00;0.00;0.00;; } Material { 0.753;0.565;0.341;0.000;; 0.00; 0.00;0.00;0.00;; 0.00;0.00;0.00;; } } }

Share this post


Link to post
Share on other sites
Ok, so I’m having a hard time figuring this out… I have this simple box

The .X File is:

-----------------------------------------------------------

xof 0303txt 0032
# Advanced Archaio Document Viewer X File - Created By Joshua

Mesh CubeMesh {
8;
1.0, 1.0, -1.0;,
-1.0, 1.0, -1.0;,
-1.0, 1.0, 1.0;,
1.0, 1.0, 1.0;,
1.0, -1.0, -1.0;,
-1.0, -1.0, -1.0;,
-1.0, -1.0, 1.0;,
1.0, -1.0, 1.0;;
12;
3; 0, 1, 2;,
3; 0, 2, 3;,
3; 0, 4, 5;,
3; 0, 5, 1;,
3; 1, 5, 6;,
3; 1, 6, 2;,
3; 2, 6, 7;,
3; 2, 7, 3;,
3; 3, 7, 4;,
3; 3, 4, 0;,
3; 4, 7, 6;,
3; 4, 6, 5;;
MeshMaterialList
{
2;
12;
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1;;
Material
{
1.0; 0.0; 0.0; 1.0;;
0.0;
0.0; 0.0; 0.0;;
0.0; 0.0; 0.0;;
}
Material
{
0.0; 0.0; 1.0; 1.0;;
0.0;
0.0; 0.0; 0.0;;
0.0; 0.0; 0.0;;
}
}
}

-----------------------------------------------------------

Ok, so according to the other post:

1.0, 1.0, -1.0;, (V0)
-1.0, 1.0, -1.0;, (V1)
-1.0, 1.0, 1.0;, (V2)

a = v1 – v0 => [-1.0, 1.0, -1.0] – [1.0, 1.0, -1.0] => [-2, 0, 0] or (a1, a2, a3)
b = v2 – v0 => [-1.0, 1.0, 1.0] – [1.0, 1.0, -1.0] => [-2, 0, 2] or (b1, b2, b3)

and, the cross product is:
cross(a and b) = [a2b3 – a3b2, a3b1 – a1b3, a1b2 – a2b1]
= [0*2 - 0*0, 0*(-2) – (-2)*2, (-2)*0 – 0*(-2)]
= [0, 4, 0]

so is this the first of 8 normal vectors that are needed? I think I’m calculating something incorrectly. Also, once you’ve done this you must tell which normals are for each face, correct?

There is just something I’m missing. I really just want to have a simple cube with each face having simple normal vectors that are directly perpendicular to the faces; not the cross product of each faces that touch.

I’m sorry for exasperating an all ready over talked about topic, but there is just disconnect in my brain.

Thank you for anyone who can explain this to me.






Share this post


Link to post
Share on other sites
Ok, so I've figured this out. Basically I need to calculate the normal vector for each side, but instead of multiplying the normal vectors for each polygon which touches (to have 8 normal vectors) I've calculated the simple normal vector for each triangle and assigned the normal vector to each side:

ie.

MeshNormals
{
12;
0.0; 4.0; 0.0;,
0.0; 4.0; 0.0;,
0.0; 0.0; -4.0;,
0.0; 0.0; -4.0;,
-4.0; 0.0; 0.0;,
-4.0; 0.0; 0.0;,
0.0; 0.0; 4.0;,
0.0; 0.0; 4.0;,
4.0; 0.0; 0.0;,
4.0; 0.0; 0.0;,
0.0; -4.0; 0.0;,
0.0; -4.0; 0.0;;
12;
3; 0, 0, 0;,
3; 1, 1, 1;,
3; 2, 2, 2;,
3; 3, 3, 3;,
3; 4, 4, 4;,
3; 5, 5, 5;,
3; 6, 6, 6;,
3; 7, 7, 7;,
3; 8, 8, 8;,
3; 9, 9, 9;,
3; 10, 10, 10;,
3; 11, 11, 11;;
}

Maybe this is not the best way, but for a simple box which I want each side to be shaded consistently; this works fine.

Share this post


Link to post
Share on other sites
Are you, by any chance, loading this into an ID3DXMesh ??

Or, even if you're not, you can still use the code... but D3DXComputeNormals() might well be the simplest way...

Even if you just did a Load mesh -> Create Normals -> Save mesh operation, provided you're not too fussed about performance [wink]

hth
Jack

Share this post


Link to post
Share on other sites
Quote:
Original post by calidev234
Ok, so I've figured this out. Basically I need to calculate the normal vector for each side, but instead of multiplying the normal vectors for each polygon which touches (to have 8 normal vectors) I've calculated the simple normal vector for each triangle and assigned the normal vector to each side:

....

Maybe this is not the best way, but for a simple box which I want each side to be shaded consistently; this works fine.

I'm sorry I told you to ask away with questions then vanished. I forgot that I replied to this thread. Anyway, your solution is the correct one. It's the only way it can and should be done. The smooth normal approach requires calculating each face normal and averaging to find the vertex normal. Your flat approach only requires the first step. The disadvantage here is that you will need duplicate vertices, having exactly 4 for each side. But I'm not aware of any other way to accomplish it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this