Coordinate system and basic elements (4)

Source: Internet
Author: User
Coordinate system and basic elements (4)

Drawing various basic elements

The above uses the vertex buffer to draw the Triangle List elements. We have previously introduced six basic elements: direct3d support point list, line segment list, line segment strip, Triangle List, triangle strip, and triangle slice. The following example program basicprimitive is used to show how to draw various basic elements. The sample program uses the same vertex buffer to draw different types of elements. The program uses a global variable to identify the currently drawn primitive type, by clicking "1 "~ The "6" numeric key allows you to switch between the primitive types. Click the Space key to switch between the online box mode and the object mode.

Triangle strip:

 

Triangle List:

 

Line Segment Strip:

 

Line Segment list:

 

Point list:

 

Triangle slice:

 

Entity mode:

 

Complete source code:

 

# Include <d3d9. h>

# Define primitive_triangle_strip 1
# Define primitive_triangle_list 2
# Define primitive_line_strip 3
# Define primitive_line_list 4
# Define primitive_point_list 5
# Define primitive_triangle_fan 6

# Define class_name "gameapp"

# Define release_com (p) do {If (p) {(P)-> release (); (p) = NULL ;}} while (0)

Idirect3d9 * g_d3d;
Idirect3ddevice9 * g_device;
Idirect3dvertexbuffer9 * g_vertex_buffer;

DWORD g_primitive_type = primitive_triangle_strip;
DWORD g_fill_mode = d3dfill_wireframe;

Struct scustomvertex
{
Float X, Y, Z, rhw;
DWORD color;
};

# Define d3dfvf_custom_vertex (d3dfvf_xyzrhw | d3dfvf_diffuse)

Void init_vb ()
{
Scustomvertex vertices [] =
{
{500000f, 2500000f, 0.5f, 1.0f, 0xffff0000 ,},
{1500000f, 500000f, 0.5f, 1.0f, 0xffff0000 ,},
{2500000f, 2500000f, 0.5f, 1.0f, 0xffff0000 ,},

{3500000f, 500000f, 0.5f, 1.0f, 0xffff0000 ,},
{00000000f, 2500000f, 0.5f, 1.0f, 0xffff0000 ,},
{5500000f, 500000f, 0.5f, 1.0f, 0xffff0000 ,}
};

G_device-> createvertexbuffer (sizeof (vertices), 0, d3dfvf_custom_vertex, d3dpool_default, & g_vertex_buffer, null );

Void * PTR;

G_vertex_buffer-> lock (0, sizeof (vertices), (void **) & PTR, 0 );
Memcpy (PTR, vertices, sizeof (vertices ));
G_vertex_buffer-> unlock ();
}

Bool init_d3d (hwnd)
{
G_d3d = direct3dcreate9 (d3d_sdk_version );

If (g_d3d = NULL)
Return false;

D3dpresent_parameters d3dpp;
Zeromemory (& d3dpp, sizeof (d3dpp ));

D3dpp. paiwed = true;
D3dpp. swapeffect = d3dswapeffect_discard;
D3dpp. backbufferformat = d3dfmt_unknown;

If (failed (g_d3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd, d3dcreate_software_vertexprocessing,
& D3dpp, & g_device )))
{
Return false;
}

Init_vb ();

G_device-> setrenderstate (d3drs_cullmode, d3dcull_none); // do not cull any face
G_device-> setrenderstate (d3drs_fillmode, g_fill_mode );

Return true;
}

Void cleanup ()
{
Release_com (g_vertex_buffer );
Release_com (g_device );
Release_com (g_d3d );
}

Void render ()
{
G_device-> clear (0, null, d3dclear_target, d3dcolor_xrgb (5, 5, 5), 1.0f, 0 );

G_device-> beginscene ();

G_device-> setrenderstate (d3drs_fillmode, g_fill_mode );
G_device-> setstreamsource (0, g_vertex_buffer, 0, sizeof (scustomvertex ));
G_device-> setfvf (d3dfvf_custom_vertex );

Switch (g_primitive_type)
{
Case primitive_triangle_strip:
G_device-> drawprimitive (d3dpt_trianglestrip, 0, 4 );
Break;

Case primitive_triangle_list:
G_device-> drawprimitive (d3dpt_trianglelist, 0, 2 );
Break;

Case primitive_line_strip:
G_device-> drawprimitive (d3dpt_linestrip, 0, 5 );
Break;

Case primitive_line_list:
G_device-> drawprimitive (d3dpt_linelist, 0, 3 );
Break;

Case primitive_point_list:
G_device-> drawprimitive (d3dpt_pointlist, 0, 6 );
Break;

Case primitive_triangle_fan:
G_device-> drawprimitive (d3dpt_trianglefan, 0, 4 );
Break;
}

G_device-> endscene ();

G_device-> present (null, null );
}

Lresult winapi winproc (hwnd, uint MSG, wparam, lparam)
{
Switch (MSG)
{
Case wm_keydown:
Switch (wparam)
{
Case 49: // key "1"
Case 50: // key "2"
Case 51: // key "3"
Case 52: // key "4"
Case 53: // key "5"
Case 54: // key "6"
G_primitive_type = (DWORD) wparam-48;
Break;

Case vk_space:
G_fill_mode = (g_fill_mode = d3dfill_wireframe )? D3dfill_solid: d3dfill_wireframe;
Break;

Case vk_escape:
Destroywindow (hwnd );
Break;
}
Break;

Case wm_destroy:
Postquitmessage (0 );
Return 0;
}

Return defwindowproc (hwnd, MSG, wparam, lparam );
}

Int winapi winmain (hinstance inst, hinstance, lpstr, INT)
{
Wndclassex WC;

WC. cbsize = sizeof (wndclassex );
WC. Style = cs_classdc;
WC. lpfnwndproc = winproc;
WC. cbclsextra = 0;
WC. cbwndextra = 0;
WC. hinstance = inst;
WC. hicon = NULL;
WC. hcursor = NULL;
WC. hbrbackground = NULL;
WC. lpszmenuname = NULL;
WC. lpszclassname = class_name;
WC. hiconsm = NULL;

If (! Registerclassex (& WC ))
Return-1;

Hwnd = createwindow (class_name, "direct3d app", ws_overlappedwindow, 200,100,600,300,
Null, null, WC. hinstance, null );

If (hwnd = NULL)
Return-1;

If (init_d3d (hwnd ))
{
Showwindow (hwnd, sw_showdefault );
Updatewindow (hwnd );

MSG;
Zeromemory (& MSG, sizeof (MSG ));

While (msg. message! = Wm_quit)
{
If (peekmessage (& MSG, null, 0, 0, pm_remove ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}

Render ();
}
}

Cleanup ();

Unregisterclass (class_name, WC. hinstance );

Return 0;
}

 

Coordinate system and basic elements (4)

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.