//--------------------------------------------------------------------------------------
// Create any D3D10 resources that depend on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D10ResizedSwapChain(ID3D10Device* pd3dDevice,
		IDXGISwapChain* pSwapChain,
		const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext)
{
	HRESULT hr;
	V_RETURN(g_DialogResourceManager.OnD3D10ResizedSwapChain(pd3dDevice,
			 pBackBufferSurfaceDesc));
	V_RETURN(g_D3DSettingsDlg.OnD3D10ResizedSwapChain(pd3dDevice,
			 pBackBufferSurfaceDesc));
	// Setup the camera's projection parameters
	float fAspectRatio =
		1.0;//pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
	g_Camera.SetProjParams(D3DX_PI / 4, fAspectRatio, 0.1f, 10.0f);
	g_Camera.SetWindow(pBackBufferSurfaceDesc->Width,
					   pBackBufferSurfaceDesc->Height);
	g_Camera.SetButtonMasks(MOUSE_RIGHT_BUTTON, MOUSE_WHEEL, MOUSE_MIDDLE_BUTTON);
	g_HUD.SetLocation(pBackBufferSurfaceDesc->Width - 170, 0);
	g_HUD.SetSize(170, 170);
	g_SampleUI.SetLocation(pBackBufferSurfaceDesc->Width - 170,
						   pBackBufferSurfaceDesc->Height - 300);
	g_SampleUI.SetSize(170, 300);
	// resize the texture so that it fits to the current screen size
	UINT width = (DXUTIsAppRenderingWithD3D9()) ?
				 DXUTGetD3D9BackBufferSurfaceDesc()->Width :
				 DXUTGetDXGIBackBufferSurfaceDesc()->Width;
	UINT height = (DXUTIsAppRenderingWithD3D9()) ?
				  DXUTGetD3D9BackBufferSurfaceDesc()->Height :
				  DXUTGetDXGIBackBufferSurfaceDesc()->Height;
	g_vsObj->SetupTextures(pd3dDevice, g_pEffect10, width, height);
	return S_OK;
}
//--------------------------------------------------------------------------------------
// Create any D3D10 resources that aren't dependant on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D10CreateDevice(ID3D10Device* pd3dDevice,
									 const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext)
{
	HRESULT hr;
	g_device = pd3dDevice;
	V_RETURN(g_DialogResourceManager.OnD3D10CreateDevice(pd3dDevice));
	V_RETURN(g_D3DSettingsDlg.OnD3D10CreateDevice(pd3dDevice));
	V_RETURN(D3DX10CreateFont(pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
							  OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
							  L"Arial", &g_pFont10));
	V_RETURN(D3DX10CreateSprite(pd3dDevice, 512, &g_pSprite10));
	g_pTxtHelper = new CDXUTTextHelper(NULL, NULL, g_pFont10, g_pSprite10, 15);
	V_RETURN(CDXUTDirectionWidget::StaticOnD3D10CreateDevice(pd3dDevice));
	// Read the D3DX effect file
	WCHAR str[MAX_PATH];
	ID3D10Blob* pErrBlob = NULL;
	V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, L"Effect_Undistort.fx"));
	hr = D3DX10CreateEffectFromFile(str, NULL, NULL, "fx_4_0",
									D3D10_SHADER_ENABLE_STRICTNESS, 0,
									pd3dDevice, NULL, NULL, &g_pEffect10, &pErrBlob, NULL);

	if (FAILED(hr))
	{
		std::string errStr((LPCSTR)pErrBlob->GetBufferPointer(),
						   pErrBlob->GetBufferSize());
		WCHAR err[256];
		MultiByteToWideChar(CP_ACP, 0, errStr.c_str(), (int)errStr.size(), err,
							errStr.size());
		MessageBox(NULL, (LPCWSTR)err, L"Error", MB_OK);
		return hr;
	}

	// Setup the vector image and the display
	UINT width = (DXUTIsAppRenderingWithD3D9()) ?
				 DXUTGetD3D9BackBufferSurfaceDesc()->Width :
				 DXUTGetDXGIBackBufferSurfaceDesc()->Width;
	UINT height = (DXUTIsAppRenderingWithD3D9()) ?
				  DXUTGetD3D9BackBufferSurfaceDesc()->Height :
				  DXUTGetDXGIBackBufferSurfaceDesc()->Height;
	g_vsObj = new VSObject(pd3dDevice);
	D3D10_RASTERIZER_DESC rasterizerState;
	rasterizerState.FillMode = D3D10_FILL_SOLID;
	rasterizerState.CullMode = D3D10_CULL_NONE;
	rasterizerState.FrontCounterClockwise = true;
	rasterizerState.DepthBias = false;
	rasterizerState.DepthBiasClamp = 0;
	rasterizerState.SlopeScaledDepthBias = 0;
	rasterizerState.DepthClipEnable = true;
	rasterizerState.ScissorEnable = false;
	rasterizerState.MultisampleEnable = false;
	rasterizerState.AntialiasedLineEnable = false;
	pd3dDevice->CreateRasterizerState(&rasterizerState, &g_pRasterState);
	return S_OK;
}
void DX11CustomRenderTarget::Unbind( ID3D11DeviceContext* pd3dDeviceContext )
{
	D3D11_VIEWPORT vp;
	vp.Width = (FLOAT)DXUTGetDXGIBackBufferSurfaceDesc()->Width;
	vp.Height = (FLOAT)DXUTGetDXGIBackBufferSurfaceDesc()->Height;
	vp.MinDepth = 0;
	vp.MaxDepth = 1;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;
	pd3dDeviceContext->RSSetViewports( 1, &vp );

	ID3D11DepthStencilView* pDSV = DXUTGetD3D11DepthStencilView();
	ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView();

	pd3dDeviceContext->OMSetRenderTargets(1, &pRTV, pDSV);
}
Exemple #4
0
// Render the help and statistics text
void RenderText()
{
    UINT nBackBufferHeight = ( DXUTIsAppRenderingWithD3D9() ) ? DXUTGetD3D9BackBufferSurfaceDesc()->Height :
            DXUTGetDXGIBackBufferSurfaceDesc()->Height;

    gTxtHelper->Begin();
    gTxtHelper->SetInsertionPos( 2, 0 );
    gTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
    gTxtHelper->DrawTextLine( DXUTGetFrameStats( false ) );
    gTxtHelper->DrawTextLine( DXUTGetDeviceStats() );

    // Draw help
    if( gShowHelp )
    {
        gTxtHelper->SetInsertionPos( 2, nBackBufferHeight - 20 * 6 );
        gTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 0.75f, 0.0f, 1.0f ) );
        gTxtHelper->DrawTextLine( L"Controls:" );

        gTxtHelper->SetInsertionPos( 20, nBackBufferHeight - 20 * 5 );
        gTxtHelper->DrawTextLine(	L"Switch Compressed/Uncompressed views: TAB\n"
									L"Hide help: F1\n"
                                    L"Quit: ESC\n" );
    }
    else
    {
        gTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
        gTxtHelper->DrawTextLine( L"Press F1 for help" );
    }

    gTxtHelper->End();
}
//--------------------------------------------------------------------------------------
// Render the help and statistics text
//--------------------------------------------------------------------------------------
void RenderText()
{
    UINT nBackBufferHeight = DXUTGetDXGIBackBufferSurfaceDesc()->Height;

    g_pTxtHelper->Begin();
    g_pTxtHelper->SetInsertionPos(2, 0);
    g_pTxtHelper->SetForegroundColor(XMFLOAT4(1.0f, 1.0f, 0.0f, 1.0f));
    g_pTxtHelper->DrawTextLine(DXUTGetFrameStats(DXUTIsVsyncEnabled()));
    g_pTxtHelper->DrawTextLine(DXUTGetDeviceStats());

    // Draw help
    if (g_bShowHelp)
    {
        g_pTxtHelper->SetInsertionPos(2, nBackBufferHeight - 20 * 6);
        g_pTxtHelper->SetForegroundColor(XMFLOAT4(1.0f, 0.75f, 0.0f, 1.0f));
        g_pTxtHelper->DrawTextLine(L"Controls:");

        g_pTxtHelper->SetInsertionPos(20, nBackBufferHeight - 20 * 5);
        g_pTxtHelper->DrawTextLine(L"Move forward and backward with 'E' and 'D'\n"
            L"Move left and right with 'S' and 'D' \n"
            L"Click the mouse button to roate the camera\n");

        g_pTxtHelper->SetInsertionPos(350, nBackBufferHeight - 20 * 5);
        g_pTxtHelper->DrawTextLine(L"Hide help: F1\n"
            L"Quit: ESC\n");
    }
    else
    {
        g_pTxtHelper->SetForegroundColor(XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f));
        g_pTxtHelper->DrawTextLine(L"Press F1 for help");
    }

    g_pTxtHelper->End();
}
//--------------------------------------------------------------------------------------
// Render the scene using the D3D11 device
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D11FrameRender(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double /*fTime*/,
    FLOAT fElapsedTime, void* /*pUserContext*/)
{

    if (g_D3DSettingsDlg.IsActive())
    {
        g_D3DSettingsDlg.OnRender(fElapsedTime);
        return;
    }

    FLOAT ClearColor[4] = { 0.0f, 0.25f, 0.25f, 0.55f };
    ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView();
    ID3D11DepthStencilView* pDSV = DXUTGetD3D11DepthStencilView();
    pd3dImmediateContext->ClearRenderTargetView(pRTV, ClearColor);
    pd3dImmediateContext->ClearDepthStencilView(pDSV, D3D11_CLEAR_DEPTH, 1.0, 0);

    g_VarianceShadow.InitFrame(pd3dDevice, g_pSelectedMesh);

    g_VarianceShadow.RenderShadowsForAllCascades(pd3dDevice, pd3dImmediateContext, g_pSelectedMesh);

    D3D11_VIEWPORT vp;
    vp.Width = (FLOAT)DXUTGetDXGIBackBufferSurfaceDesc()->Width;
    vp.Height = (FLOAT)DXUTGetDXGIBackBufferSurfaceDesc()->Height;
    vp.MinDepth = 0;
    vp.MaxDepth = 1;
    vp.TopLeftX = 0;
    vp.TopLeftY = 0;

    g_VarianceShadow.RenderScene(
        pd3dImmediateContext, pRTV, pDSV, g_pSelectedMesh, g_pActiveCamera, &vp, g_bVisualizeCascades);

    pd3dImmediateContext->RSSetViewports(1, &vp);
    pd3dImmediateContext->OMSetRenderTargets(1, &pRTV, pDSV);

    DXUT_BeginPerfEvent(DXUT_PERFEVENTCOLOR, L"HUD / Stats");

    g_HUD.OnRender(fElapsedTime);
    g_SampleUI.OnRender(fElapsedTime);
    RenderText();
    DXUT_EndPerfEvent();
}
//--------------------------------------------------------------------------------------
// Render text part of the GUI
//--------------------------------------------------------------------------------------
void RenderText()
{
    g_pTxtHelper->Begin();
    g_pTxtHelper->SetInsertionPos( 2, 0 );
    g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 0.0f, 1.0f, 1.0f ) );
    g_pTxtHelper->DrawTextLine( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
    g_pTxtHelper->DrawTextLine( DXUTGetDeviceStats() );
    g_pTxtHelper->DrawTextLine( L"'G' key toggles display of GUI" );

    g_pTxtHelper->End();
 
    if( g_pScenePS == NULL )
    {
        g_pTxtHelper1->Begin();
        g_pTxtHelper1->SetInsertionPos( ( DXUTGetDXGIBackBufferSurfaceDesc()->Width - 300 )/2,
                                          DXUTGetDXGIBackBufferSurfaceDesc()->Height / 2 );
        g_pTxtHelper1->SetForegroundColor( D3DXCOLOR( 1.0f, 0.0f, 0.0f, 1.0f ) );
        g_pTxtHelper1->DrawTextLine( L"Compiling shaders ..." );
        g_pTxtHelper1->End();
    }
 }
static HRESULT CALLBACK on_device_create( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc,
    void* pUserContext )
{
    g_render_context.reset(new c_render_system_context(DXUTGetD3D11Device(), 
        DXUTGetD3D11DeviceContext(), 
        DXUTGetDXGIBackBufferSurfaceDesc(), 
        DXUTGetDXGISwapChain())); 

    g_scene->on_create_resource(g_render_context); 
    g_resolver->on_create_resource(g_render_context);

    return S_OK;
}
Exemple #9
0
void InitApp(ID3D11Device* d3dDevice)
{
	//#MSH if an app exists, destroy it
    DestroyApp();
    
    // Get current UI settings
    unsigned int msaaSamples = PtrToUint(gMSAACombo->GetSelectedData());
    gApp = new App(d3dDevice, 1 << gLightsSlider->GetValue(), msaaSamples);

    // Initialize with the current surface description
    gApp->OnD3D11ResizedSwapChain(d3dDevice, DXUTGetDXGIBackBufferSurfaceDesc());

    // Zero out the elapsed time for the next frame
    gZeroNextFrameTime = true;


}
//--------------------------------------------------------------------------------------
// Render the help and statistics text. This function uses the ID3DXFont interface for 
// efficient text rendering.
//--------------------------------------------------------------------------------------
void RenderText()
{
    // Output statistics
    g_pTxtHelper->Begin();
    g_pTxtHelper->SetInsertionPos( 5, 5 );
    g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
    g_pTxtHelper->DrawTextLine( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
    g_pTxtHelper->DrawTextLine( DXUTGetDeviceStats() );

    g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
    g_pTxtHelper->DrawTextLine( g_strFileSaveMessage );

    // Draw help
    if( g_bShowHelp )
    {
        UINT nBackBufferHeight = DXUTGetDXGIBackBufferSurfaceDesc()->Height;
        g_pTxtHelper->SetInsertionPos( 10, nBackBufferHeight - 15 * 5 );
        g_pTxtHelper->SetForegroundColor( D3DCOLOR_ARGB( 200, 150, 150, 150 ) );
        g_pTxtHelper->DrawTextLine( L"Controls (F1 to hide):" );

        g_pTxtHelper->SetInsertionPos( 20, nBackBufferHeight - 15 * 4 );
        g_pTxtHelper->DrawTextLine( L"Rotate model: Left mouse button\n"
                                    L"Rotate camera: Right mouse button\n"
                                    L"Zoom camera: Mouse wheel scroll\n" );

        g_pTxtHelper->SetInsertionPos( 250, nBackBufferHeight - 15 * 4 );
        g_pTxtHelper->DrawTextLine( L"Hide help: F1\n" );
        g_pTxtHelper->DrawTextLine( L"Quit: ESC\n" );
    }
    else
    {
        g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
        g_pTxtHelper->DrawTextLine( L"Press F1 for help" );
    }

    g_pTxtHelper->End();
}
//--------------------------------------------------------------------------------------
// Render the help and statistics text
//--------------------------------------------------------------------------------------
void RenderText()
{
    UINT nBackBufferHeight = ( DXUTIsAppRenderingWithD3D9() ) ? DXUTGetD3D9BackBufferSurfaceDesc()->Height :
            DXUTGetDXGIBackBufferSurfaceDesc()->Height;

    g_pTxtHelper->Begin();
    g_pTxtHelper->SetInsertionPos( 2, 0 );
    g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
    g_pTxtHelper->DrawTextLine( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
    g_pTxtHelper->DrawTextLine( DXUTGetDeviceStats() );

    // Draw help
    if( g_bShowHelp )
    {
        g_pTxtHelper->SetInsertionPos( 2, nBackBufferHeight - 20 * 6 );
        g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 0.75f, 0.0f, 1.0f ) );
        g_pTxtHelper->DrawTextLine( L"Controls:" );

        g_pTxtHelper->SetInsertionPos( 20, nBackBufferHeight - 20 * 5 );
        g_pTxtHelper->DrawTextLine( L"Rotate model: Left mouse button\n"
                                    L"Rotate light: Right mouse button\n"
                                    L"Rotate camera: Middle mouse button\n"
                                    L"Zoom camera: Mouse wheel scroll\n" );

        g_pTxtHelper->SetInsertionPos( 550, nBackBufferHeight - 20 * 5 );
        g_pTxtHelper->DrawTextLine( L"Hide help: F1\n"
                                    L"Quit: ESC\n" );
    }
    else
    {
        g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
        g_pTxtHelper->DrawTextLine( L"Press F1 for help" );
    }

    g_pTxtHelper->End();
}
Exemple #12
0
void Input::Pick(_Out_ XMVECTOR& rayPos, _Out_ XMVECTOR& rayDir, _In_ XMMATRIX& world, _In_ XMMATRIX& view, _In_ XMMATRIX& proj)
{
	const DXGI_SURFACE_DESC* pd3dsdBackBuffer = DXUTGetDXGIBackBufferSurfaceDesc();

	POINT ptcursor = getMousePos();

	// Compute the vector of the pick ray in screen space
	XMFLOAT4X4	mproj = XMFLOAT4X4();
	XMStoreFloat4x4(&mproj, proj);

	XMFLOAT3 v;
	v.x = (((2.0f * ptcursor.x) / pd3dsdBackBuffer->Width) - 1) / mproj._11;
	v.y = -(((2.0f * ptcursor.y) / pd3dsdBackBuffer->Height) - 1) / mproj._22;
	v.z = 1.0f;

	// Get the inverse view matrix	
	XMMATRIX mWorldView = world * view;
	//XMVECTOR determin;
	XMMATRIX mInv = XMMatrixInverse(NULL, mWorldView);

	XMFLOAT4X4	m = XMFLOAT4X4();
	XMStoreFloat4x4(&m, mInv);

	// Transform the screen space pick ray into 3D space
	XMFLOAT3 pickRayDir;
	pickRayDir.x = v.x * m._11 + v.y * m._21 + v.z * m._31;
	pickRayDir.y = v.x * m._12 + v.y * m._22 + v.z * m._32;
	pickRayDir.z = v.x * m._13 + v.y * m._23 + v.z * m._33;
	rayDir = XMLoadFloat3(&pickRayDir);

	XMFLOAT3 pickRayOrig;
	pickRayOrig.x = m._41;
	pickRayOrig.y = m._42;
	pickRayOrig.z = m._43;
	rayPos = XMLoadFloat3(&pickRayOrig);
}
Exemple #13
0
void CALLBACK OnD3D11FrameRender(ID3D11Device* d3dDevice, ID3D11DeviceContext* d3dDeviceContext, double time,
                                 float elapsedTime, void* userContext)
{
	D3DXVECTOR3 cubePos;

    if (gZeroNextFrameTime) {
        elapsedTime = 0.0f;
    }
    gZeroNextFrameTime = false;

    if (gD3DSettingsDlg.IsActive()) {
        gD3DSettingsDlg.OnRender(elapsedTime);
        return;
    }

    // Lazily create the application if need be
    if (!gApp) {
        InitApp(d3dDevice);
    }

    // Lazily load scene
	/*!gMeshOpaque.IsLoaded() && !gMeshAlpha.IsLoaded() &&!gMeshOpaque2.IsLoaded()*/
    if (!sceneGraph.IsLoaded()) {
        InitScene(d3dDevice);
    }
	if(PXEngine)
	{
		for(int i = 0; i < cubeList->size(); i++)
		PXEngine->StepPhysX();
		if(cubeList)
		{
			for(int i = 0; i < cubeList->size(); i++)
			{
				if((*cubeList)[i]->id<0)
				{
					sceneGraph.SetInstancePosition(1,(*cubeList)[i]->id, (*cubeList)[i]->x, (*cubeList)[i]->y, (*cubeList)[i]->z);
				}
				else
				{
					sceneGraph.SetMeshPosition(0,(*cubeList)[i]->id, (*cubeList)[i]->x, (*cubeList)[i]->y, (*cubeList)[i]->z);
				}
			}
		}
	}

	//crosshair
	D3DRECT rec2 = {screenCenterX-20, screenCenterY, screenCenterX + 20, screenCenterY + 2};
	D3DRECT rec3 = {screenCenterX, screenCenterY - 20, screenCenterX + 2, screenCenterY + 20};

    ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView();
	
    
    D3D11_VIEWPORT viewport;
    viewport.Width    = static_cast<float>(DXUTGetDXGIBackBufferSurfaceDesc()->Width);
    viewport.Height   = static_cast<float>(DXUTGetDXGIBackBufferSurfaceDesc()->Height);
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;
    viewport.TopLeftX = 0.0f;
    viewport.TopLeftY = 0.0f;
	screenCenterX = viewport.Width / 2;
	screenCenterY = viewport.Height / 2;

	gViewPort.Width = viewport.Width;
	gViewPort.Height = viewport.Height;
	gViewPort.MaxZ = viewport.MaxDepth;
	gViewPort.MinZ = viewport.MinDepth;
	gViewPort.X = viewport.TopLeftX;
	gViewPort.Y = viewport.TopLeftY;


		 gApp->Render(d3dDeviceContext, pRTV, sceneGraph, gSkyboxSRV,
        gWorldMatrix, &gViewerCamera, &viewport, &gUIConstants);
	
		 
		 sceneGraph.SetMeshPosition(0,1, 100000, 100000, 100000);
    if (gDisplayUI) {
        d3dDeviceContext->RSSetViewports(1, &viewport);

        // Render HUDs in reverse order
        d3dDeviceContext->OMSetRenderTargets(1, &pRTV, 0);
        for (int i = HUD_NUM - 1; i >= 0; --i) {
            gHUD[i].OnRender(elapsedTime);
        }

        // Render text
        gTextHelper->Begin();

        gTextHelper->SetInsertionPos(2, 0);
        gTextHelper->SetForegroundColor(D3DXCOLOR(1.0f, 1.0f, 0.0f, 1.0f));
        gTextHelper->DrawTextLine(DXUTGetFrameStats(DXUTIsVsyncEnabled()));
        //gTextHelper->DrawTextLine(DXUTGetDeviceStats());

        // Output frame time
        {
            std::wostringstream oss;
            oss << 1000.0f / DXUTGetFPS() << " ms / frame";
            gTextHelper->DrawTextLine(oss.str().c_str());
        }

        // Output light info
        {
            std::wostringstream oss;
            oss << "Lights: " << gApp->GetActiveLights();
            gTextHelper->DrawTextLine(oss.str().c_str());
        }
        
        gTextHelper->End();
    }
	
}
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D10FrameRender(ID3D10Device* pDev10, double fTime, float fElapsedTime, void* pUserContext)
{
    HRESULT hr;
	g_pCamManager->SyncToCameraUI(g_CameraUI);
    //dont delete, used no only here
   	const DXGI_SURFACE_DESC *pBackBufferSurfaceDesc = DXUTGetDXGIBackBufferSurfaceDesc();

    if( ShadowAlgorithm != OldShadowAlgorithm )
    {
        OldShadowAlgorithm = ShadowAlgorithm;
        switch( ShadowAlgorithm )
        {
        case    STANDARD_BP:
                g_ABP.OnD3D10DestroyDevice();
                break;
        case    BP_MSSM_KERNEL:
	            g_BPMSSMKernel.OnD3D10DestroyDevice();
                break;
        case    STD_VSM:
	            g_StdVSM.OnD3D10DestroyDevice();
                break;
        case    MIP_VSM:
	            g_MipVSM.OnD3D10DestroyDevice();
                break;
        case    HIR_BP:
                g_HBP.OnD3D10DestroyDevice();
                break;
        case    BP_GI:
                g_BPGI.OnD3D10DestroyDevice();
                break;
        case    STD_PCSS:
                g_PCSS.OnD3D10DestroyDevice();
                break;
        default:
                break;
        };
		switch( ShadowAlgorithm )
        {
        case    STANDARD_BP:
                g_ABP.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext);
                g_ABP.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext );
                break;
        case    BP_MSSM_KERNEL:
	            g_BPMSSMKernel.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext);
                g_BPMSSMKernel.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext );
                break;
        case    STD_VSM:
	            g_StdVSM.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext);
                g_StdVSM.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext );
                break;
        case    MIP_VSM:
	            g_MipVSM.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext);
                g_MipVSM.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext );
                break;
        case    HIR_BP:
                g_HBP.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext);
                g_HBP.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext );
                break;
        case    BP_GI:
                g_BPGI.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext);
                g_BPGI.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext );
                break;
        case    STD_PCSS:
                g_PCSS.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext);
                g_PCSS.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext );
                break;
        default:
                break;
        };
    }

	//begin  light and view pos management
	{		
		static double old_fTime = 0.001;
		fTime = old_fTime;
		old_fTime += 0.02;
		static double oldTime = 0;
		static unsigned old_iSta = 0;
		static double stop_time = 0;
		static double total_stop_time = 0;
		double tmp = fTime;
		unsigned iSta = g_SampleUI.GetCheckBox(IDC_STATIC)->GetChecked();
		if( 0 == old_iSta  && 1 == iSta )//turn to be static
		{
			stop_time = fTime - total_stop_time;
		}
		if( 1 == iSta )
		{
			total_stop_time += ( fTime - oldTime );
			fTime = stop_time;
		}
		if( 0 == iSta )
		{
			fTime -= total_stop_time;
		}
		old_iSta = iSta;
		oldTime = tmp;
	}//end light and view pos management
	S3UTCamera& g_CameraRef = *(g_pCamManager->ActiveEye());

	// compute view matrix
	D3DXMATRIX mTmp, mWorldView, mWorldViewProj, mWorldViewInv;
	D3DXMatrixInverse(&mTmp, NULL, g_CameraRef.GetWorldMatrix());
	D3DXMatrixMultiply(&mWorldView, &mTmp, g_CameraRef.GetViewMatrix());

	// correct near/far clip planes according to camera location
	D3DXVECTOR3 vBox[2];
	float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;

	// clear depth and color
	ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView();
	pDev10->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0);
	ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView();

	if( g_D3DSettingsDlg.IsActive() )
	{
		g_D3DSettingsDlg.OnRender( fElapsedTime );
		return;
	}


	Parameters para;
	para.fLightZn				=	g_fCtrledLightZn;				

	float biases[15];
	biases[0]	=	g_fDepthBiasObject0;
	biases[1]	=	g_fDefaultDepthBias;		
	biases[2]	=	g_fDepthBiasHammer;
	biases[3]	=	g_fDepthBiasLeftForearm;
	biases[4]	=	g_fDepthBiasRightForearm;
	biases[5]	=	g_fDepthBiasLeftShoulder;
	biases[6]	=	g_fDepthBiasRightShoulder;
	biases[7]	=	g_fDepthBiasBlackPlate;
	biases[8]	=	g_fDepthBiasHelmet;
	biases[9]	=	g_fDepthBiasEyes;
	biases[10]	=	g_fDepthBiasBelt;
	biases[11]	=	g_fDepthBiasLeftThigh;
	biases[12]	=	g_fDepthBiasRightThigh;
	biases[13]	=	g_fDepthBiasLeftShin;
	biases[14]	=	g_fDepthBiasRightShin;
	g_MeshScene.set_biases(biases,15);
	
	float light_size[NUM_LIGHT] = LIGHT_SIZE;
	float light_ZNS[NUM_LIGHT] = LIGHT_ZNS;
	float light_view_angle[NUM_LIGHT] = LIGHT_VIEW_ANGLES;

	D3DXVECTOR4 light_color[NUM_LIGHT] = LIGHT_COLOR;

	bool render_ogre = g_SampleUI.GetCheckBox( IDC_ANIMATE )->GetChecked();
	bool render_scene = g_SampleUI.GetCheckBox( IDC_SCENE )->GetChecked();
	bool render_fan = g_SampleUI.GetCheckBox( IDC_FAN )->GetChecked();

	float ClearColor[4] = { 0, 0, 0, 1 };

	pDev10->ClearRenderTargetView(g_pBlendBuffer->m_pRTView, ClearColor);

	//use alpha val 1 to represent untouched pixels
	pDev10->ClearRenderTargetView(g_pWidgetBuffer->m_pRTView, ClearColor);

	//light management
	ID3D10RenderTargetView *p_RTV;
	ID3D10ShaderResourceView *p_SRV;

	static float light_scale_factor = 0.2;
	static float ls_incre = 0.04;
	
	if( g_LightVary == true || g_fFilterSize < g_fFilterSizeCtrl )
	{
		g_fFilterSize -= ls_incre;
		if( g_fFilterSize < 0.1 || g_fFilterSize > g_fFilterSizeCtrl )
			ls_incre = -ls_incre;
	}
	else
	{
		g_fFilterSize = g_fFilterSizeCtrl;
	}
	g_fFilterSize = g_fFilterSizeCtrl;
	
	//light pos is not modified here. this light pos stored in 3DWidget is only synced with light camera after rendering of the light
	//if you try to modify light pos here, light would be freezed, of course, this is a bug introduced by bad design
	D3DXVECTOR3 vTmp;

	// render GBuffer
	pDev10->OMSetDepthStencilState(g_pDSState,0);
	g_GBuffer.OnD3D10FrameRender(	true, true, g_SampleUI, 
									g_MeshScene, g_CameraRef, 
									pDev10, fTime, fElapsedTime, pUserContext );
	
	g_Blender.OriginalSampleMask = 0;
	pDev10->OMGetBlendState( &g_Blender.pOriginalBlendState, g_Blender.OriginalBlendFactor, &g_Blender.OriginalSampleMask );

	float BlendBufferClearColor[4] = { 1, 1, 0, 1 };
	bool isFirstPass = true;
	for( int cam_idx = 0; cam_idx < g_pCamManager->CameraCount(); ++cam_idx )
	{
	// rendering a subdivided light
		if( g_pCamManager->Camera(cam_idx)->GetCamType() == S3UTCamera::eLight&&
			g_pCamManager->Camera(cam_idx)->IsActive() )
		{
			float scaled_half_light_size = (g_pCamManager->Camera(cam_idx)->GetLightSize()*LIGHT_SCALE_FACTOR);
			float fStartPt = -scaled_half_light_size;
			float fInterval = 2 * scaled_half_light_size / g_nNumLightSample;
			float fSubLightSize = g_pCamManager->Camera(cam_idx)->GetLightSize();
			if( g_nNumLightSample > 0 )
			{
				fSubLightSize = fSubLightSize / g_nNumLightSample;
			}
			if( g_nNumLightSample == 0 )
			{
				g_nNumLightSample = 1;
			}

			static float total_light_x_incre = 0;
			static int light_mov_dir = 0;
			float shadow_factor = 0.8/(g_nNumLightSample * g_nNumLightSample);
			for( int ix = 0; ix < g_nNumLightSample; ++ix )
			{
				for( int iy = 0; iy < g_nNumLightSample; ++iy )
				{
					D3DXVECTOR3 vLight = *g_pCamManager->Camera(cam_idx)->GetEyePt();

					S3UTCamera  local_cam = *g_pCamManager->Camera(cam_idx);

					D3DXVECTOR3 vTrans( fStartPt+(ix+0.5)*fInterval,fStartPt+(iy+0.5)*fInterval,0 );
					if( g_nNumLightSample == 1 )
					{
						vTrans = D3DXVECTOR3(0,0,0);
					}
					D3DXMATRIX mInvLightView;
					D3DXVECTOR4 tmp_light_pos;
					D3DXMatrixInverse(&mInvLightView, NULL, local_cam.GetViewMatrix());
					D3DXVec3Transform(&tmp_light_pos, &vTrans, &mInvLightView );
					D3DXVECTOR3 tmp_light_pos_3(tmp_light_pos.x,tmp_light_pos.y,tmp_light_pos.z);
					D3DXVECTOR3 vLookAt = *local_cam.GetLookAtPt();

					local_cam.SetViewParams( &tmp_light_pos_3, &vLookAt );
					
					g_MeshScene.set_parameters( render_ogre, render_scene, render_fan, false );
					D3DXMATRIX mLightView;
					// here we compute light viewprojection so that light oversees the whole scene
					D3DXMATRIX mTranslate;

					D3DXMatrixInverse(&mTranslate, NULL, local_cam.GetWorldMatrix());
					D3DXMatrixMultiply(&mLightView, &mTranslate, local_cam.GetViewMatrix());
			
					unsigned iTmp = g_SampleUI.GetCheckBox(IDC_BDUMP_SHADOWMAP)->GetChecked();
					ssmap.Render(pDev10, &g_MeshScene, local_cam,fTime,fElapsedTime,iTmp);
					
					pDev10->RSSetState(g_pRenderState);

					p_RTV = g_pBlendBuffer->m_pRTView;
			
					float NewBlendFactor[4] = {0,0,0,0};

					//alpha blending initial;
					if( isFirstPass )
					{		
						isFirstPass = false;
						pDev10->OMSetBlendState( g_Blender.m_pSceneBlendStateInitial, NewBlendFactor, 0xffffffff );					
					}		
					else
					{
						pDev10->OMSetBlendState( g_Blender.m_pSceneBlendStateOn, NewBlendFactor, 0xffffffff );											
					}
					if( ShadowAlgorithm == BP_GI )
					{
						V(g_BPGI.m_pEffect->GetVariableByName("g_fLumiFactor")->AsScalar()->SetFloat( shadow_factor ));

						g_BPGI.set_parameters( para,p_RTV,&light_color[0] );
						g_BPGI.set_input_buffer( &g_GBuffer );
						g_BPGI.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext);
					}
					else if( ShadowAlgorithm == STANDARD_BP )
					{
						V(g_ABP.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor ));

						g_ABP.set_parameters( para,p_RTV,&light_color[0] );
						g_ABP.set_input_buffer( &g_GBuffer );
						g_ABP.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext);
					}
					else if( ShadowAlgorithm == BP_MSSM_KERNEL )
					{
						V(g_BPMSSMKernel.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor ));

						g_BPMSSMKernel.set_parameters( para,p_RTV,&light_color[0] );
						g_BPMSSMKernel.set_input_buffer( &g_GBuffer );
						g_BPMSSMKernel.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext);
					}
					else if( ShadowAlgorithm == STD_VSM )
					{
						V(g_StdVSM.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor ));

						g_StdVSM.set_bias( g_fDefaultDepthBias,g_f3rdDepthDelta, g_f1stDepthDelta );
						g_StdVSM.set_parameters( para,p_RTV,&light_color[0] );
						g_StdVSM.set_input_buffer( &g_GBuffer );
						g_StdVSM.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext);
					}
					else if( ShadowAlgorithm == MIP_VSM )
					{
						V(g_MipVSM.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor ));

						g_MipVSM.set_parameters( para,p_RTV,&light_color[0] );
						g_MipVSM.set_input_buffer( &g_GBuffer );
						g_MipVSM.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext);
					}
					else if( ShadowAlgorithm == STD_PCSS )
					{
						V(g_PCSS.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor ));

						g_PCSS.set_parameters( para,p_RTV,&light_color[0] );
						g_PCSS.set_input_buffer( &g_GBuffer );
						g_PCSS.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext);
					}
					//alpha blending restore			
					pDev10->OMSetBlendState( g_Blender.pOriginalBlendState, g_Blender.OriginalBlendFactor, g_Blender.OriginalSampleMask );
					if( g_SampleUI.GetCheckBox( IDC_SHOW_3DWIDGET )->GetChecked() )
					{
						if( g_pCamManager->Camera(cam_idx)->GetCamType() == S3UTCamera::eLight&&
							g_pCamManager->Camera(cam_idx)->IsActive() )
						{
							pDev10->OMSetRenderTargets(1, &(g_pWidgetBuffer->m_pRTView), g_GBuffer.m_pDepthBuffer->m_pDSView);
							g_Widget.m_pSsmap = &ssmap;
							g_Widget.OnD3D10FrameRender(pDev10,g_CameraRef,local_cam);
						}
					}
				}
			}

		}
	}
	//-----------------------------------------------------------------------------------

	D3DXMATRIX mMatrixScale;
	D3DXMATRIX mMatrixScaleWVP;
	D3DXMatrixScaling( &mMatrixScale,(FLOAT)5,(FLOAT)5,(FLOAT)5 );
	D3DXMatrixMultiply( &mMatrixScaleWVP, &mMatrixScale, &mWorldViewProj );
	ID3D10RenderTargetView* pOrigRTV = DXUTGetD3D10RenderTargetView();
	pDev10->OMSetRenderTargets(1,&pOrigRTV,NULL);
	
	float FinalClearColor[4] = { 1, 0, 0, 1 };
	pDev10->ClearRenderTargetView(pOrigRTV, FinalClearColor);
	
	g_MeshScene.set_parameters( render_ogre,render_scene, render_fan );
	g_Final.set_parameters( para, pOrigRTV, NULL, g_pBlendBuffer->m_pSRView, g_pWidgetBuffer->m_pSRView );
	S3UTCamera& g_LCameraRef = g_LCamera[0];
	g_Final.set_input_buffer( &g_GBuffer );
	g_pSkyBox->OnFrameRender( mMatrixScaleWVP );
	g_Final.OnD3D10FrameRender(g_SampleUI,g_MeshScene,g_fFilterSize,ssmap,g_CameraRef,g_LCameraRef,pDev10,fTime,fElapsedTime,pUserContext);

	g_LCameraRef.SetProjParams(g_fCtrledLightFov, 1.0, g_fCtrledLightZn, g_fCtrledLightZf);
	
	//if( g_SampleUI.GetCheckBox( IDC_SHOW_3DWIDGET )->GetChecked() )
	//{
	//		for( int cam_idx = 0; cam_idx < g_pCamManager->CameraCount(); ++cam_idx )
	//		{
	//		// rendering a subdivided light
	//			if( g_pCamManager->Camera(cam_idx)->GetCamType() == S3UTCamera::eLight&&
	//				g_pCamManager->Camera(cam_idx)->IsActive() )
	//			{
	//				g_Widget.m_pSsmap = &ssmap;
	//				g_Widget.OnD3D10FrameRender(pDev10,g_CameraRef,*(g_pCamManager->Camera(cam_idx)));
	//			}
	//		}
	//}

    // render UI
    if (g_bShowUI)
    {
		RenderText();
        g_SampleUI.OnRender(fElapsedTime);
        //g_HUD.OnRender(fElapsedTime);
    }
	if( g_bShowLightUI )
	{
		g_CameraUI.OnRender(fElapsedTime);
	}

	if( g_SampleUI.GetCheckBox( IDC_FRAME_DUMP )->GetChecked() )
	{
	  static int g_Frame = 0;
	  IDXGISwapChain* pSwapChain = DXUTGetDXGISwapChain();
	  ID3D10Texture2D* pRT;
	  pSwapChain->GetBuffer(0, __uuidof(pRT), reinterpret_cast<void**>(&pRT));
	  WCHAR filename[32];
	  StringCchPrintf(filename, 100, L"DumpedImages\\screenshot%.3d.jpg", g_Frame); 
	  D3DX10SaveTextureToFile(pRT, D3DX10_IFF_JPG, filename);
	  SAFE_RELEASE(pRT);
	  ++g_Frame;
	}
}
Exemple #15
0
void MagnifyTool::OnResizedSwapChain( ID3D10Device* pd3dDevice, IDXGISwapChain *pSwapChain, 
                                        const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext, 
                                        int nPositionX, int nPositionY )
{
    CreateDepthStencil( pd3dDevice, pSwapChain, pBackBufferSurfaceDesc, pUserContext );

    m_MagnifyUI.SetLocation( nPositionX, nPositionY );
    m_MagnifyUI.SetSize( 170, 155 );

    m_Magnify.OnResizedSwapChain( DXUTGetDXGIBackBufferSurfaceDesc() );

    if( m_bReleaseRTOnResize )
    {
        D3D10_RENDER_TARGET_VIEW_DESC RTDesc;
        DXUTGetD3D10RenderTargetView()->GetResource( &m_pSourceRTResource );
        DXUTGetD3D10RenderTargetView()->GetDesc( &RTDesc );
        m_RTFormat = RTDesc.Format;
        m_nWidth = DXUTGetDXGIBackBufferSurfaceDesc()->Width;
        m_nHeight = DXUTGetDXGIBackBufferSurfaceDesc()->Height;
        m_nSamples = DXUTGetDXGIBackBufferSurfaceDesc()->SampleDesc.Count;

        if( !m_MagnifyUI.GetCheckBox( IDC_MAGNIFY_CHECKBOX_DEPTH )->GetChecked() )
        {
            m_Magnify.SetSourceResource( m_pSourceRTResource, m_RTFormat, m_nWidth, m_nHeight, m_nSamples );
        }
    }

    if( m_bReleaseDepthOnResize )
    {
        D3D10_DEPTH_STENCIL_VIEW_DESC DSDesc;
        m_pDSV->GetResource( &m_pSourceDepthResource );
        m_pDSV->GetDesc( &DSDesc );
        m_DepthFormat = DSDesc.Format;
        m_nWidth = DXUTGetDXGIBackBufferSurfaceDesc()->Width;
        m_nHeight = DXUTGetDXGIBackBufferSurfaceDesc()->Height;
        m_nSamples = DXUTGetDXGIBackBufferSurfaceDesc()->SampleDesc.Count;

        if( m_MagnifyUI.GetCheckBox( IDC_MAGNIFY_CHECKBOX_DEPTH )->GetChecked() )
        {
            m_Magnify.SetSourceResource( m_pSourceDepthResource, m_DepthFormat, m_nWidth, m_nHeight, m_nSamples );
        }
    }

    EnableTool();

    if( m_nSamples > 1 )
    {
        EnableSubSampleUI();
    }
    else
    {
        DisableSubSampleUI();
    }

    ID3D10Device1* pd3dDevice1 = DXUTGetD3D10Device1();
    if( ( NULL == pd3dDevice1 ) && ( m_nSamples > 1 ) )
    {
        DisableDepthUI();

        if( NULL == m_pSourceRTResource )
        {
            DisableUI();
            DisableTool();
        }
        else
        {
            m_Magnify.SetSourceResource( m_pSourceRTResource, m_RTFormat, m_nWidth, m_nHeight, m_nSamples );
        }
    }
    else
    {
        if( NULL != m_pSourceDepthResource )
        {
            EnableDepthUI();
        }
    }
}