Skip to content

Commit

Permalink
Moved the camera code over to using the new GameComponent/Services "f…
Browse files Browse the repository at this point in the history
…ramework".
  • Loading branch information
vorporeal committed Apr 15, 2010
1 parent a35ce3b commit 23f6c89
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 48 deletions.
125 changes: 109 additions & 16 deletions Engine/Camera.cs
Expand Up @@ -5,14 +5,33 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

// TODO: Documentation for Camera code.

namespace Mammoth.Engine
{
/**
* This is a simple and fairly inefficient camera class taken from the camera class in the PhysX.Net samples. It
* could be improved so that it stores more information and therefore has to do less calculation each update cycle,
* but that shouldn't be worried about unless it is deemed to be an issue (by way of profiling).
*/
public class Camera : GameComponent
/// <summary>
/// This interface encapsulates the idea of a camera service. This can be used with Game.GetService()
/// to get the current camera.
/// </summary>
public interface ICameraService
{
Matrix View
{
get;
}

Matrix Projection
{
get;
}

Camera.CameraType Type
{
get;
}
}

public abstract class Camera : GameComponent, ICameraService
{

#region Variables
Expand All @@ -28,14 +47,16 @@ public enum CameraType

public Camera(Game game) : base(game)
{
this.Type = CameraType.THIRD_PERSON;

}

public override void Update(GameTime gameTime)
public abstract override void Update(GameTime gameTime);

public override void Initialize()
{
base.Update(gameTime);
base.Initialize();

UpdateView();
UpdateProjection();
}

/// <summary>
Expand All @@ -49,7 +70,7 @@ public void UpdateProjection()
0.1f, 10000.0f);
}

public void UpdateView()
/*public void UpdateView()
{
LocalPlayer lp = Engine.Instance.LocalPlayer;
Vector3 forward = Vector3.Transform(Vector3.Forward, lp.HeadOrient);
Expand All @@ -75,26 +96,98 @@ public void UpdateView()
}
this.View = Matrix.CreateLookAt(position, look, Vector3.Up);
}
}*/

#region Properties

public Matrix View
{
get;
private set;
protected set;
}

public Matrix Projection
{
get;
private set;
protected set;
}

public CameraType Type
public abstract CameraType Type
{
get;
internal set;
protected set;
}

#endregion
}

public class FirstPersonCamera : Camera
{
#region Variables

private CameraType _type;

#endregion

public FirstPersonCamera(Game game)
: base(game)
{
this.Type = CameraType.FIRST_PERSON;
}

public override void Update(GameTime gameTime)
{
LocalPlayer lp = Engine.Instance.LocalPlayer;
Vector3 forward = Vector3.Transform(Vector3.Forward, lp.HeadOrient);

Vector3 position = lp.Position + (Vector3.Up * lp.Height / 4.0f);
Vector3 look = position + forward;

this.View = Matrix.CreateLookAt(position, look, Vector3.Up);
}

#region Properties

public override CameraType Type
{
get { return _type; }
protected set { _type = value; }
}

#endregion
}

public class ThirdPersonCamera : Camera
{
#region Variables

private CameraType _type;

#endregion

public ThirdPersonCamera(Game game)
: base(game)
{
this.Type = CameraType.THIRD_PERSON;
}

public override void Update(GameTime gameTime)
{
LocalPlayer lp = Engine.Instance.LocalPlayer;
Vector3 forward = Vector3.Transform(Vector3.Forward, lp.HeadOrient);

Vector3 position = lp.Position - forward * 15 + Vector3.Up * 5;
Vector3 look = lp.Position + Vector3.Up * lp.Height * 3 / 4;

this.View = Matrix.CreateLookAt(position, look, Vector3.Up);
}

#region Properties

public override CameraType Type
{
get { return _type; }
protected set { _type = value; }
}

#endregion
Expand Down
16 changes: 4 additions & 12 deletions Engine/Engine.cs
Expand Up @@ -86,9 +86,10 @@ protected override void Initialize()
this.Components.Add(this.LocalPlayer);

// Create the camera next, and have it update after the player.
this.Camera = new Camera(this);
this.Camera.UpdateOrder = 2;
this.Components.Add(this.Camera);
Camera cam = new FirstPersonCamera(this);
cam.UpdateOrder = 2;
this.Components.Add(cam);
this.Services.AddService(typeof(ICameraService), cam);

base.Initialize();
}
Expand All @@ -107,9 +108,6 @@ protected override void LoadContent()
// Create the renderer here, as we need to give it a graphics device.
this.Renderer = Renderer.Instance;

// We need to set the camera's initial projection matrix.
this.Camera.UpdateProjection();

// Let's create a soldier so we can see something.
this.Components.Add(new SoldierObject(this));

Expand Down Expand Up @@ -210,12 +208,6 @@ public Scene Scene
private set;
}

public Camera Camera
{
get;
private set;
}

public LocalPlayer LocalPlayer
{
get;
Expand Down
7 changes: 0 additions & 7 deletions Engine/Engine.csproj
Expand Up @@ -67,17 +67,10 @@
<ItemGroup>
<Compile Include="Camera.cs" />
<Compile Include="Engine.cs" />
<Compile Include="IEncodable.cs" />
<Compile Include="IRenderable.cs" />
<Compile Include="IUsable.cs" />
<Compile Include="LocalPlayer.cs" />
<Compile Include="Player.cs" />
<Compile Include="RemotePlayer.cs" />
<Compile Include="Renderer.cs" />
<Compile Include="StaticObject.cs" />
<Compile Include="Weapon.cs" />
</ItemGroup>
<ItemGroup>
<None Include="ClassDiagram.cd" />
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion Engine/LocalPlayer.cs
Expand Up @@ -174,7 +174,9 @@ public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);

if(Engine.Instance.Camera.Type != Camera.CameraType.FIRST_PERSON)
Camera cam = (Camera) this.Game.Services.GetService(typeof(ICameraService));

if(cam.Type != Camera.CameraType.FIRST_PERSON)
Renderer.Instance.DrawObject(this);
}

Expand Down
20 changes: 8 additions & 12 deletions Engine/Renderer.cs
Expand Up @@ -26,8 +26,6 @@ private Renderer()
{
_content = Engine.Instance.Content;
_graphics = Engine.Instance.GraphicsDevice;

this.Camera = Engine.Instance.Camera;
}

public Model LoadModel(string path)
Expand All @@ -47,13 +45,15 @@ public SpriteFont LoadFont(string path)

public void DrawObject(IRenderable obj)
{
Camera cam = (Camera) Engine.Instance.Services.GetService(typeof(ICameraService));

Model m = obj.Model3D;

Matrix[] transforms = new Matrix[m.Bones.Count];
m.CopyAbsoluteBoneTransformsTo(transforms);

Matrix projection = this.Camera.Projection;
Matrix view = this.Camera.View;
Matrix projection = cam.Projection;
Matrix view = cam.View;

foreach (ModelMesh mesh in m.Meshes)
{
Expand All @@ -78,12 +78,14 @@ public void DrawObject(IRenderable obj)
/// <param name="scene">The PhysX scene that we want to draw debug geometry for.</param>
public void DrawPhysXDebug(StillDesign.PhysX.Scene scene)
{
Camera cam = (Camera)Engine.Instance.Services.GetService(typeof(ICameraService));

_graphics.VertexDeclaration = new VertexDeclaration(_graphics, VertexPositionColor.VertexElements);

BasicEffect debugEffect = new BasicEffect(_graphics, null);
debugEffect.World = Matrix.Identity;
debugEffect.View = this.Camera.View;
debugEffect.Projection = this.Camera.Projection;
debugEffect.View = cam.View;
debugEffect.Projection = cam.Projection;

DebugRenderable data = scene.GetDebugRenderable();

Expand Down Expand Up @@ -162,12 +164,6 @@ public static Renderer Instance
}
}

public Camera Camera
{
get;
private set;
}

#endregion
}
}

0 comments on commit 23f6c89

Please sign in to comment.