www.xbdev.net xbdev - software development
Monday December 10, 2018
home | about | contact | Donations


PrtX. Using index buffer into our vertex buffer...
by bkenwright@xbdev.net

Well if you think about it, this method does make sense.... as a cube is made up of 8 triangles, which would make 8*3=24 vertice points... but loads are repeated. (download full source code).

Instead a better alternative is to create a single list of all the vertice points and have a index buffer which lists each triangle's sides into this list of vertices.

Well its nothing special!  Didnt' want to complicate things.  All I'm doing is listing a single list of vertices in a vertex buffer and then listing which ones make up a single triangle using the index buffer..!  Of course this is only to show you the API's and there uses.

Lets code!

/***************************************************************************/

/*                                                                         */

/* File: game.cpp                                                          */

/*                                                                         */

/***************************************************************************/

 

#include <d3dx8.h>

#include <d3d8.h>

 

// These two items, the struct and the UINT, are the defentions of a single

// vertex point in space!.. the UINT is for directX so it knows what we want.

struct my_vertex

{

      FLOAT x, y, z;  // D3DFVF_XYZ

      DWORD colour;   // D3DFVF_DIFFUSE

};

 

UINT my_vertex_description = (D3DFVF_XYZ | D3DFVF_DIFFUSE);

 

 

// Well if we need to do any drawing or rendering triangles we can do it in here!

void Render()

{

      if(!g_pD3DDevice)return;

 

 

      my_vertex v[] =

      {   // Three points make up the face of the triangle.

            {-1.0f,-1.0f, 0.0f, 0xffffff00},  //[1]

            {0.0f, 1.0f, 0.0f, 0xffff2200},   //[2]

            {1.0f, -1.0f, 0.0f, 0xffffff00}   //[3]

      };

 

      WORD my_indexs[] =

      {

            0, 1, 2

      };

 

      IDirect3DVertexBuffer8 * DX_vb;

      g_pD3DDevice->CreateVertexBuffer( 3*sizeof(my_vertex), 0, my_vertex_description,

                                                                   D3DPOOL_MANAGED, &DX_vb );

 

      IDirect3DIndexBuffer8 * DX_ib;

      g_pD3DDevice->CreateIndexBuffer( sizeof(my_indexs), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,

                                                                   D3DPOOL_DEFAULT, &DX_ib);

 

      void *t;

      DX_ib->Lock(0,0, (BYTE**)&t, 0);

      memcpy(t, my_indexs, sizeof(my_indexs));

      DX_ib->Unlock();

 

      // Copy our array which is in computer memory over to the directX memory.. using that pointer we

      // just created etc.

      unsigned char *temp_pointer_vb;

      DX_vb->Lock(0,0, &temp_pointer_vb, 0);

      memcpy(temp_pointer_vb, v, sizeof(v) );

      DX_vb->Unlock();

     

      // So we've created our vertices and our index array on the graphics card you might

      // say, so now we can tell our graphics card which memory to use and how much etc..

 

      // Some stuff to setup or graphics card!

 

      //Turn off lighting becuase we are specifying that our vertices have colour

      g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

 

      g_pD3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);

      g_pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_DIFFUSE);

 

 

      // Clear the back buffer to a blue color

      g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

 

 

      // Draw our triangle.

      g_pD3DDevice->SetStreamSource(0, DX_vb, sizeof(my_vertex));

      g_pD3DDevice->SetVertexShader(my_vertex_description);

      g_pD3DDevice->SetIndices(DX_ib, 0);

   

      //  The ending point in the index buffer (e.g. which triangle)------

      //  The starting point in the index buffer-----------------------   |

      //                                                               |  |

      //  The end in the vertex buffer (e.g. how many points)---       |  |

      //  Starting vertex in the vertex buffer (e.g. x,y,z)--   |      |  |

      //                                                     |  |      |  |

      //                                                    \ /\ /    \ /\ /

      //                                                     |  |      |  |          

      g_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 3,     0, 1);

 

      // After rendering the scene we display it.

      g_pD3DDevice->Present( NULL, NULL, NULL, NULL );

 

      // As where calling the functino over and over again.. we'd be constantly creating new memory

      // without releasing the old if not for these line!

      DX_vb->Release();

      DX_ib->Release();

}

 

void mainloop()

{

      Render();

}

 

Okay there are only a few lines of difference... which are as follows

[1] - Where creating a index buffer as well as a vertex buffer using CreateIndexBuffer(..)

[2] - We call DrawIndexedPrimitive(..) instead of DrawPrimitive(..).

[3] - Finally not that we set our device to use the index buffer using SetIndices(..).

 

Note as well, that most 3D file formats store there information this way... as a total list of vertices followed by a list of index's into this array of vertices.

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.