BumpMap.cpp
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:12k
源码类别:
DirextX编程
开发平台:
Visual C++
- // BumpMap.cpp : Defines the class behaviors for the application.
- #include "stdafx.h"
- #include "BumpMap.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- const bool g_bFullScreen
- #ifdef _DEBUG
- = false; // true;
- #else
- = true; // false;
- #endif // _DEBUG
- const bool g_bUseZBuffer = true; // false;
- const DWORD g_dwWidth = 640; // 800; // 1024;
- const DWORD g_dwHeight = 480; // 600; // 768;
- const DWORD g_dwBPP = 16; // 32;
- const bool g_bUseVoodoo12 = false; // true;
- const _GUID* const g_lpGuidD3DDevice
- // = NULL;
- // = &IID_IDirect3DTnLHalDevice;
- // = &IID_IDirect3DRGBDevice;
- // = &IID_IDirect3DHALDevice;
- = &IID_IDirect3DRefDevice;
- bool g_bTextureOn = true;
- bool g_bBumpMapOn = true;
- bool g_bEnvMapOn = true;
- CD3DTexture::BUMPMAPFORMAT g_BumpMapFormat = CD3DTexture::BUMPMAP_U5V5L6;
- CBumpMapApp::CBumpMapApp(void)
- {
- m_dwAppInitFlags &= ~CDirectXApp::DXAPPIF_DD;
- m_dwAppInitFlags |= CDirectXApp::DXAPPIF_D3DIM;
- SetFullScreen(g_bFullScreen);
- SetPackFileName(_T("Data"), _T("gui"));
- // Called during initial app startup, this function
- // performs all the permanent initialization.
- m_pD3DDevice = NULL;
- m_pD3DMaterial = NULL;
- // add your permanent init code here !
- m_pBlockTexture = m_pEarthTexture = m_pEarthBumpTexture
- = m_pEarthEnvMapTexture = NULL;
- m_pLight = NULL;
- // Initialize geometry
- // Sets up the vertices for a bump-mapped sphere.
- m_pEllipse = new CD3DEllipse(14, 14,
- CD3DUN2Vertex::TypeDesc);
- }
- // Called before the app exits, this function gives the app
- // the chance to cleanup after itself.
- CBumpMapApp::~CBumpMapApp()
- {
- // add your cleaup code here !
- if (m_pEllipse != NULL)
- {
- delete m_pEllipse;
- m_pEllipse = NULL;
- }
- }
- bool CBumpMapApp::GetDXInitSettings(void)
- {
- if (m_pDirectSound != NULL)
- {
- if (m_pDirectSound->SelectDSDriver(0) == false)
- return false;
- }
- if (m_pDirectMusic != NULL)
- {
- if (m_pDirectMusic->SelectDefaultDMusPort() == false)
- return false;
- }
- if ((g_bUseVoodoo12 == false)
- || m_pDirectDraw->SelectDDDevice(0, 1) == false)
- {
- if (m_pDirectDraw->SelectDDDevice(0) == false)
- return false;
- }
- if (g_bUseZBuffer)
- m_dwDDInitFlags |= CDDDevice::D3DIF_ZBUFFER;
- if (GetFirstDDDevice()->IsCanRenderWindowed() == false)
- {
- m_dwDDInitFlags &= ~CDDDevice::DDIF_WINDOWED;
- m_dwDDInitFlags &= ~CDDDevice::DDIF_NO_FLIP;
- }
- else
- {
- m_dwDDInitFlags |= CDDDevice::DDIF_NO_FLIP;
- }
- if (g_lpGuidD3DDevice == NULL)
- {
- if (GetFirstDDDevice()->SelectDefaultD3DDevice() == false)
- return false;
- }
- else
- {
- if (GetFirstDDDevice()->SelectD3DDevice(g_lpGuidD3DDevice) == false)
- return false;
- }
- if (GetFirstDDDevice()->SelectDisplayMode(
- IsFullScreen(), g_dwWidth, g_dwHeight, g_dwBPP) == false)
- return false;
- return true;
- }
- // Called during device intialization, this code checks the device
- // for some minimum set of capabilities, Initialize scene objects.
- bool CBumpMapApp::InitDXObjects(void)
- {
- m_pD3DDevice = GetFirstD3DDevice();
- ASSERT(m_pD3DDevice != NULL);
- m_pD3DMaterial = m_pD3DDevice->GetD3DMaterial();
- // add your init code here !
- DWORD dwTextureOpCaps = m_pD3DDevice->GetD3DDeviceDesc()->dwTextureOpCaps;
- if ((dwTextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP) == 0
- || (dwTextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE) == 0)
- return false;
- // Load texture maps
- m_pBlockTexture = new CD3DTexture;
- if (m_pBlockTexture->Create(m_pD3DDevice,
- _T("block.bmp"), m_pPackFileManager, 0, 0) == false)
- return false;
- m_pEarthTexture = new CD3DTexture;
- if (m_pEarthTexture->Create(m_pD3DDevice,
- _T("earth.bmp"), m_pPackFileManager, 0, 0) == false)
- return false;
- m_pEarthBumpTexture = new CD3DTexture;
- if (m_pEarthBumpTexture->CreateBumpMapTexture(m_pD3DDevice,
- _T("earthbump.bmp"), m_pPackFileManager, g_BumpMapFormat, 1) == false)
- return false;
- m_pEarthEnvMapTexture = new CD3DTexture;
- if (m_pEarthEnvMapTexture->Create(m_pD3DDevice,
- _T("EarthEnvMap.bmp"), m_pPackFileManager, 0, 0) == false)
- return false;
- for (DWORD t = 0; t < 3; ++ t)
- {
- m_pD3DDevice->SetTextureStageState(t, D3DTSS_ADDRESS, D3DTADDRESS_WRAP);
- m_pD3DDevice->SetTextureStageState(t, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
- m_pD3DDevice->SetTextureStageState(t, D3DTSS_MINFILTER, D3DTFN_LINEAR);
- }
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_DITHERENABLE, TRUE);
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, FALSE);
- // Set matrices
- D3DVECTOR vEyePt(0.0f, 2.0f, 10.0f);
- D3DVECTOR vLookatPt(0.0f, 0.0f, 0.0f);
- D3DVECTOR vUpVec(0.0f, 1.0f, 0.0f);
- CD3DMatrix matScale, matWorld, matView, matProj;
- matView.SetView(vEyePt, vLookatPt, vUpVec);
- matProj.SetProjection(g_PI/4, 1.0f, 1.0f, 20.0f);
- matScale.SetScale(3.0f, 3.0f, 3.0f );
- matWorld.SetRotateY(80.0f * (g_PI/180) );
- matWorld *= matScale;
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &matView);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &matProj);
- // Set a material
- m_pD3DMaterial->SetColor(1.0f, 1.0f, 1.0f, 1.0f, D3D_ML_DIFFUSE);
- m_pD3DMaterial->Set();
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_AMBIENT, 0x000000FF);
- // Add a directional light
- m_pLight = new CD3DDirectionalLight(0.0f, 0.0f, 1.0f);
- m_pLight->SetColor(0.9f, 0.9f, 0.9f);
- if (m_pLight->Create(m_pD3DDevice) == false)
- return false;
- if (m_pLight->Enable(true) == false)
- return false;
- return true;
- }
- // Called when the app is exitting, or the device is being changed,
- // this function deletes any device dependant objects.
- bool CBumpMapApp::DestroyDXObjects(void)
- {
- // add your destroy code here !
- if (m_pLight != NULL)
- {
- delete m_pLight;
- m_pLight = NULL;
- }
- if (m_pBlockTexture != NULL)
- {
- delete m_pBlockTexture;
- m_pBlockTexture = NULL;
- }
- if (m_pEarthTexture != NULL)
- {
- delete m_pEarthTexture;
- m_pEarthTexture = NULL;
- }
- if (m_pEarthBumpTexture != NULL)
- {
- delete m_pEarthBumpTexture;
- m_pEarthBumpTexture = NULL;
- }
- if (m_pEarthEnvMapTexture != NULL)
- {
- delete m_pEarthEnvMapTexture;
- m_pEarthEnvMapTexture = NULL;
- }
- m_pD3DMaterial = NULL;
- m_pD3DDevice = NULL;
- return CDirectXApp::DestroyDXObjects();
- }
- // Called once per frame, the call is the entry point for
- // animating the scene. This function sets up render states,
- // clears the viewport, and renders the scene.
- bool CBumpMapApp::UpdateFrame(void)
- {
- // add your code here !
- const FLOAT fAngle = 30.0f;
- CD3DMatrix matWorld, matRotate;
- matRotate.SetRotateY(-fAngle * (g_PI/180));
- m_pD3DDevice->GetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
- matWorld = matRotate * matWorld;
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &matWorld);
- ApplyEnivronmentMap(
- static_cast<CD3DUN2Vertex*>(m_pEllipse->GetVertices()),
- m_pEllipse->GetNumVertices());
- m_pD3DDevice->Clear(RGBA_MAKE(0, 0, 0xFF, 0));
- if (SUCCEEDED(m_pD3DDevice->BeginScene()))
- {
- m_pD3DDevice->SetRenderState(D3DRENDERSTATE_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 (g_bTextureOn)
- m_pEarthTexture->SetAsCurrent();
- else
- m_pBlockTexture->SetAsCurrent();
- m_pD3DDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 1);
- 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 (g_bBumpMapOn)
- {
- //m_pEarthBumpTexture->SetStage(1);
- m_pEarthBumpTexture->SetAsCurrent();
- m_pD3DDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
- 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, CDirectX::FLOAT2DWORD(0.5f));
- m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT01, CDirectX::FLOAT2DWORD(0.0f));
- m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT10, CDirectX::FLOAT2DWORD(0.0f));
- m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT11, CDirectX::FLOAT2DWORD(0.5f));
- m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVLSCALE, CDirectX::FLOAT2DWORD(1.0f));
- m_pD3DDevice->SetTextureStageState(1, D3DTSS_BUMPENVLOFFSET, CDirectX::FLOAT2DWORD(0.0f));
- if (g_bEnvMapOn)
- {
- m_pEarthEnvMapTexture->SetStage(2);
- m_pEarthEnvMapTexture->SetAsCurrent();
- 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 (g_bEnvMapOn)
- {
- m_pEarthEnvMapTexture->SetStage(1);
- m_pEarthEnvMapTexture->SetAsCurrent();
- 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_pEllipse->Render(m_pD3DDevice);
- m_pD3DDevice->EndScene();
- }
- return CDirectXApp::UpdateFrame();
- }
- // ---- add your functions ! ----
- // Performs a calculation on each of the vertices' normals to determine
- // what the texture coordinates should be for the environment map.
- void CBumpMapApp::ApplyEnivronmentMap(CD3DUN2Vertex* pv,
- DWORD dwNumVertices)
- {
- // Get the World-View(WV) and Projection(P) matrices
- CD3DMatrix W, V, WV;
- m_pD3DDevice->GetTransform(D3DTRANSFORMSTATE_VIEW, &V);
- m_pD3DDevice->GetTransform(D3DTRANSFORMSTATE_WORLD, &W);
- m_pD3DDevice->MultiplyTransform(D3DTRANSFORMSTATE_VIEW, &W);
- m_pD3DDevice->GetTransform(D3DTRANSFORMSTATE_VIEW, &WV);
- m_pD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW, &V);
- // Loop through the vertices, transforming each one and calculating
- // the correct texture coordinates.
- for (WORD i = 0; i < dwNumVertices; ++ i)
- {
- FLOAT nx = pv[i].m_dvNX;
- FLOAT ny = pv[i].m_dvNY;
- FLOAT nz = pv[i].m_dvNZ;
- FLOAT nxv = nx * WV._11 + ny * WV._21 + nz * WV._31 + WV._41;
- FLOAT nyv = nx * WV._12 + ny * WV._22 + nz * WV._32 + WV._42;
- FLOAT nzv = nx * WV._13 + ny * WV._23 + nz * WV._33 + WV._43;
- FLOAT nlen = (FLOAT)::sqrt(nxv * nxv + nyv * nyv + nzv * nzv);
- pv[i].m_dvTU1 = 0.5f + 1.2f * nxv / nlen;
- pv[i].m_dvTV1 = 0.5f - 1.2f * nyv / nlen;
- }
- }
- // ----
- #if _MSC_VER >= 1200 && _MSC_VER < 1400
- #ifdef _DEBUG
- #pragma comment(lib, "DXGuideD_VC6.lib")
- #else
- #pragma comment(lib, "DXGuide_VC6.lib")
- #endif // _DEBUG
- #endif // _MSC_VER
- #if _MSC_VER >= 1000 && _MSC_VER < 1200
- #ifdef _DEBUG
- #pragma comment(lib, "DXGuideD_VC5.lib")
- #else
- #pragma comment(lib, "DXGuide_VC5.lib")
- #endif // _DEBUG
- #endif // _MSC_VER
- BEGIN_MESSAGE_MAP(CBumpMapApp, CDirectXApp)
- //{{AFX_MSG_MAP(CBumpMapApp)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- CBumpMapApp theApp;