   Calculating normals problems directx9 c++
  Hi I have a problem with calculating normals for my terrain.When I use D3DXVec3Cross the result is always (0,0,0) and I have no idea why.sorry i dont know how to put this into a code thing://CALCULATE NORMALSfor (unsigned int i = 0; i < m_vecVertices.size(); i += 3){if (i+2 > m_vecVertices.size()){break;}D3DXVECTOR3 vec3Vertice0 = D3DXVECTOR3(m_vecVertices.x,m_vecVertices.y,m_vecVertices.z);D3DXVECTOR3 vec3Vertice1 = D3DXVECTOR3(m_vecVertices[i+1].x,m_vecVertices[i+1].y,m_vecVertices[i+1].z);D3DXVECTOR3 vec3Vertice2 = D3DXVECTOR3(m_vecVertices[i+2].x,m_vecVertices[i+2].y,m_vecVertices[i+2].z);D3DXVECTOR3 normal;D3DXVECTOR3 u = vec3Vertice1 - vec3Vertice0;D3DXVECTOR3 v = vec3Vertice2 - vec3Vertice0;D3DXVec3Cross(&normal, &u, &v);D3DXVec3Normalize(&normal, &normal);m_vecVertices.normal = normal;m_vecVertices[i+1].normal = normal;m_vecVertices[i+2].normal = normal;}thanks for help,r_bewickEDIT: D3DXVec3Cross(&normal, &u, &v); changed from D3DXVec3Cross(&out, &u, &v);(thanks enigmatix)[Edited by - r_bewick on August 28, 2009 12:03:01 AM]
  These two lines strike me as odd:D3DXVec3Cross(&out, &u, &v);D3DXVec3Normalize(&normal, &normal);Where does &out come from and why are you passing &normal as both the in and out? Shouldn't it be:D3DXVECTOR3 normal;D3DXVECTOR3 out;D3DXVECTOR3 u = vec3Vertice1 - vec3Vertice0;D3DXVECTOR3 v = vec3Vertice2 - vec3Vertice0;D3DXVec3Cross(&out, &u, &v);D3DXVec3Normalize(&normal, &out);;  oh yeah it should, sorry about that. But it still doesnt return anything other than 0, so the only light i can see is from the ambient. Is anything else you can see wrong? thanksThe lights and everything works properly when I have the normals at (0,1,0) except that it doesn't look 3d. So I'm sure it has something to do with how im calculating my normals.   ;  I think it's your entire loop.The last time I did something with terrain rendering, I used a VB and IB together. So when you have setup both, you loop through the Indices (not vertices) on a triangle basis:foreach(Vertex& v, m_vertices){    v.normal = D3DXVECTOR3(0, 0, 0);}for(unsigned int i = 0; i < m_indices.size(); i += 3){   D3DXVECTOR3 v1 = m_vertices[i+0];   D3DXVECTOR3 v2 = m_vertices[i+1];   D3DXVECTOR3 v3 = m_vertices[i+2];   D3DXVECTOR3 u = v2 - v1;   D3DXVECTOR3 v = v3 - v1;   D3DXVECTOR3 normal;   D3DXVec3Cross(&normal, &u, &v);   D3DXVec3Normalize(&normal, &normal);   m_vertices[i+0].normal += normal;   m_vertices[i+1].normal += normal;   m_vertices[i+2].normal += normal;}// Do a final normalization of all vertices normalforeach(Vertex& v, m_vertices){    D3DXVec3Normalize(&v.normal, &v.normal);}There are several differences, the first being that you need to operate per triangle. The second one is that you add the normal of a triangle to each triangle's vertex, NOT assign. For this to work, the normals need to be set to (0, 0, 0) BEFORE you start with the calculation process.After all normals have been calculated, you need to finally normalize the normal for all vertices (because it will have a length of a multiple of 1).   ;  you can try \[\s\o\u\r\c\e lang="cpp"\]\[\/\s\o\u\r\c\e\]delete all "\"   ;  Thanks CodingSmallCat, I'll try that next time I need to put some code in.SiS-ShadowMan, I am using both a vertex buffer and an index buffer in my terrain,but there is a problem.VERTICES: 63001INDICES: 375000I have much more indices than vertices so the for loop will crash. Am I doing something wrong to have this many indices? <br>thanks for your help.<br><br>By the way, I am using a vector to hold my indices and I can not create more than about 250 by 250 squares in my terrain before I have reached the max size of my vector. Is there a way to increase vector size/ use a different container that is larger? or should I just make 250 by 250 square sections or something?<br>thanks;  Try this. Note that the vertex subscript is from the index buffer.for (unsigned int i = 0; i < m_vecVertices.size(); i++)   m_vecVertices.normal = D3DXVECTOR3(0,0,0);for(unsigned int i = 0; i < m_indices.size(); i += 3){   D3DXVECTOR3 v1 = m_vecVertices[m_indices[i+0]];   D3DXVECTOR3 v2 = m_vecVertices[m_indices[i+1]];   D3DXVECTOR3 v3 = m_vecVertices[m_indices[i+2]];   D3DXVECTOR3 u = v2 - v1;   D3DXVECTOR3 v = v3 - v1;   D3DXVECTOR3 normal;   D3DXVec3Cross(&normal, &u, &v);   D3DXVec3Normalize(&normal, &normal);   m_vecVertices[m_indices[i+0]].normal += normal;   m_vecVertices[m_indices[i+1]].normal += normal;   m_vecVertices[m_indices[i+2]].normal += normal;}// Do a final normalization of all vertex normals - "averages" the normalsfor (unsigned int i = 0; i < m_vecVertices.size(); i++)   D3DXVec3Normalize(&m_vecVertices,&m_vecVertices);Are you using DrawIndexedPrimitiveUP? If so, you can allocate an array.MaybeDWORD *indices= new DWORD[ big_number ];if( indices==NULL ) { // out of memory }// don't forget to delete indices when you're doneBe sure to set the index format as D3DFMT_INDEX32.[Edited by - Buckeye on August 29, 2009 9:32:55 AM];  Wow thanks Buckeye, that worked perfectly! :Dno sorry i don't use DrawPrimitiveUP, never mind. I will use 250x250 sections.Thanks everyone for your help :D   ;Quote:Original post by r_bewickSiS-ShadowMan, I am using both a vertex buffer and an index buffer in my terrain,but there is a problem.VERTICES: 63001INDICES: 375000I have much more indices than vertices so the for loop will crash. Am I doing something wrong to have this many indices? <br>thanks for your help.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>There is nothing wrong, because this is the reason you have an index buffer: To preserve space in the vertexbuffer. You need to understand that you <b>don't use the index</b> of the indexbuffer to access a vertex in the vertexbuffer, which would look like this: for(int i = 0; i &lt; 375000; ++i) m_vertices<span style="font-weight:bold;"><br>But instead, you <b>use the value</b> of a particular element of the indexbuffer to access a vertex in the vertexbuffer:<br>for(int i = 0; i &lt; 375000; ++i) m_vertices[m_indices<span style="font-weight:bold;">];<br><br>Basically, you use the indexbuffer to tell your graphics card "use vertices x, y and z more than 1 time, because they happen to be in more than 1 primitive".;Quote:Original post by r_bewickSiS-ShadowMan, I am using both a vertex buffer and an index buffer in my terrain,but there is a problem.VERTICES: 63001INDICES: 375000I have much more indices than vertices so the for loop will crash. Am I doing something wrong to have this many indices? <br>thanks for your help.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>You're right, I forgot the actual access of the indexbuffer: This is the corrected code<br><br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br>foreach(Vertex&amp; v, m_vertices)<br>{<br>    v.normal = D3DXVECTOR3(<span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>);<br>}<br><br><span class="cpp-keyword">for</span>(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>; i &lt; m_indices.size(); i += <span class="cpp-number">3</span>)<br>{<br>   D3DXVECTOR3 v1 = m_vertices[m_indices[i+<span class="cpp-number">0</span>]];<br>   D3DXVECTOR3 v2 = m_vertices[m_indices[i+<span class="cpp-number">1</span>]];<br>   D3DXVECTOR3 v3 = m_vertices[m_indices[i+<span class="cpp-number">2</span>]];<br><br>   D3DXVECTOR3 u = v2 - v1;<br>   D3DXVECTOR3 v = v3 - v1;<br><br>   D3DXVECTOR3 normal;<br>   D3DXVec3Cross(&amp;normal, &amp;u, &amp;v);<br>   D3DXVec3Normalize(&amp;normal, &amp;normal);<br><br>   m_vertices[m_indices[i+<span class="cpp-number">0</span>]].normal += normal;<br>   m_vertices[m_indices[i+<span class="cpp-number">1</span>]].normal += normal;<br>   m_vertices[m_indices[i+<span class="cpp-number">2</span>]].normal += normal;<br>}<br><br><span class="cpp-comment">// Do a final normalization of all vertices normal</span><br>foreach(Vertex&amp; v, m_vertices)<br>{<br>    D3DXVec3Normalize(&amp;v.normal, &amp;v.normal);<br>}<br><br><br></pre></div><!–ENDSCRIPT–> 