D3DX8指南02_Vertices

根据 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_Vertices;

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

// 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..2] of CUSTOMVERTEX=
 (
   (x:150.0; y: 50.0; z:0.5; rhw:1.0; color:$FF0000), // x, y, z, rhw, color
   (x:250.0; y:250.0; z:0.5; rhw:1.0; color:$00FF00),
   (x: 50.0; y:250.0; z:0.5; rhw:1.0; color:$00FFFF)
 );
var
 pVertices:^CUSTOMVERTEX;
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(3*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;

  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: 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.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 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值