Advertisement
SirButcher

SlimDX Camera class

Mar 29th, 2013
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1.     internal class Camera
  2.     {
  3.         private Matrix projection = Matrix.Identity;
  4.         private Matrix view = Matrix.Identity;
  5.  
  6.         private Vector3 position;
  7.         private Vector3 origTarget;
  8.         private Vector3 cameraFinalTarget;
  9.  
  10.         private Vector3 up = new Vector3(0, 1, 0);
  11.  
  12.         private float leftRightRot = 0;
  13.         private float upDownRot = 0;
  14.  
  15.  
  16.  
  17.         internal Ray CameraRay
  18.         {
  19.             get
  20.             {
  21.                 //Q - P is a vector pointing from P to Q
  22.                 Vector3 direction = (cameraFinalTarget - position);
  23.                 direction.Normalize();
  24.                 return new SlimDX.Ray(position, direction);
  25.             }
  26.         }
  27.         internal Vector3 CamPosition
  28.         {
  29.             get { return position; }
  30.         }
  31.         internal Matrix ViewProjection
  32.         {
  33.             get { return view * projection; }
  34.         }
  35.         internal CameraEventHandler CamEventHandlers
  36.         {
  37.             get
  38.             {
  39.                 return new CameraEventHandler(new EventHandler(CameraMove), new EventHandler(CameraRotate));
  40.             }
  41.         }
  42.  
  43.         internal Camera(Vector3 position, Vector3 target, ref Device device)
  44.         {
  45.             projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, device.Viewport.Width / device.Viewport.Height, 0.3f, 20000f);
  46.             view = Matrix.LookAtLH(position, target, up);
  47.  
  48.             this.position = position;
  49.             this.origTarget = target;
  50.         }
  51.  
  52.         internal void TakeALook()
  53.         {
  54.             Matrix cameraRotation = Matrix.RotationX(upDownRot) * Matrix.RotationY(leftRightRot);
  55.  
  56.             Vector3 cameraRotatedTarget = Vector3.TransformCoordinate(origTarget, cameraRotation);
  57.             cameraFinalTarget = position + cameraRotatedTarget;
  58.  
  59.             Vector3 cameraRotatedUpVector = Vector3.TransformCoordinate(up, cameraRotation);
  60.  
  61.             view = Matrix.LookAtLH(position, cameraFinalTarget, cameraRotatedUpVector);
  62.         }
  63.  
  64.         private void CameraMove(object vector, EventArgs e)
  65.         {
  66.             //Eventhandler, called when camera moved
  67.             //object == Vector3 -> vector to add
  68.  
  69.             Vector3 vectorToAdd = (Vector3)vector;
  70.  
  71.             Matrix cameraRotation = Matrix.RotationX(upDownRot) * Matrix.RotationY(leftRightRot);
  72.             Vector3 rotatedVector = Vector3.TransformCoordinate(vectorToAdd, cameraRotation);
  73.  
  74.             position += rotatedVector;
  75.         }
  76.         private void CameraRotate(object vector, EventArgs e)
  77.         {
  78.             //Eventhandler, called when camera rotated
  79.             //object == Vector2
  80.             //x == leftRightRot
  81.             //y == upDownRot
  82.  
  83.             Vector2 rotation = (Vector2)vector;
  84.  
  85.             this.leftRightRot += rotation.X;
  86.             this.upDownRot -= rotation.Y;
  87.         }
  88.  
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement