DirectX 8. Начинаем работу с DirectX Graphics (Ваткин, Dempski) - страница 22

> FLOAT x, y, z;

> DWORD color;

> FLOAT u, v;

>};


>#define D3DFVF_PANELVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

This structure and Flexible Vertex Format (FVF) specify that we are talking about a vertex that has a position, a color, and a set of texture coordinates.

Now we need a vertex buffer. Add the following line of code to the list of globals. Again, for simplicity, I'm making it global — this is not a demonstration of good coding practice.

>LPDIRECT3DVERTEXBUFFER8 g_pVertices = NULL;

Now, add the following lines of code to the PostInitialize function (explanation to follow):

>float PanelWidth = 50.0f;

>float PanelHeight = 100.0f;

>g_pd3dDevice->CreateVertexBuffer(4 * sizeof(PANELVERTEX), D3DUSAGE_WRITEONLY,

> D3DFVF_PANELVERTEX, D3DPOOL_MANAGED, &g_pVertices);

>PANELVERTEX* pVertices = NULL;

>g_pVertices->Lock(0, 4 * sizeof(PANELVERTEX), (BYTE**)&pVertices, 0);

>//Set all the colors to white

>pVertices[0].color = pVertices[1].color = pVertices[2].color = pVertices[3].color = 0xffffffff;

>//Set positions and texture coordinates

>pVertices[0].x = pVertices[3].x = -PanelWidth / 2.0f;

>pVertices[1].x = pVertices[2].x = PanelWidth / 2.0f;

>pVertices[0].y = pVertices[1].y = PanelHeight / 2.0f;

>pVertices[2].y = pVertices[3].y = -PanelHeight / 2.0f;

>pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 1.0f;

>pVertices[1].u = pVertices[2].u = 1.0f;

>pVertices[0].u = pVertices[3].u = 0.0f;

>pVertices[0].v = pVertices[1].v = 0.0f;

>pVertices[2].v = pVertices[3].v = 1.0f;

>g_pVertices->Unlock();

This is actually much simpler than it may look. First, I made up a size for the panel just so we'd have something to work with. Next, I asked the device to create a vertex buffer that contained enough memory for four vertices of my format. Then I locked the buffer so I could set the values. One thing to note, locking buffers is very expensive, so I'm only going to do it once. We can manipulate the vertices without locking, but we'll discuss that later. For this example I have set the four points centered on the (0, 0). Keep this in the back of your mind; it will have ramifications later. Also, I set the texture coordinates. The SDK explains these pretty well, so I won't get into that. The short story is that we are set up to draw the entire texture. So, now we have a rectangle set up. The next step is to draw it…

Drawing the Panel

Drawing the rectangle is pretty easy. Add the following lines of code to your Render2D function:

>g_pd3dDevice->SetVertexShader(D3DFVF_PANELVERTEX);

>g_pd3dDevice->SetStreamSource(0, g_pVertices, sizeof(PANELVERTEX));