/// <summary> /// Calculates the projection transformation matrix, which transforms 3-D camera or /// view space coordinates into 2-D screen coordinates. /// </summary> protected virtual void ComputeProjectionMatrix(Viewport viewport) { double zNear = this._altitude * 0.1f; double distToCenterOfPlanet = (this._altitude + this.WorldRadius); double tangentalDistance = Math.Sqrt(distToCenterOfPlanet * distToCenterOfPlanet - _worldRadius * _worldRadius); if (World.Settings.Project == Projection.OrthoGraghics) { tangentalDistance *= 1000.0f; } double aspectRatio = (double)viewport.Width / (double)viewport.Height; m_ProjectionMatrix = Matrix4d.PerspectiveFovRH(_fov.Radians, aspectRatio, zNear, tangentalDistance + 100000.0f); }
public static Matrix4d RotationZ(double angle) { //Matrix4d test = ConvertDX.ToMatrix4d(Microsoft.DirectX.Matrix.RotationZ((float)angle)); double dSin = Math.Sin(angle); double dCos = Math.Cos(angle); Matrix4d solution = new Matrix4d(new MatrixV(new double[][] { new double[] { dCos, dSin, 0, 0 }, new double[] { -dSin, dCos, 0, 0 }, new double[] { 0, 0, 1, 0 }, new double[] { 0, 0, 0, 1 } })); return(solution); }
public static Matrix4d LookAtRH(Vector3d cameraPosition, Vector3d cameraTarget, Vector3d cameraUpVector) { //Matrix4d test = ConvertDX.ToMatrix4d(Microsoft.DirectX.Matrix.LookAtRH(ConvertDX.FromVector3d(cameraPosition), ConvertDX.FromVector3d(cameraTarget), ConvertDX.FromVector3d(cameraUpVector))); Vector3d z = Vector3d.normalize(cameraPosition - cameraTarget); Vector3d x = Vector3d.normalize(Vector3d.cross(cameraUpVector, z)); Vector3d y = Vector3d.cross(z, x); Matrix4d solution = new Matrix4d(new MatrixV(new double[][] { new double[] { x.X, y.X, z.X, 0 }, new double[] { x.Y, y.Y, z.Y, 0 }, new double[] { x.Z, y.Z, z.Z, 0 }, new double[] { -Vector3d.dot(x, cameraPosition), -Vector3d.dot(y, cameraPosition), -Vector3d.dot(z, cameraPosition), 1 } })); return(solution); }
public static Matrix4d PerspectiveFovRH(double fieldOfViewY, double aspectRatio, double znearPlane, double zfarPlane) { //Matrix4d test = ConvertDX.ToMatrix4d(Microsoft.DirectX.Matrix.PerspectiveFovRH((float)fieldOfViewY, (float)aspectRatio, (float)znearPlane, (float)zfarPlane)); //return test; double dYScale = Math.Tan(Math.PI / 2.0 - fieldOfViewY / 2.0); double dXScale = dYScale / aspectRatio; Matrix4d solution = new Matrix4d(new MatrixV(new double[][] { new double[] { dXScale, 0, 0, 0 }, new double[] { 0, dYScale, 0, 0 }, new double[] { 0, 0, zfarPlane / (znearPlane - zfarPlane), -1 }, new double[] { 0, 0, znearPlane * zfarPlane / (znearPlane - zfarPlane), 0 } })); return(solution); }
public void Project(object viewport, Matrix4d projection, Matrix4d view, Matrix4d world) { //Microsoft.DirectX.Vector3 test = ConvertDX.FromVector3d(this); //test.Project(viewport, ConvertDX.FromMatrix4d(projection), ConvertDX.FromMatrix4d(view), ConvertDX.FromMatrix4d(world)); Viewport2d vp = ConvertDX.ToViewport2d((Microsoft.DirectX.Direct3D.Viewport)viewport); Matrix4d m = world * view * projection; double w = 1.0; MultiplyMatrix(m, ref w); this.X /= w; this.Y /= w; this.Z /= w; // Make x/y range from 0 to 1 this.X = this.X * 0.5 + 0.5; this.Y = this.Y * 0.5 + 0.5; // Convert to to viewport coordinates (Y is inverted) this.X = this.X * vp.Width + vp.X; this.Y = vp.Height - this.Y * vp.Height + vp.Y; }
public void Unproject(object viewport, Matrix4d projection, Matrix4d view, Matrix4d world) { //Microsoft.DirectX.Vector3 test = ConvertDX.FromVector3d(this); //test.Unproject(viewport, ConvertDX.FromMatrix4d(projection), ConvertDX.FromMatrix4d(view), ConvertDX.FromMatrix4d(world)); Viewport2d vp = ConvertDX.ToViewport2d((Microsoft.DirectX.Direct3D.Viewport)viewport); // Convert from viewport coordinates this.X = (this.X - vp.X) / vp.Width; this.Y = (vp.Height + vp.Y - this.Y) / vp.Height; // Make x/y range from -1 to 1 this.X = this.X * 2.0 - 1.0; this.Y = this.Y * 2.0 - 1.0; Matrix4d m = Matrix4d.Invert(world * view * projection); double w = 1.0; MultiplyMatrix(m, ref w); this.X /= w; this.Y /= w; this.Z /= w; }
public static Matrix4d operator *(Matrix4d left, Matrix4d right) { return(Matrix4d.Multiply(left, right)); }
public static Matrix4d OrthoRH(float width, float height, float znearPlane, float zfarPlane) { Matrix4d test = ConvertDX.ToMatrix4d(Microsoft.DirectX.Matrix.OrthoRH(width, height, znearPlane, zfarPlane)); return(test); }