private void InitializeUpdateMethod() { doAR = false; DateTime startTime = DateTime.Now; int framesCount = 0; CompositionTarget.Rendering += (s, ea) => { if (doAR) { // Take webcam snapshot. // CaptureImageAsync calls the event CaptureImageCompleted += (s, e) => DetectMarkers(e.Result); captureSource.CaptureImageAsync(); } else { var trans = Matrix.CreateTranslation(new Vector(0, 0, -400)); timedRotation += 2; trans = Matrix.CreateRotationZ(timedRotation) * trans; ApplyFinalTransformation(trans); } // Update FPS text and reset counter every 30 frames if (++framesCount >= 30) { TimeSpan elapsed = DateTime.Now - startTime; Txt.Text = String.Format("{0:###0.00} fps", framesCount / elapsed.TotalSeconds); startTime = DateTime.Now; framesCount = 0; } }; }
private void ApplyFinalTransformation(Matrix baseTransformation) { // Transform the mesh and change coordinate system Matrix swapAxis = Matrix.CreateRotationX(90); Matrix scale = Matrix.CreateScale(50f); var trans = swapAxis * scale * baseTransformation; this.ActiveMesh.World = trans; }
public void Update() { var pos = collisionSkin.NewPosition.ToBalder(); var orient = PhysicsBody.CollisionSkin.GetPrimitiveLocal(0).Transform.Orientation *PhysicsBody.Orientation; var orientation = orient.ToBalder(); World = orientation * Matrix.CreateTranslation(pos); if (Transformation != null) { World *= Transformation; } f += 0.05f; //World = Balder.Math.Matrix.CreateScale((float)Math.Sin(f) + 1); }
private void DetectMarkers(WriteableBitmap bmp) { // Init. here because the captureSource.VideoCaptureDevice.DesiredFormat getter is not reliable and throws an Exception if (arDetector == null) { InitializeDetector(bmp.PixelWidth, bmp.PixelHeight); } // Detect var detectedResults = arDetector.DetectAllMarkers(bmp); // Reused for marker highlighting bmp.Clear(); ViewportOverlay.Source = bmp; if (detectedResults.HasResults) { var trans = detectedResults[0].Transformation.ToBalderMatrix(); if (detectedResults.Count > 1) { // Calculate distance vector wayInterpol += (3f * sign); var trans2 = detectedResults[1].Transformation.ToBalderMatrix(); var p1 = new Vector(trans[3, 0], trans[3, 1], trans[3, 2]); var p2 = new Vector(trans2[3, 0], trans2[3, 1], trans2[3, 2]); var d = p1 - p2; var dn = Vector.Normalize(d); var p = dn * -wayInterpol; // Turning point ? if (p.LengthSquared() > d.LengthSquared()) { sign = -1; } else if (wayInterpol < 0) { sign = 1; } var translate = Matrix.CreateTranslation(p); // Calculate Yaw and align the mesh to it var dot = Vector.Dot(Vector.UnitY, dn); var rotRad = Math.Acos(dot); var rotDeg = MathHelper.ToDegrees((float)rotRad); var yaw = Matrix.CreateRotationZ(150 + rotDeg + (sign > 0 ? 0 : 180)); // Combine matrix trans = yaw * trans * translate; } else { // If no 2nd marker was found only rotate around z axis timedRotation += 2; trans = Matrix.CreateRotationZ(timedRotation) * trans; } // Transform the mesh and change coordinate system ApplyFinalTransformation(trans); // Highlight detected markers using the WriteableBitmapEx foreach (var r in detectedResults) { bmp.DrawQuad((int)r.Square.P1.X, (int)r.Square.P1.Y, (int)r.Square.P2.X, (int)r.Square.P2.Y, (int)r.Square.P3.X, (int)r.Square.P3.Y, (int)r.Square.P4.X, (int)r.Square.P4.Y, Colors.Red); } } }