private void InitGrabber() { try { this.pixelFormat = PixelFormat.Format32bppRgb; boundsRect = new System.Drawing.Rectangle(0, 0, WIDTH, HEIGHT); var format = SharpDX.Direct3D9.Format.A8R8G8B8; SharpDX.Direct3D9.PresentParameters present_params = new SharpDX.Direct3D9.PresentParameters(); present_params.Windowed = true; present_params.BackBufferFormat = format; present_params.SwapEffect = SharpDX.Direct3D9.SwapEffect.Discard; present_params.BackBufferWidth = WIDTH; present_params.BackBufferHeight = HEIGHT; dx9Device = new SharpDX.Direct3D9.Device(new SharpDX.Direct3D9.Direct3D(), 0, SharpDX.Direct3D9.DeviceType.Hardware, IntPtr.Zero, SharpDX.Direct3D9.CreateFlags.HardwareVertexProcessing, present_params); dx9Device.SetRenderState(RenderState.CullMode, Cull.None); dx9Device.SetRenderState(RenderState.Lighting, false); dx9Device.SetRenderState(RenderState.AntialiasedLineEnable, false); } catch (SharpDX.SharpDXException dxe) { LdpLog.Error("SharpDX InitializeDX9\n" + dxe.Message); } catch (Exception ex) { LdpLog.Error("InitializeDX9\n" + ex.Message); } }
public void Initialize() { Direct3D = new Direct3D(); Device = new Device(Direct3D, 0, DeviceType.Hardware, dxViewport.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(dxViewport.Width, dxViewport.Height)); Device.SetTransform(TransformState.Projection, Matrix.OrthoLH(dxViewport.Width, dxViewport.Height, -1, 1)); Device.SetRenderState(RenderState.AlphaBlendEnable, true); Device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha); Device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha); Content = new ContentManager(Device); spriteBatch = new SpriteBatch(Device, Content); Initialized = true; }
/* Sets the different device parameters on each reset. */ private void SetDeviceParameters(IntPtr device) { SharpDX.Direct3D9.Device localDevice = new SharpDX.Direct3D9.Device(device); if (_dx9Settings.EnableMSAA) { localDevice.SetRenderState(RenderState.MultisampleAntialias, true); } if (_dx9Settings.EnableAF) { localDevice.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Anisotropic); localDevice.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Anisotropic); localDevice.SetSamplerState(0, SamplerState.MaxAnisotropy, _dx9Settings.AFLevel); } }
public static void Main() { var form = new RenderForm("SimpleD3D9 by C#") { ClientSize = new Size(1024, 768) }; var device = new D3D9Device( new Direct3D(), CUDADevice.Default.ID, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(form.ClientSize.Width, form.ClientSize.Height)); var vertices = new VertexBuffer(device, Utilities.SizeOf <Vector4>() * Total, Usage.WriteOnly, VertexFormat.None, Pool.Default); var vertexElems = new [] { new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), new VertexElement(0, 12, DeclarationType.Ubyte4, DeclarationMethod.Default, DeclarationUsage.Color, 0), VertexElement.VertexDeclarationEnd }; var vertexDecl = new VertexDeclaration(device, vertexElems); var worker = Worker.CreateByFunc(() => Generate(device)); var updater = new SimpleD3D9(GPUModuleTarget.Worker(worker)); var view = Matrix.LookAtLH( new Vector3(0.0f, 3.0f, -2.0f), // the camera position new Vector3(0.0f, 0.0f, 0.0f), // the look-at position new Vector3(0.0f, 1.0f, 0.0f)); // the up direction var proj = Matrix.PerspectiveFovLH( (float)(Math.PI / 4.0), // the horizontal field of view 1.0f, 1.0f, 100.0f); device.SetTransform(TransformState.View, view); device.SetTransform(TransformState.Projection, proj); device.SetRenderState(RenderState.Lighting, false); var vbres = RegisterVerticesResource(vertices); var clock = System.Diagnostics.Stopwatch.StartNew(); RenderLoop.Run(form, () => { var time = (float)(clock.Elapsed.TotalMilliseconds) / 300.0f; updater.Update(vbres, time); // Now normal D3D9 rendering procedure. device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new ColorBGRA(0, 40, 100, 0), 1.0f, 0); device.BeginScene(); device.VertexDeclaration = vertexDecl; device.SetStreamSource(0, vertices, 0, Utilities.SizeOf <Vector4>()); // we use PointList as the graphics primitives device.DrawPrimitives(SharpDX.Direct3D9.PrimitiveType.PointList, 0, Total); device.EndScene(); device.Present(); }); UnregisterVerticesResource(vbres); updater.Dispose(); worker.Dispose(); vertexDecl.Dispose(); vertices.Dispose(); device.Dispose(); form.Dispose(); }
public static void Main() { var form = new RenderForm("SimpleD3D9 by C#") { ClientSize = new Size(1024, 768) }; var device = new D3D9Device( new Direct3D(), CUDADevice.Default.ID, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(form.ClientSize.Width, form.ClientSize.Height)); var vertices = new VertexBuffer(device, Utilities.SizeOf<Vector4>()*Total, Usage.WriteOnly, VertexFormat.None, Pool.Default); var vertexElems = new [] { new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), new VertexElement(0, 12, DeclarationType.Ubyte4, DeclarationMethod.Default, DeclarationUsage.Color, 0), VertexElement.VertexDeclarationEnd }; var vertexDecl = new VertexDeclaration(device, vertexElems); var worker = Worker.CreateByFunc(() => Generate(device)); var updater = new SimpleD3D9(GPUModuleTarget.Worker(worker)); var view = Matrix.LookAtLH( new Vector3(0.0f, 3.0f, -2.0f), // the camera position new Vector3(0.0f, 0.0f, 0.0f), // the look-at position new Vector3(0.0f, 1.0f, 0.0f)); // the up direction var proj = Matrix.PerspectiveFovLH( (float) (Math.PI/4.0), // the horizontal field of view 1.0f, 1.0f, 100.0f); device.SetTransform(TransformState.View, view); device.SetTransform(TransformState.Projection, proj); device.SetRenderState(RenderState.Lighting, false); var vbres = RegisterVerticesResource(vertices); var clock = System.Diagnostics.Stopwatch.StartNew(); RenderLoop.Run(form, () => { var time = (float) (clock.Elapsed.TotalMilliseconds)/300.0f; updater.Update(vbres, time); // Now normal D3D9 rendering procedure. device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new ColorBGRA(0, 40, 100, 0), 1.0f, 0); device.BeginScene(); device.VertexDeclaration = vertexDecl; device.SetStreamSource(0, vertices, 0, Utilities.SizeOf<Vector4>()); // we use PointList as the graphics primitives device.DrawPrimitives(SharpDX.Direct3D9.PrimitiveType.PointList, 0, Total); device.EndScene(); device.Present(); }); UnregisterVerticesResource(vbres); updater.Dispose(); worker.Dispose(); vertexDecl.Dispose(); vertices.Dispose(); device.Dispose(); form.Dispose(); }
/// <summary>Renders the view.</summary> public void Render(Device device) { device.SetRenderState(RenderState.CullMode, Cull.None); device.SetRenderState(RenderState.ZEnable, ZBufferType.DontUseZBuffer); device.SetRenderState(RenderState.ZWriteEnable, false); device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Point); device.SetSamplerState(0, SamplerState.MagFilter, _useLinearFiltering ? TextureFilter.Linear : TextureFilter.Point); device.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.Point); device.SetRenderState(RenderState.ShadeMode, ShadeMode.Flat); device.SetRenderState(RenderState.FillMode, FillMode.Solid); device.SetRenderState(RenderState.Lighting, false); device.SetTexture(0, _viewTexture); device.SetStreamSource(0, _vertexBuffer, 0, Marshal.SizeOf(typeof(DX9TransformedColorTexture))); device.VertexDeclaration = _vertexDeclaration; device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); }
public void RenderSur(SharpDX.Direct3D9.Device device, List <ModelViewForm.MeshGroup> meshgroups) { var colors = new SharpDX.Color[] { new SharpDX.Color(180, 180, 180, 50), new SharpDX.Color(180, 148, 62, 50), new SharpDX.Color(114, 124, 206, 50), new SharpDX.Color(96, 168, 98, 50), new SharpDX.Color(193, 92, 165, 50), new SharpDX.Color(203, 90, 76, 50) }; device.SetTexture(0, null); device.SetTransform(TransformState.World, SharpDX.Matrix.Identity); device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.SelectArg2); device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.TFactor); device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.SelectArg2); device.SetTextureStageState(0, TextureStage.AlphaArg2, TextureArgument.TFactor); Dictionary <uint, SharpDX.Matrix> meshgrouptransforms = new Dictionary <uint, SharpDX.Matrix>(); foreach (var m in meshgroups) { var k = Utilities.FLModelCRC(m.Name); if (meshgrouptransforms.ContainsKey(k)) { continue; } else { meshgrouptransforms[k] = m.Transform; } } int c = 0; foreach (var m in Meshes) { if (meshgrouptransforms.ContainsKey(m.MeshId)) { device.SetTransform(TransformState.World, meshgrouptransforms[m.MeshId]); } else { device.SetTransform(TransformState.World, SharpDX.Matrix.Identity); } List <SharpDX.Vector3> tmpVertices = new List <SharpDX.Vector3>(); List <int> tmpTri = new List <int>(); foreach (var ss in m.SurfaceSections) { foreach (var vert in ss.Vertices) { tmpVertices.Add(new SharpDX.Vector3(vert.X, vert.Y, vert.Z)); } foreach (var tg in ss.TriangleGroups) { foreach (var tri in tg.Triangles) { foreach (var i in tri.Indices) { tmpTri.Add(i.VertexId); System.Diagnostics.Debug.Assert(i.VertexId < tmpVertices.Count); } } } } var color = colors[c % colors.Length]; device.SetRenderState(RenderState.TextureFactor, color.ToRgba()); device.DrawIndexedUserPrimitives(SharpDX.Direct3D9.PrimitiveType.TriangleList, 0, tmpVertices.Count, tmpTri.Count / 3, tmpTri.ToArray(), SharpDX.Direct3D9.Format.Index32, tmpVertices.ToArray()); c++; } }