Calculating cube map coordinates

Started by
-1 comments, last by cheese 19 years, 6 months ago
Hi, I'm trying to manually sample a cube map, based on a normalized 3D vector, from the centre of the cube. I can't think of any method of getting d3d to do this for me without using a pixel shader. Here's my code, its based on an NVidia OpenGL tutorial:

void CubeCoords(D3DXVECTOR3 xyz)
{
	float rx = xyz.x;
	float ry = xyz.y;
	float rz = xyz.z;

	int majorAxis = -1;

	float absX = absolute(rx);
	float absY = absolute(ry);
	float absZ = absolute(rz);

	if (absX > absY && absX > absZ)
	{
		if (rx <= 0)
			majorAxis = D3DCUBEMAP_FACE_NEGATIVE_X;
		else
			majorAxis = D3DCUBEMAP_FACE_POSITIVE_X;
	}

	if (absY > absX && absY > absZ)
	{
		if (ry <= 0)
			majorAxis = D3DCUBEMAP_FACE_NEGATIVE_Y;
		else
			majorAxis = D3DCUBEMAP_FACE_POSITIVE_Y;
	}
	
	if (absZ > absX && absZ > absY)
	{
		if (rz <= 0)
			majorAxis = D3DCUBEMAP_FACE_NEGATIVE_Z;
		else
			majorAxis = D3DCUBEMAP_FACE_POSITIVE_Z;
	}

	float sc, tc, ma;

	switch(majorAxis)
	{
		case D3DCUBEMAP_FACE_POSITIVE_X:
		{
			sc = -rz;
			tc = -ry;
			ma = rx;
		} break;

		case D3DCUBEMAP_FACE_NEGATIVE_X:
		{
			sc = rz;
			tc = -ry;
			ma = rx;
		} break;

		case D3DCUBEMAP_FACE_POSITIVE_Y:
		{
			sc = -rx;
			tc = -rz;
			ma = ry;
		} break;

		case D3DCUBEMAP_FACE_NEGATIVE_Y:
		{
			sc = rx;
			tc = -rz;
			ma = ry;
		} break;

		case D3DCUBEMAP_FACE_POSITIVE_Z:
		{
			sc = rx;
			tc = -ry;
			ma = rz;
		} break;

		case D3DCUBEMAP_FACE_NEGATIVE_Z:
		{
			sc = -rx;
			tc = -ry;
			ma = rz;
		} break;
	}

	float u = ((sc / absolute(ma)+1) / 2) * cubeSize;
	float v = ((tc / absolute(ma)+1) / 2) * cubeSize;
}

My code seems to work for the most part, however it appears incorrect around the seams of the cube, and for the positive and negative y faces. My theory is that perhaps the seams are visible because texture clamping is not being used, or that perhaps OpenGL cube maps are not oriented in the same way as D3D cube maps. I can post screenshots to demonstrate the problem graphically if anyone would prefer. Many thanks in advance, I am very grateful.

This topic is closed to new replies.

Advertisement