完成DXSDK的安裝後,在開始菜單中,找到DirectX Sample Browser:
這裏麪包含了微軟提供的學習例子。
找到並安裝 Tutorial 4: Lights 。
接下來,我們對 Lights 這個例子進行面向對象的封裝。
【封裝過程】
一、 增加 DxGraphics.h 和 DxGraphics.cpp 文件
DxGraphics.h 內容如下:
#ifndef __GRAPHICS_H__
#define __GRAPHICS_H__
#include <d3dx9.h>
struct CUSTOM_VERTEX
{
D3DXVECTOR3 position; // The 3D position for the vertex
D3DXVECTOR3 normal; // The surface normal for the vertex
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
class DxGraphics
{
public:
~DxGraphics();
// return the instance of DxGraphics
static DxGraphics* getInstance();
static void releaseInstance();
// Init D3D
bool Init(HWND hWnd);
// user define geometry
bool InitTestGeometry();
// init world
void InitWorldMatrices();
// setup lights
void SetupLights();
// render geometry
void Render();
private:
DxGraphics();
// D3D 接口
LPDIRECT3D9 m_pD3d;
// D3D 設備
LPDIRECT3DDEVICE9 m_pD3dDevice;
// Vertex buffer
LPDIRECT3DVERTEXBUFFER9 m_pVertexBuffer;
};
#endif // __GRAPHICS_H__
|
#include "DxGraphics.h"
static DxGraphics* p_Graphics = NULL;
DxGraphics* DxGraphics::getInstance()
{
if (NULL == p_Graphics)
p_Graphics = new DxGraphics();
return p_Graphics;
}
void DxGraphics::releaseInstance()
{
delete(p_Graphics);
p_Graphics = NULL;
}
DxGraphics::~DxGraphics(void)
{
if( m_pD3d != NULL )
m_pD3d->Release();
if( m_pD3dDevice != NULL )
m_pD3dDevice->Release();
if( m_pVertexBuffer != NULL )
m_pVertexBuffer->Release();
}
DxGraphics::DxGraphics()
{
m_pD3d = NULL;
m_pD3dDevice = NULL;
m_pVertexBuffer = NULL;
}
////////////////////////////////////////////////////////////////////////////////
bool DxGraphics::Init(HWND hWnd)
{
// Create the D3D object.
if( NULL == ( m_pD3d = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return false;
// Set up the structure used to create the D3DDevice. Since we are now
// using more complex geometry, we will create a device with a zbuffer.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// Create the D3DDevice
if( FAILED( m_pD3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &m_pD3dDevice ) ) )
{
return false;
}
// Turn off culling
m_pD3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn on the zbuffer
m_pD3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
return true;
}
bool DxGraphics::InitTestGeometry()
{
// Create the vertex buffer.
if( FAILED( m_pD3dDevice->CreateVertexBuffer( 50 * 2 * sizeof( CUSTOM_VERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &m_pVertexBuffer, NULL ) ) )
{
return false;
}
// Fill the vertex buffer. We are algorithmically generating a cylinder
// here, including the normals, which are used for lighting.
CUSTOM_VERTEX* pVertices;
if( FAILED( m_pVertexBuffer->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
return false;
for( DWORD i = 0; i < 50; i++ )
{
FLOAT theta = ( 2 * D3DX_PI * i ) / ( 50 - 1 );
pVertices[2 * i + 0].position = D3DXVECTOR3( sinf( theta ), -1.0f, cosf( theta ) );
pVertices[2 * i + 0].normal = D3DXVECTOR3( sinf( theta ), 0.0f, cosf( theta ) );
pVertices[2 * i + 1].position = D3DXVECTOR3( sinf( theta ), 1.0f, cosf( theta ) );
pVertices[2 * i + 1].normal = D3DXVECTOR3( sinf( theta ), 0.0f, cosf( theta ) );
}
m_pVertexBuffer->Unlock();
return true;
}
//-----------------------------------------------------------------------------
// Name: SetupLights()
// Desc: Sets up the Lights and materials for the scene.
//-----------------------------------------------------------------------------
void DxGraphics::SetupLights()
{
// Set up a material. The material here just has the diffuse and ambient
// colors set to yellow. Note that only one material can be used at a time.
D3DMATERIAL9 mtrl;
ZeroMemory( &mtrl, sizeof( D3DMATERIAL9 ) );
mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
mtrl.Diffuse.b = mtrl.Ambient.b = 0.0f;
mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;
m_pD3dDevice->SetMaterial( &mtrl );
// Set up a white, directional light, with an oscillating direction.
// Note that many Lights may be active at a time (but each one slows down
// the rendering of our scene). However, here we are just using one. Also,
// we need to set the D3DRS_LIGHTING renderstate to enable lighting
D3DXVECTOR3 vecDir;
D3DLIGHT9 light;
ZeroMemory( &light, sizeof( D3DLIGHT9 ) );
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.r = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.b = 1.0f;
vecDir = D3DXVECTOR3( cosf( timeGetTime() / 350.0f ),
1.0f,
sinf( timeGetTime() / 350.0f ) );
D3DXVec3Normalize( ( D3DXVECTOR3* )&light.Direction, &vecDir );
light.Range = 1000.0f;
m_pD3dDevice->SetLight( 0, &light );
m_pD3dDevice->LightEnable( 0, TRUE );
m_pD3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
// Finally, turn on some ambient light.
m_pD3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
}
//-----------------------------------------------------------------------------
// Name: InitWorldMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
void DxGraphics::InitWorldMatrices()
{
// Set up world matrix
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity( &matWorld );
D3DXMatrixRotationX( &matWorld, timeGetTime() / 500.0f );
m_pD3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Set up our view matrix. A view matrix can be defined given an eye point,
// a point to lookat, and a direction for which way is up. Here, we set the
// eye five units back along the z-axis and up three units, look at the
// origin, and define "up" to be in the y-direction.
D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
m_pD3dDevice->SetTransform( D3DTS_VIEW, &matView );
// For the projection matrix, we set up a perspective transform (which
// transforms geometry from 3D view space to 2D viewport space, with
// a perspective divide making objects smaller in the distance). To build
// a perpsective transform, we need the field of view (1/4 pi is common),
// the aspect ratio, and the near and far clipping planes (which define at
// what distances geometry should be no longer be rendered).
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
m_pD3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
void DxGraphics::Render()
{
// Clear the backbuffer and the zbuffer
m_pD3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
// Begin the scene
if( SUCCEEDED( m_pD3dDevice->BeginScene() ) )
{
// Setup the Lights and materials
SetupLights();
// Setup the world, view, and projection matrices
InitWorldMatrices();
// Render the vertex buffer contents
m_pD3dDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof( CUSTOM_VERTEX ) );
m_pD3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
m_pD3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 * 50 - 2 );
// End the scene
m_pD3dDevice->EndScene();
}
// Present the backbuffer contents to the display
m_pD3dDevice->Present( NULL, NULL, NULL, NULL );
}
|
//-----------------------------------------------------------------------------
// File: Main.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning
#include <strsafe.h>
#pragma warning( default : 4996 )
#include "DxGraphics.h"
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
DxGraphics::releaseInstance();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER( hInstance );
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL
};
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 04: Lights",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL );
DxGraphics* pGraphics = DxGraphics::getInstance();
// Initialize Direct3D
if( pGraphics->Init(hWnd) )
{
// Create the geometry
if( pGraphics->InitTestGeometry() )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Enter the message loop
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
pGraphics->Render();
}
}
}
UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}
|
link...
編譯...
enjoy!