您的位置:首页 > 其它

D3DX8指南03_Matrices

2007-04-01 01:11 169 查看
根据 DirectX 8.1 SDK samples/Multimedia/Direct3D/Tutorials/Tut03_Matrices翻译
保留原文注释

//-----------------------------------------------------------------------------
// File: Matrices.cpp
//
// Desc: Now that we know how to create a device and render some 2D vertices,
// this tutorial goes the next step and renders 3D geometry. To deal with
// 3D geometry we need to introduce the use of 4x4 matrices to transform
// the geometry with translations, rotations, scaling, and setting up our
// camera.
//
// Geometry is defined in model space. We can move it (translation),
// rotate it (rotation), or stretch it (scaling) using a world transform.
// The geometry is then said to be in world space. Next, we need to
// position the camera, or eye point, somewhere to look at the geometry.
// Another transform, via the view matrix, is used, to position and
// rotate our view. With the geometry then in view space, our last
// transform is the projection transform, which "projects" the 3D scene
// into our 2D viewport.
//
// Note that in this tutorial, we are introducing the use of D3DX, which
// is a set of helper utilities for D3D. In this case, we are using some
// of D3DX's useful matrix initialization functions. To use D3DX, simply
// include <d3dx8.h> and link with d3dx8.lib.
//
//-----------------------------------------------------------------------------
program Tut03_Matrices;

uses
Windows,
Messages,
Direct3D8 in 'JEDI/Direct3D8.pas',
DXTypes in 'JEDI/DXTypes.pas',
DXFile in 'JEDI/DXFile.pas',
D3DX8 in 'JEDI/D3DX8.pas';

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
var
g_pD3D :IDirect3D8 = nil; // Used to create the D3DDevice
g_pd3dDevice:IDirect3DDevice8 = nil; // Our rendering device
g_pVB :IDirect3DVertexBuffer8 = nil; // Buffer to hold vertices

// A structure for our custom vertex type
type
CUSTOMVERTEX=record
x, y, z :Single; // The untransformed, 3D position for the vertex
color :DWORD; // The vertex color
end;

// Our custom FVF, which describes our custom vertex structure
const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZ or D3DFVF_DIFFUSE);

//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
function InitD3D(hWnd: THandle) :HRESULT;
var
d3ddm :TD3DDisplayMode;
d3dpp :TD3DPresentParameters;
begin
// Create the D3D object.
g_pD3D:=Direct3DCreate8(D3D_SDK_VERSION);
if g_pD3D=nil then begin
Result:=E_FAIL;
exit;
end;

// Get the current desktop display mode, so we can set up a back
// buffer of the same format
if FAILED(g_pD3D.GetAdapterDisplayMode( D3DADAPTER_DEFAULT, d3ddm )) then begin
Result:=E_FAIL;
exit;
end;

// Set up the structure used to create the D3DDevice Fillchar( d3dpp, sizeof(d3dpp), 0 );
Fillchar(d3dpp, sizeof(d3dpp), 0);
d3dpp.Windowed := TRUE;
d3dpp.SwapEffect := D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat := d3ddm.Format;

// Create the Direct3D device.
if FAILED(g_pD3D.CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
d3dpp, g_pd3dDevice )) then begin
Result:=E_FAIL;
exit;
end;

// Turn off culling, so we see the front and back of the triangle
g_pd3dDevice.SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice.SetRenderState( D3DRS_LIGHTING, Ord(FALSE) );

Result:=S_OK;
end;

//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Creates the scene geometry
//-----------------------------------------------------------------------------
function InitGeometry :HRESULT;
const
// Initialize three vertices for rendering a triangle
g_Vertices:array[0..2] of CUSTOMVERTEX=
(
(x:-1.0; y:-1.0; z:0.0; color:$FF0000),
(x: 1.0; y:-1.0; z:0.0; color:$0000FF),
(x: 0.0; y: 1.0; z:0.0; color:$FFFFFF)
);
var
pVertices:^CUSTOMVERTEX;
begin
// Create the vertex buffer.
if FAILED(g_pd3dDevice.CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, g_pVB)) then begin
Result:=E_FAIL;
exit;
end;

// Fill the vertex buffer.
pVertices:=nil;
if FAILED(g_pVB.Lock(0, sizeof(g_Vertices), PByte(pVertices), 0)) then begin
Result:=E_FAIL;
exit;
end;
CopyMemory(pVertices, @g_Vertices[0], sizeof(g_Vertices));
g_pVB.Unlock;

pVertices:=nil;

Result:=S_OK;
end;

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
procedure Cleanup;
begin
g_pVB :=nil;
g_pd3dDevice:=nil;
g_pD3D :=nil;
end;

//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
procedure SetupMatrices;
var
matWorld, matView, matProj :TD3DXMatrix;
begin
// For our world matrix, we will just rotate the object about the y-axis.
D3DXMatrixRotationY( matWorld, GetTickCount/1000.0 ); //原文使用mmsystem的timeGetTime()/150.0f
g_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.
D3DXMatrixLookAtLH( matView, D3DXVECTOR3( 0.0, 3.0,-5.0 ),
D3DXVECTOR3( 0.0, 0.0, 0.0 ),
D3DXVECTOR3( 0.0, 1.0, 0.0 ) );
g_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).
D3DXMatrixPerspectiveFovLH( matProj, D3DX_PI/4, 1.0, 1.0, 100.0 );
g_pd3dDevice.SetTransform( D3DTS_PROJECTION, matProj );
end;

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
procedure Render;
begin
// Clear the backbuffer to a blue color
g_pd3dDevice.Clear(0, nil, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0, 0);

// Begin the scene
g_pd3dDevice.BeginScene;

// Setup the world, view, and projection matrices
SetupMatrices();

// Render the vertex buffer contents
g_pd3dDevice.SetStreamSource(0, g_pVB, sizeof(CUSTOMVERTEX));
g_pd3dDevice.SetVertexShader(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice.DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

// End the scene
g_pd3dDevice.EndScene;

// Present the backbuffer contents to the display
g_pd3dDevice.Present(nil, nil, 0, nil);
end;

//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
function MsgProc( h_Wnd : THandle; aMSG : Cardinal; wParam : Cardinal; lParam : Integer ) : LRESULT; stdcall;
begin
case aMSG of
WM_DESTROY:
PostQuitMessage( 0 );
end;

Result :=DefWindowProc(h_Wnd, aMSG, wParam, lParam);
end;

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
function WinMain( hInst :LongWord ) :Integer;
var
wc :TWndClassEx;
hWnd :THandle;
aMsg :TMsg;
begin
// Register the window class
FillChar(wc,sizeof(wc),0);
wc.cbSize:=sizeof(wc);
wc.style:=CS_CLASSDC;
wc.lpfnWndProc:=@MsgProc;
wc.cbClsExtra:=0;
wc.cbWndExtra:=0;
wc.hInstance:=hInst;
wc.hIcon:=0;
wc.hCursor:=0;
wc.hbrBackground:=0;
wc.lpszMenuName:=nil;
wc.lpszClassName:='D3D Tutorial';
wc.hIconSm:=0;
RegisterClassEx(wc);

// Create the application's window
hWnd := CreateWindow('D3D Tutorial', 'D3D Tutorial 02: Vertices',
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), 0, wc.hInstance, nil);

// Initialize Direct3D
if SUCCEEDED(InitD3D(hWnd)) then
// Create the scene geometry
if SUCCEEDED(InitGeometry) then begin
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );

// Enter the message loop
Fillchar(aMSG, sizeof(aMSG), 0);
while not (aMsg.message = WM_QUIT) do
if PeekMessage( aMsg, 0, 0, 0, PM_REMOVE ) then begin
TranslateMessage ( aMsg ) ;
DispatchMessage ( aMsg ) ;
end else
Render;
end;

// Clean up everything and exit the app
Cleanup;
UnregisterClass( 'D3D Tutorial', wc.hInstance );
Result:=0;
end;

begin
WinMain(hInstance);
Halt(0);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: