Sign in to follow this  
bengaltgrs

HLSL Cube mapping (solved)

Recommended Posts

bengaltgrs    282
I tried to make a cube mapping demo following the SDk sample, but I'm having some odd results. It seems to get some of the texture correct, but certain spots on the texture seem to be sampling bad data. I followed the SDK sample so my code is very similar to that, and I really can't tell which part of the code is causing the problem. So I was wondering if anyone else was having an issue with cube maps sampling bad data. I have a screenshot of it here so you can see what I'm talking about. [Edited by - bengaltgrs on December 15, 2006 5:16:50 AM]

Share this post


Link to post
Share on other sites
circlesoft    1178
First, I would double check that the view matrices are correct for each cubemap face. Having one matrix incorrect can lead to parts of the cubemap being incorrect, kind of like you are experiencing.

If you are using a look-at projection for each view matrix, I think these are what your lookAt and up vectors should be. The eye vector should be the same for each face (ie where the cubemap is centered around):


Face lookAt up
0 [1.0 eye.y 0.0] [0.0 1.0 0.0]
1 [-1.0 eye.y 0.0] [0.0 1.0 0.0]
2 [0.0 (eye.y + 1.0) 0.0] [0.0 0.0 -1.0]
3 [0.0 (eye.y – 1.0) 0.0] [0.0 0.0 1.0]
4 [0.0 eye.y 1.0] [0.0 1.0 0.0]
5 [0.0 eye.y -1.0] [0.0 1.0 0.0]

Share this post


Link to post
Share on other sites
bengaltgrs    282
I worked with it quite a bit and now there are no more artifacts, but it uses the same texture on all six sides of the cube. I am using practically the exact same method to get the textures as the SDK sample, so I'm not sure what I'm doing wrong. Here is the code I am using to render the scene into the cube map and screenshot of what it looks like now:


HRESULT hr;

D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI * 0.5f, 1.0f, 1.5f, 1000.0f );

// Get the current view matrix, to concat it with the cubemap view vectors
D3DXMATRIXA16 matViewDir( ActiveCamera->View );
matViewDir._41 = 0.0f; matViewDir._42 = 0.0f; matViewDir._43 = 0.0f;

// store old render target and old depth/stencil buffer
// set stencil buffer
if(FAILED(hr = m_pRenderToEnvMap->BeginCube( m_pCubeMap )))
return hr;

for( UINT i = 0; i < 6; i++ )
{
// gets cube map render target surface and set it as render target
m_pRenderToEnvMap->Face((D3DCUBEMAP_FACES)i, 0 );

// Set the view transform for this cubemap surface
D3DXMATRIXA16 matView;
matView = D3DUtil_GetCubeMapViewMatrix((D3DCUBEMAP_FACES)i);

// Render the scene (except for the teapot)

D3DDevice->Clear( 0, NULL, D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 );

RenderScene();
}

// Restore depth-stencil buffer and render target
m_pRenderToEnvMap->End( 0 );

D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace )
{
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vLookDir;
D3DXVECTOR3 vUpDir;

switch( dwFace )
{
case D3DCUBEMAP_FACE_POSITIVE_X:
vLookDir = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_X:
vLookDir = D3DXVECTOR3(-1.0f, 0.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_POSITIVE_Y:
vLookDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_Y:
vLookDir = D3DXVECTOR3( 0.0f,-1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
break;
case D3DCUBEMAP_FACE_POSITIVE_Z:
vLookDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_Z:
vLookDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
}

// Set the view transform for this cubemap surface
D3DXMATRIXA16 mView;
D3DXMatrixLookAtLH( &mView, &vEyePt, &vLookDir, &vUpDir );
return mView;
}







[Edited by - bengaltgrs on December 15, 2006 12:59:39 AM]

Share this post


Link to post
Share on other sites
bengaltgrs    282
Alright, I've finally fixed it. I forgot to actually set the view transform every time I rendered a new texture. I'll clean the code up a bit and put it up on my site for anyone that's interested.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this