Article on learning d3dframework (1)

Source: Internet
Author: User
The unpleasant version of directx9 (decemeber) installed two days ago conflicts with the version of direct3d game programming getting started tutorial. The direct3d framework is used in the book, which is different from the directx9 framework I installed. I heard that this version contains many directx10 things. Maybe this is the reason. I repeatedly thought about whether to use a new framework or install it back to an earlier version. I read the tutorail and documents provided by DirectX in English, which is not conducive to my quick development. If you use the original version, it may take some time to change it back. I want to finally unload the original version and install it back to the original version. Let's talk about it quickly. Well, the first example in Chapter 5th is basic.
  • Cd3dapplication class

This is the application base class in the d3d universal framework. Declare a cd3dapplication object in winmain (), and then call its run () method program to run it. The simple code is as follows:

Int winapi winmain (hinstance hinst, hinstance, lpstr, INT)
{
Cmyd3dapplication d3dapp;

Initcommoncontrols ();
If (failed (d3dapp. Create (hinst )))
Return 0;

Return d3dapp. Run ();
}

Let's take a look at the cmyd3dapplication statement:

Class cmyd3dapplication: Public cd3dapplication
{
Lpdirect3dvertexbuffer9 m_pvb; // vertice buffer pointer
DWORD m_dwsizeofvertices; // Number of vertices

Protected:
Hresult onetimesceneinit ();
Hresult initdeviceobjects ();
Hresult restoredeviceobjects ();
Hresult invalidatedeviceobjects ();
Hresult deletedeviceobjects ();
Hresult finalcleanup ();
Hresult render ();
Hresult framemove ();
Hresult confirmdevice (d3dcaps9 * pcaps, DWORD dwbehavior,
D3dformat adapterformat, d3dformat backbufferformat );

Public:
Cmyd3dapplication ();
};

The meaning of the two member variables has been commented out. It should be noted that the program uses vertex buffer to process vertices, that is, directly copying the vertices to the driver buffer that the video card can directly access, so that you do not have to copy the vertices each time, improved efficiency. The following describes how to call methods of this class:

  1. The execution sequence of program startup is: confirmdevice ()-> onetimesceneinit ()-> initdeviceobject ()-> restoredeviceobjects ().
  2. A loop is executed during the program running: framemove ()-> render ()
  3. If the window size is changed during running, the Framework calls: invalidatedeviceobjects ()-> restoredeviceobjects ();
  4. If you use F2 or the menu to change the device (Hal or ref), call: invalidatedeviceobjects ()-> deletedeviceobjects ()-> initdeviceobjects ()-> restoredeviceobjects ();
  5. Run the following command when the program exits: invalidatedeviceobjects ()-> deletedeviceobjects ()-> finalcleanup ().

The following describes the meaning of a specific function:

  1. Confirmdevice (): it is first executed to check the video card capability. If the video card is not supported, the Framework switches to the reference grating or switches to the vertex software for processing. For specific parameters, see the tutorial.
  2. Onetimesceneinit (): It is a one-time permanent initialization. It initializes things unrelated to the device and works with finalcleanup (). The initialized data can be destroyed in finalcleanup.
  3. Initdeviceobjects (): initializes a device-related object. If the device changes through F2, you need to re-execute the initialization. And deletedeviceobjects.
  4. Invalidatedeviceobjects (): responds to window size changes.
  5. Restoredeviceobjects (): Works with invalidatedeviceobjects () to reset the projection matrix and rendering status when the window size changes. Besides the thought at the beginning of the program, it will be called in pairs with invalidatedeviceobjects.
  6. Deletedeviceobjects (): destroys device resources when a device changes.
  7. Render (): it is called as the 3D rendering entry point at each frame. You can set the render state, clear buffer, and Rendering scenario.
  8. Framemove (): code that holds all animations.
  9. Finalcleanup (): destroys non-device resources such as geometric data and file objects.

OK. After learning about the execution process, let's take a look at the important steps below.

In the basic example, the program is relatively simple. It draws a quadrilateral without any coordinate transformation and projection, and uses m_d3denumeration.appusesdepthbuffer = false; it is set not to use the depth cache. An important method here is to create a vertex buffer in restoredeviceobjects:

M_pd3ddevice-> createvertexbuffer (m_dwsizeofvertices, d3dusage_writeonly, fvf, d3dpool_managed, & m_pvb, null)

Here m_dwsizeofvertices is the number of vertices and m_pvb is the buffer pointer, which is easy to understand. Fvf represents the representation of each vertex and is defined as const DWORD fvf = (d3dfvf_xyzrhw | d3dfvf_diffuse). It indicates that each vertex is composed of (X, Y, Z, rhw, diffuse, the vertex representation of direct3d includes specular, normal, texture, and other information, which correspond to different variable combinations.

D3dpool_managed is the resource manager of d3d, which is responsible for unified management of texture and geometric data. D3d Resource Manager is divided into five types:

  1. D3dpool_default: the resource is stored in the AGP or video memory. It is used when the vertex buffer or index buffer is updated frequently to avoid "too slow" copying from the memory to the video memory ". For a texture that never changes, using the default pool is a good choice.
  2. D3dpool_managed: it is a managed pool. Resources are stored in the AGP or video memory and copied in the system memory. Only Memory changes need to be copied to the video memory. If performance is not the most important, the managed pool is always the safest choice.
  3. D3dpool_systemmen: system pool, which is stored in the system memory. After a device is lost (switching in full screen or window mode or changing the resolution), the resource does not need to be rebuilt.
  4. D3dpool_scratch: a temporary pool where resources are stored in the system memory and cannot be rebuilt when the device is lost. It is used together with createoffscreenplainsurface () to create an image surface.
  5. D3dpool_force_dword:

After creating the vertex buffer, It is the lock () vertex buffer, then execute memcpy () to copy the vertex, And Then unlock (). here, lock () contains four parameters. offsettolock is the offset starting from which the vertex buffer is locked, sizetolock is the lock size, and ppbdata is the pointer to the memory buffer, the last one is to specify how to use the vertex buffer.

Finally, let's take a look at the render () method:

Hresult cmyd3dapplication: render ()
{
// Begin the scene
If (succeeded (m_pd3ddevice-> beginscene ()))
{
// In dx8: m_pd3ddevice-> setvertexshader (d3dfvf_customvertex );
// Passing an fvf to idirect3ddevice9: setfvf specifies a legacy fvf with stream 0.
M_pd3ddevice-> setfvf (fvf); // new in DX9
// Dx8: only three parameters. In DX9 the third parameter is new
M_pd3ddevice-> setstreamsource (0, m_pvb, 0, sizeof (vertex ));
M_pd3ddevice-> drawprimitive (d3dpt_trianglefan, 0, 2 );

// End the scene.
M_pd3ddevice-> endscene ();
}

Return s_ OK;
}

In the render () method, use m_pd3ddevice to initialize a scene beginscene () so that the scene is in the processing process, so that the subsequent painting will work. Otherwise, the d3derr_scene_not_in_scene error will be returned. Call endscene () to clear the mark indicating that the scene is in the processing process, refresh the cached data, and confirm that the rendering surface is intact.

Setfvf () has only one parameter, that is, the vertex structure. This method is only used for fixed pipelines. setvertexshader () must be called when shader is used ().

Setstreamsource () sets the data stream, and drawprimitive () renders the data.

Over!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.