void Mesh::CalculateVertexNormals()
{
	// Clear all vertex normals
	for( unsigned int i = 0; i < this->_vertices.size(); ++i )
	{
		Vertex* vertex = this->_vertices.at(i);
		if( vertex )
		{
			vertex->Normal() = 0.0f;
		}
	}

	// Sum all adjacent triangle normals
	for( unsigned int i = 0; i < this->_triangles.size(); ++i )
	{
		Triangle* triangle = this->_triangles.at(i);
		if( triangle )
		{
			if( triangle->IsValid() )
			{
				for( unsigned int j = 0; j < 3; ++j )
				{
					Vertex* vertex = triangle->GetVertex(j);
					vertex->Normal() += triangle->GetNormal();
				}
			}
		}
	}

	// Normalize all vertex normals
	for( unsigned int i = 0; i < this->_vertices.size(); ++i )
	{
		Vertex* vertex = this->_vertices.at(i);
		if( vertex )
		{
			vertex->Normal().Normalize();
		}
	}
}
void Terrain::UpdateVertices()
{
	if( this->IsValid() )
	{
		for( int y = 0; y <= this->_size.height; ++y )
		{
			for( int x = 0; x <= this->_size.width; ++x )
			{

				Vertex* vertex = this->_heightMatrix+x+y*(this->_size.width+1);
				
				vertex->Position().x = (float)x + (float)this->_offset.x;
				vertex->Position().z = -(float)y - (float)this->_offset.y;

				vertex->TextureCoords().u = (float)x / (float)this->_size.width;
				vertex->TextureCoords().v = (float)y / (float)this->_size.height;

				vertex->Normal().x = 0.0f;
				vertex->Normal().y = 1.0f;
				vertex->Normal().z = 0.0f;
			}
		}
	}
}