Problem with candy wrapper

randomcode

16-11-2016 05:10:12

Hey, guys. I want to create the physx cloth with Physx Candy Wrapper, but i meet problems:
here is my code:

class TestCloth
{
public TestCloth(Scene scene, ClothDesc clothdesc, float width, float height, float length, string txtFileName, bool tearLines)
{
ClothMeshDesc desc=new ClothMeshDesc();
generateRegularMeshDesc(desc, width, height, length, true, false);
}
private void generateRegularMeshDesc(ClothMeshDesc desc,float w,float h,float d,bool txtCoods,bool tearLines)
{
int numX = (int)(w / d) + 1;
int numY = (int)(h / d) + 1;

desc.VertexCount = ((uint)numX+1)*((uint)numY+1);
desc.TriangleCount = (uint)(numX * numY) * 2;
desc.PointsByteStride = (uint)Marshal.SizeOf(new Vector3());
desc.TrianglesByteStride = (uint)sizeof(UInt32);
desc.VertexMassesByteStride = (uint)sizeof(float);
desc.VertexFlagsByteStride = (uint)sizeof(UInt32);
desc. = Marshal.AllocHGlobal((int)(Marshal.SizeOf(new Vector3()) * desc.VertexCount));
desc.TrianglesPtr = Marshal.AllocHGlobal((int)(sizeof(UInt32)*desc.TriangleCount));
//desc.VertexMassesPtr = mas;
int i, j;
Vector3 p = (Vector3)desc.PointsPtr;
for (i = 0; i <= numY; i++)
{
for (j = 0; j <= numX; j++)
{

p.(d * j, 0.0f, d * i);
p++;
}
}
}
}


original c++ code:

void MyCloth::generateRegularMeshDesc(NxClothMeshDesc &desc, NxReal w, NxReal h, NxReal d, bool texCoords, bool tearLines)
{
int numX = (int)(w / d) + 1;
int numY = (int)(h / d) + 1;

desc.numVertices = (numX+1) * (numY+1);
desc.numTriangles = numX*numY*2;
desc.pointStrideBytes = sizeof(NxVec3);
desc.triangleStrideBytes = 3*sizeof(NxU32);
desc.vertexMassStrideBytes = sizeof(NxReal);
desc.vertexFlagStrideBytes = sizeof(NxU32);
desc.points = (NxVec3*)malloc(sizeof(NxVec3)*desc.numVertices);
desc.triangles = (NxU32*)malloc(sizeof(NxU32)*desc.numTriangles*3);
desc.vertexMasses = 0;
desc.vertexFlags = 0;
desc.flags = 0;

mMaxVertices = TEAR_MEMORY_FACTOR * desc.numVertices;
mMaxIndices = 3 * desc.numTriangles;

int i,j;
NxVec3 *p = (NxVec3*)desc.points;
for (i = 0; i <= numY; i++) {
for (j = 0; j <= numX; j++) {
p->set(d*j, 0.0f, d*i);
p++;
}
}

if (texCoords) {
mTempTexCoords = (GLfloat *)malloc(sizeof(GLfloat)*2*TEAR_MEMORY_FACTOR*desc.numVertices);
GLfloat *f = mTempTexCoords;
GLfloat dx = 1.0f; if (numX > 0) dx /= numX;
GLfloat dy = 1.0f; if (numY > 0) dy /= numY;
for (i = 0; i <= numY; i++) {
for (j = 0; j <= numX; j++) {
*f++ = j*dx;
*f++ = i*dy;
}
}
mNumTempTexCoords = desc.numVertices;
}
else
{
mNumTempTexCoords = 0;
mTempTexCoords = NULL;
}

NxU32 *id = (NxU32*)desc.triangles;
for (i = 0; i < numY; i++) {
for (j = 0; j < numX; j++) {
NxU32 i0 = i * (numX+1) + j;
NxU32 i1 = i0 + 1;
NxU32 i2 = i0 + (numX+1);
NxU32 i3 = i2 + 1;
if ((j+i)%2) {
*id++ = i0; *id++ = i2; *id++ = i1;
*id++ = i1; *id++ = i2; *id++ = i3;
}
else {
*id++ = i0; *id++ = i2; *id++ = i3;
*id++ = i0; *id++ = i3; *id++ = i1;
}
}
}

// generate tear lines if necessary
if(tearLines)
generateTearLines(desc, numX + 1, numY + 1);
}


i can't convert intptr to vector3, and i didn't find any property in "desc" has the Vector3 return value, what should i do?






and no gravity!!!!!! i use scenedesc.Gravity = new Mogre.Vector3(0, -9.8f, 0);, but the gravity can't work!!!!!!!!!!!!

randomcode

17-11-2016 05:51:44

Update: i have created cloth successfully, but new problem comes, the cloth i create can't display, here is my new code:

public void makeCloth()
{
using (var cd = new ClothDesc())
{
MeshManager.Singleton.CreatePlane("flag",
ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, new Plane(Vector3.UNIT_Y, 0), 8, 1, 1, 1, true, 1, 1, 1, Vector3.UNIT_Z);
Entity entFlag = m_SceneMgr.CreateEntity("Flag", "flag");
MeshPtr mp = entFlag.GetMesh();
StaticMeshData meshdata = new StaticMeshData(mp);
cd.ClothMesh = CookClothMesh(meshdata);

cd.BendingStiffness = cd.StretchingStiffness = 0.1f;
cd.GlobalPose = Matrix4.GetTrans(new Vector3(0, 0, 0));
cd.Flags |= ClothFlags.Gravity | ClothFlags.Visualization;

c = scene.CreateCloth(cd);
}
}

private ClothMesh CookClothMesh(StaticMeshData meshdata)
{
CookingInterface.InitCooking();
using (var cd = new ClothMeshDesc())
{
cd.PinPoints<float>(meshdata.Points, 0, 12);
cd.PinTriangles<uint>(meshdata.Indices, 0, 12);

cd.VertexCount = 4;
cd.TriangleCount = 2;
ClothMesh cookedcm = null;
using (System.IO.MemoryStream str = new System.IO.MemoryStream(1024))
{
if (CookingInterface.CookClothMesh(cd, str))
{
str.Seek(0, System.IO.SeekOrigin.Begin);
cookedcm = physx.CreateClothMesh(str);
CookingInterface.CloseCooking();
}
return cookedcm;
}
}
}



@TADS