D3d manual vertex normal

Source: Internet
Author: User

D3dxvector3 myd3d: values (d3dxvector3 vertex1, d3dxvector3 vertex2, d3dxvector3 vertex3) <br/>{< br/> d3dxvector3 normal = d3dxvector3 (0.0, 0.0, 0.0 ); <br/> d3dxvector3 vector1 = vertex2-vertex1; <br/> d3dxvector3 vector2 = vertex3-vertex1; <br/> // calculate the cross product <br/> d3dxvec3cross (& Normal, & vector1, & vector2); <br/> d3dxvec3normalize (& Normal, & Normal); <br/> return normal; <br/>}</P> <p> // <br/> bool myd3d: my_vertex_normal_computer (d3dxvector3 * arrvertexposbuffer, DWORD dwvertexnum, word * arrindexbuffer, DWORD dwfacenum, d3dxvector3 * pvertexnormalbuffer) <br/>{< br/> If (arrvertexposbuffer = NULL | arrindexbuffer = NULL | pvertexnormalbuffer = NULL | dwvertexnum = 0 | dwfacenum = 0) <br/> return false; <br/> // calculate the surface normal <br/> d3dxvector3 * arrfacenormalbuffer = new d3dxvector3 [dwfacenum]; <br/> // d3dxvector3 arr [ox * Oz * 2]; <br/> for (DWORD I = 0; I <dwfacenum; I ++) <br/> {<br/> // arr [I] = This-> my_face_normal_computer (arrvertexposbuffer [arrindexbuffer [3 * I], arrvertexposbuffer [arrindexbuffer [3 * I + 1], arrvertexposbuffer [arrindexbuffer [3 * I + 2]); <br/> arrfacenormalbuffer [I] = This-> my_face_normal_computer (arrvertexposbuffer [arrindexbuffer [3 * I], arrvertexposbuffer [arrindexbuffer [3 * I + 1], arrvertexposbuffer [arrindexbuffer [3 * I + 2]); <br/>}< br/> // calculates the vertex normal -- average value <br/> word numfaceshare = 0; // Number of faces sharing the same vertex <br/> word vertexfaces [50]; // each vertex can be shared by 50 faces <br/> for (word J = 0; j <dwvertexnum; j ++) <br/>{< br/> for (dword k = 0; k <dwfacenum * 3; k ++) <br/>{< br/> If (arrindexbuffer [k] = J) <br/>{< br/> vertexfaces [numfaceshare] = K/3; // obtain the surface index <br/> numfaceshare ++; <br/>}< br/> // calculate the mean value <br/> pvertexnormalbuffer [J] = d3dxvector3 (0.0f, 0.0f, 0.0f ); <br/> for (DWORD m = 0; m <numfaceshare; m ++) <br/>{< br/> pvertexnormalbuffer [J] + = arrfacenormalbuffer [vertexfaces [m]; <br/>}< br/> d3dxvec3normalize (& pvertexnormalbuffer [J], & pvertexnormalbuffer [J]); <br/> numfaceshare = 0; <br/>}< br/> Delete arrfacenormalbuffer; <br/> return true; <br/>}

Declaration: Please see ~~~~~~

The source code is as above, but the comparison shows that it is inconsistent with the vertex normal calculated by d3dxcomputenormals... The specific reason is unclear .. However, the correct lighting effect can be achieved.

1. The above code is obviously very inefficient. You need to optimize it yourself...

2. note that the order of the vertex index you provide will affect the direction of face normal, and the messy vertex index will cause face normal confusion, so vertex normal will inevitably become messy... The last is the "screen" in the light...

3. according to the guidance of Daniel, the vertex data dynamically generated by the function can be computed in normal mode through "Derivation", so that more computing tasks can be placed on the GPU, but how to handle tangent... Approximate values .. Otherwise .. Think about it myself...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.