Exemple #1
0
        /// <summary>
        /// Accumulate a scaling to this model matrix.
        /// </summary>
        /// <param name="s">
        /// A <see cref="Single"/> holding the scaling factor on X, Y and Z axes.
        /// </param>
        public void Scale(float s)
        {
            ModelMatrix matrix = new ModelMatrix();

            matrix.SetScale(s);

            Set(this * matrix);
        }
Exemple #2
0
        /// <summary>
        /// Accumulate a scaling to this model matrix.
        /// </summary>
        /// <param name="s">
        /// A <see cref="Vertex3f"/> holding the scaling factors on three dimensions.
        /// </param>
        public void Scale(Vertex3f s)
        {
            ModelMatrix matrix = new ModelMatrix();

            matrix.SetScale(s);

            Set(this * matrix);
        }
Exemple #3
0
        /// <summary>
        /// Accumulate a scaling to this model matrix.
        /// </summary>
        /// <param name="x">
        /// A <see cref="Double"/> holding the scaling factor on X axis.
        /// </param>
        /// <param name="y">
        /// A <see cref="Double"/> holding the scaling factor on Y axis.
        /// </param>
        /// <param name="z">
        /// A <see cref="Double"/> holding the scaling factor on Z axis.
        /// </param>
        public void Scale(float x, float y, float z)
        {
            ModelMatrix matrix = new ModelMatrix();

            matrix.SetScale(x, y, z);

            Set(this * matrix);
        }
Exemple #4
0
        /// <summary>
        /// Accumulate a scaling to this model matrix.
        /// </summary>
        /// <param name="s">
        /// A <see cref="Vertex3f"/> holding the scaling factors on three dimensions.
        /// </param>
        public void Scale(Vertex3f s)
        {
            ModelMatrix scaleModel = new ModelMatrix();

            scaleModel.SetScale(s);

            Set(this * scaleModel);
        }
Exemple #5
0
        /// <summary>
        /// Translate a ModelMatrix in two-dimensional space.
        /// </summary>
        /// <param name="m"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static ModelMatrix operator -(ModelMatrix m, Vertex2f v)
        {
            ModelMatrix res = new ModelMatrix(m);

            res.Translate(-v);

            return(res);
        }
Exemple #6
0
        /// <summary>
        /// Accumulate a scaling to this model matrix.
        /// </summary>
        /// <param name="x">
        /// A <see cref="Double"/> holding the scaling factor on X axis.
        /// </param>
        /// <param name="y">
        /// A <see cref="Double"/> holding the scaling factor on Y axis.
        /// </param>
        /// <param name="z">
        /// A <see cref="Double"/> holding the scaling factor on Z axis.
        /// </param>
        public void Scale(float x, float y, float z)
        {
            ModelMatrix scaleModel = new ModelMatrix();

            scaleModel.SetScale(x, y, z);

            Set(this * scaleModel);
        }
Exemple #7
0
        /// <summary>
        /// Accumulate a scaling to this model matrix.
        /// </summary>
        /// <param name="s">
        /// A <see cref="Single"/> holding the scaling factor on X, Y and Z axes.
        /// </param>
        public void Scale(float s)
        {
            ModelMatrix scaleModel = new ModelMatrix();

            scaleModel.SetScale(s);

            Set(this * scaleModel);
        }
Exemple #8
0
        /// <summary>
        /// Accumulate a translation on this ModelMatrix.
        /// </summary>
        /// <param name="x">
        /// A <see cref="float"/> indicating the translation on X axis.
        /// </param>
        /// <param name="y">
        /// A <see cref="float"/> indicating the translation on Y axis.
        /// </param>
        public void Translate(float x, float y)
        {
            ModelMatrix matrix = new ModelMatrix();

            matrix[3, 0] = x;
            matrix[3, 1] = y;

            Set(this * matrix);
        }
Exemple #9
0
        /// <summary>
        /// Accumulate a translation on this ModelMatrix.
        /// </summary>
        /// <param name="p">
        /// A <see cref="Vertex2f"/> that specify the translation.
        /// </param>
        public void Translate(Vertex2f p)
        {
            ModelMatrix matrix = new ModelMatrix();

            matrix[3, 0] = p.x;
            matrix[3, 1] = p.y;

            Set(this * matrix);
        }
Exemple #10
0
        /// <summary>
        /// Accumulate a translation on this ModelMatrix.
        /// </summary>
        /// <param name="p">
        /// A <see cref="Vertex2f"/> that specify the translation.
        /// </param>
        public void Translate(Vertex2f p)
        {
            ModelMatrix translationMatrix = new ModelMatrix();

            translationMatrix[3, 0] = p.x;
            translationMatrix[3, 1] = p.y;

            Set(this * translationMatrix);
        }
Exemple #11
0
        /// <summary>
        /// Accumulate a translation on this ModelMatrix.
        /// </summary>
        /// <param name="x">
        /// A <see cref="float"/> indicating the translation on X axis.
        /// </param>
        /// <param name="y">
        /// A <see cref="float"/> indicating the translation on Y axis.
        /// </param>
        public void Translate(float x, float y)
        {
            ModelMatrix translationMatrix = new ModelMatrix();

            translationMatrix[3, 0] = x;
            translationMatrix[3, 1] = y;

            Set(this * translationMatrix);
        }
Exemple #12
0
        /// <summary>
        /// Compute the product of two ModelMatrix.
        /// </summary>
        /// <param name="m1">
        /// A <see cref="Matrix4x4"/> that specify the left multiplication operand.
        /// </param>
        /// <param name="m2">
        /// A <see cref="Matrix4x4"/> that specify the right multiplication operand.
        /// </param>
        /// <returns>
        /// A <see cref="Matrix4x4"/> resulting from the product of the matrix <paramref name="m1"/> and
        /// the matrix <paramref name="m2"/>. This operator is used to concatenate successive transformations.
        /// </returns>
        public static ModelMatrix operator *(ModelMatrix m1, ModelMatrix m2)
        {
            // Allocate product matrix
            ModelMatrix prod = new ModelMatrix();

            // Compute product
            ComputeMatrixProduct(prod, m1, m2);

            return(prod);
        }
Exemple #13
0
        /// <summary>
        /// Setup this matrix to represent a rotation on the Z axis.
        /// </summary>
        /// <param name="angle">
        /// The rotation angle, in degree.
        /// </param>
        public void RotateZ(float angle)
        {
            ModelMatrix matrix = new ModelMatrix();

            float cosa = (float)Math.Cos(Angle.ToRadians(angle));
            float sina = (float)Math.Sin(Angle.ToRadians(angle));

            matrix[0, 0] = +cosa;
            matrix[1, 0] = -sina;
            matrix[0, 1] = +sina;
            matrix[1, 1] = +cosa;

            Set(this * matrix);
        }
Exemple #14
0
        /// <summary>
        /// Setup this matrix to represent a rotation on the Y axis.
        /// </summary>
        /// <param name="angle">
        /// The rotation angle, in degree.
        /// </param>
        public void RotateY(float angle)
        {
            ModelMatrix rotationMatrix = new ModelMatrix();

            float cosa = (float)Math.Cos(Angle.ToRadians(angle));
            float sina = (float)Math.Sin(Angle.ToRadians(angle));

            rotationMatrix[0, 0] = +cosa;
            rotationMatrix[2, 0] = +sina;
            rotationMatrix[0, 2] = -sina;
            rotationMatrix[2, 2] = +cosa;

            Set(this * rotationMatrix);
        }
Exemple #15
0
        /// <summary>
        /// Compute the transpose of this ModelMatrix.
        /// </summary>
        /// <returns>
        /// A <see cref="ModelMatrix"/> which hold the transpose of this ModelMatrix.
        /// </returns>
        public new ModelMatrix Transpose()
        {
            ModelMatrix transpose = new ModelMatrix();

            // Transpose matrix
            for (uint c = 0; c < 4; c++)
            {
                for (uint r = 0; r < 4; r++)
                {
                    transpose[r, c] = this[c, r];
                }
            }

            return(transpose);
        }
        /// <summary>
        /// Multiply this IModelMatrix with another IModelMatrix.
        /// </summary>
        /// <param name="a">
        /// A <see cref="IModelMatrix"/> that specify the right multiplication operand.
        /// </param>
        /// <returns>
        /// A <see cref="IModelMatrix"/> resulting from the product of this matrix and the matrix <paramref name="a"/>.
        /// </returns>
        IModelMatrix IModelMatrix.Multiply(IModelMatrix a)
        {
            ModelMatrixDouble rModelMatrix = new ModelMatrixDouble();

            ModelMatrixDouble modelMatrixDouble = a as ModelMatrixDouble;

            if (modelMatrixDouble != null)
            {
                ComputeMatrixProduct(rModelMatrix, this, modelMatrixDouble);
                return(rModelMatrix);
            }

            ModelMatrix modelMatrix = a as ModelMatrix;

            if (modelMatrix != null)
            {
                ComputeMatrixProduct(rModelMatrix, this, modelMatrix);
                return(rModelMatrix);
            }

            throw new NotSupportedException(String.Format("multiplication of {0} by {1} is not supported", GetType(), a.GetType()));
        }
Exemple #17
0
        /// <summary>
        /// Setup this matrix to view the universe in a certain direction.
        /// </summary>
        /// <param name="eyePosition">
        /// A <see cref="Vertex3f"/> that specify the eye position, in local coordinates.
        /// </param>
        /// <param name="forwardVector">
        /// A <see cref="Vertex3f"/> that specify the direction of the view. It will be normalized.
        /// </param>
        /// <param name="upVector">
        /// A <see cref="Vertex3f"/> that specify the up vector of the view camera abstraction. It will be normalized
        /// </param>
        /// <returns>
        /// It returns a view transformation matrix used to transform the world coordinate, in order to view
        /// the world from <paramref name="eyePosition"/>, looking at <paramref name="forwardVector"/> having
        /// an up direction equal to <paramref name="upVector"/>.
        /// </returns>
        public void LookAtDirection(Vertex3f eyePosition, Vertex3f forwardVector, Vertex3f upVector)
        {
            Vertex3f rightVector;

            // Normalize forward vector
            forwardVector.Normalize();
            // Normalize up vector
            upVector.Normalize();
            // Compute the right vector (cross-product between forward and up vectors; right is perperndicular to the plane)
            rightVector = forwardVector ^ upVector;
            rightVector.Normalize();
            // Derive up vector
            upVector = rightVector ^ forwardVector;
            upVector.Normalize();

            // Compute view matrix
            ModelMatrix lookatMatrix = new ModelMatrix(), positionMatrix = new ModelMatrix();

            // Row 0: right vector
            lookatMatrix[0, 0] = rightVector.x;
            lookatMatrix[1, 0] = rightVector.y;
            lookatMatrix[2, 0] = rightVector.z;
            // Row 1: up vector
            lookatMatrix[0, 1] = upVector.x;
            lookatMatrix[1, 1] = upVector.y;
            lookatMatrix[2, 1] = upVector.z;
            // Row 2: opposite of forward vector
            lookatMatrix[0, 2] = forwardVector.x;
            lookatMatrix[1, 2] = forwardVector.y;
            lookatMatrix[2, 2] = forwardVector.z;

            // Eye position
            positionMatrix.Translate(-eyePosition);

            // Complete look-at matrix
            Set(lookatMatrix * positionMatrix);
            Set(GetInverseMatrix());
        }
Exemple #18
0
        /// <summary>
        /// Setup this matrix to view the universe in a certain direction.
        /// </summary>
        /// <param name="eyePosition">
        /// A <see cref="Vertex3f"/> that specify the eye position, in local coordinates.
        /// </param>
        /// <param name="forwardVector">
        /// A <see cref="Vertex3f"/> that specify the direction of the view. It will be normalized.
        /// </param>
        /// <param name="upVector">
        /// A <see cref="Vertex3f"/> that specify the up vector of the view camera abstraction. It will be normalized
        /// </param>
        /// <returns>
        /// It returns a view transformation matrix used to transform the world coordinate, in order to view
        /// the world from <paramref name="eyePosition"/>, looking at <paramref name="forwardVector"/> having
        /// an up direction equal to <paramref name="upVector"/>.
        /// </returns>
        public void LookAtDirection(Vertex3f eyePosition, Vertex3f forwardVector, Vertex3f upVector)
        {
            Vertex3f rightVector;

            forwardVector.Normalize();
            upVector.Normalize();
            rightVector = forwardVector ^ upVector;
            if (rightVector.Module() <= 0.0f)
            {
                rightVector = Vertex3f.UnitX;
            }
            rightVector.Normalize();
            upVector = rightVector ^ forwardVector;
            upVector.Normalize();

            // Compute view matrix
            ModelMatrix lookatMatrix = new ModelMatrix();

            // Row 0: right vector
            lookatMatrix[0, 0] = rightVector.x;
            lookatMatrix[1, 0] = rightVector.y;
            lookatMatrix[2, 0] = rightVector.z;
            // Row 1: up vector
            lookatMatrix[0, 1] = upVector.x;
            lookatMatrix[1, 1] = upVector.y;
            lookatMatrix[2, 1] = upVector.z;
            // Row 2: opposite of forward vector
            lookatMatrix[0, 2] = -forwardVector.x;
            lookatMatrix[1, 2] = -forwardVector.y;
            lookatMatrix[2, 2] = -forwardVector.z;

            // Eye position
            lookatMatrix.Translate(-eyePosition);

            // Complete look-at matrix
            Set(lookatMatrix);
        }
		/// <summary>
		/// Setup this matrix to represent a rotation on the Y axis.
		/// </summary>
		/// <param name="angle">
		/// The rotation angle, in degree.
		/// </param>
		public void RotateY(float angle)
		{
			ModelMatrix rotationMatrix = new ModelMatrix();

			float cosa = (float)Math.Cos(Angle.ToRadians(angle));
			float sina = (float)Math.Sin(Angle.ToRadians(angle));

			rotationMatrix[0, 0] = +cosa;
			rotationMatrix[2, 0] = +sina;
			rotationMatrix[0, 2] = -sina;
			rotationMatrix[2, 2] = +cosa;

			Set(this * rotationMatrix);
		}
Exemple #20
0
		private void SampleGraphicsControl_Render(object sender, GraphicsControlEventArgs e)
		{
			GraphicsContext ctx = e.Context;
			GraphicsSurface framebuffer = e.Framebuffer;

			if (_AnimationBegin == DateTime.MinValue)
				_AnimationBegin = DateTime.UtcNow;

			PerspectiveProjectionMatrix matrixProjection = new PerspectiveProjectionMatrix();
			Matrix4x4 matrixView;

			// Set projection
			matrixProjection.SetPerspective(60.0f, (float)ClientSize.Width / (float)ClientSize.Height, 1.0f, 1000.0f);
			// Set view
			ModelMatrix matrixViewModel = new ModelMatrix();
			matrixViewModel.RotateX(_ViewElevation);
			matrixViewModel.RotateY(_ViewAzimuth);
			matrixViewModel.Translate(0.0f, 0.0f, _ViewDistance);
			matrixView = matrixViewModel.GetInverseMatrix();

			_NewtonProgram.Bind(ctx);
			_NewtonProgram.SetUniform(ctx, "hal_ModelViewProjection", matrixProjection * matrixView);
			_NewtonProgram.SetUniform(ctx, "hal_FrameTimeInterval", (float)(DateTime.UtcNow - _AnimationBegin).TotalSeconds);

			_NewtonVertexArray.Draw(ctx, _NewtonProgram);

			SwapNewtonVertexArrays();

			// Issue another rendering
			SampleGraphicsControl.Invalidate();
		}
Exemple #21
0
		/// <summary>
		/// Multiply this IModelMatrix with another IModelMatrix.
		/// </summary>
		/// <param name="a">
		/// A <see cref="IModelMatrix"/> that specifies the right multiplication operand.
		/// </param>
		/// <returns>
		/// A <see cref="IModelMatrix"/> resulting from the product of this matrix and the matrix <paramref name="a"/>.
		/// </returns>
		IModelMatrix IModelMatrix.Multiply(IModelMatrix a)
		{
			ModelMatrix rModelMatrix = new ModelMatrix();

			ModelMatrix modelMatrix = a as ModelMatrix;
			if (modelMatrix != null) {
				ComputeMatrixProduct(rModelMatrix, this, modelMatrix);
				return (rModelMatrix);
			}

			ModelMatrixDouble modelMatrixDouble = a as ModelMatrixDouble;
			if (modelMatrixDouble != null) {
				ComputeMatrixProduct(rModelMatrix, this, modelMatrixDouble);
				return (rModelMatrix);
			}

			throw new NotSupportedException(String.Format("multiplication of {0} by {1} is not supported", GetType(), a.GetType()));
		}
Exemple #22
0
		/// <summary>
		/// Setup this matrix to view the universe in a certain direction.
		/// </summary>
		/// <param name="eyePosition">
		/// A <see cref="Vertex3f"/> that specify the eye position, in local coordinates.
		/// </param>
		/// <param name="forwardVector">
		/// A <see cref="Vertex3f"/> that specify the direction of the view. It will be normalized.
		/// </param>
		/// <param name="upVector">
		/// A <see cref="Vertex3f"/> that specify the up vector of the view camera abstraction. It will be normalized
		/// </param>
		/// <returns>
		/// It returns a view transformation matrix used to transform the world coordinate, in order to view
		/// the world from <paramref name="eyePosition"/>, looking at <paramref name="forwardVector"/> having
		/// an up direction equal to <paramref name="upVector"/>.
		/// </returns>
		public void LookAtDirection(Vertex3f eyePosition, Vertex3f forwardVector, Vertex3f upVector)
		{
			Vertex3f rightVector;

			// Normalize forward vector
			forwardVector.Normalize();
			// Normalize up vector
			upVector.Normalize();
			// Compute the right vector (cross-product between forward and up vectors; right is perperndicular to the plane)
			rightVector = forwardVector ^ upVector;
			rightVector.Normalize();
			// Derive up vector
			upVector = rightVector ^ forwardVector;
			upVector.Normalize();

			// Compute view matrix
			ModelMatrix lookatMatrix = new ModelMatrix(), positionMatrix = new ModelMatrix();

			// Row 0: right vector
			lookatMatrix[0, 0] = rightVector.x;
			lookatMatrix[1, 0] = rightVector.y;
			lookatMatrix[2, 0] = rightVector.z;
			// Row 1: up vector
			lookatMatrix[0, 1] = upVector.x;
			lookatMatrix[1, 1] = upVector.y;
			lookatMatrix[2, 1] = upVector.z;
			// Row 2: opposite of forward vector
			lookatMatrix[0, 2] = forwardVector.x;
			lookatMatrix[1, 2] = forwardVector.y;
			lookatMatrix[2, 2] = forwardVector.z;

			// Eye position
			positionMatrix.Translate(-eyePosition);

			// Complete look-at matrix
			Set(lookatMatrix * positionMatrix);
			Set(GetInverseMatrix());
		}
Exemple #23
0
		/// <summary>
		/// Accumulate a translation on this ModelMatrix.
		/// </summary>
		/// <param name="x">
		/// A <see cref="float"/> indicating the translation on X axis.
		/// </param>
		/// <param name="y">
		/// A <see cref="float"/> indicating the translation on Y axis.
		/// </param>
		/// <param name="z">
		/// A <see cref="float"/> indicating the translation on Z axis.
		/// </param>
		public void Translate(float x, float y, float z)
		{
			ModelMatrix translationMatrix = new ModelMatrix();

			translationMatrix[3, 0] = x;
			translationMatrix[3, 1] = y;
			translationMatrix[3, 2] = z;

			Set(this * translationMatrix);
		}
Exemple #24
0
		/// <summary>
		/// Translate a ModelMatrix in two-dimensional space.
		/// </summary>
		/// <param name="m"></param>
		/// <param name="v"></param>
		/// <returns></returns>
		public static ModelMatrix operator -(ModelMatrix m, Vertex2f v)
		{
			ModelMatrix res = new ModelMatrix(m);

			res.Translate(-v);

			return (res);
		}
Exemple #25
0
		/// <summary>
		/// Setup this matrix to represent a rotation on the Z axis.
		/// </summary>
		/// <param name="angle">
		/// The rotation angle, in degree.
		/// </param>
		public void RotateZ(float angle)
		{
			ModelMatrix rotationMatrix = new ModelMatrix();

			float cosa = (float)Math.Cos(angle * Angle.DegreeToRadian);
			float sina = (float)Math.Sin(angle * Angle.DegreeToRadian);

			rotationMatrix[0, 0] = +cosa;
			rotationMatrix[1, 0] = -sina;
			rotationMatrix[0, 1] = +sina;
			rotationMatrix[1, 1] = +cosa;

			Set(this * rotationMatrix);
		}
Exemple #26
0
		/// <summary>
		/// Accumulate a scaling to this model matrix.
		/// </summary>
		/// <param name="s">
		/// A <see cref="System.Single"/> holding the scaling factor on X, Y and Z axes.
		/// </param>
		public void Scale(float s)
		{
			ModelMatrix scaleModel = new ModelMatrix();
			scaleModel.SetScale(s);

			Set(this * scaleModel);
		}
Exemple #27
0
		/// <summary>
		/// ModelMatrix copy constructor.
		/// </summary>
		/// <param name="m">
		/// A <see cref="ModelMatrix"/> to be copied.
		/// </param>
		public ModelMatrix(ModelMatrix m)
			: base(m)
		{

		}
Exemple #28
0
		/// <summary>
		/// Compute the transpose of this ModelMatrix.
		/// </summary>
		/// <returns>
		/// A <see cref="ModelMatrix"/> which hold the transpose of this ModelMatrix.
		/// </returns>
		public new ModelMatrix Transpose()
		{
			ModelMatrix transpose = new ModelMatrix();

			// Transpose matrix
			for (uint c = 0; c < 4; c++)
				for (uint r = 0; r < 4; r++)
					transpose[r, c] = this[c, r];

			return (transpose);
		}
Exemple #29
0
		/// <summary>
		/// Compute the product of two ModelMatrix.
		/// </summary>
		/// <param name="m1">
		/// A <see cref="Matrix4x4"/> that specifies the left multiplication operand.
		/// </param>
		/// <param name="m2">
		/// A <see cref="Matrix4x4"/> that specifies the right multiplication operand.
		/// </param>
		/// <returns>
		/// A <see cref="Matrix4x4"/> resulting from the product of the matrix <paramref name="m1"/> and
		/// the matrix <paramref name="m2"/>. This operator is used to concatenate successive transformations.
		/// </returns>
		public static ModelMatrix operator *(ModelMatrix m1, ModelMatrix m2)
		{
			// Allocate product matrix
			ModelMatrix prod = new ModelMatrix();

			// Compute product
			ComputeMatrixProduct(prod, m1, m2);

			return (prod);
		}
Exemple #30
0
 /// <summary>
 /// ModelMatrix copy constructor.
 /// </summary>
 /// <param name="m">
 /// A <see cref="ModelMatrix"/> to be copied.
 /// </param>
 public ModelMatrix(ModelMatrix m)
     : base(m)
 {
 }
Exemple #31
0
		/// <summary>
		/// Accumulate a translation on this ModelMatrix.
		/// </summary>
		/// <param name="p">
		/// A <see cref="Vertex3f"/> that specifies the translation.
		/// </param>
		public void Translate(Vertex3f p)
		{
			ModelMatrix translationMatrix = new ModelMatrix();

			translationMatrix[3, 0] = p.x;
			translationMatrix[3, 1] = p.y;
			translationMatrix[3, 2] = p.z;

			Set(this * translationMatrix);
		}
Exemple #32
0
		/// <summary>
		/// Accumulate a scaling to this model matrix.
		/// </summary>
		/// <param name="s">
		/// A <see cref="Vertex3f"/> holding the scaling factors on three dimensions.
		/// </param>
		public void Scale(Vertex3f s)
		{
			ModelMatrix scaleModel = new ModelMatrix();
			scaleModel.SetScale(s);

			Set(this * scaleModel);
		}
Exemple #33
0
		/// <summary>
		/// Accumulate a scaling to this model matrix.
		/// </summary>
		/// <param name="x">
		/// A <see cref="System.Double"/> holding the scaling factor on X axis.
		/// </param>
		/// <param name="y">
		/// A <see cref="System.Double"/> holding the scaling factor on Y axis.
		/// </param>
		/// <param name="z">
		/// A <see cref="System.Double"/> holding the scaling factor on Z axis.
		/// </param>
		public void Scale(float x, float y, float z)
		{
			ModelMatrix scaleModel = new ModelMatrix();
			scaleModel.SetScale(x, y, z);

			Set(this * scaleModel);
		}