Exemple #1
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 #2
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 #3
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);
        }
Exemple #4
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 #5
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());
		}
		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();
		}