Microsoft Visual Studio .NET 2003
 Microsoft DirectX 9.0 SDK (December 2004)
 シェーダーモデル 2.0

■ゴーストシェーダー Prev  Top  Next
関連ページ:ブラーフィルターその2

無料レンタルサーバー

今回はなんとなく思いついたネタをやります。ゴーストシェーダーです。幽霊ぽい感じですかね。一応オリジナルですが、まあ自分が知らないだけで同じことやってるゲームはあるではないでしょうか?


こんな感じです。輪郭がぼけているので、ぼかしたトラを加算合成してるだけにも見えますが、正面部分のテクスチャーがはっきりしています。ここがポイントです。

処理フローはこんな風になります。
1.背景をレンダリングする。
2.レンダーターゲットを切り替えてゴーストモデルをレンダリングする。ただしZバッファは1で使用したものをそのまま使用する。
3.2のレンダーターゲットサーフェイスにブラーを適応する。
4.2と3の結果を線形合成し、その結果とバックバッファのイメージとを加算合成する。

さてソースを見ていきます。

---GhostShader.fx---


float4x4 m_WVP;                 //ワールド × ビュー × 遠近射影
float4 m_EyePos;                //視点ベクトル

sampler tex0 : register(s0);    //Pass0:オブジェクトのテクスチャー、Pass1:ぼけてないゴーストモデルのレンダリングイメージ
sampler tex1 : register(s1);    //Pass1:ぼけたゴーストモデルのレンダリングイメージ

struct VS1_OUTPUT
{
   float4 Pos    : POSITION;
   float2 Tex    : TEXCOORD0;
   float3 N      : TEXCOORD1;
   float3 E      : TEXCOORD3;
};

VS1_OUTPUT VS1( float4 Pos    : POSITION,
                float4 Normal : NORMAL,
                float2 Tex    : TEXCOORD0 )
{
   VS1_OUTPUT Out;
   
   Out.Pos = mul( Pos, m_WVP );
   Out.Tex = Tex; 
   
   //オブジェクトの法線ベクトルを正規化する
   Out.N = normalize( Normal.xyz );
   
   //頂点 -> 視点 へのベクトルを計算
   Out.E = m_EyePos.xyz - Pos.xyz;
   
  return Out;
}

float4 PS1( VS1_OUTPUT In ) : COLOR0
{ 
   float4 Out;
   float3 Normal = normalize( In.N );
   float3 Eye    = normalize( In.E );
   
   Out = tex2D( tex0, In.Tex );
   
   //アルファ成分に視線と法線ベクトルの内積を格納する
   Out.a = dot( Normal, Eye );

   return Out;
}

struct VS2_OUTPUT
{
   float4 Pos    : POSITION;
   float2 Tex    : TEXCOORD0;
};

VS2_OUTPUT VS2( float4 Pos    : POSITION,
                float4 Normal : NORMAL,
                float2 Tex    : TEXCOORD0 )
{
   VS2_OUTPUT Out;

   Out.Pos = mul( Pos, m_WVP );
   Out.Tex = Tex;

  return Out;
}

float4 PS2( VS2_OUTPUT In ) : COLOR0
{ 
   //ぼけてないゴーストモデルのレンダリングイメージ
   float4 Out1 = tex2D( tex0, In.Tex );

   //ぼけたゴーストモデルのレンダリングイメージ
   float4 Out2 = tex2D( tex1, In.Tex );

   //視線と法線ベクトルの内積を参照し、ぼけたイメージとぼけてないイメージを線形合成して出力する。
   //基本的にモデルの輪郭付近がぼけるようになる。
   return lerp( Out2, Out1, Out1.a );
}

technique TShader
{
   pass P0
   {
      VertexShader = compile vs_1_1 VS1();
      PixelShader  = compile ps_2_0 PS1();
   }
   
   pass P1
   {
      VertexShader = compile vs_1_1 VS2();
      PixelShader  = compile ps_2_0 PS2();
   }
}

ゴーストシェーダーです。パス0でゴーストモデルをレンダリングします。ただし、アルファ成分に視線と法線ベクトルの角度を格納します。 パス1で行う処理は、パス0でレンダリングしたイメージとそれをぼかしたイメージとを線形合成して出力することです。モデルの輪郭付近はぼけたイメージとなるように合成するので最終的に 輪郭がはっきりしないイメージになります。

---GhostShader.h---


//D3D2DSQUAREは2Dオブジェクト。詳細は表面化散乱(Subsurface Scattering) を参照
class GHOST_SHADER : public D3D2DSQUARE

{
private:
   LPD3DXEFFECT m_pEffect;
   D3DXHANDLE m_pTechnique, m_pWVP, m_pEyePos, m_pAmbient;
   D3DXMATRIX m_matView, m_matProj;
   LPDIRECT3DDEVICE9 m_pd3dDevice;
   D3DPRESENT_PARAMETERS* m_pd3dParameters;

public:
   GHOST_SHADER( LPDIRECT3DDEVICE9 pd3dDevice, D3DPRESENT_PARAMETERS* pd3dParameters );
   ~GHOST_SHADER();
   void Invalidate();
   void Restore();
   HRESULT Load();
   void Step1Begin();
   void Step1BeginPass();
   void Step1SetAmbient( float Ambient );
   void Step1SetAmbient( D3DXVECTOR4* pAmbient );
   void Step1SetMatrix( D3DXMATRIX* pMatWorld, D3DXVECTOR4* pCameraPos );
   void Step1EndPass();
   void Step1End();

   void Step2Render( LPDIRECT3DTEXTURE9 pNoneBlurTexture, LPDIRECT3DTEXTURE9 pBlurTexture );

   void CommitChanges();
   BOOL IsOK();
   LPD3DXEFFECT GetEffect(){ return m_pEffect; };
};

---GhostShader.cpp---


GHOST_SHADER::GHOST_SHADER( LPDIRECT3DDEVICE9 pd3dDevice, D3DPRESENT_PARAMETERS* pd3dParameters ) : D3D2DSQUARE( pd3dDevice, pd3dParameters )
{
   m_pd3dDevice = pd3dDevice;
   m_pd3dParameters = pd3dParameters;
   m_pEffect = NULL;
}

GHOST_SHADER::~GHOST_SHADER()
{
   SafeRelease( m_pEffect );
}

void GHOST_SHADER::Invalidate()
{
   if( m_pEffect )
      m_pEffect->OnLostDevice();
}

void GHOST_SHADER::Restore()
{
   if( m_pEffect )
      m_pEffect->OnResetDevice();
}

HRESULT GHOST_SHADER::Load()
{
   D3DCAPS9 caps;
   HRESULT hr;

   m_pd3dDevice->GetDeviceCaps( &caps );
   if( caps.VertexShaderVersion >= D3DVS_VERSION( 1, 1 ) && caps.PixelShaderVersion >= D3DPS_VERSION( 2, 0 ) )
   {
      hr = D3D2DSQUARE::Load();
      if( FAILED( hr ) )
         return -1;

      //シェーダーの初期化
      LPD3DXBUFFER pErr = NULL;
      hr = D3DXCreateEffectFromFile( m_pd3dDevice, _T("GhostShader.fx"), NULL, NULL, 0, NULL, &m_pEffect, &pErr );
      if( FAILED( hr ) )
         return -2;

      m_pTechnique = m_pEffect->GetTechniqueByName( "TShader" );
      m_pWVP       = m_pEffect->GetParameterByName( NULL, "m_WVP" );
      m_pEyePos    = m_pEffect->GetParameterByName( NULL, "m_EyePos" );
      m_pAmbient   = m_pEffect->GetParameterByName( NULL, "m_Ambient" );

      m_pEffect->SetTechnique( m_pTechnique );   
   }

   else
   {
      return -3;
   }

   return S_OK;
}

void GHOST_SHADER::Step1Begin()
{
   if(  m_pEffect )
   {
      m_pd3dDevice->GetTransform( D3DTS_VIEW, &m_matView );
      m_pd3dDevice->GetTransform( D3DTS_PROJECTION, &m_matProj );
      m_pEffect->Begin( NULL, 0 );
   }
}

void GHOST_SHADER::Step1BeginPass()
{
   if( m_pEffect )
   {
      m_pEffect->BeginPass( 0 );
   }
}

void GHOST_SHADER::Step1SetAmbient( float Ambient )
{
   if( m_pEffect )
   {
      D3DXVECTOR4 A;
      A = D3DXVECTOR4( Ambient, Ambient, Ambient, 1.0f );
      m_pEffect->SetVector( m_pAmbient, &A );
   }

   else
   {
      D3DMATERIAL9 old_material;
      m_pd3dDevice->GetMaterial( &old_material );
      old_material.Ambient.r = Ambient;
      old_material.Ambient.g = Ambient;
      old_material.Ambient.b = Ambient;
      old_material.Ambient.a = 1.0f;
      m_pd3dDevice->SetMaterial( &old_material );
   }
}

void GHOST_SHADER::Step1SetAmbient( D3DXVECTOR4* pAmbient )
{
   if( m_pEffect )
      m_pEffect->SetVector( m_pAmbient, pAmbient );

   else
   {
      D3DMATERIAL9 old_material;
      m_pd3dDevice->GetMaterial( &old_material );
      old_material.Ambient.r = pAmbient->x;
      old_material.Ambient.g = pAmbient->y;
      old_material.Ambient.b = pAmbient->z;
      old_material.Ambient.a = pAmbient->w;
      m_pd3dDevice->SetMaterial( &old_material );
   }
}

//ローカル座標系
void GHOST_SHADER::Step1SetMatrix( D3DXMATRIX* pMatWorld, D3DXVECTOR4* pCameraPos )
{
   if( m_pEffect )
   {
      D3DXMATRIX m;
     D3DXVECTOR4 v;

      m = (*pMatWorld) * m_matView * m_matProj;
      m_pEffect->SetMatrix( m_pWVP, &m );

      //カメラ位置
      m = (*pMatWorld) * m_matView;
      D3DXMatrixInverse( &m, NULL, &m );
      D3DXVec4Transform( &v, pCameraPos, &m );
      m_pEffect->SetVector( m_pEyePos, &v );
   }

   else
      m_pd3dDevice->SetTransform( D3DTS_WORLD, pMatWorld );
}

void GHOST_SHADER::Step1EndPass()
{
   if( m_pEffect )
   {
      m_pEffect->EndPass();
   }
}

void GHOST_SHADER::Step1End()
{
   if( m_pEffect )
   {
      m_pEffect->End();
   }
}

void GHOST_SHADER::Step2Render( LPDIRECT3DTEXTURE9 pNoneBlurTexture, LPDIRECT3DTEXTURE9 pBlurTexture )
{
   if( m_pEffect )
   {
      m_pd3dDevice->SetTexture( 0, pNoneBlurTexture );
      m_pd3dDevice->SetTexture( 1, pBlurTexture );

      m_pEffect->Begin( NULL, 0 );
      m_pEffect->BeginPass( 1 );

     D3D2DSQUARE::Render();

     m_pEffect->EndPass();
      m_pEffect->End();

      m_pd3dDevice->SetTexture( 1, NULL );
   }
}

void GHOST_SHADER::CommitChanges()
{
   if( m_pEffect )
      m_pEffect->CommitChanges();
}

BOOL GHOST_SHADER::IsOK()
{
   if( m_pEffect )
      return TRUE;

   return FALSE;
}

ゴーストシェーダークラスです。

---Main.cpp---


LPDIRECT3D9 m_pdirect3d9 = NULL;
LPDIRECT3DDEVICE9 m_pd3dDevice = NULL;
D3DPRESENT_PARAMETERS m_d3dParameters;

D3DCAPS9 Caps;

//シーンのメッシュ
//DirectX SDK(December 2004) に添付されているDXUTMesh.cppファイルにあるヘルパークラス群
CDXUTMesh* m_pMeshBack = NULL;
CDXUTMesh* m_pMeshSun = NULL;
CDXUTMesh* m_pMeshTiger = NULL;

//2Dオブジェクト(表面化散乱(Subsurface Scattering) ページ参照)
D3D2DSQUARE* m_pSquObj = NULL;

//ランバート拡散照明クラスの宣言
LAMBERT1* m_pLambert = NULL;

//ゴーストシェーダークラスの宣言
GHOST_SHADER* m_pGhostShader = NULL;

//ブラーフィルタークラスの宣言
BLURFILTER2* m_pBlurFilter = NULL;

//スクリーンの解像度
UINT nWidth = 1024;
UINT nHeight = 768;

//太陽の位置ベクトル
//光源の位置はカメラの視線方向にある
D3DXVECTOR4 LightPos = D3DXVECTOR4( 0.0f, 40.0f, -70.0f, 1.0f );

//平行光源の光の方向ベクトル
D3DXVECTOR4 LightDir;

//視点の位置ベクトル
D3DXVECTOR4 EyePos   = D3DXVECTOR4( 0.0f, 0.0f, 0.0f, 1.0f );

bool RenderOK = false;

int APIENTRY WinMain( HINSTANCE hInstance,
                      HINSTANCE /*hPrevInstance*/,
                      LPSTR     /*lpCmpLine*/,
                      INT       /*nCmdShow*/)
{  
   char* AppName = "Tutrial";

   MSG msg;
   ZeroMemory(&msg, sizeof(MSG));
   HWND hWnd = NULL;

   WNDCLASSEX wc;
   wc.cbSize        = sizeof(WNDCLASSEX);
   wc.style         = CS_VREDRAW | CS_HREDRAW;
   wc.lpfnWndProc   = (WNDPROC)WndProc;
   wc.cbClsExtra    = 0;
   wc.cbWndExtra    = sizeof(DWORD);
   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wc.hIcon         = NULL;
   wc.hIconSm       = NULL;
   wc.lpszMenuName  = NULL;
   wc.lpszClassName = AppName;
   wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
   wc.hInstance     = hInstance;
   ::RegisterClassEx(&wc);


   //****************************************************************
   //ここでウィンドウの作成処理
   //****************************************************************


   //****************************************************************
   //ここでDirect3Dの初期化を行う。
   //****************************************************************

   m_pd3dDevice->GetDeviceCaps(&Caps);

   //ランバート拡散照明クラスの初期化
   m_pLambert = new LAMBERT1( m_pd3dDevice );
   m_pLambert->Load();

   //ゴーストシェーダークラスの初期化
   m_pGhostShader = new GHOST_SHADER( m_pd3dDevice, &m_d3dParameters );
   m_pGhostShader->Load();

   //ブラーフィルタークラスの初期化
   m_pBlurFilter = new BLURFILTER2( m_pd3dDevice, nWidth / 4, nHeight / 4 );
   m_pBlurFilter->Load();

   //2Dオブジェクトのロード
   m_pSquObj = new D3D2DSQUARE( m_pd3dDevice, &m_d3dParameters );
   m_pSquObj->Load();

   //メッシュのロード
   //背景
   m_pMeshBack = new CDXUTMesh();
   m_pMeshBack->Create( m_pd3dDevice, _T("res\\back.x") );
   m_pMeshBack->SetFVF( m_pd3dDevice, D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );

   //太陽
   m_pMeshSun = new CDXUTMesh();
   m_pMeshSun->Create( m_pd3dDevice, _T("res\\sun.x") );
   m_pMeshSun->SetFVF( m_pd3dDevice, D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );

   //トラ
   m_pMeshTiger = new CDXUTMesh();
   m_pMeshTiger->Create( m_pd3dDevice, _T("res\\tiger.x") );
   m_pMeshTiger->SetFVF( m_pd3dDevice, D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );


   //平行光源の位置ベクトルから方向ベクトルを計算する
   LightDir = D3DXVECTOR4( -LightPos.x, -LightPos.y, -LightPos.z, 0.0f );
   D3DXVec3Normalize( (D3DXVECTOR3*)&LightDir, (D3DXVECTOR3*)&LightDir );

   RenderOK = true;

   //デバイス消失後にリストアする必要があるオブジェクトの初期化
   Restore();

   ::ShowWindow(hWnd, SW_SHOW);
   ::UpdateWindow(hWnd);

   do
   { 
      if( ::PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
      {
         ::TranslateMessage(&msg); 
         ::DispatchMessage(&msg); 
      }
      else
      {
         if( MainLoop(hWnd) == FALSE )
            ::DestroyWindow( hWnd );
      }
   }while( msg.message != WM_QUIT );

   ::UnregisterClass( AppName, hInstance );

   return msg.wParam;
}

//デバイスのリセット前に開放すべきオブジェクト
void Invalidate()
{
   m_pLambert->Invalidate();
   m_pGhostShader->Invalidate();
   m_pBlurFilter->Invalidate();

   for( int i=0; i<2; i++ )
   {
      SafeRelease( m_pBlurSurface[i] );
      SafeRelease( m_pBlurTexture[i] );
   }
   SafeRelease( m_pColorSurface );
   SafeRelease( m_pColorTexture );
}

//デバイスのリセット後に初期化すべきオブジェクト
void Restore()
{
   m_pLambert->Restore();
   m_pGhostShader->Restore();
   m_pBlurFilter->Restore();

   //シーンのレンダリング結果を格納するサーフェイス
   m_pd3dDevice->CreateTexture( nWidth,
                                nHeight,
                                1,
                                D3DUSAGE_RENDERTARGET,
                                D3DFMT_A8R8G8B8,
                                D3DPOOL_DEFAULT,
                                &m_pColorTexture,
                                NULL );
   m_pColorTexture->GetSurfaceLevel( 0, &m_pColorSurface );

   //ぼかすためのサーフェイス
   for( int i=0; i<2; i++ )
   {
      m_pd3dDevice->CreateTexture( m_pBlurFilter->GetWidth(),
                                   m_pBlurFilter->GetHeight(),
                                   1,
                                   D3DUSAGE_RENDERTARGET,
                                   D3DFMT_A8R8G8B8,
                                   D3DPOOL_DEFAULT,
                                   &m_pBlurTexture[i],
                                   NULL );
      m_pBlurTexture[i]->GetSurfaceLevel( 0, &m_pBlurSurface[i] );
   }


   //固定機能パイプラインライティングを設定する
   D3DLIGHT9 Light;
   ZeroMemory(&Light, sizeof(D3DLIGHT9));
   Light.Type = D3DLIGHT_DIRECTIONAL;
   Light.Direction = D3DXVECTOR3( LightDir.x, LightDir.y, LightDir.z );
   Light.Ambient   = D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f );
   Light.Diffuse   = D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f );
   Light.Specular  = D3DXCOLOR( 0.0f, 0.0f, 0.0f, 0.0f );
   m_pd3dDevice->SetLight(0, &Light);
   m_pd3dDevice->LightEnable(0, TRUE);

   D3DMATERIAL9 Material;
   ZeroMemory( &Material, sizeof( Material ) );
   Material.Diffuse.r = 1.0f;
   Material.Diffuse.g = 1.0f;
   Material.Diffuse.b = 1.0f;
   Material.Diffuse.a = 1.0f;
   m_pd3dDevice->SetMaterial( &Material );
}

//メッセージループからコールされる関数
BOOL MainLoop( HWND HWnd )
{
   HRESULT hr;
   
   //レンダリング不可能
   if( RenderOK == false )
   {
      hr = m_pd3dDevice->TestCooperativeLevel();
      switch( hr )
      {
      //デバイスは消失しているがReset可能
      case D3DERR_DEVICENOTRESET:

         //開放
         Invalidate();

         //デバイスをリセットする
         hr = m_pd3dDevice->Reset( &m_d3dParameters );
         
         switch( hr )
         {
         //デバイスロスト
         case D3DERR_DEVICELOST:
            break;

         //内部ドライバーエラー
         case D3DERR_DRIVERINTERNALERROR:
            return FALSE;
            break;

         //メソッドの呼び出しが無効です
         case D3DERR_INVALIDCALL:
            return FALSE;
            break;

         case S_OK:

            //初期化
            Restore();

            RenderOK = true;
         }
         break;
      }
   }

   //レンダリング可能
   else
   {
      D3DXMATRIX matPProj, matView, matWorld, matTiger, matScaling, matTranslation, matRotation;

      //遠近射影座標変換
      //クリップ面はアプリケーションごとに調整すること
      D3DXMatrixPerspectiveFovLH( &matPProj,
                                  D3DX_PI/4.0f,
                                  4.0f / 3.0f,
                                  30.0f,
                                  700.0f );
      m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matPProj );


      //ビュー座標変換
      D3DXMatrixIdentity( &matView );
      m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

      //トラ
      D3DXMatrixRotationY( &matRotation, D3DXToRadian( 45.0f ) );
      D3DXMatrixScaling( &matScaling, 50.0f, 50.0f, 50.0f );
      D3DXMatrixTranslation( &matTranslation, 0.0f, 90.0f, 400.0f );
      matTiger = matScaling * matRotation * matTranslation;

      m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
      m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
      m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_NONE );

      m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
      m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
      m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MIPFILTER, D3DTEXF_NONE );
      
      m_pd3dDevice->BeginScene();

      //****************************************************************
      //ステップ1 : 背景をレンダリングする
      //****************************************************************

      //背景レンダリング
      m_pLambert->Begin();
      D3DXMatrixIdentity( &matWorld );
      m_pLambert->SetMatrix( &matWorld, &LightDir );
      m_pd3dDevice->SetTexture( 0, m_pMeshBack->m_pTextures[0] );
      m_pLambert->SetAmbient( 0.1f );
      m_pLambert->BeginPass();
      m_pMeshBack->m_pLocalMesh->DrawSubset( 0 );
      m_pLambert->EndPass();

      //空レンダリング
      //Zバッファ書込み禁止
      m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
      m_pd3dDevice->SetTexture( 0, m_pMeshBack->m_pTextures[1] );
      D3DXMatrixIdentity( &matWorld );
      m_pLambert->SetMatrix( &matWorld, &LightDir );
      m_pLambert->SetAmbient( 1.0f );
      m_pLambert->BeginPass();
      m_pMeshBack->m_pLocalMesh->DrawSubset( 1 );
      m_pLambert->EndPass();

      //太陽レンダリング
      //αブレンドを有効にする
      m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
      //加算合成
      m_pd3dDevice->SetRenderState( D3DRS_BLENDOP,   D3DBLENDOP_ADD );
      m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
      m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );

      m_pd3dDevice->SetTexture( 0, m_pMeshSun->m_pTextures[0] );      

      D3DXMatrixTranslation( &matTranslation, LightPos.x, LightPos.y, LightPos.z );
      //ビルボードマトリックスを取得(ビルボード ページ参照)
      matWorld = GetBillBoardMatrix( m_pd3dDevice, &matTranslation );
      m_pLambert->SetMatrix( &matWorld, &LightDir );
      m_pLambert->SetAmbient( 1.0f );
      m_pLambert->BeginPass();
      m_pMeshSun->m_pLocalMesh->DrawSubset( 0 );
      m_pLambert->EndPass();
      m_pLambert->End();
      m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
      m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );


      //****************************************************************
      //ステップ2 : ゴーストモデルをレンダリング
      //****************************************************************

      //レンダーターゲットサーフェイスを切り替える
      LPDIRECT3DSURFACE9 OldSurface = NULL;
      m_pd3dDevice->GetRenderTarget( 0, &OldSurface );
      m_pd3dDevice->SetRenderTarget( 0, m_pColorSurface );
      
      //Zバッファはクリアしない
      m_pd3dDevice->Clear( 0L,
                           NULL,
                           D3DCLEAR_TARGET,
                           0x0,
                           1.0f,
                           0L
                         );

      //トラ
      m_pGhostShader->Step1Begin();
      m_pGhostShader->Step1SetMatrix( &matTiger, &EyePos );
      m_pd3dDevice->SetTexture( 0, m_pMeshTiger->m_pTextures[0] );
      m_pGhostShader->Step1BeginPass();
      m_pMeshTiger->m_pLocalMesh->DrawSubset( 0 );
      m_pGhostShader->Step1EndPass();
      m_pGhostShader->Step1End();


      //****************************************************************
      //ステップ3 : ブラーを適応する
      //****************************************************************

      //ハーフサイズのサーフェイスにレンダリングするのでビューポートもハーフサイズに変更する
      D3DSURFACE_DESC desc;
      D3DVIEWPORT9 OldViewport, NewViewport;

      m_pBlurSurface[0]->GetDesc( &desc );
      m_pd3dDevice->GetViewport( &OldViewport );
      NewViewport.X = 0;
      NewViewport.Y = 0;
      NewViewport.Width = desc.Width;
      NewViewport.Height = desc.Height;
      NewViewport.MinZ = 0.0f;
      NewViewport.MaxZ = 1.0f;
      m_pd3dDevice->SetViewport( &NewViewport );

      m_pd3dDevice->SetRenderTarget( 0, m_pBlurSurface[0] );
      m_pd3dDevice->SetTexture( 0, m_pColorTexture );
      m_pBlurFilter->Render(0);

      m_pd3dDevice->SetRenderTarget( 0, m_pBlurSurface[1] );
      m_pd3dDevice->SetTexture( 0, m_pBlurTexture[0] );
      m_pBlurFilter->Render(1);

      //ビューポートを戻す
      m_pd3dDevice->SetViewport( &OldViewport );

      //レンダーターゲットサーフェイスをバックバッファに戻す
      m_pd3dDevice->SetRenderTarget( 0, OldSurface );
      SafeRelease( OldSurface );


      //****************************************************************
      //ステップ4 : 合成する
      //****************************************************************

      m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE );
      m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );

      //ぼかしたイメージを加算合成
      m_pd3dDevice->SetRenderState( D3DRS_BLENDOP,   D3DBLENDOP_ADD );
      m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
      m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );

      m_pGhostShader->Step2Render( m_pColorTexture, m_pBlurTexture[1] );

      m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );
      m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );

      m_pd3dDevice->EndScene();

      hr = m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
      
      //デバイスロストのチェック
      switch( hr )
      {
      //デバイスロスト
      case D3DERR_DEVICELOST:
         RenderOK = false;
         break;

      //内部ドライバーエラー
      case D3DERR_DRIVERINTERNALERROR:
         return FALSE;
         break;

      //メソッドの呼び出しが無効です
      case D3DERR_INVALIDCALL:
         return FALSE;
         break;
      }
   }

   return TRUE;
}

以上です。

今年最後の更新でした。でも公開は来年です(笑)。さて来年は影をいいかげんやらないとなあ。

Prev  Top  Next
inserted by FC2 system