So in this post we are going to cover some utilities that will make out life’s allot easier going forward.
So Lets have a look at out first new class it’s called MathUtil.
The Class will have the following function,
- CreateProjectionMatrix - Generates a projection matrix for a draw call
- CreateWorldMatrix – Creates a world matrix
- Vector3ToMatrix – Converts a rotation vector into a rotation matrix
- MatrixToVector3 – Converts a rotation matrix into a rotation vector
So here the code for our MathUtil class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using GameEngine;
namespace GameEngine.Utilities
{
public static class MathUtil
{
/// <summary>
/// Creates the projection matrix.
/// </summary>
/// <returns></returns>
public static Matrix CreateProjectionMatrix()
{
return Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
(float)Engine.GraphicsDevice.Viewport.Width / (float)Engine.GraphicsDevice.Viewport.Height,
.01f,
1000000);
}
/// <summary>
/// Creates the world matrix.
/// </summary>
/// <param name="Translation">The translation.</param>
/// <returns></returns>
public static Matrix CreateWorldMatrix(Vector3 Translation)
{
return CreateWorldMatrix(Translation, Matrix.Identity);
}
/// <summary>
/// Creates the world matrix.
/// </summary>
/// <param name="Translation">The translation.</param>
/// <param name="Rotation">The rotation.</param>
/// <returns></returns>
public static Matrix CreateWorldMatrix(Vector3 Translation, Matrix Rotation)
{
return CreateWorldMatrix(Translation, Rotation, Vector3.One);
}
/// <summary>
/// Creates the world matrix.
/// </summary>
/// <param name="Translation">The translation.</param>
/// <param name="Rotation">The rotation.</param>
/// <param name="Scale">The scale.</param>
/// <returns></returns>
public static Matrix CreateWorldMatrix(Vector3 Translation, Matrix Rotation, Vector3 Scale)
{
return Matrix.CreateScale(Scale) * Rotation * Matrix.CreateTranslation(Translation);
}
/// <summary>
/// Vector3s to matrix.
/// </summary>
/// <param name="Rotation">The rotation.</param>
/// <returns></returns>
public static Matrix Vector3ToMatrix(Vector3 Rotation)
{
return Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z);
}
/// <summary>
/// Matrixes to vector3.
/// </summary>
/// <param name="Rotation">The rotation.</param>
/// <returns></returns>
public static Vector3 MatrixToVector3(Matrix Rotation)
{
Quaternion q = Quaternion.CreateFromRotationMatrix(Rotation);
return new Vector3(q.X, q.Y, q.Z);
}
}
}
Next we are going to Add a Class to help with some graphic stuff, can you guess what we are going to call it? thats right GraphicsUtil
The Class will have the following function,
- CreateRenderTarget – This function is going to have multiple signatures within out Class
So here’s the code.
using Microsoft.Xna.Framework.Graphics;
using GameEngine;
namespace GameEngine.Utilities
{
public static class GraphicsUtil
{
/// <summary>
/// Creates the render target.
/// </summary>
/// <returns></returns>
public static RenderTarget2D CreateRenderTarget()
{
return CreateRenderTarget(Engine.GraphicsDevice.Viewport.Width,
Engine.GraphicsDevice.Viewport.Height);
}
/// <summary>
/// Creates the render target.
/// </summary>
/// <param name="Width">The width.</param>
/// <param name="Height">The height.</param>
/// <returns></returns>
public static RenderTarget2D CreateRenderTarget(int Width, int Height)
{
return CreateRenderTarget(Width, Height,
Engine.GraphicsDevice.DisplayMode.Format);
}
/// <summary>
/// Creates the render target.
/// </summary>
/// <param name="Width">The width.</param>
/// <param name="Height">The height.</param>
/// <param name="Format">The format.</param>
/// <returns></returns>
public static RenderTarget2D CreateRenderTarget(int Width, int Height,
SurfaceFormat Format)
{
return CreateRenderTarget(Width, Height, Format,
Engine.GraphicsDevice.PresentationParameters.MultiSampleCount,
Engine.GraphicsDevice.PresentationParameters.DepthStencilFormat);
}
/// <summary>
/// Creates the render target.
/// </summary>
/// <param name="Width">The width.</param>
/// <param name="Height">The height.</param>
/// <param name="Format">The format.</param>
/// <param name="MultiSampleQuality">The multi sample quality.</param>
/// <param name="SampleType">Type of the sample.</param>
/// <returns></returns>
public static RenderTarget2D CreateRenderTarget(int Width, int Height,
SurfaceFormat Format, int MultiSampleQuality, DepthFormat SampleType)
{
return new RenderTarget2D(Engine.GraphicsDevice, Width,
Height, false, Format, SampleType, MultiSampleQuality,
RenderTargetUsage.DiscardContents);
}
/// <summary>
/// Creates the resolve texture.
/// </summary>
/// <returns></returns>
public static RenderTarget2D CreateResolveTexture()
{
return new RenderTarget2D(Engine.GraphicsDevice,
Engine.GraphicsDevice.Viewport.Width,
Engine.GraphicsDevice.Viewport.Height, false,
Engine.GraphicsDevice.DisplayMode.Format,
Engine.GraphicsDevice.PresentationParameters.DepthStencilFormat,
Engine.GraphicsDevice.PresentationParameters.MultiSampleCount,
RenderTargetUsage.DiscardContents);
}
}
}
That’s it for this post.