8,304
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <mmsystem.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "CMESH.H"
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"winmm.lib")
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool initDirect3D(HWND hwnd);
void render(void);
int screen_height = 480;
int screen_width = 640;
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
CMESH *g_pSkyBox;
LPDIRECT3DVERTEXBUFFER9 pVB;
LPD3DXMESH pTeapot;
LPD3DXEFFECT pEffect;
LPDIRECT3DCUBETEXTURE9 m_pCubeMap;
ID3DXRenderToEnvMap* m_pRenderToEnvMap;
HINSTANCE hInst;
HWND wndHandle;
D3DXMATRIX view,proj;
/////////////////////////////////////////////////
D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace )
{
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vLookDir;
D3DXVECTOR3 vUpDir;
switch( dwFace )
{
case D3DCUBEMAP_FACE_POSITIVE_X:
vLookDir = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_X:
vLookDir = D3DXVECTOR3(-1.0f, 0.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_POSITIVE_Y:
vLookDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_Y:
vLookDir = D3DXVECTOR3( 0.0f,-1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
break;
case D3DCUBEMAP_FACE_POSITIVE_Z:
vLookDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_Z:
vLookDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
}
// Set the view transform for this cubemap surface
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookDir, &vUpDir );
return matView;
}
HRESULT RenderSceneIntoEnvMap()
{
HRESULT hr;
// Set the projection matrix for a field of view of 90 degrees
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI * 0.5f, 1.0f, 0.5f, 1000.0f );
// Get the current view matrix, to concat it with the cubemap view vectors
D3DXMATRIXA16 matViewDir( view );
matViewDir._41 = 0.0f; matViewDir._42 = 0.0f; matViewDir._43 = 0.0f;
// Render the six cube faces into the environment map
if( m_pCubeMap )
hr = m_pRenderToEnvMap->BeginCube( m_pCubeMap );
if(FAILED(hr))
return hr;
for( UINT i = 0; i < 6; i++ )
{
m_pRenderToEnvMap->Face( (D3DCUBEMAP_FACES) i, 0 );
// Set the view transform for this cubemap surface
D3DXMATRIXA16 matView;
matView = D3DUtil_GetCubeMapViewMatrix( (D3DCUBEMAP_FACES) i );
D3DXMatrixMultiply( &matView, &matViewDir, &matView );
// Render the scene (except for the teapot)
// RenderScene( &matView, &matProj, FALSE );
}
m_pRenderToEnvMap->End( 0 );
return S_OK;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
if (!initWindow(hInstance))
{
MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
return false;
}
if (!initDirect3D(wndHandle))
{
MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
return false;
}
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
if( NULL == pd3dDevice )
break;
render();
}
}
if( pd3dDevice != NULL)
{
pd3dDevice->Release();
pd3dDevice = NULL;
}
if( pD3D != NULL)
{
pD3D->Release();
pD3D = NULL;
}
return (int) msg.wParam;
}
bool initWindow(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "DirectXExample";
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
wndHandle = CreateWindow("DirectXExample",
"DirectXExample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
NULL,
NULL,
hInstance,
NULL);
if (!wndHandle)
return false;
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
bool initDirect3D(HWND hwnd)
{
if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = screen_height;
d3dpp.BackBufferWidth = screen_width;
d3dpp.hDeviceWindow = hwnd;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.EnableAutoDepthStencil = TRUE;
if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &pd3dDevice ) ) )
{
return false;
}
pd3dDevice->SetRenderState( D3DRS_LIGHTING, true );
pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
g_pSkyBox=new CMESH(pd3dDevice,"lobby_skybox.x");
D3DXCreateTeapot(pd3dDevice,&pTeapot,NULL);
D3DXCreateEffectFromFile(pd3dDevice,"hlsl.fx",NULL,NULL,D3DXSHADER_DEBUG,NULL,&pEffect,NULL);
D3DXCreateCubeTextureFromFile(pd3dDevice, "lobbycube.dds", &m_pCubeMap);
D3DXCreateRenderToEnvMap( pd3dDevice, D3DX_DEFAULT, 1,
D3DFMT_UNKNOWN, TRUE, D3DFMT_D16, &m_pRenderToEnvMap );
D3DXVECTOR3 pos=D3DXVECTOR3(0,0,-10.5f);
D3DXVECTOR3 at=D3DXVECTOR3(0,0,0);
D3DXVECTOR3 up=D3DXVECTOR3(0,1,0);
D3DXMatrixLookAtLH(&view,&pos,&at,&up);
D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI*0.25,(float)640/(float)480,0.1,1000);
pd3dDevice->SetTransform(D3DTS_VIEW,&view);
pd3dDevice->SetTransform(D3DTS_PROJECTION,&proj);
return true;
}
void render(void)
{
static int k;
k++;
static float bk,tea;
D3DXMATRIX trans,rot,world;
D3DXMatrixRotationY(&rot,D3DXToRadian(bk+=0.5));
pd3dDevice->SetTransform(D3DTS_WORLD,&rot);
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,0.0f), 1.0f, 0 );
pd3dDevice->BeginScene();
g_pSkyBox->Render();
D3DXMatrixRotationY(&rot,D3DXToRadian(tea+=1.0));
world=rot;
D3DXMATRIX mWorldViewProj;
mWorldViewProj=world*view*proj;
pEffect->SetMatrix( "matWorldViewProj", &mWorldViewProj );
pEffect->SetMatrix( "matWorld", &world);
pEffect->SetVector( "vecEye", &D3DXVECTOR4(0,0,-10.5f,0));
UINT nPasses;
UINT iPass;
if( pEffect != NULL )
{
pEffect->SetTechnique(pEffect->GetTechniqueByName("TShader"));
pEffect->SetTexture("CubeMap", m_pCubeMap);
pEffect->Begin( &nPasses, 0 );
for( iPass = 0; iPass < nPasses; iPass ++ )
{
pEffect->SetTexture("CubeMap", m_pCubeMap);
pEffect->Pass( iPass );
pTeapot->DrawSubset( 0 );
}
pEffect->End();
}
pd3dDevice->EndScene();
pd3dDevice->Present( NULL, NULL, NULL, NULL );
}