Exemple #1
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            QuadTreeNode.CalculateIndexData(GraphicsDevice);

            _oldMouseState    = Mouse.GetState();
            _oldKeyboardState = Keyboard.GetState();

            int            worldSeed               = 1;
            NoiseProvider  mainNoiseProvider       = new NoiseProvider(worldSeed, 4, 0.3f, 6000);
            NoiseProvider  modulationNoiseProvider = new NoiseProvider(worldSeed + 1, 2, 0.2f, 50000);
            DirectionLight light      = new DirectionLight(Vector3.Normalize(new Vector3(0, -0.3f, 1)), 1, 0.1f);
            Atmosphere     atmosphere = new Atmosphere(150000, new Color(0.3f, 0.5f, 0.9f));

            _world = new World(GraphicsDevice, new TerrainHeightProvider(mainNoiseProvider, modulationNoiseProvider), 1737000, light, new Random(worldSeed), atmosphere);

            Matrix cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), GraphicsDevice.Viewport.AspectRatio, CentredCamera.NearPlaneDistance, CentredCamera.FarPlaneDistance);

            _camera = new CentredCamera()
            {
                Position         = new Vector3Double(0, _world.Radius + 3000, 0),
                Orientation      = Matrix.Identity,
                ProjectionMatrix = cameraProjectionMatrix,
                MovementSpeed    = 1000
            };

            _starTexture = new Texture2D(GraphicsDevice, 1, 1);
            _starTexture.SetData(new Color[] { Color.White });

            _spriteBatch = new SpriteBatch(GraphicsDevice);

            base.Initialize();
        }
        internal byte LuminosityDirectionLight(Vector normal, DirectionLight sourceLight)
        {
            var dotProduct = (normal * sourceLight.Direction) / (sourceLight.Direction.Magnitude * normal.Magnitude);

            if (dotProduct > 0)
            {
                return(0);
            }

            return((byte)((-dotProduct) * byte.MaxValue));
        }
        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            if (textBox8.Text == "")
            {
                return;
            }
            if (!IsRightType <DirectionLight>())
            {
                return;
            }

            DirectionLight dl = (DirectionLight)GameLogic.SelectedEntity;

            dl.Direction = ConvertTextboxToVector3Normalized(textBox8, 10);
            SetLightInfo();
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            TerrainChunk.CalculateIndices();

            _defaultRasterizerState = GraphicsDevice.RasterizerState;
            _treeRasterizerState    = new RasterizerState()
            {
                CullMode = CullMode.None
            };

            _oldMouseState    = Mouse.GetState();
            _oldKeyboardState = Keyboard.GetState();

            int            worldSeed               = 0;
            NoiseProvider  mainNoiseProvider       = new NoiseProvider(worldSeed, 4, 0.2f, 200);
            NoiseProvider  modulationNoiseProvider = new NoiseProvider(worldSeed + 1, 2, 0.2f, 600);
            DirectionLight light = new DirectionLight(Vector3.Normalize(new Vector3(0, -0.3f, 1)), 1, 0.1f, 64);
            NoiseProvider  decorNoiseProvider = new NoiseProvider(worldSeed + 2, 2, 0.2f, 200);

            _world = new World(GraphicsDevice, new TerrainHeightProvider(mainNoiseProvider, modulationNoiseProvider), light, decorNoiseProvider);

            Matrix cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), GraphicsDevice.Viewport.AspectRatio, 1, World.ChunkRenderDistance);

            _camera = new Camera()
            {
                ProjectionMatrix = cameraProjectionMatrix
            };

            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _waterTileScaleMatrix = Matrix.CreateScale(TerrainChunk.EdgeLength);

            _mouseLookActive = true;

            _waterRefractionMapRenderTarget = new RenderTarget2D(GraphicsDevice, WaterMapSize, WaterMapSize, false, SurfaceFormat.Color, DepthFormat.Depth24);
            _waterReflectionMapRenderTarget = new RenderTarget2D(GraphicsDevice, WaterMapSize, WaterMapSize, false, SurfaceFormat.Color, DepthFormat.Depth24);

            _terrainChunkIndexBuffer = new IndexBuffer(GraphicsDevice, typeof(ushort), TerrainChunk.Indices.Length, BufferUsage.WriteOnly);
            _terrainChunkIndexBuffer.SetData(TerrainChunk.Indices);

            base.Initialize();
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            _light = new DirectionLight(Vector3.Normalize(new Vector3(1, -1, -1)), 1, 0.2f);

            _plankRigidBody = new RigidBody()
            {
                Mass             = 5,
                BoundingVertices = new Vector3[]
                {
                    new Vector3(-1, 0.2f, 3),
                    new Vector3(1, 0.2f, 3),
                    new Vector3(1, 0.2f, -3),
                    new Vector3(-1, 0.2f, -3),
                    new Vector3(-1, -0.2f, 3),
                    new Vector3(1, -0.2f, 3),
                    new Vector3(1, -0.2f, -3),
                    new Vector3(-1, -0.2f, -3)
                },
                State = new RigidBodyState()
                {
                    Position        = new Vector3(-5, 5, 0),
                    Velocity        = new Vector3(0, 5, -2),
                    Orientation     = Matrix.Identity,
                    AngularMomentum = new Vector3(5, 18, 18)
                }
            };

            // The plank requires an inertia tensor to be defined. This depends on mass and dimensions, and
            // the formulation differs across object shapes. A good list for common shapes can be found here:
            // https://en.wikipedia.org/wiki/List_of_moments_of_inertia#List_of_3D_inertia_tensors
            // The plank uses the solid cuboid definition.
            Matrix plankBodyInertiaTensor = Matrix.Identity;

            plankBodyInertiaTensor[0, 0]             = _plankRigidBody.Mass / 12 * (6 * 6 + 0.4f * 0.4f);
            plankBodyInertiaTensor[1, 1]             = _plankRigidBody.Mass / 12 * (6 * 6 + 2 * 2);
            plankBodyInertiaTensor[2, 2]             = _plankRigidBody.Mass / 12 * (2 * 2 + 0.4f * 0.4f);
            _plankRigidBody.InverseBodyInertiaTensor = Matrix.Invert(plankBodyInertiaTensor);

            _cubeRigidBody = new SimplifiedRigidBody()
            {
                BoundingVertices = new Vector3[]
                {
                    new Vector3(-1, 1, 1),
                    new Vector3(1, 1, 1),
                    new Vector3(1, 1, -1),
                    new Vector3(-1, 1, -1),
                    new Vector3(-1, -1, 1),
                    new Vector3(1, -1, 1),
                    new Vector3(1, -1, -1),
                    new Vector3(-1, -1, -1)
                },
                State = new SimplifiedRigidBodyState()
                {
                    Position        = new Vector3(0, 5, 0),
                    Velocity        = new Vector3(0, 2, 1),
                    Orientation     = Matrix.Identity,
                    AngularVelocity = new Vector3(1, 1, 2)
                }
            };

            _cameraPosition = new Vector3(10, 10, 10);
            _gravity        = new Vector3(0, -9.81f, 0);

            _viewMatrix       = Matrix.CreateLookAt(_cameraPosition, new Vector3(0, 0, 0), new Vector3(0, 1, 0));
            _projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), GraphicsDevice.Viewport.AspectRatio, 0.1f, 100);

            Vector3 virtualLightPosition = -20 * _light.Direction;

            _lightViewMatrix = Matrix.CreateLookAt(virtualLightPosition, virtualLightPosition + _light.Direction, new Vector3(0, 1, 0));

            _lightProjectionMatrix = Matrix.CreateOrthographic(30, 30, 0.1f, 100);

            _shadowMapRenderTarget = new RenderTarget2D(GraphicsDevice, ShadowMapSize, ShadowMapSize, false, SurfaceFormat.Single, DepthFormat.Depth24);

            base.Initialize();
        }