# Include <d3d9. h> // header files not required by direct3d
# Include <d3dx9. h> // This header file is required if you want to use the d3dx library.
# Include <mmsystem. h>
# Include <windows. h>
# Pragma comment (Lib, "d3d9. lib ")
# Pragma comment (Lib, "d3dx9. lib ")
# Pragma comment (Lib, "winmm. lib ")
// Four global variables used in this program
Lpdirect3d9 g_pd3d = NULL; // direct3d pointer
Lpdirect3ddevice9 g_pd3ddevice = NULL; // direct3d Device
Lpdirect3dvertexbuffer9 g_pvb = NULL; // vertex buffer pointer
Lpdirect3dindexbuffer9 g_pib = NULL; // index buffer pointer
// Define the vertex structure used
Struct customvertex
{
Float x, y, z;
DWORD color;
};
// Define the fvf structure of the above Vertex
# Define d3dfvf_customvertex (d3dfvf_xyz | d3dfvf_diffuse)
// Initialize the direct3d device process function
Hresult initd3d (hwnd)
{
// Create a direct3d object
If (null = (g_pd3d = direct3dcreate9 (d3d_sdk_version )))
{
Return e_fail;
}
// Enter the structure for creating the direct3d Device
D3dpresent_parameters d3dpp;
Zeromemory (& d3dpp, sizeof (d3dpp ));
D3dpp. cmdwed = false; // run in window mode
D3dpp. swapeffect = d3dswapeffect_discard; // the most efficient way to work
D3dpp. backbufferformat = d3dfmt_unknown; // pixel format of screen buffer data
// Create a direct3d Device
If (failed (g_pd3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd,
D3dcreate_software_vertexprocessing, & d3dpp, & g_pd3ddevice )))
{
Return e_fail;
}
Return s_ OK;
}
// Initialize the data functions related to the plot of the program
Hresult initdraw ()
{
// Create a vertex buffer
G_pd3ddevice-> createvertexbuffer (8 * sizeof (customvertex ),
D3dusage_writeonly,
D3dfvf_customvertex,
D3dpool_managed,
& G_pvb,
0 );
// Create an index Buffer
G_pd3ddevice-> createindexbuffer (36 * sizeof (Word ),
D3dusage_writeonly,
D3dfmt_index16,
D3dpool_managed,
& G_pib,
0 );
// Create eight vertices of the cube. Note that the color of each vertex is different.
// During painting, direct3d interpolation is performed on the pixels inside the triangle Based on the vertex color, so the cube looks colored.
Customvertex source_vertices [] = {
{-1.0f,-1.0f,-1.0f, d3dcolor_xrgb (255, 0 )},
{-1.0f, 1.0f,-1.0f, d3dcolor_xrgb (0,255, 0 )},
{1.0f, 1.0f,-1.0f, d3dcolor_xrgb (255 )},
{1.0f,-1.0f,-1.0f, d3dcolor_xrgb (255,255, 0 )},
{-1.0f,-1.0f, 1.0f, d3dcolor_xrgb (255,0, 255 )},
{-1.0f, 1.0f, 1.0f, d3dcolor_xrgb (0,255,255 )},
{1.0f, 1.0f, 1.0f, d3dcolor_xrgb (0, 0 )},
{1.0f,-1.0f, 1.0f, d3dcolor_xrgb (255,255,255 )},
};
// Copy the preceding vertex data to the created buffer.
Customvertex * pvertices;
If (failed (g_pvb-> lock (0, 8 * sizeof (customvertex), (void **) & pvertices, 0 )))
{
Return e_fail;
}
Memcpy (pvertices, source_vertices, 8 * sizeof (customvertex ));
G_pvb-> unlock ();
// Define the content of the index buffer, which can be manually defined here, or set with arrays, and then copy the content, which is the same as the top vertex buffer.
Word * indices = 0;
G_pib-> lock (0, 0, (void **) & indices, 0 );
// Front
Indices [0] = 0; indices [1] = 1; indices [2] = 2;
Indices [3] = 0; indices [4] = 2; indices [5] = 3;
// Back
Indices [6] = 4; indices [7] = 6; indices [8] = 5;
Indices [9] = 4; indices [10] = 7; indices [11] = 6;
// Left
Indices [12] = 4; indices [13] = 5; indices [14] = 1;
Indices [15] = 4; indices [16] = 1; indices [17] = 0;
// Right
Indices [18] = 3; indices [19] = 2; indices [20] = 6;
Indices [21] = 3; indices [22] = 6; indices [23] = 7;
// Top surface
Indices [24] = 1; indices [25] = 5; indices [26] = 6;
Indices [27] = 1; indices [28] = 6; indices [29] = 2;
// Bottom
Indices [30] = 4; indices [31] = 0; indices [32] = 3;
Indices [33] = 4; indices [34] = 3; indices [35] = 7;
G_pib-> unlock ();
// Set the camera
D3dxvector3 position (0.0f, 0.0f,-3.0f );
D3dxvector3 target (0.0f, 0.0f, 0.0f );
D3dxvector3 up (0.0f, 1.0f, 0.0f );
D3dxmatrix V;
D3dxmatrixlookatlh (& V, & position, & target, & UP );
G_pd3ddevice-> settransform (d3dts_view, & V );
// Sets the projection matrix.
D3dxmatrix proj;
D3dxmatrixperspectivefovlh (& proj,
D3dx_pi * 0.5f,
(Float) 800/(float) 600,
1.0f,
1000.0f );
G_pd3ddevice-> settransform (d3dts_projection, & proj );
// Because no material or texture information is used, turn off the light to display its own color.
G_pd3ddevice-> setrenderstate (d3drs_lighting, false );
Return s_ OK;
}
Void cleanup ()
{
If (g_pvb! = NULL)
{
G_pvb-> release ();
}
If (g_pib! = NULL)
{
G_pib-> release ();
}
If (g_pd3ddevice! = NULL)
{
G_pd3ddevice-> release ();
}
If (g_pd3d! = NULL)
{
G_pd3d-> release ();
}
}
// Rendering Function
Void render ()
{
// Clear the screen buffer to the Blue Screen
G_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_xrgb (0, 0, 255), 1.0f, 0 );
// Start drawing
If (succeeded (g_pd3ddevice-> beginscene ()))
{
// Create a matrix rendered along the three axes
D3dxmatrixa16 matworld_x;
D3dxmatrixidentity (& matworld_x );
D3dxmatrixrotationx (& matworld_x, timegettime ()/500366f );
D3dxmatrixa16 matworld_y;
D3dxmatrixidentity (& matworld_y );
D3dxmatrixrotationy (& matworld_y, timegettime ()/500366f );
D3dxmatrixa16 matworld_z;
D3dxmatrixidentity (& matworld_z );
D3dxmatrixrotationz (& matworld_z, timegettime ()/500366f );
// Sets the world matrix of an object.
G_pd3ddevice-> settransform (d3dts_world, & (matworld_x * matworld_y * matworld_z ));
// Hook up the rendering pipeline, set the fvf value, set the index, and render
G_pd3ddevice-> setstreamsource (0, g_pvb, 0, sizeof (customvertex ));
G_pd3ddevice-> setfvf (d3dfvf_customvertex );
G_pd3ddevice-> setindices (g_pib );
G_pd3ddevice-> drawindexedprimitive (d3dpt_trianglelist, 0, 0, 8, 0, 12 );
// End the painting
G_pd3ddevice-> endscene ();
}
// Display to the screen
G_pd3ddevice-> present (null, null );
}
// Message callback function. Only the Exit message is processed, so the mouse is always busy.
Lresult winapi msgproc (hwnd, uint MSG, wparam, lparam)
{
Switch (MSG)
{
Case wm_destroy:
Cleanup ();
Postquitmessage (0 );
Return 0;
}
Return defwindowproc (hwnd, MSG, wparam, lparam );
}
Int winapi winmain (hinstance hinst, hinstance, lpstr, INT)
{
// Registration window
Wndclassex WC = {sizeof (wndclassex), cs_classdc, msgproc, 0l, 0l,
Getmodulehandle (null), null, "d3d tutorical", null
};
Registerclassex (& WC );
// Create a window
Hwnd = createwindow ("d3d tutorical ",
"3D cube ",
Ws_overlappedwindow,
100,
100,
800,
600,
Getasktopwindow (),
Null,
WC. hinstance,
Null );
// Nested if ensures that all initialization is successful before it enters the cyclic Operation
If (succeeded (initd3d (hwnd )))
{
If (succeeded (initdraw ()))
{
// Display window
Showwindow (hwnd, sw_showdefault );
Updatewindow (hwnd );
// Optimized message loop
MSG;
Zeromemory (& MSG, sizeof (MSG ));
While (msg. message! = Wm_quit)
{
If (peekmessage (& MSG, null, 0u, 0u, pm_remove ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Else
{
Render ();
}
}
}
}
Unregisterclass ("d3d tutorical", WC. hinstance );
Return 0;
}