public void Rotate(Vector2d startPoint, Vector2d endPoint)
        {
            var rotation = CalculateRotation(startPoint, endPoint);
            _tempCameraOrientation = rotation.GetRotationMatrix();

            FireCameraChanged();
        }
 /// <summary>Create a random translation matrix.</summary>
 /// <param name="self"></param>
 /// <param name="min"></param>
 /// <param name="max"></param>
 /// <param name="result"></param>
 public static void NextTranslationMatrix4d(this Random self, double min, double max, out Matrix4d result)
 {
     Vector3d vmin, vmax;
     vmin.X = vmin.Y = vmin.Z = min;
     vmax.X = vmax.Y = vmax.Z = max;
     self.NextTranslationMatrix4d(ref vmin, ref vmax, out result);
 }
        private static bool Project(ref Vector3d world, ref Matrix4d modelviewMatrix, ref Matrix4d projectionMatrix, int[] viewport, out Vector3d screen)
        {
            Vector4d _in = new Vector4d(world, 1.0);
            Vector4d _out = new Vector4d();

            Vector4d.Transform(ref _in, ref modelviewMatrix, out _out);
            Vector4d.Transform(ref _out, ref projectionMatrix, out _in);

            if (_in.W == 0.0)
            {
                screen = Vector3d.Zero;
                return false;
            }

            _in.X /= _in.W;
            _in.Y /= _in.W;
            _in.Z /= _in.W;

            /* Map x, y and z to range 0-1 */
            _in.X = _in.X * 0.5 + 0.5;
            _in.Y = _in.Y * 0.5 + 0.5;
            _in.Z = _in.Z * 0.5 + 0.5;

            /* Map x,y to viewport */
            _in.X = _in.X * viewport[2] + viewport[0];
            _in.Y = _in.Y * viewport[3] + viewport[1];

            screen = new Vector3d(_in);
            return true;
        }
            public StereoTransformApply(Matrix4x4 Left, Matrix4x4 Right, MatrixMode Mode, LoadOrMultiply LoadOrMultiply)
			{
				this.Left = UMath.ToGL(Left);
                this.Right = UMath.ToGL(Right);
				this.Mode = Mode;
				this.LoadOrMultiply = LoadOrMultiply;
			}
Beispiel #5
0
 public Matrix4(double m11, double m12, double m13, double m14, double m21, double m22, double m23, double m24, double m31, double m32, double m33, double m34, double m41, double m42, double m43, double m44)
     : this()
 {
     OpenTKEquivalent = new Matrix4d(m11, m12, m13, m14,
                                     m21, m22, m23, m24,
                                     m31, m32, m33, m34,
                                     m41, m42, m43, m44);
 }
        public static Vector3d WorldToScreen(Vector3d pos, Matrix4d modelviewMatrix, Matrix4d projectionMatrix, int[] viewport)
        {
            Vector3d point;
            Project(ref pos, ref modelviewMatrix, ref projectionMatrix, viewport, out point);
            point.Y = (float)viewport[3] - point.Y;

            return point;
        }
Beispiel #7
0
 public List<KeyValuePair<string, string>> GetOpenGLPNameInfo()
 {
     List<KeyValuePair<string, string>> info = 
         new List<KeyValuePair<string, string>>(); 
     foreach (GetPName pname in Enum.GetValues(typeof(GetPName)))
     {
         double[] buff = new double[32];
         GL.GetDouble(pname, buff);
         int last = 0;
         for (int i = 0; i < 32; i++)
         {
             if (buff[i] != 0.0)
             {
                 last = i + 1;
             }
         }
         string str = null;
         switch (last)
         {
             case 0:
                 str = "0";
                 break;
             case 1:
                 str = buff[0].ToString();
                 break;
             case 2:
                 Vector2d v2 = new Vector2d(buff[0], buff[1]);
                 str = v2.ToString();
                 break;
             case 3:
                 Vector3d v3 = new Vector3d(buff[0], buff[1], buff[2]);
                 str = v3.ToString();
                 break;
             case 4:
                 Vector4d v4 = new Vector4d(buff[0], buff[1], buff[2], buff[3]);
                 str = v4.ToString();
                 break;
             case 16:
                 Matrix4d m4 = new Matrix4d(buff[0], buff[1], buff[2], buff[3],
                                         buff[4], buff[5], buff[6], buff[7],
                                         buff[8], buff[9], buff[10], buff[11],
                                         buff[12], buff[13], buff[14], buff[15]);
                 str = m4.ToString();
                 break;
             default:
                 StringBuilder sb = new StringBuilder();
                 for (int i = 0; i < last; i++)
                 {
                     sb.Append(buff[i]);
                     sb.Append(',');
                 }
                 str = sb.ToString();
                 break;
         }
         info.Add(new KeyValuePair<string, string>(pname.ToString(), str));
     } 
     return info;
 }
        public void Render(ICamera camera, ICamera lodCamera, Vector3d lightPosition)
        {
            var transformation = new Matrix4d(
                new Vector4d(1, 0, 0, 0),
                new Vector4d(0, 0, 1, 0),
                new Vector4d(0, 1, 0, 1),
                new Vector4d(0, 0, 0, 1));

            var visibleChunks = _chunkedLod.Calculate(
                _tree,
                lodCamera.Width,
                lodCamera.HorizontalFieldOfView,
                Vector3d.Transform(lodCamera.Position, transformation),
                30,
                FrustumPlaneExtractor.ExtractRowMajor(transformation * lodCamera.ComputeCameraMatrix() * lodCamera.ComputeProjectionMatrix()));

            GL.ClearColor(Color4.White);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            GL.Enable(EnableCap.DepthTest);
            GL.CullFace(CullFaceMode.Back);
            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Cw);

            _simpleMaterial.Bind();

            _simpleMaterial.ProjectionMatrix.Set(camera.ComputeProjectionMatrix().ToMatrix4());
            _simpleMaterial.ViewMatrix.Set(camera.ComputeCameraMatrix().ToMatrix4());
            _simpleMaterial.LightDirection.Set(new Vector3(1, 1, 0).Normalized());

            _simpleMaterial.Color.Set(new Vector4(0.9f, 0.9f, 0.9f, 1.0f));

            foreach (var chunkedLodTreeNode in visibleChunks)
            {
                var renderableMesh = _cache.GetRenderable(chunkedLodTreeNode.Bounds);
                if (renderableMesh == null)
                    continue;

                renderableMesh.CreateVAO();
                renderableMesh.VertexArrayObject.Bind();

                var bounds = chunkedLodTreeNode.Bounds;
                var translation = Matrix4.CreateTranslation((float)bounds.Center.X, 0, (float)bounds.Center.Y);
                var delta = bounds.Max - bounds.Min;
                var scale = Matrix4.CreateScale((float)delta.X, 1, (float)delta.Y);

                var modelMatrix = scale * translation;
                _simpleMaterial.ModelMatrix.Set(modelMatrix);
                _simpleMaterial.NormalToWorld3x3.Set(new Matrix3(Matrix4.Transpose(modelMatrix.Inverted())));

                _simpleMaterial.LightPosition.Set((Vector3)lightPosition);

                GL.DrawElements(BeginMode.Triangles, renderableMesh.Faces * 3, DrawElementsType.UnsignedInt, 0);

                renderableMesh.VertexArrayObject.Unbind();
            }
            _simpleMaterial.Unbind();
        }
Beispiel #9
0
 public static Vector3[] Transform(Vector3[] vectors, Matrix4d matrix)
 {
     Vector3[] vList = new Vector3[vectors.Length];
     for (int i = 0; i < vectors.Length; i++)
     {
         vList[i] = (Vector3)Vector3d.Transform(new Vector3d(vectors[i].X, vectors[i].Y, vectors[i].Z), matrix);
     }
     return vList;
 }
Beispiel #10
0
		public static Matrix4d ToGL(Matrix4x4 matrix)
		{
			Matrix4d value = new Matrix4d(
				matrix.m11, matrix.m12, matrix.m13, matrix.m14,
				matrix.m21, matrix.m22, matrix.m23, matrix.m24,
				matrix.m31, matrix.m32, matrix.m33, matrix.m34,
				matrix.m41, matrix.m42, matrix.m43, matrix.m44
				);
			return value;
		}
Beispiel #11
0
        public void CommitRotation(Vector2d startPoint, Vector2d endPoint)
        {
            var rotation = CalculateRotation(startPoint, endPoint);
            var rotationMatrix = rotation.GetRotationMatrix();

            _cameraOrientation = _cameraOrientation.Multiply(rotationMatrix);
            _tempCameraOrientation = Matrix4d.Identity;

            FireCameraChanged();
        }
 public void moveCamera(Vector3d whatPosition)
 {
     Vector3d normalizedConnection = Vector3d.NormalizeFast(position - target);
     double cosine = Vector3d.Dot(normalizedConnection, Vector3d.UnitY);
     if (!(cosine < 1.02 && cosine > 0.98) && !(cosine > -1.02 && cosine < -0.98))
     {
         position = whatPosition;
         cameraMatrix = Matrix4d.LookAt(position, target, Vector3d.UnitY);
     }
 }
Beispiel #13
0
 // https://gist.github.com/871099/8d37734ba22737c69173c2e44eaa332f9c85bcde
 // http://www.opentk.com/node/1892
 // http://www.opentk.com/node/1276
 // http://www.opentk.com/node/887
 // http://mesa3d.org/
 /// <summary>
 /// Projects a coordinate from world space into screen space.
 /// </summary>
 /// <param name="coordinate">The coordinate to project</param>
 /// <param name="viewport">The viewport dimensions</param>
 /// <param name="projection">The projection matrix</param>
 /// <param name="modelview">The modelview matrix</param>
 /// <returns>The coordinate in screen space.</returns>
 public static Coordinate Project(Coordinate coordinate, int[] viewport, Matrix4d projection, Matrix4d modelview)
 {
     var source = new Vector4d(coordinate.DX, coordinate.DY, coordinate.DZ, 1);
     var imed = Vector4d.Transform(source, modelview);
     var vector = Vector4d.Transform(imed, projection);
     if (Math.Abs(vector.W - 0) < 0.00001) return null;
     var result = Vector3d.Divide(vector.Xyz, vector.W);
     result.X = viewport[0] + viewport[2] * (result.X + 1) / 2;
     result.Y = viewport[1] + viewport[3] * (result.Y + 1) / 2;
     result.Z = (result.Z + 1) / 2;
     return new Coordinate((decimal) result.X, (decimal) result.Y, (decimal) result.Z);
 }
Beispiel #14
0
 public ImageLayer()
 {
     objectToWorld = Matrix4d.Identity;
     WorldToObject = Matrix4d.Identity;
     Plane = new Plane()
     {
         Origin = new Vector3d(0, 0, -1),
         Normal = new Vector3d(0, 0, 1)
     };
     Depth = -20;
     FieldOfView = 0.5;
     RasterSize = new Size(1, 1);
 }
Beispiel #15
0
 /// <summary>
 /// Converts a screen space point into a corresponding point in world space.
 /// </summary>
 /// <param name="coordinate">The coordinate to project</param>
 /// <param name="viewport">The viewport dimensions</param>
 /// <param name="projection">The projection matrix</param>
 /// <param name="modelview">The modelview matrix</param>
 /// <returns>The coordinate in world space.</returns>
 public static Coordinate Unproject(Coordinate coordinate, int[] viewport, Matrix4d projection, Matrix4d modelview)
 {
     var matrix = Matrix4d.Invert(Matrix4d.Mult(modelview, projection));
     var source = new Vector4d(
         (coordinate.DX - viewport[0]) * 2 / viewport[2] - 1,
         (coordinate.DY - viewport[1]) * 2 / viewport[3] - 1,
         2 * coordinate.DZ - 1,
         1);
     var vector = Vector4d.Transform(source, matrix);
     if (Math.Abs(vector.W - 0) < 0.00001) return null;
     var result = Vector3d.Divide(vector.Xyz, vector.W);
     return new Coordinate((decimal) result.X, (decimal) result.Y, (decimal) result.Z);
 }
Beispiel #16
0
 public static bool AlmostEqual(Matrix4d matrix0, Matrix4d matrix1, double delta, double percent)
 {
     for (int i = 0; i < MATRIX_4_SIZE; i++)
     {
         for (int j = 0; j < MATRIX_4_SIZE; j++)
         {
             if (Math.Abs(matrix0[i, j] - matrix1[i, j]) > delta && Math.Abs(1 - matrix1[i, j] / matrix0[i, j]) > percent)
             {
                 return false;
             }
         }
     }
     return true;
 }
        public static void Multiply(Matrix4d* mLeft, Matrix4d* mRight, Matrix4d* mOut)
        {
            double* s1 = mLeft->_data, s2 = mRight->_data;
            double* dPtr = mOut->_data;
            int index = 0;
            double val;

            for (int b = 0; b < 16; b += 4)
                for (int a = 0; a < 4; a++)
                {
                    val = 0.0;
                    for (int x = b, y = a; y < 16; y += 4)
                        val += s1[x++] * s2[y];
                    dPtr[index++] = val;
                }
        }
        public static Vector3d ScreenToWorld(Vector2d pos, Matrix4d modelviewMatrix, Matrix4d projectionMatrix, int[] viewport)
        {
            Vector3d win = Vector3d.Zero;
            win.X = pos.X;
            win.Y = (viewport[3] - pos.Y);

            double zz = WorldToScreen(Vector3d.Zero, modelviewMatrix, projectionMatrix, viewport).Z;

            float[] boxedZ = new float[1];
            GL.ReadPixels((int)pos.X, viewport[3] - (int)pos.Y, 1, 1, PixelFormat.DepthComponent, PixelType.Float, boxedZ);
            win.Z = boxedZ[0];
            win.Z = zz;

            System.Diagnostics.Debug.Print("{0}, {1}", win.Z, zz);

            return UnProject(win, modelviewMatrix, projectionMatrix, viewport);
        }
        public void OnLoad(object sender, EventArgs e)
        {
            // Check for necessary capabilities:
            var version = new Version(GL.GetString(StringName.Version).Substring(0, 3));
            var target = new Version(2, 0);
            if (version < target)
            {
                throw new NotSupportedException(
                    String.Format(CultureInfo.InvariantCulture,
                    "OpenGL {0} is required (you only have {1}).", target, version));
            }

            cameraPosition = Matrix4d.LookAt(eyeX: 8, eyeY: 8, eyeZ: -11, targetX: 2, targetY: -1, targetZ: -7, upX: 0.0, upY: 1.0f, upZ: 0);

            VSync = VSyncMode.On;
            fullScreenMode();
            music.StartMusic();
        }
 public override void Render()
 {
     if (model == null)
     {
         return;
     }
     TheClient.SetEnts();
     if (TheClient.RenderTextures)
     {
         TheClient.Textures.White.Bind();
     }
     TheClient.Rendering.SetMinimumLight(0f);
     BEPUutilities.Matrix matang = BEPUutilities.Matrix.CreateFromQuaternion(Angles);
     //matang.Transpose();
     Matrix4d matang4 = new Matrix4d(matang.M11, matang.M12, matang.M13, matang.M14,
         matang.M21, matang.M22, matang.M23, matang.M24,
         matang.M31, matang.M32, matang.M33, matang.M34,
         matang.M41, matang.M42, matang.M43, matang.M44);
     Matrix4d mat = matang4 * Matrix4d.CreateTranslation(ClientUtilities.ConvertD(GetPosition()));
     TheClient.MainWorldView.SetMatrix(2, mat);
     model.Draw(); // TODO: Animation?
 }
Beispiel #21
0
 public static void GetDouble(GetPName pname, out Matrix4d matrix)
 {
     unsafe
     {
         fixed (Matrix4d* ptr = &matrix)
             GetDouble(pname, (double*)ptr);
     }
 }
 /// <summary>Create a random translation matrix.</summary>
 /// <param name="self"></param>
 /// <param name="min"></param>
 /// <param name="max"></param>
 /// <param name="result"></param>
 public static void NextTranslationMatrix4d(this Random self, ref Vector3d min, ref Vector3d max, out Matrix4d result)
 {
     Vector3d amount;
     self.NextVector3dInRange(ref min, ref max, out amount);
     Matrix4d.Translate(ref amount, out result);
 }
Beispiel #23
0
 /// <summary>
 /// Creates and returns an OpenGL Matrix4d object based on this Matrix
 /// </summary>
 public Matrix4d ConvertToMatrix4d()
 {
     Matrix4d matrix = new Matrix4d(
                           A, B, 0, 0,
                           C, D, 0, 0,
                           0, 0, 1, 0,
                           Tx, Ty, 0, 1
                       ); 
     return matrix;
 }
        private void SetOrthographic(double width = 192, double height = 108) {
            GL.MatrixMode(MatrixMode.Projection);
            Matrix4d Projection = Matrix4d.CreateOrthographic(width, height, 1.0f, 6400.0f);
            this.projection = Projection;
			GL.LoadMatrix(ref projection);
        }
 public void SetModelView() {
     Matrix4d modelview = Matrix4d.LookAt((Vector3d)eye, (Vector3d)lookat, Vector3d.UnitY);
     this.model = modelview;
     GL.MatrixMode(MatrixMode.Modelview);
     GL.LoadMatrix(ref modelview);
 }
 private void SetPerspective() {
     GL.MatrixMode(MatrixMode.Projection);
     Matrix4d projection = Matrix4d.CreatePerspectiveFieldOfView(fov, width / (float)height, 1.0f, 12800.0f);
     this.projection = projection;
     GL.LoadMatrix(ref projection);
 }
Beispiel #27
0
 public static void MultTransposeMatrix(ref Matrix4d mat)
 {
     unsafe
     {
         fixed (Double* m_ptr = &mat.Row0.X)
         {
             GL.MultTransposeMatrix((Double*)m_ptr);
         }
     }
 }
Beispiel #28
0
 public static void LoadMatrix(ref Matrix4d mat)
 {
     unsafe
     {
         fixed (Double* m_ptr = &mat.Row0.X)
         {
             GL.LoadMatrix((Double*)m_ptr);
         }
     }
 }
Beispiel #29
0
        private void CalculateMatrices()
        {
            Rectangle viewport = GetViewport3D();
            _projectionMatrix = Matrix4d.Perspective(45, viewport.Width / (double) viewport.Height, 4, 512);

            Bounds3 vec = Bounds3.EmptyBounds;
            Bounds3 allBounds = Bounds3.EmptyBounds;
            int count = 0;

            if (CurrentModel != null)
            {
                int meshIndex = 0;
                foreach (Mesh mesh in CurrentModel.Meshes)
                {
                    allBounds += mesh.Bounds;

                    if (CurrentModel.PartsEnabled[meshIndex])
                    {
                        vec += mesh.Bounds;
                        count++;
                    }

                    meshIndex++;
                }
            }

            if (count == 0)
                vec.Mins = vec.Maxs = allBounds.Mins = allBounds.Maxs = Vector3.Zero;

            GrassY = allBounds.Maxs.Y;
            Vector3 center = vec.Center;

            _viewMatrix =
                Matrix4.CreateTranslation(-center.X + _3DOffset.X, -center.Y + _3DOffset.Y, -center.Z + _3DOffset.Z) *
                Matrix4.CreateFromAxisAngle(new Vector3(0, -1, 0), MathHelper.DegreesToRadians(_3DRotationY)) *
                Matrix4.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.DegreesToRadians(_3DRotationX)) *
                Matrix4.CreateTranslation(0, 0, _3DZoom);

            _cameraMatrix = _viewMatrix;
            _cameraMatrix.Invert();

            CameraPosition = Vector3.TransformPosition(Vector3.Zero, _cameraMatrix);
        }
Beispiel #30
0
 public Ray Transform(Matrix4d matrix)
 {
     Vector3d newOrigin = Vector3d.Transform(Origin, matrix);
     // TODO: direction cannot be transformed with the same matrix as position!!!
     // use the inverse transpose !!!
     Vector3d newDirection = Vector3d.Transform(Direction, matrix);
     return new Ray(newOrigin, newDirection);
 }