protected override void Draw() { // Render 3D targets. glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glDepthFunc(GL_LEQUAL); activeLoop.DrawTargets(); mainTarget.Apply(); activeLoop.Draw(); // Render 2D targets. glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glDepthFunc(GL_NEVER); canvas.DrawTargets(sb); // Draw to the main screen. glBindFramebuffer(GL_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, (uint)Resolution.WindowWidth, (uint)Resolution.WindowHeight); sb.ApplyTarget(null); mainSprite.Draw(sb); canvas.Draw(sb); sb.Flush(); }
public void DrawTargets() { renderTarget.Apply(); localCamera.Update(0); vec3[] cameraBox = DrawViewBox(camera.OrthoWidth, camera.OrthoHeight, camera.NearPlane, camera.FarPlane, vec3.Zero, camera.Orientation, Color.Green, out vec3 center); vec3 light = batch.Light.Direction; quat lightOrientation = new quat(mat4.LookAt(vec3.Zero, light, vec3.UnitY)); vec3[] transformed = cameraBox.Select(p => p * lightOrientation.Inverse).ToArray(); //localCamera.Position = center - light * 25; //localCamera.Orientation = new quat(mat4.LookAt(vec3.Zero, light, vec3.UnitY)); float left = transformed.Min(p => p.x); float right = transformed.Max(p => p.x); float top = transformed.Max(p => p.y); float bottom = transformed.Min(p => p.y); float near = batch.ShadowNearPlane; float far = batch.ShadowFarPlane; vec3 shadowPosition = center - batch.Light.Direction * (far - near) / 2; DrawViewBox(right - left, top - bottom, near, far, shadowPosition, lightOrientation, Color.Yellow, out center); primitives.Flush(); }
public void DrawTargets() { const int OrthoSize = 8; glDisable(GL_CULL_FACE); mat4 lightView = mat4.LookAt(-lightDirection * 10, vec3.Zero, vec3.UnitY); mat4 lightProjection = mat4.Ortho(-OrthoSize, OrthoSize, -OrthoSize, OrthoSize, 0.1f, 100); mat4[] bones = { bone1, bone2 }; lightMatrix = lightProjection * lightView; shadowMapTarget.Apply(); shadowMapShader.Apply(); shadowMapShader.SetUniform("bones[0]", bones); model.RecomputeWorldMatrix(); shadowMapShader.SetUniform("lightMatrix", lightMatrix * model.WorldMatrix); Draw(model.Mesh); }
unsafe void OnRenderModel() { if (mRenderer == null) { return; } mTarget.Clear(); mTarget.Apply(); var ctx = WorldFrame.Instance.GraphicsContext; var vp = ctx.Viewport; ctx.Context.Rasterizer.SetViewport(new Viewport(0, 0, ImgWidth, ImgHeight, 0.0f, 1.0f)); ctx.Context.VertexShader.SetConstantBuffer(0, mMatrixBuffer.Native); mRenderer.RenderPortrait(); mTarget.Remove(); ctx.Context.Rasterizer.SetViewport(vp); ctx.Context.ResolveSubresource(mTarget.Texture, 0, mResolveTexture, 0, Format.B8G8R8A8_UNorm); ctx.Context.CopyResource(mResolveTexture, mMapTexture); var box = ctx.Context.MapSubresource(mMapTexture, 0, MapMode.Read, MapFlags.None); var bmp = new Bitmap(ImgWidth, ImgHeight, PixelFormat.Format32bppArgb); var bmpd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); byte *ptrDst = (byte *)bmpd.Scan0.ToPointer(); byte *ptrSrc = (byte *)box.DataPointer.ToPointer(); for (var i = 0; i < bmp.Height; ++i) { UnsafeNativeMethods.CopyMemory(ptrDst + i * bmp.Width * 4, ptrSrc + i * box.RowPitch, bmp.Width * 4); } bmp.UnlockBits(bmpd); ctx.Context.UnmapSubresource(mMapTexture, 0); //Cache thumbnail if (mThumbnailCaptureFrame > 0 && --mThumbnailCaptureFrame == 0) { renderTimer.Stop(); Bitmap thumbnail = new Bitmap(ImgWidth, ImgHeight); using (var g = System.Drawing.Graphics.FromImage(thumbnail)) { g.Clear(Color.Black); g.DrawImage(bmp, new PointF(0, 0)); } ThumbnailCache.Cache(mRenderer.Model.FileName, thumbnail); if (mModels.Count > 0) //More models so render next { LoadModel(); } } }
public void Run() { using (var form = new Form()) { _form = form; form.Text = "ImGui.NET on LightDx"; form.ClientSize = new Size(800, 600); form.KeyDown += OnKeyDown; form.KeyUp += OnKeyUp; using (var device = LightDevice.Create(form)) { _device = device; var target = new RenderTarget(device.GetDefaultTarget()); target.Apply(); Pipeline pipeline = device.CompilePipeline(InputTopology.Triangle, ShaderSource.FromResource("Shader.fx", ShaderType.Vertex | ShaderType.Pixel));; _pipeline = pipeline; pipeline.SetResource(0, CreateFontTexture()); pipeline.SetBlender(Blender.AlphaBlender); pipeline.Apply(); var input = pipeline.CreateVertexDataProcessor <Vertex>(); _inputDataProcessor = input; var constant = pipeline.CreateConstantBuffer <VSConstant>(); pipeline.SetConstant(ShaderType.Vertex, 0, constant); void UpdateWindowSize() { constant.Value.Width = device.ScreenWidth; constant.Value.Height = device.ScreenHeight; constant.Update(); } device.ResolutionChanged += (sender, e) => UpdateWindowSize(); UpdateWindowSize(); form.Show(); device.RunLoop(delegate() { target.ClearAll(); RenderFrame(); device.Present(true); }); } } }
public void DrawTargets() { modelTarget.Apply(); // Note that by this point, the scene's shadow map should have already been computed. var renderer = Scene.Renderer; renderer.VpMatrix = viewOrientation; renderer.Draw(); shader.Apply(); Sky.Target.Bind(0); modelTarget.Bind(1); }
public void DrawTargets() { if (!IsEnabled) { return; } Light.RecomputeMatrices(VpMatrix); shadowMapTarget.Apply(); DrawShadow(modelRenderer); DrawShadow(spriteBatch3D); DrawShadow(skeletonRenderer); }
public void DrawTargets(float t) { if (!IsDrawEnabled) { return; } Light.Recompute(t); Light.RecomputeMatrices(camera); shadowMapTarget.Apply(); DrawShadow(modelRenderer, t); DrawShadow(spriteBatch3D, t); DrawShadow(skeletonRenderer, t); }
unsafe void OnRenderModel() { if (mRenderer == null) { return; } mTarget.Clear(); mTarget.Apply(); var ctx = WorldFrame.Instance.GraphicsContext; var vp = ctx.Viewport; ctx.Context.Rasterizer.SetViewport(new Viewport(0, 0, ClientSize.Width, ClientSize.Height, 0.0f, 1.0f)); ctx.Context.VertexShader.SetConstantBuffer(0, mMatrixBuffer.Native); mRenderer.OnFrame(); mTarget.Remove(); ctx.Context.Rasterizer.SetViewport(vp); ctx.Context.ResolveSubresource(mTarget.Texture, 0, mResolveTexture, 0, SharpDX.DXGI.Format.B8G8R8A8_UNorm); ctx.Context.CopyResource(mResolveTexture, mMapTexture); var box = ctx.Context.MapSubresource(mMapTexture, 0, MapMode.Read, MapFlags.None); var bmp = new Bitmap(ClientSize.Width, ClientSize.Height, PixelFormat.Format32bppArgb); var bmpd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); byte *ptrDst = (byte *)bmpd.Scan0.ToPointer(); byte *ptrSrc = (byte *)box.DataPointer.ToPointer(); for (var i = 0; i < bmp.Height; ++i) { UnsafeNativeMethods.CopyMemory(ptrDst + i * bmp.Width * 4, ptrSrc + i * box.RowPitch, bmp.Width * 4); } bmp.UnlockBits(bmpd); if (mPaintBitmap != null) { mPaintBitmap.Dispose(); } mPaintBitmap = bmp; ctx.Context.UnmapSubresource(mMapTexture, 0); Invalidate(); }
public void DrawTargets() { const int OrthoSize = 8; glDisable(GL_CULL_FACE); mat4 lightView = mat4.LookAt(-LightDirection * 10, vec3.Zero, vec3.UnitY); mat4 lightProjection = mat4.Ortho(-OrthoSize, OrthoSize, -OrthoSize, OrthoSize, 0.1f, 100); lightMatrix = lightProjection * lightView; shadowMapTarget.Apply(); shadowMapShader.Apply(); foreach (ModelHandle handle in handles) { Model model = handle.Model; model.RecomputeWorldMatrix(); shadowMapShader.SetUniform("lightMatrix", lightMatrix * model.WorldMatrix); Draw(handle); } }
static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var form = new Form { ClientSize = new Size(800, 600), Text = "Tutorial 4: Buffers, Shaders, and HLSL", }; using (var device = LightDevice.Create(form)) { //--------------------- // Target & Pipeline //--------------------- var target = new RenderTarget(device.GetDefaultTarget()); target.Apply(); Pipeline pipeline = device.CompilePipeline(InputTopology.Triangle, ShaderSource.FromResource("Color_vs.fx", ShaderType.Vertex), ShaderSource.FromResource("Color_ps.fx", ShaderType.Pixel)); pipeline.Apply(); //--------------------- // Vertex buffer //--------------------- var vertexDataProcessor = pipeline.CreateVertexDataProcessor <Vertex>(); var vertexBuffer = vertexDataProcessor.CreateImmutableBuffer(new[] { new Vertex { Position = new Vector4(-1, -1, 0, 1), Color = new Vector4(0, 1, 0, 1) }, new Vertex { Position = new Vector4(0, 1, 0, 1), Color = new Vector4(0, 1, 0, 1) }, new Vertex { Position = new Vector4(1, -1, 0, 1), Color = new Vector4(0, 1, 0, 1) }, }); //--------------------- // Index buffer //--------------------- var indexBuffer = pipeline.CreateImmutableIndexBuffer(new uint[] { 0, 1, 2 }); //--------------------- // Constant buffer (VS) //--------------------- var constantBuffer = pipeline.CreateConstantBuffer <Constants>(); pipeline.SetConstant(ShaderType.Vertex, 0, constantBuffer); void SetupProjMatrix() { constantBuffer.Value.Projection = device.CreatePerspectiveFieldOfView((float)Math.PI / 4).Transpose(); } device.ResolutionChanged += (sender, e) => SetupProjMatrix(); constantBuffer.Value.World = Matrix4x4.Identity.Transpose(); SetupProjMatrix(); //--------------------- // Camera //--------------------- var camera = new Camera(); //--------------------- // Start main loop //--------------------- form.Show(); device.RunMultithreadLoop(delegate() { // Update matrix constantBuffer.Value.View = camera.GetViewMatrix().Transpose(); constantBuffer.Update(); // Clear and draw target.ClearAll(); indexBuffer.DrawAll(vertexBuffer); 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); 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); }); } }