public Device() { // Default background color backgroundColor = new SlimDX.Color4(0.5f, 0.5f, 1.0f); form = new SlimDX.Windows.RenderForm() { Text = "Game", WindowState = System.Windows.Forms.FormWindowState.Maximized }; swapChainDescription = new SlimDX.DXGI.SwapChainDescription() { BufferCount = 1, Usage = SlimDX.DXGI.Usage.RenderTargetOutput, OutputHandle = form.Handle, IsWindowed = true, ModeDescription = new SlimDX.DXGI.ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new SlimDX.Rational(60, 1), SlimDX.DXGI.Format.R8G8B8A8_UNorm), SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0), Flags = SlimDX.DXGI.SwapChainFlags.AllowModeSwitch, SwapEffect = SlimDX.DXGI.SwapEffect.Discard }; var result = SlimDX.Direct3D11.Device.CreateWithSwapChain(SlimDX.Direct3D11.DriverType.Hardware, SlimDX.Direct3D11.DeviceCreationFlags.SingleThreaded, swapChainDescription, out device, out swapChain); if (result.IsFailure) { throw new ApplicationException("Failed to initialize DirectX 11."); } deviceContext = device.ImmediateContext; using (var resource = SlimDX.Direct3D11.Resource.FromSwapChain<SlimDX.Direct3D11.Texture2D>(swapChain, 0)) { screenRenderTargetView = new SlimDX.Direct3D11.RenderTargetView(device, resource); } using (var factory = swapChain.GetParent<SlimDX.DXGI.Factory>()) { result = factory.SetWindowAssociation(form.Handle, SlimDX.DXGI.WindowAssociationFlags.IgnoreAltEnter); if (result.IsFailure) { throw new ApplicationException("Failed to associate window with DirectX device."); } } viewport = new SlimDX.Direct3D11.Viewport(0.0f, 0.0f, Convert.ToSingle(form.ClientSize.Width), Convert.ToSingle(form.ClientSize.Height)); deviceContext.Rasterizer.SetViewports(viewport); form.KeyDown += (sender, e) => { if (e.Alt && e.KeyCode == System.Windows.Forms.Keys.Enter) { swapChain.IsFullScreen = !swapChain.IsFullScreen; } }; form.UserResized += (sender, e) => { var resizeResult = swapChain.ResizeBuffers(2, 0, 0, SlimDX.DXGI.Format.R8G8B8A8_UNorm, SlimDX.DXGI.SwapChainFlags.AllowModeSwitch); if (resizeResult.IsSuccess) { using (var resource = SlimDX.Direct3D11.Resource.FromSwapChain<SlimDX.Direct3D11.Texture2D>(swapChain, 0)) { screenRenderTargetView.Dispose(); screenRenderTargetView = new SlimDX.Direct3D11.RenderTargetView(device, resource); } viewport = new SlimDX.Direct3D11.Viewport(0.0f, 0.0f, Convert.ToSingle(form.ClientSize.Width), Convert.ToSingle(form.ClientSize.Height)); deviceContext.Rasterizer.SetViewports(viewport); } }; }
public override void Render() { SlimDX.Direct3D11.DepthStencilState oldDSState = DeviceManager.Instance.context.OutputMerger.DepthStencilState; SlimDX.Direct3D11.BlendState oldBlendState = DeviceManager.Instance.context.OutputMerger.BlendState; SlimDX.Direct3D11.RasterizerState oldRasterizerState = DeviceManager.Instance.context.Rasterizer.State; SlimDX.Direct3D11.VertexShader oldVertexShader = DeviceManager.Instance.context.VertexShader.Get(); SlimDX.Direct3D11.Buffer[] oldVSCBuffers = DeviceManager.Instance.context.VertexShader.GetConstantBuffers(0, 10); SlimDX.Direct3D11.PixelShader oldPixelShader = DeviceManager.Instance.context.PixelShader.Get(); SlimDX.Direct3D11.Buffer[] oldPSCBuffers = DeviceManager.Instance.context.PixelShader.GetConstantBuffers(0, 10); SlimDX.Direct3D11.ShaderResourceView[] oldShaderResources = DeviceManager.Instance.context.PixelShader.GetShaderResources(0, 10); SlimDX.Direct3D11.GeometryShader oldGeometryShader = DeviceManager.Instance.context.GeometryShader.Get(); base.Render(); DeviceManager.Instance.context.OutputMerger.DepthStencilState = oldDSState; DeviceManager.Instance.context.OutputMerger.BlendState = oldBlendState; DeviceManager.Instance.context.Rasterizer.State = oldRasterizerState; DeviceManager.Instance.context.VertexShader.Set(oldVertexShader); DeviceManager.Instance.context.VertexShader.SetConstantBuffers(oldVSCBuffers, 0, 10); DeviceManager.Instance.context.PixelShader.Set(oldPixelShader); DeviceManager.Instance.context.PixelShader.SetConstantBuffers(oldPSCBuffers, 0, 10); DeviceManager.Instance.context.PixelShader.SetShaderResources(oldShaderResources, 0, 10); DeviceManager.Instance.context.GeometryShader.Set(oldGeometryShader); foreach (RenderInstance instance in _renderInstances) { if (instance.Name == null) { continue; } SlimDX.Matrix world = instance.Transform; SlimDX.Matrix view = CameraManager.Instance.frameCamera.View; SlimDX.Matrix projection = CameraManager.Instance.frameCamera.Perspective; //SlimDX.Matrix viewProj = view * projection; SlimDX.Matrix worldViewProjection = world * view * projection; SlimDX.Direct3D11.Viewport vp = DeviceManager.Instance.context.Rasterizer.GetViewports()[0]; SlimDX.Vector3 position3D = SlimDX.Vector3.Project(SlimDX.Vector3.Zero, vp.X, vp.Y, vp.Width, vp.Height, vp.MinZ, vp.MaxZ, worldViewProjection); SlimDX.Vector2 position2D = new SlimDX.Vector2(position3D.X, position3D.Y); if (position3D.Z < vp.MaxZ) { SlimDX.Vector3 objPos = SlimDX.Vector3.Zero; objPos = SlimDX.Vector3.TransformCoordinate(objPos, instance.Transform); SlimDX.Vector3 camPos = CameraManager.Instance.frameCamera.eye; SlimDX.Vector3 objOffset = objPos - camPos; float scale = Math.Min(2.0f, 5.0f / (float)Math.Sqrt(Math.Max(1.0f, objOffset.Length()))); RenderManager.Instance.DrawString(instance.Name, position2D, 16 * scale, new SlimDX.Color4(1.0f, 1.0f, 1.0f)); } } DeviceManager.Instance.context.OutputMerger.DepthStencilState = oldDSState; DeviceManager.Instance.context.OutputMerger.BlendState = oldBlendState; DeviceManager.Instance.context.Rasterizer.State = oldRasterizerState; DeviceManager.Instance.context.VertexShader.Set(oldVertexShader); DeviceManager.Instance.context.VertexShader.SetConstantBuffers(oldVSCBuffers, 0, 10); DeviceManager.Instance.context.PixelShader.Set(oldPixelShader); DeviceManager.Instance.context.PixelShader.SetConstantBuffers(oldPSCBuffers, 0, 10); DeviceManager.Instance.context.PixelShader.SetShaderResources(oldShaderResources, 0, 10); DeviceManager.Instance.context.GeometryShader.Set(oldGeometryShader); }
public void SetViewport(Viewport vp) { DXViewport dxvp = Direct3DConverter11.ConvertToViewport(vp); GraphicsDirect3D11.Device.ImmediateContext.Rasterizer.SetViewports(dxvp); }
private Actor LoadCamera(JToken source) { // Create the actor Actor actor = new Actor(Owner.MessagePool); // Add the required components to it Transform transform = actor.AddComponent<Transform>(); Camera camera = actor.AddComponent<Camera>(); // Load generic transform LoadTransform(transform, source); // Load camera if (source["projectiontype"] != null) { string proj = (string)source["projectiontype"]; if (proj == "perspective") camera.ProjectionType = CameraType.Perspective; if (proj == "orthographic") camera.ProjectionType = CameraType.Orthographic; } if (source["fov"] != null) camera.FoV = (float)source["fov"]; if (source["nearz"] != null) camera.NearZ = (float)source["nearz"]; if (source["farz"] != null) camera.FarZ = (float)source["farz"]; if (source["viewport"] != null) { var viewport = new SlimDX.Direct3D11.Viewport(0.0f, 0.0f, (float)source["viewport"][0], (float)source["viewport"][1]); viewport.MaxZ = 1.0f; viewport.MinZ = 0.0f; camera.Viewport = viewport; } if (source["skybox"] != null) { camera.Background = BackgroundType.Skybox; camera.Skybox = Owner.GetComponent<MaterialSystem>().GetMaterial((string)source["skybox"]); } if (source["userendertarget"] != null && (bool)source["userendertarget"]) { camera.Target = Owner.GetComponent<Renderer>().CreateRenderTarget(1, (int)camera.Viewport.Width, (int)camera.Viewport.Height, "camera_rt"); camera.Target.AddDepthComponent(); camera.Target.AddTextureComponent(); camera.Target.Finish(); } if (source["reflectedcamera"] != null && (bool)source["reflectedcamera"]) { ReflectedCamera reflectedcam = actor.AddComponent<ReflectedCamera>(); reflectedcam.ReflectionPlane = new Plane((float)source["reflectionplane"][0], (float)source["reflectionplane"][1], (float)source["reflectionplane"][2], (float)source["reflectionplane"][3]); reflectedcam.MainCamera = actors[(string)source["reflectionmimic"]]; reflectedcam.ReflectionTarget = Owner.GetComponent<MaterialSystem>().GetMaterial((string)source["reflectiontarget"]); } if (source["enabled"] != null) camera.Enabled = (bool)source["enabled"]; if (source["priority"] != null) camera.RenderPriority = (int)source["priority"]; if (source["clip_plane"] != null) { camera.UseClipping = true; camera.ClipPlane = new Plane((float)source["clip_plane"][0], (float)source["clip_plane"][1], (float)source["clip_plane"][2], (float)source["clip_plane"][3]); } // Initialise and return actor.Init(); return actor; }