I have a bit of a dilemma ... So i setup my scene how I want it with some bits in it and now I want to move all my camera related code in to a camera class however doing so is proving to be a pain in my ass.
So in my game code (my main game class) I have ...
protected override void Initialize()
{
model = new TerrainModel();
base.Initialize();
// Load known core components
camera = new Camera(this, cameraPosition, new Vector3(10, 10, 10));
}
Then in update ...
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
camera.Update();
base.Update(gameTime);
}
Then in draw ...
protected override void Draw(GameTime gameTime)
{
cubeEffect.View = Matrix.CreateLookAt(cameraPosition, new Vector3(10, 10, 10), Vector3.Up);
cubeEffect.View = camera.View;
cubeEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 1.0f, 1000.0f);
cubeEffect.Projection = camera.Projection;
}
It's these draw lines that have me confused. If i use the first pair (the lines that create the matrices there and then) it all works.
If I use the second pair (pulling the same thing from my camera class) it doesn't and i get a blank screen.
I'm only rendering a single model so there's a chance it could be pointing in the wrong direction but it seems to have done what i expect when i put breakpoints in and look at the variables.
So what have i done wrong?
Here's my camera class ...
public class Camera
{
#region fields
Matrix viewMatrix;
Matrix projectionMatrix;
Viewport viewPort;
BoundingFrustum cameraViewableBounds;
Vector3 currentTarget;
Vector3 cameraPosition;
#endregion
#region Properties
public Matrix Projection { get { return projectionMatrix; } }
public Matrix View { get { return viewMatrix; } }
public Viewport ViewPort { get { return viewPort; } }
public Vector3 Position
{
get { return cameraPosition; }
set
{
cameraPosition = value;
Update();
}
}
public Vector3 TargetPosition { get { return currentTarget; } }
public bool CanSee(BoundingBox ObjectBounds)
{
if (cameraViewableBounds.Contains(ObjectBounds) != ContainmentType.Disjoint)
return true;
return false;
}
#endregion
public Camera(Game game, Vector3 startingPosition, Vector3 startingTarget)
{
cameraPosition = startingPosition;
// creates a new view matrix
LookAt(startingTarget);
viewPort = game.GraphicsDevice.Viewport;
// Set the Projection matrix which defines how we see the scene (Field of view)
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 0, 1, 10000);
}
#region Camera state updating
public void Update()
{
viewMatrix = Matrix.CreateLookAt(cameraPosition, TargetPosition, Vector3.Up);
cameraViewableBounds = new BoundingFrustum(viewMatrix * projectionMatrix);
}
public void LookAt(Vector3 target)
{
currentTarget = target;
viewMatrix = Matrix.CreateLookAt(Position, TargetPosition, Vector3.Up);
}
#endregion
}
}
I'm sure this is something crazy simple that i've missed ... i just can't see it.
NOTE: I have omitted a lot of code here to try and simplify my question but I'm pretty sure the rest of my code is fine because it works unless I use this new camera class, however I can provide more if its needed.