BumpEarth.cpp
资源名称:VC++视频传输.rar [点击查看]
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:36k
源码类别:
VC书籍
开发平台:
Visual C++
- //-----------------------------------------------------------------------------
- // File: BumpEarth.cpp
- //
- // Desc: Direct3D 环境贴图/凹凸贴图 实例。
- //-----------------------------------------------------------------------------
- #define STRICT
- #include <Windows.h>
- #include <commctrl.h>
- #include <math.h>
- #include <stdio.h>
- #include <D3DX9.h>
- #include "DXUtil.h"
- #include "D3DEnumeration.h"
- #include "D3DSettings.h"
- #include "D3DApp.h"
- #include "D3DFile.h"
- #include "D3DFont.h"
- #include "D3DUtil.h"
- #include "resource.h"
- //-----------------------------------------------------------------------------
- // 定义,常量,全局变量声明
- //-----------------------------------------------------------------------------
- // Vertex 数据结构,bumpmapped environment map使用
- struct BUMPVERTEX
- {
- D3DXVECTOR3 p;
- D3DXVECTOR3 n;
- FLOAT tu1, tv1;
- FLOAT tu2, tv2;
- static const DWORD FVF;
- };
- const DWORD BUMPVERTEX::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX2;
- // 将FLOAT转换为DWORD,在SetRenderState()调用中将用到
- inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); }
- //-----------------------------------------------------------------------------
- // Name: class CMyD3DApplication
- // Desc:
- //-----------------------------------------------------------------------------
- class CMyD3DApplication : public CD3DApplication
- {
- CD3DFont* m_pFont; //用于输出文本
- CD3DArcBall m_ArcBall; //用于鼠标输入
- LPDIRECT3DTEXTURE9 m_pBlockTexture; //黑,灰纹理
- LPDIRECT3DTEXTURE9 m_pEarthTexture; //地表纹理
- LPDIRECT3DTEXTURE9 m_pEnvMapTexture; //环境贴图
- LPDIRECT3DTEXTURE9 m_pEarthBumpTexture; //凹凸贴图资源
- LPDIRECT3DTEXTURE9 m_psBumpMap; //实际的凹凸贴图
- CD3DMesh* m_pSkyBox;
- LPDIRECT3DTEXTURE9 m_pDispMapTexture; //偏移贴图纹理
- IDirect3DVertexShader9* m_pDispMapBumpShader;//偏移贴图+凹凸贴图着色
- IDirect3DVertexShader9* m_pDispMapShader; //偏移贴图+无凹凸贴图着色
- IDirect3DVertexDeclaration9* m_pDispMapVertexDecl;
- D3DXMATRIXA16 m_matWorld;
- D3DXMATRIXA16 m_matView;
- D3DXMATRIXA16 m_matProj;
- D3DFORMAT m_BumpMapFormat; //凹凸贴图纹理格式
- LPDIRECT3DVERTEXBUFFER9 m_pEarthVB; //地球几何体
- DWORD m_dwNumSphereVertices;
- BOOL m_bHighTesselation; //用户选项
- BOOL m_bTextureOn;
- BOOL m_bBumpMapOn;
- BOOL m_bEnvMapOn;
- BOOL m_bDeviceValidationFailed;
- BOOL m_bDispMapOn; //偏移贴图开关
- BOOL m_bCanDoDispMap;
- VOID SetMenuStates();
- HRESULT CreateEarthVertexBuffer();
- VOID ApplyEnvironmentMap();
- HRESULT InitBumpMap();
- protected:
- HRESULT OneTimeSceneInit();
- HRESULT InitDeviceObjects();
- HRESULT RestoreDeviceObjects();
- HRESULT InvalidateDeviceObjects();
- HRESULT DeleteDeviceObjects();
- HRESULT Render();
- HRESULT FrameMove();
- HRESULT FinalCleanup();
- HRESULT ConfirmDevice( D3DCAPS9*, DWORD, D3DFORMAT, D3DFORMAT );
- public:
- CMyD3DApplication();
- LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
- };
- //-----------------------------------------------------------------------------
- // Name: WinMain()
- // Desc: 程序入口。进行全部的初始化工作,然后进入消息处理循环,空闲时间被用来
- // 进行渲染
- //-----------------------------------------------------------------------------
- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
- {
- CMyD3DApplication d3dApp;
- InitCommonControls();
- if( FAILED( d3dApp.Create( hInst ) ) )
- return 0;
- return d3dApp.Run();
- }
- //-----------------------------------------------------------------------------
- // Name: CMyD3DApplication()
- // Desc:
- //-----------------------------------------------------------------------------
- CMyD3DApplication::CMyD3DApplication()
- {
- m_strWindowTitle = _T("BumpEarth: Direct3D BumpMapping Demo");
- m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
- m_bShowCursorWhenFullscreen = TRUE;
- m_psBumpMap = NULL;
- m_bTextureOn = TRUE;
- m_bBumpMapOn = TRUE;
- m_bEnvMapOn = TRUE;
- m_bHighTesselation = TRUE;
- m_bDispMapOn = FALSE;
- m_bCanDoDispMap = FALSE;
- m_pDispMapShader = NULL;
- m_pDispMapBumpShader= NULL;
- m_pDispMapVertexDecl= NULL;
- m_pDispMapTexture = NULL;
- m_pBlockTexture = NULL;
- m_pEarthTexture = NULL;
- m_pEarthBumpTexture = NULL;
- m_pEnvMapTexture = NULL;
- m_pSkyBox = NULL;
- m_bDeviceValidationFailed = FALSE;
- m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
- m_pEarthVB = NULL;
- }
- //-----------------------------------------------------------------------------
- // Name: OneTimeSceneInit()
- // Desc: 在初始程序开始时调用。此函数进行所有常设值得初始化
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::OneTimeSceneInit()
- {
- // 设置指针使用户可以用鼠标移动目标
- #ifdef _WIN64
- SetClassLongPtr( m_hWnd, GCLP_HCURSOR, (LONG_PTR)LoadCursor( NULL, IDC_SIZEALL ) );
- #else
- SetClassLong( m_hWnd, GCL_HCURSOR, HandleToLong( LoadCursor( NULL, IDC_SIZEALL ) ) );
- #endif
- m_pSkyBox = new CD3DMesh();
- if( m_pSkyBox == NULL )
- return E_OUTOFMEMORY;
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: ApplyEnvironmentMap()
- // Desc: 计算每个顶点的法向,从而决定环境贴图对应的纹理
- //-----------------------------------------------------------------------------
- VOID CMyD3DApplication::ApplyEnvironmentMap()
- {
- D3DXMATRIXA16 matWorldView;
- D3DXMatrixMultiply( &matWorldView, &m_matWorld, &m_matView );
- BUMPVERTEX* vtx;
- m_pEarthVB->Lock( 0, 0, (void**)&vtx, 0 );
- DWORD dwNumSphereRings = m_bHighTesselation ? 15 : 5;
- DWORD dwNumSphereSegments = m_bHighTesselation ? 30 : 10;
- FLOAT fDeltaRingAngle = ( D3DX_PI / dwNumSphereRings );
- FLOAT fDeltaSegAngle = ( 2.0f * D3DX_PI / dwNumSphereSegments );
- D3DXVECTOR4 vT;
- FLOAT fScale;
- for( DWORD ring = 0; ring < dwNumSphereRings; ring++ )
- {
- FLOAT r0 = sinf( (ring+0) * fDeltaRingAngle );
- FLOAT r1 = sinf( (ring+1) * fDeltaRingAngle );
- FLOAT y0 = cosf( (ring+0) * fDeltaRingAngle );
- FLOAT y1 = cosf( (ring+1) * fDeltaRingAngle );
- for( DWORD seg = 0; seg < (dwNumSphereSegments+1); seg++ )
- {
- FLOAT x0 = r0 * sinf( seg * fDeltaSegAngle );
- FLOAT z0 = r0 * cosf( seg * fDeltaSegAngle );
- FLOAT x1 = r1 * sinf( seg * fDeltaSegAngle );
- FLOAT z1 = r1 * cosf( seg * fDeltaSegAngle );
- (*vtx).p = (*vtx).n = D3DXVECTOR3(x0,y0,z0);
- (*vtx).tu2 = 1.0f - ((FLOAT)seg) / dwNumSphereSegments;
- (*vtx).tv2 = (ring+0) / (FLOAT)dwNumSphereRings;
- D3DXVec3Transform( &vT, &(*vtx).n, &matWorldView );
- fScale = 1.37f / D3DXVec4Length( &vT );
- (*vtx).tu1 = 0.5f + fScale*vT.x;
- (*vtx).tv1 = 0.5f - fScale*vT.y;
- vtx++;
- (*vtx).p = (*vtx).n = D3DXVECTOR3(x1,y1,z1);
- (*vtx).tu2 = 1.0f - ((FLOAT)seg) / dwNumSphereSegments;
- (*vtx).tv2 = (ring+1) / (FLOAT)dwNumSphereRings;
- D3DXVec3Transform( &vT, &(*vtx).n, &matWorldView );
- fScale = 1.37f / D3DXVec4Length( &vT );
- (*vtx).tu1 = 0.5f + fScale*vT.x;
- (*vtx).tv1 = 0.5f - fScale*vT.y;
- vtx++;
- }
- }
- m_pEarthVB->Unlock();
- }
- //-----------------------------------------------------------------------------
- // Name: FrameMove()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::FrameMove()
- {
- static FLOAT fRotationAngle = 0.0f;
- if( FALSE == m_ArcBall.IsBeingDragged() )
- fRotationAngle += m_fElapsedTime;
- D3DXMatrixRotationY( &m_matWorld, -fRotationAngle );
- D3DXMatrixMultiply( &m_matWorld, &m_matWorld, m_ArcBall.GetRotationMatrix() );
- D3DXMatrixMultiply( &m_matWorld, &m_matWorld, m_ArcBall.GetTranslationMatrix() );
- D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, -3.0f );
- D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
- D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
- D3DXMatrixLookAtLH( &m_matView, &vEyePt, &vLookatPt, &vUpVec );
- ApplyEnvironmentMap();
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: Render()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::Render()
- {
- if( FAILED( m_pd3dDevice->BeginScene() ) )
- return S_OK;
- {
- D3DXMATRIXA16 matWorldSkybox;
- D3DXMatrixScaling( &matWorldSkybox, 10.0f, 10.0f, 10.0f );
- m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorldSkybox );
- D3DXMATRIXA16 matViewSkybox = m_matView;
- matViewSkybox._41 = matViewSkybox._42 = matViewSkybox._43 = 0.0f;
- m_pd3dDevice->SetTransform( D3DTS_VIEW, &matViewSkybox );
- m_pd3dDevice->SetRenderState( D3DRS_WRAP0, 0 );
- m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
- m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
- if( (m_d3dCaps.TextureAddressCaps & D3DPTADDRESSCAPS_MIRROR) == D3DPTADDRESSCAPS_MIRROR )
- {
- m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR );
- m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR );
- }
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- m_pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_ALWAYS );
- m_pSkyBox->Render( m_pd3dDevice );
- m_pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_LESSEQUAL );
- m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matWorld );
- m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView );
- }
- m_pd3dDevice->SetRenderState( D3DRS_WRAP0, D3DWRAP_U | D3DWRAP_V );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- if( m_bTextureOn )
- m_pd3dDevice->SetTexture( 0, m_pEarthTexture );
- else
- m_pd3dDevice->SetTexture( 0, m_pBlockTexture );
- if( m_bDispMapOn )
- {
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 2 );
- m_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
- m_pd3dDevice->SetSamplerState(D3DDMAPSAMPLER, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
- m_pd3dDevice->SetSamplerState(D3DDMAPSAMPLER, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
- m_pd3dDevice->SetSamplerState(D3DDMAPSAMPLER, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
- m_pd3dDevice->SetTexture(D3DDMAPSAMPLER, m_pDispMapTexture);
- D3DXMATRIX m;
- D3DXMatrixMultiply(&m, &m_matWorld, &m_matView);
- D3DXMatrixMultiplyTranspose(&m, &m, &m_matProj);
- m_pd3dDevice->SetVertexShaderConstantF(0, (float*)&m, 4);
- float c[4] = {0.15f,0,0,0};
- m_pd3dDevice->SetVertexShaderConstantF(4, c, 1);
- m_pd3dDevice->SetNPatchMode(4);
- m_pd3dDevice->SetVertexDeclaration( m_pDispMapVertexDecl );
- if( m_bBumpMapOn && m_bEnvMapOn )
- {
- m_pd3dDevice->SetVertexShader( m_pDispMapBumpShader );
- }
- else
- {
- m_pd3dDevice->SetVertexShader( m_pDispMapShader );
- }
- }
- else
- {
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 1 );
- m_pd3dDevice->SetFVF( BUMPVERTEX::FVF );
- }
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- if( m_bBumpMapOn && m_bEnvMapOn )
- {
- m_pd3dDevice->SetTexture( 1, m_psBumpMap );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
- if( m_BumpMapFormat == D3DFMT_V8U8 )
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAP );
- else
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT00, F2DW(0.5f) );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT01, F2DW(0.0f) );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT10, F2DW(0.0f) );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT11, F2DW(0.5f) );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLSCALE, F2DW(4.0f) );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f) );
- if( m_bEnvMapOn )
- {
- m_pd3dDevice->SetTexture( 2, m_pEnvMapTexture );
- if( !m_bDispMapOn )
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 );
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_ADD );
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_CURRENT );
- }
- else
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
- }
- else
- {
- if( m_bEnvMapOn )
- {
- m_pd3dDevice->SetTexture( 1, m_pEnvMapTexture );
- if( !m_bDispMapOn )
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_ADD );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
- }
- else
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
- }
- m_pd3dDevice->SetStreamSource( 0, m_pEarthVB, 0, sizeof(BUMPVERTEX) );
- DWORD dwNumPasses;
- HRESULT hr;
- hr = m_pd3dDevice->ValidateDevice( &dwNumPasses );
- if ( ( D3D_OK == hr) || ( D3DERR_DEVICELOST == hr ) )
- {
- m_bDeviceValidationFailed = FALSE;
- }
- else
- {
- m_bDeviceValidationFailed = TRUE;
- }
- m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, m_dwNumSphereVertices-2 );
- if ( m_bDispMapOn )
- {
- m_pd3dDevice->SetNPatchMode(0);
- m_pd3dDevice->SetVertexShader( NULL );
- }
- m_pd3dDevice->SetTexture( 0, NULL );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
- m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
- m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
- if( m_bDeviceValidationFailed )
- {
- m_pFont->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,0,0),
- _T("Warning: Device validation failed. Rendering may not look right.") );
- }
- m_pd3dDevice->EndScene();
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: InitBumpMap()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::InitBumpMap()
- {
- LPDIRECT3DTEXTURE9 psBumpSrc = m_pEarthBumpTexture;
- D3DSURFACE_DESC d3dsd;
- D3DLOCKED_RECT d3dlr;
- psBumpSrc->GetLevelDesc( 0, &d3dsd );
- if( FAILED( m_pd3dDevice->CreateTexture( d3dsd.Width, d3dsd.Height, 1, 0,
- m_BumpMapFormat, D3DPOOL_MANAGED, &m_psBumpMap, NULL ) ) )
- {
- return E_FAIL;
- }
- psBumpSrc->LockRect( 0, &d3dlr, 0, 0 );
- DWORD dwSrcPitch = (DWORD)d3dlr.Pitch;
- BYTE* pSrcTopRow = (BYTE*)d3dlr.pBits;
- BYTE* pSrcCurRow = pSrcTopRow;
- BYTE* pSrcBotRow = pSrcTopRow + (dwSrcPitch * (d3dsd.Height - 1) );
- m_psBumpMap->LockRect( 0, &d3dlr, 0, 0 );
- DWORD dwDstPitch = (DWORD)d3dlr.Pitch;
- BYTE* pDstTopRow = (BYTE*)d3dlr.pBits;
- BYTE* pDstCurRow = pDstTopRow;
- for( DWORD y=0; y<d3dsd.Height; y++ )
- {
- BYTE* pSrcB0;
- BYTE* pSrcB1;
- BYTE* pSrcB2;
- BYTE* pDstT;
- pSrcB0 = pSrcCurRow;
- if( y == d3dsd.Height - 1)
- pSrcB1 = pSrcTopRow;
- else
- pSrcB1 = pSrcCurRow + dwSrcPitch;
- if( y == 0 )
- pSrcB2 = pSrcBotRow;
- else
- pSrcB2 = pSrcCurRow - dwSrcPitch;
- pDstT = pDstCurRow;
- for( DWORD x=0; x<d3dsd.Width; x++ )
- {
- LONG v00;
- LONG v01;
- LONG vM1;
- LONG v10;
- LONG v1M;
- v00 = *(pSrcB0+0);
- if( x == d3dsd.Width - 1 )
- v01 = *(pSrcCurRow);
- else
- v01 = *(pSrcB0+4);
- if( x == 0 )
- vM1 = *(pSrcCurRow + (4 * (d3dsd.Width - 1)));
- else
- vM1 = *(pSrcB0-4);
- v10 = *(pSrcB1+0);
- v1M = *(pSrcB2+0);
- LONG iDu = (vM1-v01);
- LONG iDv = (v1M-v10);
- WORD uL = ( v00>1 ) ? 63 : 127;
- switch( m_BumpMapFormat )
- {
- case D3DFMT_V8U8:
- *pDstT++ = (BYTE)iDu;
- *pDstT++ = (BYTE)iDv;
- break;
- case D3DFMT_L6V5U5:
- *(WORD*)pDstT = (WORD)( ( (iDu>>3) & 0x1f ) << 0 );
- *(WORD*)pDstT |= (WORD)( ( (iDv>>3) & 0x1f ) << 5 );
- *(WORD*)pDstT |= (WORD)( ( ( uL>>2) & 0x3f ) << 10 );
- pDstT += 2;
- break;
- case D3DFMT_X8L8V8U8:
- *pDstT++ = (BYTE)iDu;
- *pDstT++ = (BYTE)iDv;
- *pDstT++ = (BYTE)uL;
- *pDstT++ = (BYTE)0L;
- break;
- }
- pSrcB0+=4;
- pSrcB1+=4;
- pSrcB2+=4;
- }
- pSrcCurRow += dwSrcPitch;
- pDstCurRow += dwDstPitch;
- }
- m_psBumpMap->UnlockRect(0);
- psBumpSrc->UnlockRect(0);
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: InitDeviceObjects()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::InitDeviceObjects()
- {
- m_pFont->InitDeviceObjects( m_pd3dDevice );
- if( FAILED( m_pSkyBox->Create( m_pd3dDevice, _T("lobby_skybox.x") ) ) )
- return D3DAPPERR_MEDIANOTFOUND;
- if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Block.bmp"),
- &m_pBlockTexture, D3DFMT_R5G6B5 ) ) )
- return D3DAPPERR_MEDIANOTFOUND;
- if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Earth.bmp"),
- &m_pEarthTexture, D3DFMT_R5G6B5 ) ) )
- return D3DAPPERR_MEDIANOTFOUND;
- if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Earthbump.bmp"),
- &m_pEarthBumpTexture, D3DFMT_A8R8G8B8 ) ) )
- return D3DAPPERR_MEDIANOTFOUND;
- if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("EarthEnvMap.bmp"),
- &m_pEnvMapTexture, D3DFMT_R5G6B5 ) ) )
- return D3DAPPERR_MEDIANOTFOUND;
- D3DFORMAT adapterFormat = m_d3dSettings.DisplayMode().Format;
- BOOL bCanDoV8U8 = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
- m_d3dCaps.DeviceType, adapterFormat,
- 0, D3DRTYPE_TEXTURE,
- D3DFMT_V8U8 ) ) &&
- (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP );
- BOOL bCanDoL6V5U5 = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
- m_d3dCaps.DeviceType, adapterFormat,
- 0, D3DRTYPE_TEXTURE,
- D3DFMT_L6V5U5 ) ) &&
- (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE );
- BOOL bCanDoL8V8U8 = SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
- m_d3dCaps.DeviceType, adapterFormat,
- 0, D3DRTYPE_TEXTURE,
- D3DFMT_X8L8V8U8 ) ) &&
- (m_d3dCaps.TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE );
- if( bCanDoV8U8 ) m_BumpMapFormat = D3DFMT_V8U8;
- else if( bCanDoL6V5U5 ) m_BumpMapFormat = D3DFMT_L6V5U5;
- else if( bCanDoL8V8U8 ) m_BumpMapFormat = D3DFMT_X8L8V8U8;
- else return E_FAIL;
- m_bCanDoDispMap = ( (m_pd3dDevice->GetSoftwareVertexProcessing() == FALSE) &&
- (m_d3dCaps.DevCaps2 & D3DDEVCAPS2_DMAPNPATCH ) &&
- m_d3dCaps.VertexShaderVersion >= D3DVS_VERSION(1,1) &&
- SUCCEEDED( m_pD3D->CheckDeviceFormat( m_d3dCaps.AdapterOrdinal,
- m_d3dCaps.DeviceType,
- adapterFormat,
- D3DUSAGE_DMAP,
- D3DRTYPE_TEXTURE,
- D3DFMT_L8 ) ) );
- if( !m_bCanDoDispMap )
- m_bDispMapOn = false;
- HMENU hMenu = GetMenu( m_hWnd );
- EnableMenuItem( hMenu, IDM_U8V8, bCanDoV8U8 ? MF_ENABLED : MF_GRAYED );
- EnableMenuItem( hMenu, IDM_U5V5L6, bCanDoL6V5U5 ? MF_ENABLED : MF_GRAYED );
- EnableMenuItem( hMenu, IDM_U8V8L8, bCanDoL8V8U8 ? MF_ENABLED : MF_GRAYED );
- EnableMenuItem( hMenu, IDM_DISPMAPTOGGLE, m_bCanDoDispMap? MF_ENABLED : MF_GRAYED );
- SetMenuStates();
- if( FAILED( CreateEarthVertexBuffer() ) )
- return E_FAIL;
- if( FAILED( InitBumpMap() ) )
- return E_FAIL;
- if( m_bCanDoDispMap )
- {
- HRESULT hr;
- TCHAR strPath[MAX_PATH];
- if( FAILED( hr = DXUtil_FindMediaFileCb( strPath, sizeof(strPath), TEXT("Earthbump.bmp") ) ) )
- return hr;
- hr = D3DXCreateTextureFromFileEx( m_pd3dDevice,
- strPath,
- D3DX_DEFAULT,
- D3DX_DEFAULT,
- D3DX_DEFAULT,
- D3DUSAGE_DMAP,
- D3DFMT_L8,
- D3DPOOL_MANAGED,
- D3DX_DEFAULT,
- D3DX_DEFAULT,
- 0,
- NULL,
- NULL,
- &m_pDispMapTexture );
- if( FAILED(hr) )
- return D3DAPPERR_MEDIANOTFOUND;
- if( FAILED( hr = DXUtil_FindMediaFileCch( strPath, MAX_PATH,
- TEXT("EarthDispMap.fx") ) ) )
- {
- return hr;
- }
- LPD3DXEFFECT pEffect;
- if( FAILED( hr = D3DXCreateEffectFromFile( m_pd3dDevice, strPath, NULL, NULL,
- 0, NULL, &pEffect, NULL ) ) )
- {
- return hr;
- }
- D3DXHANDLE h = pEffect->GetParameterByName( NULL, TEXT("EarthBumpMap") );
- if( h == 0 )
- {
- SAFE_RELEASE( pEffect );
- return E_FAIL;
- }
- if( FAILED (hr = pEffect->GetVertexShader(h, &m_pDispMapBumpShader) ) )
- return hr;
- h = pEffect->GetParameterByName( NULL, TEXT("EarthNoBumpMap") );
- if( h == 0 )
- {
- SAFE_RELEASE( pEffect );
- return E_FAIL;
- }
- if( FAILED (hr = pEffect->GetVertexShader(h, &m_pDispMapShader) ) )
- {
- SAFE_RELEASE( pEffect );
- return hr;
- }
- SAFE_RELEASE( pEffect );
- D3DVERTEXELEMENT9 decl[] =
- {
- {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
- {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
- {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
- {0, 32, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
- {0, 32, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_LOOKUP, D3DDECLUSAGE_SAMPLE, 0},
- D3DDECL_END()
- };
- if (FAILED(hr = m_pd3dDevice->CreateVertexDeclaration(decl, &m_pDispMapVertexDecl)))
- {
- return hr;
- }
- }
- m_bDeviceValidationFailed = FALSE;
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: RestoreDeviceObjects()
- // Desc: 初始化scene 对象
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::RestoreDeviceObjects()
- {
- m_pFont->RestoreDeviceObjects();
- m_pSkyBox->RestoreDeviceObjects( m_pd3dDevice );
- m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );
- m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );
- m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
- m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
- m_pd3dDevice->SetSamplerState( 1, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );
- m_pd3dDevice->SetSamplerState( 1, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );
- m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
- m_pd3dDevice->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
- m_pd3dDevice->SetSamplerState( 2, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP );
- m_pd3dDevice->SetSamplerState( 2, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP );
- m_pd3dDevice->SetSamplerState( 2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
- m_pd3dDevice->SetSamplerState( 2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
- m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
- FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
- //设置投影矩阵
- D3DXMatrixPerspectiveFovLH( &m_matProj, D3DX_PI/4, fAspect, 0.1f, 225.0f );
- m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_matProj );
- //设置ArcBall参数
- m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 3.0f );
- m_ArcBall.SetRadius( 1.0f );
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: InvalidateDeviceObjects()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::InvalidateDeviceObjects()
- {
- m_pFont->InvalidateDeviceObjects();
- m_pSkyBox->InvalidateDeviceObjects();
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: DeleteDeviceObjects()
- // Desc: 程序退出或者设备改变时调用,该函数删除一切设备相关的对象
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::DeleteDeviceObjects()
- {
- SAFE_RELEASE( m_pBlockTexture );
- SAFE_RELEASE( m_pEarthTexture );
- SAFE_RELEASE( m_pEarthBumpTexture );
- SAFE_RELEASE( m_pEnvMapTexture );
- SAFE_RELEASE( m_pDispMapBumpShader );
- SAFE_RELEASE( m_pDispMapShader );
- SAFE_RELEASE( m_pDispMapVertexDecl );
- SAFE_RELEASE( m_pDispMapTexture );
- m_pFont->DeleteDeviceObjects();
- m_pSkyBox->Destroy();
- SAFE_RELEASE( m_psBumpMap );
- SAFE_RELEASE( m_pEarthVB );
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: FinalCleanup()
- // Desc:
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::FinalCleanup()
- {
- SAFE_DELETE( m_pFont );
- SAFE_DELETE( m_pSkyBox );
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: ConfirmDevice()
- // Desc: 设备初始化时调用,此函数检查设备必须具备的最低限度的功能
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
- D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat )
- {
- if( pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE )
- {
- if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
- pCaps->DeviceType, adapterFormat,
- 0, D3DRTYPE_TEXTURE,
- D3DFMT_X8L8V8U8 ) ) )
- {
- return S_OK;
- }
- if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
- pCaps->DeviceType, adapterFormat,
- 0, D3DRTYPE_TEXTURE,
- D3DFMT_L6V5U5 ) ) )
- {
- return S_OK;
- }
- }
- if( pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP )
- {
- if( SUCCEEDED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal,
- pCaps->DeviceType, adapterFormat,
- 0, D3DRTYPE_TEXTURE,
- D3DFMT_V8U8 ) ) )
- {
- return S_OK;
- }
- }
- return E_FAIL;
- }
- //-----------------------------------------------------------------------------
- // Name: CreateEarthVertexBuffer()
- // Desc: 建立要进行凹凸贴图地球体各个定点
- //-----------------------------------------------------------------------------
- HRESULT CMyD3DApplication::CreateEarthVertexBuffer()
- {
- SAFE_RELEASE( m_pEarthVB );
- // 选择 tesselation级
- DWORD dwNumSphereRings = m_bHighTesselation ? 15 : 5;
- DWORD dwNumSphereSegments = m_bHighTesselation ? 30 : 10;
- m_dwNumSphereVertices = 2 * dwNumSphereRings * (dwNumSphereSegments+1);
- DWORD usage = D3DUSAGE_WRITEONLY;
- if( m_bDispMapOn )
- usage |= D3DUSAGE_NPATCHES;
- // 创建顶点缓冲
- if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwNumSphereVertices*sizeof(BUMPVERTEX),
- usage, BUMPVERTEX::FVF,
- D3DPOOL_MANAGED, &m_pEarthVB, NULL ) ) )
- return E_FAIL;
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: SetMenuStates()
- // Desc:
- //-----------------------------------------------------------------------------
- VOID CMyD3DApplication::SetMenuStates()
- {
- HMENU hMenu = GetMenu( m_hWnd );
- CheckMenuItem( hMenu, IDM_TEXTURETOGGLE,
- m_bTextureOn ? MF_CHECKED : MF_UNCHECKED );
- CheckMenuItem( hMenu, IDM_BUMPMAPTOGGLE,
- m_bBumpMapOn ? MF_CHECKED : MF_UNCHECKED );
- CheckMenuItem( hMenu, IDM_ENVMAPTOGGLE,
- m_bEnvMapOn ? MF_CHECKED : MF_UNCHECKED );
- CheckMenuItem( hMenu, IDM_DISPMAPTOGGLE,
- m_bDispMapOn ? MF_CHECKED : MF_UNCHECKED );
- CheckMenuItem( hMenu, IDM_U8V8L8,
- m_BumpMapFormat==D3DFMT_X8L8V8U8 ? MF_CHECKED : MF_UNCHECKED );
- CheckMenuItem( hMenu, IDM_U5V5L6,
- m_BumpMapFormat==D3DFMT_L6V5U5 ? MF_CHECKED : MF_UNCHECKED );
- CheckMenuItem( hMenu, IDM_U8V8,
- m_BumpMapFormat==D3DFMT_V8U8 ? MF_CHECKED : MF_UNCHECKED );
- CheckMenuItem( hMenu, IDM_LOW_TESSELATION,
- (!m_bHighTesselation) ? MF_CHECKED : MF_UNCHECKED );
- CheckMenuItem( hMenu, IDM_HIGH_TESSELATION,
- m_bHighTesselation ? MF_CHECKED : MF_UNCHECKED );
- }
- //-----------------------------------------------------------------------------
- // Name: MsgProc()
- // Desc: 消息处理函数,处理来自键盘和菜单的消息
- //-----------------------------------------------------------------------------
- LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
- LPARAM lParam )
- {
- // 将鼠标消息传递给 ArcBall ,从而建立变换矩阵
- m_ArcBall.HandleMouseMessages( hWnd, uMsg, wParam, lParam );
- // Trap context menu
- if( WM_CONTEXTMENU == uMsg )
- return 0;
- //处理菜单命令
- if( WM_COMMAND == uMsg )
- {
- switch( LOWORD(wParam) )
- {
- case IDM_TEXTURETOGGLE:
- m_bTextureOn = !m_bTextureOn;
- break;
- case IDM_BUMPMAPTOGGLE:
- m_bBumpMapOn = !m_bBumpMapOn;
- break;
- case IDM_DISPMAPTOGGLE:
- m_bDispMapOn = !m_bDispMapOn;
- CreateEarthVertexBuffer();
- break;
- case IDM_ENVMAPTOGGLE:
- m_bEnvMapOn = !m_bEnvMapOn;
- break;
- case IDM_U8V8L8:
- SAFE_RELEASE( m_psBumpMap );
- m_BumpMapFormat = D3DFMT_X8L8V8U8;
- InitBumpMap();
- break;
- case IDM_U5V5L6:
- SAFE_RELEASE( m_psBumpMap );
- m_BumpMapFormat = D3DFMT_L6V5U5;
- InitBumpMap();
- break;
- case IDM_U8V8:
- SAFE_RELEASE( m_psBumpMap );
- m_BumpMapFormat = D3DFMT_V8U8;
- InitBumpMap();
- break;
- case IDM_LOW_TESSELATION:
- m_bHighTesselation = FALSE;
- CreateEarthVertexBuffer();
- break;
- case IDM_HIGH_TESSELATION:
- m_bHighTesselation = TRUE;
- CreateEarthVertexBuffer();
- break;
- }
- // 当有状态变化时,更新菜单
- SetMenuStates();
- }
- // 将其他消息传递给默认处理函数
- return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
- }