//--------------------------------------------------------------------------------------
// Render the scene using the D3D11 device
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime,
                                  float fElapsedTime, void* pUserContext )
{
    HRESULT hr;

    // If the settings dialog is being shown, then render it instead of rendering the app's scene
    if( g_D3DSettingsDlg.IsActive() )
    {
        g_D3DSettingsDlg.OnRender( fElapsedTime );
        return;
    }

    // Clear the render target and depth stencil
    auto pRTV = DXUTGetD3D11RenderTargetView();
    pd3dImmediateContext->ClearRenderTargetView( pRTV, Colors::MidnightBlue );
    auto pDSV = DXUTGetD3D11DepthStencilView();
    pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 );

    // Get the projection & view matrix from the camera class
    XMMATRIX mWorld = g_Camera.GetWorldMatrix();
    XMMATRIX mProj  = g_Camera.GetProjMatrix();
    XMMATRIX mView  = g_Camera.GetViewMatrix();

    // Get the light direction
    XMVECTOR vLightDir = g_LightControl.GetLightDirection();

    // Render the light arrow so the user can visually see the light dir
    V( g_LightControl.OnRender( Colors::Yellow, mView, mProj, g_Camera.GetEyePt() ) );

    // Ambient Light
    static const XMVECTORF32 s_vLightColorA = { 0.1f, 0.1f, 0.1f, 1.0f };
    g_pAmbientLightColor->SetFloatVector( s_vLightColorA );
    g_pAmbientLightEnable->SetBool(true);

    // Hemi Ambient Light
    static const XMVECTORF32 s_vLightColorH1 = { 0.3f, 0.3f, 0.4f, 1.0f };
    g_pHemiAmbientLightColor->SetFloatVector( s_vLightColorH1 );
    g_pHemiAmbientLightEnable->SetBool(true);

    XMFLOAT4 vLightGrndClr( 0.05f, 0.05f, 0.05f, 1.f );
    g_pHemiAmbientLightGroundColor->SetFloatVector( reinterpret_cast<float*>( &vLightGrndClr ) );

    XMFLOAT4 vVec(0.0f, 1.0f, 0.0f, 1.0f);
    g_pHemiAmbientLightDirUp->SetFloatVector( reinterpret_cast<float*>( &vVec ) );

    // Directional Light
    g_pDirectionalLightColor->SetFloatVector( Colors::White );
    g_pDirectionalLightEnable->SetBool(true);

    XMFLOAT4 tmp;
    XMStoreFloat4( &tmp, vLightDir );
    tmp.w = 1.f;
    g_pDirectionalLightDir->SetFloatVector( reinterpret_cast<float*>( &tmp ) );

    // Environment Light - color comes from the texture
    g_pEnvironmentLightColor->SetFloatVector( Colors::Black );
    g_pEnvironmentLightEnable->SetBool(true);

    // Setup the Eye based on the DXUT camera
    XMVECTOR vEyePt = g_Camera.GetEyePt();
    XMVECTOR vDir = g_Camera.GetLookAtPt() - vEyePt;
    XMStoreFloat4( &tmp, vDir );
    tmp.w = 1.f;
    g_pEyeDir->SetFloatVector( reinterpret_cast<float*>( &tmp ) );

    //Get the mesh
    //IA setup
    pd3dImmediateContext->IASetInputLayout( g_pVertexLayout11 );
    UINT Strides[1];
    UINT Offsets[1];
    ID3D11Buffer* pVB[1];
    pVB[0] = g_Mesh11.GetVB11( 0, 0 );
    Strides[0] = ( UINT )g_Mesh11.GetVertexStride( 0, 0 );
    Offsets[0] = 0;
    pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets );
    pd3dImmediateContext->IASetIndexBuffer( g_Mesh11.GetIB11( 0 ), g_Mesh11.GetIBFormat11( 0 ), 0 );
 
    // Set the per object constant data
    XMMATRIX mWorldViewProjection = mWorld * mView * mProj;

    // VS Per object
    XMFLOAT4X4 tmp4x4;
    XMStoreFloat4x4( &tmp4x4, mWorldViewProjection );
    g_pWorldViewProjection->SetMatrix( reinterpret_cast<float*>( &tmp4x4 ) );
    XMStoreFloat4x4( &tmp4x4, mWorld );
    g_pWorld->SetMatrix( reinterpret_cast<float*>( &tmp4x4 ) );

    // Setup the Shader Linkage based on the user settings for Lighting
    ID3DX11EffectClassInstanceVariable* pLightClassVar;
    
    // Ambient Lighting First - Constant or Hemi?
    if ( g_bHemiAmbientLighting )
    {
        pLightClassVar = g_pHemiAmbientLightClass;
    }
    else
    {
        pLightClassVar = g_pAmbientLightClass;
    }
    if (g_pAmbientLightIface)
    {
        g_pAmbientLightIface->SetClassInstance(pLightClassVar);
    }
    
    // Direct Light - None or Directional 
    if (g_bDirectLighting) 
    {
        pLightClassVar = g_pDirectionalLightClass;
    }
    else
    {
        // Disable ALL Direct Lighting
        pLightClassVar = g_pAmbientLightClass;
    }
    if (g_pDirectionalLightIface)
    {
        g_pDirectionalLightIface->SetClassInstance(pLightClassVar);
    }

    // Setup the selected material class instance
    E_MATERIAL_TYPES iMaterialTech = g_iMaterial;
    switch( g_iMaterial )
    {
    case MATERIAL_PLASTIC:
    case MATERIAL_PLASTIC_TEXTURED:
        // Bind the Environment light for reflections
        pLightClassVar = g_pEnvironmentLightClass;
        if (g_bLightingOnly)
        {
            iMaterialTech = MATERIAL_PLASTIC_LIGHTING_ONLY;
        }
        break;
    case MATERIAL_ROUGH:
    case MATERIAL_ROUGH_TEXTURED:
        // UnBind the Environment light 
        pLightClassVar = g_pAmbientLightClass;
        if (g_bLightingOnly)
        {
            iMaterialTech = MATERIAL_ROUGH_LIGHTING_ONLY;
        }
        break;
    }
    if (g_pEnvironmentLightIface)
    {
        g_pEnvironmentLightIface->SetClassInstance(pLightClassVar);
    }

    ID3DX11EffectTechnique* pTechnique = g_pTechnique;

    if (g_pMaterialIface)
    {
#if USE_BIND_INTERFACES

        // We're using the techniques with pre-bound materials,
        // so select the appropriate technique.
        pTechnique = g_MaterialClasses[ iMaterialTech ].pTechnique;

#else

        // We're using a single technique and need to explicitly
        // bind a concrete material instance.
        g_pMaterialIface->SetClassInstance( g_MaterialClasses[ iMaterialTech ].pClass );

#endif
    }

    // PS Per Prim

    // Shiny Plastic
    XMFLOAT3 clr1(1, 0, 0.5f);
    g_MaterialClasses[MATERIAL_PLASTIC].pColor->SetFloatVector( reinterpret_cast<float*>( &clr1 ) );
    g_MaterialClasses[MATERIAL_PLASTIC].pSpecPower->SetInt(255);

    // Shiny Plastic with Textures
    XMFLOAT3 clr2(1, 0, 0.5f);
    g_MaterialClasses[MATERIAL_PLASTIC_TEXTURED].pColor->SetFloatVector( reinterpret_cast<float*>( &clr2 ) );
    g_MaterialClasses[MATERIAL_PLASTIC_TEXTURED].pSpecPower->SetInt(128);

    // Lighting Only Plastic
    XMFLOAT3 clr3(1, 1, 1);
    g_MaterialClasses[MATERIAL_PLASTIC_LIGHTING_ONLY].pColor->SetFloatVector( reinterpret_cast<float*>( &clr3 ) );
    g_MaterialClasses[MATERIAL_PLASTIC_LIGHTING_ONLY].pSpecPower->SetInt(128);

    // Rough Material
    XMFLOAT3 clr4(0, 0.5f, 1);
    g_MaterialClasses[MATERIAL_ROUGH].pColor->SetFloatVector( reinterpret_cast<float*>( &clr4 ) );
    g_MaterialClasses[MATERIAL_ROUGH].pSpecPower->SetInt(6);

    // Rough Material with Textures
    XMFLOAT3 clr5(0, 0.5f, 1);
    g_MaterialClasses[MATERIAL_ROUGH_TEXTURED].pColor->SetFloatVector( reinterpret_cast<float*>( &clr5 ) );
    g_MaterialClasses[MATERIAL_ROUGH_TEXTURED].pSpecPower->SetInt(6);

    // Lighting Only Rough
    XMFLOAT3 clr6(1, 1, 1);
    g_MaterialClasses[MATERIAL_ROUGH_LIGHTING_ONLY].pColor->SetFloatVector( reinterpret_cast<float*>( &clr6 ) );
    g_MaterialClasses[MATERIAL_ROUGH_LIGHTING_ONLY].pSpecPower->SetInt(6);

    if (g_bWireFrame)
        g_pFillMode->SetInt(1);
    else
        g_pFillMode->SetInt(0);

    // Apply the technique to update state.
    pTechnique->GetPassByIndex(0)->Apply(0, pd3dImmediateContext);

    //Render
    g_Mesh11.Render( pd3dImmediateContext, 0, 1, INVALID_SAMPLER_SLOT);

    // Tell the UI items to render 
    DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" );
    g_HUD.OnRender( fElapsedTime );
    g_SampleUI.OnRender( fElapsedTime );
    RenderText();
    DXUT_EndPerfEvent();
}
Пример #2
0
void CALLBACK OnD3D11FrameRender(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime,
	float fElapsedTime, void* pUserContext)
{
	pd3dImmediateContext->ClearRenderTargetView(DXUTGetD3D11RenderTargetView(), Colors::Black);
	pd3dImmediateContext->ClearDepthStencilView(DXUTGetD3D11DepthStencilView(), D3D11_CLEAR_DEPTH, 1.0, 0);

	XMMATRIX mworld = g_camera.GetWorldMatrix();
	XMMATRIX mview = g_camera.GetViewMatrix();
	XMMATRIX mproj = g_camera.GetProjMatrix();

	g_BatchEffect->SetView(mview);
	g_BatchEffect->SetProjection(mproj);

	// Draw Grids
	const XMVECTORF32 xaxis = { 50.f, 0.f, 0.f };
	const XMVECTORF32 yaxis = { 0.f, 0.f, 50.f };
	DrawGrid(xaxis, yaxis, g_XMZero, 50, 50, Colors::Azure);
	

	// Draw Font
	g_dwtext->Render(L"га╥╧юс = ", DXUTGetFPS(), 10, 10);
	
	XMVECTOR worldPos;
	XMVECTOR worldDir;
	
	//g_input->getMousePosWorld(worldPos, worldDir, mview, mproj, TRUE);
	g_input->Pick(worldPos, worldDir, mworld, mview, mproj);


	/*
		POSITION
	*/
	WCHAR strformat[256] = {};
	XMFLOAT3 pos;
	XMStoreFloat3(&pos, worldPos);
	swprintf(strformat, L"pos = %f , %f , %f", pos.x, pos.y, pos.z);
	g_dwtext->Render(strformat, 0, 10, 30);


	/*
		DIRECTION
	*/
	XMFLOAT3 dir;
	XMStoreFloat3(&dir, worldDir);

	swprintf(strformat, L"dir = %f , %f , %f", dir.x, dir.y, dir.z);
	g_dwtext->Render(strformat, 0, 10, 50);


	/*
		CURSOR POSITION
	*/
	POINT pt = g_input->getMousePos();
	swprintf(strformat, L"cursor = %d , %d", pt.x, pt.y );
	g_dwtext->Render(strformat, 0, 10, 70);


	XMVECTOR eye = g_camera.GetEyePt();
	XMFLOAT3 feye;
	XMStoreFloat3(&feye, eye);
	swprintf(strformat, L"eye = %f , %f , %f", feye.x, feye.y, feye.z);
	g_dwtext->Render(strformat, 0, 10, 90);

	XMVECTOR lookat = g_camera.GetLookAtPt();
	XMFLOAT3 flookat;
	XMStoreFloat3(&flookat, lookat);
	swprintf(strformat, L"lookat = %f , %f , %f", flookat.x, flookat.y, flookat.z);
	g_dwtext->Render(strformat, 0, 10, 110);

	
	XMVECTOR axis1 = XMVectorSet( pos.x, pos.y, pos.z, 0.0f);
	XMVECTOR axis2 = XMVectorSet(dir.x, dir.y, dir.z, 0.0f);
	DrawCenterGrid(axis1, axis2);

	XMMATRIX wpos = XMMatrixTranslation(pos.x, pos.y, pos.z);
	g_ShapePos->Draw(wpos, mview, mproj, Colors::LawnGreen);

	XMMATRIX wdir = XMMatrixTranslation(dir.x, dir.y, dir.z);
	g_ShapeDir->Draw(wdir, mview, mproj, Colors::OrangeRed);
	
}