The index cache is actually something that exists to optimize the vertex cache and can save memory consumption. It is understood that the combination of the index cache with the vertex cache will be much faster. Index caches and vertex caches are like Yu Boya and Zhong Ziqi periods
The index cache uses the same steps as the vertex cache to design vertex formats in four steps
This step is the same as the first step of the vertex cache.
Design a structure that represents a point, using a macro to define a flexible vertex format
creating vertex caches and index caches
The vertex cache is represented by the Idirect3dvertexbuffer9 interface object. The index cache is represented by the Idirect3dindexbuffer9 interface object.
The object for the vertex cache does not have to be restated again, and the next step is to focus on the creation of the index cache
HRESULT Createindexbuffer (
[In] UINT Length,
[In] DWORD Usage,
[In] D3dformat Format,
[In] D3dpool Pool,
[Out,reval] Idirect3dindexbuffer9 **ppindexbuffer,
[In] HANDLE *psharehandle
);
Detailed parameters
L UINT Length: Indicates the size of the index cache, in bytes
L DWORD Usage: Used to specify some additional properties that use the cache, which takes 0 to indicate no attached property
n Other parameters refer to the schematic in the vertex cache description article
L D3dformat Format: This is the biggest difference between CreateVertexBuffer and Createindexbuffer. This parameter is used to specify the size of each index that is stored in the index cache and can be seen as a D3dformat enumeration type. Specific to MSDN, a bit more
L D3dpool Pool: This is a D3dpool enumeration type that specifies the storage location where the index cache is stored to be in memory or in the graphics card video. By default, it is in the video card's memory
L IDIRECT3DINDEXBUFFER9 **ppindexbuffer: output parameter, call Createindexbuffer to initialize it, then the operation of the index cache is based on the parameters of this output.
L reserved parameters, generally set to null
accessing the index cache
As with the vertex cache, access to the index cache also requires lock, and unlock, where the access code is written between lock and unlock
HRESULT Lock (
[In] UINT Offsettolock,
[In] UINT Sizetolock,
[out] VOID **ppbdata,
[In] DWORD Flags
);
Detailed parameters
L UINT offsettolock: Indicates the offset of the lock area from the starting position of the storage space to the start lock position
L UINT Sizetolock: Indicates the number of bytes to lock, that is, the size of the lock area
L VOID **ppbdata: Pointer to the starting address of the locked store
L DWORD Flags: Indicates the way of locking, we can set it to 0, or you can use one of the following or combination
n parameter details of lock in reference vertex cache
Draw a graphic
In the vertex cache, call three functions before you start drawing
G_pd3ddevice->setstreamsource (0,g_pvertexbuffer,0,sizeof (CustomVertex));
G_PD3DDEVICE->SETFVF (D3dfvf_customvertex);
G_pd3ddevice->drawprimitive (d3dpt_trianglelist,0,2);
When the vertex cache is combined with the index cache, when the first two functions are called, you need to call a setindices method of the Idirect3ddecive9 interface to set the index cache, and finally the drawing function
HRESULT SetIndices (
[In] Idirect3dindexbuffer9 *pindexdata
);
The only parameter is the cache we created in Createindexbuffer
The last drawing function is not drawprimitive but drawindexedprimitive
HRESULT drawindexprimitive (
[In] D3dprimitivetype type,
[In] INT BaseVertexIndex,
[In] UINT Minindex,
[In] UINT Numvertices,
[In] UINT StartIndex,
[In] Primitivecount
);
Detailed parameters
L D3dprimitivetype Type: Represents the types of soil elements to be drawn
L INT BaseVertexIndex: Indicates the index position of the starting vertex of the index cache that will be drawn, that is, the point at which we start our index directory
L UINT Minindex: Represents the smallest index value in an indexed array, typically set to 0
L UINT numvertices: Represents the number of vertices we need to call the DrawIndexedPrimitive method this time.
L UINT StartIndex: Represents the start of drawing our entities from the index at the index location
L UINT Primitivecount: The number of entities to be drawn
The code is as follows
--------------------"Program description"-------------------
Program Name: D3ddemocore
November 2016 created by Cold River Snow
Description: Direct3D Program Core Framework
//-------------------------------------------------
---------------------"header file section"-------------------
Description: Contains the header file that the program depends on
//----------------------------------------------------
#include <d3d9.h>
#include <tchar.h>
#include <d3dx9.h>
----------------------"library file contains part"-----------------
#pragma comment (lib, "Winmm.lib")//the file required to invoke the PlaySound function
#pragma comment (lib, "D3dx9.lib")
#pragma comment (lib, "Dxerr.lib")
#pragma comment (lib, "Dxguid.lib")
#pragma comment (lib, "D3dx9d.lib")
#pragma comment (lib, "D3dx10.lib")
#pragma comment (lib, "D3d9.lib")
#pragma comment (lib, "Comctl32.lib")
-----------------------the macro definition section-------------
Description: Defines some auxiliary macros
//-------------------------------------------------
#define WINDOW_WIDTH 800//macro defined for window width to make it easier to modify the window width here
#define WINDOW_HEIGHT 600//a macro defined for window height to make it easier to modify the height of the window here
#define WINDOW_TITLE L "core framework of the Direct3D Program"//Macro defined for the bed title
#define SAFE_RELEASE (P) {if (p) {(P)->release ();(p) =null;}} Defines a security release macro that facilitates the release of subsequent COM interface pointers
-------------------"struct declaration"-----------------
Description: struct declaration
//-------------------------------------------------
struct CUSTOMVERTEX
{
float x, y, z, RHW;
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (d3dfvf_xyzrhw| D3dfvf_diffuse)
-------------------"global variable Declaration"-----------------
Description: Global variable declaration
//-------------------------------------------------
idirect3ddevice9* g_pd3ddevice = NULL;
id3dxfont* g_pfont = null;//font COM interface