/// <summary> /// 指定シーンフレームに進みます。 /// </summary> public void FrameMoveDerived() { if (OpenNiEnabled && NiSimpleTracker.IsTracking()) { OpenNiSolve(); } if (MotionEnabled) { return; } if (solver.Solved) { return; } Figure fig; if (TryGetFigure(out fig)) { if (current_effector_path == "|W_Hips") { solver.SolveRootNode(fig.Tmo, current_effector_path); } else { solver.Solve(fig.Tmo, current_effector_path, solver.Target); } fig.UpdateBoneMatricesWithoutTMOFrame(); } }
private void OnDeviceLost(object sender, EventArgs e) { Console.WriteLine("CCDViewer.OnDeviceLost"); if (OpenNiEnabled) { ni_model_translation = Vector3.Empty; NiSimpleTracker.Clean(); Console.WriteLine("ok OpenNIClean"); OpenNiEnabled = false; } if (surface != null) { surface.Dispose(); } }
private void OnDeviceReset(object sender, EventArgs e) { Console.WriteLine("CCDViewer.OnDeviceReset"); { StringBuilder path = new StringBuilder(Path.Combine(Application.StartupPath, "Data\\SamplesConfig.xml")); Console.WriteLine("OpenNIInit on {0}", path); OpenNiEnabled = (NiSimpleTracker.Init(path) == 0); Console.WriteLine("ok OpenNIInit:{0}", OpenNiEnabled); } if (OpenNiEnabled) { surface = device.CreateOffscreenPlainSurface(camw, camh, Format.X8R8G8B8, Pool.Default); surface_buf = new byte[camw * camh * 4]; } CreateGrid(); }
/// <summary> /// シーンをレンダリングします。 /// </summary> public void RenderDerived() { if (OpenNiEnabled) { if (NiSimpleTracker.IsTracking()) { Vector4 color = new Vector4(1, 0, 0, 0.5f); foreach (XnSkeletonJointPosition xnp in ni_joint_ary) { Vector3 p1 = ToWorldPosition(xnp.position); Vector3 pos = p1 - ni_model_translation; DrawMesh(sphere, Matrix.Translation(pos), color); } } { GraphicsStream gs = surface.LockRectangle(LockFlags.None); NiSimpleTracker.DrawDepthMap(); IntPtr ptr = NiSimpleTracker.GetDepthBuf(); int len = camw * camh * 4; Marshal.Copy(ptr, surface_buf, 0, len); Marshal.Copy(surface_buf, 0, gs.InternalData, len); surface.UnlockRectangle(); } Rectangle src_rect = new Rectangle(0, 0, camw, camh); //カメラ画像の転写矩形を作成 Viewport vp = device.Viewport; Rectangle view_rect = new Rectangle(vp.X + vp.Width - vp.Width / 4, vp.Y, vp.Width / 4, vp.Height / 4); { //背景描画 Surface dest_surface = device.GetBackBuffer(0, 0, BackBufferType.Mono); device.StretchRectangle(surface, src_rect, dest_surface, view_rect, TextureFilter.None); } } if (MotionEnabled) { return; } DrawEffector(); DrawTarget(); DrawGrid(); }
private void OpenNiSolve() { Figure fig; if (TryGetFigure(out fig)) { IntPtr ptr = NiSimpleTracker.GetJointPos(); for (int i = 0; i < 15; i++) { ni_joint_ary[i] = (XnSkeletonJointPosition)Marshal.PtrToStructure(ptr, typeof(XnSkeletonJointPosition)); ptr = (IntPtr)(ptr.ToInt32() + Marshal.SizeOf(typeof(XnSkeletonJointPosition))); } if (ni_joint_ary[0].confidence < 0.5f) { return; } for (int i = 0; i < 15; i++) { ni_joint_map[ni_joint_names[i]] = ni_joint_ary[i]; } TMOFile tmo = fig.Tmo; { Vector3 p0 = tmo.FindNodeByName("W_Hips").Translation; Vector3 p1 = ToWorldPosition(Mean(ni_joint_map["LeftHip"].position, ni_joint_map["RightHip"].position)); if (ni_model_translation == Vector3.Empty) { ni_model_translation = p1 - p0; } tmo.FindNodeByName("W_Hips").Translation = p1 - ni_model_translation; } tmo.nodemap["|W_Hips"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_Neck"].Rotation = Quaternion.Identity; //tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_Neck|Head"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_RightShoulder_Dummy|W_RightShoulder"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_RightShoulder_Dummy|W_RightShoulder|W_RightArm_Dummy|W_RightArm"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_RightShoulder_Dummy|W_RightShoulder|W_RightArm_Dummy|W_RightArm|W_RightArmRoll|W_RightForeArm"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_RightShoulder_Dummy|W_RightShoulder|W_RightArm_Dummy|W_RightArm|W_RightArmRoll|W_RightForeArm|W_RightForeArmRoll|W_RightHand"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_RightHips_Dummy|W_RightUpLeg"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_RightHips_Dummy|W_RightUpLeg|W_RightUpLegRoll|W_RightLeg"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_RightHips_Dummy|W_RightUpLeg|W_RightUpLegRoll|W_RightLeg|W_RightLegRoll|W_RightFoot"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_LeftShoulder_Dummy|W_LeftShoulder"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_LeftShoulder_Dummy|W_LeftShoulder|W_LeftArm_Dummy|W_LeftArm"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_LeftShoulder_Dummy|W_LeftShoulder|W_LeftArm_Dummy|W_LeftArm|W_LeftArmRoll|W_LeftForeArm"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_LeftShoulder_Dummy|W_LeftShoulder|W_LeftArm_Dummy|W_LeftArm|W_LeftArmRoll|W_LeftForeArm|W_LeftForeArmRoll|W_LeftHand"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_LeftHips_Dummy|W_LeftUpLeg"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_LeftHips_Dummy|W_LeftUpLeg|W_LeftUpLegRoll|W_LeftLeg"].Rotation = Quaternion.Identity; tmo.nodemap["|W_Hips|W_LeftHips_Dummy|W_LeftUpLeg|W_LeftUpLegRoll|W_LeftLeg|W_LeftLegRoll|W_LeftFoot"].Rotation = Quaternion.Identity; Quaternion q; if (TryNiRotationDirX(out q, ni_joint_map["LeftHip"], ni_joint_map["RightHip"])) { tmo.FindNodeByName("W_Hips").Rotation = q; } Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3", "Torso"); Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_Neck", "Neck"); Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_Neck|Head", "Head"); //Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_RightShoulder_Dummy|W_RightShoulder|W_RightArm_Dummy|W_RightArm", "LeftShoulder"); Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_RightShoulder_Dummy|W_RightShoulder|W_RightArm_Dummy|W_RightArm|W_RightArmRoll|W_RightForeArm", "LeftElbow"); Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_RightShoulder_Dummy|W_RightShoulder|W_RightArm_Dummy|W_RightArm|W_RightArmRoll|W_RightForeArm|W_RightForeArmRoll|W_RightHand", "LeftHand"); Solve(tmo, "|W_Hips|W_RightHips_Dummy|W_RightUpLeg|W_RightUpLegRoll|W_RightLeg", "LeftKnee"); Solve(tmo, "|W_Hips|W_RightHips_Dummy|W_RightUpLeg|W_RightUpLegRoll|W_RightLeg|W_RightLegRoll|W_RightFoot", "LeftFoot"); //Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_LeftShoulder_Dummy|W_LeftShoulder|W_LeftArm_Dummy|W_LeftArm", "RightShoulder"); Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_LeftShoulder_Dummy|W_LeftShoulder|W_LeftArm_Dummy|W_LeftArm|W_LeftArmRoll|W_LeftForeArm", "RightElbow"); Solve(tmo, "|W_Hips|W_Spine_Dummy|W_Spine1|W_Spine2|W_Spine3|W_LeftShoulder_Dummy|W_LeftShoulder|W_LeftArm_Dummy|W_LeftArm|W_LeftArmRoll|W_LeftForeArm|W_LeftForeArmRoll|W_LeftHand", "RightHand"); Solve(tmo, "|W_Hips|W_LeftHips_Dummy|W_LeftUpLeg|W_LeftUpLegRoll|W_LeftLeg", "RightKnee"); Solve(tmo, "|W_Hips|W_LeftHips_Dummy|W_LeftUpLeg|W_LeftUpLegRoll|W_LeftLeg|W_LeftLegRoll|W_LeftFoot", "RightFoot"); fig.UpdateBoneMatricesWithoutTMOFrame(); } }