what's the problem...

hbd760112

06-10-2008 19:45:00




bool InitCooking(NxUserAllocator* allocator=0, NxUserOutputStream* outputStream=0)
{
#ifdef COOKING_INTERFACE
hasCookingLibrary();
if ( !gCooking ) return false;
return gCooking->NxInitCooking(allocator, outputStream);
#else
return NxInitCooking(allocator, outputStream);
#endif
}

void createOgreTriangleMesh(char* filename)
{
int vertex_count =0;
int index_count = 0;

bool added_shared = false;
size_t current_offset = vertex_count;
size_t shared_offset = vertex_count;
size_t next_offset = vertex_count;
size_t index_offset = index_count;
size_t prev_vert = vertex_count;
size_t prev_ind = index_count;

// Ogre Load mesh
SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
char name[256];
sprintf(name,"%s %d",filename,instanceId);
Entity* ent = mSceneMgr->createEntity(name,filename);
node->attachObject( ent);
MeshPtr mesh = ent->getMesh();

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

// We only need to add the shared vertices once
if(submesh->useSharedVertices)
{
if(!added_shared)
{
VertexData* vertex_data = mesh->sharedVertexData;
vertex_count += vertex_data->vertexCount;
added_shared = true;
}
}
else
{
VertexData* vertex_data = submesh->vertexData;
vertex_count += vertex_data->vertexCount;
}

// Add the indices
Ogre::IndexData* index_data = submesh->indexData;
index_count += index_data->indexCount;
}


Vector3 *vertices = new Vector3[vertex_count];
unsigned *indices = new unsigned[index_count];
added_shared = false;
// Run through the submeshes again, adding the data into the arrays
int h = mesh->getNumSubMeshes();
for(int i = 0;i < mesh->getNumSubMeshes();i++)
{
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));
Ogre::Real* pReal;

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

Vector3 pt;

pt.x = (*pReal++);
pt.y = (*pReal++);
pt.z = (*pReal++);

//pt = (orient * (pt * scale)) + position;

vertices[current_offset + j].x = pt.x;
vertices[current_offset + j].y = pt.y;
vertices[current_offset + j].z = pt.z;
}
vbuf->unlock();
next_offset += vertex_data->vertexCount;
}

Ogre::IndexData* index_data = submesh->indexData;

size_t numTris = index_data->indexCount / 3;
unsigned short* pShort;
unsigned int* pInt;
Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
bool use32bitindexes = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
if (use32bitindexes) pInt = static_cast<unsigned int*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
else pShort = static_cast<unsigned short*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
for(size_t k = 0; k < numTris; ++k)
{
size_t offset = (submesh->useSharedVertices)?shared_offset:current_offset;

unsigned int vindex = use32bitindexes? *pInt++ : *pShort++;
indices[index_offset + 0] = vindex + offset;
vindex = use32bitindexes? *pInt++ : *pShort++;
indices[index_offset + 1] = vindex + offset;
vindex = use32bitindexes? *pInt++ : *pShort++;
indices[index_offset + 2] = vindex + offset;

index_offset += 3;
}
ibuf->unlock();
current_offset = next_offset;
}

// Build physical model
NxTriangleMeshDesc terrainDesc;
terrainDesc.numVertices = vertex_count;
terrainDesc.numTriangles = index_count/3;
terrainDesc.pointStrideBytes = sizeof(Vector3);
terrainDesc.triangleStrideBytes = 3*sizeof(NxU32);
terrainDesc.points = vertices;
terrainDesc.triangles = indices;
terrainDesc.flags = 0;

terrainDesc.heightFieldVerticalAxis = NX_Y;
//terrainDesc.heightFieldVerticalExtent = -1000.0f;



MemoryWriteBuffer buf;
bool status = CookTriangleMesh(terrainDesc, buf);
if (!status) {
printf("Unable to cook a triangle mesh.");
exit(1);
}
MemoryReadBuffer readBuffer(buf.data);
NxTriangleMesh* terrainMesh = gPhysicsSDK->createTriangleMesh(readBuffer);

//
// Please note about the created Triangle Mesh, user needs to release it when no one uses it to save memory. It can be detected
// by API "meshData->getReferenceCount() == 0". And, the release API is "gPhysicsSDK->releaseTriangleMesh(*meshData);"
//
int in = terrainMesh->getReferenceCount();
NxTriangleMeshShapeDesc terrainShapeDesc;
terrainShapeDesc.meshData = terrainMesh;
terrainShapeDesc.shapeFlags = NX_SF_FEATURE_INDICES;

NxActorDesc ActorDesc;
ActorDesc.shapes.pushBack(&terrainShapeDesc);
NxActor *gTerrain = gScene->createActor(ActorDesc);
gTerrain->userData = (void*)0;

CloseCooking();


}

void CloseCooking()
{
#ifdef COOKING_INTERFACE
if ( !gCooking ) return;
gCooking->NxCloseCooking();
#else
return NxCloseCooking();
#endif
}



I've initialize cooking in InitNx()
There is no error dialog.
However, the box falling on the trianglemesh doesn't correctly response.
What's wrong?
Should I do some thing in 3d Max?
I use ogremax to export mesh

Thanks

betajaen

06-10-2008 20:02:08

I assume your not using NxOgre (for reasons I don't understand) but are your normals facing the right way? Your mesh is obviously been generated; PhysX usually crashes or returns a NULL pointer on a bad mesh. Have you tried "looking" at in the PhysX remote debugger?

Prophet

06-10-2008 21:13:31

Center the pivot point.