Exemplo n.º 1
0
LRESULT CALLBACK windowMessageCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		// Send the WM_QUIT message, terminating the message loop
	case WM_CREATE:
		{
			create();
		}break;
	case WM_DESTROY:
		{
			::PostQuitMessage(0);
		}
		break;
	case WM_SIZE:
		{
			D3DVIEWPORT9 vp;
			vp.X = 0;
			vp.Y = 0;
			vp.Width = LOWORD(lParam);
			vp.Height = HIWORD(lParam);
			vp.MinZ = 0;
			vp.MaxZ = 1;
			//gDevice->SetViewport(&vp);
			//
			LPDIRECT3DSWAPCHAIN9 sc;
			gDevice->GetSwapChain(0, &sc);
			sc->Release();
			//
			D3DPRESENT_PARAMETERS pp;
			sc->GetPresentParameters(&pp);
			
			pp.BackBufferWidth = vp.Width;
			pp.BackBufferHeight = vp.Height;
			gDevice->Reset(&pp);//会重设viewport,仅设vp而不reset是错误的
		}break;
	default:
		{
		}
		break;
	}
	// Forward any other messages we didn't handle above to the default window procedure
	return ::DefWindowProc(hWnd, message, wParam, lParam);
}
bool D3DCubeTextureRenderTarget::CreateTextures( uint texture_size, TextureFormat::Format texture_format )
{
	m_CubeTextureSize = texture_size;
	m_TextureFormat = texture_format;

	D3DFORMAT d3d_fmt;
	if( texture_format == TextureFormat::Invalid )
	/* || any ohter texture formats invalid for cube mapping ) */
	{
		LOG_PRINT_ERROR( fmt_string("An unsupported texture format: %d", texture_format) );
		return false;
	}

	d3d_fmt = ConvertTextureFormatToD3DFORMAT( texture_format );

	LPDIRECT3DDEVICE9 pd3dDevice = DIRECT3D9.GetDevice();
	HRESULT hr;

	m_NumCubes = 1; // always use the single cube texture

/*	if( d3d_fmt == D3DFMT_A16B16G16R16F )
	{
		// Create the cube textures
		ZeroMemory( m_apCubeMapFp, sizeof( m_apCubeMapFp ) );
		hr = pd3dDevice->CreateCubeTexture( m_CubeTextureSize,
											1,
											D3DUSAGE_RENDERTARGET,
											D3DFMT_A16B16G16R16F,
											D3DPOOL_DEFAULT,
											&m_apCubeMapFp[0],
											NULL );

		m_pCurrentCubeMap = m_apCubeMapFp[0];
	}
*/

	hr = pd3dDevice->CreateCubeTexture( m_CubeTextureSize,
										1,
										D3DUSAGE_RENDERTARGET,
										d3d_fmt,
										D3DPOOL_DEFAULT,
										&m_pCubeMap32,
										NULL );

	if( FAILED(hr) || !m_pCubeMap32 )
	{
		LOG_PRINT_WARNING( "CreateCubeTexture() failed. Cannot create cube texture." );
		return false;
	}

	m_pCurrentCubeMap =  m_pCubeMap32;

//	DXUTDeviceSettings d3dSettings = DXUTGetDeviceSettings();

	LPDIRECT3DSWAPCHAIN9 pSwapChain;
	pd3dDevice->GetSwapChain( 0, &pSwapChain );

	D3DPRESENT_PARAMETERS pp;
	pSwapChain->GetPresentParameters( &pp );

    hr = pd3dDevice->CreateDepthStencilSurface( m_CubeTextureSize,
                                                m_CubeTextureSize,
//                                              d3dSettings.d3d9.pp.AutoDepthStencilFormat,
                                                pp.AutoDepthStencilFormat,
                                                D3DMULTISAMPLE_NONE,
                                                0,
                                                TRUE,
                                                &m_pDepthCube,
                                                NULL );

	if( FAILED(hr) || !m_pCurrentCubeMap )
	{
		LOG_PRINT_WARNING( "CreateDepthStencilSurface() failed. Cannot create depth stencil surface." );
		return false;
	}

/*
	// Initialize the number of cube maps required when using floating point format
//	IDirect3D9* pD3D = DXUTGetD3D9Object(); 
//	D3DCAPS9 caps;
//	hr = pD3D->GetDeviceCaps( pDeviceSettings->d3d9.AdapterOrdinal, pDeviceSettings->d3d9.DeviceType, &caps );

//	if( FAILED( pD3D->CheckDeviceFormat( caps.AdapterOrdinal, caps.DeviceType,
//										 pDeviceSettings->d3d9.AdapterFormat, D3DUSAGE_RENDERTARGET, 
//										 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F ) ) )

	if( FAILED( pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, DIRECT3D9.GetDeviceType(),
										 DIRECT3D9.GetAdapterFormat(), D3DUSAGE_RENDERTARGET, 
										 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F ) ) )
	{
		m_NumCubes = m_NumFpCubeMap = 2;
	}
	else
	{
		m_NumCubes = m_NumFpCubeMap = 1;
	}

	// If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW 
	// then switch to SWVP.
//	if( (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
//			caps.VertexShaderVersion < D3DVS_VERSION(1,1) )
//	{
//		pDeviceSettings->d3d9.BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
//	}*/

	return true;
}