void RigidBodyWorld::UpdatePhysics () { RigidBodyWorldDesc* const desc = (RigidBodyWorldDesc*) RigidBodyWorldDesc::GetDescriptor(); float timestep = 1.0f / desc->m_minFps; if (timestep > 1.0f / 60.0f) { timestep = 1.0f / 60.0f; } desc->m_updateRigidBodyMatrix = false; NewtonUpdate(desc->m_newton, timestep); TimeValue t (GetCOREInterface()->GetTime()); float scale = 1.0f / float (GetMasterScale(UNITS_METERS)); for (NewtonBody* body = NewtonWorldGetFirstBody(desc->m_newton); body; body = NewtonWorldGetNextBody(desc->m_newton, body)) { dMatrix matrix; INode* const node = (INode*)NewtonBodyGetUserData(body); NewtonBodyGetMatrix(body, &matrix[0][0]); matrix = desc->m_systemMatrix * matrix * desc->m_systemMatrixInv; matrix.m_posit = matrix.m_posit.Scale (scale); Matrix3 maxMatrix (GetMatrixFromdMatrix (matrix)); node->SetNodeTM(t, maxMatrix); } UpdateViewPorts (); desc->m_updateRigidBodyMatrix = true; }
void RigidBodyWorld::InitUI(HWND hWnd) { RigidBodyWorldDesc* const desc = (RigidBodyWorldDesc*) RigidBodyWorldDesc::GetDescriptor(); HWND minFps = GetDlgItem(hWnd, IDC_MINUMIN_SIMULATION_RATE); HWND gravity_x = GetDlgItem(hWnd, IDC_GRAVITY_X); HWND gravity_y = GetDlgItem(hWnd, IDC_GRAVITY_Y); HWND gravity_z = GetDlgItem(hWnd, IDC_GRAVITY_Z); m_minFps = GetICustEdit(minFps); m_gravity[0] = GetICustEdit(gravity_x); m_gravity[1] = GetICustEdit(gravity_y); m_gravity[2] = GetICustEdit(gravity_z); float scale = 1.0f / float (GetMasterScale(UNITS_METERS)); dVector gravity = desc->m_systemMatrixInv.RotateVector(desc->m_gravity.Scale (scale)); m_minFps->SetText(desc->m_minFps); m_gravity[0]->SetText(gravity.m_x, 1); m_gravity[1]->SetText(gravity.m_y, 1); m_gravity[2]->SetText(gravity.m_z, 1); RegisterNotification(OnUndoRedo, this, NOTIFY_SCENE_UNDO); RegisterNotification (OnUndoRedo, this, NOTIFY_SCENE_REDO); }
Import::Import(const char* pathName, Interface* ip, ImpInterface* impip) { // set the path for textures char* ptr = NULL; sprintf (m_path, "%s", pathName); for (int i = 0; m_path[i]; i ++) { if ((m_path[i] == '\\') || (m_path[i] == '/') ) { ptr = &m_path[i]; } } *ptr = 0; m_ip = ip; m_impip = impip; m_succes = TRUE; MaterialCache materialCache (NewDefaultMultiMtl()); SetSceneParameters(); dFloat scale; scale = float (GetMasterScale(UNITS_METERS)); dMatrix scaleMatrix (GetIdentityMatrix()); scaleMatrix[0][0] = 1.0f / scale; scaleMatrix[1][1] = 1.0f / scale; scaleMatrix[2][2] = 1.0f / scale; dMatrix globalRotation (scaleMatrix * dPitchMatrix(3.14159265f * 0.5f) * dRollMatrix(3.14159265f * 0.5f)); NewtonWorld* newton = NewtonCreate(); dScene scene (newton); scene.Deserialize (pathName); // dScene::Iterator iter (scene); // for (iter.Begin(); iter; iter ++) { // dScene::dTreeNode* node = iter.GetNode(); // dNodeInfo* info = scene.GetInfoFromNode(node); // if (info->GetTypeId() == dMeshNodeInfo::GetRttiType()) { // dMeshNodeInfo* mesh = (dMeshNodeInfo*) scene.GetInfoFromNode(node); // mesh->ConvertToTriangles(); // } // } scene.BakeTransform (globalRotation); GeometryCache geometryCache; MaxNodeChache maxNodeCache; LoadMaterials (scene, materialCache); LoadGeometries (scene, geometryCache, materialCache); LoadNodes (scene, geometryCache, materialCache.m_multiMat, maxNodeCache); ApplyModifiers (scene, maxNodeCache); scene.CleanUp(); NewtonDestroy(newton); }
INT_PTR CALLBACK RigidBodyWorld::Proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { RigidBodyWorld* const world = (RigidBodyWorld *)GetWindowLong (hWnd, GWLP_USERDATA); RigidBodyWorldDesc* const desc = (RigidBodyWorldDesc*) RigidBodyWorldDesc::GetDescriptor(); switch (msg) { case WM_INITDIALOG: { RigidBodyWorld* const world = (RigidBodyWorld *)lParam; SetWindowLong(hWnd, GWLP_USERDATA, (LONG)world); world->m_myWindow = hWnd; world->RigidBodyWorld::InitUI(hWnd); break; } case WM_DESTROY: { world->StopsSimulation (); world->RigidBodyWorld::DestroyUI(hWnd); break; } case WM_ENABLE: { //EnableWindow(obj->m_worldPaneHandle, (BOOL) wParam); break; } case WM_TIMER: { world->UpdatePhysics (); break; } case WM_COMMAND: { switch (LOWORD(wParam)) { case IDC_MAKE_RIGIDBODY: { world->StopsSimulation (); Interface* const ip = GetCOREInterface(); int selectionCount = ip->GetSelNodeCount(); for (int i = 0; i < selectionCount; i ++) { INode* const node = ip->GetSelNode(i); Object* const obj = node->GetObjOrWSMRef(); if (obj) { world->AttachRigiBodyController (node); } } world->UpdateViewPorts(); break; } case IDC_DELETE_RIGIDBODY: { world->StopsSimulation (); Interface* const ip = GetCOREInterface(); int selectionCount = ip->GetSelNodeCount(); for (int i = 0; i < selectionCount; i ++) { INode* const node = ip->GetSelNode(i); world->DetachRigiBodyController (node); } world->UpdateViewPorts (); break; } case IDC_SHOW_GIZMOS: { world->StopsSimulation (); for (NewtonBody* body = NewtonWorldGetFirstBody(desc->m_newton); body; body = NewtonWorldGetNextBody(desc->m_newton, body)) { INode* const node = (INode*)NewtonBodyGetUserData(body); RigidBodyController* const bodyInfo = (RigidBodyController*)desc->GetRigidBodyControl(node); _ASSERTE (bodyInfo); bodyInfo->m_hideGizmos = FALSE; } world->UpdateViewPorts(); break; } case IDC_HIDE_GIZMOS: { world->StopsSimulation (); for (NewtonBody* body = NewtonWorldGetFirstBody(desc->m_newton); body; body = NewtonWorldGetNextBody(desc->m_newton, body)) { INode* const node = (INode*)NewtonBodyGetUserData(body); RigidBodyController* const bodyInfo = (RigidBodyController*)desc->GetRigidBodyControl(node); _ASSERTE (bodyInfo); bodyInfo->m_hideGizmos = TRUE; } world->UpdateViewPorts(); break; } case IDC_SELECT_ALL: { world->StopsSimulation (); world->m_selectionChange = false; Interface* const ip = GetCOREInterface(); ip->ClearNodeSelection(FALSE); for (NewtonBody* body = NewtonWorldGetFirstBody(desc->m_newton); body; body = NewtonWorldGetNextBody(desc->m_newton, body)) { INode* const node = (INode*)NewtonBodyGetUserData(body); ip->SelectNode(node, 0); } world->m_selectionChange = true; world->SelectionSetChanged (ip, NULL); world->UpdateViewPorts(); break; } case IDC_REMOVE_ALL: { world->StopsSimulation (); Interface* const ip = GetCOREInterface(); ip->ClearNodeSelection(FALSE); for (NewtonBody* body = NewtonWorldGetFirstBody(desc->m_newton); body; ) { INode* const node = (INode*)NewtonBodyGetUserData(body); body = NewtonWorldGetNextBody(desc->m_newton, body); world->DetachRigiBodyController (node); } world->UpdateViewPorts(); break; } case IDC_MINUMIN_SIMULATION_RATE: { world->StopsSimulation (); desc->m_minFps = world->m_minFps->GetFloat(); break; } case IDC_GRAVITY_X: case IDC_GRAVITY_Y: case IDC_GRAVITY_Z: { world->StopsSimulation (); dVector gravity (world->m_gravity[0]->GetFloat(), world->m_gravity[1]->GetFloat(), world->m_gravity[2]->GetFloat(), 0.0f); //world->m_gravity[0]->SetText(gravity.m_x, 1); //world->m_gravity[1]->SetText(gravity.m_y, 1); //world->m_gravity[2]->SetText(gravity.m_z, 1); desc->m_gravity = desc->m_systemMatrix.RotateVector(gravity.Scale(float (GetMasterScale(UNITS_METERS)))); break; } case IDC_PREVIEW_WORLD: { if (IsDlgButtonChecked(hWnd, IDC_PREVIEW_WORLD) == BST_CHECKED) { world->Undo(); unsigned timeOut = unsigned (1000.0f / desc->m_minFps); SetTimer(hWnd, TIMER_ID, timeOut, NULL); } else { world->StopsSimulation (); } break; } case IDC_STEP_WORLD: { world->StopsSimulation (); world->Undo(); world->UpdatePhysics (); break; } } break; } default: return FALSE; } return TRUE; }
int AIShapeImport::DoImport(const TCHAR *filename,ImpInterface *i,Interface *gi, BOOL suppressPrompts) { // Get a scale factor from points (the file storage) to our units double mScale = GetMasterScale(UNITS_INCHES); float scaleFactor = float((1.0 / mScale) / 72.0); // Set a global prompt display switch showPrompts = suppressPrompts ? FALSE : TRUE; WorkFile theFile(filename,_T("rb")); if(suppressPrompts) { } else { if (!DialogBox(hInstance, MAKEINTRESOURCE(IDD_MERGEORREPL), gi->GetMAXHWnd(), ImportDlgProc)) return IMPEXP_CANCEL; } dStream = i->DumpFile(); if(!(stream = theFile.Stream())) { if(showPrompts) MessageBox(IDS_TH_ERR_OPENING_FILE, IDS_TH_3DSIMP); return 0; // Didn't open! } // Got the file -- Now put up the options dialog! if(showPrompts) { int result = DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_SHAPEIMPORTOPTIONS), gi->GetMAXHWnd(), ShapeImportOptionsDlgProc, (LPARAM)this); if(result <= 0) return IMPEXP_CANCEL; } else { // Set default parameters here importType = MULTIPLE_SHAPES; } if (replaceScene) { if (!i->NewScene()) return IMPEXP_CANCEL; } theShapeImport = this; int line,count,status,phase,buflen, reason; float x1,y1,x2,y2,x3,y3; char buffer[256]; phase=0; count=0; line=0; gotStuff = FALSE; GetDecSymbolFromRegistry(); loop: line++; if((status=getaline(stream,buffer,255,0))<0) { reason = IDS_TH_LINE_TOO_LONG; corrupted: if(showPrompts) MessageBox(reason, IDS_TH_3DSIMP); FinishWorkingShape(TRUE, i); return gotStuff; } if(status==0) { FinishWorkingShape(TRUE, i); return gotStuff; // EOF } /* Look for appropriate section of file */ buflen=static_cast<int>(strlen(buffer)); // SR DCAST64: Downcast to 2G limit. switch(phase) { case 0: /* Looking for the path */ buffer[10]=0; if(stricmp(buffer,"%%endsetup")==0) phase=1; break; case 1: /* Loading the path data -- looking for initial 'm' */ if(buflen<2) break; if(buffer[buflen-2]==' ' && buffer[buflen-1]=='m') { phase=2; goto phase2; } break; case 2: phase2: if(buflen<2) break; if(buffer[buflen-2]!=' ') break; switch(buffer[buflen-1]) { case 'm': { /* Moveto */ FixDecimalSymbol(buffer); if(sscanf(buffer,"%f %f",&x1,&y1)!=2) { #ifdef DBG1 DebugPrint("Moveto buffer:%s\n",buffer); #endif bad_file: reason = IDS_TH_INVALIDFILE; goto corrupted; } // If had one working, wrap it up FinishWorkingShape(FALSE, i); // Start this new spline if(!StartWorkingShape()) { reason = IDS_TH_NO_RAM; goto corrupted; } Point3 p(x1 * scaleFactor, y1 * scaleFactor, 0.0f); SplineKnot k(KTYPE_BEZIER, LTYPE_CURVE, p, p, p); spline->AddKnot(k); } break; case 'l': /* Lineto */ case 'L': { /* Lineto corner */ FixDecimalSymbol(buffer); if(sscanf(buffer,"%f %f",&x1,&y1)!=2) { #ifdef DBG1 DebugPrint("Lineto buffer:%s\n",buffer); #endif goto bad_file; } Point3 p(x1 * scaleFactor, y1 * scaleFactor, 0.0f); SplineKnot k(KTYPE_BEZIER, LTYPE_CURVE, p, p, p); spline->AddKnot(k); } break; case 'c': /* Curveto */ case 'C': { /* Curveto corner */ FixDecimalSymbol(buffer); if(sscanf(buffer,"%f %f %f %f %f %f",&x1,&y1,&x2,&y2,&x3,&y3)!=6) { #ifdef DBG1 DebugPrint("Curveto buffer:%s\n",buffer); #endif goto bad_file; } int lastKnot = spline->KnotCount() - 1; spline->SetOutVec(lastKnot, Point3(x1 * scaleFactor, y1 * scaleFactor, 0.0f)); Point3 p(x3 * scaleFactor, y3 * scaleFactor, 0.0f); Point3 in(x2 * scaleFactor, y2 * scaleFactor, 0.0f); SplineKnot k(KTYPE_BEZIER, LTYPE_CURVE, p, in, p); spline->AddKnot(k); } break; case 'v': /* Current/vec */ case 'V': { /* Current/vec corner */ FixDecimalSymbol(buffer); if(sscanf(buffer,"%f %f %f %f",&x2,&y2,&x3,&y3)!=4) { #ifdef DBG1 DebugPrint("Current/vec buffer:%s\n",buffer); #endif goto bad_file; } Point3 p(x3 * scaleFactor, y3 * scaleFactor, 0.0f); Point3 in(x2 * scaleFactor, y2 * scaleFactor, 0.0f); SplineKnot k(KTYPE_BEZIER, LTYPE_CURVE, p, in, p); spline->AddKnot(k); } break; case 'y': /* Vec/next */ case 'Y': { /* Vec/next corner */ FixDecimalSymbol(buffer); if(sscanf(buffer,"%f %f %f %f",&x1,&y1,&x3,&y3)!=4) { #ifdef DBG1 DebugPrint("vec/next buffer:%s\n",buffer); #endif goto bad_file; } int lastKnot = spline->KnotCount() - 1; spline->SetOutVec(lastKnot, Point3(x1 * scaleFactor, y1 * scaleFactor, 0.0f)); Point3 p(x3 * scaleFactor, y3 * scaleFactor, 0.0f); SplineKnot k(KTYPE_BEZIER, LTYPE_CURVE, p, p, p); spline->AddKnot(k); } break; } break; } count++; goto loop; }