The code for an easy to use DirectX based Camera
This class will allow you to abstract the issue of placing and moving the "camera" in a direct3d application. Once instantiated, all you have to do is call the various methods to move it ... think wolfenstine 3D. It has support for orientation on all 3 axis, however, you have to be careful the order in which you call them because rolling, then adjusting the pitch will have a different effect than adjusting the pitch then rolling.
Of course, that all depends on what kind of application you're writing ... it works just fine for me at the moment.
To use it, simply call the SetView method before rendering anything else ... that sets the device's View transformation to the camera class' internal representation of the current view.
using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace dxtest1
{
public class Camera
{
private Matrix view;
public Camera(float forward, float up, float strafe)
{
view = Matrix.LookAtLH(new Vector3(strafe,up,forward), new Vector3(0,0,0), new Vector3(0,1,0));
}
public void Turn(float amount)
{
view.Multiply(Matrix.RotationY(0-amount));
}
public void AdjustPitch(float amount)
{
view.Multiply(Matrix.RotationX(0-amount));
}
public void Roll(float amount)
{
view.Multiply(Matrix.RotationZ(0-amount));
}
public void Move(float forward, float up, float strafe)
{
Vector3 m = new Vector3(0-strafe,0-up,0-forward);
view.Multiply(Matrix.Translation(m));
}
public void SetView(Device dev)
{
dev.Transform.View = view;
}
}
}