static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var form = new Form { ClientSize = new Size(800, 600), Text = "Tutorial 21: Specular Mapping", }; using (var device = LightDevice.Create(form)) { //--------------------- // Target & Pipeline //--------------------- var target = new RenderTarget(device.GetDefaultTarget(), device.CreateDefaultDepthStencilTarget()); target.Apply(); Pipeline pipeline = device.CompilePipeline(InputTopology.Triangle, ShaderSource.FromResource("SpecMap_vs.fx", ShaderType.Vertex), ShaderSource.FromResource("SpecMap_ps.fx", ShaderType.Pixel)); pipeline.Apply(); //--------------------- // Vertex buffer //--------------------- var vertexDataProcessor = pipeline.CreateVertexDataProcessor <ModelVertex>(); VertexBuffer vertexBuffer; using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial21.cube.txt")) { vertexBuffer = vertexDataProcessor.CreateImmutableBuffer(Model.ReadModelFile(stream)); } //--------------------- // Constant buffer (Matrix, VS) //--------------------- var matrixBuffer = pipeline.CreateConstantBuffer <MatrixBuffer>(); pipeline.SetConstant(ShaderType.Vertex, 0, matrixBuffer); void SetupProjMatrix() { matrixBuffer.Value.Projection = device.CreatePerspectiveFieldOfView((float)Math.PI / 4).Transpose(); } device.ResolutionChanged += (sender, e) => SetupProjMatrix(); matrixBuffer.Value.World = Matrix4x4.Identity.Transpose(); SetupProjMatrix(); //--------------------- // Constant buffer (Camera, VS) //--------------------- var cameraBuffer = pipeline.CreateConstantBuffer <CameraBuffer>(); pipeline.SetConstant(ShaderType.Vertex, 1, cameraBuffer); //--------------------- // Constant buffer (Light, PS) //--------------------- var lightBuffer = pipeline.CreateConstantBuffer <LightBuffer>(); pipeline.SetConstant(ShaderType.Pixel, 0, lightBuffer); lightBuffer.Value.DiffuseColor = Color.White.WithAlpha(1); lightBuffer.Value.LightDirection = Vector3.Normalize(new Vector3(0, 0, 1)); lightBuffer.Value.SpecularColor = Color.White.WithAlpha(1); lightBuffer.Value.SpecularPower = 16; lightBuffer.Update(); //--------------------- // Texture //--------------------- Texture2D CreateTextureFromResource(string name) { using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream($"Tutorial21.{name}.dds")) { return(device.CreateTexture2D(stream)); } } var tex1 = CreateTextureFromResource("stone02"); var tex2 = CreateTextureFromResource("bump02"); var tex3 = CreateTextureFromResource("spec02"); pipeline.SetResource(0, tex1); pipeline.SetResource(1, tex2); pipeline.SetResource(2, tex3); //--------------------- // Camera //--------------------- var camera = new Camera(); //--------------------- // Start main loop //--------------------- form.Show(); var frameCounter = new FrameCounter(); frameCounter.Start(); device.RunMultithreadLoop(delegate() { // Update matrix buffer var time = frameCounter.NextFrame() / 1000; Matrix4x4 rotate = Matrix4x4.CreateRotationX(time * 3) * Matrix4x4.CreateRotationY(time * 6) * Matrix4x4.CreateRotationZ(time * 4); matrixBuffer.Value.World *= rotate; matrixBuffer.Value.View = camera.GetViewMatrix().Transpose(); matrixBuffer.Update(); // Update camera buffer cameraBuffer.Value.CameraPosition = camera.Position; cameraBuffer.Update(); // Clear and draw target.ClearAll(); vertexBuffer.DrawAll(); device.Present(true); }); } }
static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var form = new Form { ClientSize = new Size(800, 600), Text = "Tutorial 7: 3D Model Rendering", }; using (var device = LightDevice.Create(form)) { //--------------------- // Target & Pipeline //--------------------- var target = new RenderTarget(device.GetDefaultTarget(), device.CreateDefaultDepthStencilTarget()); target.Apply(); Pipeline pipeline = device.CompilePipeline(InputTopology.Triangle, ShaderSource.FromResource("Light_vs.fx", ShaderType.Vertex), ShaderSource.FromResource("Light_ps.fx", ShaderType.Pixel)); pipeline.Apply(); //--------------------- // Vertex buffer //--------------------- var vertexDataProcessor = pipeline.CreateVertexDataProcessor <ModelVertex>(); VertexBuffer vertexBuffer; using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial07.Cube.txt")) { vertexBuffer = vertexDataProcessor.CreateImmutableBuffer(Model.ReadModelFile(stream)); } //--------------------- // Constant buffer (VS) //--------------------- var constantBufferVS = pipeline.CreateConstantBuffer <VSConstants>(); pipeline.SetConstant(ShaderType.Vertex, 0, constantBufferVS); void SetupProjMatrix() { constantBufferVS.Value.Projection = device.CreatePerspectiveFieldOfView((float)Math.PI / 4).Transpose(); } device.ResolutionChanged += (sender, e) => SetupProjMatrix(); constantBufferVS.Value.World = Matrix4x4.Identity.Transpose(); SetupProjMatrix(); //--------------------- // Constant buffer (PS) //--------------------- var constantBufferPS = pipeline.CreateConstantBuffer <PSConstants>(); pipeline.SetConstant(ShaderType.Pixel, 0, constantBufferPS); constantBufferPS.Value.Diffuse = Color.White.WithAlpha(1); constantBufferPS.Value.LightDir = Vector3.Normalize(new Vector3(-3f, -4f, 6f)); constantBufferPS.Update(); //--------------------- // Texture //--------------------- Texture2D tex; using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial07.seafloor.dds")) { tex = device.CreateTexture2D(stream); } pipeline.SetResource(0, tex); //--------------------- // Camera //--------------------- var camera = new Camera(); //--------------------- // Start main loop //--------------------- form.Show(); var frameCounter = new FrameCounter(); frameCounter.Start(); device.RunMultithreadLoop(delegate() { // Update matrix var time = frameCounter.NextFrame() / 1000; Matrix4x4 rotate = Matrix4x4.CreateRotationX(time * 3) * Matrix4x4.CreateRotationY(time * 6) * Matrix4x4.CreateRotationZ(time * 4); constantBufferVS.Value.World *= rotate; constantBufferVS.Value.View = camera.GetViewMatrix().Transpose(); constantBufferVS.Update(); // Clear and draw target.ClearAll(); vertexBuffer.DrawAll(); device.Present(true); }); } }
static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string filename; using (var dialog = new OpenFileDialog { Filter = "*.pmx|*.pmx" }) { if (dialog.ShowDialog() == DialogResult.Cancel) { return; } filename = dialog.FileName; } var form = new Form { ClientSize = new Size(800, 600), Text = "Pmx Viewer", }; var device = LightDevice.Create(form); var renderToTexturePipeline = new RenderToTexturePipeline(device); var deferredTarget = new DeferredTarget(device); var deferredRendering1 = new DeferredRenderer(device, "Deferred1.fx", new[] { deferredTarget.ColorData, deferredTarget.NormalData, deferredTarget.DepthData }, false); var deferredRendering2 = new DeferredRenderer(device, "Deferred2.fx", new[] { deferredTarget.NormalData, deferredTarget.DepthData }, true); deferredRendering2.ClearColor = Color.Black.WithAlpha(0); var model = new PmxModel(renderToTexturePipeline, filename); model.HideMaterial(29); //Hide the shadow of hair. We don't have diffuse color in shader. model.SetTransparent(26); var camera = new Camera(form); form.Show(); form.Activate(); var frameCounter = new FrameCounter(); frameCounter.Start(); device.RunMultithreadLoop(delegate() { var time = frameCounter.NextFrame() / 1000; camera.Update(); renderToTexturePipeline.View = camera.GetViewMatrix().Transpose(); renderToTexturePipeline.UpdateConstants(); deferredTarget.ClearAll(); deferredTarget.ApplyDeferred(); renderToTexturePipeline.ApplyDeferred(); model.DrawSolid(); deferredRendering1.ClearAll(); deferredRendering1.Render(); deferredTarget.ClearDeferred(); deferredTarget.ApplyForward(); renderToTexturePipeline.ApplyForward(); model.DrawTransparent(); deferredRendering2.Render(); device.Present(true); }); }