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