static void Main(string[] args) { var form = new RenderForm("Conway's Game of Life"); var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(form.ClientSize.Width, form.ClientSize.Height)); var vertices = new VertexBuffer(device, 3 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Managed); vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] { new Vertex { Color = Color.Red, Position = new Vector4(400.0f, 100.0f, 0.5f, 1.0f)}, new Vertex { Color = Color.Blue, Position = new Vector4(650.0f, 500.0f, 0.5f, 1.0f)}, new Vertex { Color = Color.Green, Position = new Vector4(150.0f, 500.0f, 0.5f, 1.0f)}, }); vertices.Unlock(); var vertexElements = new[] { new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0), new VertexElement(0, 16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0), VertexElement.VertexDeclarationEnd, }; var vertexDeclaration = new VertexDeclaration(device, vertexElements); RenderLoop.Run(form, () => { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); device.SetStreamSource(0, vertices, 0, 20); device.VertexDeclaration = vertexDeclaration; device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); device.EndScene(); device.Present(); }); }
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(); }
private static void Main() { var form = new RenderForm("SharpDX - MiniCube Direct3D9 Sample"); // Creates the Device var direct3D = new Direct3D(); var device = new Device(direct3D, 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters(form.ClientSize.Width, form.ClientSize.Height)); // Creates the VertexBuffer var vertices = new VertexBuffer(device, Utilities.SizeOf<Vector4>() * 2 * 36, Usage.WriteOnly, VertexFormat.None, Pool.Managed); vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] { new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), // Front new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // BACK new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Top new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f,-1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), // Bottom new Vector4( 1.0f,-1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f,-1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f,-1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4( 1.0f,-1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4( 1.0f,-1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), // Left new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), // Right new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), }); vertices.Unlock(); // Compiles the effect var effect = Effect.FromFile(device, "MiniCube.fx", ShaderFlags.None); // Allocate Vertex Elements var vertexElems = new[] { new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0), new VertexElement(0, 16, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Color, 0), VertexElement.VertexDeclarationEnd }; // Creates and sets the Vertex Declaration var vertexDecl = new VertexDeclaration(device, vertexElems); device.SetStreamSource(0, vertices, 0, Utilities.SizeOf<Vector4>() * 2); device.VertexDeclaration = vertexDecl; // Get the technique var technique = effect.GetTechnique(0); var pass = effect.GetPass(technique, 0); // Prepare matrices var view = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY); var proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, form.ClientSize.Width / (float)form.ClientSize.Height, 0.1f, 100.0f); var viewProj = Matrix.Multiply(view, proj); // Use clock var clock = new Stopwatch(); clock.Start(); RenderLoop.Run(form, () => { var time = clock.ElapsedMilliseconds / 1000.0f; device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); effect.Technique = technique; effect.Begin(); effect.BeginPass(0); var worldViewProj = Matrix.RotationX(time) * Matrix.RotationY(time * 2) * Matrix.RotationZ(time * .7f) * viewProj; effect.SetValue("worldViewProj", worldViewProj); device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12); effect.EndPass(); effect.End(); device.EndScene(); device.Present(); }); effect.Dispose(); vertices.Dispose(); device.Dispose(); direct3D.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(); }
public void Draw(ref Device device, ref Matrix Transformation, ref Matrix View, ref Matrix Proj, ref List<LightClass> Lights, ref float luminosity, ref float percent_negatif) { if (State != ObjectState.Not_prepared) { if (State != ObjectState.Disposed) { for (int i = 0; i < Lights.Count; i++) { this.effect.SetValue("LightPosition[" + i.ToString() + "]", new Vector4(Lights[i].Position, 1)); this.effect.SetValue("LightDistanceSquared[" + i.ToString() + "]", Lights[i].Range); this.effect.SetValue("LightDiffuseColor[" + i.ToString() + "]", Lights[i].Diffuse.ToVector4()); } this.effect.SetValue("percent_Negatif", percent_negatif); this.effect.SetValue("View", View); this.effect.SetValue("Projection", Proj); this.effect.SetValue("luminosity", luminosity); this.effect.Technique = Technique; this.effect.Begin(); this.effect.BeginPass(0); this.effect.SetValue("World", SubTransformation * Transformation); this.effect.SetValue("WorldInverseTranspose", Matrix.Transpose(Matrix.Invert(SubTransformation * Transformation))); device.SetStreamSource(0, VertexBuffer, 0, Utilities.SizeOf<structVertex>()); device.VertexDeclaration = this.vertexElems3D; var aaa = ResManager.getTexture(materialSet.map_Kd, ref device, false); device.SetTexture(0, ResManager.ListTextures[ResManager.getTexture(materialSet.map_Kd, ref device, false)].Texture); device.SetTexture(1, ResManager.ListTextures[ResManager.getTexture(materialSet.map_Ns, ref device, true)].Texture); device.DrawPrimitives(PrimitiveType.TriangleList, 0, Vertices.Length / 3); this.effect.EndPass(); this.effect.End(); } else { debug.WriteNicely("!", ConsoleColor.DarkRed, "ATTENTION, VOUS TENTEZ D'AFFICHER UN MESH N'EXISTANT PLUS", 0); } } else { debug.WriteNicely("!", ConsoleColor.DarkRed, "ATTENTION, VOUS TENTEZ D'AFFICHER UN MESH PAS PREPARE", 0); } }
/// <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); }