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