Sign in to follow this  

Vertex Buffer

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What i want to do is instead of my Quad class creating a vertex buffer every time i want a quad, i want the quad class to just define the vertices. Then i want to put all the vertices into 1 VB for rendering. I will probably program a render queue. 1. How do i go about copying vertices from the different memory locations into the 1 Vertex Buffer, assuming i can ammend each new set of vertices to the end of the VB. I want to create a vertex buffer and continually add vertices to it. 2. So assuming that every quad i wanted in the vb needs a different texture, how do i run through the VB each frame and apply a different texture for the vertices that belong to different objects, whats the overall principle for this? ANy help is appreciated, ace

Share this post


Link to post
Share on other sites
Use a std::vector to hold the vertices. In this case there's no need for indices, so 4 vertices each quad; you can even use quad-structures for the std::vector.
Now once a frame, you lock the VertexBuffer and go through the std::vector to add the vertices to it.

You don't need to worry about the speed of the std::vectors, as long as you don't over-run the buffer (with std::vector::add()) too much it won't slow down. If you know howmany vertices/quads there are, then you can std::vector::reserve(..).

This will be a lot faster than seperate VertexBuffers.

Share this post


Link to post
Share on other sites
Quote:
Original post by ace_lovegrove
1. How do i go about copying vertices from the different memory locations into the 1 Vertex Buffer, assuming i can ammend each new set of vertices to the end of the VB. I want to create a vertex buffer and continually add vertices to it.
Just pass the address of where to put the vertexes into the VB to the quad, and it can copy its vertexes to that location. The drawback is that the quad must know the format of the vertexes in the VB. The alternative is to go to each quad and get the vertex information and use that to fill in the VB. If you don't/can't know the size of the vertex buffer ahead of time, then you make a fixed size buffer. When it is full, draw it and start another one.
Quote:
Original post by ace_lovegrove
2. So assuming that every quad i wanted in the vb needs a different texture, how do i run through the VB each frame and apply a different texture for the vertices that belong to different objects, whats the overall principle for this?
You must keep track of which vertexes use which textures and either sort the VB or use an index buffer. Then call DP for each texture, passing the relevant elements of the VB/IB.

Share this post


Link to post
Share on other sites
I'm just messing around with graphics right now and this is how I'm doing it (I believe the code you need is in the DrawAll() function in SceneGraph):

// Note that I haven't really tried using textures yet.
//##################################################################################
// Vertex2
//##################################################################################
class Vertex2
{
public:
float x, y, z, rhw;
D3DCOLOR diffuse;
float u, v;

Vertex2(float fx = 0.0f, float fy = 0.0f, D3DCOLOR cdiffuse = D3DCOLOR_XRGB(255, 255, 255), float fu = 0.0f, float fv = 0.0f);
const Vertex2& operator =(const Vertex2& vVertex);
};
const DWORD VertexFVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;

//##################################################################################
// Polygon2
//##################################################################################
class Polygon2
{
Vertex2* m_pVertices;
int m_iCount;

public:
Polygon2();
Polygon2(int iCount);
~Polygon2();

int AddVertices(int iCount = 1);
void SetVertex(int iIndex, float x, float y, D3DCOLOR diffuse, float u, float v);
void SetVertex(int iIndex, const Vertex2& vVertex);

Vertex2* GetVertices() const;
int GetCount() const;
int Size() const;
};

//##################################################################################
// SceneGraph
//##################################################################################
class SceneGraph
{
vector<Polygon2*> m_Polygons;

public:
SceneGraph();
~SceneGraph();

int AddPolygon(Polygon2* pPolygon);
void DrawAll(IDirect3DDevice9* pDevice);
};




//##################################################################################
// Vertex2()
//##################################################################################
Vertex2::Vertex2(float fx, float fy, D3DCOLOR cdiffuse, float fu, float fv)
{
x = fx;
y = fy;
z = 0.0f;
rhw = 1.0f;
diffuse = cdiffuse;
u = fu;
v = fv;
}

//##################################################################################
// operator =()
//##################################################################################
const Vertex2& Vertex2::operator =(const Vertex2& vVertex)
{
x = vVertex.x;
y = vVertex.y;
z = vVertex.z;
rhw = vVertex.rhw;
diffuse = vVertex.diffuse;
u = vVertex.u;
v = vVertex.v;

return *this;
}

//##################################################################################
// Polygon2()
//##################################################################################
Polygon2::Polygon2()
{
m_pVertices = NULL;
m_iCount = 0;
}

//##################################################################################
// Polygon2()
//##################################################################################
Polygon2::Polygon2(int iCount)
{
m_pVertices = NULL;
m_iCount = 0;

if(iCount > 0)
AddVertices(iCount);
}

//##################################################################################
// ~Polygon2()
//##################################################################################
Polygon2::~Polygon2()
{
if(m_pVertices) delete [] m_pVertices;
}

//##################################################################################
// AddVertices()
//##################################################################################
int Polygon2::AddVertices(int iCount)
{
Vertex2* pVertices = NULL;

if(!(pVertices = new Vertex2[m_iCount + iCount])) return -1;

if(m_pVertices)
{
memcpy(pVertices, m_pVertices, m_iCount * sizeof(Vertex2));
delete [] m_pVertices;
}

m_pVertices = pVertices;
m_iCount += iCount;

return m_iCount - iCount;
}

//##################################################################################
// SetVertex()
//##################################################################################
void Polygon2::SetVertex(int iIndex, float x, float y, D3DCOLOR diffuse, float u, float v)
{
m_pVertices[iIndex] = Vertex2(x, y, diffuse, u, v);
}

//##################################################################################
// SetVertex()
//##################################################################################
void Polygon2::SetVertex(int iIndex, const Vertex2& vVertex)
{
m_pVertices[iIndex] = vVertex;
}

Vertex2* Polygon2::GetVertices() const { return m_pVertices; }
int Polygon2::GetCount() const { return m_iCount; }
int Polygon2::Size() const { return m_iCount * sizeof(Vertex2); }

SceneGraph::SceneGraph(){}
SceneGraph::~SceneGraph(){}

int SceneGraph::AddPolygon(Polygon2* pPolygon)
{
m_Polygons.push_back(pPolygon);
return 0;
}

void SceneGraph::DrawAll(IDirect3DDevice9* pDevice)
{
IDirect3DVertexBuffer9* pVB = NULL;

int iCount = 0;
for(int a = 0; a < m_Polygons.size(); a++) iCount += m_Polygons[a]->GetCount();
pDevice->CreateVertexBuffer(iCount * sizeof(Vertex2), 0, VertexFVF, D3DPOOL_MANAGED, &pVB, NULL);

void* pVertices = NULL;
iCount = 0;
pVB->Lock(0, 0, (void**)&pVertices, 0);
int iSize = m_Polygons.size();
Polygon2* pPolygon = NULL;
for(a = 0; a < iSize; a++)
{
pPolygon = m_Polygons[a];
memcpy((Vertex2*)pVertices + iCount, pPolygon->GetVertices(), pPolygon->Size());
iCount += pPolygon->GetCount();
}
pVB->Unlock();

pDevice->SetFVF(VertexFVF);
pDevice->SetStreamSource(0, pVB, 0, sizeof(Vertex2));

pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, iCount / 3);

pVB->Release();
}




Hopefully that helps you out!

Share this post


Link to post
Share on other sites

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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