0001:
0002:
0003:
0004:
0005:
0006:
0007: #define STRICT
0008: #include <windows.h>
0009: #include <time.h>
0010: #include <commctrl.h>
0011: #include <commdlg.h>
0012: #include <basetsd.h>
0013: #include <math.h>
0014: #include <stdio.h>
0015: #include <d3dx9.h>
0016: #include <dxerr9.h>
0017: #include <tchar.h>
0018: #include "DXUtil.h"
0019: #include "D3DEnumeration.h"
0020: #include "D3DSettings.h"
0021: #include "D3DApp.h"
0022: #include "D3DFont.h"
0023: #include "D3DFile.h"
0024: #include "D3DUtil.h"
0025: #include "resource.h"
0026: #include "main.h"
0027:
0028: #define MAP_SIZE 128
0029: #define DECALE_MAP_SIZE 512
0030:
0031: #define frand() ((float)rand()/(float)RAND_MAX)
0032:
0033:
0034:
0035:
0036: CMyD3DApplication* g_pApp = NULL;
0037: HINSTANCE g_hInst = NULL;
0038:
0039: typedef struct {
0040: FLOAT p[4];
0041: FLOAT tu, tv;
0042: } TVERTEX;
0043:
0044:
0045:
0046:
0047:
0048:
0049:
0050: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0051: {
0052: CMyD3DApplication d3dApp;
0053:
0054: g_pApp = &d3dApp;
0055: g_hInst = hInst;
0056:
0057: InitCommonControls();
0058: if( FAILED( d3dApp.Create( hInst ) ) )
0059: return 0;
0060:
0061: return d3dApp.Run();
0062: }
0063:
0064:
0065:
0066:
0067:
0068:
0069:
0070:
0071:
0072:
0073:
0074:
0075:
0076: CMyD3DApplication::CMyD3DApplication()
0077: {
0078: int i;
0079: time_t t;
0080:
0081: time( &t );
0082: srand( t );
0083:
0084: m_iState = -1;
0085: m_bPause = false;
0086:
0087: m_Shader = 0;
0088:
0089: m_fDiffuse = 1.0f;
0090: m_fSpecular = 0.2f;
0091: m_fTranslucent = 3.0f;
0092:
0093: m_pos = D3DXVECTOR3(0,0,0);
0094: m_vel = D3DXVECTOR3(frand(),frand(),frand());
0095: m_rot = D3DXVECTOR3(0,0,0);
0096: m_omega = D3DXVECTOR3( 0, 1.0, 0 );
0097:
0098:
0099: m_EnvRate = 0;
0100: for(i=0;i<4;i++)
0101: {
0102: m_EnvRot[i] = D3DXVECTOR3( 2.0*D3DX_PI*frand(), 2.0*D3DX_PI*frand(), 2.0*D3DX_PI*frand() );
0103: }
0104: D3DXMATRIX mx,my,mz;
0105: D3DXMatrixRotationX( &mx, m_EnvRot[1].x );
0106: D3DXMatrixRotationY( &my, m_EnvRot[1].y );
0107: D3DXMatrixRotationZ( &mz, m_EnvRot[1].z );
0108: m_mEnv = mx*my*mz;
0109:
0110: m_pMesh = new CD3DMesh();
0111: m_pMeshBg = new CD3DMesh();
0112: m_pMeshEnv = new CD3DMesh();
0113:
0114: m_pMapZ = NULL;
0115: m_pPosTex = NULL;
0116: m_pPosSurf = NULL;
0117: m_pPosLockTex = NULL;
0118: m_pPosLockSurf = NULL;
0119: m_pNormalTex = NULL;
0120: m_pNormalSurf = NULL;
0121: m_pNormalLockTex = NULL;
0122: m_pNormalLockSurf = NULL;
0123: m_pParaboloidTex[0] = NULL;
0124: m_pParaboloidSurf[0] = NULL;
0125: m_pParaboloidTex[1] = NULL;
0126: m_pParaboloidSurf[1] = NULL;
0127: for(i=0;i<TEX_MAX;i++){
0128: m_pJacobianTex[i][0] = NULL;
0129: m_pJacobianTex[i][1] = NULL;
0130: }
0131:
0132: for( i = 0; i < REDUCTION_MAPS; i++ )
0133: {
0134: m_pReductionTex[i] = NULL;
0135: m_pReductionSurf[i] = NULL;
0136: }
0137: for( i=0;i<TEX_MAX;i++){
0138: m_pFinalTex[i] = NULL;
0139: m_pFinalSurf[i] = NULL;
0140: m_pFinalSSTex[i] = NULL;
0141: m_pFinalSSSurf[i] = NULL;
0142: }
0143: m_p64Tex = NULL;
0144: m_p64Surf = NULL;
0145: m_p64Tex2 = NULL;
0146: m_p64Surf2 = NULL;
0147: m_p64Z = NULL;
0148: m_p8Tex = NULL;
0149: m_p8Surf = NULL;
0150: m_pMaskTex = NULL;
0151:
0152: m_pEffect = NULL;
0153: m_hTechnique = NULL;
0154: m_hmWV = NULL;
0155: m_hmWVP = NULL;
0156: m_htSrcTex = NULL;
0157: m_fWidth = NULL;
0158: m_fHeight = NULL;
0159:
0160: m_fWorldRotX = -0.28f;
0161: m_fWorldRotY = D3DX_PI*0.5f;
0162: m_fViewZoom = 7.0f;
0163:
0164: m_dwCreationWidth = 512;
0165: m_dwCreationHeight = 512;
0166: m_strWindowTitle = TEXT( "main" );
0167: m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
0168: m_bStartFullscreen = false;
0169: m_bShowCursorWhenFullscreen = false;
0170:
0171: m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0172: m_bLoadingApp = TRUE;
0173:
0174: ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0175: }
0176:
0177:
0178:
0179:
0180:
0181:
0182:
0183:
0184:
0185: CMyD3DApplication::~CMyD3DApplication()
0186: {
0187: }
0188:
0189:
0190:
0191:
0192:
0193:
0194:
0195:
0196:
0197:
0198:
0199:
0200:
0201:
0202:
0203: HRESULT CMyD3DApplication::OneTimeSceneInit()
0204: {
0205:
0206:
0207: SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0208:
0209: m_bLoadingApp = FALSE;
0210:
0211: return S_OK;
0212: }
0213:
0214:
0215:
0216:
0217:
0218:
0219:
0220:
0221:
0222:
0223: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0224: DWORD dwBehavior, D3DFORMAT Format )
0225: {
0226: UNREFERENCED_PARAMETER( Format );
0227: UNREFERENCED_PARAMETER( dwBehavior );
0228: UNREFERENCED_PARAMETER( pCaps );
0229:
0230:
0231:
0232:
0233: if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0234: return E_FAIL;
0235:
0236:
0237:
0238: if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0239: && 0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0240: return E_FAIL;
0241:
0242: return S_OK;
0243: }
0244:
0245:
0246:
0247:
0248:
0249:
0250: typedef struct {
0251: int iRank;
0252: bool bFront;
0253: }sParaboloid;
0254:
0255: VOID WINAPI ParaboloidJacobian (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord,
0256: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0257: {
0258: UNREFERENCED_PARAMETER( pTexelSize );
0259:
0260: sParaboloid *p = (sParaboloid *)pData;
0261:
0262: FLOAT x = 2.0f*( pTexCoord->x-0.5f);
0263: FLOAT y = 2.0f*(-pTexCoord->y+0.5f);
0264:
0265: FLOAT r2 = x*x+y*y;
0266: FLOAT J = 0.5f*(1.0f+r2);
0267:
0268: FLOAT col = (r2<(1.0f-1.0f/(FLOAT)MAP_SIZE)*(1.0f-1.0f/(FLOAT)MAP_SIZE)) ? J : 0.0f;
0269:
0270:
0271: FLOAT nz = 0.5f * (1.0f-r2);
0272: FLOAT nx = x;
0273: FLOAT ny = y;
0274: FLOAT n = sqrtf(nx*nx+ny*ny+nz*nz);
0275: nx /= n;
0276: ny /= n;
0277: nz /= n;
0278: if(p->bFront) nz *= -1;
0279:
0280: pOut->x = 0.5f * col * (-nx) + 0.5f;
0281: pOut->y = 0.5f * col * ( ny) + 0.5f;
0282: pOut->z = 0.5f * col * (-nz) + 0.5f;
0283: pOut->w = 0.5f * col + 0.5f;
0284: }
0285:
0286:
0287:
0288:
0289: VOID WINAPI FillTex (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord,
0290: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0291: {
0292: UNREFERENCED_PARAMETER( pData );
0293:
0294: FLOAT x = 2.0f*(pTexCoord->x-0.5f);
0295: FLOAT y = 2.0f*(pTexCoord->y-0.5f);
0296: FLOAT col = (x*x+y*y<(1.0f-pTexelSize->x)*(1.0f-pTexelSize->y))
0297: ? 1.0f : 0.0f;
0298:
0299: pOut->x = pOut->y = pOut->z = pOut->w = col;
0300: }
0301:
0302:
0303:
0304:
0305:
0306:
0307:
0308:
0309:
0310:
0311:
0312:
0313:
0314:
0315: HRESULT CMyD3DApplication::InitDeviceObjects()
0316: {
0317: HRESULT hr;
0318: int i;
0319:
0320:
0321: #ifdef DX9C
0322: if(FAILED(hr=m_pMesh->Create( m_pd3dDevice, (LPCWSTR)("t-pot.x"))))
0323: return DXTRACE_ERR( "Load Model", hr );
0324:
0325: if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, (LPCWSTR)("map.x"))))
0326: return DXTRACE_ERR( "Load Model", hr );
0327:
0328: if(FAILED(hr=m_pMeshEnv->Create( m_pd3dDevice, (LPCWSTR)("room.x"))))
0329: return DXTRACE_ERR( "Load Model", hr );
0330: #else
0331: if(FAILED(hr=m_pMesh->Create( m_pd3dDevice, _T("t-pot.x"))))
0332: return DXTRACE_ERR( "Load Model", hr );
0333:
0334: if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, _T("map.x"))))
0335: return DXTRACE_ERR( "Load Model", hr );
0336:
0337: if(FAILED(hr=m_pMeshEnv->Create( m_pd3dDevice, _T("room.x"))))
0338: return DXTRACE_ERR( "Load Model", hr );
0339: #endif
0340: m_pMesh->UseMeshMaterials(FALSE);
0341: m_pMeshBg->UseMeshMaterials(FALSE);
0342: m_pMeshEnv->UseMeshMaterials(FALSE);
0343:
0344:
0345: sParaboloid data;
0346: for( i = 0; i < TEX_MAX; i++ )
0347: {
0348: data.iRank = i;
0349:
0350: data.bFront = 0;
0351: if( FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0352: , 0, D3DFMT_A8R8G8B8
0353: , D3DPOOL_MANAGED, &m_pJacobianTex[i][0], NULL)))
0354: return E_FAIL;
0355: if( FAILED(D3DXFillTexture(m_pJacobianTex[i][0], ParaboloidJacobian, &data)))
0356: return E_FAIL;
0357:
0358: data.bFront = 1;
0359: if( FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0360: , 0, D3DFMT_A8R8G8B8
0361: , D3DPOOL_MANAGED, &m_pJacobianTex[i][1], NULL)))
0362: return E_FAIL;
0363: if( FAILED(D3DXFillTexture(m_pJacobianTex[i][1], ParaboloidJacobian, &data)))
0364: return E_FAIL;
0365: }
0366:
0367:
0368: LPD3DXBUFFER pErr;
0369: if( FAILED( hr = D3DXCreateEffectFromFile(
0370: m_pd3dDevice, "hlsl.fx", NULL, NULL,
0371: D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0372: MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0373: , "ERROR", MB_OK);
0374: return DXTRACE_ERR( "CreateEffectFromFile", hr );
0375: }
0376: m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0377: m_hmWV = m_pEffect->GetParameterByName( NULL, "mWV" );
0378: m_hmWVP = m_pEffect->GetParameterByName( NULL, "mWVP" );
0379: m_htSrcTex = m_pEffect->GetParameterByName( NULL, "SrcTex" );
0380: m_fWidth = m_pEffect->GetParameterByName( NULL, "MAP_WIDTH" );
0381: m_fHeight = m_pEffect->GetParameterByName( NULL, "MAP_HEIGHT" );
0382:
0383:
0384: m_pFont->InitDeviceObjects( m_pd3dDevice );
0385:
0386: return S_OK;
0387: }
0388:
0389:
0390:
0391:
0392:
0393:
0394:
0395:
0396:
0397:
0398:
0399:
0400: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0401: {
0402: DWORD i;
0403:
0404: m_iState = -1;
0405: m_iChangeState = TRUE;
0406: m_bPause = false;
0407:
0408:
0409: m_pMesh->RestoreDeviceObjects( m_pd3dDevice );
0410: m_pMeshBg->RestoreDeviceObjects( m_pd3dDevice );
0411: m_pMeshEnv->RestoreDeviceObjects( m_pd3dDevice );
0412:
0413:
0414: D3DMATERIAL9 mtrl;
0415: D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0416: m_pd3dDevice->SetMaterial( &mtrl );
0417:
0418:
0419: m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
0420: m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
0421: m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
0422: m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x000F0F0F );
0423:
0424: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
0425: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0426: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0427: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
0428: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
0429: m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
0430: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0431: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0432: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0433: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0434:
0435:
0436: D3DXMATRIX matIdentity;
0437: D3DXMatrixIdentity( &m_mWorld );
0438:
0439:
0440: D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0441: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0442: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0443: D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0444:
0445:
0446: FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0447: D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0448:
0449:
0450: if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(MAP_SIZE, MAP_SIZE,
0451: D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL)))
0452: return E_FAIL;
0453:
0454: if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1,
0455: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pPosTex, NULL)))
0456: return E_FAIL;
0457: if (FAILED(m_pPosTex->GetSurfaceLevel(0, &m_pPosSurf)))
0458: return E_FAIL;
0459: if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1,
0460: 0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pPosLockTex, NULL)))
0461: return E_FAIL;
0462: if (FAILED(m_pPosLockTex->GetSurfaceLevel(0, &m_pPosLockSurf)))
0463: return E_FAIL;
0464:
0465: if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1,
0466: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pNormalTex, NULL)))
0467: return E_FAIL;
0468: if (FAILED(m_pNormalTex->GetSurfaceLevel(0, &m_pNormalSurf)))
0469: return E_FAIL;
0470: if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1,
0471: 0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pNormalLockTex, NULL)))
0472: return E_FAIL;
0473: if (FAILED(m_pNormalLockTex->GetSurfaceLevel(0, &m_pNormalLockSurf)))
0474: return E_FAIL;
0475:
0476: if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1,
0477: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pParaboloidTex[0], NULL)))
0478: return E_FAIL;
0479: if (FAILED(m_pParaboloidTex[0]->GetSurfaceLevel(0, &m_pParaboloidSurf[0])))
0480: return E_FAIL;
0481: if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1,
0482: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pParaboloidTex[1], NULL)))
0483: return E_FAIL;
0484: if (FAILED(m_pParaboloidTex[1]->GetSurfaceLevel(0, &m_pParaboloidSurf[1])))
0485: return E_FAIL;
0486:
0487:
0488: if( FAILED(m_pd3dDevice->CreateTexture( 64, 64, 1
0489: , 0, D3DFMT_A8R8G8B8
0490: , D3DPOOL_DEFAULT, &m_pMaskTex, NULL)))
0491: return E_FAIL;
0492: if( FAILED(D3DXFillTexture(m_pMaskTex, FillTex, NULL)))
0493: return E_FAIL;
0494:
0495:
0496: int size = 128;
0497: for( i = 0; i < REDUCTION_MAPS; i++ )
0498: {
0499: if (FAILED(m_pd3dDevice->CreateTexture(size, size, 1,
0500: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pReductionTex[i], NULL)))
0501: return E_FAIL;
0502: if (FAILED(m_pReductionTex[i]->GetSurfaceLevel(0, &m_pReductionSurf[i])))
0503: return E_FAIL;
0504: size /= 8;
0505: }
0506: if (FAILED(m_pd3dDevice->CreateTexture( 64, 64, 1,
0507: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_p64Tex, NULL)))
0508: return E_FAIL;
0509: if (FAILED(m_p64Tex->GetSurfaceLevel(0, &m_p64Surf)))
0510: return E_FAIL;
0511: if (FAILED(m_pd3dDevice->CreateTexture( 64, 64, 1,
0512: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_p64Tex2, NULL)))
0513: return E_FAIL;
0514: if (FAILED(m_p64Tex2->GetSurfaceLevel(0, &m_p64Surf2)))
0515: return E_FAIL;
0516: if (FAILED(m_pd3dDevice->CreateDepthStencilSurface( 64, 64,
0517: D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_p64Z, NULL)))
0518: return E_FAIL;
0519:
0520: if (FAILED(m_pd3dDevice->CreateTexture( 8, 8, 1,
0521: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_p8Tex, NULL)))
0522: return E_FAIL;
0523: if (FAILED(m_p8Tex->GetSurfaceLevel(0, &m_p8Surf)))
0524: return E_FAIL;
0525:
0526:
0527: for(i=0;i<TEX_MAX;i++){
0528: if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1,
0529: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFinalTex[i], NULL)))
0530: return E_FAIL;
0531: if (FAILED(m_pFinalTex[i]->GetSurfaceLevel(0, &m_pFinalSurf[i])))
0532: return E_FAIL;
0533: if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1,
0534: D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFinalSSTex[i], NULL)))
0535: return E_FAIL;
0536: if (FAILED(m_pFinalSSTex[i]->GetSurfaceLevel(0, &m_pFinalSSSurf[i])))
0537: return E_FAIL;
0538: }
0539:
0540:
0541: m_pEffect->OnResetDevice();
0542:
0543:
0544: m_pFont->RestoreDeviceObjects();
0545:
0546: return S_OK;
0547: }
0548:
0549: void CatmullRom( D3DXVECTOR3 *v, const D3DXVECTOR3 *v0, const D3DXVECTOR3 *v1, const D3DXVECTOR3 *v2, const D3DXVECTOR3 *v3,FLOAT rate)
0550: {
0551: const D3DXMATRIX mCatmullRom = D3DXMATRIX(
0552: -1.0/2.0, 3.0/2.0,-3.0/2.0, 1.0/2.0,
0553: 2.0/2.0,-5.0/2.0, 4.0/2.0,-1.0/2.0,
0554: -1.0/2.0, 0.0/2.0, 1.0/2.0, 0.0/2.0,
0555: 0.0/2.0, 2.0/2.0, 0.0/2.0, 0.0/2.0);
0556:
0557: D3DXVECTOR4 vr = D3DXVECTOR4(rate*rate*rate,rate*rate,rate,1);
0558: D3DXVECTOR4 v4;
0559:
0560: D3DXVec4Transform(&v4, &vr, &mCatmullRom);
0561:
0562: *v = v4.x * (*v0) + v4.y * (*v1) + v4.z * (*v2) + v4.w * (*v3);
0563: }
0564:
0565:
0566:
0567:
0568:
0569:
0570:
0571: HRESULT CMyD3DApplication::FrameMove()
0572: {
0573: m_iCount++;
0574:
0575: if(m_iChangeState) {m_iState++;m_iCount=0;m_iChangeState=0;}
0576:
0577: switch(m_iState){
0578: case 0:
0579: m_iChangeState = this->FrameMoveCreateMap();
0580: return S_OK;
0581: case 1:
0582: m_iChangeState = this->FrameMoveSSPRT();
0583: return S_OK;
0584: case 2:
0585: m_iChangeState = this->FrameMovePRT();
0586: return S_OK;
0587: default:
0588: break;
0589: }
0590:
0591:
0592: UpdateInput( &m_UserInput );
0593:
0594:
0595:
0596:
0597:
0598: D3DXMATRIX matRotY;
0599: D3DXMATRIX matRotX;
0600:
0601: if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0602: m_fWorldRotY += m_fElapsedTime;
0603: else
0604: if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0605: m_fWorldRotY -= m_fElapsedTime;
0606:
0607: if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0608: m_fWorldRotX += m_fElapsedTime;
0609: else
0610: if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0611: m_fWorldRotX -= m_fElapsedTime;
0612:
0613: D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0614: D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0615:
0616: D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0617:
0618:
0619:
0620:
0621:
0622: if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0623: m_fViewZoom += m_fElapsedTime;
0624: else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0625: m_fViewZoom -= m_fElapsedTime;
0626:
0627: D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0628: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.3f, 0.0f );
0629: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0630: D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0631:
0632:
0633:
0634:
0635: if( m_UserInput.bChangeShader ) m_Shader = (m_Shader + 1) % 3;
0636:
0637:
0638:
0639:
0640: if( m_UserInput.bPause ) m_bPause = !m_bPause;
0641:
0642:
0643:
0644:
0645: if( !m_bPause )
0646: {
0647: D3DXVec3Normalize( &m_vel, &m_vel );
0648: D3DXVec3Scale( &m_vel, &m_vel, 10 );
0649:
0650: m_rot.y += 1.0 * this->m_fElapsedTime;
0651: m_rot.y = (float)fmod(m_rot.y, 2.0f*D3DX_PI);
0652:
0653: float size = 5.0f - 1.0f;
0654:
0655: if( m_pos.x < -size )
0656: {
0657: m_pos.x = -size;
0658: m_vel.x *= -1;
0659: }
0660: if( size < m_pos.x )
0661: {
0662: m_pos.x = size;
0663: m_vel.x *= -1;
0664: }
0665: if( m_pos.y < -size )
0666: {
0667: m_pos.y = -size;
0668: m_vel.y *= -1;
0669: }
0670: if( size < m_pos.y )
0671: {
0672: m_pos.y = size;
0673: m_vel.y *= -1;
0674: }
0675: if( m_pos.z < -size )
0676: {
0677: m_pos.z = -size;
0678: m_vel.z *= -1;
0679: }
0680: if( size < m_pos.z )
0681: {
0682: m_pos.z = size;
0683: m_vel.z *= -1;
0684: }
0685:
0686: m_EnvRate += 0.1f * this->m_fElapsedTime;
0687: while(1.0f<m_EnvRate)
0688: {
0689: m_EnvRate -= 1.0f;
0690: for(DWORD i=0;i<3;i++)
0691: {
0692: m_EnvRot[i] = m_EnvRot[i+1];
0693: }
0694: m_EnvRot[3] = D3DXVECTOR3( 2.0*D3DX_PI*frand(),
0695: 2.0*D3DX_PI*frand(),
0696: 2.0*D3DX_PI*frand() );
0697: }
0698:
0699: D3DXVECTOR3 v;
0700: CatmullRom( &v, m_EnvRot+0, m_EnvRot+1, m_EnvRot+2, m_EnvRot+3, m_EnvRate);
0701: D3DXMATRIX mx,my,mz;
0702: D3DXMatrixRotationX( &mx, v.x );
0703: D3DXMatrixRotationY( &my, v.y );
0704: D3DXMatrixRotationZ( &mz, v.z );
0705: m_mEnv = mx*my*mz;
0706: }
0707:
0708:
0709:
0710:
0711: if( m_UserInput.bDiffuseUp && !m_UserInput.bDiffuseDown )
0712: m_fDiffuse += min(0.1f,m_fElapsedTime);
0713: else if( m_UserInput.bDiffuseDown && !m_UserInput.bDiffuseUp )
0714: m_fDiffuse -= min(0.1f,m_fElapsedTime);
0715: if(m_fDiffuse<0)m_fDiffuse=0;
0716:
0717: if( m_UserInput.bSpecularUp && !m_UserInput.bSpecularDown )
0718: m_fSpecular += min(0.1f,m_fElapsedTime);
0719: else if( m_UserInput.bSpecularDown && !m_UserInput.bSpecularUp )
0720: m_fSpecular -= min(0.1f,m_fElapsedTime);
0721: if(m_fSpecular<0)m_fSpecular=0;
0722:
0723: if( m_UserInput.bTranslucentUp && !m_UserInput.bTranslucentDown )
0724: m_fTranslucent += min(0.1f,m_fElapsedTime);
0725: else if( m_UserInput.bTranslucentDown && !m_UserInput.bTranslucentUp )
0726: m_fTranslucent -= min(0.1f,m_fElapsedTime);
0727: if(m_fTranslucent<0)m_fTranslucent=0;
0728:
0729: return S_OK;
0730: }
0731:
0732:
0733:
0734:
0735:
0736: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0737: {
0738: pUserInput->bPause = ( m_bActive && (GetAsyncKeyState( 'P' ) & 0x8001) == 0x8001 );
0739: pUserInput->bChangeShader = ( m_bActive && (GetAsyncKeyState( 'Q' ) & 0x8001) == 0x8001 );
0740:
0741: pUserInput->bRotateUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000) == 0x8000 );
0742: pUserInput->bRotateDown = ( m_bActive && (GetAsyncKeyState( VK_DOWN ) & 0x8000) == 0x8000 );
0743: pUserInput->bRotateLeft = ( m_bActive && (GetAsyncKeyState( VK_LEFT ) & 0x8000) == 0x8000 );
0744: pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0745:
0746: pUserInput->bZoomIn = ( m_bActive && (GetAsyncKeyState( 'Z' ) & 0x8000) == 0x8000 );
0747: pUserInput->bZoomOut = ( m_bActive && (GetAsyncKeyState( 'X' ) & 0x8000) == 0x8000 );
0748:
0749: pUserInput->bDiffuseUp = ( m_bActive && (GetAsyncKeyState( 'D' ) & 0x8000) == 0x8000 );
0750: pUserInput->bDiffuseDown = ( m_bActive && (GetAsyncKeyState( 'C' ) & 0x8000) == 0x8000 );
0751: pUserInput->bSpecularUp = ( m_bActive && (GetAsyncKeyState( 'F' ) & 0x8000) == 0x8000 );
0752: pUserInput->bSpecularDown = ( m_bActive && (GetAsyncKeyState( 'V' ) & 0x8000) == 0x8000 );
0753: pUserInput->bTranslucentUp = ( m_bActive && (GetAsyncKeyState( 'G' ) & 0x8000) == 0x8000 );
0754: pUserInput->bTranslucentDown = ( m_bActive && (GetAsyncKeyState( 'B' ) & 0x8000) == 0x8000 );
0755: }
0756:
0757:
0758:
0759:
0760:
0761:
0762:
0763:
0764: int CMyD3DApplication::FrameMoveCreateMap()
0765: {
0766:
0767: if(1<m_iCount) return 1;
0768:
0769: return 0;
0770: }
0771:
0772:
0773:
0774:
0775:
0776:
0777: void CMyD3DApplication::RenderCreateMap()
0778: {
0779: LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0780: D3DVIEWPORT9 oldViewport;
0781:
0782: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0783: {
0784:
0785:
0786:
0787: m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0788: m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0789: m_pd3dDevice->GetViewport(&oldViewport);
0790:
0791:
0792:
0793:
0794: m_pd3dDevice->SetRenderTarget(0, m_pPosSurf);
0795: m_pd3dDevice->SetRenderTarget(1, m_pNormalSurf);
0796: m_pd3dDevice->SetDepthStencilSurface(NULL);
0797:
0798: D3DVIEWPORT9 viewport_height = {0,0
0799: , DECALE_MAP_SIZE
0800: , DECALE_MAP_SIZE
0801: , 0.0f,1.0f};
0802: m_pd3dDevice->SetViewport(&viewport_height);
0803:
0804:
0805:
0806:
0807: m_pd3dDevice->Clear(0L, NULL
0808: , D3DCLEAR_TARGET
0809: , 0xffffffff, 1.0f, 0L);
0810:
0811: if( m_pEffect != NULL )
0812: {
0813:
0814:
0815:
0816: m_pEffect->SetTechnique( m_hTechnique );
0817: m_pEffect->Begin( NULL, 0 );
0818:
0819: m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
0820: m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
0821:
0822:
0823:
0824:
0825: BEGIN_PASS(10);
0826: m_pMesh->Render(m_pd3dDevice);
0827: END_PASS();
0828:
0829: m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
0830: m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
0831:
0832: m_pEffect->End();
0833: }
0834:
0835:
0836:
0837:
0838: m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0839: m_pd3dDevice->SetRenderTarget(1, NULL);
0840: m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0841: m_pd3dDevice->SetViewport(&oldViewport);
0842: pOldBackBuffer->Release();
0843: pOldZBuffer->Release();
0844:
0845:
0846:
0847:
0848: m_pd3dDevice->Clear(0L, NULL
0849: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0850: , 0xff000000, 1.0f, 0L);
0851:
0852: m_pd3dDevice->EndScene();
0853: }
0854: }
0855:
0856:
0857:
0858:
0859:
0860:
0861: static int itex=0;
0862: static int ix=0;
0863: static int iy=0;
0864: static float pos[DECALE_MAP_SIZE][DECALE_MAP_SIZE][3];
0865: static float normal[DECALE_MAP_SIZE][DECALE_MAP_SIZE][3];
0866: int CMyD3DApplication::FrameMoveSSPRT()
0867: {
0868: itex = this->m_iCount / (DECALE_MAP_SIZE*DECALE_MAP_SIZE);
0869: ix = this->m_iCount % DECALE_MAP_SIZE;
0870: iy = (this->m_iCount / DECALE_MAP_SIZE) % DECALE_MAP_SIZE;
0871:
0872: if(0==this->m_iCount){
0873: D3DLOCKED_RECT d3dlr;
0874: float *p;
0875: m_pd3dDevice->GetRenderTargetData(m_pPosSurf,m_pPosLockSurf);
0876: m_pPosLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY);
0877: p = (float *)d3dlr.pBits;
0878: for(int y=0;y<DECALE_MAP_SIZE;y++){
0879: for(int x=0;x<DECALE_MAP_SIZE;x++){
0880: pos[x][y][0]=p[0];
0881: pos[x][y][1]=p[1];
0882: pos[x][y][2]=p[2];
0883: p+=4;
0884: }
0885: }
0886: m_pPosLockSurf->UnlockRect();
0887:
0888: m_pd3dDevice->GetRenderTargetData(m_pNormalSurf,m_pNormalLockSurf);
0889: m_pNormalLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY);
0890: p = (float *)d3dlr.pBits;
0891: for( y=0;y<DECALE_MAP_SIZE;y++){
0892: for( int x=0;x<DECALE_MAP_SIZE;x++){
0893: normal[x][y][0]=p[0];
0894: normal[x][y][1]=p[1];
0895: normal[x][y][2]=p[2];
0896: p+=4;
0897: }
0898: }
0899: m_pNormalLockSurf->UnlockRect();
0900: }
0901:
0902:
0903: if(DECALE_MAP_SIZE-1==ix && DECALE_MAP_SIZE-1==iy && TEX_MAX-1==itex) return 1;
0904:
0905: return 0;
0906: }
0907:
0908:
0909:
0910:
0911:
0912:
0913: void CMyD3DApplication::RenderSSPRT()
0914: {
0915: D3DXMATRIX m, mView;
0916: LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0917: D3DVIEWPORT9 oldViewport;
0918: D3DSURFACE_DESC d3dsd;
0919:
0920: D3DVIEWPORT9 viewport = {0,0
0921: , 1, 1
0922: , 0.0f,1.0f};
0923:
0924: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0925: {
0926:
0927:
0928:
0929: m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0930: m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0931: m_pd3dDevice->GetViewport(&oldViewport);
0932:
0933:
0934:
0935:
0936:
0937:
0938:
0939:
0940:
0941:
0942:
0943: m_pReductionSurf[0]->GetDesc(&d3dsd);
0944: viewport.Height = d3dsd.Width;
0945: viewport.Width = d3dsd.Height;
0946: m_pd3dDevice->SetViewport(&viewport);
0947:
0948: if( m_pEffect != NULL )
0949: {
0950:
0951:
0952:
0953: float x[3] = {pos[ix][iy][0], pos[ix][iy][1], pos[ix][iy][2]};
0954: float n[3] = {normal[ix][iy][0], normal[ix][iy][1], normal[ix][iy][2]};
0955: D3DXVECTOR3 vFromPt = D3DXVECTOR3( x[0], x[1], x[2] ) - 0.05f*D3DXVECTOR3( n[0], n[1], n[2] );
0956: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( -n[0], -n[1], -n[2] ) + vFromPt;
0957: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0958: D3DXMatrixLookAtLH( &mView, &vFromPt, &vLookatPt, &vUpVec );
0959: m_pEffect->SetMatrix( "mWV", &mView );
0960:
0961: D3DXMatrixTranspose(&m, &mView);
0962: m_pEffect->SetMatrix( "mST", &m );
0963:
0964:
0965: m_pEffect->SetTechnique( m_hTechnique );
0966: m_pEffect->Begin( NULL, 0 );
0967:
0968:
0969:
0970:
0971: m_pd3dDevice->SetRenderTarget(0, m_p64Surf2 );
0972: m_pd3dDevice->SetDepthStencilSurface( m_p64Z );
0973: m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L);
0974: BEGIN_PASS(16);
0975: m_pMesh->Render(m_pd3dDevice);
0976: END_PASS();
0977:
0978:
0979:
0980:
0981: TVERTEX Vertex[4] = {
0982:
0983: {-1,-1,0, 0, 0,},
0984: {+1,-1,0, 1, 0,},
0985: {+1,+1,0, 1, 1,},
0986: {-1,+1,0, 0, 1,},
0987: };
0988: m_pd3dDevice->SetRenderTarget(0, m_p64Surf );
0989: m_pd3dDevice->SetDepthStencilSurface(NULL);
0990: m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
0991: m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
0992:
0993: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0994: m_pEffect->SetTexture(m_htSrcTex, m_pMaskTex);
0995: m_pEffect->SetTexture( "SSTex", m_p64Tex2 );
0996: BEGIN_PASS( 17 + itex );
0997: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0998: END_PASS();
0999:
1000: m_pEffect->End();
1001: }
1002:
1003:
1004:
1005:
1006:
1007: m_pd3dDevice->SetRenderTarget( 0, m_p8Surf );
1008: m_pd3dDevice->SetDepthStencilSurface(NULL);
1009:
1010: m_p8Surf->GetDesc(&d3dsd);
1011: viewport.Height = d3dsd.Width;
1012: viewport.Width = d3dsd.Height;
1013: m_pd3dDevice->SetViewport(&viewport);
1014:
1015: if( m_pEffect != NULL ) {
1016:
1017:
1018:
1019: m_pEffect->SetTechnique( m_hTechnique );
1020: m_pEffect->Begin( NULL, 0 );
1021:
1022: m_pEffect->SetFloat( m_fWidth, 64);
1023: m_pEffect->SetFloat( m_fHeight, 64);
1024:
1025:
1026:
1027:
1028: TVERTEX Vertex1[4] = {
1029:
1030: {-1.0f, +1.0f, 0.1f, 0, 0},
1031: {+1.0f, +1.0f, 0.1f, 1, 0},
1032: {+1.0f, -1.0f, 0.1f, 1, 1},
1033: {-1.0f, -1.0f, 0.1f, 0, 1},
1034: };
1035: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1036: m_pEffect->SetTexture( m_htSrcTex, m_p64Tex );
1037: BEGIN_PASS( 3 );
1038: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1039: , 2, Vertex1, sizeof( TVERTEX ) );
1040: END_PASS();
1041:
1042: m_pEffect->End();
1043: }
1044:
1045:
1046:
1047:
1048:
1049:
1050: m_pd3dDevice->SetRenderTarget(0, m_pFinalSSSurf[itex]);
1051: m_pd3dDevice->SetDepthStencilSurface(NULL);
1052:
1053: m_pFinalSSSurf[itex]->GetDesc(&d3dsd);
1054: viewport.Height = d3dsd.Width;
1055: viewport.Width = d3dsd.Height;
1056: m_pd3dDevice->SetViewport(&viewport);
1057:
1058:
1059: if(0==ix&&0==iy)m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0x80808080, 1.0f, 0L);
1060:
1061: if( m_pEffect != NULL ) {
1062:
1063:
1064:
1065: m_pEffect->SetTechnique( m_hTechnique );
1066: m_pEffect->Begin( NULL, 0 );
1067:
1068: m_pEffect->SetFloat( m_fWidth, 8);
1069: m_pEffect->SetFloat( m_fHeight, 8);
1070:
1071:
1072:
1073:
1074: float x = 2.0f*((float)ix/(float)DECALE_MAP_SIZE) - 1.0f;
1075: float y = -2.0f*((float)iy/(float)DECALE_MAP_SIZE) + 1.0f;
1076: TVERTEX Vertex1[4] = {
1077:
1078: {x, y, 0.1f, 0, 0},
1079: {x+2.0f/(float)DECALE_MAP_SIZE, y, 0.1f, 1, 0},
1080: {x+2.0f/(float)DECALE_MAP_SIZE, y-2.0f/(float)DECALE_MAP_SIZE,0.1f, 1, 1},
1081: {x, y-2.0f/(float)DECALE_MAP_SIZE,0.1f, 0, 1},
1082: };
1083: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1084: m_pEffect->SetTexture( m_htSrcTex, m_p8Tex );
1085: BEGIN_PASS(3);
1086: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1087: , 2, Vertex1, sizeof( TVERTEX ) );
1088: END_PASS();
1089:
1090: m_pEffect->End();
1091: }
1092:
1093:
1094:
1095:
1096:
1097: m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
1098: m_pd3dDevice->SetRenderTarget(1, NULL);
1099: m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
1100: m_pd3dDevice->SetViewport(&oldViewport);
1101: pOldBackBuffer->Release();
1102: pOldZBuffer->Release();
1103:
1104: m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
1105: m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
1106:
1107:
1108:
1109:
1110: m_pd3dDevice->Clear(0L, NULL
1111: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1112: , 0x00001020, 1.0f, 0L);
1113:
1114: #if 1
1115: {
1116: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1117: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
1118: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
1119: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1120: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
1121: m_pd3dDevice->SetVertexShader(NULL);
1122: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1123: m_pd3dDevice->SetPixelShader(0);
1124: float scale = 256.0f*2/3;
1125: for(DWORD i=0; i<3; i++){
1126: TVERTEX Vertex[4] = {
1127:
1128: { 0,(i+0)*scale,0, 1, 0, 0,},
1129: {scale,(i+0)*scale,0, 1, 1, 0,},
1130: {scale,(i+1)*scale,0, 1, 1, 1,},
1131: { 0,(i+1)*scale,0, 1, 0, 1,},
1132: };
1133: if(0==i) m_pd3dDevice->SetTexture( 0, m_pFinalSSTex[itex] );
1134: if(1==i) m_pd3dDevice->SetTexture( 0, m_p64Tex );
1135: if(2==i) m_pd3dDevice->SetTexture( 0, m_p8Tex );
1136: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1137: }
1138: }
1139: #endif
1140:
1141: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
1142: TCHAR szMsg[MAX_PATH] = TEXT("");
1143:
1144: sprintf( szMsg, TEXT("Precomputing 1/2\nx:%4d\ny:%4d"), ix, iy);
1145: m_pFont->DrawText( 2, (FLOAT)m_d3dsdBackBuffer.Height-60, fontColor, szMsg );
1146:
1147: m_pd3dDevice->EndScene();
1148: }
1149: }
1150:
1151:
1152:
1153:
1154:
1155:
1156: int CMyD3DApplication::FrameMovePRT()
1157: {
1158: itex = this->m_iCount / (DECALE_MAP_SIZE*DECALE_MAP_SIZE);
1159: ix = this->m_iCount % DECALE_MAP_SIZE;
1160: iy = (this->m_iCount / DECALE_MAP_SIZE) % DECALE_MAP_SIZE;
1161:
1162: if(0==this->m_iCount){
1163: #if 0
1164: D3DLOCKED_RECT d3dlr;
1165: float *p;
1166: m_pd3dDevice->GetRenderTargetData(m_pPosSurf,m_pPosLockSurf);
1167: m_pPosLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY);
1168: p = (float *)d3dlr.pBits;
1169: for(int y=0;y<DECALE_MAP_SIZE;y++){
1170: for(int x=0;x<DECALE_MAP_SIZE;x++){
1171: pos[x][y][0]=p[0];
1172: pos[x][y][1]=p[1];
1173: pos[x][y][2]=p[2];
1174: p+=4;
1175: }
1176: }
1177: m_pPosLockSurf->UnlockRect();
1178:
1179: m_pd3dDevice->GetRenderTargetData(m_pNormalSurf,m_pNormalLockSurf);
1180: m_pNormalLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY);
1181: p = (float *)d3dlr.pBits;
1182: for( y=0;y<DECALE_MAP_SIZE;y++){
1183: for( int x=0;x<DECALE_MAP_SIZE;x++){
1184: normal[x][y][0]=p[0];
1185: normal[x][y][1]=p[1];
1186: normal[x][y][2]=p[2];
1187: p+=4;
1188: }
1189: }
1190: m_pNormalLockSurf->UnlockRect();
1191: #endif
1192: }
1193:
1194:
1195: if(DECALE_MAP_SIZE-1==ix && DECALE_MAP_SIZE-1==iy && TEX_MAX-1==itex) return 1;
1196:
1197: return 0;
1198: }
1199:
1200:
1201:
1202:
1203:
1204:
1205: void CMyD3DApplication::RenderPRT()
1206: {
1207: D3DXMATRIX m, mView;
1208: LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
1209: D3DVIEWPORT9 oldViewport;
1210: D3DSURFACE_DESC d3dsd;
1211:
1212: D3DVIEWPORT9 viewport = {0,0
1213: , 1, 1
1214: , 0.0f,1.0f};
1215:
1216: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
1217: {
1218: m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
1219: m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
1220:
1221:
1222:
1223:
1224: m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
1225: m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
1226: m_pd3dDevice->GetViewport(&oldViewport);
1227:
1228:
1229:
1230:
1231:
1232:
1233:
1234:
1235:
1236:
1237: m_pd3dDevice->SetRenderTarget(0, m_p64Surf );
1238: m_pd3dDevice->SetDepthStencilSurface(NULL);
1239:
1240: m_p64Surf->GetDesc(&d3dsd);
1241: viewport.Height = d3dsd.Width;
1242: viewport.Width = d3dsd.Height;
1243: m_pd3dDevice->SetViewport(&viewport);
1244:
1245: if( m_pEffect != NULL )
1246: {
1247:
1248:
1249:
1250: float x[3] = {pos[ix][iy][0], pos[ix][iy][1], pos[ix][iy][2]};
1251: float n[3] = {normal[ix][iy][0], normal[ix][iy][1], normal[ix][iy][2]};
1252: D3DXVECTOR3 vFromPt = D3DXVECTOR3( x[0], x[1], x[2] ) + 0.01f*D3DXVECTOR3( n[0], n[1], n[2] );
1253: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( n[0], n[1], n[2] ) + vFromPt;
1254: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
1255: D3DXMatrixLookAtLH( &mView, &vFromPt, &vLookatPt, &vUpVec );
1256: m_pEffect->SetMatrix( "mWV", &mView );
1257:
1258: D3DXMatrixTranspose(&m, &mView);
1259: m_pEffect->SetMatrix( "mST", &m );
1260:
1261:
1262: m_pEffect->SetTechnique( "TShader" );
1263: m_pEffect->Begin( NULL, 0 );
1264:
1265: TVERTEX Vertex[4] = {
1266:
1267: {-1,-1,0, 0, 0,},
1268: {+1,-1,0, 1, 0,},
1269: {+1,+1,0, 1, 1,},
1270: {-1,+1,0, 0, 1,},
1271: };
1272: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1273: m_pEffect->SetTexture(m_htSrcTex, m_pMaskTex);
1274: BEGIN_PASS( 11 + itex );
1275: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1276: END_PASS();
1277:
1278:
1279:
1280:
1281: BEGIN_PASS( 15 );
1282: m_pMesh->Render(m_pd3dDevice);
1283: END_PASS();
1284: m_pEffect->End();
1285: }
1286:
1287:
1288:
1289:
1290:
1291: m_pd3dDevice->SetRenderTarget( 0, m_p8Surf );
1292: m_pd3dDevice->SetDepthStencilSurface(NULL);
1293:
1294: m_p8Surf->GetDesc(&d3dsd);
1295: viewport.Height = d3dsd.Width;
1296: viewport.Width = d3dsd.Height;
1297: m_pd3dDevice->SetViewport(&viewport);
1298:
1299: if( m_pEffect != NULL ) {
1300:
1301:
1302:
1303: m_pEffect->SetTechnique( m_hTechnique );
1304: m_pEffect->Begin( NULL, 0 );
1305:
1306: m_pEffect->SetFloat( m_fWidth, 64);
1307: m_pEffect->SetFloat( m_fHeight, 64);
1308:
1309:
1310:
1311:
1312: TVERTEX Vertex1[4] = {
1313:
1314: {-1.0f, +1.0f, 0.1f, 0, 0},
1315: {+1.0f, +1.0f, 0.1f, 1, 0},
1316: {+1.0f, -1.0f, 0.1f, 1, 1},
1317: {-1.0f, -1.0f, 0.1f, 0, 1},
1318: };
1319: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1320: m_pEffect->SetTexture( m_htSrcTex, m_p64Tex );
1321: BEGIN_PASS( 3 );
1322: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1323: , 2, Vertex1, sizeof( TVERTEX ) );
1324: END_PASS();
1325:
1326: m_pEffect->End();
1327: }
1328:
1329:
1330:
1331:
1332:
1333:
1334: m_pd3dDevice->SetRenderTarget(0, m_pFinalSurf[itex]);
1335: m_pd3dDevice->SetDepthStencilSurface(NULL);
1336:
1337: m_pFinalSurf[itex]->GetDesc(&d3dsd);
1338: viewport.Height = d3dsd.Width;
1339: viewport.Width = d3dsd.Height;
1340: m_pd3dDevice->SetViewport(&viewport);
1341:
1342:
1343: if(0==ix&&0==iy)m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0x80808080, 1.0f, 0L);
1344:
1345: if( m_pEffect != NULL ) {
1346:
1347:
1348:
1349: m_pEffect->SetTechnique( m_hTechnique );
1350: m_pEffect->Begin( NULL, 0 );
1351:
1352: m_pEffect->SetFloat( m_fWidth, 8);
1353: m_pEffect->SetFloat( m_fHeight, 8);
1354:
1355:
1356:
1357:
1358: float x = 2.0f*((float)ix/(float)DECALE_MAP_SIZE) - 1.0f;
1359: float y = -2.0f*((float)iy/(float)DECALE_MAP_SIZE) + 1.0f;
1360: TVERTEX Vertex1[4] = {
1361:
1362: {x, y, 0.1f, 0, 0},
1363: {x+2.0f/(float)DECALE_MAP_SIZE, y, 0.1f, 1, 0},
1364: {x+2.0f/(float)DECALE_MAP_SIZE, y-2.0f/(float)DECALE_MAP_SIZE,0.1f, 1, 1},
1365: {x, y-2.0f/(float)DECALE_MAP_SIZE,0.1f, 0, 1},
1366: };
1367: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1368: m_pEffect->SetTexture( m_htSrcTex, m_p8Tex );
1369: BEGIN_PASS( 3 );
1370: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1371: , 2, Vertex1, sizeof( TVERTEX ) );
1372: END_PASS();
1373:
1374: m_pEffect->End();
1375: }
1376:
1377:
1378:
1379:
1380:
1381: m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
1382: m_pd3dDevice->SetRenderTarget(1, NULL);
1383: m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
1384: m_pd3dDevice->SetViewport(&oldViewport);
1385: pOldBackBuffer->Release();
1386: pOldZBuffer->Release();
1387:
1388: m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
1389: m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
1390:
1391:
1392:
1393:
1394: m_pd3dDevice->Clear(0L, NULL
1395: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1396: , 0x00001020, 1.0f, 0L);
1397:
1398: #if 1
1399: {
1400: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1401: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
1402: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
1403: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1404: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
1405: m_pd3dDevice->SetVertexShader(NULL);
1406: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1407: m_pd3dDevice->SetPixelShader(0);
1408: float scale = 256.0f*2/3;
1409: for(DWORD i=0; i<3; i++){
1410: TVERTEX Vertex[4] = {
1411:
1412: { 0,(i+0)*scale,0, 1, 0, 0,},
1413: {scale,(i+0)*scale,0, 1, 1, 0,},
1414: {scale,(i+1)*scale,0, 1, 1, 1,},
1415: { 0,(i+1)*scale,0, 1, 0, 1,},
1416: };
1417: if(0==i) m_pd3dDevice->SetTexture( 0, m_pFinalTex[itex] );
1418: if(1==i) m_pd3dDevice->SetTexture( 0, m_p64Tex );
1419: if(2==i) m_pd3dDevice->SetTexture( 0, m_p8Tex );
1420: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1421: }
1422: }
1423: #endif
1424:
1425: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
1426: TCHAR szMsg[MAX_PATH] = TEXT("");
1427:
1428: sprintf( szMsg, TEXT("Precomputing 2/2\nx:%4d\ny:%4d"), ix, iy);
1429: m_pFont->DrawText( 2, (FLOAT)m_d3dsdBackBuffer.Height-60, fontColor, szMsg );
1430:
1431: m_pd3dDevice->EndScene();
1432: }
1433: }
1434:
1435:
1436:
1437:
1438:
1439: void CMyD3DApplication::RenderParaboloidMap()
1440: {
1441: LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
1442: D3DVIEWPORT9 oldViewport;
1443: D3DMATERIAL9 *pMtrl;
1444: int i, j, k;
1445: D3DXMATRIX m, mT, mR, mW, mView, mProj;
1446: D3DXVECTOR3 vFrom;
1447: D3DXVECTOR3 vLookat;
1448: D3DXVECTOR3 vUpVec;
1449:
1450: D3DVIEWPORT9 viewport = {0, 0
1451: , 1, 1
1452: , 0.0f,1.0f};
1453:
1454:
1455:
1456:
1457: m_pd3dDevice->GetRenderTarget( 0, &pOldBackBuffer );
1458: m_pd3dDevice->GetDepthStencilSurface( &pOldZBuffer );
1459: m_pd3dDevice->GetViewport( &oldViewport );
1460:
1461:
1462:
1463:
1464: if( m_pEffect != NULL )
1465: {
1466:
1467: m_pEffect->SetTechnique( m_hTechnique );
1468: m_pEffect->Begin( NULL, 0 );
1469:
1470: for( i = 0; i < 2; i++ )
1471: {
1472:
1473: m_pd3dDevice->SetRenderTarget( 0, m_pParaboloidSurf[i] );
1474: m_pd3dDevice->SetDepthStencilSurface( m_pMapZ );
1475: viewport.Width = viewport.Height = MAP_SIZE;
1476: m_pd3dDevice->SetViewport( &viewport );
1477:
1478:
1479: m_pd3dDevice->Clear(0L, NULL
1480: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1481: , 0xff000000, 1.0f, 0L);
1482:
1483:
1484:
1485:
1486: D3DXMatrixRotationX( &mW, m_rot.x );
1487: D3DXMatrixRotationY( &mR, m_rot.y );
1488: mW = mW * mR;
1489: D3DXMatrixRotationZ( &mR, m_rot.z );
1490: mW = mW * mR;
1491: D3DXMatrixTranslation( &mT, m_pos.x, m_pos.y, m_pos.z );
1492: mW = mW * mT;
1493:
1494: vFrom = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
1495: D3DXVec3TransformCoord( &vFrom, &vFrom, &mW);
1496: switch(i)
1497: {
1498: case 0:
1499:
1500: vLookat = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
1501: break;
1502: case 1:
1503:
1504: vLookat = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
1505: break;
1506: }
1507: D3DXVec3TransformCoord( &vLookat, &vLookat, &mW);
1508: vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
1509: D3DXVec3TransformCoord( &vUpVec, &vUpVec, &mW);
1510: vUpVec -= vFrom;
1511: D3DXMatrixLookAtLH( &mView, &vFrom, &vLookat, &vUpVec );
1512:
1513:
1514: m = mView;
1515: D3DXMatrixTranslation( &mT, 0, -3, 0 );
1516: m = mT * m;
1517:
1518:
1519:
1520: m_pEffect->SetMatrix( m_hmWV, &m );
1521:
1522: pMtrl = m_pMeshBg->m_pMaterials;
1523: for( unsigned int s=0; s<m_pMeshBg->m_dwNumMaterials; s++ ) {
1524: m_pEffect->SetTexture(m_htSrcTex, m_pMeshBg->m_pTextures[s] );
1525: BEGIN_PASS( 4 );
1526: m_pMeshBg->m_pLocalMesh->DrawSubset( s );
1527: END_PASS();
1528: pMtrl++;
1529: }
1530:
1531:
1532: D3DXMatrixScaling ( &m, 100, 100, 100 );
1533: m = m_mEnv * m * mView;
1534: m_pEffect->SetMatrix( m_hmWV, &m );
1535:
1536: pMtrl = m_pMeshEnv->m_pMaterials;
1537: for( s=0; s<m_pMeshEnv->m_dwNumMaterials; s++ ) {
1538: m_pEffect->SetTexture(m_htSrcTex, m_pMeshEnv->m_pTextures[s] );
1539: BEGIN_PASS( 4 );
1540: m_pMeshEnv->m_pLocalMesh->DrawSubset( s );
1541: END_PASS();
1542: pMtrl++;
1543: }
1544: }
1545:
1546:
1547:
1548:
1549: m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[0]);
1550: m_pd3dDevice->SetDepthStencilSurface(NULL);
1551: viewport.Width = viewport.Height = 128;
1552: m_pd3dDevice->SetViewport(&viewport);
1553:
1554:
1555:
1556:
1557: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX2 );
1558: m_pEffect->SetTexture( "ParaboloidFrontTex", m_pParaboloidTex[0] );
1559: m_pEffect->SetTexture( "ParaboloidBackTex", m_pParaboloidTex[1] );
1560: m_pEffect->SetTexture( "WeightTexFront", m_pJacobianTex[0][0] );
1561: m_pEffect->SetTexture( "WeightTexBack", m_pJacobianTex[0][1] );
1562: float scale = 64;
1563: int pass = 6;
1564: for( j = 0; j < 2; j++)
1565: {
1566: for( k = 0; k < 2; k++)
1567: {
1568: TVERTEX Vertex[4] = {
1569:
1570: {(j+0)*scale,(k+0)*scale, 0, 1, 0+1/128, 0+1/128 },
1571: {(j+1)*scale,(k+0)*scale, 0, 1, 1+1/128, 0+1/128 },
1572: {(j+1)*scale,(k+1)*scale, 0, 1, 1+1/128, 1+1/128 },
1573: {(j+0)*scale,(k+1)*scale, 0, 1, 0+1/128, 1+1/128 },
1574: };
1575: BEGIN_PASS( pass++ );
1576: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1577: END_PASS();
1578: }
1579: }
1580:
1581:
1582:
1583: m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[1]);
1584: viewport.Width = viewport.Height = 16;
1585: m_pd3dDevice->SetViewport(&viewport);
1586:
1587: TVERTEX Vertex1[4] = {
1588:
1589: {-1.0f, +1.0f, 0.5f, 0, 0},
1590: {+1.0f, +1.0f, 0.5f, 1, 0},
1591: {+1.0f, -1.0f, 0.5f, 1, 1},
1592: {-1.0f, -1.0f, 0.5f, 0, 1},
1593: };
1594: m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1595:
1596: m_pEffect->SetFloat( m_fWidth, 128 );
1597: m_pEffect->SetFloat( m_fHeight, 128 );
1598: m_pEffect->SetTexture( m_htSrcTex, m_pReductionTex[0] );
1599: BEGIN_PASS( 3 );
1600: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1601: , 2, Vertex1, sizeof( TVERTEX ) );
1602: END_PASS();
1603:
1604:
1605:
1606: m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[2]);
1607: viewport.Width = viewport.Height = 2;
1608: m_pd3dDevice->SetViewport(&viewport);
1609:
1610: m_pEffect->SetFloat( m_fWidth, 16 );
1611: m_pEffect->SetFloat( m_fHeight, 16 );
1612: m_pEffect->SetTexture(m_htSrcTex, m_pReductionTex[1]);
1613: BEGIN_PASS( 3 );
1614: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1615: , 2, Vertex1, sizeof( TVERTEX ) );
1616: END_PASS();
1617:
1618: m_pEffect->End();
1619: }
1620:
1621:
1622:
1623:
1624:
1625:
1626: m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
1627: m_pd3dDevice->SetRenderTarget(1, NULL);
1628: m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
1629: m_pd3dDevice->SetViewport(&oldViewport);
1630: pOldBackBuffer->Release();
1631: pOldZBuffer->Release();
1632:
1633: }
1634:
1635:
1636:
1637:
1638:
1639:
1640:
1641: HRESULT CMyD3DApplication::Render()
1642: {
1643: D3DXMATRIX m, mT, mR, mView, mProj;
1644: DWORD i;
1645: D3DXVECTOR4 v;
1646: D3DMATERIAL9 *pMtrl;
1647:
1648: switch(m_iState){
1649: case 0:
1650: this->RenderCreateMap();
1651: return S_OK;
1652: case 1:
1653: this->RenderSSPRT();
1654: return S_OK;
1655: case 2:
1656: this->RenderPRT();
1657: return S_OK;
1658: default:
1659: break;
1660: }
1661:
1662:
1663:
1664: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
1665: {
1666:
1667:
1668:
1669: RenderParaboloidMap();
1670:
1671:
1672: m_pd3dDevice->Clear(0L, NULL
1673: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1674: , 0xff000000, 1.0f, 0L);
1675:
1676: if( m_pEffect != NULL )
1677: {
1678:
1679:
1680:
1681: m_pEffect->SetTechnique( m_hTechnique );
1682: m_pEffect->Begin( NULL, 0 );
1683:
1684:
1685:
1686:
1687:
1688: D3DXMatrixRotationX( &mR, m_rot.x );
1689: D3DXMatrixRotationY( &m, m_rot.y );
1690: mR = mR * m;
1691: D3DXMatrixRotationZ( &m, m_rot.z );
1692: mR = mR * m;
1693: D3DXMatrixTranslation( &mT, m_pos.x, m_pos.y, m_pos.z );
1694: m = mR * mT * m_mWorld * m_mView;
1695: m_pEffect->SetMatrix( m_hmWV, &m );
1696: m = m * m_mProj;
1697: m_pEffect->SetMatrix( m_hmWVP, &m );
1698:
1699:
1700: m = mR * mT * m_mWorld * m_mView;
1701: D3DXMatrixInverse( &m, NULL, &m);
1702: v = D3DXVECTOR4( 0, 0, 0, 1 );
1703: D3DXVec4Transform( &v, &v, &m);
1704: m_pEffect->SetVector( "vEye", &v );
1705:
1706: m_pEffect->SetFloat( "diffuse", m_fDiffuse );
1707: m_pEffect->SetFloat( "specular", m_fSpecular );
1708: m_pEffect->SetFloat( "translucent", m_fTranslucent );
1709:
1710:
1711: m_pEffect->SetTexture( "ParaboloidFrontTex", m_pParaboloidTex[0] );
1712: m_pEffect->SetTexture( "ParaboloidBackTex", m_pParaboloidTex[1] );
1713: m_pEffect->SetTexture( m_htSrcTex, m_pReductionTex[2]);
1714:
1715: m_pEffect->SetTexture( "PRTTex", m_pFinalTex[0] );
1716: m_pEffect->SetTexture( "SSTex", m_pFinalSSTex[0] );
1717:
1718: m_pEffect->SetTexture( "DecaleTex", m_pMesh->m_pTextures[0] );
1719: BEGIN_PASS( 0+m_Shader );
1720: m_pMesh->Render( m_pd3dDevice );
1721: END_PASS();
1722:
1723:
1724:
1725:
1726:
1727: m = m_mWorld * m_mView * m_mProj;
1728: D3DXMatrixTranslation( &mT, 0, -3, 0 );
1729: m = mT * m;
1730:
1731:
1732:
1733: m_pEffect->SetMatrix( m_hmWVP, &m );
1734:
1735:
1736: pMtrl = m_pMeshBg->m_pMaterials;
1737: for( i=0; i<m_pMeshBg->m_dwNumMaterials; i++ ) {
1738: m_pEffect->SetTexture(m_htSrcTex, m_pMeshBg->m_pTextures[i] );
1739: BEGIN_PASS( 5 );
1740: m_pMeshBg->m_pLocalMesh->DrawSubset( i );
1741: END_PASS();
1742: pMtrl++;
1743: }
1744:
1745:
1746:
1747:
1748:
1749: D3DXMatrixScaling ( &m, 100, 100, 100 );
1750: m = m * m_mEnv * m_mWorld * m_mView * m_mProj;
1751: m_pEffect->SetMatrix( m_hmWVP, &m );
1752:
1753:
1754: pMtrl = m_pMeshEnv->m_pMaterials;
1755: for( i=0; i<m_pMeshEnv->m_dwNumMaterials; i++ ) {
1756: m_pEffect->SetTexture(m_htSrcTex, m_pMeshEnv->m_pTextures[i] );
1757: BEGIN_PASS( 5 );
1758: m_pMeshEnv->m_pLocalMesh->DrawSubset( i );
1759: END_PASS();
1760: pMtrl++;
1761: }
1762:
1763: m_pEffect->End();
1764: }
1765:
1766: #ifdef _DEBUG
1767: {
1768: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
1769: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
1770: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
1771: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1772: m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
1773: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1774: float scale = 512.0f/11;
1775: for(DWORD i=0; i<11; i++){
1776: TVERTEX Vertex[4] = {
1777:
1778: {(i+0)*scale, (FLOAT)m_d3dsdBackBuffer.Height-scale, 0, 1, 0, 0,},
1779: {(i+1)*scale, (FLOAT)m_d3dsdBackBuffer.Height-scale, 0, 1, 1, 0,},
1780: {(i+1)*scale, (FLOAT)m_d3dsdBackBuffer.Height- 0, 0, 1, 1, 1,},
1781: {(i+0)*scale, (FLOAT)m_d3dsdBackBuffer.Height- 0, 0, 1, 0, 1,},
1782: };
1783: if(0==i) m_pd3dDevice->SetTexture( 0, m_pParaboloidTex[0] );
1784: if(1==i) m_pd3dDevice->SetTexture( 0, m_pParaboloidTex[1] );
1785: if(2==i) m_pd3dDevice->SetTexture( 0, m_pJacobianTex[0][0] );
1786: if(3==i) m_pd3dDevice->SetTexture( 0, m_pJacobianTex[0][1] );
1787: if(4==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[0] );
1788: if(5==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[1] );
1789: if(6==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[2] );
1790: if(7==i) m_pd3dDevice->SetTexture( 0, m_pPosTex );
1791: if(8==i) m_pd3dDevice->SetTexture( 0, m_pNormalTex );
1792: if(9==i) m_pd3dDevice->SetTexture( 0, m_pFinalSSTex[0] );
1793: if(10==i) m_pd3dDevice->SetTexture( 0, m_pFinalTex[0] );
1794:
1795: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1796: }
1797: }
1798: #endif
1799:
1800:
1801: RenderText();
1802:
1803:
1804: m_pd3dDevice->EndScene();
1805: }
1806:
1807: return S_OK;
1808: }
1809:
1810:
1811:
1812:
1813:
1814:
1815:
1816:
1817:
1818: HRESULT CMyD3DApplication::RenderText()
1819: {
1820: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
1821: TCHAR szMsg[MAX_PATH] = TEXT("");
1822: FLOAT fNextLine;
1823:
1824:
1825: fNextLine = 0;
1826:
1827: static const TCHAR *shader_str[]=
1828: {
1829: "Diffuse+Translucent+Specular",
1830: "Diffuse",
1831: "Translucent",
1832: };
1833:
1834: sprintf( szMsg, TEXT("Press 'Q' to change shader[%s]"), shader_str[m_Shader] );
1835: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1836: fNextLine += 20.0f;
1837:
1838: sprintf( szMsg, TEXT("Diffuse[D-C] : %f"), m_fDiffuse );
1839: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1840: fNextLine += 20.0f;
1841:
1842: sprintf( szMsg, TEXT("Specular[F-V] : %f"), m_fSpecular );
1843: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1844: fNextLine += 20.0f;
1845:
1846: sprintf( szMsg, TEXT("Translucent[G-B] : %f"), m_fTranslucent );
1847: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1848: fNextLine += 20.0f;
1849:
1850:
1851: lstrcpy( szMsg, m_strDeviceStats );
1852: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1853: fNextLine += 20.0f;
1854: lstrcpy( szMsg, m_strFrameStats );
1855: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1856: fNextLine += 20.0f;
1857:
1858: #ifndef _DEBUG
1859: fNextLine = (FLOAT) m_d3dsdBackBuffer.Height-20;
1860: m_pFont->DrawText( 6, fNextLine+2, 0xff000000, TEXT("t-pot.com") );
1861: m_pFont->DrawText( 4, fNextLine, 0xffff8040, TEXT("t-pot.com") );
1862: #endif
1863:
1864: return S_OK;
1865: }
1866:
1867:
1868:
1869:
1870:
1871:
1872:
1873:
1874:
1875:
1876: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
1877: WPARAM wParam, LPARAM lParam )
1878: {
1879: switch( msg )
1880: {
1881: case WM_PAINT:
1882: {
1883: if( m_bLoadingApp )
1884: {
1885:
1886: HDC hDC = GetDC( hWnd );
1887: TCHAR strMsg[MAX_PATH];
1888: wsprintf(strMsg, TEXT("Loading... Please wait"));
1889: RECT rct;
1890: GetClientRect( hWnd, &rct );
1891: DrawText( hDC, strMsg, -1, &rct
1892: , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
1893: ReleaseDC( hWnd, hDC );
1894: }
1895: break;
1896: }
1897:
1898: }
1899:
1900: return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
1901: }
1902:
1903:
1904:
1905:
1906:
1907:
1908:
1909:
1910:
1911: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
1912: {
1913: int i;
1914:
1915:
1916: m_pFont->InvalidateDeviceObjects();
1917:
1918:
1919: for(int j=0;j<TEX_MAX;j++){
1920: SAFE_RELEASE(m_pFinalSSSurf[j]);
1921: SAFE_RELEASE(m_pFinalSSTex[j]);
1922: SAFE_RELEASE(m_pFinalSurf[j]);
1923: SAFE_RELEASE(m_pFinalTex[j]);
1924: }
1925: SAFE_RELEASE( m_p8Surf );
1926: SAFE_RELEASE( m_p8Tex );
1927: SAFE_RELEASE( m_p64Z );
1928: SAFE_RELEASE( m_p64Surf2 );
1929: SAFE_RELEASE( m_p64Tex2 );
1930: SAFE_RELEASE( m_p64Surf );
1931: SAFE_RELEASE( m_p64Tex );
1932: for( i = 0; i < REDUCTION_MAPS; i++ )
1933: {
1934: SAFE_RELEASE(m_pReductionSurf[i]);
1935: SAFE_RELEASE(m_pReductionTex[i]);
1936: }
1937: SAFE_RELEASE(m_pMaskTex);
1938: SAFE_RELEASE(m_pParaboloidSurf[1]);
1939: SAFE_RELEASE(m_pParaboloidTex[1]);
1940: SAFE_RELEASE(m_pParaboloidSurf[0]);
1941: SAFE_RELEASE(m_pParaboloidTex[0]);
1942: SAFE_RELEASE(m_pNormalLockSurf);
1943: SAFE_RELEASE(m_pNormalLockTex);
1944: SAFE_RELEASE(m_pNormalSurf);
1945: SAFE_RELEASE(m_pNormalTex);
1946: SAFE_RELEASE(m_pPosLockSurf);
1947: SAFE_RELEASE(m_pPosLockTex);
1948: SAFE_RELEASE(m_pPosSurf);
1949: SAFE_RELEASE(m_pPosTex);
1950: SAFE_RELEASE(m_pMapZ);
1951:
1952:
1953: m_pMeshEnv->InvalidateDeviceObjects();
1954: m_pMeshBg->InvalidateDeviceObjects();
1955: m_pMesh->InvalidateDeviceObjects();
1956:
1957:
1958: if( m_pEffect != NULL ) m_pEffect ->OnLostDevice();
1959:
1960: return S_OK;
1961: }
1962:
1963:
1964:
1965:
1966:
1967:
1968:
1969:
1970:
1971:
1972:
1973: HRESULT CMyD3DApplication::DeleteDeviceObjects()
1974: {
1975:
1976: m_pFont->DeleteDeviceObjects();
1977:
1978:
1979: for( int i = 0; i < TEX_MAX; i++ )
1980: {
1981: SAFE_RELEASE(m_pJacobianTex[i][0]);
1982: SAFE_RELEASE(m_pJacobianTex[i][1]);
1983: }
1984:
1985:
1986: SAFE_RELEASE( m_pEffect );
1987:
1988:
1989: m_pMeshEnv->Destroy();
1990: m_pMeshBg->Destroy();
1991: m_pMesh->Destroy();
1992:
1993: return S_OK;
1994: }
1995:
1996:
1997:
1998:
1999:
2000:
2001:
2002:
2003:
2004:
2005:
2006: HRESULT CMyD3DApplication::FinalCleanup()
2007: {
2008:
2009: SAFE_DELETE( m_pFont );
2010:
2011:
2012: SAFE_DELETE( m_pMeshEnv );
2013: SAFE_DELETE( m_pMeshBg );
2014: SAFE_DELETE( m_pMesh );
2015:
2016: return S_OK;
2017: }
2018:
2019:
2020:
2021:
2022: