Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to compute the "gravity center" of a mesh, some kind of barycenter, and then find the farest vertices to compute a bounding sphere.

Here is what I have for the moment:

D3DXVECTOR3 result(0,0,0);
int num = mesh -> GetNumVertices();
mesh -> GetVertexBuffer((*vertexBuffer));
void* rawData;
vertexBuffer -> Lock(0, mesh->GetNumBytesPerVertex() * mesh->GetNumVertices(), &rawData, D3DLOCK_READONLY);
UINT numElements;
mesh -> GetDeclaration();
mesh -> GetFVF();

Apparently I need to cast the rawdata pointer, but I don't know what to do next...

share|improve this question
I assume this is pseudocode? Because you're missing parameters and/or not saving off useful return values here. – Josh Petrie Mar 25 '11 at 19:25
This seems to be C++ code, but yes, some functions miss parameters... – jokoon Mar 25 '11 at 19:39

Are you asking how to compute the "center" or how to get the points with which to do so? I'll assume -- and answer for -- the latter:

Once you have the buffer pointer itself, and information about the format of the vertices within and the number of vertices (it appears from your code snippet that you have all of this already), you use the format information to determine where the positional data is for each vertex.

In general this involves iterating the elements of the vertex declaration, updating a byte offset until you reach the element representing the position. However in practice, the position is almost always at offset 0 from the start of a vertex so you may not need to be so pedantic.

Once you have that offset, initialize a cursor pointer to (start of buffer + that offset) and read the next few bytes of data, which will be either XYZ or XYZW floating point values, depending on the format of your buffer. Then increment the cursor pointer by the number of bytes in each vertex (including stride, if it exists) to get to the positional data for the next vertex. Repeat until you've read all the positional data.

You can save off all this positional data into a container (like a std::vector) and process it after the fact, or you can run your algorithm as you do the extraction, if appropriate.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.