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, 1},
0104: { 1,-1, 0, 0, 0, 1, 1, 1},
0105: { -1, 1, 0, 0, 0, 1, 0, 0},
0106: { 1, 1, 0, 0, 0, 1, 1, 0},
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: UINT i;
0175:
0176: m_pPlane = new CPlane();
0177: for(i=0;i<6;i++){
0178: m_pTexDepth[i] = NULL;
0179: m_pTexColor[i] = NULL;
0180: m_pTexNormal[i] = NULL;
0181: }
0182:
0183: m_pDecl = NULL;
0184: m_pEffect = NULL;
0185: m_hmWVP = NULL;
0186: m_hvEye = NULL;
0187: m_hvLight = NULL;
0188:
0189: m_fWorldRotX =-0.10f*D3DX_PI;
0190: m_fWorldRotY = 0.25f*D3DX_PI;
0191: m_Eye = D3DXVECTOR3( 0.0f, 0.0f, -4.0f );
0192:
0193:
0194: m_dwCreationWidth = 750;
0195: m_dwCreationHeight = 450;
0196: m_strWindowTitle = TEXT( "main" );
0197: m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
0198: m_bStartFullscreen = false;
0199: m_bShowCursorWhenFullscreen = false;
0200:
0201: m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0202: m_bLoadingApp = TRUE;
0203:
0204: ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0205: }
0206:
0207:
0208:
0209:
0210:
0211:
0212:
0213:
0214: CMyD3DApplication::~CMyD3DApplication()
0215: {
0216: }
0217:
0218:
0219:
0220:
0221:
0222:
0223:
0224:
0225:
0226:
0227: HRESULT CMyD3DApplication::OneTimeSceneInit()
0228: {
0229:
0230: SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0231:
0232: m_bLoadingApp = FALSE;
0233:
0234: return S_OK;
0235: }
0236:
0237:
0238:
0239:
0240:
0241:
0242:
0243:
0244: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps
0245: , DWORD dwBehavior, D3DFORMAT Format )
0246: {
0247: UNREFERENCED_PARAMETER( Format );
0248: UNREFERENCED_PARAMETER( dwBehavior );
0249: UNREFERENCED_PARAMETER( pCaps );
0250:
0251:
0252: if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) &&
0253: !(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING ) )
0254: return E_FAIL;
0255:
0256: if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0257: return E_FAIL;
0258:
0259: return S_OK;
0260: }
0261:
0262:
0263:
0264:
0265:
0266:
0267:
0268:
0269:
0270: HRESULT CMyD3DApplication::InitDeviceObjects()
0271: {
0272: HRESULT hr;
0273: UINT i;
0274:
0275:
0276: const char *depth_map[6] = {
0277: "depth0.bmp",
0278: "depth1.bmp",
0279: "depth2.bmp",
0280: "depth3.bmp",
0281: "depth4.bmp",
0282: "depth5.bmp",
0283: };
0284: const char *color_map[6] = {
0285: "color0.bmp",
0286: "color1.bmp",
0287: "color2.bmp",
0288: "color3.bmp",
0289: "color4.bmp",
0290: "color5.bmp",
0291: };
0292: const char *normal_map[6] = {
0293: "normal0.bmp",
0294: "normal1.bmp",
0295: "normal2.bmp",
0296: "normal3.bmp",
0297: "normal4.bmp",
0298: "normal5.bmp",
0299: };
0300: for( i=0 ; i<6 ; i++ ){
0301: D3DXCreateTextureFromFileEx(m_pd3dDevice, depth_map[i], 0,0,0,0,D3DFMT_A8R8G8B8,
0302: D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0303: 0, NULL, NULL, &m_pTexDepth[i]);
0304: D3DXCreateTextureFromFileEx(m_pd3dDevice, color_map[i], 0,0,0,0,D3DFMT_A8R8G8B8,
0305: D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0306: 0, NULL, NULL, &m_pTexColor[i]);
0307: D3DXCreateTextureFromFileEx(m_pd3dDevice, normal_map[i], 0,0,0,0,D3DFMT_A8R8G8B8,
0308: D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR,
0309: 0, NULL, NULL, &m_pTexNormal[i]);
0310: }
0311:
0312: m_pPlane->Create( m_pd3dDevice );
0313:
0314:
0315: D3DVERTEXELEMENT9 decl[] =
0316: {
0317: {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
0318: {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
0319: D3DDECL_END()
0320: };
0321: if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration(
0322: decl, &m_pDecl )))
0323: return DXTRACE_ERR ("CreateVertexDeclaration", hr);
0324:
0325:
0326: if( FAILED( hr = D3DXCreateEffectFromFile(
0327: m_pd3dDevice, "hlsl.fx", NULL, NULL,
0328: 0, NULL, &m_pEffect, NULL ) ) )
0329: return DXTRACE_ERR( "CreateEffectFromFile", hr );
0330: m_hmWVP = m_pEffect->GetParameterByName( NULL, "mWVP" );
0331: m_hvEye = m_pEffect->GetParameterByName( NULL, "vEyePos" );
0332: m_hvLight = m_pEffect->GetParameterByName( NULL, "vLight" );
0333:
0334: m_pFont->InitDeviceObjects( m_pd3dDevice );
0335:
0336: return S_OK;
0337: }
0338:
0339:
0340:
0341:
0342:
0343:
0344: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0345: {
0346:
0347: #define RS m_pd3dDevice->SetRenderState
0348:
0349:
0350: RS ( D3DRS_ZENABLE, TRUE );
0351: RS ( D3DRS_LIGHTING, FALSE );
0352:
0353:
0354: D3DXMatrixIdentity( &m_mWorld );
0355:
0356:
0357: D3DXVECTOR3 vLookat = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0358: D3DXVECTOR3 vUp = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0359: D3DXMatrixLookAtLH( &m_mView, &m_Eye, &vLookat, &vUp );
0360:
0361:
0362: FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width )
0363: / ((FLOAT)m_d3dsdBackBuffer.Height);
0364: D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect
0365: , 1.0f, 100.0f );
0366:
0367:
0368: if( m_pEffect!=NULL ) m_pEffect->OnResetDevice();
0369:
0370: m_pFont->RestoreDeviceObjects();
0371:
0372: return S_OK;
0373: }
0374:
0375:
0376:
0377:
0378:
0379:
0380:
0381:
0382: HRESULT CMyD3DApplication::FrameMove()
0383: {
0384: UpdateInput( &m_UserInput );
0385:
0386:
0387:
0388:
0389:
0390: if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0391: m_fWorldRotY += m_fElapsedTime;
0392: else if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0393: m_fWorldRotY -= m_fElapsedTime;
0394:
0395: if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0396: m_fWorldRotX += m_fElapsedTime;
0397: else if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0398: m_fWorldRotX -= m_fElapsedTime;
0399:
0400:
0401:
0402:
0403:
0404: D3DXMATRIX matRotX, matRotY;
0405: D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0406: D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0407: D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0408:
0409: return S_OK;
0410: }
0411:
0412:
0413:
0414:
0415:
0416:
0417:
0418:
0419: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0420: {
0421: pUserInput->bRotateUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000) == 0x8000 );
0422: pUserInput->bRotateDown = ( m_bActive && (GetAsyncKeyState( VK_DOWN ) & 0x8000) == 0x8000 );
0423: pUserInput->bRotateLeft = ( m_bActive && (GetAsyncKeyState( VK_LEFT ) & 0x8000) == 0x8000 );
0424: pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0425: }
0426:
0427:
0428:
0429:
0430:
0431:
0432:
0433:
0434: HRESULT CMyD3DApplication::Render()
0435: {
0436: D3DXHANDLE hTechnique;
0437: D3DXMATRIX m, mL;
0438: D3DXVECTOR4 v;
0439: UINT i;
0440:
0441: if( SUCCEEDED( m_pd3dDevice->BeginScene()))
0442: {
0443:
0444: m_pd3dDevice->Clear( 0L, NULL,
0445: D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
0446: 0x4004080, 1.0f, 0L );
0447:
0448: if(m_pEffect != NULL)
0449: {
0450:
0451:
0452:
0453: hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0454: m_pEffect->SetTechnique( hTechnique );
0455: m_pEffect->Begin( NULL, 0 );
0456: m_pEffect->Pass( 0 );
0457: m_pd3dDevice->SetVertexDeclaration( m_pDecl );
0458: for( i=0 ; i<6 ; i++ ){
0459:
0460:
0461:
0462:
0463: switch(i){
0464: case 0:
0465: D3DXMatrixRotationY ( &m,+0.0f*D3DX_PI );
0466: break;
0467: case 1:
0468: D3DXMatrixRotationY ( &m,+1.0f*D3DX_PI );
0469: break;
0470: case 2:
0471: D3DXMatrixRotationY ( &m,+0.5f*D3DX_PI );
0472: break;
0473: case 3:
0474: D3DXMatrixRotationY ( &m,-0.5f*D3DX_PI );
0475: break;
0476: case 4:
0477: D3DXMatrixRotationX ( &m,-0.5f*D3DX_PI );
0478: break;
0479: case 5:
0480: D3DXMatrixRotationX ( &m,+0.5f*D3DX_PI );
0481: break;
0482: }
0483: D3DXMatrixTranslation ( &mL, 0.0f, 0.0f, -1.0 );
0484: mL = mL * m;
0485: m = mL * m_mWorld * m_mView * m_mProj;
0486: m_pEffect->SetMatrix( m_hmWVP, &m );
0487:
0488:
0489: m = mL * m_mWorld;
0490: D3DXMatrixInverse( &m, NULL, &m );
0491: D3DXVec3Transform( &v, &m_Eye, &m );
0492: m_pEffect->SetVector( m_hvEye, &v );
0493:
0494:
0495: v = D3DXVECTOR4(0.7, 0.7, -0.7, 1.0);
0496: m = m_mWorld;
0497: D3DXMatrixInverse( &m, NULL, &m );
0498: D3DXVec4Transform( &v, &v, &m );
0499: v.w = 0.3f;
0500: m_pEffect->SetVector( m_hvLight, &v );
0501:
0502:
0503:
0504:
0505: m_pEffect->SetTexture( "Depth", m_pTexDepth [i] );
0506: m_pEffect->SetTexture( "Color", m_pTexColor [i] );
0507: m_pEffect->SetTexture( "Normal", m_pTexNormal[i] );
0508: m_pPlane->Render(m_pd3dDevice);
0509: }
0510:
0511: m_pEffect->End();
0512: }
0513:
0514: #if 1
0515: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1);
0516: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0517: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE);
0518: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0519: m_pd3dDevice->SetVertexShader(NULL);
0520: m_pd3dDevice->SetPixelShader(0);
0521: for(DWORD loop = 0; loop < 3; loop++){
0522: const float scale = 128.0f;
0523: typedef struct {FLOAT p[4]; FLOAT tu, tv;} TVERTEX;
0524:
0525: TVERTEX Vertex[4] = {
0526:
0527: { 0,(loop )*scale,0, 1, 0, 0,},
0528: {scale,(loop )*scale,0, 1, 1, 0,},
0529: {scale,(loop+1)*scale,0, 1, 1, 1,},
0530: { 0,(loop+1)*scale,0, 1, 0, 1,},
0531: };
0532: switch(loop){
0533: case 0: m_pd3dDevice->SetTexture( 0, m_pTexDepth [0] ); break;
0534: case 1: m_pd3dDevice->SetTexture( 0, m_pTexColor [0] ); break;
0535: case 2: m_pd3dDevice->SetTexture( 0, m_pTexNormal[0] ); break;
0536: }
0537: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0538: }
0539: #endif
0540:
0541: RenderText();
0542:
0543: m_pd3dDevice->EndScene();
0544: }
0545:
0546: return S_OK;
0547: }
0548:
0549:
0550:
0551:
0552:
0553:
0554:
0555:
0556: HRESULT CMyD3DApplication::RenderText()
0557: {
0558: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
0559: TCHAR szMsg[MAX_PATH] = TEXT("");
0560:
0561: FLOAT fNextLine = 40.0f;
0562:
0563:
0564: fNextLine = (FLOAT) m_d3dsdBackBuffer.Height;
0565: lstrcpy( szMsg, TEXT("Use arrow keys to rotate object") );
0566: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0567:
0568: lstrcpy( szMsg, m_strDeviceStats );
0569: fNextLine -= 20.0f;m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0570: lstrcpy( szMsg, m_strFrameStats );
0571: fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0572:
0573: return S_OK;
0574: }
0575:
0576:
0577:
0578:
0579:
0580:
0581:
0582:
0583: LRESULT CMyD3DApplication::MsgProc( HWND hWnd,
0584: UINT msg, WPARAM wParam, LPARAM lParam )
0585: {
0586: switch( msg )
0587: {
0588: case WM_PAINT:
0589: {
0590: if( m_bLoadingApp )
0591: {
0592:
0593: HDC hDC = GetDC( hWnd );
0594: TCHAR strMsg[MAX_PATH];
0595: wsprintf(strMsg, TEXT("Loading... Please wait"));
0596: RECT rct;
0597: GetClientRect( hWnd, &rct );
0598: DrawText( hDC, strMsg, -1, &rct,
0599: DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0600: ReleaseDC( hWnd, hDC );
0601: }
0602: break;
0603: }
0604:
0605: }
0606:
0607: return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0608: }
0609:
0610:
0611:
0612:
0613:
0614:
0615:
0616:
0617: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0618: {
0619: if(m_pEffect!=NULL) m_pEffect->OnLostDevice();
0620:
0621: m_pFont->InvalidateDeviceObjects();
0622:
0623: return S_OK;
0624: }
0625:
0626:
0627:
0628:
0629:
0630:
0631:
0632:
0633: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0634: {
0635: UINT i;
0636:
0637: m_pPlane->DeleteDeviceObjects();
0638: for( i=0 ; i<6 ; i++ ){
0639: SAFE_RELEASE( m_pTexNormal[i] );
0640: SAFE_RELEASE( m_pTexDepth [i] );
0641: SAFE_RELEASE( m_pTexColor [i] );
0642: }
0643:
0644: SAFE_RELEASE( m_pEffect );
0645: SAFE_RELEASE( m_pDecl );
0646:
0647: m_pFont->DeleteDeviceObjects();
0648:
0649: return S_OK;
0650: }
0651:
0652:
0653:
0654:
0655:
0656:
0657:
0658:
0659: HRESULT CMyD3DApplication::FinalCleanup()
0660: {
0661: SAFE_DELETE( m_pFont );
0662:
0663: return S_OK;
0664: }
0665:
0666:
0667:
0668:
0669: