DirectX Graphical Interface Guide (2)

zhaozj2021-02-08  264

Guide 1: Create a device

In order to use Microsoft Direct3D, you first need to create an application window and then create and initialize the Direct3D object. You should use the COM interface provided by these objects to manipulate them, and create other objects that depict one scenario. The CreateDevice example included in this guide will illustrate and describe the following work: Create a Direct3D device and draw a simple blue screen.

This guide uses the following steps: initialize Direct3D, draw a scene, and final cleaning and closing.

Step 1: Create a window

Step 2: Initialize Direct3D

· Step 3: Handling system messages

Step 4: Draw and display the scene

· Step 5: Close and Clear

Note: The path to the CreateDevice sample program is:

(SDK root) / Samples / Multimedia / Direct3D / Tutorials / TUT01_CREATEDEVICE.

Step 1: Creating a window All Microsoft Windows programs must be made in the execution of the Microsoft Windows program is to create an application window and display it to the user. To do this, the CreateDevice routine will first implement its WinMain function. The following sample code completes the initialization of the window.

Int WinApi WinMain (Hinstance Hinst, Hinstance, LPSTR, INT)

{

// register the window class.

WNDCLASSEX WC = {SizeOf (WNDCLASSEX), CS_CLASSDC, MSGPROC, 0L, 0L,

GetModuleHandle (NULL), NULL, NULL, NULL, NULL,

"D3D Tutorial", NULL};

RegisterClassex (& WC);

// CREATE The Application's Window.

HWND HWND = CREATEWINDOW ("D3D Tutorial", "D3D Tutorial 01: CreateDevice",

WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,

GetDesktopWindow (), NULL, WC.HINSTANCE, NULL);

The aforementioned sample code is a standard Windows programming. The example starts and registers a window class name "D3D Tutorial". After class registration, sample code creates a fund-level window with a registered class, with 300 pixels wide, 300 pixels wide. This window does not have a menu or sub-window. The example uses the WS_OVERLAPPEDWINDOW property to create a normal window including maximizing, minimizing, and closing buttons. (If the routine will run in full screen mode, the preferred window property should be WS_EX_TOPMOST, which specifies the created window to be placed and kept before all non-TopMost windows, even in the event of windows. Once the window is created, the code calls the standard Microsoft Win32 function display and update the window.

After the application window is ready, you can start setting the specific Microsoft Direct3D object,

See: Step 2: Initializing Direct3D

Step 2: Initialize Direct3D

CreateDevice Example After creating a window in WinMain, call the program defined function initd3d to complete the Microsoft Direct3D initialization process. After the window is created, the program is ready to initialize the Direct3D object you will use to draw the scene. This process includes creating a Direct3D object, setting present parameters, and finalizing Direct3D devices. After you have created the Direct3D object, you can create a Direct3D device with the iDirect3d8 :: createDevice method immediately. You can also enumerate your device, type, mode, and other things using Direct3D objects. The code segment of these work should be located after using the Direct3DCReate8 function to create a Direct3D object.

IF (null == (g_pd3d = direct3dcreate8 (d3d_sdk_version))))))))

Return E_FAIL;

The unique parameter passed to Direct3DCreate8 should always be D3D_SDK_Version, which tells Direct3D currently used header file information. In any case, the header or other changes will result in this value to increase and force the application of this value to recompile. If this version does not match, call Direct3DCREATE8 will fail.

The next step is to find the current display mode using the iDirect3d8 :: getadapterdisplaymode interface, the code is as follows:

D3DDISPLAYMODE D3DDM;

IF (failed (g_pd3d-> getadapterdisplaymode (d3dadapter_default, & d3ddm)))))

Return E_FAIL;

The Format variable in the D3DDisplayMode structure will be used to create a Direct3D device. If it is running in window mode, the Format parameter is usually used to create a back buffer that matches the current mode of the adapter.

When assigning parameters to D3DPresent_Parameters, you must specify how your application works in 3D. This CreateDevice routine Sets the D3DPresent_Parameters structure Windowed to true, swapect is d3dswapeffect_discard, backbufferformat is D3DDM.FORMAT.

D3DPresent_Parameters D3DPP;

ZeromeMory (& D3DPP, SIZEOF (D3DPP));

D3DPP.Windowed = true;

D3dpp.swapeffect = D3DSWAPEFFECT_DISCARD;

D3dpp.backbufferformat = D3DDM.FORMAT;

The last step is to create a Direct3D device using the iDirect3d8 :: createDevice function, the code is as follows:

IF (Failed (g_pd3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd,

D3DCREATE_SOFTWARE_VERTEXPROCESSING,

& D3DPP, & g_pd3ddevice)))))))))))))

The aforementioned code creates a device using the provisions deficiencies using the D3DADAPTER_DEFAULT flag. In a very large number, there is only one adapter unless it has a plurality of graphics acceleration cards. By setting the DeviceType parameter to D3DDEVTYPE_HAL, you want to get an actual hardware device instead of software virtual devices (Software Device). The sample code also uses the D3DCREATE_SOFTWARE_VERTEXPROCESSING flag notification system to use software vertex processing. Note that if you specify the D3DCREATE_HARDWARE_VERTEXPROCESSING flag notification system Using Hardware Vertex Process, you can get a large performance increase on the graphics acceleration card that supports the hardware vertex. Now Direct3D has been initialized, the next step is to make sure your program has a mechanism to handle system messages.

See below: Step 3: Handling System Messages

Step 3: Handling system messages

Complete the creation program window and initialize Direct3D, you are ready to draw a scene (Render Scene). In most cases, the Microsoft Windows program monitors the system message in their message loops, and draws the screen frame when there is no message in the queue. However, the CreateDevice routine notifies all parts of the window when it is waited until a WM_Paint appears in the queue.

// The message loop.

MSG msg;

While (GetMessage (& MSG, NULL, 0, 0))

{

TranslateMessage (& MSG);

DispatchMessage (& MSG);

}

When each cycle, DispatchMessage calls MsgProc, the latter is responsible for handling the message in the queue, calling the program's own defined function render () when the WM_Paint message is entered the team, which will be responsible for redrawing the window. The Microsoft Win32 function validaterect is then executed and sets the entire customer area to be valid.

The example code of the message processing function is as follows:

LResult WinApi MsgProc (HWND HWND, UINT MSG, WPARAM WPARAM, LPARAM LPARAM)

{

Switch (msg)

{

Case WM_DESTROY:

PostquitMessage (0);

Return 0;

Case WM_Paint:

Render ();

ValidateERECT (HWND, NULL);

Return 0;

}

Return DefWindowProc (HWND, MSG, WPARAM, LPARAM);

}

Now, the application processes the system message, the next step is to draw the display, see: Step 4: Draw and display the scene

Step 4: Draw and display the scene

In order to depict and display the required scenarios, this routine fills the back buffer in this step, then pass the contents of the back buffer to the foreground buffer and submit the foreground buffer to the screen. .

Clear the surface, call the idirect3ddevice8 :: CLEAR function:

// Clear THE Back Buffer to a blue color

g_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_xrgb (0, 0, 255), 1.0F, 0);

The first two parameters accepted notified Microsoft Direct 3D The base and size of the rectangular area array cleared, which describes the area that needs to be cleared in the render target surface. In most cases, only a single rectangle covers the entire drawing target surface. This way you only need to set the first parameter of 0 and the second parameter as NULL. The third parameter will determine the behavior of the method, you can use a specific flag to clear the render Target Surface, the associated Z buffer, the stencil buffer, and any of these three Mixing of people. This guide does not use Z buffer, so only the D3DCLEAR_TARGET flag is used. The last three parameters are used to set the corresponding drawing target surface, the Z buffer, and the reflection value of the template buffer. The CreateDevice routine sets the clear pad color of the drawing destination to blue (D3DCOLOR_XRGB (0, 0, 255). Because the corresponding flag is not set, the last two parameters are ignored by CLEAR ().

After clearing the viewport, the CreateDevice routine tells the Direct3D drawing will start, then immediately notify this draw complete, see the following code segment:

// begin the scene.

g_pd3ddevice-> beginscene ();

// rendering of screne objects happens here.

// end the scene.

g_pd3ddevice-> endscene ();

When drawing start or completion, iDirect3ddevice8 :: beginscene and iDirect3ddevice8 :: EndScene functions will use the signal notification system. You can only call other drawing functions between these two functions. Even if the calling function fails, you should call EndScene before reinterping Beginscene.

After drawing, call IDirect3dDevice8 :: Present to display the scenario:

g_pd3ddevice-> present (null, null, null, null);

The first two parameters accepted are raw rectangles and target rectangles. In this step, the routine settings these two parameters are NULL and submit the entire back buffer to the foreground buffer. The third parameter is used to set the target window of the submit. Because this parameter is set to NULL, the actual window is the HWndDeviceWindow member of D3DPresent_Parameters. The fourth is the DirtyRegion parameters, which should be set to NULL in most cases.

The final step of this guide is to close the app, see: Step 5: Close and Clear

Step 5: Close and clear

At a number of times the execution, your application must be shut down immediately. Turning off a Direct3D application does not mean you have to destroy the program window, and you also want to release any Direct3D objects used in the program and invalidate their pointers. When a WM_DESTROY message is received, the CreateDevice routine handles these work by calling a local defined function cleanup ().

Void cleanup ()

{

IF (g_pd3ddevice! = NULL)

g_pd3ddevice-> release ();

IF (g_pd3d! = null)

g_pd3d-> release ();

}

The above functions call the IUNKNOWN: Release method to each object to release them itself. Since DirectX follows COM rules, most objects are automatically released from memory when their reference count falls to 0. For other closing programs, it may happen in the usual execution of the program - such as the user changes the parameters or color depth of the desktop - you might need to revoke and rebuild the Microsoft Direct3D object in use. So a good idea is to put your release code together so that you can call it at any time when you need it.

This guide has explained how to create a device, guide 2: Render Vertex, will tell you how to create a geometry in the vertex (Vertex). (Guide 2: See the third part of this article ...)

转载请注明原文地址:https://www.9cbs.com/read-2861.html

New Post(0)