)之间代码稍有不同之外,和空白窗口演示程序中的代码完全相同。一旦启动场景,就先调用SetStreamObjects()函数将顶点缓存设为渲染流。SetStreamObjects()函数的参数包括要设置的流索引(从0到最大流数–1)、开始渲染缓存的偏移字节量和顶点跨度。顶点跨度指的是正在渲染的缓存中,在几何图形中定义单个点时用到的顶点结构大小。
完成流设置后,就可以设置顶点格式FVF。这样,Direct3D就可以知道正在发送的数据格式。SetFVF()函数中最合适的标识符是用来指定顶点格式的标识符。源文件全局部分的最后一行代码是一个#define语句(参见程序清单1.11),该语句定义了绘制线段必须用到的名为D3DFVF_VERTEX标识符。对程序中用到的每个顶点结构而言, 都应该有一个匹配的顶点格式。
绘制出顶点缓存内容的最后一个步骤是调用DrawPrimitive()函数。DrawPrimitive()函数将绘制出当前设置为流的顶点缓存内容。该函数的参数包括:要渲染的图元类型、开始渲染的起始顶点索引(开始为0)以及要渲染的图元数目。由于这里指定在屏幕上绘制两条线段,因此最后一个参数的值为2。图元类型(第一个参数)可以是D3DPT_POINTLIST、D3DPT_LINELIST、D3DPT_LINESTRIP、D3DPT_TRIANGLELIST、D3DPT_TRANGLESTRIP或D3DPT_TRIANGLEFAN。
到此,就完成了全部的演示程序。
下面贴出Lines演示程序的全部代码:
[cpp]
#include
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME "Drawing Lines"
// Function Prototypes...
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();
// Direct3D object and device.
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;
// Vertex buffer to hold the geometry.
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;
// A structure for our custom vertex type
struct stD3DVertex
{
float x, y, z;
unsigned long color;
};
// Our custom FVF, which describes our custom vertex structure.
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_KEYUP:
if(wp == VK_ESCAPE) PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wp, lp);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,
LPSTR cmdLine, int show)
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc,
0, 0, GetModuleHandle(NULL), NULL, NULL,
NULL, NULL, WINDOW_CLASS, NULL };
RegisterClassEx(&wc);
// Create the application's window
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,
WS_OVERLAPPEDWINDOW, 100, 100,
640, 480, GetDesktopWindow(), NULL,
wc.hInstance, NULL);
// Initialize Direct3D
if(InitializeD3D(hWnd, false))
{
// 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
RenderScene();
}
}
// Release any and all resources.
Shutdown();
// Unregister our window.
UnregisterClass(WINDOW_CLASS, wc.hInstance);
return 0;
}
bool InitializeD3D(HWND hWnd, bool fullscreen)
{
D3DDISPLAYMODE displayMode;
// Create the D3D object.
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;
// Get the desktop display mode.
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
&displayMode))) return false;
// Set up the structure used to create the D3DDevice.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
if(fullscreen)
{
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = 640;
d3dpp.BackBufferHeight = 480;
}
else
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = displayMode.Format;
// Create the D3DDevice
if(FAILED