1
\$\begingroup\$

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();
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
mesh -> GetVertexBuffer((*vertexBuffer));
void* rawData;
vertexBuffer -> Lock(0, mesh->GetNumBytesPerVertex() * mesh->GetNumVertices(), &rawData, D3DLOCK_READONLY);
D3DVERTEXELEMENT9 decl;//[MAXD3DDECLLENGTH];
UINT numElements;
mesh -> GetDeclaration();
mesh -> GetFVF();

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

\$\endgroup\$
2
  • \$\begingroup\$ I assume this is pseudocode? Because you're missing parameters and/or not saving off useful return values here. \$\endgroup\$
    – user1430
    Mar 25 '11 at 19:25
  • \$\begingroup\$ This seems to be C++ code, but yes, some functions miss parameters... \$\endgroup\$
    – jokoon
    Mar 25 '11 at 19:39
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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