DX9 Update中的HDRLighting Sample分析 (2)
上一篇我们分析到得出了场景的平均亮度值。下面如果我们不打算做Star和Bloom效果的话,我们可以直接使用这个亮度值来进行我们最后的调整,获得最后的画面。但是如果HDR缺少了Star和Bloom或类似的效果的话,就如同太阳失去了光芒一样。不过在进行我们的Star和Bloom效果处理之前还有一个问题需要解决,这个问题和这些效果没什么关系。我们知道我们的眼睛当遇到强光的时候,并不是一下子就能适应,而是有个渐进的过程。如果我们的程序没有这个处理的话,将会失色很多。也就是说两个亮度值之间需要有个过渡的变化。我们如何做到这一点呢?
fNewAdaptation = fAdaptedLum + (fCurrentLum - fAdaptedLum) * ( 1 - pow( 0.98f, 30 * g_fElapsedTime ) );
上面的就是我们计算过渡亮度值的公式,fCurrentLum是我们的目标的亮度值,fAdaptedLum是上一桢的亮度值,我们在每次计算的时候,一般是保持fCurrentLum,上一次计算的fNewAdaptation作为这次计算的fAdaptedLum,这样我们就能做到逐步的接近我们的最终结果fCurrentLum。而我们把每桢计算得出的fNewAdaptation作为我们调整颜色用的亮度值。
让我们来具体的看看代码
if( m_bAdaptationInvalid )
{
// Clear the update flag
m_bAdaptationInvalid = false;
// Calculate the current luminance adaptation level
CalculateAdaptation();
}
这里的if( m_bAdaptationInvalid )并不起作用,因为在FrameMove()函数内都m_bAdaptationInvalid设置成了true.也就是说每桢都调用CalculateAdaptation();我们来看看着个函数是如何实现上面说得那样的计算:
HRESULT CMyD3DApplication::CalculateAdaptation()
{
HRESULT hr = S_OK;
UINT uiPass, uiPassCount;
// Swap current & last luminance
PDIRECT3DTEXTURE9 pTexSwap = m_pTexAdaptedLuminanceLast;
m_pTexAdaptedLuminanceLast = m_pTexAdaptedLuminanceCur;
m_pTexAdaptedLuminanceCur = pTexSwap;
这里可以看到每桢都交换了Cur和Last,将上桢的结果作为这一桢的输入参数
m_pEffect->SetTechnique("CalculateAdaptedLum");
m_pEffect->SetFloat("g_fElapsedTime", m_fElapsedTime);
m_pd3dDevice->SetRenderTarget(0, pSurfAdaptedLum);
m_pd3dDevice->SetTexture(0, m_pTexAdaptedLuminanceLast);
m_pd3dDevice->SetTexture(1, m_apTexToneMap[0]);
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_POINT );
这里m_apTexToneMap(0)就是我们的最终的目标亮度值,这里用到的shader的代码如下
float4 CalculateAdaptedLum(in float2 vScreenPosition : TEXCOORD0) : COLOR
{
float fAdaptedLum = tex2D(s0, float2(0.5f, 0.5f));
float fCurrentLum = tex2D(s1, float2(0.5f, 0.5f));
// The user's adapted luminance level is simulated by closing the gap between
// adapted luminance and current luminance by 2% every frame, based on a
// 30 fps rate. This is not an accurate model of human adaptation, which can
// take longer than half an hour.
float fNewAdaptation = fAdaptedLum + (fCurrentLum - fAdaptedLum) *
( 1 - pow( 0.98f, 30 * g_fElapsedTime ) );
return float4(fNewAdaptation, fNewAdaptation, fNewAdaptation, 1.0f);
}
这里就是使用上面说到的公式来计算我们当前的亮度值
}
通过上面的计算,我们在最后使用的亮度值保存在了m_pTexAdaptedLuminanceCur里面。
接下来我们就准备开始获得Star和Bloom效果了.首先我们需要将场景中特别亮的像素给提取出来。
SceneScaled_To_BrightPass();
这个函数就是获得我们Star和Bloom所需要的来源贴图.我们来看看着个函数的内部实现
HRESULT CMyD3DApplication::SceneScaled_To_BrightPass()
{
....
m_pEffect->SetTechnique("BrightPassFilter");
m_pd3dDevice->SetRenderTarget( 0, pSurfBrightPass );
m_pd3dDevice->SetTexture( 0, m_pTexSceneScaled );
m_pd3dDevice->SetTexture( 1, m_pTexAdaptedLuminanceCur );
m_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, TRUE );
m_pd3dDevice->SetScissorRect( &rectDest );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_POINT );
m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
我们使用刚刚计算得出的m_pTexAdaptedLuminanceCur(如果跳过模拟过渡的阶段,这里应该使用m_apTexToneMap(0))和m_pTexSceneScaled作为我们的参数,计算后的只剩下我们需要的亮度信息保存pSurfBrightPass,我们来看看这里用到的shader的操作
float4 BrightPassFilter(in float2 vScreenPosition : TEXCOORD0) : COLOR
{
float4 vSample = tex2D( s0, vScreenPosition );
float fAdaptedLum = tex2D( s1, float2(0.5f, 0.5f) );
// Determine what the pixel's value will be after tone-mapping occurs
vSample.rgb *= g_fMiddleGray/(fAdaptedLum + 0.001f);
// Subtract out dark pixels
vSample.rgb -= BRIGHT_PASS_THRESHOLD;
// Clamp to 0
vSample = max(vSample, 0.0f);
// Map the resulting value into the 0 to 1 range. Higher values for
// BRIGHT_PASS_OFFSET will isolate lights from illuminated scene
// objects.
vSample.rgb /= (BRIGHT_PASS_OFFSET+vSample);
return vSample;
}
这里通过-BRIGHT_PASS_THRESHOLD和Clamp to 0我们就得到了只剩下我们需要的像素亮度值得Texture了
}