///////////////////////////////////////////////////////////////////////////////
// Doug Macdonald
// http://www.dougwillsave.us
// 
// Cooking Interface: Uses PhysX to cook a vertex data generated from a *.FBX
// file into physics data useable during PhysX simulation. Part of a model
// exporting tool developed for a senior game project at DigiPen Institute
// of Technology.
//
// © 2009 DigiPen (USA) Corporation
///////////////////////////////////////////////////////////////////////////////

void Cooker::CookTriangleMesh(int numVertices, void *Vertices, int numTriangles, void *Triangles, std::string filename)
{
  bool init = CookingInterface->NxInitCooking();
  FSASSERT(init, "Initializing cooking triangle mesh failed.");

  NxTriangleMeshDesc mesh;
  mesh.numVertices	= numVertices;
  mesh.points = Vertices;
  mesh.numTriangles = numTriangles/3;
  mesh.triangles = Triangles;
  mesh.pointStrideBytes	= sizeof(NxVec3);
  mesh.triangleStrideBytes = 3 * sizeof(NxU32);
  mesh.flags = 0;

  UserStream stream(filename, false);
  bool cooked = CookingInterface->NxCookTriangleMesh(mesh, stream);
  FSASSERT(cooked, "Cooking the triangle mesh failed.");
  NxCloseCooking();
}

void Cooker::CookConvexMesh(int numVertices, void* Vertices, std::string filename)
{
  bool init = CookingInterface->NxInitCooking();
  FSASSERT(init, "Initializing cooking convex mesh failed.");

  NxConvexMeshDesc mesh;
  mesh.numVertices	= numVertices;
  mesh.points = Vertices;
  mesh.pointStrideBytes	= sizeof(NxVec3);
  mesh.flags = NX_CF_COMPUTE_CONVEX;

  UserStream stream(filename, false);
  bool cooked = CookingInterface->NxCookConvexMesh(mesh, stream);
  FSASSERT(cooked, "Cooking the triangle mesh failed.");
  NxCloseCooking();
}

NxTriangleMesh* Cooker::LoadTriangleMesh(std::string filename)
{
  return FSPhysX.gPhysicsSDK->createTriangleMesh(UserStream(filename, true));
}

NxConvexMesh* Cooker::LoadConvexMesh(std::string filename)
{
  return FSPhysX.gPhysicsSDK->createConvexMesh(UserStream(filename, true));
}
