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

■ナイトスコープシェーダー Prev  Top  Next
関連ページ:ランバート拡散照明

無料レンタルサーバー

今回も簡単なやつでごまかし。ナイトスコープシェーダーというものをやります。 元ネタもまたまた前回と同じく「GAME Watch」のネタです。

ナイトスコープは暗闇でも周囲の情景が見えるようにするための双眼鏡みたいなやつです。光量を増幅することにより暗闇でも見えるようにします。 したがって完全な暗闇の場合は暗闇のままとなります。今回のサンプルでもそのようになります。

 
左側が普通にレンダリングしたサンプルです。平行光源を暗めに設定しています。完全な暗闇ではないですがほとんど何も見えません。 右側がナイトスコープシェーダーを使用したサンプルです。トラがいるのがわかります。ちなみに「GAME Watch」で紹介されている「Splinter Cell」ではグレー色になっています。 でもネットでナイトスコープを調べてみましたが上のサンプルのような色になるようなのでこういう色にしました。また黒い粒粒のノイズを入れています。ですが実際には ノイズはかからないようです(多少はかかるのかな)。ただ「Splinter Cell」でもノイズかけてますが、かけたほうがナイトスコープで見ているイメージっぽい気がするのでノイズをかけました。

さて実装方法ですが、たいして難しくないのでいきなりソースをみていきます。

---Lambert19.fx---


float4x4 m_WVP;           //ワールド × ビュー × 射影行列
float4 m_LightColor;      //平行光源の色
float4 m_LightDir;        //平行光源の方向ベクトル
float4 m_Ambient = 0.0f;  //環境光

//オブジェクトのテクスチャー
sampler tex0 : register(s0);

struct VS_OUTPUT
{
   float4 Pos   : POSITION;
   float4 Col   : COLOR0;
   float2 Tex   : TEXCOORD0;
};
VS_OUTPUT VS( float4 Pos     : POSITION,
              float4 Normal  : NORMAL,
              float2 Tex     : TEXCOORD0 )
{
   VS_OUTPUT Out;
   
   Out.Pos    = mul( Pos, m_WVP );
   Out.Tex    = Tex;
   
   float3 L = -m_LightDir.xyz;   
   float3 N = normalize( Normal.xyz );
   Out.Col = max( m_Ambient, dot(N, L) * m_LightColor );
    
   return Out;
}

float4 PS( VS_OUTPUT In ) : COLOR0
{  
   float4 Out;
   
   Out = In.Col * tex2D( tex0, In.Tex );
      
   return Out;
}

technique TShader
{
   pass P0
   {
      VertexShader = compile vs_1_1 VS();
      PixelShader  = compile ps_1_1 PS();   
   }     
}

ランバート拡散照明です。Lambert1.fxとほとんど同じで、平行光源の色パラメータを追加しています。あと自己発光パラメータは使わないので削除しました。まあLambert1.fxを修正してもいいです。

---Lambert.h---


class LAMBERT19
{
private:
   LPD3DXEFFECT m_pEffect;
   D3DXHANDLE m_pTechnique, m_pWVP, m_pLightColor, m_pLightDir, m_pAmbient;
   D3DXMATRIX m_matView, m_matProj;
   LPDIRECT3DDEVICE9 m_pd3dDevice;

public:
   LAMBERT19( LPDIRECT3DDEVICE9 pd3dDevice );
   ~LAMBERT19();
   void Invalidate();
   void Restore();
   HRESULT Load();
   void Begin();
   void BeginPass( UINT Pass = 0 );
   void SetAmbient( float Ambient );
   void SetAmbient( D3DXVECTOR4* pAmbient );
   void SetMatrix( D3DXMATRIX* pMatWorld, D3DXVECTOR4* pLightColor, D3DXVECTOR4* pLightDir );
   void CommitChanges();
   void EndPass();
   void End();
   BOOL IsOK();
   LPD3DXEFFECT GetEffect(){ return m_pEffect; };
};

---Lambert.cpp---


LAMBERT19::LAMBERT19( LPDIRECT3DDEVICE9 pd3dDevice )
{
   m_pd3dDevice = pd3dDevice;
   m_pEffect = NULL;
}

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

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

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

HRESULT LAMBERT19::Load()
{
   D3DCAPS9 caps;

   m_pd3dDevice->GetDeviceCaps( &caps );
   if( caps.VertexShaderVersion >= D3DVS_VERSION( 1, 1 ) && caps.PixelShaderVersion >= D3DPS_VERSION( 1, 1 ) )
   {
      LPD3DXBUFFER pErr = NULL;
      HRESULT hr = D3DXCreateEffectFromFile( m_pd3dDevice, _T("..\\file\\shader\\Lambert19.fx"), NULL, NULL, 0, NULL, &m_pEffect, &pErr );
      if( SUCCEEDED( hr ) )
      {
         m_pTechnique  = m_pEffect->GetTechniqueByName( "TShader" );
         m_pWVP        = m_pEffect->GetParameterByName( NULL, "m_WVP" );
         m_pLightColor = m_pEffect->GetParameterByName( NULL, "m_LightColor" );
         m_pLightDir   = m_pEffect->GetParameterByName( NULL, "m_LightDir" );
         m_pAmbient    = m_pEffect->GetParameterByName( NULL, "m_Ambient" );

         m_pEffect->SetTechnique( m_pTechnique );   
      }

      else
      {
         return -1;
      }
   }

   else
   {
      return -2;
   }

   return S_OK;
}

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

void LAMBERT19::BeginPass( UINT Pass )
{
   if( m_pEffect )
   {
      m_pEffect->BeginPass( Pass );      
   }
}

void LAMBERT19::SetAmbient( 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 LAMBERT19::SetAmbient( 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 LAMBERT19::SetMatrix( D3DXMATRIX* pMatWorld, D3DXVECTOR4* pLightColor, D3DXVECTOR4* pLightDir )
{
   if( m_pEffect )
   {
      D3DXMATRIX m, m1;
      D3DXVECTOR4 v;

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

      //平行光源の色
      m_pEffect->SetVector( m_pLightColor, pLightColor );

      //平行光源の方向ベクトル
      D3DXMatrixInverse( &m1, NULL, pMatWorld );
      D3DXVec4Transform( &v, pLightDir, &m1 );
      //XYZ成分について正規化する
      D3DXVec3Normalize( (D3DXVECTOR3*)&v, (D3DXVECTOR3*)&v );
      m_pEffect->SetVector( m_pLightDir, &v );
   }

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

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

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

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

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

   return FALSE;
}

平行光源の色パラメータを追加したランバート拡散照明クラスです。

---NightScopeShader.fx---


float m_Parameter;             //明度の増幅率
sampler tex0 : register(s0);   //シーンのレンダリングイメージ
sampler tex1 : register(s1);   //ノイズテクスチャー

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

VS_OUTPUT VS( float4 Pos    : POSITION,
              float2 Tex    : TEXCOORD0 )
{
   VS_OUTPUT Out;
   
   Out.Pos = Pos;
   Out.Tex = Tex;
   
   return Out;
}

float4 PS( VS_OUTPUT In ) : COLOR0
{
   float4 Col = tex2D( tex0, In.Tex );
   
   //3原色の平均値
   float a = ( Col.r + Col.g + Col.b ) * 0.3333f;
   
   //シーンの色を増幅し、ナイトスコープっぽい色に調整する
   Col.rgb = ( a * m_Parameter ) * float3( 0.5f, 0.8f, 0.2 );
   
   //ノイズの適応
   Col.rgb *= tex2D( tex1, In.Tex ).rgb;
   
   return Col;
}

technique TShader
{
   pass P0
   {
      VertexShader = compile vs_1_1 VS();
      PixelShader  = compile ps_2_0 PS();
   }
}


ナイトスコープシェーダーです。

---NightScopeShader.h---


//D3D2DSQUAREはスクリーン全体をおおう2Dオブジェクト(表面化散乱(Subsurface Scattering) ページ参照)
class NIGHT_SCOPE_SHADER : public D3D2DSQUARE
{
private:
   LPD3DXEFFECT m_pEffect;
   D3DXHANDLE m_pTechnique, m_pParameter;
   LPDIRECT3DDEVICE9 m_pd3dDevice;

public:
   NIGHT_SCOPE_SHADER( LPDIRECT3DDEVICE9 pd3dDevice,  D3DPRESENT_PARAMETERS* pd3dParameters );
   ~NIGHT_SCOPE_SHADER();
   void Invalidate();
   void Restore();
   HRESULT Load();
   void SetParameter( float Parameter );
   void Render();
   BOOL IsOK();
   LPD3DXEFFECT GetEffect(){ return m_pEffect; };
};

---NightScopeShader.cpp---


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

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

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

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

HRESULT NIGHT_SCOPE_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("NightScopeShader.fx"), NULL, NULL, 0, NULL, &m_pEffect, &pErr );
      if( SUCCEEDED( hr ) )
      {
         m_pTechnique  = m_pEffect->GetTechniqueByName( "TShader" );
         m_pParameter  = m_pEffect->GetParameterByName( NULL, "m_Parameter" );
         m_pEffect->SetTechnique( m_pTechnique );
      }

      else
      {
         return -2;
      }
   }

   else
   {
      return -3;
   }

   return S_OK;
}

void NIGHT_SCOPE_SHADER::SetParameter( float Parameter )
{
   if( m_pEffect )
   {
      m_pEffect->SetFloat( m_pParameter, Parameter );
   }
}

void NIGHT_SCOPE_SHADER::Render()
{
   if( m_pEffect )
   {
      m_pEffect->Begin( NULL, 0 );
      m_pEffect->BeginPass( 0 );

      D3D2DSQUARE::Render();

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


BOOL NIGHT_SCOPE_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;

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

//ナイトスコープシェーダークラスの宣言
NIGHT_SCOPE_SHADER* m_pNightScopeShader = NULL;

//シーンのレンダリングイメージ用サーフェイス
LPDIRECT3DTEXTURE9    m_pColorTexture = NULL;
LPDIRECT3DSURFACE9    m_pColorSurface = NULL;

//ノイズテクスチャー
LPDIRECT3DTEXTURE9    m_pNoiseTexture = NULL;

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

//太陽の位置ベクトル
//光源の位置はカメラの視線方向にある
D3DXVECTOR4 LightPos = D3DXVECTOR4( 100.0f, 100.0f, -700.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_pLambert19 = new LAMBERT19( m_pd3dDevice );
   m_pLambert19->Load();

   //ナイトスコープクラスの初期化
   m_pNightScopeShader = new NIGHT_SCOPE_SHADER( m_pd3dDevice, &m_d3dParameters );
   m_pNightScopeShader->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 );

   //ノイズテクスチャーの初期化
   D3DXCreateTextureFromFileEx( m_pd3dDevice,
                                _T("Noise.bmp"),          //ファイル名
                                D3DX_DEFAULT,             //幅
                                D3DX_DEFAULT,             //高さ
                                1,                        //ミップマップレベル
                                0,                        //テクスチャーの使用目的
                                D3DFMT_UNKNOWN,           //フォーマット
                                D3DPOOL_MANAGED,          //メモリ管理方法
                                D3DX_DEFAULT,             //フィルタリング方法
                                D3DX_DEFAULT,             //フィルタリング方法
                                0x0,                      //カラーキー
                                NULL,                     //ファイルの情報を格納する構造体
                                NULL,                     //256色パレットを示す構造体
                                &m_pNoiseTexture );       //IDirect3DTexture9 インターフェイス


   //平行光源の位置ベクトルから方向ベクトルを計算する
   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_pLambert19->Invalidate();
   m_pNightScopeShader->Invalidate();

   SafeRelease( m_pColorSurface );
   SafeRelease( m_pColorTexture );
}

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

   m_pd3dDevice->CreateTexture( nWidth,
                                nHeight,
                                1,
                                D3DUSAGE_RENDERTARGET,
                                D3DFMT_A8R8G8B8,
                                D3DPOOL_DEFAULT,
                                &m_pColorTexture,
                                NULL );
   m_pColorTexture->GetSurfaceLevel( 0, &m_pColorSurface );

   //固定機能パイプラインライティングを設定する
   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 matWVP, matPProj, matView, matWorld, matRotation, matScaling, matTranslation, matTiger;

      //遠近射影座標変換
      //クリップ面はアプリケーションごとに調整すること
      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, 80.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 : シーンのレンダリング
      //****************************************************************

      LPDIRECT3DSURFACE9 OldSurface = NULL;
      m_pd3dDevice->GetRenderTarget( 0, &OldSurface );

      m_pd3dDevice->SetRenderTarget( 0, m_pColorSurface );

      m_pd3dDevice->Clear( 0L,
                           NULL,
                           D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                           0x00000000,                            
                           1.0f,
                           0L
                         );

      //背景レンダリング
      m_pLambert19->Begin();
      D3DXMatrixIdentity( &matWorld );

      //平行光源の色を設定
      D3DXVECTOR4 DirectionalColor = D3DXVECTOR4( 0.1f, 0.1f, 0.1f, 1.0f );
      
      m_pLambert19->SetMatrix( &matWorld, &DirectionalColor, &LightDir );
      m_pd3dDevice->SetTexture( 0, m_pMeshBack->m_pTextures[0] );
      m_pLambert19->SetAmbient( 0.1f );
      m_pLambert19->BeginPass(0);
      m_pMeshBack->m_pLocalMesh->DrawSubset( 0 );
      m_pLambert19->EndPass();

      //トラ
      m_pLambert19->SetMatrix( &matTiger, &DirectionalColor, &LightDir );
      m_pd3dDevice->SetTexture( 0, m_pMeshTiger->m_pTextures[0] );
      m_pLambert19->SetAmbient( 0.1f );
      m_pLambert19->BeginPass(0);
      m_pMeshTiger->m_pLocalMesh->DrawSubset( 0 );
      m_pLambert19->EndPass();

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

      m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );

      m_pd3dDevice->SetRenderTarget( 0, OldSurface );
      SafeRelease( OldSurface );

      //****************************************************************
      //ステップ2 : ナイトスコープ適応
      //****************************************************************

      m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE );

      m_pd3dDevice->SetTexture( 0, m_pColorTexture );
      m_pd3dDevice->SetTexture( 1, m_pNoiseTexture );
      
      //ナイトスコープの光の増幅率を設定する
      m_pNightScopeShader->SetParameter( 7.0f );
      
      m_pNightScopeShader->Render();

      m_pd3dDevice->SetTexture( 1, NULL );
      m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );

      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