0001: // ---------------------------------------------------------------------------- 0002: // 0003: // draw.cpp - 描画部分 0004: // 0005: // Copyright (c) 2002 IF(IMAGIRE Takashi) (imagire@gmail.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: static const unsigned int SHADOW_MAP_SIZE = 512; 0032: LPDIRECT3DSURFACE8 pBackbuffer = NULL; 0033: DWORD hShadowPixelShader=~0; 0034: DWORD hShadowVertexShader=~0; 0035: LPDIRECT3DVERTEXBUFFER8 pBlurVB = NULL; 0036: LPDIRECT3DINDEXBUFFER8 pBlurIB = NULL; 0037: 0038: LPDIRECT3DTEXTURE8 pTexture; 0039: LPDIRECT3DSURFACE8 pTextureSurface; 0040: 0041: typedef struct { 0042: float x,y,z; 0043: float tu,tv; 0044: }D3D_BLUR_VERTEX; 0045: #define D3DFVF_BLUR_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1) 0046: 0047: DWORD dwBlurDecl[] = { 0048: D3DVSD_STREAM(0), 0049: D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ), //D3DVSDE_POSITION, 0 0050: D3DVSD_REG(D3DVSDE_TEXCOORD0,D3DVSDT_FLOAT2 ), //D3DVSDE_TEXCOORD0, 7 0051: D3DVSD_END() 0052: }; 0053: 0054: 0055: // ---------------------------------------------------------------------------- 0056: // 外部関数 0057: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev); 0058: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev); 0059: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev); 0060: 0061: 0062: 0063: // ---------------------------------------------------------------------------- 0064: // Name: LoadXFile(char* filename, LPDIRECT3DDEVICE8 lpD3DDev) 0065: // Desc: X-Fileの読み込み 0066: //----------------------------------------------------------------------------- 0067: HRESULT LoadXFile(char* filename, LPDIRECT3DDEVICE8 lpD3DDev) 0068: { 0069: LPD3DXMESH pMesh, pMeshOpt; 0070: LPD3DXBUFFER pD3DXMtrlBuffer = NULL; 0071: DWORD i; 0072: HRESULT hr; 0073: 0074: hr = D3DXLoadMeshFromX(filename, D3DXMESH_MANAGED, 0075: lpD3DDev, NULL, 0076: &pD3DXMtrlBuffer, &dwNumMaterials, 0077: &pMesh); 0078: if(FAILED(hr)) return E_FAIL; 0079: 0080: //並び替えておく 0081: pMesh->Optimize(D3DXMESHOPT_ATTRSORT, NULL, NULL, NULL, NULL, &pMeshOpt); 0082: RELEASE(pMesh); 0083: 0084: //アトリビュートテーブル 0085: pMeshOpt->GetAttributeTable(NULL,&dwNumMaterials); 0086: pSubsetTable = new D3DXATTRIBUTERANGE[dwNumMaterials]; 0087: pMeshOpt->GetAttributeTable(pSubsetTable, &dwNumMaterials); 0088: 0089: // FVF変換 0090: hr = pMeshOpt->CloneMeshFVF(pMeshOpt->GetOptions(), D3DFVF_VERTEX, lpD3DDev, &pMesh); 0091: if(FAILED(hr)) return E_FAIL; 0092: RELEASE(pMeshOpt); 0093: D3DXComputeNormals(pMesh, NULL); 0094: 0095: //Vertex Bufferにコピーする 0096: D3DVERTEX* pSrc; 0097: D3D_CUSTOMVERTEX* pDest; 0098: LPDIRECT3DINDEXBUFFER8 pSrcIndex; 0099: WORD* pISrc; 0100: WORD* pIDest; 0101: 0102: DWORD nMeshVertices = pMesh->GetNumVertices(); 0103: DWORD nMeshFaces = pMesh->GetNumFaces(); 0104: lpD3DDev->CreateVertexBuffer(nMeshVertices * sizeof(D3D_CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pMeshVB); 0105: lpD3DDev->CreateIndexBuffer(nMeshFaces * 3 * sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&pMeshIndex); 0106: 0107: LPDIRECT3DVERTEXBUFFER8 pVB; 0108: pMesh->GetVertexBuffer(&pVB); 0109: pVB->Lock(0,0,(BYTE**)&pSrc,0); 0110: pMeshVB->Lock(0,0,(BYTE**)&pDest,0); 0111: MeshRadius = 0.0f; 0112: for(i=0;i<nMeshVertices;i++){ 0113: pDest->x = pSrc->x; 0114: pDest->y = pSrc->y; 0115: pDest->z = pSrc->z; 0116: pDest->nx = pSrc->nx; 0117: pDest->ny = pSrc->ny; 0118: pDest->nz = pSrc->nz; 0119: pDest->tu0 = pSrc->tu0; 0120: pDest->tu0 = pSrc->tu0; 0121: // サイズの計算 0122: FLOAT radius = sqrtf( pSrc->x*pSrc->x + pSrc->y*pSrc->y + pSrc->z*pSrc->z ); 0123: if (MeshRadius < radius) MeshRadius = radius; 0124: 0125: pSrc += 1; 0126: pDest += 1; 0127: } 0128: pVB->Unlock(); 0129: pVB->Release(); 0130: pMeshVB->Unlock(); 0131: 0132: //インデックスのコピー 0133: pMesh->GetIndexBuffer(&pSrcIndex); 0134: pSrcIndex->Lock(0,0,(BYTE**)&pISrc,0); 0135: pMeshIndex->Lock(0,0,(BYTE**)&pIDest,0); 0136: CopyMemory(pIDest,pISrc,nMeshFaces * 3 * sizeof(WORD)); 0137: pSrcIndex->Unlock(); 0138: pMeshIndex->Unlock(); 0139: pSrcIndex->Release(); 0140: 0141: // pD3DXMtrlBuffer から、質感やテクスチャーの情報を読み取る 0142: D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer(); 0143: pMeshTextures = new LPDIRECT3DTEXTURE8[dwNumMaterials]; 0144: pMeshMaterials = new D3DMATERIAL8[dwNumMaterials]; 0145: 0146: for(i = 0; i < dwNumMaterials; i++){ 0147: pMeshMaterials[i] = d3dxMaterials[i].MatD3D; 0148: pMeshMaterials[i].Ambient = pMeshMaterials[i].Diffuse; 0149: hr = D3DXCreateTextureFromFile( lpD3DDev, 0150: d3dxMaterials[i].pTextureFilename, 0151: &pMeshTextures[i] ); 0152: if(FAILED(hr)) pMeshTextures[i] = NULL; 0153: } 0154: RELEASE(pD3DXMtrlBuffer); 0155: 0156: RELEASE(pMesh); 0157: 0158: return S_OK; 0159: } 0160: //----------------------------------------------------------------------------- 0161: // Name: InitRenderingTexture() 0162: // Desc: 描画するテクスチャーの下準備 0163: //----------------------------------------------------------------------------- 0164: HRESULT InitRenderingTexture(LPDIRECT3DDEVICE8 lpD3DDev) 0165: { 0166: HRESULT hr; 0167: DWORD i; 0168: 0169: // 頂点バッファの作成 0170: D3D_BLUR_VERTEX *pBlurDest; 0171: WORD *pIndex; 0172: lpD3DDev->CreateVertexBuffer( 4 * sizeof(D3D_BLUR_VERTEX), 0173: D3DUSAGE_WRITEONLY, D3DFVF_BLUR_VERTEX, D3DPOOL_MANAGED, 0174: &pBlurVB ); 0175: // 頂点をセットアップ 0176: pBlurVB->Lock ( 0, 0, (BYTE**)&pBlurDest, 0 ); 0177: for (i = 0; i < 4; i++) { 0178: pBlurDest->x = (i == 0 || i == 1)?-1:(float)1; 0179: pBlurDest->y = (i == 0 || i == 2)?-1:(float)1; 0180: pBlurDest->z = 0.0f; 0181: pBlurDest->tu = (i == 2 || i == 3)?1:(float)0; 0182: pBlurDest->tv = (i == 0 || i == 2)?1:(float)0; 0183: pBlurDest++; 0184: } 0185: pBlurVB->Unlock (); 0186: // インデックスをセットアップ 0187: lpD3DDev->CreateIndexBuffer( 6 * sizeof(WORD), 0188: 0, 0189: D3DFMT_INDEX16, D3DPOOL_MANAGED, 0190: &pBlurIB ); 0191: pBlurIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 ); 0192: pIndex[0] = 0; pIndex[1] = 1; pIndex[2] = 2; 0193: pIndex[3] = 1; pIndex[4] = 3; pIndex[5] = 2; 0194: pBlurIB->Unlock (); 0195: 0196: // 描画用テクスチャーを用意する 0197: D3DSURFACE_DESC Desc; 0198: LPDIRECT3DSURFACE8 lpZbuffer = NULL; 0199: if( FAILED(hr = lpD3DDev->GetRenderTarget(&pBackbuffer))) return hr; 0200: if( FAILED(hr = pBackbuffer->GetDesc( &Desc ))) return hr; 0201: 0202: // 深度バッファのサーフェスを確保する 0203: if( FAILED(hr = lpD3DDev->GetDepthStencilSurface( &lpZbuffer ))) return hr; 0204: 0205: // テクスチャーの生成 0206: if( FAILED(hr = lpD3DDev->CreateTexture(SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, 1 0207: , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTexture))) return hr; 0208: // テクスチャーとサーフェスを関連づける 0209: if( FAILED(hr = pTexture->GetSurfaceLevel(0,&pTextureSurface))) return hr; 0210: // テクスチャー用の描画と深度バッファを関連付ける 0211: if( FAILED(hr = lpD3DDev->SetRenderTarget(pTextureSurface, lpZbuffer ))) return hr; 0212: 0213: // 描画を元の画面に戻す 0214: lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer ); 0215: 0216: // シェ-ダーのロード 0217: if ( FAILED( CPixelShaderMgr::Load(lpD3DDev, "shadow.psh", &hShadowPixelShader)) ) return hr; 0218: if ( FAILED(CVertexShaderMgr::Load(lpD3DDev, "shadow.vsh", &hShadowVertexShader, dwDecl)) ) return hr; 0219: 0220: return S_OK; 0221: } 0222: //----------------------------------------------------------------------------- 0223: // Name: InitRender() 0224: // Desc: Load the mesh and build the material and texture arrays 0225: //----------------------------------------------------------------------------- 0226: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDev) 0227: { 0228: HRESULT hr; 0229: 0230: timeBeginPeriod( 10 ); 0231: 0232: // モデルの読み込み 0233: if ( FAILED(hr = LoadXFile("nsx.x", lpD3DDev)) ) return hr; 0234: 0235: // バーテックスシェーダーを作成する 0236: if ( FAILED(CVertexShaderMgr::Load(lpD3DDev, "vs.vsh", &hVertexShader, dwDecl)) ) return hr; 0237: if ( FAILED( CPixelShaderMgr::Load(lpD3DDev, "ps.psh", &hPixelShader)) ) return hr; 0238: 0239: // 描画するテクスチャーの下準備 0240: InitRenderingTexture(lpD3DDev); 0241: 0242: // 背景部分の初期化 0243: InitBg(lpD3DDev); 0244: 0245: // 不変なレジスタの設定 0246: lpD3DDev->SetRenderState( D3DRS_ZENABLE, TRUE ); 0247: lpD3DDev->SetRenderState( D3DRS_LIGHTING, FALSE ); 0248: lpD3DDev->SetTextureStageState(1,D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); 0249: lpD3DDev->SetTextureStageState(1,D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); 0250: lpD3DDev->SetVertexShaderConstant(12, &D3DXVECTOR4(0.0f, 0.5f, 1.0f, 2.0f), 1); 0251: 0252: return S_OK; 0253: } 0254: //----------------------------------------------------------------------------- 0255: // Name: Render() 0256: // Desc: Draws the scene 0257: //----------------------------------------------------------------------------- 0258: D3DXVECTOR4 lightDir(0.666f, 0.666f, 0.333f, 0.0f); 0259: 0260: VOID DrawModel(LPDIRECT3DDEVICE8 lpD3DDev, bool bShadowMap, bool bBg) 0261: { 0262: D3DXMATRIX mWorld, mView, mProj, mScale, mProjMap, mVPm, mViewI, mSign, mLightView, mProjL, mShift; 0263: D3DXMATRIX mVP, mVPI, mLP, m; 0264: D3DXVECTOR4 vl; 0265: DWORD i; 0266: float q, qz; 0267: 0268: // ビュー行列 0269: D3DXVECTOR4 eye = D3DXVECTOR4(0.0f,1.4f*MeshRadius,2.5f*MeshRadius,1); 0270: D3DXMatrixLookAtLH(&mView, (D3DXVECTOR3*)&eye 0271: , &D3DXVECTOR3(0,0,0) 0272: , &D3DXVECTOR3(0,1,0)); 0273: 0274: // 射影行列 0275: const float z_min = 0.01f; 0276: const float z_max = 100.0f; 0277: D3DXMatrixPerspectiveFovLH(&mProj 0278: ,60.0f*PI/180.0f // 視野角 0279: ,(float)WIDTH/(float)HEIGHT // アスペクト比 0280: ,z_min, z_max // 最近接距離,最遠方距離 0281: ); 0282: mVP = mView * mProj; 0283: 0284: // 影マップに関する射影(含む適当なスケーリング) 0285: D3DXMatrixScaling(&mScale 0286: ,mView.m[3][2]/mProj.m[0][0] 0287: ,mView.m[3][2]/mProj.m[1][1] 0288: ,100.0f*mView.m[3][2]*mView.m[3][2]); 0289: q = 1.0f/(z_max-z_min); 0290: float d = D3DXVec3Length((D3DXVECTOR3*)&eye); 0291: D3DXMatrixScaling(&mScale, d, d, 10000.0f*d*d); 0292: mProjMap = D3DXMATRIX(1,0,0,0, 0293: 0,1,0,0, 0294: 0,0,q,1, 0295: 0,0,-q*z_min,0) * mScale; 0296: mVPm = mView * mProjMap; 0297: 0298: // 射影空間を(ゆがんだ)ワールド座標に戻すための逆ビュー行列 0299: D3DXVECTOR4 cent = D3DXVECTOR4(0,0,0,1); 0300: D3DXVECTOR4 zdir = D3DXVECTOR4(0,0,1,1); 0301: D3DXVECTOR4 udir = D3DXVECTOR4(0,1,0,1); 0302: D3DXVec4Transform(¢, ¢, &mVPm); D3DXVec4Scale(¢, ¢, 1.0f/cent.w); 0303: D3DXVec4Transform(&zdir, &zdir, &mVPm); D3DXVec4Scale(&zdir, &zdir, 1.0f/zdir.w); 0304: D3DXVec4Transform(&udir, &udir, &mVPm); D3DXVec4Scale(&udir, &udir, 1.0f/udir.w); 0305: D3DXMatrixLookAtLH(&mViewI, (D3DXVECTOR3*)¢, (D3DXVECTOR3*)&zdir, (D3DXVECTOR3*)&udir); 0306: 0307: // 逆ビュー行列による符号の変換を元に戻す 0308: D3DXMatrixScaling(&mSign,-1, -1, 1); 0309: mVPI = mView * mProjMap * mViewI * mSign; 0310: 0311: // ライト方向からのビュー行列 0312: const float zoom =30.f; 0313: D3DXVECTOR4 eyeP; 0314: D3DXVec3Transform(&eyeP, &D3DXVECTOR3(zoom*lightDir.x, zoom*lightDir.y, zoom*lightDir.z), &mVPI); 0315: D3DXVec4Scale(&eyeP, &eyeP, 1.0f/eyeP.w); 0316: D3DXMatrixLookAtLH(&mLightView, (D3DXVECTOR3*)&eyeP, &D3DXVECTOR3(0,0,0), &D3DXVECTOR3(0,1,0)); 0317: 0318: // ライト方向に関する射影行列 0319: const float lz_min = 20.0f; 0320: const float lz_max = 300.0f; 0321: D3DXMatrixPerspectiveFovLH(&mProjL 0322: ,60.0f*PI/180.0f // 視野角 0323: ,1 // アスペクト比 0324: ,lz_min, lz_max // 最近接距離,最遠方距離 0325: ); 0326: 0327: // 中心をずらして、影マップを有効に使う 0328: D3DXMatrixTranslation(&mShift, -0.5f, 0.5f, 0); 0329: mLP =mVPI * mLightView * mProjL * mShift; 0330: 0331: // z値を0.0fから1.0fに補正する定数 0332: q = 0.07f /(lz_max-lz_min); 0333: qz = 0.07f*lz_min/(lz_max-lz_min); 0334: lpD3DDev->SetVertexShaderConstant(15, &D3DXVECTOR4(q,-qz, 0.0f, 0.0f), 1); 0335: 0336: // 0337: // 逆カリング処理 0338: // 0339: if(bShadowMap){ 0340: lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); 0341: }else{ 0342: lpD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); 0343: } 0344: 0345: // 0346: // 背景描画 0347: // 0348: if(bBg){ 0349: D3DXMatrixScaling(&mWorld, 3.0f, 3.0f, 3.0f); 0350: 0351: m = mWorld * mVP; 0352: D3DXMatrixTranspose( &m , &m); 0353: lpD3DDev->SetVertexShaderConstant(0,&m, 4); 0354: m = mWorld * mLP; 0355: D3DXMatrixTranspose( &m , &m); 0356: lpD3DDev->SetVertexShaderConstant(4,&m, 4); 0357: 0358: D3DXMatrixInverse( &m, NULL, &mWorld); 0359: D3DXVec4Transform(&vl, &lightDir, &m); 0360: D3DXVec4Normalize(&vl, &vl); 0361: vl[3] = 0.3f; 0362: lpD3DDev->SetVertexShaderConstant(13, &vl, 1); 0363: 0364: lpD3DDev->SetVertexShaderConstant(14, &D3DXVECTOR4(0.0f, 0.0f, 0.0f, 1.0f), 1); 0365: DrawBg(lpD3DDev); 0366: } 0367: 0368: // 0369: // 外側もでる描画 0370: // 0371: int t = timeGetTime() & 0xffffff; 0372: D3DXMatrixTranslation(&m, 1.5f*MeshRadius, 0,0); 0373: D3DXMatrixRotationY( &mWorld, t/1000.0f ); 0374: //D3DXMatrixRotationY( &mWorld, -0.40*PI/2 ); // 撮影アングル 0375: mWorld = m * mWorld; 0376: 0377: m = mWorld * mVP; 0378: D3DXMatrixTranspose( &m , &m); 0379: lpD3DDev->SetVertexShaderConstant(0,&m, 4); 0380: m = mWorld * mLP; 0381: D3DXMatrixTranspose( &m , &m); 0382: lpD3DDev->SetVertexShaderConstant(4,&m, 4); 0383: 0384: D3DXMatrixInverse( &m, NULL, &mWorld); 0385: D3DXVec4Transform(&vl, &lightDir, &m); 0386: D3DXVec4Normalize(&vl, &vl); 0387: vl[3] = 0.3f; 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: 0414: m = mWorld * mVP; 0415: D3DXMatrixTranspose( &m , &m); 0416: lpD3DDev->SetVertexShaderConstant(0,&m, 4); 0417: m = mWorld * mLP; 0418: D3DXMatrixTranspose( &m , &m); 0419: lpD3DDev->SetVertexShaderConstant(4,&m, 4); 0420: 0421: D3DXMatrixInverse( &m, NULL, &mWorld); 0422: D3DXVec4Transform(&vl, &lightDir, &m); 0423: D3DXVec4Normalize(&vl, &vl); 0424: vl[3] = 0.3f; 0425: lpD3DDev->SetVertexShaderConstant(13, &vl, 1); 0426: 0427: for(i=0;i<dwNumMaterials;i++){ 0428: //色をセット 0429: D3DXVECTOR4 vl; 0430: vl.x = pMeshMaterials[i].Diffuse.r; 0431: vl.y = pMeshMaterials[i].Diffuse.g; 0432: vl.z = pMeshMaterials[i].Diffuse.b; 0433: lpD3DDev->SetVertexShaderConstant(14, &vl, 1); 0434: 0435: lpD3DDev->SetTexture(0,pMeshTextures[i]); 0436: lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0437: pSubsetTable[i].VertexStart, 0438: pSubsetTable[i].VertexCount, 0439: pSubsetTable[i].FaceStart * 3, 0440: pSubsetTable[i].FaceCount); 0441: } 0442: } 0443: //----------------------------------------------------------------------------- 0444: // Name: Render() 0445: // Desc: Draws the scene 0446: //----------------------------------------------------------------------------- 0447: VOID Render(LPDIRECT3DDEVICE8 lpD3DDev) 0448: { 0449: D3DXMATRIX mWorld, mView, mLightView, m; 0450: 0451: LPDIRECT3DSURFACE8 lpZbuffer = NULL; 0452: lpD3DDev->GetDepthStencilSurface( &lpZbuffer ); 0453: lpD3DDev->SetRenderTarget(pTextureSurface, lpZbuffer); 0454: lpD3DDev->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0),1.0f,0); 0455: 0456: // シャドウマップの作成 0457: lpD3DDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); 0458: lpD3DDev->SetVertexShader(hVertexShader); 0459: lpD3DDev->SetPixelShader(hPixelShader); 0460: DrawModel(lpD3DDev, 1, 0); 0461: 0462: // 描画をバックバッファに戻す 0463: lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer ); 0464: lpD3DDev->Clear(0,NULL,D3DCLEAR_ZBUFFER, 0,1.0f,0); 0465: 0466: // 比較しながらの描画 0467: lpD3DDev->SetVertexShader(hShadowVertexShader); 0468: lpD3DDev->SetPixelShader(hShadowPixelShader); 0469: lpD3DDev->SetTexture( 1, pTexture ); // 元テクスチャー 0470: DrawModel(lpD3DDev, 0, 1); 0471: 0472: lpD3DDev->SetPixelShader(0); 0473: #if 1 0474: // 描画した画面をテクスチャーとして描く 0475: { 0476: typedef struct { 0477: float x,y,z,rhw; 0478: float tu,tv; 0479: } TLVERTEX; 0480: #define FVF_TLVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1) 0481: lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1); 0482: lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE); 0483: const float size = 128.0f; 0484: TLVERTEX Vertex[4] = { 0485: // x y z rhw tu tv 0486: { 0, 0,0, 1, 0, 0,}, 0487: {size, 0,0, 1, 1, 0,}, 0488: {size,size,0, 1, 1, 1,}, 0489: { 0,size,0, 1, 0, 1,}, 0490: }; 0491: lpD3DDev->SetTexture( 0, pTexture ); 0492: lpD3DDev->SetVertexShader( FVF_TLVERTEX ); 0493: lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TLVERTEX ) ); 0494: } 0495: #endif 0496: } 0497: //----------------------------------------------------------------------------- 0498: // メッシュオブジェクト削除 0499: //----------------------------------------------------------------------------- 0500: void DeleteMeshObject(void) 0501: { 0502: DWORD i; 0503: 0504: if(pMeshVB == NULL) return; 0505: 0506: for(i=0; i<dwNumMaterials; i++){ 0507: RELEASE(pMeshTextures[i]); 0508: } 0509: delete[] pMeshTextures; 0510: delete[] pMeshMaterials; 0511: delete[] pSubsetTable; 0512: 0513: RELEASE(pMeshVB); 0514: RELEASE(pMeshIndex); 0515: } 0516: //----------------------------------------------------------------------------- 0517: // Name: CleanRender() 0518: // Desc: 後始末 0519: //----------------------------------------------------------------------------- 0520: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDev) 0521: { 0522: CleanBg(lpD3DDev); 0523: 0524: RELEASE(pTextureSurface); 0525: RELEASE(pTexture); 0526: RELEASE(pBackbuffer); 0527: 0528: CPixelShaderMgr::Release(lpD3DDev, &hShadowPixelShader); 0529: CVertexShaderMgr::Release(lpD3DDev, &hShadowVertexShader); 0530: CPixelShaderMgr::Release(lpD3DDev, &hPixelShader); 0531: CVertexShaderMgr::Release(lpD3DDev, &hVertexShader); 0532: 0533: DeleteMeshObject(); 0534: } 0535: