public void Draw(IFramebuffer targetFramebuffer, IRenderbuffer depthStencil) { infra.GlContext.States.DepthStencil.StencilTestEnable.Set(true); infra.GlContext.States.Blend.BlendEnable.Set(false); infra.GlContext.States.DepthStencil.DepthMask.Set(false); infra.GlContext.States.DepthStencil.DepthTestEnable.Set(false); infra.GlContext.States.DepthStencil.Back.StencilWriteMask.Set(0xff); infra.GlContext.States.DepthStencil.Front.StencilWriteMask.Set(0xff); infra.GlContext.States.Rasterizer.CullFaceEnable.Set(false); var offScreen = offScreenContainer.Get(this, depthStencil, depthStencil.Width, depthStencil.Height, depthStencil.Samples, OffScreenTtl); drawSpotFramebuffer.AttachRenderbuffer(FramebufferAttachmentPoint.Color0, offScreen.ColorBuffer); drawSpotFramebuffer.AttachRenderbuffer(FramebufferAttachmentPoint.DepthStencil, depthStencil); drawSpotFramebuffer.ClearColor(0, new Color4(0, 0, 0, 0)); infra.GlContext.Bindings.Framebuffers.Draw.Set(drawSpotFramebuffer); infra.GlContext.Bindings.Program.Set(program); infra.GlContext.Bindings.VertexArray.Set(vao); SetStencilFunc(StencilFunction.Equal); infra.GlContext.Actions.Draw.Arrays(BeginMode.TriangleStrip, 0, 4); offScreen.Resolve(); infra.GlContext.Bindings.Framebuffers.Draw.Set(offScreen.Framebuffer); SetStencilFunc(StencilFunction.Always); bleedDrawer.Draw(offScreen.ResolvedTex, Common.Numericals.Colors.Color4.Orange); offScreen.Resolve(); infra.GlContext.Bindings.Framebuffers.Draw.Set(targetFramebuffer); SetStencilFunc(StencilFunction.Notequal); quadDrawer.Draw(offScreen.ResolvedTex); //quadDrawer.Draw(Common.Numericals.Colors.Color4.Blue); SetStencilFunc(StencilFunction.Always); infra.GlContext.States.DepthStencil.StencilTestEnable.Set(false); infra.GlContext.States.DepthStencil.DepthMask.Set(true); infra.GlContext.States.DepthStencil.DepthTestEnable.Set(true); }
public override void Initialize() { renderTarget = Context.Create.Texture2D(RenderTargetSize, RenderTargetSize, TextureHelper.CalculateMipCount(RenderTargetSize, 1, 1), Format.Rgba8); depthStencil = Context.Create.Renderbuffer(RenderTargetSize, RenderTargetSize, Format.Depth24Stencil8); framebuffer = Context.Create.Framebuffer(); framebuffer.AttachTextureImage(FramebufferAttachmentPoint.Color0, renderTarget, 0); framebuffer.AttachRenderbuffer(FramebufferAttachmentPoint.DepthStencil, depthStencil); var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 24 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[] { new Vertex(1f, -1f, 1f, 1f, 0f, 0f, 0f, 0f), new Vertex(1f, 1f, 1f, 1f, 0f, 0f, 1f, 0f), new Vertex(1f, 1f, -1f, 1f, 0f, 0f, 1f, 1f), new Vertex(1f, -1f, -1f, 1f, 0f, 0f, 0f, 1f), new Vertex(1f, 1f, 1f, 0f, 1f, 0f, 0f, 0f), new Vertex(-1f, 1f, 1f, 0f, 1f, 0f, 1f, 0f), new Vertex(-1f, 1f, -1f, 0f, 1f, 0f, 1f, 1f), new Vertex(1f, 1f, -1f, 0f, 1f, 0f, 0f, 1f), new Vertex(-1f, 1f, 1f, -1f, 0f, 0f, 0f, 0f), new Vertex(-1f, -1f, 1f, -1f, 0f, 0f, 1f, 0f), new Vertex(-1f, -1f, -1f, -1f, 0f, 0f, 1f, 1f), new Vertex(-1f, 1f, -1f, -1f, 0f, 0f, 0f, 1f), new Vertex(-1f, -1f, 1f, 0f, -1f, 0f, 0f, 0f), new Vertex(1f, -1f, 1f, 0f, -1f, 0f, 1f, 0f), new Vertex(1f, -1f, -1f, 0f, -1f, 0f, 1f, 1f), new Vertex(-1f, -1f, -1f, 0f, -1f, 0f, 0f, 1f), new Vertex(-1f, -1f, 1f, 0f, 0f, 1f, 0f, 0f), new Vertex(-1f, 1f, 1f, 0f, 0f, 1f, 1f, 0f), new Vertex(1f, 1f, 1f, 0f, 0f, 1f, 1f, 1f), new Vertex(1f, -1f, 1f, 0f, 0f, 1f, 0f, 1f), new Vertex(-1f, 1f, -1f, 0f, 0f, -1f, 0f, 0f), new Vertex(-1f, -1f, -1f, 0f, 0f, -1f, 1f, 0f), new Vertex(1f, -1f, -1f, 0f, 0f, -1f, 1f, 1f), new Vertex(1f, 1f, -1f, 0f, 0f, -1f, 0f, 1f) }); var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 36 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[] { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23 }); vertexArray = Context.Create.VertexArray(); vertexArray.SetElementArrayBuffer(indexBuffer); vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 32, 0); vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 32, 12); vertexArray.SetVertexAttributeF(2, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, 32, 24); transformBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw); cameraBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw); #if INTEL_WORKAROUND cameraOutsideBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw); #endif cameraExtraBuffer = Context.Create.Buffer(BufferTarget.Uniform, 12, BufferUsageHint.DynamicDraw); lightBuffer = Context.Create.Buffer(BufferTarget.Uniform, 12, BufferUsageHint.DynamicDraw); using (var textureLoader = new TextureLoader("../Textures/DiffuseTest.png")) { diffuseMap = Context.Create.Texture2D(textureLoader.Width, textureLoader.Height, TextureHelper.CalculateMipCount(textureLoader.Width, textureLoader.Height, 1), Format.Rgba8); for (int i = 0; i < diffuseMap.MipCount; i++) diffuseMap.SetData(i, textureLoader.GetMipData(i), FormatColor.Rgba, FormatType.UnsignedByte); } sampler = Context.Create.Sampler(); sampler.SetMagFilter(TextureMagFilter.Linear); sampler.SetMinFilter(TextureMinFilter.LinearMipmapLinear); sampler.SetMaxAnisotropy(16f); IVertexShader vsh = Context.Create.VertexShader(VertexShaderText); IFragmentShader fsh = Context.Create.FragmentShader(FragmentShaderText); program = Context.Create.Program(new ShaderProgramDescription { VertexShaders = new[] {vsh}, FragmentShaders = new[] {fsh}, VertexAttributeNames = new[] {"in_position", "in_normal", "in_tex_coord"}, UniformBufferNames = new[] {"Transform", "Camera", "Light"}, SamplerNames = new[] {"DiffuseMap"} }); }
public override void Initialize() { renderTarget = Context.Create.Texture2D(RenderTargetSize, RenderTargetSize, TextureHelper.CalculateMipCount(RenderTargetSize, 1, 1), Format.Rgba8); depthStencil = Context.Create.Renderbuffer(RenderTargetSize, RenderTargetSize, Format.Depth24Stencil8); framebuffer = Context.Create.Framebuffer(); framebuffer.AttachTextureImage(FramebufferAttachmentPoint.Color0, renderTarget, 0); framebuffer.AttachRenderbuffer(FramebufferAttachmentPoint.DepthStencil, depthStencil); var vertexBuffer = Context.Create.Buffer(BufferTarget.Array, 24 * 8 * sizeof(float), BufferUsageHint.StaticDraw, new[] { new Vertex(1f, -1f, 1f, 1f, 0f, 0f, 0f, 0f), new Vertex(1f, 1f, 1f, 1f, 0f, 0f, 1f, 0f), new Vertex(1f, 1f, -1f, 1f, 0f, 0f, 1f, 1f), new Vertex(1f, -1f, -1f, 1f, 0f, 0f, 0f, 1f), new Vertex(1f, 1f, 1f, 0f, 1f, 0f, 0f, 0f), new Vertex(-1f, 1f, 1f, 0f, 1f, 0f, 1f, 0f), new Vertex(-1f, 1f, -1f, 0f, 1f, 0f, 1f, 1f), new Vertex(1f, 1f, -1f, 0f, 1f, 0f, 0f, 1f), new Vertex(-1f, 1f, 1f, -1f, 0f, 0f, 0f, 0f), new Vertex(-1f, -1f, 1f, -1f, 0f, 0f, 1f, 0f), new Vertex(-1f, -1f, -1f, -1f, 0f, 0f, 1f, 1f), new Vertex(-1f, 1f, -1f, -1f, 0f, 0f, 0f, 1f), new Vertex(-1f, -1f, 1f, 0f, -1f, 0f, 0f, 0f), new Vertex(1f, -1f, 1f, 0f, -1f, 0f, 1f, 0f), new Vertex(1f, -1f, -1f, 0f, -1f, 0f, 1f, 1f), new Vertex(-1f, -1f, -1f, 0f, -1f, 0f, 0f, 1f), new Vertex(-1f, -1f, 1f, 0f, 0f, 1f, 0f, 0f), new Vertex(-1f, 1f, 1f, 0f, 0f, 1f, 1f, 0f), new Vertex(1f, 1f, 1f, 0f, 0f, 1f, 1f, 1f), new Vertex(1f, -1f, 1f, 0f, 0f, 1f, 0f, 1f), new Vertex(-1f, 1f, -1f, 0f, 0f, -1f, 0f, 0f), new Vertex(-1f, -1f, -1f, 0f, 0f, -1f, 1f, 0f), new Vertex(1f, -1f, -1f, 0f, 0f, -1f, 1f, 1f), new Vertex(1f, 1f, -1f, 0f, 0f, -1f, 0f, 1f) }); var indexBuffer = Context.Create.Buffer(BufferTarget.ElementArray, 36 * sizeof(ushort), BufferUsageHint.StaticDraw, new ushort[] { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23 }); vertexArray = Context.Create.VertexArray(); vertexArray.SetElementArrayBuffer(indexBuffer); vertexArray.SetVertexAttributeF(0, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 32, 0); vertexArray.SetVertexAttributeF(1, vertexBuffer, VertexAttributeDimension.Three, VertexAttribPointerType.Float, false, 32, 12); vertexArray.SetVertexAttributeF(2, vertexBuffer, VertexAttributeDimension.Two, VertexAttribPointerType.Float, false, 32, 24); transformBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw); cameraBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw); #if INTEL_WORKAROUND cameraOutsideBuffer = Context.Create.Buffer(BufferTarget.Uniform, 64, BufferUsageHint.DynamicDraw); #endif cameraExtraBuffer = Context.Create.Buffer(BufferTarget.Uniform, 12, BufferUsageHint.DynamicDraw); lightBuffer = Context.Create.Buffer(BufferTarget.Uniform, 12, BufferUsageHint.DynamicDraw); using (var textureLoader = new TextureLoader("../Textures/DiffuseTest.png")) { diffuseMap = Context.Create.Texture2D(textureLoader.Width, textureLoader.Height, TextureHelper.CalculateMipCount(textureLoader.Width, textureLoader.Height, 1), Format.Rgba8); for (int i = 0; i < diffuseMap.MipCount; i++) { diffuseMap.SetData(i, textureLoader.GetMipData(i), FormatColor.Rgba, FormatType.UnsignedByte); } } sampler = Context.Create.Sampler(); sampler.SetMagFilter(TextureMagFilter.Linear); sampler.SetMinFilter(TextureMinFilter.LinearMipmapLinear); sampler.SetMaxAnisotropy(16f); IVertexShader vsh = Context.Create.VertexShader(VertexShaderText); IFragmentShader fsh = Context.Create.FragmentShader(FragmentShaderText); program = Context.Create.Program(new ShaderProgramDescription { VertexShaders = new[] { vsh }, FragmentShaders = new[] { fsh }, VertexAttributeNames = new[] { "in_position", "in_normal", "in_tex_coord" }, UniformBufferNames = new[] { "Transform", "Camera", "Light" }, SamplerNames = new[] { "DiffuseMap" } }); }