Sign in to follow this  
steg

Best way to do vertex buffers

Recommended Posts

Hi, I'm trying to come up with a class to handle vertex buffers, most examples I see use something on the lines of :
CUSTOMVERTEX g_Vertices[] =
{
	{  10.0f,  10.0f, 0.5f, 0xafff0000, },
	{ 100.0f,  10.0f, 0.5f, 0xaf00ff00, },
	{  10.0f, 100.0f, 0.5f, 0xaf0000ff, },
};

struct CUSTOMVERTEX { 
    FLOAT x, y, z; 
    DWORD color; 
};

I was thinking of having a class with a similar vertex structure in it, within this calls a method to add a vertex, all this will need to be done dynamic though.
class CVertexManager 
{
private:
   struct CUSTOMVERTEX { 
      FLOAT x, y, z; 
      DWORD dwColor; 
   }*m_pVertexStructure;

   struct Vertex
   {
      CUSTOMVERTEX* pVertexStruct;
      Vertex* m_pNext;
   } *m_pVertex;

   Vertex* m_pHead;

public:
   void AddVertex(float x, float y, float z, DWORD dwColor);
};

Now, I guess when adding a new vertex I will need to allocate memory for this new vertex, such as :
if ( m_pVertex == NULL )
{
  m_pVertex = new Vertex;
  m_pHead = m_pVertex;
  m_pVertex->m_pNext = NULL;
}
else
{
  m_pVertex->m_pNext = new Vertex;
  m_pVertex = m_pVertex->m_pNext;
}

m_pVertex->pVertexStruct = new CUSTOMVERTEX;
m_pVertex->pVertexStruct->x = 10.0f;
m_pVertex->pVertexStruct->y = 100.0f;
m_pVertex->pVertexStruct->z = 0.0f;
m_pVertex->pVertexStruct->dwColor = 0xffff0000;

Thus here, using a single link list to store the vertices, is this ott ? How do you guys do this ? Best regards, Steve Edited by Coder: Source tags are good. Check GDNet Forums FAQ [Edited by - Coder on August 18, 2004 10:11:42 AM]

Share this post


Link to post
Share on other sites
I would definitely recommend not using a linked list to store vertices. Horribly inefficient for such a use. Try looking into the STL's vector object. And if you do use a vector object, then you'll be to easily use memcpy to copy all the vertices from your vector in memory to a locked IDirect3DVertextBuffer9 object.

Share this post


Link to post
Share on other sites
Thanks Agony, I thought of using the STL Vector, I'm not a STL person really, mainly use MFC (this has some great data structures) but not for game dev. As for not using a whole class for this, I tend to agree with you Lee_ in some sense, but how to simplyfy is my problem. I know in a real situation, a static vertex structure is of no use, one that gets loaded dynamically to speak. Just think my brain is muddled today!

Best regards,
Steve

Share this post


Link to post
Share on other sites
I should have stated more. Everyone does it differently, some choose a renderer class and others just lock and discard wherever (especially if they don't use OOP). If I didn't know better, it looks like your manually typing in all the vertices besides using some kind of file format...? Just don't think too hard about this stuff though.. for example, if you know you're going to be updating and rendering something dynamic each time, then do it all in one loop instead of calling two functions. Try to think about how you're going to batch everything too, best to just start practicing now because could gain hundreds of fps back if done properly... or should I say, you could lose hundreds of fps if done improperly. I wish I could give more advice, but I'm sure it'll just come on naturally the more you do this stuff.

Share this post


Link to post
Share on other sites
Thanks Lee_

I really appreciate your comments and help.

Last thing I did in Direct3D was a year ago, that was a terrain engine of soughts using chunk LOD, I left it when I implemented the skirts for fixing the cracks. My vertex buffer then was allocated at runtime and the vertices read in from a file, well at least for the height data. I think having a new born baby now and lack of sleep makes my brain a little more tired :-(

Kind regards,
Steve

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this