   CookTriangleMesh Always Returns FASLE!!
  Hi, i am very angry, lot of time wasted with this.I want to Crete a NxCookTriangleMesh from Ageia PhysX, i pass all to the NxTriangleMeshDesc structure andalways return False:My code for Get the Vertices and the Index:void CVertex::GetModelVertexIndex(MODEL *Model,LPCTSTR nombre,LPDIRECT3DDEVICE9 d3ddev){ID3DXMesh *pMesh; // Hold the Tmp Mesh InfoBYTE *ptr=NULL;;NxVec3 vertex;DWORD numVerts;DWORD numFaces;DWORD fvf;DWORD vertSize;D3DXLoadMeshFromX(nombre,  D3DPOOL_MANAGED,  d3ddev,  NULL,  NULL,  NULL,  NULL, &pMesh);pMesh->LockVertexBuffer(0, (LPVOID*)&ptr);Model->numVertex = numVerts = pMesh->GetNumVertices();Model->numFaces = numFaces = pMesh->GetNumFaces();fvf = pMesh->GetFVF();vertSize = D3DXGetFVFVertexSize(fvf);Model->verts = new NxVec3[numVerts];Model->faces = new NxU32[numFaces*3];for(DWORD i=0;i<numVerts;i++){NxVec3 *vPtr=(NxVec3 *) ptr;*vPtr+=vertex;Model->verts.x = vPtr->x;Model->verts.y = vPtr->y;Model->verts.z = vPtr->z;ptr+=vertSize;}pMesh->UnlockVertexBuffer();pMesh->LockIndexBuffer(0,(void**)&Model->faces);pMesh->UnlockIndexBuffer();pMesh->Release();return;}And the fill of the structure:NxTriangleMeshDesc terrainDesc;terrainDesc.numVertices= Model->numVertex;terrainDesc.numTriangles= Model->numFaces;terrainDesc.pointStrideBytes= sizeof(NxVec3);terrainDesc.triangleStrideBytes= 3*sizeof(NxU32);terrainDesc.points= Model->verts;terrainDesc.triangles= Model->faces;terrainDesc.flags = 0;   NxInitCooking();MemoryWriteBuffer buf;    bool status = CookTriangleMesh(terrainDesc, buf);Status always returns False, i dont know what its wrong,Model->verts its a NxVec3* and Model->faces its a NxU32*,this its a nightmare :'(...thanks a Lot[Edited by - _Camus_ on June 5, 2008 4:18:37 AM]
  Hi Mr AngryFirst, be prepared to 'waste' lots of time...there will always be something to keep you on your toes.  Passion is good, but due to international language barriers, you may well find that not everyone is quite so understanding ;)A few things stand out in your code.  You say that you want to cook a convex mesh, yet you pass it through a triangle mesh desc?  If your model is convex then you need to pass it through NxConvexShapeDesc, and not NxTriangleMeshDescI also noticed that you are not setting any flags.For convex you need to pass NX_CF_COMPUTE_CONVEXConvex is also limited with number of verts/faces...you really need to read up on all of this.  And examine the lessons (sdk 2.7.3 or earlier), or the samples (current).Take some more time to understanding which type of shape will best suit your model data.  And have fun =]   ;  Thanks for reply and the comment, sorry about that, i am over a contest and have no much time, and i think i found the first problem, i downloaded the2.8.1, this distribution dosnt have tutorials or something, now i am noticedthat 2.7.3 do have it, i ll read to much.Now, the mesh that im trying to load its a box, not a iregular mesh, my first desire was a terrain, but i chamged to a box to have less problemsit seems i have to read very deep in the tutorials/sample code.Thanks for your help, ill try to calm down xD.cheersEDIT:Well, i found what the best shape for my need in fact its the trianglemesh, coz my .X its a custom Terrain made on 3dsmax,i export with Kw-xporter, and i search over and over the weband its the best choice. I am a noob and say in the first post: "I want to Crete a NxCookConvexMesh"In fact i want to create a NxCookTriangleMesh,sorry about that. Now with this new information,whats wrong ? I see the code again and again, changelittle thins but nothing!BTW i see other topics with this problem, or similar caseslike this:http://www.gamedev.net/community/forums/topic.asp?topic_id=453916&whichpage=1&Its a SDK bug? or my code its wrong, i tried with 16bit index and nothing.Thanks Again![Edited by - _Camus_ on June 5, 2008 4:45:08 AM];  I see some potential problems in your code. They all have to do with your use of the Direct3D API and not your use of PhysX:Quote:Original post by _Camus_for(DWORD i=0;i<numVerts;i++){NxVec3 *vPtr=(NxVec3 *) ptr;*vPtr+=vertex;Model->verts.x = vPtr->x;Model->verts.y = vPtr->y;Model->verts.z = vPtr->z;ptr+=vertSize;}I don't know why you're doing "*vPtr+=vertex". I don't see that you have initialized vertex. The x,y,z values it stores may be undefined #'s. Here you are adding vertex.x,y,z to the current vPtr->x,y,z...why? If vertex is uninitilized, all your Model->verts values will be undefined or otherwise wrong. If they're undefined, PhysX will likely choke. This looks like it is probably wrong to me. Take that line out and see what happens.Quote:Original post by _Camus_pMesh->LockIndexBuffer(0,(void**)&Model->faces);pMesh->UnlockIndexBuffer();This looks quite wrong also. I believe that form of LockIndexBuffer gives you a pointer to a D3D-API or driver-managed block of memory that is only valid while the buffer is locked. It looks like you are attempting to use it to copy the buffer contents into your allocated faces array, but instead the code is changing Model->faces to point to the currently locked, driver-managed buffer. And then you immediately unlock the buffer, which will invalidate the pointer you just got. The indices you think you stored in the Model->faces you allocated in fact weren't copied there at all and now are no longer available. Since you use the over-written Model->faces pointer when you call PhysX, PhysX will access unallocated/unavailable memory and probably generates and traps an access violation....even if the vertex coordinates happen to be correct. You need to Lock the index buffer to a new pointer, then memcpy it to Model->faces, or simply loop through as you have with the vertices, before unlocking the index buffer.In fact, the memcpy isn't a good idea, since the DirectX mesh you loaded might use 16 bit buffers while you're sending 32-bit buffers to PhysX....unless you are certain the buffers are compatible, you will simply have to loop through the indices and copy the index buffer to Models->faces 1-by-1.So, I hope that helps. As someone else said, you may need to add a flag to the structure you pass to the PhysX cooking function, but you need to get your Models->verts/faces data filled in properly before PhysX can begin to work properly.;  THANKS, REALLY THANKS FOR YOUR TIME AND YOUR HELP!Ok, I have a lot of things to say:First: The "always returns FASLE" wasnt for the fill itselfI was calling NxInitCooking(); and then call CookTriangleMesh(terrainDesc, buf),i changed it for: NxCookTriangleMesh(terrainDesc,buf);And the program begins to work, also i used a function called isValid().Seccond: grhodes_at_work thanks for that one, the index buffer was terriblewrong, and that vertex was pure trash xD, then i restructure the code forthat and now everything works fine!I want to share my working code now for those people what are looking on everywhere and dosnt find nothing about the implementation of TriangleMeshwith .X meshes:The Code of the gather function:void CVertex::Vertex(MODEL *Model,LPCTSTR nombre,LPDIRECT3DDEVICE9 d3ddev){ID3DXMesh *pMesh;BYTE *ptr=NULL;BYTE *lpIB;LPDIRECT3DINDEXBUFFER9 lpIndexBuffer;D3DINDEXBUFFER_DESC ibdesc;int numIndices;DWORD numVerts;DWORD numFaces;DWORD fvf;DWORD vertSize;int count=0;D3DXLoadMeshFromX(nombre,  D3DPOOL_MANAGED,  d3ddev,  NULL,  NULL,  NULL,  NULL,  &pMesh);pMesh->LockVertexBuffer(0, (LPVOID*)&ptr);Model->numVertex = numVerts = pMesh->GetNumVertices();Model->numFaces = numFaces = pMesh->GetNumFaces();fvf = pMesh->GetFVF();vertSize = D3DXGetFVFVertexSize(fvf);Model->verts = new NxVec3[numVerts];for(DWORD i=0;i<numVerts;i++){NxVec3 *vPtr=(NxVec3 *) ptr;Model->verts.x = vPtr->x;Model->verts.y = vPtr->y;Model->verts.z = vPtr->z;ptr+=vertSize;}pMesh->UnlockVertexBuffer();pMesh->LockIndexBuffer ( D3DLOCK_READONLY, (VOID**)&lpIB); pMesh->GetIndexBuffer( &lpIndexBuffer );lpIndexBuffer->GetDesc( &ibdesc );if(ibdesc.Format == D3DFMT_INDEX32){numIndices = ibdesc.Size / sizeof(DWORD); Model->tri32 = new NxU32[numFaces*3]; Model->b_32 = true; }else{numIndices = ibdesc.Size / sizeof(WORD);Model->tri16 = new NxU16[numFaces*3];Model->b_32 = false;}for(int i=0; i<numIndices; i += 3 ){if (ibdesc.Format == D3DFMT_INDEX32  )    {Model->tri32[count] = ((DWORD*)lpIB);<br>count++;<br>Model-&gt;tri32[count] = ((DWORD*)lpIB)[ i+1 ];<br>count++;<br>Model-&gt;tri32[count] = ((DWORD*)lpIB)[ i+2 ];<br>count++;<br>    }<br>else<br>{Model-&gt;tri16[count] = ((WORD*)lpIB);<br>count++;<br>Model-&gt;tri16[count] = ((WORD*)lpIB)[ i+1 ];<br>count++;<br>Model-&gt;tri16[count] = ((WORD*)lpIB)[ i+2 ];<br>count++;<br>}<br>}<br><br>pMesh-&gt;UnlockIndexBuffer();<br>pMesh-&gt;Release();<br>}<br></pre><br><br><br>Now the fill of the structure:<br><br><pre><br>NxTriangleMeshDesc terrainDesc;<br>terrainDesc.numVertices = Model-&gt;numVertex;<br>terrainDesc.numTriangles= Model-&gt;numFaces;<br>terrainDesc.pointStrideBytes = sizeof(NxVec3);<br>if(Model-&gt;b_32)<br>{terrainDesc.triangleStrideBytes = 3*sizeof(NxU32);}<br>else{terrainDesc.triangleStrideBytes  =3*sizeof(NxU16);}<br>terrainDesc.points= Model-&gt;verts;<br>if(Model-&gt;b_32)<br>{terrainDesc.triangles   = Model-&gt;tri32;}<br>else{terrainDesc.triangles = Model-&gt;tri16;}<br>terrainDesc.flags   = Model-&gt;b_32 ? 0 : NX_MF_16_BIT_INDICES;<br><br>terrainDesc.heightFieldVerticalAxis= NX_Y;<br>terrainDesc.heightFieldVerticalExtent= -1000.0f;<br><br>NxInitCooking();<br>MemoryWriteBuffer buf;<br>boolstatus = NxCookTriangleMesh(terrainDesc,buf);<br><br>MemoryReadBuffer readBuffer(buf.data);<br>terrainMesh = gPhysicsSDK-&gt;createTriangleMesh(readBuffer);<br></pre><br><br>Note this is FOR TERRAIN &#79;NLY, for other meshes we have to do little <br>changes.<br><br>Again thanks for Help.<br><br>PD- Some Moderator can put in code blocks the code?<br><br>cheers<br><br>[modified by grhodes_at_work to wrap source in "code" blocks]<br><br><!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - grhodes_at_work on June 6, 2008 12:16:27 PM]<!–EDIT–></span><!–/EDIT–>;  Glad you got it working! Thanks for the followup!