Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Cubemapping

Started by Adriano Oct 29, 2005 at 8:01 AM 5 replies 550+ views
Original Post
Adriano
Adriano
Hi. I still have some troubles with cubemapping. It seems not to be a difficoult bug but I really cant find it. Any suggestions are very appreciated This is how my cubemap should appear right And this is how it appears wrong Note how it seems deformed... you can clearly see the faces of the cubemap!! I use this piece of code for updating the map
//-------------------------------------------------------------------------------
// Name: CD3D::ReflectionUpdate()
// Desc: Update a reflection
// Params: Index    - Object index (IN)
//         Position - Position where the reflection is calculated (IN). NOTE:
//                    this is usually the position of the object that reflects.
// Return: Operation result
//-------------------------------------------------------------------------------
HRESULT CD3D::ReflectionUpdate ( DWORD Index, D3DXVECTOR3 Position )
{
	CReflection* pCRefl;

	
	// Select the reflection
	if ( ( Index >= 0 ) && ( Index < pReflectionList->ListCount() ) )
	{
		pReflectionList->Return ( &pCRefl, Index );

		// Save transformation matrices of the device
		D3DXMATRIX matProjSave, matViewSave;
		pd3dDevice->GetTransform(D3DTS_VIEW,       &matViewSave);
		pd3dDevice->GetTransform(D3DTS_PROJECTION, &matProjSave);

		// Store the current back buffer
		LPDIRECT3DSURFACE9 pBackBuffer;
		pd3dDevice->GetRenderTarget( 0, &pBackBuffer);
		
		// The projection matrix has a FOV of 90 degrees and asp ratio of 1
		D3DXMATRIXA16 mProj;
		D3DXMatrixPerspectiveFovLH( &Matrices.matProj, D3DX_PI*0.5f, 1.0f, 1.f, Matrices.fViewDistance );

		D3DXMATRIXA16 mViewDir = D3DXMATRIXA16( Matrices.matView );
		mViewDir._41 = mViewDir._42 = mViewDir._43 = 0.0f;

		// Loop through the six faces of the cube map
		for( DWORD dwFace=0; dwFace<6; dwFace++ )
		{
			// Standard view that will be overridden below
			D3DXVECTOR3 vEnvEyePt = Position;
			D3DXVECTOR3 vLookDir, 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 matView;
			D3DXMatrixLookAtLH( &matView, &vEnvEyePt, &vLookDir, &vUpDir );
			
			D3DXMatrixMultiply( &matView, &mViewDir, &matView );
			pd3dDevice->SetTransform(D3DTS_VIEW, &matView);
	    
			// Get pointer to surface in order to render to it
			LPDIRECT3DSURFACE9 pFace;
			pCRefl->pCubeMap->GetCubeMapSurface((D3DCUBEMAP_FACES)dwFace, 0, &pFace);
			pd3dDevice->SetRenderTarget (0, pFace);
			SAFE_RELEASE(pFace);

			pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, 0x00000000, 1.0f, 0L );

			pd3dDevice->BeginScene();

				// Render scene to texture
			
				DWORD IndexArray[MAXREFLOBJNUM];
				for( DWORD i = 0; i < pCRefl->dwNumReflMeshes; i++ )
					IndexArray = *(pCRefl->ppReflMeshesIndexes);
				MeshRender ( IndexArray, pCRefl->dwNumReflMeshes );

				IndexArray[MAXREFLOBJNUM];
				for( DWORD i = 0; i < pCRefl->dwNumReflBillboards; i++ )
					IndexArray = *(pCRefl->ppReflBillboardsIndexes);
				BillboardRender ( IndexArray, pCRefl->dwNumReflBillboards );
				
				
			pd3dDevice->EndScene();
		}

		// Change the render target back to the main back buffer.
		pd3dDevice->SetRenderTarget(0, pBackBuffer);
		SAFE_RELEASE(pBackBuffer);

		// Restore the original transformation matrices
		pd3dDevice->SetTransform(D3DTS_VIEW,       &matViewSave);
		pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProjSave);
		return S_OK;
	}
	return E_INVALIDARG;
}
Stylistics
Stylistics
:) Heeeyyyy i have nearly the same problem @ the moment ... i hope anybody has a answer :(
Stylistics
Stylistics
I think this line is the Problem:
D3DXMATRIXA16 mViewDir = D3DXMATRIXA16( Matrices.matView );

Ohmg ... i dont have nearly the same problem ... I HAVE THE SAME PROBLEM!!!

I think that the Matrix Tranfomations are not correctly...

greets
Jens
Stylistics
Stylistics
Has nobody a solution for the problem???

Greets
Jens
Kibble
Kibble
I see that you make the right projection matrix, but I can't find where you actually set this matrix to the projection transform?
Stylistics
Stylistics
Hi,
thats true he´s dont set de Projection Matrix.


But i´am set the Projection Matrix!

The only differnce is this part:

D3DXMATRIX matView;
g_pD3DDevice->GetTransform( D3DTS_VIEW,matView);
matView._41 = matView._41 = matView._42 = matView._43 = 0;
D3DXMatrixLookAtLH( &matView, &vEnvEyePt, &vLookatPt, &vUpVec );


in place of this:
D3DXMATRIXA16 mViewDir = D3DXMATRIXA16( Matrices.matView );
mViewDir._41 = mViewDir._42 = mViewDir._43 = 0.0f;


What is Matrices.matView in your Application?

Greeets
Jens
Adriano
Adriano
Hello, thank for replying

You're right Kibble!!! I messed up badly with projection matrices! Thanks a lot, I really needed a pair of fresh eyes.

Changes made were:

// The projection matrix has a FOV of 90 degrees and asp ratio of 1
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI*0.5f, 1.0f, 1.f, //There should be matProj
Matrices.fViewDistance );


[...]

// This was missing
pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);



Matrices.matXXX are the setting of the camera in my dumb & obsolete framework ^^


Thanks, bye!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.