[ create a new paste ] login | about

Project: ec
Link: http://ec.codepad.org/NsxUim61    [ raw code | fork ]

C++, pasted on Feb 8:
void HeightmapObject::CalcNormals(void)
{
	Vector a, b, n;
	Vector v1, v2, v3, v4;
	float heights[2][2];
	float xx, zz;
	float scale = 0.01;
	
	if(m_size == -1) return;
	
	m_normals = new Vector[m_size*m_size*2];
	
	for(int x=0; x < m_size-1; x++) {
		for(int z=0; z < m_size-1; z++) {
			heights[0][0] = (float)m_data[ x + ((z) * m_size)];
			heights[1][0] = (float)m_data[ x+1 + ((z) * m_size)];
			heights[0][1] = (float)m_data[ x + ((z+1) * m_size)];
			heights[1][1] = (float)m_data[ x+1 + ((z+1) * m_size)];
			
			// Calculate our vectors			
			xx = x * scale;
			zz = z * scale;
			
			v1.x = xx;
			v1.y = heights[0][0] * scale;
			v1.z = zz;
			
			v2.x = xx + scale;
			v2.y = heights[1][0] * scale;
			v2.z = zz;
			
			v3.x = xx;
			v3.y = heights[0][1] * scale;
			v3.z = zz + scale;
			
			v4.x = xx + scale;
			v4.y = heights[1][1] * scale;
			v4.z = zz + scale;
			

			// Calculate the normal of the top left triangle			
			a = v1 - v2;
			b = v2 - v3;

			n = CrossProduct(a, b);
			n.Normalize();
			m_normals[(x + (z * m_size)) * 2 + 0] = n;
			// Calc the normals of the second
			b = v4-v2;
			a = v2-v3;
		
			n = CrossProduct(a, b);
			n.Normalize();
			m_normals[(x + (z * m_size)) * 2 + 1] = n;
		}
	}
	
}


Create a new paste based on this one


Comments: