Jump to content
  • Advertisement
Sign in to follow this  
cheese

OpenGL Calculating cube map coordinates

This topic is 5043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!