0001:
0002:
0003:
0004:
0005:
0006: #define STRICT
0007: #include <windows.h>
0008: #include <commctrl.h>
0009: #include <commdlg.h>
0010: #include <basetsd.h>
0011: #include <math.h>
0012: #include <stdio.h>
0013: #include <d3dx9.h>
0014: #include <dxerr9.h>
0015: #include <tchar.h>
0016: #include "DXUtil.h"
0017: #include "D3DEnumeration.h"
0018: #include "D3DSettings.h"
0019: #include "D3DApp.h"
0020: #include "D3DFont.h"
0021: #include "D3DFile.h"
0022: #include "D3DUtil.h"
0023: #include "resource.h"
0024: #include "main.h"
0025:
0026: #define MAP_SIZE 256
0027: #define DIFFUSE_SIZE 64
0028:
0029:
0030:
0031: #define RS m_pd3dDevice->SetRenderState
0032: #define TSS m_pd3dDevice->SetTextureStageState
0033: #define SAMP m_pd3dDevice->SetSamplerState
0034:
0035:
0036:
0037:
0038:
0039: typedef struct {
0040: FLOAT p[4];
0041: FLOAT tu, tv;
0042: } TVERTEX;
0043:
0044: typedef struct {
0045: FLOAT p[4];
0046: FLOAT t[4][2];
0047: } T4VERTEX;
0048:
0049:
0050:
0051:
0052: CMyD3DApplication* g_pApp = NULL;
0053: HINSTANCE g_hInst = NULL;
0054:
0055:
0056:
0057:
0058:
0059: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0060: {
0061: CMyD3DApplication d3dApp;
0062:
0063: g_pApp = &d3dApp;
0064: g_hInst = hInst;
0065:
0066: InitCommonControls();
0067: if( FAILED( d3dApp.Create( hInst ) ) )
0068: return 0;
0069:
0070: return d3dApp.Run();
0071: }
0072:
0073:
0074:
0075:
0076:
0077:
0078:
0079:
0080: CMyD3DApplication::CMyD3DApplication()
0081: {
0082: m_iState = -1;
0083: m_Shader = 0;
0084:
0085: m_pMeshBg = new CD3DMesh();
0086:
0087: m_pMapZ = NULL;
0088: m_pPosTex = NULL;
0089: m_pPosSurf = NULL;
0090: m_pPosLockTex = NULL;
0091: m_pPosLockSurf = NULL;
0092: m_pNormalTex = NULL;
0093: m_pNormalSurf = NULL;
0094: m_pNormalLockTex = NULL;
0095: m_pNormalLockSurf = NULL;
0096: m_pDiffuseTex = NULL;
0097: m_pDiffuseSurf = NULL;
0098: m_pFinalTex = NULL;
0099: m_pFinalSurf = NULL;
0100: for(int i=0;i<REDUCTION_MAPS;i++){
0101: m_pReductionTex[i] = NULL;
0102: m_pReductionSurf[i] = NULL;
0103: }
0104:
0105: m_pEffect = NULL;
0106: m_hTechnique = NULL;
0107: m_hmWVP = NULL;
0108: m_htSrcTex = NULL;
0109:
0110: m_fWorldRotX = -0.5f;
0111: m_fWorldRotY = 0.0f;
0112: m_fViewZoom = 13.0f;
0113:
0114: m_LighPos = D3DXVECTOR3( -5.0f, 5.0f,-2.0f );
0115:
0116: m_dwCreationWidth = 512;
0117: m_dwCreationHeight = 512;
0118: m_strWindowTitle = TEXT( "main" );
0119: m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
0120: m_bStartFullscreen = false;
0121: m_bShowCursorWhenFullscreen = false;
0122:
0123: m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0124: m_bLoadingApp = TRUE;
0125:
0126: ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0127: }
0128:
0129:
0130:
0131:
0132:
0133:
0134:
0135:
0136: CMyD3DApplication::~CMyD3DApplication()
0137: {
0138: }
0139:
0140:
0141:
0142:
0143:
0144:
0145:
0146:
0147:
0148:
0149: HRESULT CMyD3DApplication::OneTimeSceneInit()
0150: {
0151:
0152: SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0153:
0154: m_bLoadingApp = FALSE;
0155:
0156: return S_OK;
0157: }
0158:
0159:
0160:
0161:
0162:
0163:
0164:
0165:
0166: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0167: DWORD dwBehavior, D3DFORMAT Format )
0168: {
0169: UNREFERENCED_PARAMETER( Format );
0170: UNREFERENCED_PARAMETER( dwBehavior );
0171: UNREFERENCED_PARAMETER( pCaps );
0172:
0173:
0174:
0175: if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0176: return E_FAIL;
0177:
0178:
0179: if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0180: && 0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0181: return E_FAIL;
0182:
0183: return S_OK;
0184: }
0185:
0186:
0187:
0188:
0189:
0190:
0191:
0192:
0193:
0194: HRESULT CMyD3DApplication::InitDeviceObjects()
0195: {
0196: HRESULT hr;
0197:
0198:
0199: if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, _T("map.x"))))
0200: return DXTRACE_ERR( "Load Model", hr );
0201: m_pMeshBg->UseMeshMaterials(FALSE);
0202:
0203:
0204: LPD3DXBUFFER pErr;
0205: if( FAILED( hr = D3DXCreateEffectFromFile(
0206: m_pd3dDevice, "hlsl.fx", NULL, NULL,
0207: D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0208: MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0209: , "ERROR", MB_OK);
0210: return DXTRACE_ERR( "CreateEffectFromFile", hr );
0211: }
0212: m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0213: m_hmWVP = m_pEffect->GetParameterByName( NULL, "mWVP" );
0214: m_htSrcTex = m_pEffect->GetParameterByName( NULL, "SrcTex" );
0215:
0216:
0217: m_pFont->InitDeviceObjects( m_pd3dDevice );
0218:
0219: return S_OK;
0220: }
0221:
0222:
0223:
0224:
0225: VOID WINAPI FillTex (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord,
0226: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0227: {
0228: FLOAT x = 2.0f*(pTexCoord->x-0.5f);
0229: FLOAT y = 2.0f*(pTexCoord->y-0.5f);
0230: FLOAT col = (x*x+y*y<(1.0f-2.0f/DIFFUSE_SIZE)*(1.0f-2.0f/DIFFUSE_SIZE))
0231: ? 1.0f : 0.0f;
0232:
0233: pOut->x = pOut->y = pOut->z = pOut->w = col;
0234: }
0235:
0236:
0237:
0238:
0239:
0240:
0241: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0242: {
0243: m_iState = -1;
0244: m_iChangeState = TRUE;
0245:
0246: DWORD i;
0247:
0248:
0249: m_pMeshBg->RestoreDeviceObjects( m_pd3dDevice );
0250:
0251:
0252: D3DMATERIAL9 mtrl;
0253: D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0254: m_pd3dDevice->SetMaterial( &mtrl );
0255:
0256:
0257:
0258: RS( D3DRS_DITHERENABLE, FALSE );
0259: RS( D3DRS_SPECULARENABLE, FALSE );
0260: RS( D3DRS_ZENABLE, TRUE );
0261: RS( D3DRS_AMBIENT, 0x000F0F0F );
0262:
0263: TSS( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
0264: TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0265: TSS( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0266: TSS( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
0267: TSS( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
0268: SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0269: SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0270: SAMP( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0271: SAMP( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0272:
0273:
0274: D3DXMATRIX matIdentity;
0275: D3DXMatrixIdentity( &m_mWorld );
0276:
0277:
0278: D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0279: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0280: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0281: D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0282:
0283:
0284: FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0285: D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0286:
0287:
0288: m_pFont->RestoreDeviceObjects();
0289:
0290:
0291: if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(MAP_SIZE, MAP_SIZE,
0292: D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL)))
0293: return E_FAIL;
0294:
0295: if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1,
0296: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pPosTex, NULL)))
0297: return E_FAIL;
0298: if (FAILED(m_pPosTex->GetSurfaceLevel(0, &m_pPosSurf)))
0299: return E_FAIL;
0300: if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1,
0301: 0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pPosLockTex, NULL)))
0302: return E_FAIL;
0303: if (FAILED(m_pPosLockTex->GetSurfaceLevel(0, &m_pPosLockSurf)))
0304: return E_FAIL;
0305:
0306: if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1,
0307: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pNormalTex, NULL)))
0308: return E_FAIL;
0309: if (FAILED(m_pNormalTex->GetSurfaceLevel(0, &m_pNormalSurf)))
0310: return E_FAIL;
0311: if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1,
0312: 0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pNormalLockTex, NULL)))
0313: return E_FAIL;
0314: if (FAILED(m_pNormalLockTex->GetSurfaceLevel(0, &m_pNormalLockSurf)))
0315: return E_FAIL;
0316:
0317: if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1,
0318: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pDiffuseTex, NULL)))
0319: return E_FAIL;
0320: if (FAILED(m_pDiffuseTex->GetSurfaceLevel(0, &m_pDiffuseSurf)))
0321: return E_FAIL;
0322: if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1,
0323: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFinalTex, NULL)))
0324: return E_FAIL;
0325: if (FAILED(m_pFinalTex->GetSurfaceLevel(0, &m_pFinalSurf)))
0326: return E_FAIL;
0327:
0328:
0329: for(i=0;i<REDUCTION_MAPS;i++){
0330: int size = 1<<(3*(REDUCTION_MAPS-i));
0331: if (FAILED(m_pd3dDevice->CreateTexture(size, size, 1,
0332: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pReductionTex[i], NULL)))
0333: return E_FAIL;
0334: if (FAILED(m_pReductionTex[i]->GetSurfaceLevel(0, &m_pReductionSurf[i])))
0335: return E_FAIL;
0336: }
0337:
0338:
0339: if( FAILED(m_pd3dDevice->CreateTexture(DIFFUSE_SIZE, DIFFUSE_SIZE, 1
0340: , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8
0341: , D3DPOOL_DEFAULT, &m_pMaskTex, NULL)))
0342: return E_FAIL;
0343: if( FAILED(D3DXFillTexture(m_pMaskTex, FillTex, NULL)))
0344: return E_FAIL;
0345:
0346: m_pEffect->OnResetDevice();
0347:
0348: return S_OK;
0349: }
0350:
0351:
0352:
0353:
0354:
0355:
0356: int CMyD3DApplication::FrameMoveCreateMap()
0357: {
0358:
0359: if(1<m_iCount) return 1;
0360:
0361: return 0;
0362: }
0363:
0364:
0365:
0366:
0367: void CMyD3DApplication::RenderCreateMap()
0368: {
0369: LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0370: D3DVIEWPORT9 oldViewport;
0371:
0372: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0373: {
0374:
0375:
0376:
0377: m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0378: m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0379: m_pd3dDevice->GetViewport(&oldViewport);
0380:
0381:
0382:
0383:
0384: m_pd3dDevice->SetRenderTarget(0, m_pPosSurf);
0385: m_pd3dDevice->SetRenderTarget(1, m_pNormalSurf);
0386: m_pd3dDevice->SetDepthStencilSurface(NULL);
0387:
0388: D3DVIEWPORT9 viewport_height = {0,0
0389: , MAP_SIZE
0390: , MAP_SIZE
0391: , 0.0f,1.0f};
0392: m_pd3dDevice->SetViewport(&viewport_height);
0393:
0394:
0395:
0396:
0397: m_pd3dDevice->Clear(0L, NULL
0398: , D3DCLEAR_TARGET
0399: , 0x00000000, 1.0f, 0L);
0400:
0401: if( m_pEffect != NULL )
0402: {
0403:
0404:
0405:
0406: m_pEffect->SetTechnique( m_hTechnique );
0407: m_pEffect->Begin( NULL, 0 );
0408: m_pEffect->Pass( 0 );
0409:
0410: RS( D3DRS_ZENABLE, FALSE );
0411: RS( D3DRS_CULLMODE, D3DCULL_NONE );
0412:
0413:
0414:
0415:
0416: m_pMeshBg->Render(m_pd3dDevice);
0417:
0418: RS( D3DRS_ZENABLE, TRUE );
0419: RS( D3DRS_CULLMODE, D3DCULL_CCW );
0420:
0421: m_pEffect->End();
0422: }
0423:
0424:
0425:
0426:
0427: m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0428: m_pd3dDevice->SetRenderTarget(1, NULL);
0429: m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0430: m_pd3dDevice->SetViewport(&oldViewport);
0431: pOldBackBuffer->Release();
0432: pOldZBuffer->Release();
0433:
0434:
0435:
0436:
0437: m_pd3dDevice->Clear(0L, NULL
0438: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0439: , 0xff000000, 1.0f, 0L);
0440:
0441: m_pd3dDevice->EndScene();
0442: }
0443: }
0444:
0445:
0446:
0447:
0448:
0449:
0450: static int ix=0;
0451: static int iy=0;
0452: static float pos[MAP_SIZE][MAP_SIZE][3];
0453: static float normal[MAP_SIZE][MAP_SIZE][3];
0454: int CMyD3DApplication::FrameMoveFinalGathering()
0455: {
0456: ix = this->m_iCount % MAP_SIZE;
0457: iy = this->m_iCount / MAP_SIZE;
0458:
0459: if(0==this->m_iCount){
0460: D3DLOCKED_RECT d3dlr;
0461: float *p;
0462: m_pd3dDevice->GetRenderTargetData(m_pPosSurf,m_pPosLockSurf);
0463: m_pPosLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY);
0464: p = (float *)d3dlr.pBits;
0465: for(int y=0;y<MAP_SIZE;y++){
0466: for(int x=0;x<MAP_SIZE;x++){
0467: pos[x][y][0]=p[0];
0468: pos[x][y][1]=p[1];
0469: pos[x][y][2]=p[2];
0470: p+=4;
0471: }
0472: }
0473: m_pPosLockSurf->UnlockRect();
0474:
0475: m_pd3dDevice->GetRenderTargetData(m_pNormalSurf,m_pNormalLockSurf);
0476: m_pNormalLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY);
0477: p = (float *)d3dlr.pBits;
0478: for(int y=0;y<MAP_SIZE;y++){
0479: for(int x=0;x<MAP_SIZE;x++){
0480: normal[x][y][0]=p[0];
0481: normal[x][y][1]=p[1];
0482: normal[x][y][2]=p[2];
0483: p+=4;
0484: }
0485: }
0486: m_pNormalLockSurf->UnlockRect();
0487: }
0488:
0489:
0490: if(MAP_SIZE-1==ix && MAP_SIZE-1==iy) return 1;
0491:
0492: return 0;
0493: }
0494:
0495:
0496:
0497:
0498: void CMyD3DApplication::RenderFinalGathering()
0499: {
0500: D3DXMATRIX m, mView;
0501: LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0502: D3DVIEWPORT9 oldViewport;
0503: D3DSURFACE_DESC d3dsd;
0504:
0505: D3DVIEWPORT9 viewport = {0,0
0506: , 1, 1
0507: , 0.0f,1.0f};
0508:
0509: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0510: {
0511: RS( D3DRS_ZENABLE, FALSE );
0512: RS( D3DRS_CULLMODE, D3DCULL_NONE );
0513:
0514:
0515:
0516:
0517: m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0518: m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0519: m_pd3dDevice->GetViewport(&oldViewport);
0520:
0521:
0522:
0523:
0524:
0525:
0526:
0527:
0528:
0529:
0530: m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[0]);
0531: m_pd3dDevice->SetDepthStencilSurface(NULL);
0532:
0533: m_pReductionSurf[0]->GetDesc(&d3dsd);
0534: viewport.Height = d3dsd.Width;
0535: viewport.Width = d3dsd.Height;
0536: m_pd3dDevice->SetViewport(&viewport);
0537:
0538:
0539:
0540:
0541: {
0542: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
0543: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0544: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0545: m_pd3dDevice->SetTextureStageState(0,D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
0546: m_pd3dDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
0547: m_pd3dDevice->SetTextureStageState(1,D3DTSS_ALPHAOP, D3DTOP_DISABLE);
0548: m_pd3dDevice->SetVertexShader(NULL);
0549: m_pd3dDevice->SetPixelShader(0);
0550: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0551: TVERTEX Vertex[4] = {
0552:
0553: { 0, 0,0, 1, 0, 0,},
0554: {d3dsd.Width, 0,0, 1, 1, 0,},
0555: {d3dsd.Width,d3dsd.Height,0, 1, 1, 1,},
0556: { 0,d3dsd.Height,0, 1, 0, 1,},
0557: };
0558: m_pd3dDevice->SetTexture( 0, m_pMaskTex );
0559: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0560: }
0561:
0562: if( m_pEffect != NULL )
0563: {
0564:
0565:
0566:
0567: m_pEffect->SetTechnique( m_hTechnique );
0568: m_pEffect->Begin( NULL, 0 );
0569: m_pEffect->Pass( 1 );
0570:
0571:
0572: RS(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED |D3DCOLORWRITEENABLE_GREEN |D3DCOLORWRITEENABLE_BLUE );
0573:
0574:
0575:
0576:
0577: float x[3] = {pos[ix][iy][0], pos[ix][iy][1], pos[ix][iy][2]};
0578: float n[3] = {normal[ix][iy][0], normal[ix][iy][1], normal[ix][iy][2]};
0579: D3DXVECTOR3 vFromPt = D3DXVECTOR3( x[0], x[1], x[2] ) + 0.05f*D3DXVECTOR3( n[0], n[1], n[2] );
0580: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( n[0], n[1], n[2] ) + vFromPt;
0581: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0582: D3DXMatrixLookAtLH( &mView, &vFromPt, &vLookatPt, &vUpVec );
0583:
0584: m_pEffect->SetMatrix( m_hmWVP, &mView );
0585:
0586: m_pMeshBg->Render(m_pd3dDevice);
0587:
0588:
0589: RS(D3DRS_COLORWRITEENABLE, 0xf );
0590: m_pEffect->End();
0591: }
0592:
0593:
0594:
0595:
0596:
0597: m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[1]);
0598: m_pd3dDevice->SetDepthStencilSurface(NULL);
0599:
0600: m_pReductionSurf[1]->GetDesc(&d3dsd);
0601: viewport.Height = d3dsd.Width;
0602: viewport.Width = d3dsd.Height;
0603: m_pd3dDevice->SetViewport(&viewport);
0604:
0605: TSS(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
0606: TSS(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0607: TSS(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0608:
0609: if( m_pEffect != NULL ) {
0610:
0611:
0612:
0613: m_pEffect->SetTechnique( m_hTechnique );
0614: m_pEffect->Begin( NULL, 0 );
0615: m_pEffect->Pass( 2 );
0616:
0617: m_pEffect->SetFloat("MAP_WIDTH", DIFFUSE_SIZE);
0618: m_pEffect->SetFloat("MAP_HEIGHT", DIFFUSE_SIZE);
0619:
0620:
0621:
0622:
0623: TVERTEX Vertex1[4] = {
0624:
0625: {-1.0f, +1.0f, 0.1f, 0, 0},
0626: {+1.0f, +1.0f, 0.1f, 1, 0},
0627: {+1.0f, -1.0f, 0.1f, 1, 1},
0628: {-1.0f, -1.0f, 0.1f, 0, 1},
0629: };
0630: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0631: m_pEffect->SetTexture("ReductionMap", m_pReductionTex[0]);
0632: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0633: , 2, Vertex1, sizeof( TVERTEX ) );
0634:
0635: m_pEffect->End();
0636: }
0637:
0638:
0639:
0640:
0641:
0642:
0643: m_pd3dDevice->SetRenderTarget(0, m_pDiffuseSurf);
0644: m_pd3dDevice->SetDepthStencilSurface(NULL);
0645:
0646: m_pDiffuseSurf->GetDesc(&d3dsd);
0647: viewport.Height = d3dsd.Width;
0648: viewport.Width = d3dsd.Height;
0649: m_pd3dDevice->SetViewport(&viewport);
0650:
0651:
0652: if(0==ix&&0==iy)m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0x00800000, 1.0f, 0L);
0653:
0654: if( m_pEffect != NULL ) {
0655:
0656:
0657:
0658: m_pEffect->SetTechnique( m_hTechnique );
0659: m_pEffect->Begin( NULL, 0 );
0660: m_pEffect->Pass( 2 );
0661:
0662: m_pEffect->SetFloat("MAP_WIDTH", 8);
0663: m_pEffect->SetFloat("MAP_HEIGHT", 8);
0664:
0665:
0666:
0667:
0668: float x = 2.0f*((float)ix/(float)MAP_SIZE) - 1.0f;
0669: float y = -2.0f*((float)iy/(float)MAP_SIZE) + 1.0f;
0670: TVERTEX Vertex1[4] = {
0671:
0672: {x, y, 0.1f, 0, 0},
0673: {x+2.0f/(float)MAP_SIZE, y, 0.1f, 1, 0},
0674: {x+2.0f/(float)MAP_SIZE, y-2.0f/(float)MAP_SIZE,0.1f, 1, 1},
0675: {x, y-2.0f/(float)MAP_SIZE,0.1f, 0, 1},
0676: };
0677: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0678: m_pEffect->SetTexture("ReductionMap", m_pReductionTex[1]);
0679: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0680: , 2, Vertex1, sizeof( TVERTEX ) );
0681:
0682: m_pEffect->End();
0683: }
0684:
0685:
0686:
0687:
0688: if(MAP_SIZE-1==ix && MAP_SIZE-1==iy){
0689: m_pd3dDevice->SetRenderTarget(0, m_pFinalSurf);
0690: m_pd3dDevice->SetDepthStencilSurface(NULL);
0691:
0692: m_pFinalSurf->GetDesc(&d3dsd);
0693: viewport.Height = d3dsd.Width;
0694: viewport.Width = d3dsd.Height;
0695: m_pd3dDevice->SetViewport(&viewport);
0696:
0697: if( m_pEffect != NULL ) {
0698: m_pEffect->SetTechnique( m_hTechnique );
0699: m_pEffect->Begin( NULL, 0 );
0700: m_pEffect->Pass( 3 );
0701:
0702: TVERTEX Vertex[4] = {
0703:
0704: { 0, 0,0, 1, 0+0.5f/(FLOAT)MAP_SIZE, 0+0.5f/(FLOAT)MAP_SIZE,},
0705: {MAP_SIZE, 0,0, 1, 1+0.5f/(FLOAT)MAP_SIZE, 0+0.5f/(FLOAT)MAP_SIZE,},
0706: {MAP_SIZE,MAP_SIZE,0, 1, 1+0.5f/(FLOAT)MAP_SIZE, 1+0.5f/(FLOAT)MAP_SIZE,},
0707: { 0,MAP_SIZE,0, 1, 0+0.5f/(FLOAT)MAP_SIZE, 1+0.5f/(FLOAT)MAP_SIZE,},
0708: };
0709: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0710: m_pEffect->SetTexture(m_htSrcTex, m_pDiffuseTex);
0711: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0712: , 2, Vertex, sizeof( TVERTEX ) );
0713:
0714: m_pEffect->End();
0715: }
0716: }
0717:
0718:
0719:
0720:
0721: m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0722: m_pd3dDevice->SetRenderTarget(1, NULL);
0723: m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0724: m_pd3dDevice->SetViewport(&oldViewport);
0725: pOldBackBuffer->Release();
0726: pOldZBuffer->Release();
0727:
0728: RS( D3DRS_ZENABLE, TRUE );
0729: RS( D3DRS_CULLMODE, D3DCULL_CCW );
0730:
0731:
0732:
0733:
0734: m_pd3dDevice->Clear(0L, NULL
0735: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0736: , 0x00001020, 1.0f, 0L);
0737:
0738: #if 1
0739: {
0740: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
0741: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0742: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0743: m_pd3dDevice->SetVertexShader(NULL);
0744: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0745: m_pd3dDevice->SetPixelShader(0);
0746: float scale = 256.0f*2/3;
0747: for(DWORD i=0; i<3; i++){
0748: TVERTEX Vertex[4] = {
0749:
0750: { 0,(i+0)*scale,0, 1, 0, 0,},
0751: {scale,(i+0)*scale,0, 1, 1, 0,},
0752: {scale,(i+1)*scale,0, 1, 1, 1,},
0753: { 0,(i+1)*scale,0, 1, 0, 1,},
0754: };
0755: if(0==i) m_pd3dDevice->SetTexture( 0, m_pDiffuseTex );
0756: if(1==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[0] );
0757: if(2==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[1] );
0758: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0759: }
0760: }
0761: #endif
0762:
0763: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
0764: TCHAR szMsg[MAX_PATH] = TEXT("");
0765:
0766: sprintf( szMsg, TEXT("x:%4d\ny:%4d"), ix, iy);
0767: m_pFont->DrawText( 2, (FLOAT)m_d3dsdBackBuffer.Height-40, fontColor, szMsg );
0768:
0769: m_pd3dDevice->EndScene();
0770: }
0771: }
0772:
0773:
0774:
0775:
0776:
0777:
0778: HRESULT CMyD3DApplication::FrameMove()
0779: {
0780: m_iCount++;
0781:
0782: if(m_iChangeState) {m_iState++;m_iCount=0;m_iChangeState=0;}
0783:
0784: switch(m_iState){
0785: case 0:
0786: m_iChangeState = this->FrameMoveCreateMap();
0787: return S_OK;
0788: case 1:
0789: m_iChangeState = this->FrameMoveFinalGathering();
0790: return S_OK;
0791: default:
0792: break;
0793: }
0794:
0795:
0796:
0797: UpdateInput( &m_UserInput );
0798:
0799:
0800:
0801:
0802:
0803: D3DXMATRIX matRotY;
0804: D3DXMATRIX matRotX;
0805:
0806: if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0807: m_fWorldRotY += m_fElapsedTime;
0808: else
0809: if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0810: m_fWorldRotY -= m_fElapsedTime;
0811:
0812: if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0813: m_fWorldRotX += m_fElapsedTime;
0814: else
0815: if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0816: m_fWorldRotX -= m_fElapsedTime;
0817:
0818: D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0819: D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0820:
0821: D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0822:
0823:
0824:
0825:
0826:
0827: if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0828: m_fViewZoom += m_fElapsedTime;
0829: else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0830: m_fViewZoom -= m_fElapsedTime;
0831:
0832: D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0833: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.3f, 0.0f );
0834: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0835: D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0836:
0837:
0838:
0839:
0840: if( m_UserInput.bChangeShader ) m_Shader = (m_Shader + 1) % 3;
0841:
0842: return S_OK;
0843: }
0844:
0845:
0846:
0847:
0848: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0849: {
0850: pUserInput->bRotateUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000) == 0x8000 );
0851: pUserInput->bRotateDown = ( m_bActive && (GetAsyncKeyState( VK_DOWN ) & 0x8000) == 0x8000 );
0852: pUserInput->bRotateLeft = ( m_bActive && (GetAsyncKeyState( VK_LEFT ) & 0x8000) == 0x8000 );
0853: pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0854:
0855: pUserInput->bZoomIn = ( m_bActive && (GetAsyncKeyState( 'Z' ) & 0x8000) == 0x8000 );
0856: pUserInput->bZoomOut = ( m_bActive && (GetAsyncKeyState( 'X' ) & 0x8000) == 0x8000 );
0857:
0858: pUserInput->bChangeShader= ( m_bActive && (GetAsyncKeyState( 'A' ) & 0x8001) == 0x8001 );
0859: }
0860:
0861:
0862:
0863:
0864:
0865:
0866: HRESULT CMyD3DApplication::Render()
0867: {
0868: switch(m_iState){
0869: case 0:
0870: this->RenderCreateMap();
0871: return S_OK;
0872: case 1:
0873: this->RenderFinalGathering();
0874: return S_OK;
0875: default:
0876: break;
0877: }
0878:
0879: D3DXMATRIX m, mT, mR, mW, mView, mProj;
0880: DWORD i;
0881: D3DXVECTOR4 v;
0882: D3DMATERIAL9 *pMtrl;
0883:
0884:
0885:
0886:
0887: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0888: {
0889:
0890: m_pd3dDevice->Clear(0L, NULL
0891: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0892: , 0xff000000, 1.0f, 0L);
0893:
0894: if( m_pEffect != NULL )
0895: {
0896:
0897:
0898:
0899: m_pEffect->SetTechnique( m_hTechnique );
0900: m_pEffect->Begin( NULL, 0 );
0901: m_pEffect->Pass( 4+m_Shader );
0902:
0903:
0904:
0905:
0906:
0907: m = m_mWorld * m_mView * m_mProj;
0908: m_pEffect->SetMatrix( m_hmWVP, &m );
0909:
0910:
0911: m_pEffect->SetTexture("RadianceTex", this->m_pFinalTex );
0912: pMtrl = m_pMeshBg->m_pMaterials;
0913: for( i=0; i<m_pMeshBg->m_dwNumMaterials; i++ ) {
0914: m_pEffect->SetTexture(m_htSrcTex, m_pMeshBg->m_pTextures[i] );
0915: m_pMeshBg->m_pLocalMesh->DrawSubset( i );
0916: pMtrl++;
0917: }
0918:
0919: m_pEffect->End();
0920: }
0921:
0922:
0923:
0924:
0925: #if 1
0926: {
0927: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
0928: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0929: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0930: SAMP(0, D3DSAMP_MAGFILTER ,D3DTEXF_POINT );
0931: m_pd3dDevice->SetVertexShader(NULL);
0932: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0933: m_pd3dDevice->SetPixelShader(0);
0934: float scale = 256.0f;
0935: for(DWORD i=0; i<3; i++){
0936: TVERTEX Vertex[4] = {
0937:
0938: {(i+0)*scale, 0,0, 1, 0, 0,},
0939: {(i+1)*scale, 0,0, 1, 1, 0,},
0940: {(i+1)*scale,scale,0, 1, 1, 1,},
0941: {(i+0)*scale,scale,0, 1, 0, 1,},
0942: };
0943: if(0==i) m_pd3dDevice->SetTexture( 0, m_pPosTex );
0944: if(1==i) m_pd3dDevice->SetTexture( 0, m_pNormalTex );
0945: if(2==i) m_pd3dDevice->SetTexture( 0, m_pFinalTex );
0946: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0947: }
0948: }
0949: #endif
0950:
0951:
0952: m_pd3dDevice->EndScene();
0953: }
0954:
0955: return S_OK;
0956: }
0957:
0958:
0959:
0960:
0961:
0962:
0963:
0964:
0965: HRESULT CMyD3DApplication::RenderText()
0966: {
0967: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
0968: TCHAR szMsg[MAX_PATH] = TEXT("");
0969:
0970: FLOAT fNextLine = 40.0f;
0971:
0972:
0973: fNextLine = (FLOAT) m_d3dsdBackBuffer.Height;
0974:
0975: lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0976: fNextLine -= 20.0f;
0977: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0978:
0979: lstrcpy( szMsg, TEXT("Press 'A' to change shader") );
0980: fNextLine -= 20.0f;
0981: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0982:
0983: lstrcpy( szMsg, m_strDeviceStats );
0984: fNextLine -= 20.0f;
0985: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0986: lstrcpy( szMsg, m_strFrameStats );
0987: fNextLine -= 20.0f;
0988: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0989:
0990: return S_OK;
0991: }
0992:
0993:
0994:
0995:
0996:
0997:
0998:
0999:
1000: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
1001: WPARAM wParam, LPARAM lParam )
1002: {
1003: switch( msg )
1004: {
1005: case WM_PAINT:
1006: {
1007: if( m_bLoadingApp )
1008: {
1009:
1010: HDC hDC = GetDC( hWnd );
1011: TCHAR strMsg[MAX_PATH];
1012: wsprintf(strMsg, TEXT("Loading... Please wait"));
1013: RECT rct;
1014: GetClientRect( hWnd, &rct );
1015: DrawText( hDC, strMsg, -1, &rct
1016: , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
1017: ReleaseDC( hWnd, hDC );
1018: }
1019: break;
1020: }
1021:
1022: }
1023:
1024: return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
1025: }
1026:
1027:
1028:
1029:
1030:
1031:
1032:
1033:
1034: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
1035: {
1036:
1037: SAFE_RELEASE(m_pMaskTex);
1038: for(int i=0;i<REDUCTION_MAPS;i++){
1039: SAFE_RELEASE(m_pReductionSurf[i]);
1040: SAFE_RELEASE(m_pReductionTex[i]);
1041: }
1042: SAFE_RELEASE(m_pFinalSurf);
1043: SAFE_RELEASE(m_pFinalTex);
1044: SAFE_RELEASE(m_pDiffuseSurf);
1045: SAFE_RELEASE(m_pDiffuseTex);
1046: SAFE_RELEASE(m_pNormalLockSurf);
1047: SAFE_RELEASE(m_pNormalLockTex);
1048: SAFE_RELEASE(m_pNormalSurf);
1049: SAFE_RELEASE(m_pNormalTex);
1050: SAFE_RELEASE(m_pPosLockSurf);
1051: SAFE_RELEASE(m_pPosLockTex);
1052: SAFE_RELEASE(m_pPosSurf);
1053: SAFE_RELEASE(m_pPosTex);
1054: SAFE_RELEASE(m_pMapZ);
1055:
1056: m_pMeshBg->InvalidateDeviceObjects();
1057:
1058: m_pFont->InvalidateDeviceObjects();
1059:
1060:
1061: if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
1062:
1063: return S_OK;
1064: }
1065:
1066:
1067:
1068:
1069:
1070:
1071:
1072:
1073: HRESULT CMyD3DApplication::DeleteDeviceObjects()
1074: {
1075:
1076: SAFE_RELEASE( m_pEffect );
1077:
1078:
1079: m_pMeshBg->Destroy();
1080:
1081:
1082: m_pFont->DeleteDeviceObjects();
1083:
1084: return S_OK;
1085: }
1086:
1087:
1088:
1089:
1090:
1091:
1092:
1093:
1094: HRESULT CMyD3DApplication::FinalCleanup()
1095: {
1096: SAFE_DELETE( m_pMeshBg );
1097:
1098: SAFE_DELETE( m_pFont );
1099:
1100: return S_OK;
1101: }
1102:
1103:
1104:
1105:
1106: