public SampleGraphicsScreen(IServiceLocator services) : base(services.GetInstance<IGraphicsService>()) { _sampleFramework = services.GetInstance<SampleFramework>(); Name = "SampleScreen"; ClearBackground = false; BackgroundColor = new Color(220, 220, 220); DrawReticle = false; UseFixedWidthFont = false; // Use 2D texture for reticle. var contentManager = services.GetInstance<ContentManager>(); _reticle = contentManager.Load<Texture2D>("Reticle"); // Get the sprite fonts used in the UI theme. var uiContentManager = services.GetInstance<ContentManager>("UIContent"); _defaultFont = uiContentManager.Load<SpriteFont>("UI Themes/BlendBlue/Default"); _fixedWidthFont = uiContentManager.Load<SpriteFont>("UI Themes/BlendBlue/Console"); // Set up 2D camera such that (0, 0) is upper, left corner of screen and // (screenWidth, screenHeight) is lower, right corner of screen. var graphicsDevice = GraphicsService.GraphicsDevice; int screenWidth = graphicsDevice.PresentationParameters.BackBufferWidth; int screenHeight = graphicsDevice.PresentationParameters.BackBufferHeight; var projection = new OrthographicProjection { Near = 0, Far = 2000, Left = 0, Right = screenWidth, Top = 0, Bottom = screenHeight, }; var camera = new Camera(projection); _cameraNode2D = new CameraNode(camera) { PoseWorld = new Pose(new Vector3F(0, 0, 1000)), }; // Initialize renderers. _spriteBatch = new SpriteBatch(graphicsDevice); _meshRenderer = new MeshRenderer(); _billboardRenderer = new BillboardRenderer(GraphicsService, 2048); DebugRenderer2D = new DebugRenderer(GraphicsService, _defaultFont) { SpriteFont = _defaultFont, DefaultColor = new Color(0, 0, 0), DefaultTextPosition = new Vector2F(10) }; DebugRenderer = new DebugRenderer(GraphicsService, _defaultFont) { SpriteFont = _defaultFont, DefaultColor = new Color(0, 0, 0), DefaultTextPosition = new Vector2F(10) }; Scene = new Scene(); }
protected virtual void CloneCore(Camera source) { Name = source.Name; LastProjection = source.LastProjection; }
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 scene camera. GraphicsScreen.CameraNode = _sceneCameraNode; // 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."); }