0001: // ----------------------------------------------------------------------------
0002: //
0003: // draw.cpp - 描画部分
0004: // 
0005: // Copyright (c) 2001 if (if@edokko.com)
0006: // All Rights Reserved.
0007: //
0008: // ----------------------------------------------------------------------------
0009: #define STRICT
0010: 
0011: #include <windows.h>
0012: #include "main.h"
0013: #include "draw.h"
0014: #include "load.h"
0015: 
0016: LPDIRECT3DVERTEXBUFFER8 pMeshVB = NULL;
0017: LPDIRECT3DINDEXBUFFER8  pMeshIndex = NULL;
0018: D3DXATTRIBUTERANGE      *pSubsetTable = NULL;
0019: DWORD                   nMeshFaces = 0;
0020: DWORD                   nMeshVertices = 0;
0021: D3DMATERIAL8            *pMeshMaterials = NULL;     // メッシュの質感
0022: LPDIRECT3DTEXTURE8      *pMeshTextures  = NULL;     // メッシュのテクスチャー
0023: DWORD                   dwNumMaterials = 0L;        // マテリアルの数
0024: FLOAT                   MeshRadius;                 // メッシュの大きさ
0025: 
0026: DWORD                   hVertexShader=~0;           // モデルの描画
0027: DWORD                   hPixelShader=~0;            // モデルの描画
0028: 
0029: 
0030: // ぼかした絵を作るためのオブジェクト
0031: LPDIRECT3DSURFACE8      pBackbuffer = NULL;
0032: LPDIRECT3DTEXTURE8      pMaskTexture = NULL;        // マスク用テクスチャー
0033: DWORD                   hBlurVertexShader=~0;
0034: DWORD                   hBlurPixelShader=~0;
0035: DWORD                   hFocusPixelShader=~0;
0036: LPDIRECT3DVERTEXBUFFER8 pBlurVB = NULL;
0037: LPDIRECT3DINDEXBUFFER8  pBlurIB = NULL;
0038: 
0039: const int nBlurTex = 3;
0040: LPDIRECT3DTEXTURE8      pTexture[nBlurTex];
0041: LPDIRECT3DSURFACE8      pTextureSurface[nBlurTex];
0042: 
0043: const int nTempTex = 3;
0044: LPDIRECT3DTEXTURE8      pTmpTexture[nTempTex];
0045: LPDIRECT3DSURFACE8      pTmpSurface[nTempTex];
0046: 
0047: typedef struct {
0048:     float x,y,z;
0049:     float tu,tv;
0050: }D3D_BLUR_VERTEX;
0051: #define D3DFVF_BLUR_VERTEX      (D3DFVF_XYZ | D3DFVF_TEX1)
0052: 
0053: DWORD dwBlurDecl[] = {
0054:     D3DVSD_STREAM(0),
0055:     D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ),          //D3DVSDE_POSITION,  0
0056:     D3DVSD_REG(D3DVSDE_TEXCOORD0,D3DVSDT_FLOAT2 ),          //D3DVSDE_TEXCOORD0, 7  
0057:     D3DVSD_END()
0058: };
0059: 
0060: 
0061: // ----------------------------------------------------------------------------
0062: // 外部関数
0063: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev);
0064: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev);
0065: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev);
0066: 
0067: 
0068: 
0069: // ----------------------------------------------------------------------------
0070: // Name: LoadXFile(char* filename, LPDIRECT3DDEVICE8 lpD3DDev)
0071: // Desc: X-Fileの読み込み
0072: //-----------------------------------------------------------------------------
0073: HRESULT LoadXFile(char* filename, LPDIRECT3DDEVICE8 lpD3DDev)
0074: {
0075:     LPD3DXMESH pMesh, pMeshOpt;
0076:     LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
0077:     DWORD i;
0078:     HRESULT hr;
0079: 
0080:     hr = D3DXLoadMeshFromX(filename, D3DXMESH_MANAGED,
0081:                                 lpD3DDev, NULL,
0082:                                 &pD3DXMtrlBuffer, &dwNumMaterials,
0083:                                 &pMesh);
0084:     if(FAILED(hr)) return E_FAIL;
0085: 
0086:     //並び替えておく
0087:     pMesh->Optimize(D3DXMESHOPT_ATTRSORT, NULL, NULL, NULL, NULL, &pMeshOpt);
0088:     RELEASE(pMesh);
0089: 
0090:     //アトリビュートテーブル
0091:     pMeshOpt->GetAttributeTable(NULL,&dwNumMaterials);
0092:     pSubsetTable = new D3DXATTRIBUTERANGE[dwNumMaterials];
0093:     pMeshOpt->GetAttributeTable(pSubsetTable, &dwNumMaterials);
0094: 
0095:     // FVF変換
0096:     hr = pMeshOpt->CloneMeshFVF(pMeshOpt->GetOptions(), D3DFVF_VERTEX, lpD3DDev, &pMesh);
0097:     if(FAILED(hr)) return E_FAIL;
0098:     RELEASE(pMeshOpt);
0099:     D3DXComputeNormals(pMesh, NULL);
0100: 
0101:     //Vertex Bufferにコピーする
0102:     D3DVERTEX* pSrc;
0103:     D3D_CUSTOMVERTEX* pDest;
0104:     LPDIRECT3DINDEXBUFFER8 pSrcIndex;
0105:     WORD* pISrc;
0106:     WORD* pIDest;
0107: 
0108:     DWORD nMeshVertices = pMesh->GetNumVertices();
0109:     DWORD nMeshFaces = pMesh->GetNumFaces();
0110:     lpD3DDev->CreateVertexBuffer(nMeshVertices * sizeof(D3D_CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pMeshVB);
0111:     lpD3DDev->CreateIndexBuffer(nMeshFaces * 3 * sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&pMeshIndex);
0112: 
0113:     LPDIRECT3DVERTEXBUFFER8 pVB;
0114:     pMesh->GetVertexBuffer(&pVB);
0115:     pVB->Lock(0,0,(BYTE**)&pSrc,0);
0116:     pMeshVB->Lock(0,0,(BYTE**)&pDest,0);
0117:     MeshRadius = 0.0f;
0118:     for(i=0;i<nMeshVertices;i++){
0119:         pDest->x = pSrc->x;
0120:         pDest->y = pSrc->y;
0121:         pDest->z = pSrc->z;
0122:         pDest->nx = pSrc->nx;
0123:         pDest->ny = pSrc->ny;
0124:         pDest->nz = pSrc->nz;
0125:         pDest->tu0 = pSrc->tu0;
0126:         pDest->tu0 = pSrc->tu0;
0127:         // サイズの計算
0128:         FLOAT radius = sqrtf( pSrc->x*pSrc->x + pSrc->y*pSrc->y + pSrc->z*pSrc->z );
0129:         if (MeshRadius < radius) MeshRadius = radius;
0130:         
0131:         pSrc += 1;
0132:         pDest += 1;
0133:     }
0134:     pVB->Unlock();
0135:     pVB->Release();
0136:     pMeshVB->Unlock();
0137: 
0138:     //インデックスのコピー
0139:     pMesh->GetIndexBuffer(&pSrcIndex);
0140:     pSrcIndex->Lock(0,0,(BYTE**)&pISrc,0);
0141:     pMeshIndex->Lock(0,0,(BYTE**)&pIDest,0);
0142:     CopyMemory(pIDest,pISrc,nMeshFaces * 3 * sizeof(WORD));
0143:     pSrcIndex->Unlock();
0144:     pMeshIndex->Unlock();
0145:     pSrcIndex->Release();
0146: 
0147:     // pD3DXMtrlBuffer から、質感やテクスチャーの情報を読み取る
0148:     D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
0149:     pMeshTextures = new LPDIRECT3DTEXTURE8[dwNumMaterials];
0150:     pMeshMaterials = new D3DMATERIAL8[dwNumMaterials];
0151: 
0152:     for(i = 0; i < dwNumMaterials; i++){
0153:         pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
0154:         pMeshMaterials[i].Ambient = pMeshMaterials[i].Diffuse;
0155:         hr = D3DXCreateTextureFromFile( lpD3DDev, 
0156:                                         d3dxMaterials[i].pTextureFilename, 
0157:                                         &pMeshTextures[i] );
0158:         if(FAILED(hr)) pMeshTextures[i] = NULL;
0159:     }
0160:     RELEASE(pD3DXMtrlBuffer);
0161:     
0162:     RELEASE(pMesh);
0163: 
0164:     return S_OK;
0165: }
0166: //-----------------------------------------------------------------------------
0167: // Name: InitBlurTexture()
0168: // Desc: ボケたテクスチャー用の下準備
0169: //-----------------------------------------------------------------------------
0170: HRESULT InitBlurTexture(LPDIRECT3DDEVICE8 lpD3DDev)
0171: {
0172:     HRESULT hr;
0173:     DWORD i;
0174:     
0175:     // 頂点バッファの作成 
0176:     D3D_BLUR_VERTEX *pBlurDest;
0177:     WORD *pIndex;
0178:     lpD3DDev->CreateVertexBuffer( 4 * sizeof(D3D_BLUR_VERTEX),
0179:                                 D3DUSAGE_WRITEONLY, D3DFVF_BLUR_VERTEX, D3DPOOL_MANAGED,
0180:                                 &pBlurVB );
0181:     // 頂点をセットアップ
0182:     pBlurVB->Lock ( 0, 0, (BYTE**)&pBlurDest, 0 );
0183:     for (i = 0; i < 4; i++) {
0184:         pBlurDest->x   = (i == 0 || i == 1)?-1:(float)1;
0185:         pBlurDest->y   = (i == 0 || i == 2)?-1:(float)1;
0186:         pBlurDest->z   = 0.0f;
0187:         pBlurDest->tu = (i == 2 || i == 3)?1:(float)0;
0188:         pBlurDest->tv = (i == 0 || i == 2)?1:(float)0;
0189:         pBlurDest++;
0190:     }       
0191:     pBlurVB->Unlock ();
0192:     // インデックスをセットアップ
0193:     lpD3DDev->CreateIndexBuffer( 6 * sizeof(WORD),
0194:                                0,
0195:                                D3DFMT_INDEX16, D3DPOOL_MANAGED,
0196:                                &pBlurIB );
0197:     pBlurIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0198:     pIndex[0] = 0;  pIndex[1] = 1;  pIndex[2] = 2;
0199:     pIndex[3] = 1;  pIndex[4] = 3;  pIndex[5] = 2;
0200:     pBlurIB->Unlock ();
0201: 
0202:     // 描画用テクスチャーを用意する
0203:     D3DSURFACE_DESC Desc;
0204:     LPDIRECT3DSURFACE8 lpZbuffer = NULL;
0205:     if( FAILED(hr = lpD3DDev->GetRenderTarget(&pBackbuffer))) return hr;
0206:     if( FAILED(hr = pBackbuffer->GetDesc( &Desc ))) return hr;
0207: 
0208:     // 深度バッファのサーフェスを確保する
0209:     if( FAILED(hr = lpD3DDev->GetDepthStencilSurface( &lpZbuffer ))) return hr;
0210:     
0211:     for(i = 0; i < nBlurTex; i ++){
0212:         // テクスチャーの生成
0213:         if( FAILED(hr = lpD3DDev->CreateTexture(Desc.Width, Desc.Height, 1
0214:                                 , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTexture[i]))) return hr;
0215:         // テクスチャーとサーフェスを関連づける
0216:         if( FAILED(hr = pTexture[i]->GetSurfaceLevel(0,&pTextureSurface[i]))) return hr;
0217:         // テクスチャー用の描画と深度バッファを関連付ける(一枚目だけ深度を持つ)
0218:         if( FAILED(hr = lpD3DDev->SetRenderTarget(pTextureSurface[i], (i==0)?lpZbuffer:NULL ))) return hr;
0219:     }
0220:     // ぼけテクスチャー作成用のリソースを確保する
0221:     for (i = 0; i < nTempTex; ++i) {
0222:         // テクスチャーの生成
0223:         if( FAILED(hr = lpD3DDev->CreateTexture(Desc.Width, Desc.Height, 1
0224:                                 , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTmpTexture[i]))) return hr;
0225:         // テクスチャーとサーフェスを関連づける
0226:         if( FAILED(hr = pTmpTexture[i]->GetSurfaceLevel(0,&pTmpSurface[i]))) return hr;
0227:         // テクスチャー用の描画と深度バッファを関連付ける
0228:         if( FAILED(hr = lpD3DDev->SetRenderTarget(pTmpSurface[i], NULL ))) return hr;
0229:     }
0230: 
0231:     // 描画を元の画面に戻す
0232:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );
0233:     
0234:     // シェ-ダーのロード
0235:     if ( FAILED(CVertexShaderMgr::Load(lpD3DDev, "blur.vsh",     &hBlurVertexShader, dwBlurDecl)) ) return hr;
0236:     if ( FAILED( CPixelShaderMgr::Load(lpD3DDev, "blur.psh",     &hBlurPixelShader)) ) return hr;
0237:     if ( FAILED( CPixelShaderMgr::Load(lpD3DDev, "dof.psh",    &hFocusPixelShader)) ) return hr;
0238:     
0239:     // 定数レジスタの設定
0240:     float const s = 4.0f/3.0f;
0241:     float const inv_w = s / (float)WIDTH;
0242:     float const inv_h = s / (float)HEIGHT;
0243:     lpD3DDev->SetVertexShaderConstant(20, &D3DXVECTOR4  ( 0.0f,  0.0f, 0.0f, 0.0f), 1);
0244:     lpD3DDev->SetVertexShaderConstant(21, &D3DXVECTOR4  ( 0.0f, inv_h, 0.0f, 0.0f), 1);
0245:     lpD3DDev->SetVertexShaderConstant(22, &D3DXVECTOR4  (inv_w, inv_h, 0.0f, 0.0f), 1);
0246:     lpD3DDev->SetVertexShaderConstant(23, &D3DXVECTOR4  (inv_w,  0.0f, 0.0f, 0.0f), 1);
0247: 
0248:     return S_OK;
0249: }
0250: //-----------------------------------------------------------------------------
0251: // Name: InitRender()
0252: // Desc: Load the mesh and build the material and texture arrays
0253: //-----------------------------------------------------------------------------
0254: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDev)
0255: {
0256:     HRESULT hr;
0257: 
0258:     // モデルの読み込み
0259:     if ( FAILED(hr = LoadXFile("nsx.x", lpD3DDev)) ) return hr;
0260:     
0261:     // マスク用テクスチャーの読み込み
0262:     if ( FAILED(CTextureMgr::Load(lpD3DDev, "mask.bmp",     &pMaskTexture)) ) return hr;
0263:     
0264:     // バーテックスシェーダーを作成する
0265:     if ( FAILED(CVertexShaderMgr::Load(lpD3DDev, "vs.vsh",     &hVertexShader, dwDecl)) ) return hr;
0266:     if ( FAILED( CPixelShaderMgr::Load(lpD3DDev, "ps.psh",      &hPixelShader)) ) return hr;
0267:     
0268:     // ぼけテクスチャー用の初期化
0269:     InitBlurTexture(lpD3DDev);
0270:     
0271:     // 背景部分の初期化
0272:     InitBg(lpD3DDev);
0273: 
0274:     // 不変なレジスタの設定
0275:     lpD3DDev->SetRenderState( D3DRS_ZENABLE, TRUE );
0276:     lpD3DDev->SetRenderState( D3DRS_LIGHTING,  FALSE );
0277:     lpD3DDev->SetVertexShaderConstant(12, &D3DXVECTOR4(0.0f, 0.5f, 1.0f, 2.0f), 1);
0278: 
0279:     return S_OK;
0280: }
0281: 
0282: //-----------------------------------------------------------------------------
0283: // Name: Render()
0284: // Desc: Draws the scene
0285: //-----------------------------------------------------------------------------
0286: VOID Render(LPDIRECT3DDEVICE8 lpD3DDev)
0287: {
0288:     DWORD i, j, k;
0289:     D3DXMATRIX mWorld, mView, mProj, m;
0290:     
0291:     D3DXVECTOR4 lightDir(1.0f, 1.0f, 0.5f, 0.0f), vl;
0292: 
0293:     LPDIRECT3DSURFACE8 lpZbuffer = NULL;
0294:     lpD3DDev->GetDepthStencilSurface( &lpZbuffer );
0295:     lpD3DDev->SetRenderTarget(pTextureSurface[0], lpZbuffer);
0296:     lpD3DDev->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0),1.0f,0);
0297: 
0298:     // ビュー行列
0299:     D3DXVECTOR3 eye    = D3DXVECTOR3(0.0f,1.4f*MeshRadius,2.5f*MeshRadius);
0300:     D3DXVECTOR3 lookAt = D3DXVECTOR3(0.0f,  0.0f,  0.0f);
0301:     D3DXVECTOR3 up     = D3DXVECTOR3(0.0f,  1.0f,  0.0f);
0302:     // 通常表示
0303:     D3DXMatrixLookAtLH(&mView, &eye, &lookAt, &up);
0304: 
0305:     const float min =   0.01f;
0306:     const float max  = 100.0f;
0307:     D3DXMatrixPerspectiveFovLH(&mProj
0308:         ,60.0f*PI/180.0f                        // 視野角
0309:         ,(float)WIDTH/(float)HEIGHT             // アスペクト比
0310:         ,min,max                                // 最近接距離,最遠方距離
0311:         );
0312:     // z値を0.0fから1.0fに補正する定数
0313:     lpD3DDev->SetVertexShaderConstant(15, &D3DXVECTOR4(3.0f/(max-min), -0.45f*max/(max-min), 0.0f, 0.0f), 1);
0314:     
0315:     lpD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
0316:     lpD3DDev->SetVertexShader(hVertexShader);
0317:     lpD3DDev->SetPixelShader(hPixelShader);
0318: 
0319:     //
0320:     // 背景描画
0321:     // 
0322:     D3DXMatrixScaling(&mWorld, 3.0f, 3.0f, 3.0f);
0323:     
0324:     m = mWorld * mView * mProj;
0325:     D3DXMatrixTranspose( &m ,  &m);
0326:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0327:     
0328:     D3DXMatrixInverse( &m,  NULL, &mWorld);
0329:     D3DXVec4Transform(&vl, &lightDir, &m);
0330:     D3DXVec4Normalize(&vl, &vl);
0331:     vl[3] = 0.3f;
0332:     lpD3DDev->SetVertexShaderConstant(13, &vl, 1);
0333:     
0334:     lpD3DDev->SetVertexShaderConstant(14, &D3DXVECTOR4(0.0f, 0.0f, 0.0f, 1.0f), 1);
0335:     DrawBg(lpD3DDev);
0336:     
0337:     //
0338:     // もでる描画
0339:     //
0340:     int t = timeGetTime();
0341:     D3DXMatrixTranslation(&m, 1.5f*MeshRadius, 0,0);
0342:     D3DXMatrixRotationY( &mWorld, t/1000.0f );
0343: D3DXMatrixRotationY( &mWorld, -PI/2 );
0344:     mWorld = m * mWorld;
0345:     
0346:     m = mWorld * mView * mProj;
0347:     D3DXMatrixTranspose( &m ,  &m);
0348:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0349: 
0350:     D3DXMatrixInverse( &m,  NULL, &mWorld);
0351:     D3DXVec4Transform(&vl, &lightDir, &m);
0352:     D3DXVec4Normalize(&vl, &vl);
0353:     vl[3] = 0.3f;
0354:     lpD3DDev->SetVertexShaderConstant(13, &vl, 1);
0355:     
0356:     lpD3DDev->SetStreamSource(0, pMeshVB, sizeof(D3D_CUSTOMVERTEX));
0357:     lpD3DDev->SetIndices(pMeshIndex,0);
0358:     
0359:     for(i=0;i<dwNumMaterials;i++){
0360:         //色をセット
0361:         D3DXVECTOR4 vl;
0362:         vl.x = pMeshMaterials[i].Diffuse.r;
0363:         vl.y = pMeshMaterials[i].Diffuse.g;
0364:         vl.z = pMeshMaterials[i].Diffuse.b;
0365:         lpD3DDev->SetVertexShaderConstant(14, &vl, 1);
0366: 
0367:         lpD3DDev->SetTexture(0,pMeshTextures[i]);
0368:         lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 
0369:                                         pSubsetTable[i].VertexStart,
0370:                                         pSubsetTable[i].VertexCount,
0371:                                         pSubsetTable[i].FaceStart * 3,
0372:                                         pSubsetTable[i].FaceCount);
0373:     }
0374:     //
0375:     // モデル2
0376:     //
0377:     D3DXMatrixRotationY( &mWorld, -t/3000.0f );
0378: D3DXMatrixRotationY( &mWorld, 1.4f*PI/2 );
0379:     m = mWorld * mView * mProj;
0380:     D3DXMatrixTranspose( &m ,  &m);
0381:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0382:     for(i=0;i<dwNumMaterials;i++){
0383:         //色をセット
0384:         D3DXVECTOR4 vl;
0385:         vl.x = pMeshMaterials[i].Diffuse.r;
0386:         vl.y = pMeshMaterials[i].Diffuse.g;
0387:         vl.z = pMeshMaterials[i].Diffuse.b;
0388:         lpD3DDev->SetVertexShaderConstant(14, &vl, 1);
0389: 
0390:         lpD3DDev->SetTexture(0,pMeshTextures[i]);
0391:         lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 
0392:                                         pSubsetTable[i].VertexStart,
0393:                                         pSubsetTable[i].VertexCount,
0394:                                         pSubsetTable[i].FaceStart * 3,
0395:                                         pSubsetTable[i].FaceCount);
0396:     }
0397:     // 
0398:     // 半透明合成して、ボケたテクスチャーを作成する
0399:     // 
0400:     lpD3DDev->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
0401:     lpD3DDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
0402:     lpD3DDev->SetTextureStageState(0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE);
0403: 
0404:     for (i = 0; i < 4; ++i) {
0405:         lpD3DDev->SetTextureStageState(i, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
0406:         lpD3DDev->SetTextureStageState(i, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0407:         lpD3DDev->SetTextureStageState(i, D3DTSS_MIPFILTER, D3DTEXF_NONE);
0408:         lpD3DDev->SetTextureStageState(i, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP);
0409:         lpD3DDev->SetTextureStageState(i, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP);
0410:     }
0411: 
0412:     lpD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
0413:     lpD3DDev->SetRenderState(D3DRS_BLENDOP,   D3DBLENDOP_ADD);                 
0414:     lpD3DDev->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_ONE);
0415:     lpD3DDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
0416: 
0417:     lpD3DDev->SetRenderState(D3DRS_ZENABLE, FALSE);
0418: 
0419:     lpD3DDev->SetVertexShader(hBlurVertexShader);
0420:     lpD3DDev->SetPixelShader(hBlurPixelShader); 
0421:     lpD3DDev->SetStreamSource(0, pBlurVB, sizeof(D3D_BLUR_VERTEX));
0422:     lpD3DDev->SetIndices(pBlurIB,0);
0423:     
0424:     for (i = 1; i < nBlurTex; ++i){
0425:         for (j = 0; j < nTempTex; ++j){
0426:             LPDIRECT3DTEXTURE8  pSource      = (j ==          0)?       pTexture[i-1]:pTmpTexture[ j   &1];
0427:             LPDIRECT3DSURFACE8  pDestination = (j == nTempTex-1)?pTextureSurface[i  ]:pTmpSurface[(j+1)&1];
0428:             lpD3DDev->SetRenderTarget(pDestination, NULL);
0429:             for (k = 0; k < 4; ++k) lpD3DDev->SetTexture(k, pSource);
0430:             lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 4, 0, 2 );
0431:         }
0432:     }
0433:     // 
0434:     // 完成した一枚絵を描画する
0435:     // 
0436:     lpD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
0437:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );                 // 描画をバックバッファに戻す
0438:     lpD3DDev->SetPixelShader(hFocusPixelShader);    
0439:     lpD3DDev->SetTexture( 0, pTexture[0] );                 // 元テクスチャー
0440:     lpD3DDev->SetTexture( 1, pTexture[1] );                 // ボケテクスチャー
0441:     lpD3DDev->SetTexture( 2, pTexture[2] );                 // 大ボケテクスチャー
0442:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 4, 0, 2 );
0443:     //
0444:     // 環境を元に戻す
0445:     //
0446:     lpD3DDev->SetPixelShader(NULL); 
0447:     lpD3DDev->SetRenderState(D3DRS_ZENABLE, TRUE);
0448: }
0449: //-----------------------------------------------------------------------------
0450: // メッシュオブジェクト削除
0451: //-----------------------------------------------------------------------------
0452: void DeleteMeshObject(void)
0453: {
0454:     DWORD i;
0455: 
0456:     if(pMeshVB == NULL) return;
0457: 
0458:     for(i=0; i<dwNumMaterials; i++){
0459:         RELEASE(pMeshTextures[i]);
0460:     }
0461:     delete[] pMeshTextures;
0462:     delete[] pMeshMaterials;
0463:     delete[] pSubsetTable;
0464: 
0465:     RELEASE(pMeshVB);
0466:     RELEASE(pMeshIndex);
0467: }
0468: //-----------------------------------------------------------------------------
0469: // Name: CleanRender()
0470: // Desc: 後始末
0471: //-----------------------------------------------------------------------------
0472: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDev)
0473: {
0474:     DWORD i;
0475: 
0476:     CleanBg(lpD3DDev);
0477:     
0478:     for (i = 0; i < nTempTex; ++i) {
0479:         RELEASE(pTmpSurface[i]);
0480:         RELEASE(pTmpTexture [i]);
0481:     }
0482:     for(i = 0; i < nBlurTex; i++){
0483:         RELEASE(pTextureSurface[i]);
0484:         RELEASE(pTexture[i]);
0485:     }
0486:     
0487:     RELEASE(pBackbuffer);
0488:     
0489:     CPixelShaderMgr::Release(lpD3DDev, &hFocusPixelShader);
0490:     CPixelShaderMgr::Release(lpD3DDev, &hBlurPixelShader);
0491:     CVertexShaderMgr::Release(lpD3DDev, &hBlurVertexShader);
0492:     CVertexShaderMgr::Release(lpD3DDev, &hPixelShader);
0493:     CVertexShaderMgr::Release(lpD3DDev, &hVertexShader);
0494:     
0495:     RELEASE(pMaskTexture);
0496:     DeleteMeshObject();
0497: }
0498: