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: static const int SHADOWMAP_HEIGHT = 512;
0017: static const int SHADOWMAP_WIDTH = 512;
0018: 
0019: LPDIRECT3DVERTEXBUFFER8 pMeshVB = NULL;
0020: LPDIRECT3DINDEXBUFFER8  pMeshIndex = NULL;
0021: D3DXATTRIBUTERANGE      *pSubsetTable = NULL;
0022: DWORD                   nMeshFaces = 0;
0023: DWORD                   nMeshVertices = 0;
0024: D3DMATERIAL8            *pMeshMaterials = NULL;     // メッシュの質感
0025: LPDIRECT3DTEXTURE8      *pMeshTextures  = NULL;     // メッシュのテクスチャー
0026: DWORD                   dwNumMaterials = 0L;        // マテリアルの数
0027: FLOAT                   MeshRadius;                 // メッシュの大きさ
0028: 
0029: DWORD                   hVertexShader=~0;           // モデルの描画
0030: DWORD                   hPixelShader=~0;            // モデルの描画
0031: 
0032: 
0033: // 影を作りながら描画
0034: LPDIRECT3DSURFACE8      pBackbuffer = NULL;
0035: LPDIRECT3DSURFACE8      lpZbuffer = NULL;
0036: LPDIRECT3DTEXTURE8      pMaskTexture = NULL;        // マスク用テクスチャー
0037: DWORD                   hShadowPixelShader=~0;
0038: DWORD                   hShadowVertexShader=~0;
0039: LPDIRECT3DVERTEXBUFFER8 pBlurVB = NULL;
0040: LPDIRECT3DINDEXBUFFER8  pBlurIB = NULL;
0041: 
0042: LPDIRECT3DTEXTURE8      pTextureCol;
0043: LPDIRECT3DTEXTURE8      pTextureZ;
0044: LPDIRECT3DSURFACE8      pTextureSurfaceCol;
0045: LPDIRECT3DSURFACE8      pTextureSurfaceZ;
0046: LPDIRECT3DTEXTURE8      pLightTexture;
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: InitRenderTexture()
0168: // Desc: レンダリングテクスチャー用の下準備
0169: //-----------------------------------------------------------------------------
0170: HRESULT InitRenderTexture(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:     // 描画用テクスチャーを用意する
0204:     if( FAILED(hr = lpD3DDev->GetRenderTarget(&pBackbuffer))) return hr;
0205:     if(FAILED(lpD3DDev->GetDepthStencilSurface(&lpZbuffer))) return hr; // 追加
0206:     
0207:     if(FAILED(hr = lpD3DDev->CreateTexture(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT, 1
0208:                         , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8
0209:                         , D3DPOOL_DEFAULT, &pTextureCol)))
0210:         return hr;
0211:     if(FAILED(hr = lpD3DDev->CreateTexture(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT, 1
0212:                         , D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24S8
0213:                         , D3DPOOL_DEFAULT, &pTextureZ)))
0214:         return hr;
0215: 
0216:     if(FAILED(pTextureCol->GetSurfaceLevel(0, &pTextureSurfaceCol)))    return E_FAIL;
0217:     if(FAILED(pTextureZ->GetSurfaceLevel(0, &pTextureSurfaceZ)))        return E_FAIL;
0218: 
0219:     
0220:     if(FAILED(D3DXCreateTextureFromFileEx(lpD3DDev, 
0221:                                           "spotlight.bmp", 
0222:                                           SHADOWMAP_WIDTH,
0223:                                           SHADOWMAP_HEIGHT,
0224:                                           1,
0225:                                           D3DUSAGE_RENDERTARGET,
0226:                                           D3DFMT_A8R8G8B8,
0227:                                           D3DPOOL_DEFAULT,
0228:                                           D3DX_DEFAULT,
0229:                                           D3DX_DEFAULT,
0230:                                           0,
0231:                                           NULL,
0232:                                           NULL,
0233:                                           &pLightTexture))) return hr;
0234: 
0235: 
0236:     // シェ-ダーのロード
0237:     if ( FAILED( CPixelShaderMgr::Load(lpD3DDev, "shadow.psh",    &hShadowPixelShader)) ) return hr;
0238:     if ( FAILED(CVertexShaderMgr::Load(lpD3DDev, "shadow.vsh",    &hShadowVertexShader, dwDecl)) ) return hr;
0239:     
0240:     return S_OK;
0241: }
0242: //-----------------------------------------------------------------------------
0243: // Name: InitRender()
0244: // Desc: Load the mesh and build the material and texture arrays
0245: //-----------------------------------------------------------------------------
0246: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDev)
0247: {
0248:     HRESULT hr;
0249: 
0250:     // モデルの読み込み
0251:     if ( FAILED(hr = LoadXFile("nsx.x", lpD3DDev)) ) return hr;
0252:     
0253:     // マスク用テクスチャーの読み込み
0254:     if ( FAILED(CTextureMgr::Load(lpD3DDev, "mask.bmp",     &pMaskTexture)) ) return hr;
0255:     
0256:     // バーテックスシェーダーを作成する
0257:     if ( FAILED(CVertexShaderMgr::Load(lpD3DDev, "vs.vsh",     &hVertexShader, dwDecl)) ) return hr;
0258:     if ( FAILED( CPixelShaderMgr::Load(lpD3DDev, "ps.psh",      &hPixelShader)) ) return hr;
0259:     
0260:     // レンダリングテクスチャー用の初期化
0261:     InitRenderTexture(lpD3DDev);
0262:     
0263:     // 背景部分の初期化
0264:     InitBg(lpD3DDev);
0265: 
0266:     // 不変なレジスタの設定
0267:     lpD3DDev->SetRenderState( D3DRS_ZENABLE, TRUE );
0268:     lpD3DDev->SetRenderState( D3DRS_LIGHTING,  FALSE );
0269:     lpD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
0270:     lpD3DDev->SetVertexShaderConstant(12, &D3DXVECTOR4(0.0f, 0.5f, 1.0f, 2.0f), 1);
0271: 
0272:     // デカール
0273:     lpD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0274:     lpD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
0275:     lpD3DDev->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_NONE);
0276:     
0277:     // 影マップ
0278:     lpD3DDev->SetTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
0279:     lpD3DDev->SetTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
0280:     lpD3DDev->SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0281:     lpD3DDev->SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
0282:     lpD3DDev->SetTextureStageState(1, D3DTSS_MIPFILTER, D3DTEXF_NONE);
0283: 
0284:     // ライトマップ
0285:     lpD3DDev->SetTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
0286:     lpD3DDev->SetTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
0287:     lpD3DDev->SetTextureStageState(2, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0288:     lpD3DDev->SetTextureStageState(2, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
0289:     lpD3DDev->SetTextureStageState(2, D3DTSS_MIPFILTER, D3DTEXF_NONE);
0290: 
0291:     return S_OK;
0292: }
0293: //-----------------------------------------------------------------------------
0294: // Name: Render()
0295: // Desc: Draws the scene
0296: //-----------------------------------------------------------------------------
0297: const float z_min =   1.0f;
0298: const float z_max  = 70.0f;
0299: D3DXVECTOR4 lightDir(1.0f, 1.0f, 0.5f, 0.0f);
0300: 
0301: VOID DrawModel(LPDIRECT3DDEVICE8 lpD3DDev, BOOL bShadowMap)
0302: {
0303:     D3DXMATRIX mVP,  mWorld, mView,  mProj;
0304:     D3DXMATRIX mVPL, mLight, mViewL, mProjL;
0305:     D3DXMATRIX  m;
0306:     D3DXVECTOR4 vl;
0307:     DWORD i;
0308:     
0309:     // 
0310:     // 通常レンダリング用の行列を作成
0311:     // 
0312:     // ビュー行列
0313:     D3DXVECTOR3 eye    = D3DXVECTOR3(0.0f,1.4f*MeshRadius,2.5f*MeshRadius);
0314:     D3DXVECTOR3 lookAt = D3DXVECTOR3(0.0f,  0.0f,  0.0f);
0315:     D3DXVECTOR3 up     = D3DXVECTOR3(0.0f,  1.0f,  0.0f);
0316:     D3DXMatrixLookAtLH(&mView, &eye, &lookAt, &up);
0317:     // 射影行列
0318:     D3DXMatrixPerspectiveFovLH(&mProj
0319:         , D3DXToRadian(60.0f)                       // 視野角
0320:         , (float)WIDTH/(float)HEIGHT                // アスペクト比
0321:         , z_min, z_max                              // 最近接距離,最遠方距離
0322:         );
0323:     
0324:     mVP = mView * mProj;
0325: 
0326:     // 
0327:     // ライト方向からのレンダリング用の行列を作成
0328:     // 
0329:     // ビュー行列
0330:     D3DXVECTOR3 l_eye    = 30.0f*lightDir;
0331:     D3DXVECTOR3 l_lookAt = D3DXVECTOR3(0.0f,  0.0f,  0.0f);
0332:     D3DXVECTOR3 l_up     = D3DXVECTOR3(0.0f,  1.0f,  0.0f);
0333:     D3DXMatrixLookAtLH(&mViewL, &l_eye, &l_lookAt, &l_up);
0334:     // 射影行列
0335:     D3DXMatrixPerspectiveFovLH(&mProjL
0336:         , D3DXToRadian(60.0f)                           // 視野角
0337:         , (float)SHADOWMAP_WIDTH/(float)SHADOWMAP_HEIGHT// アスペクト比
0338:         , 1.0f, 70.0f                                   // 最近接距離,最遠方距離
0339:         );
0340:     mVPL = mViewL * mProjL;
0341: 
0342:     // 射影空間から、テクスチャーの空間に変換する
0343:     float fOffsetX = 0.5f + (0.5f / (float)SHADOWMAP_WIDTH);
0344:     float fOffsetY = 0.5f + (0.5f / (float)SHADOWMAP_HEIGHT);
0345:     unsigned int range = 0xFFFFFFFF >> (32 - 24);
0346:     float fBias    = +0.000001f * (float)range;// カリングを反対にするので、通常とは逆のオフセット
0347:     D3DXMATRIX mScaleBias( 0.5f,     0.0f,     0.0f,         0.0f,
0348:                            0.0f,    -0.5f,     0.0f,         0.0f,
0349:                            0.0f,     0.0f,     (float)range, 0.0f,
0350:                            fOffsetX, fOffsetY, fBias,        1.0f );
0351: 
0352:     mLight = mVPL * mScaleBias;
0353: 
0354:     //
0355:     // レンダリング状態の設定
0356:     // 
0357:     if(bShadowMap){
0358:         lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
0359:     }else{
0360:         lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
0361:     }
0362:     
0363:     //
0364:     // 外側もでる描画
0365:     //
0366:     int t = timeGetTime()%314159;
0367:     D3DXMatrixTranslation(&m, 1.5f*MeshRadius, 0,0);
0368:     D3DXMatrixRotationY( &mWorld, t/1000.0f );
0369: //D3DXMatrixRotationY( &mWorld, -0.2*PI/2 ); // 撮影角度
0370:     mWorld = m * mWorld;
0371:     
0372:     if(bShadowMap){
0373:         m = mWorld * mVPL;
0374:     }else{
0375:         m = mWorld * mLight;
0376:         D3DXMatrixTranspose( &m ,  &m);
0377:         lpD3DDev->SetVertexShaderConstant(4,&m, 4);
0378: 
0379:         m = mWorld * mVP;
0380:     }
0381:     D3DXMatrixTranspose( &m ,  &m);
0382:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0383: 
0384:     D3DXMatrixInverse( &m,  NULL, &mWorld);
0385:     D3DXVec4Transform(&vl, &lightDir, &m);
0386:     D3DXVec4Normalize(&vl, &vl);
0387:     vl[3] = 0.1f;// w成分は環境色の強さ
0388:     lpD3DDev->SetVertexShaderConstant(13, &vl, 1);
0389:     
0390:     lpD3DDev->SetStreamSource(0, pMeshVB, sizeof(D3D_CUSTOMVERTEX));
0391:     lpD3DDev->SetIndices(pMeshIndex,0);
0392:     
0393:     for(i=0;i<dwNumMaterials;i++){
0394:         //色をセット
0395:         D3DXVECTOR4 vl;
0396:         vl.x = pMeshMaterials[i].Diffuse.r;
0397:         vl.y = pMeshMaterials[i].Diffuse.g;
0398:         vl.z = pMeshMaterials[i].Diffuse.b;
0399:         lpD3DDev->SetVertexShaderConstant(14, &vl, 1);
0400: 
0401:         lpD3DDev->SetTexture(0,pMeshTextures[i]);
0402:         lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 
0403:                                         pSubsetTable[i].VertexStart,
0404:                                         pSubsetTable[i].VertexCount,
0405:                                         pSubsetTable[i].FaceStart * 3,
0406:                                         pSubsetTable[i].FaceCount);
0407:     }
0408:     //
0409:     // モデル2
0410:     //
0411:     D3DXMatrixRotationY( &mWorld, -t/3000.0f );
0412: //D3DXMatrixRotationY( &mWorld, 1.4f*PI/2 ); // 撮影角度
0413:     if(bShadowMap){
0414:         m = mWorld * mVPL;
0415:     }else{
0416:         m = mWorld * mLight;
0417:         D3DXMatrixTranspose( &m ,  &m);
0418:         lpD3DDev->SetVertexShaderConstant(4,&m, 4);
0419: 
0420:         m = mWorld * mVP;
0421:     }
0422:     D3DXMatrixTranspose( &m ,  &m);
0423:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0424: 
0425:     D3DXMatrixInverse( &m,  NULL, &mWorld);
0426:     D3DXVec4Transform(&vl, &lightDir, &m);
0427:     D3DXVec4Normalize(&vl, &vl);
0428:     vl[3] = 0.1f;// w成分は環境色の強さ
0429:     lpD3DDev->SetVertexShaderConstant(13, &vl, 1);
0430:     
0431:     for(i=0;i<dwNumMaterials;i++){
0432:         //色をセット
0433:         D3DXVECTOR4 vl;
0434:         vl.x = pMeshMaterials[i].Diffuse.r;
0435:         vl.y = pMeshMaterials[i].Diffuse.g;
0436:         vl.z = pMeshMaterials[i].Diffuse.b;
0437:         lpD3DDev->SetVertexShaderConstant(14, &vl, 1);
0438: 
0439:         lpD3DDev->SetTexture(0,pMeshTextures[i]);
0440:         lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 
0441:                                         pSubsetTable[i].VertexStart,
0442:                                         pSubsetTable[i].VertexCount,
0443:                                         pSubsetTable[i].FaceStart * 3,
0444:                                         pSubsetTable[i].FaceCount);
0445:     }
0446:     //
0447:     // 背景描画
0448:     // 
0449:     if(!bShadowMap){// シャドウマップ作成時は影にならない遠景は表示しない
0450:         D3DXMatrixScaling(&mWorld, 3.0f, 3.0f, 3.0f);
0451:         
0452:         if(bShadowMap){
0453:             m = mWorld * mVPL;
0454:         }else{
0455:             m = mWorld * mLight;
0456:             D3DXMatrixTranspose( &m ,  &m);
0457:             lpD3DDev->SetVertexShaderConstant(4,&m, 4);
0458: 
0459:             m = mWorld * mVP;
0460:         }
0461:         D3DXMatrixTranspose( &m ,  &m);
0462:         lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0463:         
0464:         lpD3DDev->SetVertexShaderConstant(13, &D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f), 1);
0465:         lpD3DDev->SetVertexShaderConstant(14, &D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f), 1);
0466:         DrawBg(lpD3DDev);
0467:     }
0468: }
0469: //-----------------------------------------------------------------------------
0470: // Name: Render()
0471: // Desc: Draws the scene
0472: //-----------------------------------------------------------------------------
0473: VOID Render(LPDIRECT3DDEVICE8 lpD3DDev)
0474: {
0475:     // ------------------------------------------------------------------------
0476:     // 影マップの作成
0477:     // ------------------------------------------------------------------------
0478:     // テクスチャーにレンダリングする
0479:     lpD3DDev->SetRenderTarget(pTextureSurfaceCol, pTextureSurfaceZ);
0480:     // ビューポートの変更       x y         w                h      min_z max_z
0481:     D3DVIEWPORT8 newViewport = {0,0,SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT, 0, 1};
0482:     D3DVIEWPORT8 oldViewport;
0483:     lpD3DDev->GetViewport(&oldViewport);
0484:     lpD3DDev->SetViewport(&newViewport);
0485:     // 高速化のために、深度だけ更新する
0486:     lpD3DDev->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
0487:     // 深度を初期化する
0488:     lpD3DDev->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0x000000, 1.0f, 0L);
0489: 
0490:     // シェーダーの設定
0491:     lpD3DDev->SetVertexShader(hVertexShader);
0492:     lpD3DDev->SetPixelShader(hPixelShader);
0493:     
0494:     DrawModel(lpD3DDev, 1);
0495: 
0496:     // ------------------------------------------------------------------------
0497:     // シーンの作成
0498:     // ------------------------------------------------------------------------
0499:     // 描画を戻す
0500:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );
0501:     lpD3DDev->SetViewport(&oldViewport);
0502:     lpD3DDev->SetRenderState(D3DRS_COLORWRITEENABLE, 0x7);
0503:     lpD3DDev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x000000, 1.0f, 0L);
0504: 
0505:     // シェーダーの設定
0506:     lpD3DDev->SetVertexShader(hShadowVertexShader); 
0507:     lpD3DDev->SetPixelShader(hShadowPixelShader);   
0508: 
0509:     lpD3DDev->SetTexture( 1, pTextureZ );                   // 影マップ
0510:     lpD3DDev->SetTexture( 2, pLightTexture );               // ライトテクスチャー
0511:     DrawModel(lpD3DDev, 0);
0512:     
0513:     // 他でレンダリングしても問題が出ない設定にする
0514:     lpD3DDev->SetTexture( 0, NULL );
0515:     lpD3DDev->SetTexture( 1, NULL );
0516:     lpD3DDev->SetTexture( 2, NULL );
0517:     lpD3DDev->SetPixelShader(0);
0518: }
0519: //-----------------------------------------------------------------------------
0520: // メッシュオブジェクト削除
0521: //-----------------------------------------------------------------------------
0522: void DeleteMeshObject(void)
0523: {
0524:     DWORD i;
0525: 
0526:     if(pMeshVB == NULL) return;
0527: 
0528:     for(i=0; i<dwNumMaterials; i++){
0529:         RELEASE(pMeshTextures[i]);
0530:     }
0531:     delete[] pMeshTextures;
0532:     delete[] pMeshMaterials;
0533:     delete[] pSubsetTable;
0534: 
0535:     RELEASE(pMeshVB);
0536:     RELEASE(pMeshIndex);
0537: }
0538: //-----------------------------------------------------------------------------
0539: // Name: CleanRender()
0540: // Desc: 後始末
0541: //-----------------------------------------------------------------------------
0542: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDev)
0543: {
0544:     CleanBg(lpD3DDev);
0545:     
0546:     RELEASE(pTextureSurfaceZ);
0547:     RELEASE(pTextureSurfaceCol);
0548:     RELEASE(pTextureZ);
0549:     RELEASE(pTextureCol);
0550:     RELEASE(pLightTexture);
0551:     
0552:     RELEASE(pBackbuffer);
0553:     
0554:     CPixelShaderMgr::Release(lpD3DDev, &hShadowPixelShader);
0555:     CVertexShaderMgr::Release(lpD3DDev, &hShadowVertexShader);
0556:     CPixelShaderMgr::Release(lpD3DDev, &hPixelShader);
0557:     CVertexShaderMgr::Release(lpD3DDev, &hVertexShader);
0558:     
0559:     RELEASE(pMaskTexture);
0560:     DeleteMeshObject();
0561: }
0562: