易码技术论坛

 找回密码
 加入易码
搜索
查看: 412|回复: 5

[D3D9指导]01_创建设备

[复制链接]
发表于 2009-9-21 14:41:33 | 显示全部楼层 |阅读模式
这份文档(姑且称之为文档)是我翻译微软DirectX SDK开发文档而来。
我使用的DirectX SDK版本是 2009.8月份的版本。
ps:我英文水平不够,中文水平也不咋地。。。。欢迎英文高手指导~


Tutorial 1: Creating a Device
指导1:创建设备

To use Direct3D, you first create an application window, then you create and initialize Direct3D objects. You use the component object model (COM) interfaces that these objects implement to manipulate them and to create other objects required to render a scene. The CreateDevice sample project on which this tutorial is based illustrates these tasks by creating a Direct3D device and rendering a blue screen.
This tutorial uses the following steps to initialize Direct3D, render a scene, and eventually shut down.

要使用D3D,你首先要创建一个应用程序窗口,然后创建和初始化D3D对象。你得使用组件对象模型(COM)
接口,这些接口实现D3D的各种操作,并且为D3D创建其他对象来渲染场景(scene)。这个示例说明这些内容通过创建一个D3D设备并且渲染一个蓝色的屏幕。
这份指导使用下面的步骤来初始化D3D、渲染场景最终关闭D3D。


Steps
Step 1 - Creating a Window 创建窗口
Step 2 - Initializing Direct3D 初始化D3D
Step 3 - Handling System Messages 处理系统消息
Step 4 - Rendering and Displaying a Scene 渲染、显示场景
Step 5 - Shutting Down 关闭D3D


[ 本帖最后由 Alanwywy 于 2009-9-21 14:45 编辑 ]

[ 本帖最后由 Alanwywy 于 2009-9-21 19:53 编辑 ]
 楼主| 发表于 2009-9-21 14:50:28 | 显示全部楼层
Step 1 - Creating a Window
第一步 创建窗口

The first thing any Windows application must do when it is run is to create an application window to display to the user. To do this, the CreateDevice sample project begins execution at its WinMain function. The following sample code performs window initialization.
让窗口应用程序跑起来第一件必须做的事情就是创建一个应用程序窗口来显示给用户。要做这些,CreateDevice示例项目开始于WinMain函数。后面的代码演示里一个 窗口 的初始化。
  1. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  2. {
  3.         // Register the window class.
  4.         WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
  5.         GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  6.         "Direct3D Tutorial", NULL };

  7.         RegisterClassEx( &wc );

  8.         // Create the application's window.
  9.         HWND hWnd = CreateWindow( "Direct3D Tutorial", "Direct3D Tutorial 01: CreateDevice",
  10.         WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
  11.         GetDesktopWindow(), NULL, wc.hInstance, NULL );
复制代码
The preceding code sample is standard Windows programming. The sample starts by defining and registering a window class called "Direct3D Tutorial." After the class is registered, the sample code creates a basic top-level window that uses the registered class, with a client area of 300 pixels wide by 300 pixels tall. This window has no menu or child windows. The sample uses the WS_OVERLAPPEDWINDOW window style to create a window that includes Minimize, Maximize, and Close buttons common to windowed applications. (If the sample were to run in full-screen mode, the preferred window style is WS_EX_TOPMOST, which specifies that the created window should be placed above all non-topmost windows and should stay above them, even when the window is deactivated.) When the window is created, the code sample calls standard Win32 functions to display and update the window.

前面的示例代码是一个标准的视窗程序。这个例子开始于定义和注册一个名叫"Direct3D Tutorial"的窗口类。当这个类被注册,示例代码创建一个基本的顶层窗口,这个窗口的客户区域高300像素,宽300像素。这个窗口没有菜单也没有子窗口。示例程序的窗口使用WS_OVERLAPPEDWINDOW风格创建窗口,所以又最小化、最大化以及关闭按钮。(如果示例程序使用全屏模式运行,首选的窗口风格是WS_EX_TOPMOST,WS_EX_TOPMOST风格规定创建的窗口应该在所有非顶层窗口之前,即使窗口被停用。)一旦窗口被创建,示例代码调用标准的Win32函数来显示和更新窗口。

With the application window ready, you can begin setting up the essential Direct3D objects, as described in Step 2 - Initializing Direct3D.

准备好了这些,你可以开始设置D3D对象,请收看第二步,初始化D3D。

[ 本帖最后由 Alanwywy 于 2009-9-21 15:02 编辑 ]
 楼主| 发表于 2009-9-21 15:09:06 | 显示全部楼层
Step 2 - Initializing Direct3D
第二步 初始化D3D

The CreateDevice sample project performs Direct3D initialization in the InitD3D application-defined function called from WinMain after the window is created. After you create an application window, you are ready to initialize the Direct3D object that you will use to render the scene. This process includes creating the object, setting the presentation parameters, and finally creating the Direct3D device.

CreateDevice示例工程在InitD3D函数里演示D3D的初始化。当窗口被创建,InitD3D函数被WinMain函数调用。当你创建完一个应用程序窗口,你就可以开始初始化你将用来渲染场景的D3D对象了。这个初始化的过程包括创建D3D对象(object)、设置这些演示参数以及最终创建D3D设备(Direct3D device)。

After creating a Direct3D object, use the IDirect3D9::CreateDevice method to create a device, and to enumerate devices, types, modes, and so on.

创建好D3D对象后,使用IDirect3D9::CreateDevice 方法来创建一个D3D设备,并列举设备,类型,模式,等等。
  1. //创建D3D9对象(就是D3D对象)
  2. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
  3. return E_FAIL;
复制代码
The only parameter passed to Direct3DCreate9 should always be D3D_SDK_VERSION. This informs Direct3D that the correct header files are being used. This value is incremented whenever a header or other change would require applications to be rebuilt. If the version does not match, Direct3DCreate9 will fail.

传给Direct3DCreate9函数的唯一的变量始终应该是D3D_SDK_VERSION。它告诉D3D正确的头文件正在使用。当某个头文件被修改或者其他修改会引起应用程序重新编译,这个值会被增加。如果版本不匹配,Direct3DCreate9会执行失败。

By filling in the fields of D3DPRESENT_PARAMETERS you can specify how you want your 3D application to behave. The CreateDevice sample project sets Windowed to TRUE, SwapEffect to D3DSWAPEFFECT_DISCARD, and BackBufferFormat to D3DFMT_UNKNOWN.
通过填写 D3DPRESENT_PARAMETERS 结构,你可以指定你希望的3D应用程序的行为。CreateDevice示例工程设置 Windowed 为 TRUE、SwapEffect 为 D3DSWAPEFFECT_DISCARD、 BackBufferFormat 为 D3DFMT_UNKNOWN。如后面的代码所示。
  1. D3DPRESENT_PARAMETERS d3dpp;
  2. ZeroMemory( &d3dpp, sizeof(d3dpp) );
  3. d3dpp.Windowed = TRUE;
  4. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  5. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
复制代码
The final step is to use the IDirect3D9::CreateDevice method to create the Direct3D device, as illustrated in the following code example.
最后一步是使用 IDirect3D9::CreateDevice 方法D3D设备就像后面示范的代码。
  1. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  2.                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  3.                                   &d3dpp, &g_pd3dDevice ) ) )
复制代码
The preceding code sample creates the device with the default adapter by using the D3DADAPTER_DEFAULT flag. In most cases, the system will have only a single adapter, unless it has multiple graphics hardware cards installed. Indicate that you prefer a hardware device over a software device by specifying D3DDEVTYPE_HAL for the DeviceType parameter. This code sample uses D3DCREATE_SOFTWARE_VERTEXPROCESSING to tell the system to use software vertex processing. Note that if you tell the system to use hardware vertex processing by specifying D3DCREATE_HARDWARE_VERTEXPROCESSING, you will see a significant performance gain on video cards that support hardware vertex processing.
前面的代码根据 D3DADAPTER_DEFAULT 标志创建了这个D3D设备,并且使用默认的适配器(adapter)。在大多数情况下,系统将只有一个适配器,除非他装了多显卡。如果你更喜欢硬件设备,将参数 DeviceType 设置为 D3DDEVTYPE_HAL。示例代码中使用 D3DCREATE_SOFTWARE_VERTEXPROCESSING 来告诉系统使用软件顶点处理。请注意,如果你告诉系统 D3DCREATE_HARDWARE_VERTEXPROCESSING 将使用指定硬件顶点处理,你会看到一个视频卡上显著的性能增益支持硬件顶点处理(?)。

Now that the Direct3D object has been initialized, the next step is to ensure that you have a mechanism to process system messages, as described in Step 3 - Handling System Messages.
D3D对象已经初始化好了,下一步~

[ 本帖最后由 Alanwywy 于 2009-9-21 15:33 编辑 ]
 楼主| 发表于 2009-9-21 15:45:50 | 显示全部楼层
Step 3 - Handling System Messages
第三步 处理系统消息


After you have created the application window and initialized Direct3D, you are ready to render the scene. In most cases, Windows applications monitor system messages in their message loop, and they render frames whenever no messages are in the queue. However, the CreateDevice sample project waits until a WM_PAINT message is in the queue, telling the application that it needs to redraw all or part of its window.
第一第二步完成后,你已经可以开始渲染场景了。大多数情况下,Windows应用程序监测其消息循环的系统信息,渲染者(render)并不处理消息。示例工程等待消息知道 WM_PAINT 消息出现在消息队列,告诉应用程序需要重绘全部或部分窗口。
  1. // The message loop. 消息循环
  2. MSG msg;
  3. while( GetMessage( &msg, NULL, 0, 0 ) )
  4. {
  5.     TranslateMessage( &msg );
  6.     DispatchMessage( &msg );
  7. }
复制代码
Each time the loop runs, DispatchMessage calls MsgProc, which handles messages in the queue. When WM_PAINT is queued, the application calls Render, the application-defined function that will redraw the window. Then the Win32 function ValidateRect is called to validate the entire client area.
每次循环, DispatchMessage 函数调用 MsgProc 函数,MsgProc函数处理消息队列中的消息。当WM_PAINT 进入消息队列,应用程序调用自定义的 Render 函数,窗口将被重绘。Win32函数 ValidateRect  用来设置有效的区域为整个客户区。

The sample code for the message-handling function is shown below.
示例代码如下:
  1. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  2. {
  3.     switch( msg )
  4.     {
  5.         case WM_DESTROY:
  6.             PostQuitMessage( 0 );
  7.             return 0;

  8.         case WM_PAINT:
  9.             Render();
  10.             ValidateRect( hWnd, NULL );
  11.             return 0;
  12.     }

  13.     return DefWindowProc( hWnd, msg, wParam, lParam );
  14. }
复制代码
Now that the application handles system messages, the next step is to render the display, as described in Step 4 - Rendering and Displaying a Scene.
现在应用程序处理了系统消息,下一步是渲染显示啦。
 楼主| 发表于 2009-9-21 15:48:56 | 显示全部楼层
Step 4 - Rendering and Displaying a Scene
第四步 渲染、显示场景

To render and display the scene, the sample code in this step clears the back buffer to a blue color, transfers the contents of the back buffer to the front buffer, and presents the front buffer to the screen.
为了渲染和显示场景,这段示例代码在这一步中清除了背景缓存(back buffer),并填充上了蓝色,将背景缓存转换到前景缓存(front buffer),将前景缓存刷新到屏幕上。

To clear a scene, call the IDirect3DDevice9::Clear method.
要清空场景,调用 IDirect3DDevice9::Clear 方法。
  1. // Clear the back buffer to a blue color
  2. g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
复制代码
The first two parameters accepted by IDirect3DDevice9::Clear inform Direct3D of the size and address of the array of rectangles to be cleared. The array of rectangles describes the areas on the render-target surface to be cleared.

前两个参数

[ 本帖最后由 Alanwywy 于 2009-9-21 19:48 编辑 ]
 楼主| 发表于 2009-9-21 15:49:19 | 显示全部楼层
Step 5 - Shutting Down
第五步 关闭它
您需要登录后才可以回帖 登录 | 加入易码

本版积分规则

Archiver|手机版|小黑屋|EMAX Studio

GMT+8, 2022-1-18 10:13 , Processed in 0.017987 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表