Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace CubeRender
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- Matrix projection, view;
- BoundingFrustum frustum;
- BasicEffect effect;
- List<RenderObject> cubes = new List<RenderObject>();
- int cubeCount = 0;
- bool frustumCull = false;
- bool defaultCube = false;
- private TimeSpan FpsElapsedTime;
- private float FpsCounter = 0, FpsCurrent = 0;
- private float? FpsMin = null, FpsMax = null;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- base.Initialize();
- }
- protected override void LoadContent()
- {
- projection =
- Matrix.CreatePerspectiveFieldOfView
- (
- MathHelper.ToRadians(45f),
- GraphicsDevice.Viewport.AspectRatio,
- 0.01f,
- 10000f
- );
- view =
- Matrix.CreateRotationY(MathHelper.ToRadians(-45f)) *
- Matrix.CreateRotationX(MathHelper.ToRadians(-45f)) *
- Matrix.CreateTranslation(Vector3.Zero) *
- Matrix.CreateLookAt(new Vector3(0, 0, -10f), Vector3.Zero, Vector3.Up);
- frustum = new BoundingFrustum(view * projection);
- effect = new BasicEffect(GraphicsDevice) { VertexColorEnabled = true, TextureEnabled = false };
- defaultCube = true;
- bool white = false;
- for (int x = -50; x < 50; x++)
- {
- for (int z = -50; z < 50; z++)
- {
- // change here if you want to try different cubes
- if (defaultCube)
- {
- if (white)
- {
- cubes.Add(new Cube2(GraphicsDevice, Color.White) { Position = new Vector3(x, 0, z) });
- }
- else
- {
- cubes.Add(new Cube2(GraphicsDevice, Color.Black) { Position = new Vector3(x, 0, z) });
- }
- white = !white;
- }
- else
- {
- cubes.Add(new Cube(GraphicsDevice) { Position = new Vector3(x, 0, z) });
- }
- }
- }
- }
- protected override void UnloadContent()
- {
- effect.Dispose();
- foreach (RenderObject cube in cubes)
- cube.Dispose();
- }
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- FpsElapsedTime += gameTime.ElapsedGameTime;
- if (this.FpsElapsedTime > TimeSpan.FromSeconds(1))
- {
- this.FpsElapsedTime -= TimeSpan.FromSeconds(1);
- FpsCurrent = FpsCounter;
- FpsCounter = 0;
- if (gameTime.TotalGameTime.TotalSeconds > 5)
- {
- if (FpsMax == null)
- FpsMax = FpsCurrent;
- else if (FpsCurrent > FpsMax.Value)
- FpsMax = FpsCurrent;
- if (FpsMin == null)
- FpsMin = FpsCurrent;
- else if (FpsCurrent < FpsMin.Value)
- FpsMin = FpsCurrent;
- }
- this.Window.Title = "Press space to frustum cull on or off! FPS: " + FpsCurrent + ", Cubes Drawn: " + cubeCount + " / " + cubes.Count;
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Space))
- frustumCull = false;
- else
- frustumCull = true;
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- FpsCounter++;
- GraphicsDevice.Clear(Color.CornflowerBlue);
- effect.View = view;
- effect.Projection = projection;
- cubeCount = 0;
- if (frustumCull)
- {
- foreach (RenderObject cube in cubes.Where(c => frustum.Contains(c.BoundingBox) != ContainmentType.Disjoint))
- {
- cube.Draw(GraphicsDevice, effect);
- cubeCount++;
- }
- }
- else
- {
- foreach (RenderObject cube in cubes)
- {
- cube.Draw(GraphicsDevice, effect);
- cubeCount++;
- }
- }
- base.Draw(gameTime);
- }
- }
- }
Add Comment
Please, Sign In to add comment