[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.6.4 Updating Vertex Buffers

The complexity of updating a vertex buffer entirely depends on how its contents are laid out. You can lock a buffer (See section Locking buffers), but how you write data into it vert much depends on what it contains.

Lets start with a vert simple example. Lets say you have a buffer which only contains vertex positions, so it only contains sets of 3 floating point numbers per vertex. In this case, all you need to do to write data into it is:

Real* pReal = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));

... then you just write positions in chunks of 3 reals. If you have other floating point data in there, it’s a little more complex but the principle is largely the same, you just need to write alternate elements. But what if you have elements of different types, or you need to derive how to write the vertex data from the elements themselves? Well, there are some useful methods on the VertexElement class to help you out.

Firstly, you lock the buffer but assign the result to a unsigned char* rather than a specific type. Then, for each element which is sourcing from this buffer (which you can find out by calling VertexDeclaration::findElementsBySource) you call VertexElement::baseVertexPointerToElement. This offsets a pointer which points at the base of a vertex in a buffer to the beginning of the element in question, and allows you to use a pointer of the right type to boot. Here’s a full example:

// Get base pointer
unsigned char* pVert = static_cast<unsigned char*>(vbuf->lock(HardwareBuffer::HBL_READ_ONLY));
Real* pReal;
for (size_t v = 0; v < vertexCount; ++v)
{
	// Get elements
	VertexDeclaration::VertexElementList elems = decl->findElementsBySource(bufferIdx);
	VertexDeclaration::VertexElementList::iterator i, iend;
	for (i = elems.begin(); i != elems.end(); ++i)
	{
		VertexElement& elem = *i;
		if (elem.getSemantic() == VES_POSITION)
		{
			elem.baseVertexPointerToElement(pVert, &pReal);
			// write position using pReal

		}
		
		...
		
		
	}
	pVert += vbuf->getVertexSize();
}
vbuf->unlock();

See the API docs for full details of all the helper methods on VertexDeclaration and VertexElement to assist you in manipulating vertex buffer data pointers.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on August 20, 2012 using texi2html 5.0.