ConvexMesh

BlasterN

11-09-2007 20:36:24

Hi! betajean!

Long time no see u...

first thing first, the old post
http://www.ogre3d.org/phpBB2addons/viewtopic.php?t=1861

I was working in a LOD version of Mesh to create a ConvexMesh and now is finished I use a deprecated version of NxOgre & Ogre itself.
I will upgrade all dependencias asap if the code needs to be tweaked i will do it myself soon.


the code need to be cleaned from my loggers and things like that.
.h
namespace MESHUTIL
{
/**
Dont call this directly, use: getMeshPoints or getMeshPointsLOD instead.
@see getMeshPoints
@see getMeshPointsLOD
*/
void getVertex( const Ogre::Mesh* const mesh,
const Ogre::Vector3 &position,
const Ogre::Quaternion &orient,
const Ogre::Vector3 &scale,
std::vector<Ogre::Vector3>& output,
bool removeRepeated=true);
/**
Get all Points/Vertices from a mesh, and store it into out.\n
use: ConvexShape generation
*/
void getMeshPoints(Ogre::Mesh* mesh,std::vector<Ogre::Vector3>& out);
/**
The same as getMeshPoints, but now take a LOD level to build the vertices vector.\n
use: ConvexShape generation
@see getMeshPoints
*/
void getMeshPointsLOD(Ogre::Mesh* mesh,std::vector<Ogre::Vector3>& out, Ogre::ushort LOD);

} // namespace MESHUTIL


.cpp
namespace MESHUTIL
{
//-----------------------------------------------------------------------
void getVertex( const Ogre::Mesh* const mesh,
const Ogre::Vector3 &position,
const Ogre::Quaternion &orient,
const Ogre::Vector3 &scale,
std::vector<Ogre::Vector3>& output,
bool removeRepeated)
{
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]);
if (removeRepeated)
{
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));
}
}
else
{
output.push_back((orient * (pt * scale)) + position);
}
}

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

//-----------------------------------------------------------------------
void getMeshPoints(Ogre::Mesh* mesh,std::vector<Ogre::Vector3>& out)
{
BENGINE::MESHUTIL::getVertex(mesh,
Ogre::Vector3::ZERO,Ogre::Quaternion::IDENTITY,
Ogre::Vector3(1,1,1),
out,
true);


//return out;

}

//-----------------------------------------------------------------------
void getMeshPointsLOD(Ogre::Mesh* mesh,std::vector<Ogre::Vector3>& out, Ogre::ushort LOD)
{

Ogre::MeshLodUsage meshLod = mesh->getLodLevel(LOD);
Ogre::EdgeData* edgedata = meshLod.edgeData;
Ogre::EdgeData::TriangleList trilist =edgedata->triangles;

std::vector<Ogre::Vector3> allVextex;
BENGINE::MESHUTIL::getVertex(mesh,
Ogre::Vector3::ZERO,Ogre::Quaternion::IDENTITY,
Ogre::Vector3(1,1,1),
allVextex,
false);

for (unsigned int i=0; i< trilist.size(); i++)
{
for (int z=0; z < 3; ++z)
{
Ogre::Vector3 pt=allVextex[trilist[i].vertIndex[z]];

bool add=true;
for(size_t x=0; x<out.size();++x)
{
if ( out[x] == pt )
add=false;
}

if ( add )
{
out.push_back(pt);
// OgreLog(Ogre::String("vertex added: ")+Ogre::StringConverter::toString(pt));
}
}
}

OgreLog(Ogre::String("printed"));
}

//-----------------------------------------------------------------------
}



sorry to forget it last time ^^ one year past but I'm back in game developing.