Allows observing the scene with the mouse and keyboard.
예제 #1
0
        /// <summary>
        /// Object's default constructor.
        /// </summary>
        /// <param name="graphicsDevice">Virtual adapter used to perform rendering.</param>
        /// <param name="camera">Instance of a camera object.</param>
        public SkyCube(Device graphicsDevice, Camera camera)
        {
            this.graphicsDevice = graphicsDevice;
            this.camera = camera;

            GenerateSky();

            shader = new Effect(graphicsDevice, ShaderPrecompiler.PrecompileOrLoad(@"Shaders\SkyCube.hlsl", "fx_5_0", ShaderFlags.None, EffectFlags.None));
        }
예제 #2
0
        /// <summary>
        /// Initializes object.
        /// </summary>
        /// <param name="graphicsDevice">Virtual adapter used to perform rendering.</param>
        /// <param name="camera">Reference to camera, which is used in some computations.</param>
        public VoxelMesh(Device graphicsDevice, Camera camera)
        {
            Container = new VoxelMeshContainer
            {
                Settings = new VoxelMeshSettings(graphicsDevice)
            };

            Renderer = new DefaultRenderer(graphicsDevice, camera, Container);
            Generator = new CPUGenerator(graphicsDevice, Container);
        }
예제 #3
0
        /// <summary>
        /// Computes a mouse direction in world coordinates.
        /// </summary>
        /// <param name="graphicsDevice">Virtual adapter used to perform rendering.</param>
        /// <param name="camera">Allows observing the scene with the mouse and keyboard.</param>
        /// <param name="mousePosition">Position of a mouse in screen coordinates.</param>
        /// <returns>Mouse direction vector.</returns>
        public static Vector3 MouseDirection(Device graphicsDevice, Camera camera, Vector2 mousePosition)
        {
            Viewport viewport = graphicsDevice.ImmediateContext.Rasterizer.GetViewports()[0];

            Vector3 near = new Vector3(mousePosition.X, mousePosition.Y, 0);
            Vector3 far = new Vector3(mousePosition.X, mousePosition.Y, 1);
            near = Vector3.Unproject(near, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinZ, viewport.MaxZ, camera.ViewProjection);
            far = Vector3.Unproject(far, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinZ, viewport.MaxZ, camera.ViewProjection);
            far -= near;
            far.Normalize();

            return far;
        }
예제 #4
0
        public DefaultRenderer(Device graphicsDevice, Camera camera, VoxelMeshContainer container)
        {
            this.graphicsDevice = graphicsDevice;
            this.camera = camera;
            this.container = container;

#if DEBUG
            shader = new Effect(graphicsDevice, ShaderPrecompiler.PrecompileOrLoad(@"Shaders\VoxelMesh.hlsl", "fx_5_0", ShaderFlags.Debug, EffectFlags.None));
#else
            shader = new Effect(graphicsDevice, ShaderPrecompiler.PrecompileOrLoad(@"Shaders\VoxelMesh.hlsl", "fx_5_0", ShaderFlags.None, EffectFlags.None));
#endif

            layout = new InputLayout(graphicsDevice, shader.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature, new[]
			{
				new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
				new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0, InputClassification.PerVertexData, 0),
				new InputElement("AMBIENT", 0, Format.R32_Float, 24, 0, InputClassification.PerVertexData, 0)
			});
        }
예제 #5
0
        /// <summary>
        /// Initializes SlimDX and input devices.
        /// </summary>
        /// <param name="caption">Window caption string.</param>
        public FrameworkForm(string caption)
            : base(caption)
        {
            SwapChainDescription description = new SwapChainDescription()
            {
                BufferCount = 1,
                Flags = SwapChainFlags.None,
                IsWindowed = true,
                ModeDescription = new ModeDescription(ClientSize.Width, ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                OutputHandle = Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput
            };

            try
            {
#if DEBUG
                Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, description, out graphicsDevice, out swapChain);
#else
                Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out graphicsDevice, out swapChain);
#endif
            }
            catch
            {
                MessageBox.Show("An error has occurred during initialization process.");
                Environment.Exit(0);
            }
            finally
            {
                if (graphicsDevice.FeatureLevel != FeatureLevel.Level_11_0)
                {
                    MessageBox.Show("This program requires DirectX 11. Your version is " + graphicsDevice.FeatureLevel.ToString() + ".");
                    Environment.Exit(0);
                }
            }

            Factory factory = swapChain.GetParent<Factory>();
            factory.SetWindowAssociation(Handle, WindowAssociationFlags.IgnoreAltEnter);
            KeyDown += (o, e) =>
            {
                // Fixes Alt-Enter keyboard input bug in SlimDX.
                if (e.Alt && e.KeyCode == Keys.Enter)
                    swapChain.IsFullScreen = !swapChain.IsFullScreen;

                // Makes screenshot.
                if (e.KeyCode == Keys.F12)
                    MakeScreenshot(Application.StartupPath);
            };

            SizeChanged += (o, e) =>
            {
                // Dispose old resources.
                if (renderTargetView != null)
                    renderTargetView.Dispose();
                if (backBufferTexture != null)
                    backBufferTexture.Dispose();

                // Resize buffers.
                swapChain.ResizeBuffers(1, ClientSize.Width, ClientSize.Height, Format.R8G8B8A8_UNorm, SwapChainFlags.None);

                InitializeOutputMerger();

                camera.UpdateProjection();

                postProcess.Initialize(ClientSize.Width, ClientSize.Height);
            };

            fillMode = FillMode.Solid;
            InitializeOutputMerger();

            // Initializes input devices.
            DirectInput directInput = new DirectInput();
            keyboard = new Keyboard(directInput);
            keyboard.Acquire();
            mouse = new Mouse(directInput);
            mouse.Acquire();

            camera = new Camera(graphicsDevice, new Vector3(50, 50, 50), new Vector3(0, 0, 0), 0.1f, 1000.0f);
            textures = new List<TexturePack>();
            postProcess = new PostProcess(graphicsDevice, ClientSize.Width, ClientSize.Height);
            quadRenderer = new QuadRenderer(graphicsDevice);
        }