RadixSort::~RadixSort() { // Release everything if(mDeleteRanks) { MEMALLOC_FREE(mRanks2); MEMALLOC_FREE(mRanks); } }
bool RadixSort::Resize(NxU32 nb) { if(mDeleteRanks) { // Free previously used ram MEMALLOC_FREE(mRanks2); MEMALLOC_FREE(mRanks); // Get some fresh one mRanks = (NxU32*)MEMALLOC_MALLOC(sizeof(NxU32)*nb); mRanks2 = (NxU32*)MEMALLOC_MALLOC(sizeof(NxU32)*nb); } return true; }
virtual bool sendWinMsgBinary(const char *app,const char *blobType,const void *mem,NxU32 len) { bool ret = false; #ifdef WIN32 #ifdef UNICODE wchar_t wapp[512]; mbstowcs(wapp,app,512); HWND targetwindow = ::FindWindow(L"WinMsg", wapp ); #else HWND targetwindow = ::FindWindow("WinMsg", app ); #endif if ( targetwindow ) { ret = true; NxU32 olen; void *omem = getDataBlob("WinMsgBlob",blobType,mem,len,olen); COPYDATASTRUCT copydata; copydata.dwData = 0; copydata.lpData = (PVOID)omem; copydata.cbData = olen; ::SendMessage(targetwindow, WM_COPYDATA, (WPARAM)0, (LPARAM)©data); MEMALLOC_FREE(omem); } #endif return ret; }
~_FILE_INTERFACE(void) { if ( mMyAlloc ) { MEMALLOC_FREE(mData); MemoryBlock *mb = mHeadBlock; while ( mb ) { MemoryBlock *next = mb->mNextBlock; delete mb; mb = next; } } if ( mFph ) { fclose(mFph); } }
virtual bool sendWinMsg(const char *app,const char *fmt,...) { bool ret = false; #ifdef WIN32 #ifdef UNICODE wchar_t wapp[512]; mbstowcs(wapp,app,512); HWND targetwindow = ::FindWindow(L"WinMsg", wapp ); #else HWND targetwindow = ::FindWindow("WinMsg", app ); #endif if ( targetwindow ) { ret = true; char buffer[8192]; _vsnprintf(buffer, 8192, fmt, (char *)(&fmt+1)); NxU32 len = (NxU32)strlen(buffer); NxU32 olen; void *mem = getDataBlob("WinMsg","Text",buffer,len+1,olen); COPYDATASTRUCT copydata; copydata.dwData = 0; copydata.lpData = mem; copydata.cbData = olen; ::SendMessage(targetwindow, WM_COPYDATA, (WPARAM)0, (LPARAM)©data); MEMALLOC_FREE(mem); } #endif return ret; }
~MemoryBlock(void) { MEMALLOC_FREE(mMemory); }
~WindowsMessage(void) { if (mType) MEMALLOC_FREE(mType); if ( mMessage ) MEMALLOC_FREE(mMessage); }
// warning, this code will not work on big endian processors. virtual bool importMesh(const char *_meshName,const void *_data,NxU32 dlen,MeshImportInterface *callback,const char *options,MeshImportApplicationResource *appResource) { bool ret = false; const char *meshName = _meshName; const char *slash = lastSlash(meshName); if ( slash ) { slash++; meshName = slash; } char scratch[2048]; strncpy(scratch,_meshName,2048); char *psk = stristr(scratch,".psk"); if ( psk ) { *psk = 0; strcat(scratch,".psa"); } callback->importAssetName(meshName,0); callback->importMesh(meshName,meshName); // have to make a local copy of the data because we are going to modify some of it. // Primarily just truncating some whitespace from bone names. void *data = MEMALLOC_MALLOC(dlen); memcpy(data,_data,dlen); if ( appResource ) { NxU32 len; void *mem = appResource->getApplicationResource(meshName,scratch,len); if ( mem ) { char *scan = ( char *)mem; Header *baseHeader = ( Header *)scan; scan+=sizeof(Header); Header *bonesHeader = ( Header *)scan; scan+=sizeof(Header); Bone *bones = ( Bone *)scan; assert(sizeof(Bone)==bonesHeader->mLen); scan+=(bonesHeader->mLen*bonesHeader->mCount); Header *animInfo = ( Header *)scan; scan+=sizeof(Header); assert( animInfo->mLen == sizeof(AnimInfo)); AnimInfo *ainfo = ( AnimInfo *)scan; scan+=(animInfo->mLen*animInfo->mCount); Header *animKeysHeader = ( Header *)scan; scan+=sizeof(Header); assert( animKeysHeader->mLen == sizeof(AnimKey) ); AnimKey *keys = ( AnimKey *)scan; scan+=(animKeysHeader->mLen*animKeysHeader->mCount); Header *scaleKeysHeader = ( Header *)scan; scan+=sizeof(Header); MeshAnimation ma; ma.mName = ainfo->mName; ma.mTrackCount = ainfo->mTotalBones; ma.mFrameCount = ainfo->mNumRawFrames; ma.mDtime = 1.0f / (NxF32)(ainfo->mAnimRate); ma.mDuration = ma.mDtime*ainfo->mNumRawFrames; ma.mTracks = (MeshAnimTrack **)MEMALLOC_MALLOC(sizeof(MeshAnimTrack *)*ma.mTrackCount); for (NxI32 i=0; i<ma.mTrackCount; i++) { Bone &b = bones[i]; fixName(b.mName); MeshAnimTrack *track = MEMALLOC_NEW(MeshAnimTrack); ma.mTracks[i] = track; track->mName = b.mName; track->mFrameCount = ma.mFrameCount; track->mDuration = ma.mDuration; track->mDtime = ma.mDtime; track->mPose = MEMALLOC_NEW(MeshAnimPose)[track->mFrameCount]; for (NxI32 j=0; j<ma.mFrameCount; j++) { NxU32 index = (j*ma.mTrackCount)+i; AnimKey &key = keys[index]; MeshAnimPose &p = track->mPose[j]; p.mPos[0] = key.mPosition[0]*IMPORT_SCALE; #if HACK_ANIMATION_POSITION p.mPos[1] = -key.mPosition[1]*IMPORT_SCALE; #else p.mPos[1] = key.mPosition[1]*IMPORT_SCALE; #endif p.mPos[2] = key.mPosition[2]*IMPORT_SCALE; p.mQuat[0] = key.mOrientation[0]; #if HACK_ANIMATION_ROTATION p.mQuat[1] = -key.mOrientation[1]; #else p.mQuat[1] = key.mOrientation[1]; #endif p.mQuat[2] = key.mOrientation[2]; p.mQuat[3] = key.mOrientation[3]; #if HACK_ANIMATION_ROOT if ( i == 0) { p.mQuat[3] = -p.mQuat[3]; } #endif index++; } } callback->importAnimation(ma); for (NxI32 i=0; i<ma.mTrackCount; i++) { MeshAnimTrack *track = ma.mTracks[i]; delete []track->mPose; delete track; } delete []ma.mTracks; appResource->releaseApplicationResource(mem); } } char *scan = ( char *)data; char *end = scan+dlen; Header *baseHeader = ( Header *)scan; scan+=sizeof(Header); // skip first dummy header Header *h = 0; Header *positionsHeader = h = (Header *)scan; assert( positionsHeader->mLen == sizeof(Vector)); scan+=sizeof(Header); Vector *positions = ( Vector *)scan; scan+=h->mLen*h->mCount; #if HACK_MESH_POSITION for (NxI32 i=0; i<positionsHeader->mCount; i++) { Vector &v = positions[i]; v.y*=-1; // flip the Y-coordinate } #endif Header *verticesHeader = h = ( Header *)scan; assert( verticesHeader->mLen == sizeof(Vertex)); scan+=sizeof(Header); Vertex *vertices = ( Vertex *)scan; scan+=h->mLen*h->mCount; Header *trianglesHeader = h = ( Header *)scan; assert( trianglesHeader->mLen == sizeof(Triangle)); scan+=sizeof(Header); Triangle *triangles = ( Triangle *)scan; scan+=h->mLen*h->mCount; Header *materialsHeader = h = ( Header *)scan; assert( materialsHeader->mLen == sizeof(Material)); scan+=sizeof(Header); Material *materials= ( Material *)scan; scan+=h->mLen*h->mCount; Header *bonesHeader = h = ( Header *)scan; assert( bonesHeader->mLen == sizeof(Bone)); scan+=sizeof(Header); Bone *bones= ( Bone *)scan; scan+=h->mLen*h->mCount; Header *boneInfluencesHeader = h = ( Header *)scan; assert( boneInfluencesHeader->mLen == sizeof(BoneInfluence)); scan+=sizeof(Header); BoneInfluence *boneInfluences = ( BoneInfluence *)scan; scan+=h->mLen*h->mCount; if ( bonesHeader->mCount > 0 ) { MeshSkeleton *ms = MEMALLOC_NEW(MeshSkeleton); ms->mName = meshName; ms->mBoneCount = bonesHeader->mCount; ms->mBones = MEMALLOC_NEW(MeshBone)[ms->mBoneCount]; for (NxI32 i=0; i<ms->mBoneCount; i++) { MeshBone &dest = ms->mBones[i]; Bone &src = bones[i]; fixName(src.mName); dest.mName = src.mName; dest.mParentIndex = (i==0) ? -1 : src.mParentIndex; dest.mPosition[0] = src.mPosition[0]*IMPORT_SCALE; #if HACK_SKELETON_POSITION dest.mPosition[1] = -src.mPosition[1]*IMPORT_SCALE; #else dest.mPosition[1] = src.mPosition[1]*IMPORT_SCALE; #endif dest.mPosition[2] = src.mPosition[2]*IMPORT_SCALE; dest.mOrientation[0] = src.mOrientation[0]; #if HACK_SKELETON_ROTATION dest.mOrientation[1] = -src.mOrientation[1]; #else dest.mOrientation[1] = src.mOrientation[1]; #endif dest.mOrientation[2] = src.mOrientation[2]; dest.mOrientation[3] = src.mOrientation[3]; #if HACK_SKELETON_ROOT if ( i == 0 ) { dest.mOrientation[3]*=-1; } #endif dest.mScale[0] = 1; //src.mXSize; dest.mScale[1] = 1; //src.mYSize; dest.mScale[2] = 1; //src.mZSize; } callback->importSkeleton(*ms); delete []ms->mBones; delete ms; } assert( scan == end ); DeformVector *dvertices = MEMALLOC_NEW(DeformVector)[positionsHeader->mCount]; for (NxI32 i=0; i<boneInfluencesHeader->mCount; i++) { BoneInfluence &b = boneInfluences[i]; DeformVector &d = dvertices[b.mVertexIndex]; if ( d.mCount < 4 ) { d.mWeight[d.mCount] = b.mWeight; d.mBone[d.mCount] = (NxU16)b.mBoneIndex; d.mCount++; } } Vector *normals = (Vector *)MEMALLOC_MALLOC( sizeof(Vector)*positionsHeader->mCount); memset(normals,0,sizeof(Vector)*positionsHeader->mCount); for (NxI32 i=0; i<trianglesHeader->mCount; i++) { Triangle &t = triangles[i]; MeshVertex mv1,mv2,mv3; Vertex &v1 = vertices[t.mWedgeIndex[0]]; Vertex &v2 = vertices[t.mWedgeIndex[1]]; Vertex &v3 = vertices[t.mWedgeIndex[2]]; Vector &p1 = positions[v1.mIndex]; Vector &p2 = positions[v2.mIndex]; Vector &p3 = positions[v3.mIndex]; Vector &n1 = normals[v1.mIndex]; Vector &n2 = normals[v2.mIndex]; Vector &n3 = normals[v3.mIndex]; Vector normal; fm_computePlane( &p1.x, &p3.x, &p2.x, &normal.x ); n1.x+=normal.x; n1.y+=normal.y; n1.z+=normal.z; n2.x+=normal.x; n2.y+=normal.y; n2.z+=normal.z; n3.x+=normal.x; n3.y+=normal.y; n3.z+=normal.z; } for (NxI32 i=0; i<positionsHeader->mCount; i++) { Vector &n = normals[i]; fm_normalize(&n.x); } for (NxI32 i=0; i<trianglesHeader->mCount; i++) { Triangle &t = triangles[i]; MeshVertex mv1,mv2,mv3; Vertex &v1 = vertices[t.mWedgeIndex[0]]; Vertex &v2 = vertices[t.mWedgeIndex[1]]; Vertex &v3 = vertices[t.mWedgeIndex[2]]; Vector &p1 = positions[v1.mIndex]; Vector &p2 = positions[v2.mIndex]; Vector &p3 = positions[v3.mIndex]; Vector &n1 = normals[v1.mIndex]; Vector &n2 = normals[v2.mIndex]; Vector &n3 = normals[v3.mIndex]; DeformVector &dv1 = dvertices[v1.mIndex]; DeformVector &dv2 = dvertices[v2.mIndex]; DeformVector &dv3 = dvertices[v3.mIndex]; getVertex(mv1,p1,v1,dv1,n1); getVertex(mv2,p2,v2,dv2,n2); getVertex(mv3,p3,v3,dv3,n3); const char *material = "default"; if ( t.mMaterialIndex >= 0 && t.mMaterialIndex < materialsHeader->mCount ) { material = materials[ t.mMaterialIndex ].mMaterialName; } #if HACK_MESH_WINDING callback->importTriangle(meshName,material, MIVF_ALL, mv1, mv3, mv2 ); #else callback->importTriangle(meshName,material, MIVF_ALL, mv1, mv2, mv3 ); #endif } delete []dvertices; MEMALLOC_FREE(data); return ret; }