DirectX 9.0c Game Development Journal of RPG programming self-learning Diary of the Ten--drawing with DirectX Graphics (drawing with DirectX graphic) (4th) (C)

Source: Internet
Author: User

This article by Harry _ Spider-Man original, reproduced please indicate the source! If you have any questions, please contact [email protected]

This time we continue to talk about Jim Adams old brother's RPG Programming Book Second Edition, chapter 4th, section Two: Getting down to Drawing. This super-long section of the first two talk about the more than half content, this issue we want to finish it.

Once again, the headings of the sections in this section are listed below for your reference:

1. Using Vertices ( with vertices )

2. Flexible Vertex format ( flexible vertex format )

3. Using Vertex buffers ( with vertex caching )

4. Vertex Streams ( vertex stream )

5. Vertex Shaders ( vertex shader )

6. Transformations ( transform )

7. The World Transformation ( Global Transformation )

8. The View transformation ( Perspective transform )

9. The Projection transformation ( projection transform )

10. materials and Colors ( materials and Colors )

11. clearing the Viewport ( Clear viewport )

12. Beginning and ending a scene ( start and end scenes )

13. Rendering Polygons ( render polygon )

14. presenting the scene ( show scenes )

This issue will be materials and colors the final presenting the Scene.

Original translation:

===============================================================================

2.4.10 materials and Colors ( material and color )

You've seen how to declare colors in vertex information, but when it comes to polygons, they also have specific color properties assigned to them. The color you apply to a polygon face is called a material (materials). Before using Direct3D to draw a polygon, you can choose to assign a value to the material for use (if you choose not to use a material, you can use the vertex color, if there is a vertex color).


Each material requires some color values to describe it. Using Direct3D, you define the color values of a material stored in a structure:

typedef STRUCT_D3DMATERIAL9 {  d3dcolorvalue diffuse;    Diffuse color component  d3dcolorvalue Ambient;    Ambient Color component  d3dcolorvalue specular;   Specular color component  D3dcolorvalue emisive;    Emissive color Component  float            Power;   Sharpness of specular highlights} D3DMATERIAL9;

In practice, you only have to deal with a color component: diffuse (diffuse reflection). You can set the ambient value to the same value as diffuse, and you can set specular to 0.0 or 1.0(and so on!). Specular is not a 3 - dimensional vector? (at the same time, set power to 0.0). I recommend that you try to assign different values to these color components so that you have a concept for the effects of each color component.

For the current purpose, you apply the diffuse color to a polygon surface, and the color of the material can replace the diffuse color component in the vertex structure. If you want to apply the color of a material to a polygon face that already uses a colored vertex, you will cause a change in the color of the polygon that you can observe (and usually you don't want). Therefore, it is best to either use the material or use the vertex color instead of both.


When working with material color components, you set the color component directly, rather than using a macro such as D3dcolor_rgba. But there's no need to worry-each color ingredient is replaced by its initials (R is red, G is green, B is blue, and A is Alpha), and you can assign values from 0.0 to 1.0. If you want to create a material to use yellow, you can build the material structure as follows:

D3DMATERIAL9 D3DM; Clear out Thematerial structurezeromemory (&d3dm,sizeof (D3DMATERIAL9)); Fill diffuse andambient to Yellow COLORD3DM. DIFFUSE.R =D3DM. AMBIENT.R = 1.0f;  REDD3DM. Diffuse.g =D3DM. AMBIENT.G = 1.0f;  GREEND3DM. Diffuse.b =D3DM. ambient.b = 0.0f;  BLUED3DM. Diffuse.a =D3DM. AMBIENT.A = 1.0f;  Alpha

How you build the material structure is your choice, but once the structure is set up, you need to tell Direct3D to use the material before rendering a polygon. This is the work of the Idirect3ddevice9::setmaterial function, which takes only a pointer to your material structure as a parameter:

Idirect3ddevice9::setmaterial (Constd3dmaterial9 *pmaterial);

Once called, all polygons that are rendered later use the material's settings. Here is an example of a previously defined yellow material:

G_pd3ddevice->setmaterial (&D3DM);


2.4.11 Clearing the Viewport ( Clear viewport )

You need to clean up the back buffer to prepare for drawing, so you can erase the graphics that might be there. With the Idirect3ddevice9::clear function, this is a simple routine:

Hresultidirect3ddevice9::clear (  DWORD           Count,    //0  CONST d3drect *prects,    //NULL  DWORD           Flags,    //d3dclear_target  d3dcolor        color,    //color to clear  to float               Z,    // 1.0f  DWORD        stencil);    0

The only parameter to worry about at this stage is color, which is the one you want to clean up the back buffer into. The color values you want to use can be constructed using a typical D3dcolor_rgba or D3dcolor_colrvalue macro that you have been born with. Say you want to clean the back buffer into light blue:

g_pd3ddevice =pre-initialized device objectif (FAILED (g_pd3ddevice->clear, 0,null,                                   D3dcolor_rgba (0, 0, 192, 255), 1.0f, 0)) {    //Error occurred}

Attention

===============================================================================

The d3dclear_target tag used in the example tells Direct3D that you want to clean up the current target (screen or window). There are some useful cleanup tags to use, one of which you will soon read.

===============================================================================

2.4.12 Beginningand ending a scene ( start and end a scenario )

Before you can render anything, you must tell Direct3D to prepare yourself. This is the target of the Idirect3ddevice9::beginscene function (it does not take arguments):

Hresultidirect3ddevice9::beginscene ();

After you have finished rendering the scene, you need to tell Direct3D to use the EndScene function:

Hresultidirect3ddevice9::endscene ();

You do not need to insert the call of the clear function between BeginScene and endscene; You can call the clear function before you call the BeginScene function. The only thing that needs to be inserted between the start scene and the call to end the scene function is the function that renders the polygon.

2.4.13 renderingpolygons ( render polygon )

After a long ordeal, you're going to start rendering the polygon! The typical framework of your game engine cleans the back buffer, starts the scene, sets the material to use, draws the polygon, and ends the scene. You've seen how to do all of these things except to draw the actual polygon.

You use the following function to draw a Idirect3dvertexbuffer9 object (of course, after calling the function that sets the vertex source and the FVF shader):

Hresultidirect3ddevice9::D rawprimitive (  d3dprimitivetype  primitivetype,      //primitives to draw  UINT              Startvertex,        //Vertex to start with (0)  uint              primitivecount);    # of Primitives to draw

The first parameter, PrimitiveType, tells Direct3D what type of polygon to draw (it can be one of the columns in Table 2.4). The parameter Startvertex lets you decide which vertex to start drawing from (typically 0). You assign the total number of entities (primitives) you want to draw to the parameter primitivecount.

Table 2.4 primitive types of drawprimitive

Type

Describe

D3dpt_pointlish

Draws all the vertices in pixels.

D3dpt_linelist

Draw a column of orphaned lines, each of which uses two vertices.

D3dpt_linestrip

Draw a list of interconnected lines.

D3dpt_trianglelist

Draw a few polygons that use three vertices.

D3dpt_trianglestrip

Draw a polygon band, the first polygon uses three vertices, and each subsequent polygon uses an extra vertex.

D3dpt_trianglefan

Draw some polygons with a fan, and the first vertex as the handle (all the polygons are connected to it).

The type of primitive you use depends on how you populate the vertex data into the vertex cache. If you use 3 vertices for each polygon, you use the d3dpt_trianglelist type. If you use more efficient types, such as triangle bands, then use the D3dpt_trianglestrip type.

The only thing to remember now is that you have to use the Idirect3ddevice9::beginscene function to start the scene before rendering the polygon; otherwise, the call to the DrawPrimitive function fails.

If you have created a vertex cache with 6 vertices, it constructs two triangles and forms a square. The steps to render them (and, of course, the BeginScene and EndScene functions, as well as the vertex source and FVF shader) look like this:

g_pd3ddevice =pre-initialized device object//pd3dvb =pre-initialized vertex buffer//Svertex =pre-constructed vertex s tructure//VERTEXFVF =pre-constructed FVF Descriptor//Set the Vertexstream and Shaderg_pd3ddevice->setstreamsource ( 0,PD3DVB, 0, sizeof (Svertex)); G_PD3DDEVICE->SETFVF (VERTEXFVF); if (SUCCEEDED (G_pd3ddevice->beginscene ())) {   //Render the polygons if (FAILED (G_pd3ddevice->drawprimitive ( D3dpt_trianglelist,                                                         0, 2)) {    //Error occurred  

2.4.14 presentingthe scene ( show scenes )

After a long ordeal (at long)(how is this sentence?) ), you can prepare to flip the back buffer to the viewport, and then show the user your graphics, and you do this through the following functions:

Hresultidirect3ddevice9::P Resent (  const rect   *psourcerect,  const rect   *pdestrect,  HWND          Hdestwindowoverride,  CONST rgndata *pdirtyregion);

You can safely set IDirect3DDevice9: All parameters of the:P resent function to null, which tells Direct3D to update the entire display (because the function can show only a portion at a time), as shown in the following code:

g_pd3ddevice =pre-initialized device objectif (FAILED (g_pd3ddevice->present (null,null, NULL, NULL))) {    // Error occurred}

That's it! In order to create more realistic scenes, you use a number of different vertex caches to draw a variety of mid-range objects in your world. Another way to improve the authenticity of your graphics is to use texture mapping.

===============================================================================

Well, we finally got this super-long 2nd Chapter 4th! I hope you can understand. If you do not understand the matter, you can refer to the "Dragon Book" second edition of the 4th and 6th chapters of the content, there is more detailed, it is not difficult to understand (but there will be some notable differences).

DirectX 9.0c Game Development Journal of RPG programming self-learning Diary of the Ten--drawing with DirectX Graphics (drawing with DirectX graphic) (4th) (C)

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.