How to return a VertexBuffer out of a std::vector???

Started by
11 comments, last by stefan5000 18 years, 9 months ago
Hey guys, I have a vector which holds vertex buffers. --> vector<LPDIRECT3DVERTEXBUFFER9> VBList; When I try to return one specific vertex buffer, my program is ending up in a memory error. Do you know how to return it that there is now error?! I want to write it in a function which I give two parameters: 1. int ModelIndex 2. int MaterialIndex where MaterialIndex is the index of the vertex buffer list item to return! --> LPDIRECT3DVERTEXUFFER9 CModel::GetVertexBuffer(int ModelIndex, int MaterialIndex) { return ... } thanks for help! cu
Advertisement
Try using the [] operator (which is supported by vectors)

For exmaple :
If you got the following vector

vector<VertexBuffer*> vbList;

for getting the i'th vertex buffer do :

VertexBuffer* vb = vbList;

Make sure the index is valid, or else you'll get an error.
There is nothing that can't be solved. Just people that can't solve it. :)
Well, just tested it, but it didn't work. Maybe the problem could be there, because the vertex buffer vector is part of another vector (vector<SModel> ModelList) -> So my vertex buffer list is part of the SModel structure. Maybe there is some access blocking that prevents me from reading the vertex buffer?!

I think this, because when I want to release the vertex buffers I have to do the following:

std::vector<SModel>::iterator Itr = m_ModelList.begin();
while(Itr != m_ModelList.end())
{
SModel pModel = *Itr;

for(int index = 0; index < pModel.NumVertexBuffers; index++)
{
LPDIRECT3DVERTEXBUFFER9 *pVB = pModel.VertexBufferList[index];
(*pVB)->Release();
}
++Itr;
}
m_ModelList.clear();
m_NumModels = 0;
If SModel is a class that you defined, be certain that you override the = operator. Otherwise, store them as SModel Pointers instead.
Chris ByersMicrosoft DirectX MVP - 2005
It isn't a class - it is a struct:

struct SModel
{
...
int NumVertexBuffers;
vector<LPDIRECT3DVERTEXBUFFER9*> VertexBufferList;
..
};

class CModel
{
private:
static vector<SModel> m_ModelList;
...

};
Quote:Original post by Supernat02
If SModel is a class that you defined, be certain that you override the = operator. Otherwise, store them as SModel Pointers instead.


Thats right,store your SModel structs as pointers instead of objects.

vector<SModel*> m_ModelList;

There is nothing that can't be solved. Just people that can't solve it. :)
Okay, now I changed it as you told me -> the vectors are storing pointers now:

struct SModel
{
...
vector<LPDIRECT3DVERTEXBUFFER9*> VertexBufferList;
...
};

class CModel
{
private:
static vector<SModel*> m_ModelList;
...
};


Then in my LoadModel(...) function I create one vertex buffer and add this to the VertexBufferList:

SModel Model;

...

CUSTOMMODELVERTEX *pVertex;

LPDIRECT3DVERTEXBUFFER9 *pVB = new LPDIRECT3DVERTEXBUFFER9;

m_pD3DDevice->CreateVertexBuffer(3*sizeof(CUSTOMMODELVERTEX), D3DUSAGE_DYNAMIC, D3DFVF_XYZ | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, pVB, NULL);

(*pVB)->Lock(0, 3*sizeof(CUSTOMMODELVERTEX), (void**)&pVertex, D3DLOCK_DISCARD);

pVertex[0].Position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
pVertex[1].Position = D3DXVECTOR3(0.0f, 0.0f, 50.0f);
pVertex[2].Position = D3DXVECTOR3(50.0f, 0.0f, 50.0f);

pVertex[0].Color = D3DCOLOR_XRGB(255, 255, 255);
pVertex[1].Color = D3DCOLOR_XRGB(255, 255, 255);
pVertex[2].Color = D3DCOLOR_XRGB(255, 255, 255);

(*pVB)->Unlock();

Model.VertexBufferList.push_back(pVB);

Model is just an object, because it is returned to another method.


And when I want to return the first vertex buffer of the vector, I'm running in memory errors:

LPDIRECT3DVERTEXBUFFER9 CModel::GetVertexBuffer(int ModelIndex, int MaterialIndex)
{
return (*m_ModelList[ModelIndex]->VertexBufferList[MaterialIndex]);
}
First of all, the LPDIRECT3DVERTEXBUFFER9 is already defined as a pointer to the vertex buffer interface so there is no need to add the '*'.

Second, on this line :

return (*m_ModelList[ModelIndex]->VertexBufferList[MaterialIndex]);

There is no need for the '*' since the [] operator returns the object stored at the i'th place in the vector which is already a pointer.

Try removing the above and see if it helps.
There is nothing that can't be solved. Just people that can't solve it. :)
Done what you said (and agree with it (->LP = long pointer))!

But there is still the error! Can anyone test this himself with a vector of vertex buffers?
Hmmm...

Well, doesn't work already, but I thought about the performance of my game, when every model would have multiple vertex buffers and I would have to lock and unlock them every frame for animating it!

I think I could pack all animated objects into one big buffer that is updated every frame and all static objects of a level in a static vertex buffer.

For both of it I would create a list, where information about the vertices are saved, like the following:

- vertex 0 to 20 represent Mesh 1 of Model 1
- vertex 200 to 400 represent Mesh 12 of Model 4

Of course, I would have to update the size of the vertex buffers when I load a new model.

What do you think about it?

[Edited by - stefan5000 on July 23, 2005 10:27:38 AM]

This topic is closed to new replies.

Advertisement