ConvexShadpe problem.

BlasterN

08-08-2006 20:51:23

I'm developing now a Mesh to ConvexShape.
The first goal was to extract the Edges for the mesh. I use getMeshInformation(...) from
http://www.ogre3d.org/wiki/index.php/RetrieveVertexData , the second one.
that function return:
- all vertices
- all indexes (triangles)

with a little code a get edges like that:

bool notRepeated(std::vector<Ogre::Vector3>* verts, Ogre::Vector3& A1, Ogre::Vector3& A2)
{
for(int i=0; i<verts->size(); i+=2)
{
if (
( (*verts)[i] == A1 && (*verts)[i+1] == A2 )
||
( (*verts)[i] == A2 && (*verts)[i+1] == A1 )
)
{
OgreLog(Ogre::String("REPEATED"));
return false;
}
}
return true;
}

std::vector<Ogre::Vector3>& getMeshEdges(Ogre::Mesh* mesh)
{
size_t vertex_count,index_count;
Ogre::Vector3* vertices;
unsigned long* indices;

getMeshInformation(mesh,vertex_count,vertices,index_count,indices,
Ogre::Vector3::ZERO,Ogre::Quaternion::IDENTITY,
Ogre::Vector3(1,1,1));

std::vector<Ogre::Vector3>* out=new std::vector<Ogre::Vector3>();

OgreLog(Ogre::StringConverter::toString(index_count));
OgreLog(Ogre::StringConverter::toString(vertex_count));

for(size_t i=0; i<index_count; i+=3)
{
if(notRepeated(out,vertices[indices[i]],vertices[indices[i+1]]))
{
out->push_back(vertices[indices[i]]);
out->push_back(vertices[indices[i+1]]);
}
if(notRepeated(out,vertices[indices[i]],vertices[indices[i+2]]))
{
out->push_back(vertices[indices[i]]);
out->push_back(vertices[indices[i+2]]);
}
if(notRepeated(out,vertices[indices[i+1]],vertices[indices[i+2]]))
{
out->push_back(vertices[indices[i+1]]);
out->push_back(vertices[indices[i+2]]);
}
}


delete[] vertices;
delete[] indices;

return *out;

}

(I'm not worry about the memory leak yet)

So with that code applied to a cube give you a vector with 32 points (16 edges) that's correct, but when create the ConvexShape Crash (no assertions) :S



So, someone have a Mesh -> ConvexShape vector.

betajaen

08-08-2006 22:02:28

Hey, Good work.

But there is a hard limit on how many vertices a convex shape is, and I think that is 32 or 64, also you don't have to supply the edges, or indices. Just the vertices.

BlasterN

09-08-2006 12:11:26

Ok, as allways I dont search enought in the forum (related post I missed: http://www.ogre3d.org/phpBB2addons/view ... light=link )

I made a Ogre::Mesh to std::vector<Ogre::Vector3> (vertices only no repeated)

Now works fine in Release Mode, and don't work in debug. So it will be a pain to debug my program in release without memory management.

Is there any trick to run ConvexShapes in Debug?


CODE:
void getMeshInformation( const Ogre::Mesh* const mesh,
const Ogre::Vector3 &position,
const Ogre::Quaternion &orient,
const Ogre::Vector3 &scale,
std::vector<Ogre::Vector3>& output)
{
bool added_shared = false;
size_t current_offset = 0;
size_t shared_offset = 0;
size_t next_offset = 0;
size_t vertex_count=0;

vertex_count = 0;

// Calculate how many vertices and indices we're going to need
for ( unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
{
Ogre::SubMesh* submesh = mesh->getSubMesh( i );

vertex_count += submesh->vertexData->vertexCount;
}

OgreLog(Ogre::String("vertex: ")+Ogre::StringConverter::toString(vertex_count));

added_shared = false;

// Run through the submeshes again, adding the data into the arrays
for ( unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
{
Ogre::SubMesh* submesh = mesh->getSubMesh(i);

Ogre::VertexData* vertex_data = submesh->useSharedVertices ? mesh->sharedVertexData : submesh->vertexData;

if((!submesh->useSharedVertices)||(submesh->useSharedVertices && !added_shared))
{
if(submesh->useSharedVertices)
{
added_shared = true;
shared_offset = current_offset;
}

const Ogre::VertexElement* posElem =
vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);

Ogre::HardwareVertexBufferSharedPtr vbuf =
vertex_data->vertexBufferBinding->getBuffer(posElem->getSource());

unsigned char* vertex =
static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));

// There is _no_ baseVertexPointerToElement() which takes an Ogre::Real or a double
// as second argument. So make it float, to avoid trouble when Ogre::Real will
// be comiled/typedefed as double:
// Ogre::Real* pReal;
float* pReal;

for( size_t j = 0; j < vertex_data->vertexCount; ++j, vertex += vbuf->getVertexSize())
{
posElem->baseVertexPointerToElement(vertex, &pReal);

Ogre::Vector3 pt(pReal[0], pReal[1], pReal[2]);

bool add=true;
for(size_t x=0; x<output.size();++x)
{
if ( output[x] == (orient * (pt * scale)) + position )
add=false;
}

if ( add )
{
output.push_back((orient * (pt * scale)) + position);
OgreLog(Ogre::String("vertex added: ")+Ogre::StringConverter::toString(pt)+Ogre::String("position: ")+Ogre::StringConverter::toString(current_offset + j));
}
}

vbuf->unlock();
next_offset += vertex_data->vertexCount;
}
current_offset = next_offset;
}
}

(not optimized yet)

betajaen

09-08-2006 12:45:26

Yay, well done!

But: Debug never works with that memory manager, so don't worry about it.

If you want me to add it into NxOgre, just say the word.

BlasterN

09-08-2006 19:20:35

feel free to do it. remove OgreLog() and include it in Preview 4 ( I'm waiting the new release!!! )


I'm thinking now to do a trimesh collision for debug and ConvexShape for release, with that I have both codes so I can test the speed in release for both and I can run the program in debug so i can see the memory leaks.

betajaen

09-08-2006 19:22:24

Well if you want to rewrite the memory manager supplied with Ageia go ahead. :D

BlasterN

10-08-2006 01:18:42

betajaen do me a favor.
Test it with Preview 4 and Physx 2.4.4 or 2.5 (if it's compatible nxOgre)

maybe it works, maybe not :roll:

betajaen

10-08-2006 01:23:58

I will do so in the morning. :wink:

betajaen

10-08-2006 15:03:26

Alright here you go, you have a choice of two :)


new convexShape(<vertices>, <material>, <pose>);
new convexShape(<meshname>, <material>, <pose>);


It works properly with the convexShape provided:




Fairly okay, with cube.1m.mesh




And not so well with crumple.mesh (Although I did expect PhysX to explode with 96 vertices).

BlasterN

10-08-2006 21:34:18

I tested with a lot of vertices, around 160 but all cubes, and work.

About "crumple.mesh" it isn't a Convex mesh properly, Physx try to make a Convex Mesh more regular then the Original one, with that simplify the collision.

I will test Athenea.mesh & ogre.mesh when have time, right now my app crash when try to world->setdebug(true) ( something related with the logmanager i will search now in the forums )

betajaen

10-08-2006 21:41:25

It's not really to do with the amount of vertices, although I'd assume the less the better.

It's to do that it's a polyhedron; meaning that it doesn't contain any holes or indentations.

BlasterN

15-08-2006 15:39:20

I made some progress in the new part of the code: ConvexShape LOD
That means that use a LOD level to create the Convex Shape for physics optimizations.


Here are some pics from my test.
Athnea model with 5 LODS, 60% reduction 126 vertices (using LOD 5)




Code soon.

betajaen

15-08-2006 17:34:03

Wow, that is pretty good.

However the stick thing should be a separate shape there.