LPDIRECT3D9 pD3D; LPDIRECT3DDEVICE9 pd3dDevice; // Create a Direct3D object pD3D = Direct3DCreate9(D3D_SDK_VERSION); // Create a Direct3D device object pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice);
LPDIRECT3DVERTEXBUFFER9 pVertexBuffer; // Create a vertex buffer object pd3dDevice->CreateVertexBuffer(4 * sizeof(MY_VERTEX), D3DUSAGE_WRITEONLY, MY_VERTEX::FVF, D3DPOOL_DEFAULT, &pVertexBuffer, NULL); // Lock the vertex buffer for writing void* pVertices = NULL; pVertexBuffer->Lock(0, 0, &pVertices, 0); // Write vertex data to the buffer MY_VERTEX* pMyVertices = (MY_VERTEX*)pVertices; pMyVertices[0] = MY_VERTEX(-1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0)); pMyVertices[1] = MY_VERTEX(-1.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0)); pMyVertices[2] = MY_VERTEX(1.0f, 1.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255)); pMyVertices[3] = MY_VERTEX(1.0f, -1.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 0)); // Unlock the vertex buffer pVertexBuffer->Unlock(); // Set the vertex buffer as the active source pd3dDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(MY_VERTEX)); pd3dDevice->SetFVF(MY_VERTEX::FVF); // Draw the triangle strip pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);In this example, we create a vertex buffer object using the CreateVertexBuffer method and write vertex data to it using the Lock method. We then set the vertex buffer as the active source using the SetStreamSource method and draw a triangle strip using the DrawPrimitive method. Package library: Microsoft DirectX SDK.