public Renderer(Control control) { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.BackBufferCount = 0; presentParams.BackBufferWidth = Screen.PrimaryScreen.WorkingArea.Width; presentParams.BackBufferHeight = Screen.PrimaryScreen.WorkingArea.Height; Device = new Device(new Direct3D(), 0, DeviceType.Hardware, control.Handle, CreateFlags.SoftwareVertexProcessing, presentParams); if ((Device.Capabilities.VertexProcessingCaps & VertexProcessingCaps.Tweening) == 0) { Report.ReportLog("Vertex tweening is not supported!"); } Device.SetRenderState(RenderState.Lighting, true); Device.SetRenderState(RenderState.DiffuseMaterialSource, ColorSource.Material); Device.SetRenderState(RenderState.EmissiveMaterialSource, ColorSource.Material); Device.SetRenderState(RenderState.SpecularMaterialSource, ColorSource.Material); Device.SetRenderState(RenderState.SpecularEnable, true); Device.SetRenderState(RenderState.AlphaBlendEnable, true); Device.SetRenderState(RenderState.BlendOperationAlpha, BlendOperation.Add); Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha); Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha); Device.SetSamplerState(0, SamplerState.MaxAnisotropy, 4); Device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Anisotropic); Device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Anisotropic); Device.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.Linear); Light light = new Light(); light.Type = LightType.Directional; light.Ambient = new Color4(int.Parse((string)Gui.Config["LightAmbientARGB"], System.Globalization.NumberStyles.AllowHexSpecifier)); light.Diffuse = new Color4(int.Parse((string)Gui.Config["LightDiffuseARGB"], System.Globalization.NumberStyles.AllowHexSpecifier)); light.Specular = new Color4(int.Parse((string)Gui.Config["LightSpecularARGB"], System.Globalization.NumberStyles.AllowHexSpecifier)); Device.SetLight(0, light); Device.EnableLight(0, true); TextFont = new SlimDX.Direct3D9.Font(Device, new System.Drawing.Font("Arial", 8)); TextColor = new Color4(Color.White); CursorMesh = Mesh.CreateSphere(Device, 1, 10, 10); CursorMaterial = new Material(); CursorMaterial.Ambient = new Color4(1, 1f, 1f, 1f); CursorMaterial.Diffuse = new Color4(1, 0.6f, 1, 0.3f); showNormals = (bool)Gui.Config["ShowNormals"]; showBones = (bool)Gui.Config["ShowBones"]; string[] mode = Enum.GetNames(typeof(ShowBoneWeights)); for (int i = 0; i < mode.Length; i++) { if (mode[i] == (string)Gui.Config["ShowBoneWeights"]) { showBoneWeights = (ShowBoneWeights)i; break; } } wireframe = (bool)Gui.Config["Wireframe"]; culling = (bool)Gui.Config["Culling"]; Background = Color.FromArgb(int.Parse((string)Gui.Config["RendererBackgroundARGB"], System.Globalization.NumberStyles.AllowHexSpecifier)); isInitialized = true; camera = new Camera(control); RenderControl = control; }
public Renderer(Control control) { try { SwapChainDescription description = new SwapChainDescription() { BufferCount = 1, Usage = Usage.RenderTargetOutput, OutputHandle = control.Handle, IsWindowed = true, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), SampleDescription = new SampleDescription(1, 0), Flags = SwapChainFlags.AllowModeSwitch, SwapEffect = SwapEffect.Discard }; Device device; Device.CreateWithSwapChain(DriverType.Hardware, 0, description, out device, out swapChain); Device = device; } catch (Exception e) { Match m = Regex.Match(e.Message, @"\((.+)\)"); int hexErr = 0; if (m.Groups.Count >= 2) { int.TryParse(m.Groups[1].Value, out hexErr); } Report.ReportLog("Device.CreateWithSwapChain failed: " + e.Message + (hexErr != 0 ? " => x" + hexErr.ToString("X8") : string.Empty)); return; } using (var resource = Resource.FromSwapChain <Texture2D>(swapChain, 0)) renderTarget = new RenderTargetView(Device, resource); Texture2DDescription depthStencilDesc = new Texture2DDescription { Width = control.Width, Height = control.Height, MipLevels = 1, ArraySize = 1, Format = Format.D24_UNorm_S8_UInt, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.DepthStencil, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; depthStencilBuffer = new Texture2D(Device, depthStencilDesc) { DebugName = "DepthStencilBuffer" }; depthStencilView = new DepthStencilView(Device, depthStencilBuffer); var context = Device.ImmediateContext; context.OutputMerger.SetTargets(depthStencilView, renderTarget); var viewport = new Viewport(0.0f, 0.0f, control.Width, control.Height, 0f, 1f); context.Rasterizer.SetViewports(viewport); // prevent DXGI handling of alt+enter, which doesn't work properly with Winforms using (var factory = swapChain.GetParent <Factory>()) factory.SetWindowAssociation(control.Handle, WindowAssociationFlags.IgnoreAltEnter); /* Device.SetRenderState(RenderState.DiffuseMaterialSource, ColorSource.Material); * Device.SetRenderState(RenderState.EmissiveMaterialSource, ColorSource.Material); * Device.SetRenderState(RenderState.SpecularMaterialSource, ColorSource.Material); * Device.SetRenderState(RenderState.SpecularEnable, true);*/ BlendStateDescription transDesc = new BlendStateDescription { AlphaToCoverageEnable = false, IndependentBlendEnable = false }; transDesc.RenderTargets[0].BlendEnable = true; transDesc.RenderTargets[0].SourceBlend = BlendOption.SourceAlpha; transDesc.RenderTargets[0].DestinationBlend = BlendOption.InverseSourceAlpha; transDesc.RenderTargets[0].BlendOperation = BlendOperation.Add; transDesc.RenderTargets[0].SourceBlendAlpha = BlendOption.One; transDesc.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero; transDesc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add; transDesc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; transparentBS = BlendState.FromDescription(Device, transDesc); context.OutputMerger.BlendState = transparentBS; context.OutputMerger.BlendFactor = blendFactor; context.OutputMerger.BlendSampleMask = ~0; /* Device.SetSamplerState(0, SamplerState.MaxAnisotropy, 4); * Device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Anisotropic); * Device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Anisotropic); * Device.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.Linear);*/ Lights = new Core.DirectionalLight[1] { new Core.DirectionalLight { Ambient = new Color4(int.Parse((string)Gui.Config["LightAmbientARGB"], System.Globalization.NumberStyles.AllowHexSpecifier)), Diffuse = new Color4(int.Parse((string)Gui.Config["LightDiffuseARGB"], System.Globalization.NumberStyles.AllowHexSpecifier)), Specular = new Color4(int.Parse((string)Gui.Config["LightSpecularARGB"], System.Globalization.NumberStyles.AllowHexSpecifier)), Direction = new Vector3(0.57735f, 0.57735f, 0.57735f) } }; /* TextFont = new SlimDX.Direct3D9.Font(Device, new System.Drawing.Font("Arial", 8)); * TextColor = new Color4(Color.White);*/ showNormals = (bool)Gui.Config["ShowNormals"]; showBones = (bool)Gui.Config["ShowBones"]; string[] mode = Enum.GetNames(typeof(ShowBoneWeights)); for (int i = 0; i < mode.Length; i++) { if (mode[i] == (string)Gui.Config["ShowBoneWeights"]) { showBoneWeights = (ShowBoneWeights)i; break; } } wireframe = (bool)Gui.Config["Wireframe"]; culling = (bool)Gui.Config["Culling"]; Background = Color.FromArgb(int.Parse((string)Gui.Config["RendererBackgroundARGB"], System.Globalization.NumberStyles.AllowHexSpecifier)); camera = new Camera(control); BuildFX(); CursorMesh = new Cursor(Device, LineLayout, PositionColorEffect.ColorTech); AxesMesh = new Axes(Device, LineLayout, PositionColorEffect.ColorTech); RenderControl = control; isInitialized = true; }