Direct3D Index cache is in fact the vertex cache on the basis of the fixed vertex connection, such as our plane has 4 points, respectively, and then left and right corner, and the lower left corner, starting from the upper left-hand side to give them the index in clockwise order 0,1,2,3. (These indexes are the order you set in the vertex structure).
And then we're going to use these four points to draw a square. We only need to use index cache to label, for example, 012 to form the triangle to the right of the square, then 023 to form the other side of the triangle, so that a square is drawn. It's not that hard. Let's take a look at the specific operation.
Now create the index cache and vertex cache objects
Lpdirect3dvertexbuffer9 g_pvertexbuffer =null;
Lpdirect3dindexbuffer9 g_pindexbuffer =null;
Then set the vertex structure
struct VECREX3
{
FLOAT x,y,z;
WORD color;
};
#define VECREX3_FVF (d3dfvf_xyzrhw| D3dfvf_diffuse)/Flexible vertex format
These steps are basically the same as the vertex cache
And then initialize the vertex coordinates and index
Srand (timeGetTime ()); if (8*sizeof (VECREX3), 0,VECREX3_FVF, D3dpool_default,&g_, FAILED (G_pd3ddevic->createvertexbuffer)
Pvertexbuffer,null)) {return e_fail; } VECREX3 vertexs[]={{270.0f,100.0f,0.0f,d3dcolor_xrgb (rand ()%256,rand ()%256,rand ()%256),},//0 {540.0f,100.0f, 0.0f,d3dcolor_xrgb (rand ()%256,rand ()%256,rand ()%256),},//1 {810.0f,100.0f,0.0f,d3dcolor_xrgb (rand ()%256,rand ()% 256,rand ()%256),},//2 {810.0f,300.0f,0.0f,d3dcolor_xrgb (rand ()%256,rand ()%256,rand ()%256),},//3 {810.0f,500.0f, 0.0f,d3dcolor_xrgb (rand ()%256,rand ()%256,rand ()%256),},//4 {540.0f,500.0f,0.0f,d3dcolor_xrgb (rand ()%256,rand ()% 256,rand ()%256),},//5 {270.0f,500.0f,0.0f,d3dcolor_xrgb (rand ()%256,rand ()%256,rand ()%256),},//6 {270.0f,300.0f,
0.0f,d3dcolor_xrgb (rand ()%256,rand ()%256,rand ()%256),}//7};
VOID *pvertices;
if (FAILED g_pvertexbuffer->lock (0,sizeof (Vertexs), (void**) &pvertices,0)) {return e_fail;
} memcpy (Pvertices,vertexs,sizeof (Vertexs)); G_pvertexbuffer->unLock (); if (FAILED g_pd3ddevic->createindexbuffer (36*sizeof (DWORD), 0,d3dfmt_index16,d3dpool_default, &g_
Pindexbuffer,null)) {return e_fail;
WORD indices[]={0,1,7, 1,2,3, 3,4,5, 5,6,7};
WORD *pindices=null;
if (FAILED (G_pindexbuffer->lock (0,0, (void**) &pindices,0)) {return e_fail;
} memcpy (Pindices,indices,sizeof (indices)); G_pindexbuffer->unlock ();
A careful person will find that we have fewer coordinates in this vertex, yes, because the index cache is the 017,123,345,567 of points in the vertex array to connect the order of those vertices.
The final drawing of the graph also has a little change as follows
G_pd3ddevic->beginscene ();
G_pd3ddevic->setrenderstate (d3drs_lighting, FALSE);
G_pd3ddevic->setstreamsource (0,g_pvertexbuffer,0,sizeof (VECREX3));
G_PD3DDEVIC->SETFVF (VECREX3_FVF);
G_pd3ddevic->setindices (g_pindexbuffer);
G_pd3ddevic->drawindexedprimitive (d3dpt_trianglelist,0,0,8,0,4);
8 represents 8 vertices, and 4 represents 4 entities.
Index caching more specific explanations see http://blog.csdn.net/poem_qianmo/article/details/8304741