0001: // ----------------------------------------------------------------------------
0002: //
0003: // draw.cpp - 描画部分
0004: // 
0005: // Copyright (c) 2002 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: float IdMgr::id;
0017: 
0018: 
0019: // モデル用
0020: LPDIRECT3DVERTEXBUFFER8 pMeshVB = NULL;
0021: LPDIRECT3DINDEXBUFFER8  pMeshIndex = NULL;
0022: D3DXATTRIBUTERANGE      *pSubsetTable = NULL;
0023: DWORD                   nMeshFaces = 0;
0024: DWORD                   nMeshVertices = 0;
0025: D3DMATERIAL8            *pMeshMaterials = NULL;     // メッシュの質感
0026: LPDIRECT3DTEXTURE8      *pMeshTextures  = NULL;     // メッシュのテクスチャー
0027: DWORD                   dwNumMaterials = 0L;        // マテリアルの数
0028: FLOAT                   MeshRadius;                 // メッシュの大きさ
0029: 
0030: // レンダリングテクスチャーの設定
0031: DWORD                   hVertexShaderNormal=~0;
0032: DWORD                   hPixelShaderNormal=~0;
0033: DWORD                   hVertexShaderNormalEdge=~0;
0034: DWORD                   hPixelShaderNormalEdge=~0;
0035: 
0036: LPDIRECT3DTEXTURE8      pTexture;
0037: LPDIRECT3DSURFACE8      pTextureSurface;
0038: LPDIRECT3DSURFACE8      pBackbuffer = NULL;
0039: DWORD                   hVertexShader=~0;
0040: DWORD                   hPixelShader=~0;
0041: 
0042: 
0043: // 最後の一枚絵の描画用
0044: DWORD                   hFinalVertexShader=~0;
0045: DWORD                   hFinalPixelShader=~0;
0046: LPDIRECT3DVERTEXBUFFER8 pFinalVB = NULL;
0047: LPDIRECT3DINDEXBUFFER8  pFinalIB = NULL;
0048: 
0049: typedef struct {
0050:     float x,y,z;
0051:     float tu,tv;
0052: }D3D_FINAL_VERTEX;
0053: #define D3DFVF_FINAL_VERTEX         (D3DFVF_XYZ | D3DFVF_TEX1)
0054: 
0055: DWORD dwFinalDecl[] = {
0056:     D3DVSD_STREAM(0),
0057:     D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3 ),          //D3DVSDE_POSITION,  0
0058:     D3DVSD_REG(D3DVSDE_TEXCOORD0,D3DVSDT_FLOAT2 ),          //D3DVSDE_TEXCOORD0, 7  
0059:     D3DVSD_END()
0060: };
0061: 
0062: 
0063: // ----------------------------------------------------------------------------
0064: // 外部関数
0065: void InitBg(LPDIRECT3DDEVICE8 lpD3DDev);
0066: void DrawBg(LPDIRECT3DDEVICE8 lpD3DDev);
0067: void CleanBg(LPDIRECT3DDEVICE8 lpD3DDev);
0068: 
0069: 
0070: 
0071: // ----------------------------------------------------------------------------
0072: // Name: LoadXFile(char* filename, LPDIRECT3DDEVICE8 lpD3DDev)
0073: // Desc: X-Fileの読み込み
0074: //-----------------------------------------------------------------------------
0075: HRESULT LoadXFile(char* filename, LPDIRECT3DDEVICE8 lpD3DDev)
0076: {
0077:     LPD3DXMESH pMesh, pMeshOpt;
0078:     LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
0079:     DWORD i;
0080:     HRESULT hr;
0081: 
0082:     hr = D3DXLoadMeshFromX(filename, D3DXMESH_MANAGED,
0083:                                 lpD3DDev, NULL,
0084:                                 &pD3DXMtrlBuffer, &dwNumMaterials,
0085:                                 &pMesh);
0086:     if(FAILED(hr)) return E_FAIL;
0087: 
0088:     //並び替えておく
0089:     pMesh->Optimize(D3DXMESHOPT_ATTRSORT, NULL, NULL, NULL, NULL, &pMeshOpt);
0090:     RELEASE(pMesh);
0091: 
0092:     //アトリビュートテーブル
0093:     pMeshOpt->GetAttributeTable(NULL,&dwNumMaterials);
0094:     pSubsetTable = new D3DXATTRIBUTERANGE[dwNumMaterials];
0095:     pMeshOpt->GetAttributeTable(pSubsetTable, &dwNumMaterials);
0096: 
0097:     // FVF変換
0098:     hr = pMeshOpt->CloneMeshFVF(pMeshOpt->GetOptions(), D3DFVF_VERTEX, lpD3DDev, &pMesh);
0099:     if(FAILED(hr)) return E_FAIL;
0100:     RELEASE(pMeshOpt);
0101:     D3DXComputeNormals(pMesh, NULL);
0102: 
0103:     //Vertex Bufferにコピーする
0104:     D3DVERTEX* pSrc;
0105:     D3D_CUSTOMVERTEX* pDest;
0106:     LPDIRECT3DINDEXBUFFER8 pSrcIndex;
0107:     WORD* pISrc;
0108:     WORD* pIDest;
0109: 
0110:     DWORD nMeshVertices = pMesh->GetNumVertices();
0111:     DWORD nMeshFaces = pMesh->GetNumFaces();
0112:     lpD3DDev->CreateVertexBuffer(nMeshVertices * sizeof(D3D_CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pMeshVB);
0113:     lpD3DDev->CreateIndexBuffer(nMeshFaces * 3 * sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&pMeshIndex);
0114: 
0115:     LPDIRECT3DVERTEXBUFFER8 pVB;
0116:     pMesh->GetVertexBuffer(&pVB);
0117:     pVB->Lock(0,0,(BYTE**)&pSrc,0);
0118:     pMeshVB->Lock(0,0,(BYTE**)&pDest,0);
0119:     MeshRadius = 0.0f;
0120:     for(i=0;i<nMeshVertices;i++){
0121:         pDest->x = pSrc->x;
0122:         pDest->y = pSrc->y;
0123:         pDest->z = pSrc->z;
0124:         pDest->nx = pSrc->nx;
0125:         pDest->ny = pSrc->ny;
0126:         pDest->nz = pSrc->nz;
0127:         pDest->tu0 = pSrc->tu0;
0128:         pDest->tu0 = pSrc->tu0;
0129:         // サイズの計算
0130:         FLOAT radius = sqrtf( pSrc->x*pSrc->x + pSrc->y*pSrc->y + pSrc->z*pSrc->z );
0131:         if (MeshRadius < radius) MeshRadius = radius;
0132:         
0133:         pSrc += 1;
0134:         pDest += 1;
0135:     }
0136:     pVB->Unlock();
0137:     pVB->Release();
0138:     pMeshVB->Unlock();
0139: 
0140:     //インデックスのコピー
0141:     pMesh->GetIndexBuffer(&pSrcIndex);
0142:     pSrcIndex->Lock(0,0,(BYTE**)&pISrc,0);
0143:     pMeshIndex->Lock(0,0,(BYTE**)&pIDest,0);
0144:     CopyMemory(pIDest,pISrc,nMeshFaces * 3 * sizeof(WORD));
0145:     pSrcIndex->Unlock();
0146:     pMeshIndex->Unlock();
0147:     pSrcIndex->Release();
0148: 
0149:     // pD3DXMtrlBuffer から、質感やテクスチャーの情報を読み取る
0150:     D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
0151:     pMeshTextures = new LPDIRECT3DTEXTURE8[dwNumMaterials];
0152:     pMeshMaterials = new D3DMATERIAL8[dwNumMaterials];
0153: 
0154:     for(i = 0; i < dwNumMaterials; i++){
0155:         pMeshMaterials[i] = d3dxMaterials[i].MatD3D;
0156:         pMeshMaterials[i].Ambient = pMeshMaterials[i].Diffuse;
0157:         hr = D3DXCreateTextureFromFile( lpD3DDev, 
0158:                                         d3dxMaterials[i].pTextureFilename, 
0159:                                         &pMeshTextures[i] );
0160:         if(FAILED(hr)) pMeshTextures[i] = NULL;
0161:     }
0162:     RELEASE(pD3DXMtrlBuffer);
0163:     
0164:     RELEASE(pMesh);
0165: 
0166:     return S_OK;
0167: }
0168: //-----------------------------------------------------------------------------
0169: // Name: InitRenderTexture()
0170: // Desc: レンダリングテクスチャー用の下準備
0171: //-----------------------------------------------------------------------------
0172: HRESULT InitRenderTexture(LPDIRECT3DDEVICE8 lpD3DDev)
0173: {
0174:     HRESULT hr;
0175:     DWORD i;
0176:     
0177:     // バーテックスシェーダーを作成する
0178:     hr=CVertexShaderMgr::Load(lpD3DDev, "normal.vsh", &hVertexShaderNormal, dwDecl);
0179:     if( FAILED(hr) ) return hr;
0180:     hr=CPixelShaderMgr::Load(lpD3DDev, "normal.psh", &hPixelShaderNormal);
0181:     if( FAILED(hr) ) return hr;
0182:     hr=CVertexShaderMgr::Load(lpD3DDev, "normaledge.vsh", &hVertexShaderNormalEdge, dwFinalDecl);
0183:     if( FAILED(hr) ) return hr;
0184:     hr=CPixelShaderMgr::Load(lpD3DDev, "normaledge.psh", &hPixelShaderNormalEdge);
0185:     if( FAILED(hr) ) return hr;
0186:     hr=CVertexShaderMgr::Load(lpD3DDev, "vs.vsh", &hVertexShader, dwDecl);
0187:     if( FAILED(hr) ) return hr;
0188:     hr=CPixelShaderMgr::Load(lpD3DDev, "ps.psh", &hPixelShader);
0189:     if( FAILED(hr) ) return hr;
0190:     hr=CVertexShaderMgr::Load(lpD3DDev, "final.vsh", &hFinalVertexShader, dwFinalDecl);
0191:     if( FAILED(hr) ) return hr;
0192:     hr=CPixelShaderMgr::Load(lpD3DDev, "final.psh", &hFinalPixelShader);
0193:     if( FAILED(hr) ) return hr;
0194:     
0195:     // 頂点バッファの作成 
0196:     D3D_FINAL_VERTEX *pDest;
0197:     WORD *pIndex;
0198:     hr = lpD3DDev->CreateVertexBuffer( 4 * sizeof(D3D_FINAL_VERTEX),
0199:                                 D3DUSAGE_WRITEONLY, D3DFVF_FINAL_VERTEX, D3DPOOL_MANAGED,
0200:                                 &pFinalVB );
0201:     // 頂点をセットアップ
0202:     pFinalVB->Lock ( 0, 0, (BYTE**)&pDest, 0 );
0203:     for (i = 0; i < 4; i++) {
0204:         pDest->x   = (i == 0 || i == 1)?-1:(float)1;
0205:         pDest->y   = (i == 0 || i == 2)?-1:(float)1;
0206:         pDest->z   = 0.5f;
0207:         pDest->tu = (i == 2 || i == 3)?1:(float)0;
0208:         pDest->tv = (i == 0 || i == 2)?1:(float)0;
0209:         pDest++;
0210:     }       
0211:     pFinalVB->Unlock ();
0212:     // インデックスをセットアップ
0213:     hr = lpD3DDev->CreateIndexBuffer( 6 * sizeof(WORD),
0214:                                0,
0215:                                D3DFMT_INDEX16, D3DPOOL_MANAGED,
0216:                                &pFinalIB );
0217:     pFinalIB->Lock ( 0, 0, (BYTE**)&pIndex, 0 );
0218:     pIndex[0] = 0;  pIndex[1] = 1;  pIndex[2] = 2;
0219:     pIndex[3] = 1;  pIndex[4] = 3;  pIndex[5] = 2;
0220:     pFinalIB->Unlock ();
0221:     
0222:     // -------------------------------------------------------------------------
0223:     // 定数レジスタの設定
0224:     // -------------------------------------------------------------------------
0225:     float const inv_w = 1.0f / (float)WIDTH;
0226:     float const inv_h = 1.0f / (float)HEIGHT;
0227:     lpD3DDev->SetVertexShaderConstant(20, &D3DXVECTOR4  (-0.5f*inv_w, -0.5f*inv_h, 0.0f, 0.0f), 1);
0228:     lpD3DDev->SetVertexShaderConstant(21, &D3DXVECTOR4  (+0.5f*inv_w, -0.5f*inv_h, 0.0f, 0.0f), 1);
0229:     lpD3DDev->SetVertexShaderConstant(22, &D3DXVECTOR4  (-0.5f*inv_w, +0.5f*inv_h, 0.0f, 0.0f), 1);
0230:     lpD3DDev->SetVertexShaderConstant(23, &D3DXVECTOR4  (+0.5f*inv_w, +0.5f*inv_h, 0.0f, 0.0f), 1);
0231:     // 便利な定数
0232:     lpD3DDev->SetVertexShaderConstant(12, &D3DXVECTOR4  (0.0f, 0.5f, 1.0f, 2.0f), 1);
0233: 
0234:     // z値を0.0fから1.0fに補正する定数
0235:     float lz_min = 15.0f;
0236:     float lz_max = 50.0f;
0237:     float q  = lz_max       /(lz_max-lz_min);
0238:     float qz = lz_max*lz_min/(lz_max-lz_min);
0239:     lpD3DDev->SetVertexShaderConstant(15, &D3DXVECTOR4(q,-qz, 0.0f, 0.0f), 1);
0240: 
0241:     // -------------------------------------------------------------------------
0242:     // 描画用テクスチャーを用意する
0243:     // -------------------------------------------------------------------------
0244:     D3DSURFACE_DESC Desc;
0245:     LPDIRECT3DSURFACE8 lpZbuffer = NULL;
0246:     if( FAILED(hr = lpD3DDev->GetRenderTarget(&pBackbuffer))) return hr;
0247:     if( FAILED(hr = pBackbuffer->GetDesc( &Desc ))) return hr;
0248: 
0249:     // 深度バッファのサーフェスを確保する
0250:     if( FAILED(hr = lpD3DDev->GetDepthStencilSurface( &lpZbuffer ))) return hr;
0251:     
0252:     // テクスチャーの生成
0253:     if( FAILED(hr = lpD3DDev->CreateTexture(Desc.Width, Desc.Height, 1
0254:                             , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8
0255:                             , D3DPOOL_DEFAULT, &pTexture))) return hr;
0256:     // テクスチャーとサーフェスを関連づける
0257:     if( FAILED(hr = pTexture->GetSurfaceLevel(0,&pTextureSurface))) return hr;
0258:     // テクスチャー用の描画と深度バッファを関連付ける
0259:     if( FAILED(hr = lpD3DDev->SetRenderTarget(pTextureSurface, lpZbuffer)))
0260:         return hr;
0261:     
0262:     // 描画を元の画面に戻す
0263:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );
0264:     
0265:     return S_OK;
0266: }
0267: //-----------------------------------------------------------------------------
0268: // Name: InitRender()
0269: // Desc: Load the mesh and build the material and texture arrays
0270: //-----------------------------------------------------------------------------
0271: D3DXVECTOR4 lightDir(1.0f, 1.0f, 0.5f, 0.0f);
0272: 
0273: HRESULT InitRender(LPDIRECT3DDEVICE8 lpD3DDev)
0274: {
0275:     HRESULT hr;
0276: 
0277:     // モデルの読み込み
0278:     if ( FAILED(hr = LoadXFile("nsx.x", lpD3DDev)) ) return hr;
0279:     
0280:     // レンダリングテクスチャー用の初期化
0281:     InitRenderTexture(lpD3DDev);
0282:     
0283:     // 背景部分の初期化
0284:     InitBg(lpD3DDev);
0285: 
0286:     // 不変なレジスタの設定
0287:     lpD3DDev->SetRenderState( D3DRS_ZENABLE, TRUE );
0288:     for (DWORD i = 0; i < 4; ++i) {
0289:         lpD3DDev->SetTextureStageState(i, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
0290:         lpD3DDev->SetTextureStageState(i, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
0291:         lpD3DDev->SetTextureStageState(i, D3DTSS_MIPFILTER, D3DTEXF_NONE);
0292:         lpD3DDev->SetTextureStageState(i,D3DTSS_ADDRESSU,   D3DTADDRESS_CLAMP);
0293:         lpD3DDev->SetTextureStageState(i,D3DTSS_ADDRESSV,   D3DTADDRESS_CLAMP);
0294:     }
0295:     
0296:     // 光源の設定
0297:     D3DLIGHT8 light;
0298:     ZeroMemory( &light, sizeof(D3DLIGHT8) );
0299:     light.Type       = D3DLIGHT_DIRECTIONAL;
0300:     light.Diffuse.r  = 1.0f;
0301:     light.Diffuse.g  = 1.0f;
0302:     light.Diffuse.b  = 1.0f;
0303:     D3DXVECTOR3 vecDir = D3DXVECTOR3(-lightDir.x, -lightDir.y, lightDir.z);
0304:     D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );
0305:     lpD3DDev->SetLight( 0, &light );
0306:     lpD3DDev->LightEnable( 0, TRUE );
0307:     lpD3DDev->SetRenderState( D3DRS_AMBIENT, 0x00404040 );
0308: 
0309:     return S_OK;
0310: }
0311: //-----------------------------------------------------------------------------
0312: // Name: DrawModel()
0313: // Desc: Draw Models
0314: //-----------------------------------------------------------------------------
0315: 
0316: VOID DrawModel(LPDIRECT3DDEVICE8 lpD3DDev)
0317: {
0318:     D3DXMATRIX mWorld, mView, mProj, m;
0319:     D3DXVECTOR4 vl;
0320:     DWORD i;
0321: 
0322:     // ビュー行列
0323:     D3DXVECTOR3 eye    = D3DXVECTOR3(0.0f,18,30);
0324:     D3DXVECTOR3 lookAt = D3DXVECTOR3(0.0f,  0.0f,  0.0f);
0325:     D3DXVECTOR3 up     = D3DXVECTOR3(0.0f,  1.0f,  0.0f);
0326:     D3DXMatrixLookAtLH(&mView, &eye, &lookAt, &up);
0327:     
0328:     // 射影行列
0329:     D3DXMatrixPerspectiveFovLH(&mProj
0330:         ,60.0f*PI/180.0f                        // 視野角
0331:         ,(float)WIDTH/(float)HEIGHT             // アスペクト比
0332:         ,0.01f,100.0f                           // 最近接距離,最遠方距離
0333:         );
0334: 
0335:     //
0336:     // 背景描画
0337:     // 
0338:     D3DXMatrixScaling(&mWorld, 3.0f, 3.0f, 3.0f);
0339:     m = mWorld * mView * mProj;
0340:     D3DXMatrixTranspose( &m ,  &m);
0341:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0342:     lpD3DDev->SetVertexShaderConstant(14, &D3DXVECTOR4(0,0,0,0), 1);
0343:     
0344:     D3DXMatrixInverse( &m,  NULL, &mWorld);
0345:     D3DXVec4Transform(&vl, &lightDir, &m);
0346:     D3DXVec4Normalize(&vl, &vl);
0347:     vl[3] = 0.3f;
0348:     lpD3DDev->SetVertexShaderConstant(13, &vl, 1);
0349: 
0350:     DrawBg(lpD3DDev);
0351: 
0352:     //
0353:     // 外側もでる描画
0354:     //
0355:     lpD3DDev->SetRenderState( D3DRS_LIGHTING, TRUE );
0356: 
0357:     lpD3DDev->SetStreamSource(0, pMeshVB, sizeof(D3D_CUSTOMVERTEX));
0358:     lpD3DDev->SetIndices(pMeshIndex,0);
0359: 
0360:     int t = timeGetTime();
0361:     D3DXMatrixTranslation(&m, 1.5f*MeshRadius, 0,0);
0362:     D3DXMatrixRotationY( &mWorld, t/1000.0f );
0363: //D3DXMatrixRotationY( &mWorld, -0.2*PI/2 );
0364:     mWorld = m * mWorld;
0365:     m = mWorld * mView * mProj;
0366:     D3DXMatrixTranspose( &m ,  &m);
0367:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0368:     
0369:     D3DXMatrixInverse( &m,  NULL, &mWorld);
0370:     D3DXVec4Transform(&vl, &lightDir, &m);
0371:     D3DXVec4Normalize(&vl, &vl);
0372:     vl[3] = 0.3f;
0373:     lpD3DDev->SetVertexShaderConstant(13, &vl, 1);
0374:     
0375:     IdMgr::Reset();
0376:     for(i=0;i<dwNumMaterials;i++){
0377:         D3DXVECTOR4 vl;
0378:         vl.x = pMeshMaterials[i].Diffuse.r;
0379:         vl.y = pMeshMaterials[i].Diffuse.g;
0380:         vl.z = pMeshMaterials[i].Diffuse.b;
0381:         lpD3DDev->SetVertexShaderConstant(14, &vl, 1);
0382:         lpD3DDev->SetVertexShaderConstant(16, &D3DXVECTOR4(0,0,0,IdMgr::GetId()), 1);
0383: 
0384:         lpD3DDev->SetMaterial( &pMeshMaterials[i] );
0385:         lpD3DDev->SetTexture(0,pMeshTextures[i]);
0386:         lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 
0387:                                         pSubsetTable[i].VertexStart,
0388:                                         pSubsetTable[i].VertexCount,
0389:                                         pSubsetTable[i].FaceStart * 3,
0390:                                         pSubsetTable[i].FaceCount);
0391:     }
0392:     //
0393:     // 内側もでる描画
0394:     //
0395:     D3DXMatrixRotationY( &mWorld, -t/3000.0f );
0396: //D3DXMatrixRotationY( &mWorld, -1.4f*PI/2 );
0397:     m = mWorld * mView * mProj;
0398:     D3DXMatrixTranspose( &m ,  &m);
0399:     lpD3DDev->SetVertexShaderConstant(0,&m, 4);
0400: 
0401:     D3DXMatrixInverse( &m,  NULL, &mWorld);
0402:     D3DXVec4Transform(&vl, &lightDir, &m);
0403:     D3DXVec4Normalize(&vl, &vl);
0404:     vl[3] = 0.3f;
0405:     lpD3DDev->SetVertexShaderConstant(13, &vl, 1);
0406: 
0407:     IdMgr::Reset();
0408:     for(i=0;i<dwNumMaterials;i++){
0409:         D3DXVECTOR4 vl;
0410:         vl.x = pMeshMaterials[i].Diffuse.r;
0411:         vl.y = pMeshMaterials[i].Diffuse.g;
0412:         vl.z = pMeshMaterials[i].Diffuse.b;
0413:         lpD3DDev->SetVertexShaderConstant(14, &vl, 1);
0414:         lpD3DDev->SetVertexShaderConstant(16, &D3DXVECTOR4(0,0,0,IdMgr::GetId()), 1);
0415: 
0416:         lpD3DDev->SetMaterial( &pMeshMaterials[i] );
0417:         lpD3DDev->SetTexture(0,pMeshTextures[i]);
0418:         lpD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 
0419:                                         pSubsetTable[i].VertexStart,
0420:                                         pSubsetTable[i].VertexCount,
0421:                                         pSubsetTable[i].FaceStart * 3,
0422:                                         pSubsetTable[i].FaceCount);
0423:     }
0424:     lpD3DDev->SetRenderState( D3DRS_LIGHTING, FALSE );
0425: }
0426: //-----------------------------------------------------------------------------
0427: // Name: Render()
0428: // Desc: Draw the scene
0429: //-----------------------------------------------------------------------------
0430: VOID Render(LPDIRECT3DDEVICE8 lpD3DDev)
0431: {
0432:     DWORD i;
0433:     LPDIRECT3DSURFACE8 lpZbuffer = NULL;
0434:     
0435:     IdMgr::Reset();
0436: 
0437:     lpD3DDev->GetDepthStencilSurface( &lpZbuffer );
0438:     // -----------------------------------------------------
0439:     // 法線エッジの作成
0440:     // -----------------------------------------------------
0441:     // テクスチャーに描画
0442:     lpD3DDev->SetRenderTarget(pTextureSurface, lpZbuffer);
0443:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0444:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_DIFFUSE);
0445:     lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0446:     lpD3DDev->SetVertexShader(hVertexShaderNormal);
0447:     lpD3DDev->SetPixelShader(hPixelShaderNormal);
0448:     DrawModel(lpD3DDev);
0449: 
0450:     // 描画をバックバッファに戻す
0451:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );
0452:     for (i = 0; i < 4; i++) {
0453:         lpD3DDev->SetTextureStageState(i, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP);
0454:         lpD3DDev->SetTextureStageState(i, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP);
0455:         lpD3DDev->SetTextureStageState(i, D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
0456:         lpD3DDev->SetTextureStageState(i, D3DTSS_COLORARG1, D3DTA_TEXTURE);
0457:         lpD3DDev->SetTexture( i, pTexture );                    // テクスチャー
0458:     }
0459: 
0460:     lpD3DDev->SetVertexShader(hVertexShaderNormalEdge);
0461:     lpD3DDev->SetPixelShader(hPixelShaderNormalEdge);
0462:     lpD3DDev->SetStreamSource( 0, pFinalVB, sizeof(D3D_FINAL_VERTEX) );
0463:     lpD3DDev->SetIndices(pFinalIB,0);
0464:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 4, 0, 2 );
0465:     
0466:     lpD3DDev->SetTexture( 0, NULL);
0467:     lpD3DDev->SetTexture( 1, NULL);
0468:     lpD3DDev->SetTexture( 2, NULL);
0469:     lpD3DDev->SetTexture( 3, NULL);
0470:     // -----------------------------------------------------
0471:     // 深度エッジと色の作成
0472:     // -----------------------------------------------------
0473:     // テクスチャーに描画
0474:     lpD3DDev->SetRenderTarget(pTextureSurface, lpZbuffer);
0475:     lpD3DDev->Clear(0,NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0),1.0f,0);
0476:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_MODULATE);
0477:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0478:     lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG2,  D3DTA_DIFFUSE);
0479:     lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0480:     lpD3DDev->SetVertexShader(hVertexShader);
0481:     lpD3DDev->SetPixelShader(hPixelShader);
0482:     DrawModel(lpD3DDev);
0483: 
0484:     // 描画をバックバッファに戻す
0485:     lpD3DDev->SetRenderTarget(pBackbuffer, lpZbuffer );
0486:     for (i = 0; i < 4; i++) {
0487:         lpD3DDev->SetTextureStageState(i, D3DTSS_ADDRESSU,  D3DTADDRESS_CLAMP);
0488:         lpD3DDev->SetTextureStageState(i, D3DTSS_ADDRESSV,  D3DTADDRESS_CLAMP);
0489:         lpD3DDev->SetTextureStageState(i, D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
0490:         lpD3DDev->SetTextureStageState(i, D3DTSS_COLORARG1, D3DTA_TEXTURE);
0491:         lpD3DDev->SetTexture( i, pTexture );                    // テクスチャー
0492:     }
0493:     lpD3DDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
0494:     lpD3DDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR);
0495:     lpD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
0496: 
0497:     lpD3DDev->SetVertexShader(hFinalVertexShader);
0498:     lpD3DDev->SetPixelShader(hFinalPixelShader);
0499:     lpD3DDev->SetStreamSource( 0, pFinalVB, sizeof(D3D_FINAL_VERTEX) );
0500:     lpD3DDev->SetIndices(pFinalIB,0);
0501:     lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 4, 0, 2 );
0502: 
0503:     lpD3DDev->SetPixelShader(0);
0504:     lpD3DDev->SetTexture( 0, NULL);
0505:     lpD3DDev->SetTexture( 1, NULL);
0506:     lpD3DDev->SetTexture( 2, NULL);
0507:     lpD3DDev->SetTexture( 3, NULL);
0508:     lpD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
0509: #if 0
0510:     // 描画した画面をテクスチャーとして描く
0511:     {
0512:         struct TLVERTEX
0513:         {
0514:             float x,y,z,rhw;
0515:             float tu,tv;
0516:         };
0517:         #define FVF_TLVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1)
0518:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0519:         lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0520:         lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0521:         float scale = 512.0f;
0522:         TLVERTEX Vertex[4] = {
0523:             // x  y  z rhw tu tv
0524:             {    0,    0,0, 1, 0, 0,},
0525:             {scale,    0,0, 1, 1, 0,},
0526:             {scale,scale,0, 1, 1, 1,},
0527:             {    0,scale,0, 1, 0, 1,},
0528:         };
0529:         lpD3DDev->SetTexture( 0, pTexture );
0530:         lpD3DDev->SetVertexShader( FVF_TLVERTEX );
0531:         lpD3DDev->SetPixelShader(0);
0532:         lpD3DDev->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TLVERTEX ) );
0533:     }
0534: #endif
0535: }
0536: //-----------------------------------------------------------------------------
0537: // メッシュオブジェクト削除
0538: //-----------------------------------------------------------------------------
0539: void DeleteMeshObject(void)
0540: {
0541:     DWORD i;
0542: 
0543:     if(pMeshVB == NULL) return;
0544: 
0545:     for(i=0; i<dwNumMaterials; i++){
0546:         RELEASE(pMeshTextures[i]);
0547:     }
0548:     delete[] pMeshTextures;
0549:     delete[] pMeshMaterials;
0550:     delete[] pSubsetTable;
0551: 
0552:     RELEASE(pMeshVB);
0553:     RELEASE(pMeshIndex);
0554: }
0555: //-----------------------------------------------------------------------------
0556: // Name: CleanRender()
0557: // Desc: 後始末
0558: //-----------------------------------------------------------------------------
0559: void CleanRender(LPDIRECT3DDEVICE8 lpD3DDev)
0560: {
0561:     CleanBg(lpD3DDev);
0562:     
0563:     RELEASE(pTextureSurface);
0564:     RELEASE(pTexture);
0565:     RELEASE(pBackbuffer);
0566:     
0567:     RELEASE(pFinalIB);
0568:     RELEASE(pFinalVB);
0569:      CPixelShaderMgr::Release(lpD3DDev, &hFinalPixelShader);
0570:     CVertexShaderMgr::Release(lpD3DDev, &hFinalVertexShader);
0571:      CPixelShaderMgr::Release(lpD3DDev, &hPixelShader);
0572:     CVertexShaderMgr::Release(lpD3DDev, &hVertexShader);
0573:      CPixelShaderMgr::Release(lpD3DDev, &hPixelShaderNormal);
0574:     CVertexShaderMgr::Release(lpD3DDev, &hVertexShaderNormal);
0575:      CPixelShaderMgr::Release(lpD3DDev, &hPixelShaderNormalEdge);
0576:     CVertexShaderMgr::Release(lpD3DDev, &hVertexShaderNormalEdge);
0577:     
0578:     DeleteMeshObject();
0579: }
0580: