天行健 君子当自强而不息

Direct3D程序设计基础(3)

渲染图形

使用Direct3D绘制三维图形和使用GDI绘制二维图形的方法非常类似,Direct3D程序中的Direct3D设备对象相当于GDI程序中的hdc(设备描述表),使用 GDI绘制图形前,通常需要先利用hdc进行相关设置,然后通过hdc进行绘图。同样在Direct3D程序中通常先通过Direct3D设备接口进行相关的渲染设备设置,然后再渲染图形。而且所有的渲染图形操作必须在函数BeginScene()和EndScene()之间进行。

void render()
{
g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_X#2d32aa, 1.0f, 0);
	g_device->BeginScene();
	// render game scene here 
	g_device->EndScene();
	g_device->Present(NULL, NULL, NULL, NULL);
}

IDirect3DDevice9::Clear()函数的作用是清空一个或多个表面的内容。Direct3D在绘制图形前,需要使用该函数清空视口(viewport)的颜色缓冲区、深度缓冲区或者模板缓冲区。

Clears one or more surfaces such as a render target, multiple render targets, a stencil buffer, and a depth buffer.

HRESULT Clear(
DWORD Count,
CONST D3DRECT * pRects,
DWORD Flags,
D3DCOLOR Color,
float Z,
DWORD Stencil
);

Parameters

Count
[in] Number of rectangles in the array at pRects. Must be set to 0 if pRects is NULL. May not be 0 if pRects is a valid pointer.
pRects
[in] Pointer to an array of D3DRECT structures that describe the rectangles to clear. Set a rectangle to the dimensions of the rendering target to clear the entire surface. Each rectangle uses screen coordinates that correspond to points on the render target. Coordinates are clipped to the bounds of the viewport rectangle. To indicate that the entire viewport rectangle is to be cleared, set this parameter to NULL and Count to 0.
Flags
[in] Combination of one or more D3DCLEAR flags that specify the surface(s) that will be cleared.
Color
[in] Clear a render target to this ARGB color.
Z
[in] Clear the depth buffer to this new z value which ranges from 0 to 1. See remarks.
Stencil
[in] Clear the stencil buffer to this new value which ranges from 0 to 2n - 1 (n is the bit depth of the stencil buffer). See remarks.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be: D3DERR_INVALIDCALL.

Remarks

Use this method to clear a surface including: a render target, all render targets in an MRT, a stencil buffer, or a depth buffer. Flags determines how many surfaces are cleared. Use pRects to clear a subset of a surface defined by an array of rectangles.

IDirect3DDevice9::Clear will fail if you:

  • Try to clear either the depth buffer or the stencil buffer of a render target that does not have an attached depth buffer.
  • Try to clear the stencil buffer when the depth buffer does not contain stencil data.

大多数三维图形程序拥有2个或者更多颜色缓冲区。用于当前屏幕刷新的颜色缓冲区称为前台缓冲区,而将用于图形绘制的其他颜色缓冲区称为后台缓冲区。同时拥有前台缓冲区和后台缓冲区的图形程序可以同时进行图形绘制和屏幕刷新工作,系统的运行性能优于仅有单个颜色缓冲区的图形程序。在Direct3D程序中最常见的情况是有一个前台缓冲区和一个后台缓冲区。

在绘制图形开始之前,必须先调用函数IDirect3DDevice9::BeginScene()函数,通知Direct3D设备开始绘制图形(又称渲染),否则Direct3D程序就会出错返回;渲染程序完成后,也必须调用函数IDirect3DDevice9::EndScene()通知Direct3D设备结束渲染。而且这两个函数必须成对出现,不允许嵌套和交错的情况发生。任何Direct3D渲染函数都必须在函数BeginScene()和函数EndScene()的中间出现。

Direct3D在后台缓冲区进行图形绘制操作,当所有的图形绘制操作结束之后,调用函数IDirect3DDevice9::Present()将在后台缓冲区绘制的内容提交到前台显示,这样绘制的图形就显示在屏幕上了。

Presents the contents of the next buffer in the sequence of back buffers owned by the device.

HRESULT Present(
CONST RECT * pSourceRect,
CONST RECT * pDestRect,
HWND hDestWindowOverride,
CONST RGNDATA * pDirtyRegion
);

Parameters

pSourceRect
[in] Pointer to a value that must be NULL unless the swap chain was created with D3DSWAPEFFECT_COPY. pSourceRect is a pointer to a RECT structure containing the source rectangle. If NULL, the entire source surface is presented. If the rectangle exceeds the source surface, the rectangle is clipped to the source surface.
pDestRect
[in] Pointer to a value that must be NULL unless the swap chain was created with D3DSWAPEFFECT_COPY. pDestRect is a pointer to a RECT structure containing the destination rectangle, in window client coordinates. If NULL, the entire client area is filled. If the rectangle exceeds the destination client area, the rectangle is clipped to the destination client area.
hDestWindowOverride
[in] Pointer to a destination window whose client area is taken as the target for this presentation. If this value is NULL, then the hWndDeviceWindow member of D3DPRESENT_PARAMETERS is taken.
pDirtyRegion
[in] Value must be NULL unless the swap chain was created with D3DSWAPEFFECT_COPY. For more information about swap chains, see Flipping Surfaces (Direct3D 9) and D3DSWAPEFFECT. If this value is non-NULL, the contained region is expressed in back buffer coordinates. The rectangles within the region are the minimal set of pixels that need to be updated. This method takes these rectangles into account when optimizing the presentation by copying only the pixels within the region, or some suitably expanded set of rectangles. This is an aid to optimization only, and the application should not rely on the region being copied exactly. The implementation can choose to copy the whole source rectangle.

Return Values

Possible return values include: D3D_OK or D3DERR_DEVICEREMOVED (see D3DERR).

Remarks

If necessary, a stretch operation is applied to transfer the pixels within the source rectangle to the destination rectangle in the client area of the target window.

IDirect3DDevice9::Present will fail, returning D3DERR_INVALIDCALL, if called between BeginScene and EndScene pairs unless the render target is not the current render target (such as the back buffer you get from creating an additional swap chain). This is a new behavior for Direct3D 9.


posted on 2008-04-29 14:29 lovedday 阅读(1702) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论