float4x4 ComputeCameraProjection() { float t = (float) (DateTime.Now - m_startTime).TotalSeconds; // Make a camera orbiting around the cube float T = t; float3 cameraPosition = 3.0f * new float3( (float) Math.Cos( T ), (float) Math.Cos( 3 * T ), (float) Math.Sin( T ) ); float3 cameraTarget = new float3( 0, 0, 0 ); float4x4 camera2World = new float4x4(); camera2World.BuildRotLeftHanded( cameraPosition, cameraTarget, float3.UnitY ); // Build the perspective projection matrix float4x4 camera2Proj = new float4x4(); camera2Proj.BuildProjectionPerspective( 80.0f * (float) Math.PI / 180.0f, (float) Width / Height, 0.01f, 100.0f ); // Compose the 2 matrices together to obtain the final matrix that transforms world coordinates into projected 2D coordinates return camera2World.Inverse * camera2Proj; }
static void TestFloat4x4() { if ( System.Runtime.InteropServices.Marshal.SizeOf(typeof(float4x4)) != 64 ) throw new Exception( "Not the appropriate size!" ); float4x4 test1 = new float4x4( new float[16] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 } ); float4x4 test2 = new float4x4( new float4( 1, 0, 0, 0 ), new float4( 0, 1, 0, 0 ), new float4( 0, 0, 1, 0 ), new float4( 0, 0, 0, 1 ) ); float4x4 test3 = new float4x4( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ); float4x4 test0; test0 = test3; test2.Scale( new float3( 2, 2, 2 ) ); float3x3 cast = (float3x3) test3; float4x4 mul0 = test1 * test2; float4x4 mul1 = 3.0f * test2; float4 mul2 = new float4( 1, 1, 1, 1 ) * test3; float4 access0 = test3[2]; test3[1] = new float4( 12, 13, 14, 15 ); float access1 = test3[1,2]; test3[1,2] = 18; float coFactor = test3.CoFactor( 0, 2 ); float det = test3.Determinant; float4x4 inv = test2.Inverse; float4x4 id = float4x4.Identity; test3.BuildRotLeftHanded( float3.UnitZ, float3.Zero, float3.UnitY ); test3.BuildRotRightHanded( float3.UnitZ, float3.Zero, float3.UnitY ); test3.BuildProjectionPerspective( 1.2f, 2.0f, 0.01f, 10.0f ); test3.BuildRotationX( 0.5f ); test3.BuildRotationY( 0.5f ); test3.BuildRotationZ( 0.5f ); test3.BuildFromAngleAxis( 0.5f, float3.UnitY ); }
/// <summary> /// Creates a perspective projection float4x4 for the camera /// </summary> /// <param name="_FOV"></param> /// <param name="_AspectRatio"></param> /// <param name="_Near"></param> /// <param name="_Far"></param> public void CreatePerspectiveCamera( float _FOV, float _AspectRatio, float _Near, float _Far ) { m_Near = _Near; m_Far = _Far; m_AspectRatio = _AspectRatio; m_PerspFOV = _FOV; float4x4 Temp = new float4x4(); Temp.BuildProjectionPerspective( _FOV, _AspectRatio, _Near, _Far ); this.Camera2Proj = Temp; m_bIsPerspective = true; // Build camera data m_CachedCameraData.x = (float) Math.Tan( 0.5 * m_PerspFOV ); m_CachedCameraData.y = m_AspectRatio; m_CachedCameraData.z = m_Near; m_CachedCameraData.w = m_Far; }