public void signChambersProjectionTest() { double[][] problem = new double[][] { new double[] { 1, -10, 35 }, new double[] { 3, -49, 80 }, new double[] { -13, 10, 99 } }; int[][] answer = new int[][] { new int[] { 1, -1, 1 }, new int[] { 1, -1, 1 }, new int[] { -1, 1, 1 } }; int[][] result = new ProjectionMatrix() { columnVectors = problem }.signChambers(); for (int i = 0; i < answer.Count(); i++) { for (int j = 0; j < answer[i].Count(); j++) { Assert.IsTrue(answer[i][j] == result[i][j]); } } }
public Vector4 ClipToEyeCoords(Vector4 clipCoords) { Matrix4 invertedProjection = ProjectionMatrix.Inverse(); Vector4 eyeCoords = Vector4.Transform(clipCoords, invertedProjection); return(new Vector4(eyeCoords.X, eyeCoords.Y, -1f, 0f)); }
private void SetGlContext() { GL.ClearDepth(1.0); GL.Clear(ClearBufferMask.DepthBufferBit); // Clear the Depth Buffer GL.PushAttrib(AttribMask.ViewportBit); RectangleDouble screenRect = this.TransformToScreenSpace(LocalBounds); GL.Viewport((int)screenRect.Left, (int)screenRect.Bottom, (int)screenRect.Width, (int)screenRect.Height); GL.ShadeModel(ShadingModel.Smooth); GL.FrontFace(FrontFaceDirection.Ccw); GL.CullFace(CullFaceMode.Back); GL.DepthFunc(DepthFunction.Lequal); GL.Disable(EnableCap.DepthTest); ClearToGradient(); #if DO_LIGHTING GL.Light(LightName.Light0, LightParameter.Ambient, ambientLight); GL.Light(LightName.Light0, LightParameter.Diffuse, diffuseLight0); GL.Light(LightName.Light0, LightParameter.Specular, specularLight0); GL.Light(LightName.Light0, LightParameter.Ambient, new float[] { 0, 0, 0, 0 }); GL.Light(LightName.Light1, LightParameter.Diffuse, diffuseLight1); GL.Light(LightName.Light1, LightParameter.Specular, specularLight1); GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse); GL.Enable(EnableCap.Light0); GL.Enable(EnableCap.Light1); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.Blend); GL.Enable(EnableCap.Normalize); GL.Enable(EnableCap.Lighting); GL.Enable(EnableCap.ColorMaterial); Vector3 lightDirectionVector = new Vector3(lightDirection0[0], lightDirection0[1], lightDirection0[2]); lightDirectionVector.Normalize(); lightDirection0[0] = (float)lightDirectionVector.x; lightDirection0[1] = (float)lightDirectionVector.y; lightDirection0[2] = (float)lightDirectionVector.z; GL.Light(LightName.Light0, LightParameter.Position, lightDirection0); GL.Light(LightName.Light1, LightParameter.Position, lightDirection1); #endif // set the projection matrix GL.MatrixMode(MatrixMode.Projection); GL.PushMatrix(); GL.LoadMatrix(ProjectionMatrix.GetAsDoubleArray()); // set the modelview matrix GL.MatrixMode(MatrixMode.Modelview); GL.PushMatrix(); GL.LoadMatrix(ModelviewMatrix.GetAsDoubleArray()); }
public void Far() { for (int i = 0; i < 10; i++) { float far = (float)Math.Pow(2.0f, i); ProjectionMatrix projectionMatrix = Create(0.1f, far); Assert.AreEqual(far, projectionMatrix.Far, 1.0f, String.Format("precision loss at far={0}", far)); } }
/// <summary> /// Gets a vector pointing out along a given pixel coordinate on the screen. /// </summary> /// <param name="point">The screen position in pixels.</param> public Vector3 ScreenPointToWorldDirection(Vector2 point) { ValidateDispose(); Vector3 clipPoint = new Vector3( 2.0f * ((point.X / m_resolutionX) - 0.5f), 2.0f * (-((point.Y / m_resolutionY) - 0.5f)), 1.0f ); Vector3 worldPoint = Vector3.TransformPerspective(clipPoint, ProjectionMatrix.Inverted() * ViewMatrixInverse); return((worldPoint - Transform.Position).Normalized()); }
public void ProjectionMatrixTest() { Ngon n = Program.generateRandomNgon(10); double[][] P = new ProjectionMatrix(n).columnVectors; for (int i = 0; i < P.Count(); i++) { for (int j = 0; j < P[i].Count(); j++) { Assert.AreEqual(P[i][j], P[j][i]); } } }
public ProjectionMatrix ProjectionMatrixTest(Ngon ngon) { ProjectionMatrix P = new ProjectionMatrix(ngon); for (int j = 0; j < ngon.getOrthonormal().Length; j++) { for (int i = 0; i < ngon.getOrthonormal()[j].Length; i++) { //double fromEdgeVectors = ngon.getOrthonormal()[i][0] * ngon.getOrthonormal()[j][0] + ngon.getOrthonormal()[i][1] * ngon.getOrthonormal()[j][1]; //Assert.AreEqual(fromEdgeVectors, P.columnVectors[i][j]); } } return(P); }
override public void OnInspectorGUI() { Debug.Log(System.Environment.StackTrace); DrawDefaultInspector(); ProjectionMatrix pm = target as ProjectionMatrix; pm.ProjectionMode = (ProjectionMode)EditorGUILayout.EnumPopup("ProjectionMode", pm.projectionMode); // if (GUILayout.Button("Update Homography")) { // ProjectionMatrix pm = target as ProjectionMatrix; // // pm.LoadBimberMatrix(pm.pathToMatrixFile); // } }
public Ray3d GetMouseRay() { var start = new Vector4d((Window.Mouse.X / (double)Window.Width - 0.5) * 2.0, ((Window.Height - Window.Mouse.Y) / (double)Window.Height - 0.5) * 2.0, 0.0, 1.0); var end = new Vector4d((Window.Mouse.X / (double)Window.Width - 0.5) * 2.0, ((Window.Height - Window.Mouse.Y) / (double)Window.Height - 0.5) * 2.0, -1.0, 1.0); var IPV = ProjectionMatrix.Inverted() * ViewMatrix.Inverted(); start = Vector4d.Transform(start, IPV); start /= start.W; end = Vector4d.Transform(end, IPV); end /= end.W; end -= start; return(new Ray3d(new Vector3d(start.X, start.Y, start.Z), new Vector3d(end.X, end.Y, end.Z).Normalized())); }
public void PluckerAndProjectionMatrixEquality() { Ngon ngon = Program.generateRandomNgon(4); PluckerMatrix plucker = PluckerMatrixTest(ngon); ProjectionMatrix projection = ProjectionMatrixTest(ngon); Matrix <double> D = Matrix <double> .Build.DenseOfColumnArrays(plucker.columnVectors); string d = D.ToString(); Matrix <double> Dnsq = (D.Multiply(D)).Negate(); string dnsq = Dnsq.ToString(); Matrix <double> P = Matrix <double> .Build.DenseOfColumnArrays(projection.columnVectors); string p = P.ToString(); compareMatricies(Dnsq, P); }
private void Update() { // 改投影矩阵 persp.Update(); var camera = GetComponent <Camera>(); var mat = ProjectionMatrix.Get(persp.current, camera.pixelRect, dist.current); camera.projectionMatrix = mat; // 改旋转角 rotation.Update(); // 改位置 center.Update(); dist.Update(); transform.position = center.current + transform.rotation * new Vector3(0, 0, -dist.current); }
/// <summary> /// Calculates a Line from a Screen coordinate to the Camera. /// </summary> /// <param name="p"></param> /// <returns>A LineType</returns> public LineType FromScr(Point p) { Matrix T = ProjectionMatrix.invert(); xyz pt = new xyz( (p.X + ViewPort.Left - (float)(ViewPort.Left + ViewPort.Right) / 2.0) / ((float)(ViewPort.Right - ViewPort.Left) / 2.0), -(p.Y + ViewPort.Top - (float)(ViewPort.Bottom + ViewPort.Top) / 2.0) / ((float)(ViewPort.Bottom - ViewPort.Top) / 2.0), -1); pt = T * pt; float l = ViewPort.Left; xyz pt2 = new xyz( (p.X + ViewPort.Left - (float)(ViewPort.Left + ViewPort.Right) / 2.0) / ((float)(ViewPort.Right - ViewPort.Left) / 2.0), -(p.Y + ViewPort.Top - (float)(ViewPort.Bottom + ViewPort.Top) / 2.0) / ((float)(ViewPort.Bottom - ViewPort.Top) / 2.0), 1); pt2 = T * pt2; return(new LineType(pt, (pt2 - pt).normalized())); }
public void TestProjectionMatrix() { var projectionMatrix = ProjectionMatrix.GetProjectionMatrix(50 / 10f, 0.25f, 1000); // Result: // { // (0.34641016151377546, 0, 0, 0) // (0, .7320508075688774, 0, 0) // (0, 0, -1.0005001250312577, -1) // (0, 0, -0.50012503125781449, 0) // } Assert.IsTrue(projectionMatrix.M11 == 0.34641016151377546); Assert.IsTrue(projectionMatrix.M22 == 1.7320508075688774); Assert.IsTrue(projectionMatrix.M33 == -1.0005001250312577); Assert.IsTrue(projectionMatrix.M43 == -0.50012503125781449); Assert.IsTrue(projectionMatrix.M41 == 0); Assert.IsTrue(true); }
/// <summary> /// gives the height of the screen in world coordinates. /// <param name="ReferencePoint">when the projection is perspectivly, the referencepoint gives the depth, for which the height will be calcluated. /// When the projection is orthogonal this point is not used. See also <see cref="FieldOfView"/>. /// </param> /// </summary> /// <returns>the width of the screen in world coordinates.</returns> public double getWorldHeight(xyz ReferencePoint) { if (FieldOfView <= 0.01) { return((double)this.ViewPort.Height / PixelsPerUnit); } else { if (ProjectMatrixInvalid) { ProjectMatrixInvert = ProjectionMatrix.invert(); } ProjectMatrixInvalid = false; xyz P = ProjectionMatrix * ReferencePoint; xyz A = ProjectMatrixInvert * new xyz(0, 1, P.z); xyz B = ProjectMatrixInvert * new xyz(0, -1, P.z); return(A.dist(B)); } }
void WriteToStream(Stream where) { using (BinaryWriter writer = new BinaryWriter(where)) { writer.Write(Timestamp); new Vector3Serializer(Position).WriteToStream(writer); new QuaternionSerializer(Rotation).WriteToStream(writer); if (!ProjectionMatrix.Equals(default(Matrix4x4)) || SendProjectionMatrix) { writer.Write(true); new Matrix4Serializer(ProjectionMatrix).WriteToStream(writer); } else { writer.Write(false); } } }
/// <summary> /// Draw this SceneGraph. /// </summary> /// <param name="ctx"> /// The <see cref="GraphicsContext"/> used for drawing. /// </param> /// <param name="programOverride"> /// A <see cref="ShaderProgram"/> that overrides the default one used for rendering the batch. It can be null. /// </param> public void Draw(GraphicsContext ctx, ShaderProgram programOverride) { CheckCurrentContext(ctx); // View parameters SceneRoot.LocalProjection = ProjectionMatrix; SceneRoot.LocalModel = new ModelMatrix(); SceneRoot.LocalModelView = ViewMatrix; SceneRoot.LocalModelViewProjection = new ModelMatrix((Matrix4x4)ProjectionMatrix.Multiply(ViewMatrix)); using (SceneGraphContext ctxScene = new SceneGraphContext(this)) { ObjectBatchContext objectBatchContext = new ObjectBatchContext(); // Traverse the scene graph SceneRoot.TraverseDirect(ctx, ctxScene, _TraverseDrawContext, objectBatchContext); // Generate shadow maps if ((SceneFlags & SceneGraphFlags.Lighting) != 0 && (SceneFlags & SceneGraphFlags.ShadowMaps) != 0) { _LightManager.GenerateShadowMaps(ctx, this); } // Sort geometries List <SceneObjectBatch> sceneObjects = objectBatchContext.Objects; if (((SceneFlags & SceneGraphFlags.StateSorting) != 0) && (_SorterRoot != null)) { sceneObjects = _SorterRoot.Sort(objectBatchContext.Objects); } // Draw all batches KhronosApi.LogComment("*** Draw Graph"); foreach (SceneObjectBatch objectBatch in sceneObjects) { objectBatch.Draw(ctx, programOverride); } // Debug: shadow maps if ((SceneFlags & SceneGraphFlags.Lighting) != 0 && (SceneFlags & SceneGraphFlags.ShadowMaps) != 0) { DisplayShadowMaps(ctx); } } }
/// <summary> /// calculate a pixels to world lenght. /// </summary> /// <param name="P">a refrence point, which is needed in case of perspective representation.</param> /// <param name="Pixels">Number of pixels</param> /// <returns></returns> public double PixelToWorld(Drawing3d.xyz P, int Pixels) { if (ProjectMatrixInvalid) { ProjectMatrixInvert = ProjectionMatrix.invert(); } ProjectMatrixInvalid = false; if (FieldOfView > 0.01) { xyz q = ProjectionMatrix * P; xyz A = ProjectMatrixInvert * (new xyz(0, 0, q.z)); xyz B = ProjectMatrixInvert * (new xyz(1, 0, q.z)); double d = A.dist(B); return(Pixels * d / (ViewPort.Width / 2)); } else { return((ProjectMatrixInvert.multaffin(new xyz((float)Pixels / (float)(ViewPort.Width / 2), 0, 0))).length()); } }
/// <summary> /// gets the world base of the <see cref="ProjectionMatrix"/>. See also <see cref="getProjectionMatrix"/> /// </summary> /// <returns>the projection base.</returns> public Base getProjectionBase() { return(ProjectionMatrix.invert().toBase().Orthogonalyze()); }
public void ProjectionCameraTest() { var projectionMatrix = ProjectionMatrix.GetProjectionMatrix(50 / 10f, 0.25f, 1000); }
void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame) { // Copy the raw image data into the target texture //photoCaptureFrame.UploadImageDataToTexture(targetTexture); Texture2D targetTexture = new Texture2D(cameraParameters.cameraResolutionWidth, cameraParameters.cameraResolutionHeight); string filename = string.Format(@"CapturedImage{0}_n.jpg", Time.time); string filePath = System.IO.Path.Combine(Application.persistentDataPath, filename); photoCaptureFrame.UploadImageDataToTexture(targetTexture); byte[] bytes = targetTexture.EncodeToPNG(); if (bytes != null && bytes.Length > 0) { try { File.WriteAllBytes(filePath, bytes); Debug.Log("write " + filePath); } catch (Exception ex) { Debug.Log("Failed to write Image"); } } Debug.Log("write " + filePath); photoCaptureFrame.TryGetCameraToWorldMatrix(out cameraToWorldMatrix); photoCaptureFrame.TryGetProjectionMatrix(out ProjectionMatrix); Debug.Log("cameraToWorldMatrix = "); Debug.Log(cameraToWorldMatrix.ToString("f4")); Debug.Log("ProjectionMatrix = "); Debug.Log(ProjectionMatrix.ToString("f4")); Debug.Log("camera Position = "); Debug.Log(Camera.main.cameraToWorldMatrix.ToString("f4")); Matrix4x4 mat = Camera.main.cameraToWorldMatrix; mat = mat.inverse * cameraToWorldMatrix; Debug.Log("cameraToLocateCamera = "); Debug.Log(mat.ToString("f4")); if (cliker_times % 3 != 0) { cameraToWorldMatrixVec.Add(cameraToWorldMatrix); Vector3 pos = GameObject.Find("Sphere").transform.position; Vector2 pix = Camera.main.WorldToScreenPoint(pos);//世界坐标(0,0,0),一般可以用transform. screenPosVec.Add(pix); } if (cliker_times % 3 == 0) { cameraToWorldMatrix = cameraToWorldMatrixVec[0]; List <Vector3> line1 = CaculatePixelToWorld(screenPosVec[0]); cameraToWorldMatrix = cameraToWorldMatrixVec[1]; List <Vector3> line2 = CaculatePixelToWorld(screenPosVec[1]); Vector3 dir1 = line1[1] - line1[0]; Vector3 dir2 = line2[1] - line2[0]; Vector3 positon = ClosestPointBetweenRays(line1[0], dir1.normalized, line2[0], dir2.normalized); Debug.Log("positon = "); Debug.Log(positon.ToString("f4")); cameraToWorldMatrixVec.Clear(); screenPosVec.Clear(); } photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode); }
/// <summary> /// Converts ProjectionMatrix to a float[]. /// </summary> /// <returns>A float[] containing all the values from the initial matrix.</returns> public float[] ToArray() { return(ProjectionMatrix.to_array()); }
public override string ToString() { return($"ProjMatrix: {ProjectionMatrix.ToString()} \nViewMatrix: {ViewMatrix.ToString()}"); }