D3DX8指南07_索引顶点演示
沿用 DirectX 8.1 SDK samples/Multimedia/Direct3D/Tutorials/Tut02_Vertices扩展而来
增加部分注释
//-----------------------------------------------------------------------------
// File: Vertices.cpp
//
// Desc: In this tutorial, we are rendering some vertices. This introduces the
// concept of the vertex buffer, a Direct3D object used to store
// vertices. Vertices can be defined any way we want by defining a
// custom structure and a custom FVF (flexible vertex format). In this
// tutorial, we are using vertices that are transformed (meaning they
// are already in 2D window coordinates) and lit (meaning we are not
// using Direct3D lighting, but are supplying our own colors).
//-----------------------------------------------------------------------------
program Tut02_index;
uses
Windows,
Messages,
Direct3D8 in 'JEDI/Direct3D8.pas',
DXTypes in 'JEDI/DXTypes.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
g_pIB :IDirect3DIndexBuffer8 = nil; // 顶点索引指针
// A structure for our custom vertex type
type
CUSTOMVERTEX=record
x, y, z, rhw :Single; // The transformed position for the vertex
color :DWORD; // The vertex color
end;
// Our custom FVF, which describes our custom vertex structure
const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZRHW 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;
// Device state would normally be set here
Result:=S_OK;
end;
//-----------------------------------------------------------------------------
// Name: InitVB()
// Desc: Creates a vertex buffer and fills it with our vertices. The vertex
// buffer is basically just a chuck of memory that holds vertices. After
// creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
// also uses index buffers. The special thing about vertex and index
// buffers is that they can be created in device memory, allowing some
// cards to process them in hardware, resulting in a dramatic
// performance gain.
//-----------------------------------------------------------------------------
function InitVB :HRESULT;
const
// Initialize three vertices for rendering a triangle
g_Vertices:array[0..3] of CUSTOMVERTEX=
(
{A} (x: 50.0; y:250.0; z:0.5; rhw:1.0; color:$FF0000), // x, y, z, rhw, color
{B} (x: 50.0; y: 50.0; z:0.5; rhw:1.0; color:$00FF00), //定义4个点来构成矩形
{C} (x:250.0; y:250.0; z:0.5; rhw:1.0; color:$00FFFF),
{D} (x:250.0; y: 50.0; z:0.5; rhw:1.0; color:$FFFF00)
);
var
pVertices:^CUSTOMVERTEX;
P:PWord;
begin
// Create the vertex buffer. Here we are allocating enough memory
// (from the default pool) to hold all our 3 custom vertices. We also
// specify the FVF, so the vertex buffer knows what data it contains.
if FAILED(g_pd3dDevice.CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, g_pVB)) then begin
Result:=E_FAIL;
exit;
end;
// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
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;
//建立顶点缓冲区
if FAILED(g_pd3dDevice.CreateIndexBuffer(6*sizeof(WORD), 0,
D3DFMT_INDEX16, D3DPOOL_DEFAULT,
g_pIB)) then begin
Result:=E_FAIL;
exit;
end;
//Length 缓冲区大小,矩形为2个三角形组成因此给出了6个点。
//Usage 暂时不管,填0。
//Format 索引的格式,我们用16位存储索引值(也就是说最大65535个顶点)。
//Pool 数表示通过什么方式创建,用缺省值就好了
//ppIndexBuffer 返回缓冲区指针。
// 填充顶点缓冲区
P:=nil;
if FAILED(g_pIB.Lock(0, 6*sizeof(WORD), PByte(P), 0)) then begin
Result:=E_FAIL;
exit;
end;
P^:=0; //顶点A
Inc(P);
P^:=1; //顶点B
Inc(P);
P^:=2; //顶点C
Inc(P);
P^:=1; //顶点B
Inc(P);
P^:=3; //顶点D
Inc(P);
P^:=2; //顶点C
g_pIB.Unlock;
Result:=S_OK;
end;
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
procedure Cleanup;
begin
g_pIB :=nil;
g_pVB :=nil;
g_pd3dDevice:=nil;
g_pD3D :=nil;
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;
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
g_pd3dDevice.SetStreamSource(0, g_pVB, sizeof(CUSTOMVERTEX));
//加栽顶点索引
g_pd3dDevice.SetIndices(g_pIB, 0);
g_pd3dDevice.SetVertexShader(D3DFVF_CUSTOMVERTEX);
//渲染
g_pd3dDevice.DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 4, 0, 2);
//_Type 渲染类型(点、线、面等)
//minIndex 顶点缓冲区中顶点的起始值,一般为0
//NumVertices 顶点缓冲区中的顶点数量
//startIndex 索引缓冲区中索引的起始值
//primCount 要渲染图形的数量(2个三角形)
// 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 Index',
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), 0, wc.hInstance, nil);
// Initialize Direct3D
if SUCCEEDED(InitD3D(hWnd)) then
// Create the vertex buffer
if SUCCEEDED(InitVB) 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.
D3DX8指南07_索引顶点演示
沿用 DirectX 8.1 SDK samples/Multimedia/Direct3D/Tutorials/Tut02_Vertices扩展而来
增加部分注释
//-----------------------------------------------------------------------------
// File: Vertices.cpp
//
// Desc: In this tutorial, we are rendering some vertices. This introduces the
// concept of the vertex buffer, a Direct3D object used to store
// vertices. Vertices can be defined any way we want by defining a
// custom structure and a custom FVF (flexible vertex format). In this
// tutorial, we are using vertices that are transformed (meaning they
// are already in 2D window coordinates) and lit (meaning we are not
// using Direct3D lighting, but are supplying our own colors).
//-----------------------------------------------------------------------------
program Tut02_index;
uses
Windows,
Messages,
Direct3D8 in 'JEDI/Direct3D8.pas',
DXTypes in 'JEDI/DXTypes.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
g_pIB :IDirect3DIndexBuffer8 = nil; // 顶点索引指针
// A structure for our custom vertex type
type
CUSTOMVERTEX=record
x, y, z, rhw :Single; // The transformed position for the vertex
color :DWORD; // The vertex color
end;
// Our custom FVF, which describes our custom vertex structure
const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZRHW 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;
// Device state would normally be set here
Result:=S_OK;
end;
//-----------------------------------------------------------------------------
// Name: InitVB()
// Desc: Creates a vertex buffer and fills it with our vertices. The vertex
// buffer is basically just a chuck of memory that holds vertices. After
// creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
// also uses index buffers. The special thing about vertex and index
// buffers is that they can be created in device memory, allowing some
// cards to process them in hardware, resulting in a dramatic
// performance gain.
//-----------------------------------------------------------------------------
function InitVB :HRESULT;
const
// Initialize three vertices for rendering a triangle
g_Vertices:array[0..3] of CUSTOMVERTEX=
(
{A} (x: 50.0; y:250.0; z:0.5; rhw:1.0; color:$FF0000), // x, y, z, rhw, color
{B} (x: 50.0; y: 50.0; z:0.5; rhw:1.0; color:$00FF00), //定义4个点来构成矩形
{C} (x:250.0; y:250.0; z:0.5; rhw:1.0; color:$00FFFF),
{D} (x:250.0; y: 50.0; z:0.5; rhw:1.0; color:$FFFF00)
);
var
pVertices:^CUSTOMVERTEX;
P:PWord;
begin
// Create the vertex buffer. Here we are allocating enough memory
// (from the default pool) to hold all our 3 custom vertices. We also
// specify the FVF, so the vertex buffer knows what data it contains.
if FAILED(g_pd3dDevice.CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, g_pVB)) then begin
Result:=E_FAIL;
exit;
end;
// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
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;
//建立顶点缓冲区
if FAILED(g_pd3dDevice.CreateIndexBuffer(6*sizeof(WORD), 0,
D3DFMT_INDEX16, D3DPOOL_DEFAULT,
g_pIB)) then begin
Result:=E_FAIL;
exit;
end;
//Length 缓冲区大小,矩形为2个三角形组成因此给出了6个点。
//Usage 暂时不管,填0。
//Format 索引的格式,我们用16位存储索引值(也就是说最大65535个顶点)。
//Pool 数表示通过什么方式创建,用缺省值就好了
//ppIndexBuffer 返回缓冲区指针。
// 填充顶点缓冲区
P:=nil;
if FAILED(g_pIB.Lock(0, 6*sizeof(WORD), PByte(P), 0)) then begin
Result:=E_FAIL;
exit;
end;
P^:=0; //顶点A
Inc(P);
P^:=1; //顶点B
Inc(P);
P^:=2; //顶点C
Inc(P);
P^:=1; //顶点B
Inc(P);
P^:=3; //顶点D
Inc(P);
P^:=2; //顶点C
g_pIB.Unlock;
Result:=S_OK;
end;
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
procedure Cleanup;
begin
g_pIB :=nil;
g_pVB :=nil;
g_pd3dDevice:=nil;
g_pD3D :=nil;
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;
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
g_pd3dDevice.SetStreamSource(0, g_pVB, sizeof(CUSTOMVERTEX));
//加栽顶点索引
g_pd3dDevice.SetIndices(g_pIB, 0);
g_pd3dDevice.SetVertexShader(D3DFVF_CUSTOMVERTEX);
//渲染
g_pd3dDevice.DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 4, 0, 2);
//_Type 渲染类型(点、线、面等)
//minIndex 顶点缓冲区中顶点的起始值,一般为0
//NumVertices 顶点缓冲区中的顶点数量
//startIndex 索引缓冲区中索引的起始值
//primCount 要渲染图形的数量(2个三角形)
// 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 Index',
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), 0, wc.hInstance, nil);
// Initialize Direct3D
if SUCCEEDED(InitD3D(hWnd)) then
// Create the vertex buffer
if SUCCEEDED(InitVB) 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.