TEST_F(Test_WorldTerrainNormalMap, slope45Degree)
{
	const int hmSize = 2;
	HeightMap hm;
	hm.reserve(hmSize);

	hm.set(0, 0, 1.0f);
	hm.set(1, 0, 1.0f);
	hm.set(0, 1, 0);
	hm.set(1, 1, 0);

	NormalMap nm;
	nm.generate(hm);

	//only two normals
	EXPECT_EQ(nm.size(), 2);

	//both normals should be pointing at an angle more downish
	Vec3f n1 = nm.get(0);
	Vec3f n2 = nm.get(0);
	Vec3f outDown(0.0f, -.707107f, -.707107);

	expectVec3fAreEqual(n1, outDown);
	expectVec3fAreEqual(n2, outDown);

	{
		Vec3f p1(0, 1, 0);
		Vec3f p2(1, 1, 0);
		Vec3f p3(0, 0, 1);
	}
}
TEST_F(Test_WorldTerrainNormalMap, flatTerrain)
{
	const int hmSize = 2;
	HeightMap hm;
	hm.reserve(hmSize);

	for (int y=0; y<hmSize; y++)
	{
		for (int x=0; x<hmSize; x++)
			hm.set(x, y, 0.0f);
	}

	NormalMap nm;
	nm.generate(hm);

	//there should be only two normals (since there are two triangles)
	EXPECT_EQ(nm.size(), 2);

	//both normals should point straight up
	Vec3f n1 = nm.get(0);
	Vec3f n2 = nm.get(1);
	Vec3f up(0.0f, -1.0f, 0.0f);

	expectVec3fAreEqual(n1, up, "The normal is not straight up");
	expectVec3fAreEqual(n2, up, "The normal is not straight up");
}
/**Convert an image to a height map.  The image is treated as grey-scale.

@param border create a height map which is larger than the source image.  The source
	image will be centered in the heightmap which creates a kind of border.  This border
	is filled with by tiling the soure image.  If your source image was
	created to be tilable (it ought to be) then the heightmap will be seamless.  The whole
	point of this is so that you can call HeightMap.blur() without ruining the seamless
	nature of your heightmap.  (After bluring you should ignore the border region)
*/
HeightMap * imageToHeightMap_tilable(GLImage & img, int border)
{
	HeightMap * hmap = new HeightMap(img.width + border + border, img.height + border + border);

	for (int y = 0; y < hmap->H; y++)
	{
		for (int x = 0; x < hmap->W; x++)
		{
			int imgX = infinite_coord_to_finite(x - border, img.width);
			int imgY = infinite_coord_to_finite(y - border, img.height);

			//TODO: GLImage should be a class with an inline getPixelPtr(x,y) method.
			GLubyte * pixel = &img.texels[((imgY * img.width) + imgX) * img.internalFormat];

			hmap->set(x, y, pixel[0] / 255.0f);
		}
	}

	return hmap;
}