void InitNx() { // Create a memory allocator gAllocator = new UserAllocator; // Create the physics SDK gPhysicsSDK = CreatePhysics(); // Create the scene NxSceneDesc sceneDesc; sceneDesc.gravity = gDefaultGravity; sceneDesc.simType = NX_SIMULATION_HW; gScene = gPhysicsSDK->createScene(sceneDesc); if(!gScene){ sceneDesc.simType = NX_SIMULATION_SW; gScene = gPhysicsSDK->createScene(sceneDesc); if(!gScene) return; } // Create the default material NxMaterial* defaultMaterial = gScene->getMaterialFromIndex(0); defaultMaterial->setRestitution(0.5); defaultMaterial->setStaticFriction(0.5); defaultMaterial->setDynamicFriction(0.5); // Set Core Dump directory char buff[512]; FindMediaFile(fnameCD, buff); #ifdef WIN32 SetCurrentDirectory(buff); #elif LINUX chdir(buff); #endif // Create the objects in the scene NxActor* groundPlane = CreateGroundPlane(); NxActor* box = CreateBox(NxVec3(5,0,0), NxVec3(0.5,1,0.5), 20); NxActor* sphere = CreateSphere(NxVec3(0,0,0), 1, 10); NxActor* capsule = CreateCapsule(NxVec3(-5,0,0), 2, 0.5, 10); // pyramid = CreatePyramid(NxVec3(0,0,0), NxVec3(1,0.5,1.5), 10); AddUserDataToActors(gScene); gSelectedActor = capsule; // gSelectedActor = pyramid; // Initialize HUD InitializeHUD(); // Get the current time getElapsedTime(); // Start the first frame of the simulation if (gScene) StartPhysics(); }
void World::SetupBunnyScene() { NxSoftBodyDesc softBodyDesc; softBodyDesc.globalPose.t = NxVec3(0,35,-1); softBodyDesc.particleRadius = 0.2f; softBodyDesc.volumeStiffness = 1.0f; softBodyDesc.stretchingStiffness = 0.07f; softBodyDesc.friction = 1.0f; char *fileName = "bunny"; char tetFileName[256], objFileName[256], s[256]; sprintf(tetFileName, "%s.tet", fileName); sprintf(objFileName, "%s.obj", fileName); ObjMesh *objMesh = new ObjMesh(); // it is for mesh surface rendering objMesh->loadFromObjFile(FindMediaFile(objFileName, s)); MySoftBody *softBody = new MySoftBody(gScene, softBodyDesc, FindMediaFile(tetFileName,s), objMesh); if (!softBody->getNxSoftBody()) { printf("Error: Unable to create the SoftBody for the current scene.\n"); delete softBody; } else { gSoftBodies.push_back(softBody); // soft body is pushed to list. gObjMeshes.push_back(objMesh); // surface mesh is pushed to list. } NxMat33 rot; NxActor *plate0 = CreateBox(NxVec3(-2.5,10,0), NxVec3(20.0f, 1.0f, 10.0f), 0,0.0f); rot.rotZ(1.5f); plate0->setGlobalOrientation(rot); NxActor *plate1 = CreateBox(NxVec3(2.5,10,0), NxVec3(20.0f, 1.0f, 10.0f), 0,0.0f); rot.rotZ(1.5f); plate1->setGlobalOrientation(rot); // set camera position and direction gCameraPos.set(-5.0f, 40.0f, 35.0f); gCameraForward.set(0.2,-1.1,-2); gCameraForward.normalize(); }
void World::SetupPalmScene() { NxSoftBodyDesc softBodyDesc; softBodyDesc.globalPose.t = NxVec3(0,3,0); softBodyDesc.particleRadius = 0.2f; softBodyDesc.volumeStiffness = 0.5; softBodyDesc.stretchingStiffness = 1.0f; softBodyDesc.friction = 1.0f; softBodyDesc.flags |= NX_SBF_COLLISION_TWOWAY; char* fileName = "palm"; char tetFileName[256], objFileName[256], s[256]; //sprintf(tetFileName, "%s.tet", fileName); sprintf(objFileName, "%s.obj", fileName); ObjMesh *objMesh = new ObjMesh(); objMesh->loadFromObjFile(FindMediaFile(objFileName, s)); MySoftBody *softBody = new MySoftBody(gScene, softBodyDesc, FindMediaFile(tetFileName,s), objMesh); if (!softBody->getNxSoftBody()) { printf("Error: Unable to create the SoftBody for the current scene.\n"); delete softBody; } else { gSoftBodies.push_back(softBody); gObjMeshes.push_back(objMesh); softBody->getNxSoftBody()->setExternalAcceleration(NxVec3(5,0,0)); NxActor *caps0 = CreateCompoundCapsule(NxVec3(-0.46f, -0.15f, 0.68f), 1.7f, 0.21f, 2.0f, 0.18f); caps0->userData = (void*)&gInvisible; CreateRopeSphericalJoint(NULL, caps0, NxVec3(-0.46f, -0.0f, 0.68f), NxVec3(0,0,1)); softBody->getNxSoftBody()->attachToCollidingShapes(0); } }
void InPlaceParser::SetFile(const char *fname) { if ( mMyAlloc ) { free(mData); } mData = 0; mLen = 0; mMyAlloc = false; char buff[512]; FILE *fph = fopen(FindMediaFile(fname,buff),"rb"); if ( fph ) { fseek(fph,0L,SEEK_END); mLen = ftell(fph); fseek(fph,0L,SEEK_SET); if ( mLen ) { mData = (char *) malloc(sizeof(char)*(mLen+1)); int ok = fread(mData, mLen, 1, fph); if ( !ok ) { free(mData); mData = 0; } else { mData[mLen] = 0; // zero byte terminate end of file marker. mMyAlloc = true; } } fclose(fph); } }
HRESULT PackedResource::Create( const char *strFilename ) #endif { #ifdef _XBOX1 BOOL bHasResourceOffsetsTable = FALSE; // Find the media file CHAR strResourcePath[512]; if( FAILED(FindMediaFile(strResourcePath, strFilename, sizeof(strResourcePath)))) return E_FAIL; else strFilename = strResourcePath; #endif // Open the file HANDLE hFile; DWORD dwNumBytesRead; hFile = CreateFile( strFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL ); if( hFile == INVALID_HANDLE_VALUE ) { RARCH_ERR( "PackedResource::Create(): File <%s> not found.\n", strFilename ); return E_FAIL; } // Read in and verify the XPR magic header XPR_HEADER xprh; bool retval = ReadFile( hFile, &xprh, sizeof( XPR_HEADER ), &dwNumBytesRead, NULL ); #if defined(_XBOX1) if( xprh.dwMagic == XPR0_MAGIC_VALUE ) { bHasResourceOffsetsTable = FALSE; } else if( xprh.dwMagic == XPR1_MAGIC_VALUE ) { bHasResourceOffsetsTable = TRUE; } else #elif defined(_XBOX360) if(!retval) { RARCH_ERR("Error reading XPR header in file %s.\n", strFilename ); CloseHandle( hFile ); return E_FAIL; } if( xprh.dwMagic != XPR2_MAGIC_VALUE ) #endif { RARCH_ERR( "Invalid Xbox Packed Resource (.xpr) file: Magic = 0x%08lx\n", xprh.dwMagic ); CloseHandle( hFile ); return E_FAIL; } // Compute memory requirements #if defined(_XBOX1) m_dwSysMemDataSize = xprh.dwHeaderSize - sizeof(XPR_HEADER); m_dwVidMemDataSize = xprh.dwTotalSize - xprh.dwHeaderSize; #elif defined(_XBOX360) m_dwSysMemDataSize = xprh.dwHeaderSize; m_dwVidMemDataSize = xprh.dwDataSize; #endif // Allocate memory m_pSysMemData = (BYTE*)malloc(m_dwSysMemDataSize); if( m_pSysMemData == NULL ) { RARCH_ERR( "Could not allocate system memory.\n" ); m_dwSysMemDataSize = 0; return E_FAIL; } m_pVidMemData = ( BYTE* )AllocateContiguousMemory( m_dwVidMemDataSize, #if defined(_XBOX1) D3DTEXTURE_ALIGNMENT #elif defined(_XBOX360) XALLOC_PHYSICAL_ALIGNMENT_4K #endif ); if( m_pVidMemData == NULL ) { RARCH_ERR( "Could not allocate physical memory.\n" ); m_dwSysMemDataSize = 0; m_dwVidMemDataSize = 0; free(m_pSysMemData); m_pSysMemData = NULL; return E_FAIL; } // Read in the data from the file if( !ReadFile( hFile, m_pSysMemData, m_dwSysMemDataSize, &dwNumBytesRead, NULL ) || !ReadFile( hFile, m_pVidMemData, m_dwVidMemDataSize, &dwNumBytesRead, NULL ) ) { RARCH_ERR( "Unable to read Xbox Packed Resource (.xpr) file\n" ); CloseHandle( hFile ); return E_FAIL; } // Done with the file CloseHandle( hFile ); #ifdef _XBOX1 if (bHasResourceOffsetsTable) { #endif // Extract resource table from the header data m_dwNumResourceTags = *( DWORD* )( m_pSysMemData + 0 ); m_pResourceTags = ( XBRESOURCE* )( m_pSysMemData + 4 ); // Patch up the resources for( DWORD i = 0; i < m_dwNumResourceTags; i++ ) { m_pResourceTags[i].strName = ( CHAR* )( m_pSysMemData + ( DWORD )m_pResourceTags[i].strName ); #ifdef _XBOX360 // Fixup the texture memory if( ( m_pResourceTags[i].dwType & 0xffff0000 ) == ( RESOURCETYPE_TEXTURE & 0xffff0000 ) ) { D3DTexture* pTexture = ( D3DTexture* )&m_pSysMemData[m_pResourceTags[i].dwOffset]; // Adjust Base address according to where memory was allocated XGOffsetBaseTextureAddress( pTexture, m_pVidMemData, m_pVidMemData ); } #endif } #ifdef _XBOX1 } #endif #ifdef _XBOX1 // Use user-supplied number of resources and the resource tags if( dwNumResourceTags != 0 || pResourceTags != NULL ) { m_pResourceTags = pResourceTags; m_dwNumResourceTags = dwNumResourceTags; } #endif m_bInitialized = TRUE; return S_OK; }
/********************Public*Routine****************************************\ * CPlane * \**************************************************************************/ CHall::CPlane::CPlane( IDirect3DDevice9* pDevice, TCHAR *achName, LPCTSTR lpResourceName, float fU, float fV, D3DXMATRIX& M, float a, HRESULT *phr) : m_du( 0.f) , m_fU( fU ) , m_fV( fV ) , m_fSpeed( 0.f ) , m_pTexture( NULL ) { HRESULT hr = S_OK; TCHAR achTexturePath[MAX_PATH]; D3DXVECTOR3 vLB( M(0,0), M(1,0), M(2,0)); D3DXVECTOR3 vLT( M(0,1), M(1,1), M(2,1)); D3DXVECTOR3 vRT( M(0,2), M(1,2), M(2,2)); D3DXVECTOR3 vRB( M(0,3), M(1,3), M(2,3)); D3DXVECTOR3 V; try { if( !achName ) throw E_POINTER; hr = FindMediaFile( achTexturePath, sizeof(TCHAR)*MAX_PATH, achName, lpResourceName, RT_BITMAP); if( FAILED(hr)) { TCHAR achMsg[MAX_PATH]; _stprintf( achMsg, TEXT("Cannot find media file '%s'. ") TEXT("Make sure you have valid installation of DirectX SDK"), achName); ::MessageBox( NULL, achMsg, TEXT("Error"), MB_OK); if( phr ) { *phr = hr; return; } } if( !pDevice ) throw E_POINTER; if( a<0.01f || a>0.5f ) throw E_INVALIDARG; D3DXVECTOR3 vecDiff = vLB - vLT; if( D3DXVec3LengthSq( &vecDiff )< 0.001f ) throw E_INVALIDARG; if( m_fU < 0.001f || m_fV < 0.001f ) throw E_INVALIDARG; CHECK_HR( hr = DXUtil_FindMediaFileCb( achTexturePath, sizeof(TCHAR)*MAX_PATH, achName ), DbgMsg("CPlane::CPlane: cannot find bitmap file %s in SDK media folder", achTexturePath)); // load texture ASSERT( g_pEnvFormat ); CHECK_HR( hr = D3DUtil_CreateTexture( pDevice, achTexturePath, &m_pTexture, *g_pEnvFormat ), DbgMsg("CPlane::CPlane: failed to create the texture, hr = 0x%08x", hr)); // initialize geometry // POSITION // corners memcpy( &(m_V[0].Pos), &vLB, sizeof(D3DVECTOR)); memcpy( &(m_V[1].Pos), &vLT, sizeof(D3DVECTOR)); memcpy( &(m_V[2].Pos), &vRB, sizeof(D3DVECTOR)); memcpy( &(m_V[3].Pos), &vRT, sizeof(D3DVECTOR)); // TEXTURE COORD m_V[0].tu = 0.f; m_V[0].tv = fV; m_V[1].tu = 0.f; m_V[1].tv = 0.f; m_V[2].tu = fU; m_V[2].tv = fV; m_V[3].tu = fU; m_V[3].tv = 0.f; // corners are transparent, middle vertices are opaque m_V[0].color = D3DCOLOR_RGBA( 0xFF, 0xFF, 0xFF, 0xFF); m_V[1].color = D3DCOLOR_RGBA( 0xFF, 0xFF, 0xFF, 0xFF); m_V[2].color = D3DCOLOR_RGBA( 0xFF, 0xFF, 0xFF, 0xFF); m_V[3].color = D3DCOLOR_RGBA( 0xFF, 0xFF, 0xFF, 0xFF); } catch( HRESULT hr1 ) { RELEASE( m_pTexture ); hr = hr1; } if( phr ) { *phr = hr; } }
void World::SetupSoftWheelCarScene() { NxSoftBodyDesc softBodyDesc; softBodyDesc.particleRadius = 0.2f; softBodyDesc.volumeStiffness = 1.0f; softBodyDesc.stretchingStiffness = 1.0f; softBodyDesc.friction = 1.0f; softBodyDesc.attachmentResponseCoefficient = 0.8f; softBodyDesc.tearFactor = 10.1; // should be more than 1.0, the less, the easier to tear with flag NX_SBF_TEARABLE softBodyDesc.flags |= NX_SBF_HARDWARE | NX_SBF_VOLUME_CONSERVATION | NX_SBF_TEARABLE; char *fileName = "wheel"; char tetFileName[256], objFileName[256], s[256]; sprintf(tetFileName, "%s.tet", fileName); sprintf(objFileName, "%s.obj", fileName); MySoftBody *softBody; NxReal carHeight = 7.5f; NxReal stiffness = 0.9f; NxMat34 capsulePose = NxMat34(NxMat33(NX_IDENTITY_MATRIX), NxVec3(-4, carHeight, -5.0f)); printf("capsulePose %f %f %f\n", capsulePose.t.x, capsulePose.t.y, capsulePose.t.z); capsulePose.M.rotX(NxHalfPiF32); NxActor *caps1 = CreateOrientedCapsule(capsulePose, 7.1f, 1.3f, 1.0f); capsulePose.t = NxVec3(4, carHeight, -5.0f); NxActor *caps2 = CreateOrientedCapsule(capsulePose, 7.1f, 1.3f, 1.0f); ObjMesh *objMesh = new ObjMesh(); objMesh->loadFromObjFile(FindMediaFile(objFileName, s)); NxMat33 rot; rot.rotX(NxPiF32); softBodyDesc.globalPose.t = NxVec3(4.0f, carHeight, 3.4f); softBodyDesc.globalPose.M = rot; softBodyDesc.stretchingStiffness = stiffness; softBody = new MySoftBody(gScene, softBodyDesc, FindMediaFile(tetFileName,s), objMesh); if (!softBody->getNxSoftBody()) { printf("Error: Unable to create Softbody for the current scene.\n"); delete softBody; } else { gObjMeshes.push_back(objMesh); // wheel 1 softBody->getNxSoftBody()->attachToCollidingShapes(NX_SOFTBODY_ATTACHMENT_TWOWAY); gSoftBodies.push_back(softBody); softBodyDesc.globalPose.t = NxVec3(-4.0f ,carHeight, 3.4f); softBodyDesc.globalPose.M = rot; softBodyDesc.stretchingStiffness = stiffness; softBody = new MySoftBody(gScene, softBodyDesc, FindMediaFile(tetFileName,s), objMesh); softBody->getNxSoftBody()->attachToCollidingShapes(NX_SOFTBODY_ATTACHMENT_TWOWAY); gSoftBodies.push_back(softBody); softBodyDesc.globalPose.t = NxVec3(4.0f, carHeight, -3.4f); softBodyDesc.globalPose.M.id(); softBodyDesc.stretchingStiffness = stiffness; softBody = new MySoftBody(gScene, softBodyDesc, FindMediaFile(tetFileName,s), objMesh); softBody->getNxSoftBody()->attachToCollidingShapes(NX_SOFTBODY_ATTACHMENT_TWOWAY); gSoftBodies.push_back(softBody); softBodyDesc.globalPose.t = NxVec3(-4.0f, carHeight, -3.4f); softBodyDesc.globalPose.M.id(); softBodyDesc.stretchingStiffness = stiffness; softBody = new MySoftBody(gScene, softBodyDesc, FindMediaFile(tetFileName,s), objMesh); softBody->getNxSoftBody()->attachToCollidingShapes(NX_SOFTBODY_ATTACHMENT_TWOWAY); // bind soft wheel with capsule colliding with it gSoftBodies.push_back(softBody); NxActor *box = CreateBox(NxVec3(0,carHeight,0), NxVec3(4.6f, 0.5f, 1.0f), 0,1.0f); CreateRevoluteJoint(box, caps1, NxVec3(-4,carHeight,-3.5f), NxVec3(0,0,1), false); CreateRevoluteJoint(box, caps2, NxVec3( 4,carHeight,-3.5f), NxVec3(0,0,1), false); } gSelectedActor = caps1; }
void InitNx() { // Create the physics SDK gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, NULL, &gErrorStream); if (!gPhysicsSDK) return; // Set the physics parameters gPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.05); // Set the debug visualization parameters gPhysicsSDK->setParameter(NX_VISUALIZATION_SCALE, 1); gPhysicsSDK->setParameter(NX_VISUALIZE_ACTOR_AXES, 1); gPhysicsSDK->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1); // gPhysicsSDK->setParameter(NX_VISUALIZE_COLLISION_AXES, 1); // gPhysicsSDK->setParameter(NX_VISUALIZE_COLLISION_FNORMALS, 1); gPhysicsSDK->setParameter(NX_VISUALIZE_CONTACT_POINT, 1); gPhysicsSDK->setParameter(NX_VISUALIZE_CONTACT_NORMAL, 1); // Create the scene NxSceneDesc sceneDesc; sceneDesc.gravity = gDefaultGravity; sceneDesc.simType = NX_SIMULATION_HW; gScene = gPhysicsSDK->createScene(sceneDesc); if(!gScene){ sceneDesc.simType = NX_SIMULATION_SW; gScene = gPhysicsSDK->createScene(sceneDesc); if(!gScene) return; } NxU32 set = 0; #ifdef WIN32 set = SetCurrentDirectory(&fname[0]); if (!set) set = SetCurrentDirectory(&fname1[0]); if (!set) { char basePath[256]; GetModuleFileName(NULL, basePath, 256); char* pTmp = strrchr(basePath, '\\'); basePath[pTmp-basePath+1] = 0; SetCurrentDirectory(basePath);//for running from start menu set = SetCurrentDirectory(&fname2[0]); } if (!set) set = SetCurrentDirectory(&fname3[0]); #elif LINUX set = chdir(&fname[0]); if (set != 0) set = chdir(&fname2[0]); if (set != 0) set = chdir(&fname3[0]); #endif // Create the default material NxMaterialDesc defaultMaterial; defaultMaterial.restitution = 0; defaultMaterial.staticFriction = 0.5; defaultMaterial.dynamicFriction = 0.5; NxMaterial* m = gScene->getMaterialFromIndex(0); m->loadFromDesc(defaultMaterial); char buffer[512]; FindMediaFile("Course.pml", buffer); nxmlLoadScene(buffer, gPhysicsSDK, gScene); // Switch from Max Coordinate System to // Training Program Coordinate System NxMat34 mat; NxMat33 orient; orient.setColumn(0, NxVec3(-1,0,0)); orient.setColumn(1, NxVec3(0,0,1)); orient.setColumn(2, NxVec3(0,1,0)); mat.M = orient; SwitchCoordinateSystem(gScene, mat); // Reset wheel material wsm = NULL; // Create the trike actor trike = CreateTrike(NxVec3(0,3,0)); trike->wakeUp(1e30); AddUserDataToActors(gScene); gSelectedActor = trike; // Initialize HUD InitializeHUD(); InitializeSpecialHUD(); // Get the current time getElapsedTime(); // Start the first frame of the simulation if (gScene) StartPhysics(); }