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 frand() (((FLOAT)rand())/((FLOAT)RAND_MAX))
0027:
0028: static const UINT MAP_SIZE = 256;
0029:
0030:
0031:
0032:
0033: CMyD3DApplication* g_pApp = NULL;
0034: HINSTANCE g_hInst = NULL;
0035:
0036:
0037:
0038:
0039:
0040:
0041: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0042: {
0043: CMyD3DApplication d3dApp;
0044:
0045: g_pApp = &d3dApp;
0046: g_hInst = hInst;
0047:
0048: InitCommonControls();
0049: if( FAILED( d3dApp.Create( hInst ) ) )
0050: return 0;
0051:
0052: return d3dApp.Run();
0053: }
0054:
0055:
0056: const DWORD CPlane::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
0057:
0058: D3DVERTEXELEMENT9 CPlane::decl[] =
0059: {
0060: {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
0061: {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
0062: {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
0063: D3DDECL_END()
0064: };
0065:
0066:
0067:
0068:
0069:
0070:
0071: CPlane::CPlane()
0072: {
0073: }
0074:
0075:
0076:
0077:
0078: CPlane::~CPlane()
0079: {
0080: }
0081:
0082:
0083:
0084:
0085: HRESULT CPlane::Create( LPDIRECT3DDEVICE9 lpD3DDev )
0086: {
0087: HRESULT hr;
0088:
0089: m_dwNumVertices = 4;
0090: m_dwNumFaces = 2;
0091:
0092:
0093: Vertex *pDest;
0094: if(FAILED(hr=lpD3DDev->CreateVertexBuffer(
0095: m_dwNumVertices * sizeof(CPlane::Vertex),
0096: D3DUSAGE_WRITEONLY, CPlane::FVF,
0097: D3DPOOL_MANAGED, &m_pVB, NULL )))
0098: return hr;
0099:
0100:
0101: Vertex vertex[] = {
0102:
0103: { -1,-1, 0, 0, 0, 1, 0-0.1f, 1+0.1f},
0104: { 1,-1, 0, 0, 0, 1, 1+0.1f, 1+0.1f},
0105: { -1, 1, 0, 0, 0, 1, 0-0.1f, 0-0.1f},
0106: { 1, 1, 0, 0, 0, 1, 1+0.1f, 0-0.1f},
0107: };
0108: m_pVB->Lock ( 0, 0, (void**)&pDest, 0 );
0109: memcpy(pDest, vertex, sizeof(vertex));
0110: m_pVB->Unlock ();
0111:
0112:
0113: if(FAILED(hr=lpD3DDev->CreateIndexBuffer(
0114: 3 * m_dwNumFaces * sizeof(WORD),
0115: D3DUSAGE_WRITEONLY ,
0116: D3DFMT_INDEX16, D3DPOOL_MANAGED,
0117: &m_pIB, NULL )))
0118: return hr;
0119:
0120: WORD index[] = {
0121: 2,1,0,
0122: 1,2,3,
0123: };
0124: WORD *pIndex;
0125: m_pIB->Lock ( 0, 0, (void**)&pIndex, 0 );
0126: memcpy(pIndex, index, sizeof(index));
0127: m_pIB->Unlock ();
0128:
0129:
0130: if( FAILED( hr = lpD3DDev->CreateVertexDeclaration(
0131: CPlane::decl, &m_pDecl )))
0132: return hr;
0133:
0134: return S_OK;
0135: }
0136:
0137:
0138:
0139:
0140: HRESULT CPlane::Render( LPDIRECT3DDEVICE9 lpD3DDev )
0141: {
0142:
0143: lpD3DDev->SetFVF( FVF );
0144: lpD3DDev->SetStreamSource( 0, m_pVB, 0, sizeof(Vertex) );
0145: lpD3DDev->SetIndices( m_pIB );
0146: lpD3DDev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,
0147: 0, m_dwNumVertices,
0148: 0, m_dwNumFaces );
0149: return S_OK;
0150: }
0151:
0152:
0153:
0154:
0155: HRESULT CPlane::DeleteDeviceObjects()
0156: {
0157: SAFE_RELEASE( m_pDecl );
0158: SAFE_RELEASE( m_pVB );
0159: SAFE_RELEASE( m_pIB );
0160:
0161: m_dwNumVertices = 0;
0162: m_dwNumFaces = 0;
0163:
0164: return S_OK;
0165: }
0166:
0167:
0168:
0169:
0170:
0171:
0172: CMyD3DApplication::CMyD3DApplication()
0173: {
0174: m_pPlane = new CPlane();
0175: m_pTexDepth = NULL;
0176: m_pTexColor = NULL;
0177:
0178: m_pDecl = NULL;
0179: m_pEffect = NULL;
0180: m_hmWVP = NULL;
0181: m_hvEye = NULL;
0182:
0183: m_fWorldRotX = -0.0f;
0184: m_fWorldRotY = 0.0f*2.0f*D3DX_PI;
0185: m_Eye = D3DXVECTOR3( 0.0f, 0.0f, -3.0f );
0186:
0187:
0188: m_dwCreationWidth = 500;
0189: m_dwCreationHeight = 375;
0190: m_strWindowTitle = TEXT( "main" );
0191: m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
0192: m_bStartFullscreen = false;
0193: m_bShowCursorWhenFullscreen = false;
0194:
0195: m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0196: m_bLoadingApp = TRUE;
0197:
0198: ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0199: }
0200:
0201:
0202:
0203:
0204:
0205:
0206:
0207:
0208: CMyD3DApplication::~CMyD3DApplication()
0209: {
0210: }
0211:
0212:
0213:
0214:
0215:
0216:
0217:
0218:
0219:
0220:
0221: HRESULT CMyD3DApplication::OneTimeSceneInit()
0222: {
0223:
0224: SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0225:
0226: m_bLoadingApp = FALSE;
0227:
0228: return S_OK;
0229: }
0230:
0231:
0232:
0233:
0234:
0235:
0236:
0237:
0238: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps
0239: , DWORD dwBehavior, D3DFORMAT Format )
0240: {
0241: UNREFERENCED_PARAMETER( Format );
0242: UNREFERENCED_PARAMETER( dwBehavior );
0243: UNREFERENCED_PARAMETER( pCaps );
0244:
0245:
0246: if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) &&
0247: !(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING ) )
0248: return E_FAIL;
0249:
0250: if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0251: return E_FAIL;
0252:
0253: return S_OK;
0254: }
0255:
0256:
0257:
0258:
0259:
0260:
0261:
0262:
0263:
0264: HRESULT CMyD3DApplication::InitDeviceObjects()
0265: {
0266: HRESULT hr;
0267:
0268: m_pPlane->Create( m_pd3dDevice );
0269: D3DXCreateTextureFromFileEx(m_pd3dDevice, "depth.bmp", 0,0,0,0,D3DFMT_A8R8G8B8,
0270: D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0271: 0, NULL, NULL, &m_pTexDepth);
0272: D3DXCreateTextureFromFileEx(m_pd3dDevice, "color.bmp", 0,0,0,0,D3DFMT_A8R8G8B8,
0273: D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0274: 0, NULL, NULL, &m_pTexColor);
0275:
0276:
0277: D3DVERTEXELEMENT9 decl[] =
0278: {
0279: {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
0280: {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
0281: D3DDECL_END()
0282: };
0283: if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration(
0284: decl, &m_pDecl )))
0285: return DXTRACE_ERR ("CreateVertexDeclaration", hr);
0286:
0287:
0288: if( FAILED( hr = D3DXCreateEffectFromFile(
0289: m_pd3dDevice, "hlsl.fx", NULL, NULL,
0290: 0, NULL, &m_pEffect, NULL ) ) )
0291: return DXTRACE_ERR( "CreateEffectFromFile", hr );
0292: m_hmWVP = m_pEffect->GetParameterByName( NULL, "mWVP" );
0293: m_hvEye = m_pEffect->GetParameterByName( NULL, "vEyePos" );
0294:
0295: m_pFont->InitDeviceObjects( m_pd3dDevice );
0296:
0297: return S_OK;
0298: }
0299:
0300:
0301:
0302:
0303:
0304:
0305: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0306: {
0307:
0308: #define RS m_pd3dDevice->SetRenderState
0309:
0310:
0311: RS ( D3DRS_ZENABLE, TRUE );
0312: RS ( D3DRS_LIGHTING, FALSE );
0313:
0314:
0315: D3DXMatrixIdentity( &m_mWorld );
0316:
0317:
0318: D3DXVECTOR3 vLookat = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0319: D3DXVECTOR3 vUp = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0320: D3DXMatrixLookAtLH( &m_mView, &m_Eye, &vLookat, &vUp );
0321:
0322:
0323: FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width )
0324: / ((FLOAT)m_d3dsdBackBuffer.Height);
0325: D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect
0326: , 1.0f, 100.0f );
0327:
0328:
0329: if( m_pEffect!=NULL ) m_pEffect->OnResetDevice();
0330:
0331: m_pFont->RestoreDeviceObjects();
0332:
0333: return S_OK;
0334: }
0335:
0336:
0337:
0338:
0339:
0340:
0341:
0342:
0343: HRESULT CMyD3DApplication::FrameMove()
0344: {
0345: UpdateInput( &m_UserInput );
0346:
0347:
0348:
0349:
0350:
0351: if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0352: m_fWorldRotY += m_fElapsedTime;
0353: else if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0354: m_fWorldRotY -= m_fElapsedTime;
0355:
0356: if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0357: m_fWorldRotX += m_fElapsedTime;
0358: else if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0359: m_fWorldRotX -= m_fElapsedTime;
0360:
0361:
0362:
0363:
0364:
0365: D3DXMATRIX matRotX, matRotY;
0366: D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0367: D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0368: D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0369:
0370: return S_OK;
0371: }
0372:
0373:
0374:
0375:
0376:
0377:
0378:
0379:
0380: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0381: {
0382: pUserInput->bRotateUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000) == 0x8000 );
0383: pUserInput->bRotateDown = ( m_bActive && (GetAsyncKeyState( VK_DOWN ) & 0x8000) == 0x8000 );
0384: pUserInput->bRotateLeft = ( m_bActive && (GetAsyncKeyState( VK_LEFT ) & 0x8000) == 0x8000 );
0385: pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0386: }
0387:
0388:
0389:
0390:
0391:
0392:
0393:
0394:
0395: HRESULT CMyD3DApplication::Render()
0396: {
0397: D3DXHANDLE hTechnique;
0398: D3DXMATRIX m, mL;
0399: D3DXVECTOR4 v;
0400:
0401:
0402: if( SUCCEEDED( m_pd3dDevice->BeginScene()))
0403: {
0404:
0405: m_pd3dDevice->Clear( 0L, NULL,
0406: D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
0407: 0x4004080, 1.0f, 0L );
0408:
0409: if(m_pEffect != NULL)
0410: {
0411:
0412:
0413:
0414: hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0415: m_pEffect->SetTechnique( hTechnique );
0416: m_pEffect->Begin( NULL, 0 );
0417: m_pEffect->Pass( 0 );
0418: m_pd3dDevice->SetVertexDeclaration( m_pDecl );
0419:
0420:
0421:
0422:
0423: D3DXMatrixTranslation ( &mL, 0, -0.0f, 0.0 );
0424: m = mL * m_mWorld * m_mView * m_mProj;
0425: if( m_hmWVP != NULL ) m_pEffect->SetMatrix( m_hmWVP, &m );
0426:
0427:
0428: m = mL * m_mWorld;
0429: D3DXMatrixInverse( &m, NULL, &m );
0430: D3DXVec3Transform( &v, &m_Eye, &m );
0431: if( m_hvEye != NULL ) m_pEffect->SetVector( m_hvEye, &v );
0432:
0433: m_pEffect->SetTexture( "Tex", m_pTexDepth );
0434: m_pEffect->SetTexture( "Color", m_pTexColor );
0435: m_pPlane->Render(m_pd3dDevice);
0436:
0437: m_pEffect->End();
0438: }
0439:
0440: #if 1
0441: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
0442: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0443: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0444: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0445: m_pd3dDevice->SetVertexShader(NULL);
0446: m_pd3dDevice->SetPixelShader(0);
0447: for(DWORD loop = 0; loop < 2; loop++){
0448: const float scale = 128.0f;
0449: typedef struct {FLOAT p[4]; FLOAT tu, tv;} TVERTEX;
0450:
0451: TVERTEX Vertex[4] = {
0452:
0453: { 0,(loop )*scale,0, 1, 0, 0,},
0454: {scale,(loop )*scale,0, 1, 1, 0,},
0455: {scale,(loop+1)*scale,0, 1, 1, 1,},
0456: { 0,(loop+1)*scale,0, 1, 0, 1,},
0457: };
0458: switch(loop){
0459: case 0: m_pd3dDevice->SetTexture( 0, m_pTexDepth ); break;
0460: case 1: m_pd3dDevice->SetTexture( 0, m_pTexColor ); break;
0461: }
0462: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0463: }
0464: #endif
0465:
0466: RenderText();
0467:
0468: m_pd3dDevice->EndScene();
0469: }
0470:
0471: return S_OK;
0472: }
0473:
0474:
0475:
0476:
0477:
0478:
0479:
0480:
0481: HRESULT CMyD3DApplication::RenderText()
0482: {
0483: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
0484: TCHAR szMsg[MAX_PATH] = TEXT("");
0485:
0486: FLOAT fNextLine = 40.0f;
0487:
0488:
0489: fNextLine = (FLOAT) m_d3dsdBackBuffer.Height;
0490: lstrcpy( szMsg, TEXT("Use arrow keys to rotate object") );
0491: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0492:
0493: lstrcpy( szMsg, m_strDeviceStats );
0494: fNextLine -= 20.0f;m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0495: lstrcpy( szMsg, m_strFrameStats );
0496: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0497:
0498: return S_OK;
0499: }
0500:
0501:
0502:
0503:
0504:
0505:
0506:
0507:
0508: LRESULT CMyD3DApplication::MsgProc( HWND hWnd,
0509: UINT msg, WPARAM wParam, LPARAM lParam )
0510: {
0511: switch( msg )
0512: {
0513: case WM_PAINT:
0514: {
0515: if( m_bLoadingApp )
0516: {
0517:
0518: HDC hDC = GetDC( hWnd );
0519: TCHAR strMsg[MAX_PATH];
0520: wsprintf(strMsg, TEXT("Loading... Please wait"));
0521: RECT rct;
0522: GetClientRect( hWnd, &rct );
0523: DrawText( hDC, strMsg, -1, &rct,
0524: DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0525: ReleaseDC( hWnd, hDC );
0526: }
0527: break;
0528: }
0529:
0530: }
0531:
0532: return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0533: }
0534:
0535:
0536:
0537:
0538:
0539:
0540:
0541:
0542: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0543: {
0544: if(m_pEffect!=NULL) m_pEffect->OnLostDevice();
0545:
0546: m_pFont->InvalidateDeviceObjects();
0547:
0548: return S_OK;
0549: }
0550:
0551:
0552:
0553:
0554:
0555:
0556:
0557:
0558: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0559: {
0560: m_pPlane->DeleteDeviceObjects();
0561: SAFE_RELEASE( m_pTexDepth );
0562: SAFE_RELEASE( m_pTexColor );
0563:
0564: SAFE_RELEASE( m_pEffect );
0565: SAFE_RELEASE( m_pDecl );
0566:
0567: m_pFont->DeleteDeviceObjects();
0568:
0569: return S_OK;
0570: }
0571:
0572:
0573:
0574:
0575:
0576:
0577:
0578:
0579: HRESULT CMyD3DApplication::FinalCleanup()
0580: {
0581: SAFE_DELETE( m_pFont );
0582:
0583: return S_OK;
0584: }
0585:
0586:
0587:
0588:
0589: