//--------------------------------------------------------------------------- int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR cmdline, int iWinMode) { // for determining if there are 'object leaks' unsigned int uiInitialCount = NiRefObject::GetTotalObjectCount(); #ifdef LOG_MEMORY_TRACKING NiInit(); #else NiInitOptions* pkInitOptions = NiExternalNew NiInitOptions( NiExternalNew NiMemTracker(NiExternalNew NiStandardAllocator(), false)); NiInit(pkInitOptions); #endif // If cmdline.txt file exists in the work directory, read it and // set it as the command line NiFile* pkCmdlineFile = NULL; if (!cmdline || strlen(cmdline) == 0) { pkCmdlineFile = NiNew NiFile("./cmdline.txt", NiFile::READ_ONLY); } if (pkCmdlineFile && *pkCmdlineFile) { // artificial 1024-character limit char* pcCmdline = NiAlloc(char, 1024); int iLength = 0; char c; // Read until limit is reached or end of file or end of string while (iLength < 1023) { if (!(pkCmdlineFile->Read(&c, 1) || c == '\0')) break; // Factor out newlines and carriage returns // Replace with ' ' in case newline seperates valid parameters if (c == '\n' || c == '\r') c = ' '; pcCmdline[iLength++] = c; } pcCmdline[iLength] = '\0'; // let the application know about the command line NiApplication::SetCommandLine(pcCmdline); NiFree(pcCmdline); }
//------------------------------------------------------------------------------------------------- bool sdPhysicsSystem::CreateTerrain(sdTerrain* pkTerrain) { NIASSERT(pkTerrain); NIASSERT(m_pkHeightField == NULL) sdHeightMap* pkHeightMap = pkTerrain->GetHeightMap(); NIASSERT(pkHeightMap); uint uiBlockSize = pkHeightMap->GetSize() + 1; ///< 每边顶点数(??为什么加一??) uint uiNumVert = uiBlockSize * uiBlockSize; ///< 总顶点数 float fMinHeight = pkHeightMap->GetMinHeight(); float fMaxHeight = pkHeightMap->GetMaxHeight(); float fMidHeight = (fMaxHeight + fMinHeight) * 0.5f; float fHalfDeltaHeight = (fMaxHeight - fMinHeight) * 0.5f; float fThirtyTwoKb = 32766.f; fHalfDeltaHeight = fabs(fHalfDeltaHeight) < 0.0001f ? 0.0001f : fHalfDeltaHeight; ///< 防止值过小 NxHeightFieldSample* pnxHeightFieldSample = NiAlloc2(NxHeightFieldSample, uiNumVert, NiMemHint::NONE); NIASSERT(pnxHeightFieldSample); // 创建高度域描述 NxHeightFieldDesc nxHeightFieldDesc; nxHeightFieldDesc.nbColumns = uiBlockSize; nxHeightFieldDesc.nbRows = uiBlockSize; nxHeightFieldDesc.convexEdgeThreshold = 0.f; nxHeightFieldDesc.thickness = -1000.f; nxHeightFieldDesc.samples = pnxHeightFieldSample; nxHeightFieldDesc.sampleStride = sizeof(NxU32); uchar* pucData = (uchar*)pnxHeightFieldSample; for (int i = 0; i < (int)uiBlockSize; ++i) { for (int j = 0; j < (int)uiBlockSize; ++j) { float fHeight = pkHeightMap->GetRawHeight(i, j); NxI16 nxHeight = (NxI16)(fThirtyTwoKb * ((fHeight - fMinHeight) / fHalfDeltaHeight)); NxHeightFieldSample* pkHeightFieldSample = (NxHeightFieldSample*)(pucData); pkHeightFieldSample->height = nxHeight; pkHeightFieldSample->materialIndex0 = 1; pkHeightFieldSample->materialIndex1 = 1; pkHeightFieldSample->tessFlag = 1; pucData += nxHeightFieldDesc.sampleStride; } } // 创建高度域 m_pkHeightField = m_pkPhysicsSDK->createHeightField(nxHeightFieldDesc); NIASSERT(m_pkHeightField); // NiFree(pnxHeightFieldSample); pnxHeightFieldSample = NULL; // 创建对象描述 NiPoint3 kWorldCenter(0, 0, fMidHeight); ///< ??为什么是这个值?? NxVec3 nxWorldCenter; NiPhysXTypes::NiPoint3ToNxVec3(kWorldCenter, nxWorldCenter); NiMatrix3 kWorldRotation(NiPoint3(0.f, 1.f, 0.f), NiPoint3(0.f, 0.f, 1.f), NiPoint3(1.f, 0.f, 0.f)); NxMat33 nxWorldRotation; NiPhysXTypes::NiMatrix3ToNxMat33(kWorldRotation, nxWorldRotation); NxMat34 kPose; kPose.t = nxWorldCenter; kPose.M = nxWorldRotation; NxHeightFieldShapeDesc nxHeightFieldShapeDesc; nxHeightFieldShapeDesc.heightField = m_pkHeightField; nxHeightFieldShapeDesc.heightScale = fHalfDeltaHeight / fThirtyTwoKb; nxHeightFieldShapeDesc.rowScale = 1.f; nxHeightFieldShapeDesc.columnScale = 1.f; nxHeightFieldShapeDesc.materialIndexHighBits = 0; nxHeightFieldShapeDesc.holeMaterial = 100; nxHeightFieldShapeDesc.meshFlags = NX_MESH_SMOOTH_SPHERE_COLLISIONS; nxHeightFieldShapeDesc.shapeFlags |= NX_SF_FEATURE_INDICES; // nxHeightFieldShapeDesc.shapeFlags |= NX_SF_VISUALIZATION; nxHeightFieldShapeDesc.shapeFlags &= ~NX_SF_VISUALIZATION; nxHeightFieldShapeDesc.group = E_COLLISION_GROUP_MASK_TERRAIN; nxHeightFieldShapeDesc.name = "PhysX_Terrain"; NxActorDesc nxActorDesc; nxActorDesc.body = NULL; nxActorDesc.name = NULL; nxActorDesc.shapes.push_back(&nxHeightFieldShapeDesc); nxActorDesc.globalPose = kPose; nxActorDesc.group = E_COLLISION_GROUP_MASK_TERRAIN; // 创建对象 NxActor* pkActor = m_pkScene->createActor(nxActorDesc); NIASSERT(pkActor); return true; }