Xtremehobo 820 Report post Posted July 3, 2003 I've got a large terrain mesh that uses indices and I can't figgure out how to generate normals for it basiclly, I've been trying this code here to generate a normal for each vertex but it doesn't seem to work for(int xx=0;xx<200;xx++) { for(int zz=0;zz<200;zz++) { if(xx==200 || zz==200) { } else { //Generate Normals D3DVECTOR veca; veca.x= m_Vertices[(unsigned)((xx+1)*200+zz)].x - m_Vertices[(unsigned)(xx*200+zz)].x; veca.y= m_Vertices[(unsigned)((xx+1)*200+zz)].y - m_Vertices[(unsigned)(xx*200+zz)].y; veca.z= m_Vertices[(unsigned)((xx+1)*200+zz)].z - m_Vertices[(unsigned)(xx*200+zz)].z; D3DVECTOR vecb; vecb.x= m_Vertices[(unsigned)(xx*200+(zz+1))].x - m_Vertices[(unsigned)(xx*200+zz)].x; vecb.y= m_Vertices[(unsigned)(xx*200+(zz+1))].y - m_Vertices[(unsigned)(xx*200+zz)].y; vecb.z= m_Vertices[(unsigned)(xx*200+(zz+1))].z - m_Vertices[(unsigned)(xx*200+zz)].z; m_Vertices[(unsigned)(xx*200+zz)].normal = vecNormalize(vecCrossProduct(veca,vecb)); } } } Is there any special method i would need to generate normals for indexed vertices? Thanks, Matt carpenter EDIT: this is what my terrain kinda looks like: 0 Share this post Link to post Share on other sites

oliii 2196 Report post Posted July 3, 2003 if you pre-compute and store the normals, you''ll have to compute the triangles normal first, then for each vertices of the triangles, accumulate the triangle normal to teh vertex normal. Then normalise all vertex normals, and discard the triangle normals. an example with 2 triangles and 4 vertices V0.N = (0, 0, 0)V1.N = (0, 0, 0)V2.N = (0, 0, 0)V3.N = (0, 0, 0)N0 = TriangleNormal(V1, V0, V3);N1 = TriangleNormal(V0, V2, V3);V1.N += N0;V0.N += N0;V3.N += N0;V0.N += N1;V2.N += N1;V3.N += N1;V0.N.Normalise();V1.N.Normalise();V2.N.Normalise();V3.N.Normalise();if you want a faster method, you can use 4 points surrounding a vertex \ V0 \| V1--. --V3 |\ V2 \E = V3 - V1;F = V0 - V2;V.N = Cross(E, F);V.N.Normalise();in your code, you index to (i+1) but not (i-1) for(int xx=0;xx<200;xx++) { for(int zz=0;zz<200;zz++) { if(xx==200 || zz==200 || xx==0 || zz==0) { } else { //Generate Normals D3DVECTOR veca; veca.x= m_Vertices[(unsigned)((xx+1)*200+zz)].x - m_Vertices[(unsigned)((xx-1)*200+zz)].x; veca.y= m_Vertices[(unsigned)((xx+1)*200+zz)].y - m_Vertices[(unsigned)((xx-1)*200+zz)].y; veca.z= m_Vertices[(unsigned)((xx+1)*200+zz)].z - m_Vertices[(unsigned)((xx-1)*200+zz)].z; D3DVECTOR vecb; vecb.x= m_Vertices[(unsigned)(xx*200+(zz+1))].x - m_Vertices[(unsigned)(xx*200+(zz-1))].x; vecb.y= m_Vertices[(unsigned)(xx*200+(zz+1))].y - m_Vertices[(unsigned)(xx*200+(zz-1))].y; vecb.z= m_Vertices[(unsigned)(xx*200+(zz+1))].z - m_Vertices[(unsigned)(xx*200+(zz-1))].z; m_Vertices[(unsigned)(xx*200+zz)].normal = vecNormalize(vecCrossProduct(veca,vecb)); } } }and you might want to check which way the normal is facing. If it''s facing down, you''ll get a black terrain. 0 Share this post Link to post Share on other sites

Xtremehobo 820 Report post Posted July 3, 2003 EDIT: It's all working now. Normals were being generated just fine, there was just a problem with the lights. I've got another question though,When I'm generating indices, how can I determine if a square is facing back so I can generate the indicies backwards for backface culling?My terrain looks kinda like this:Thanks for all of your help on my last problem btw [edited by - Xtremehobo on July 4, 2003 2:37:10 AM] 0 Share this post Link to post Share on other sites

SoaringTortoise 130 Report post Posted July 4, 2003 As far as I understand it, you can''t. That''s why directx uses clockwise (or counter-clockwise) to cull. Basically you assume that the points are stored in a clockwise fashion, and from that information you assume that three successive points form a triangle that faces up (when viewed along a line that makes the points appear drawn in a clockwise direction)... erm... I''d love to try that again in english, but I think there''s a broken interface in my noodle somewhere.For triangle strips, DX assumes that the first triangle is cw, the next is ccw, the next cw and so on.If you make the same assumptions, you can''t go too far wrong. 0 Share this post Link to post Share on other sites