public void SetProjectionTest() { OrthographicProjection projection = new OrthographicProjection(); projection.Set(4, 3, 2, 10); OrthographicProjection camera2 = new OrthographicProjection(); camera2.Set(4, 3); camera2.Near = 2; camera2.Far = 10; OrthographicProjection camera3 = new OrthographicProjection { Left = -2, Right = 2, Bottom = -1.5f, Top = 1.5f, Near = 2, Far = 10, }; Matrix expected = Matrix.CreateOrthographic(4, 3, 2, 10); Assert.IsTrue(Matrix.AreNumericallyEqual(expected, projection)); Assert.IsTrue(Matrix.AreNumericallyEqual(expected, camera2)); Assert.IsTrue(Matrix.AreNumericallyEqual(expected, camera3.ToMatrix())); }
public void SetProjectionTest() { Matrix projectionMatrix = Matrix.CreateOrthographicOffCenter(1, 4, 2, 5, 6, 11); OrthographicProjection orthographicProjection = new OrthographicProjection(); orthographicProjection.Set(projectionMatrix); CameraInstance cameraInstance = new CameraInstance(new Camera(orthographicProjection)); Assert.AreEqual(Vector3.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix.Identity, cameraInstance.PoseWorld.Orientation); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Width)); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Height)); Assert.That(Numeric.AreEqual(1f, cameraInstance.Camera.Projection.AspectRatio)); Assert.That(Numeric.AreEqual(6, cameraInstance.Camera.Projection.Near)); Assert.That(Numeric.AreEqual(11, cameraInstance.Camera.Projection.Far)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Left)); Assert.That(Numeric.AreEqual(4, cameraInstance.Camera.Projection.Right)); Assert.That(Numeric.AreEqual(2, cameraInstance.Camera.Projection.Bottom)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Top)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Depth)); Assert.That(Matrix.AreNumericallyEqual(orthographicProjection, cameraInstance.Camera.Projection)); Assert.That(Matrix.AreNumericallyEqual(orthographicProjection.Inverse, cameraInstance.Camera.Projection.Inverse)); Assert.IsNotNull(cameraInstance.BoundingShape); PerspectiveProjection perspectiveProjection = new PerspectiveProjection(); perspectiveProjection.Inverse = Matrix.CreatePerspectiveOffCenter(1, 5, 2, 5, 1, 10).Inverse; cameraInstance = new CameraInstance(new Camera(perspectiveProjection)); Assert.AreEqual(Vector3.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix.Identity, cameraInstance.PoseWorld.Orientation); Assert.That(Numeric.AreEqual(MathHelper.ToRadians(33.690067f), cameraInstance.Camera.Projection.FieldOfViewX)); Assert.That(Numeric.AreEqual(MathHelper.ToRadians(15.255119f), cameraInstance.Camera.Projection.FieldOfViewY)); Assert.That(Numeric.AreEqual(4, cameraInstance.Camera.Projection.Width)); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Height)); Assert.That(Numeric.AreEqual(4.0f / 3.0f, cameraInstance.Camera.Projection.AspectRatio)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Left)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Right)); Assert.That(Numeric.AreEqual(2, cameraInstance.Camera.Projection.Bottom)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Top)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Near)); Assert.That(Numeric.AreEqual(10, cameraInstance.Camera.Projection.Far)); Assert.That(Numeric.AreEqual(9, cameraInstance.Camera.Projection.Depth)); Assert.IsNotNull(cameraInstance.BoundingShape); }
private bool _cullingEnabled = true; // True to use frustum culling. False to disable frustum culling. public FrustumCullingSample(Microsoft.Xna.Framework.Game game) : base(game) { GraphicsScreen.ClearBackground = true; GraphicsScreen.BackgroundColor = Color.CornflowerBlue; // The top-down camera. var orthographicProjection = new OrthographicProjection(); orthographicProjection.Set( LevelSize * 1.1f * GraphicsService.GraphicsDevice.Viewport.AspectRatio, LevelSize * 1.1f, 1, 10000f); var topDownCamera = new Camera(orthographicProjection); _topDownCameraNode = new CameraNode(topDownCamera) { View = Matrix44F.CreateLookAt(new Vector3F(0, 1000, 0), new Vector3F(0, 0, 0), -Vector3F.UnitZ), }; // The perspective camera moving through the scene. var perspectiveProjection = new PerspectiveProjection(); perspectiveProjection.SetFieldOfView( MathHelper.ToRadians(45), GraphicsService.GraphicsDevice.Viewport.AspectRatio, 1, 500); var sceneCamera = new Camera(perspectiveProjection); _sceneCameraNode = new CameraNode(sceneCamera); // Initialize collision detection. // We use one collision domain that manages all objects. _domain = new CollisionDomain(new CollisionDetection()) { // We exchange the default broad phase with a DualPartition. The DualPartition // has special support for frustum culling. BroadPhase = new DualPartition <CollisionObject>(), }; // Create a lot of random objects and add them to the collision domain. RandomHelper.Random = new Random(12345); for (int i = 0; i < NumberOfObjects; i++) { // A real scene consists of a lot of complex objects such as characters, vehicles, // buildings, lights, etc. When doing frustum culling we need to test each objects against // the viewing frustum. If it intersects with the viewing frustum, the object is visible // from the camera's point of view. However, in practice we do not test the exact object // against the viewing frustum. Each objects is approximated by a simpler shape. In our // example, we assume that each object is approximated with an oriented bounding box. // (We could also use an other shape, such as a bounding sphere.) // Create a random box. Shape randomShape = new BoxShape(RandomHelper.Random.NextVector3F(1, 10)); // Create a random position. Vector3F randomPosition; randomPosition.X = RandomHelper.Random.NextFloat(-LevelSize / 2, LevelSize / 2); randomPosition.Y = RandomHelper.Random.NextFloat(0, 2); randomPosition.Z = RandomHelper.Random.NextFloat(-LevelSize / 2, LevelSize / 2); // Create a random orientation. QuaternionF randomOrientation = RandomHelper.Random.NextQuaternionF(); // Create object and add it to collision domain. var geometricObject = new GeometricObject(randomShape, new Pose(randomPosition, randomOrientation)); var collisionObject = new CollisionObject(geometricObject) { CollisionGroup = 0, }; _domain.CollisionObjects.Add(collisionObject); } // Per default, the collision domain computes collision between all objects. // In this sample we do not need this information and disable it with a collision // filter. // In a real application, we would use this collision information for rendering, // for example, to find out which lights overlap with which meshes, etc. var filter = new CollisionFilter(); // Disable collision between objects in collision group 0. filter.Set(0, 0, false); _domain.CollisionDetection.CollisionFilter = filter; // Start with the top-down camera. GraphicsScreen.CameraNode = _topDownCameraNode; // We will collect a few statistics for debugging. Profiler.SetFormat("NoCull", 1000, "Time in ms to submit DebugRenderer draw jobs without frustum culling."); Profiler.SetFormat("WithCull", 1000, "Time in ms to submit DebugRenderer draw jobs with frustum culling."); }
public void SetProjectionTest() { Matrix44F projectionMatrix = Matrix44F.CreateOrthographicOffCenter(1, 4, 2, 5, 6, 11); OrthographicProjection orthographicProjection = new OrthographicProjection(); orthographicProjection.Set(projectionMatrix); CameraInstance cameraInstance = new CameraInstance(new Camera(orthographicProjection)); Assert.AreEqual(Vector3F.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix33F.Identity, cameraInstance.PoseWorld.Orientation); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Width)); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Height)); Assert.That(Numeric.AreEqual(1f, cameraInstance.Camera.Projection.AspectRatio)); Assert.That(Numeric.AreEqual(6, cameraInstance.Camera.Projection.Near)); Assert.That(Numeric.AreEqual(11, cameraInstance.Camera.Projection.Far)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Left)); Assert.That(Numeric.AreEqual(4, cameraInstance.Camera.Projection.Right)); Assert.That(Numeric.AreEqual(2, cameraInstance.Camera.Projection.Bottom)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Top)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Depth)); Assert.That(Matrix44F.AreNumericallyEqual(orthographicProjection, cameraInstance.Camera.Projection)); Assert.That(Matrix44F.AreNumericallyEqual(orthographicProjection.Inverse, cameraInstance.Camera.Projection.Inverse)); Assert.IsNotNull(cameraInstance.BoundingShape); PerspectiveProjection perspectiveProjection = new PerspectiveProjection(); perspectiveProjection.Inverse = Matrix44F.CreatePerspectiveOffCenter(1, 5, 2, 5, 1, 10).Inverse; cameraInstance = new CameraInstance(new Camera(perspectiveProjection)); Assert.AreEqual(Vector3F.Zero, cameraInstance.PoseWorld.Position); Assert.AreEqual(Matrix33F.Identity, cameraInstance.PoseWorld.Orientation); Assert.That(Numeric.AreEqual(MathHelper.ToRadians(33.690067f), cameraInstance.Camera.Projection.FieldOfViewX)); Assert.That(Numeric.AreEqual(MathHelper.ToRadians(15.255119f), cameraInstance.Camera.Projection.FieldOfViewY)); Assert.That(Numeric.AreEqual(4, cameraInstance.Camera.Projection.Width)); Assert.That(Numeric.AreEqual(3, cameraInstance.Camera.Projection.Height)); Assert.That(Numeric.AreEqual(4.0f / 3.0f, cameraInstance.Camera.Projection.AspectRatio)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Left)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Right)); Assert.That(Numeric.AreEqual(2, cameraInstance.Camera.Projection.Bottom)); Assert.That(Numeric.AreEqual(5, cameraInstance.Camera.Projection.Top)); Assert.That(Numeric.AreEqual(1, cameraInstance.Camera.Projection.Near)); Assert.That(Numeric.AreEqual(10, cameraInstance.Camera.Projection.Far)); Assert.That(Numeric.AreEqual(9, cameraInstance.Camera.Projection.Depth)); Assert.IsNotNull(cameraInstance.BoundingShape); }
public void SetProjectionTest() { OrthographicProjection projection = new OrthographicProjection(); projection.Set(4, 3, 2, 10); OrthographicProjection camera2 = new OrthographicProjection(); camera2.Set(4, 3); camera2.Near = 2; camera2.Far = 10; OrthographicProjection camera3 = new OrthographicProjection { Left = -2, Right = 2, Bottom = -1.5f, Top = 1.5f, Near = 2, Far = 10, }; Matrix44F expected = Matrix44F.CreateOrthographic(4, 3, 2, 10); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, projection)); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, camera2)); Assert.IsTrue(Matrix44F.AreNumericallyEqual(expected, camera3.ToMatrix44F())); }