static void LoadCubemapLevel(GLContext control, int size, int level, AglLightMap aglLightMap, EnvironmentGraphics environmentSettings, AglLightMap.LightArea lightMapEnv, int ID) { GL.Viewport(0, 0, size, size); var shader = GlobalShaders.GetShader("LIGHTMAP"); shader.Enable(); UpdateUniforms(control, shader, level, aglLightMap, environmentSettings, lightMapEnv); for (int i = 0; i < 6; i++) { GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, TextureTarget.TextureCubeMapPositiveX + i, ID, level); } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(); var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } GL.UseProgram(0); }
static void FilterProbeVolume(GLContext control, GLTexture2D normalsMap, GLTextureCube diffuseCubemap, Vector4[] shData, int lightmapTexID, int size, int mipLevel) { if (frameBuffer == null) { Init(size); } if (frameBuffer.Width != size) { frameBuffer.Resize(size, size); } frameBuffer.Bind(); GL.Viewport(0, 0, size, size); //attach face to fbo as color attachment for (int i = 0; i < 6; i++) { //Each fragment output is a cubemap face GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, TextureTarget.TextureCubeMapPositiveX + i, lightmapTexID, mipLevel); } var shader = GlobalShaders.GetShader("PROBE"); shader.Enable(); var programID = shader.program; LoadUniforms(programID, shData); GL.ActiveTexture(TextureUnit.Texture0 + 1); normalsMap.Bind(); shader.SetInt("sampler0", 1); GL.ActiveTexture(TextureUnit.Texture0 + 2); diffuseCubemap.Bind(); shader.SetInt("sampler1", 2); //Draw once with 6 fragment outputs to form a cubemap GL.ClearColor(0, 0, 0, 1); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(); var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } frameBuffer.Unbind(); GL.BindTexture(TextureTarget.TextureCubeMap, 0); GL.UseProgram(0); }
public static void CreateShadowPrepassTexture(GLContext control, int shadowMap, int depthTexture, GLTexture2D output) { if (Filter == null) { Init(); } Filter.Bind(); GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, output.ID, 0); GL.Viewport(0, 0, control.Width, control.Height); GL.BindTexture(TextureTarget.Texture2D, 0); var shader = GlobalShaders.GetShader("SHADOWPREPASS"); shader.Enable(); if (Filter.Width != control.Width || Filter.Height != control.Height) { Filter.Resize(control.Width, control.Height); } if (output.Width != control.Width || output.Height != control.Height) { output.Bind(); GL.TexImage2D(output.Target, 0, output.PixelInternalFormat, control.Width, control.Height, 0, output.PixelFormat, output.PixelType, IntPtr.Zero); output.Unbind(); } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.ActiveTexture(TextureUnit.Texture1); GL.BindTexture(TextureTarget.Texture2D, depthTexture); shader.SetInt("depthTexture", 1); GL.ActiveTexture(TextureUnit.Texture2); GL.BindTexture(TextureTarget.Texture2D, shadowMap); shader.SetInt("shadowMap", 2); ScreenQuadRender.Draw(); GL.Flush(); Filter.Unbind(); GL.BindTexture(TextureTarget.Texture2D, 0); GL.UseProgram(0); GL.Viewport(0, 0, control.Width, control.Height); }
public static void DrawSceneLights(Camera camera, GLTexture gbuffer, GLTexture linearDepth) { return; var shader = GlobalShaders.GetShader("LIGHTPREPASS"); shader.Enable(); GL.ActiveTexture(TextureUnit.Texture0 + 1); gbuffer.Bind(); shader.SetInt("normalsTexture", 1); GL.ActiveTexture(TextureUnit.Texture0 + 2); linearDepth.Bind(); shader.SetInt("depthTexture", 2); int programID = shader.program; var projectionMatrixInverse = camera.ProjectionMatrix.Inverted(); var viewMatrixInverse = camera.ViewMatrix.Inverted(); var mtxProjView = camera.ProjectionMatrix * camera.ViewMatrix; shader.SetMatrix4x4("mtxProjInv", ref projectionMatrixInverse); shader.SetMatrix4x4("mtxViewInv", ref viewMatrixInverse); shader.SetVector3("cameraPosition", camera.TargetPosition); float projectionA = camera.ZFar / (camera.ZFar - camera.ZNear); float projectionB = (-camera.ZFar * camera.ZNear) / (camera.ZFar - camera.ZNear); shader.SetFloat("projectionA", projectionA); shader.SetFloat("projectionB", projectionB); shader.SetFloat("z_range", camera.ZFar - camera.ZNear); shader.SetFloat("fov_x", camera.Fov); shader.SetFloat("fov_y", camera.Fov); PointLight[] pointLights = new PointLight[32]; for (int i = 0; i < 32; i++) { pointLights[i] = new PointLight(); if (i == 0) { pointLights[i].Position = PointPosition; pointLights[i].Color = new Vector4(1, 0, 0, 1); } GL.Uniform4(GL.GetUniformLocation(programID, $"pointLights[{i}].uColor"), pointLights[i].Color); GL.Uniform3(GL.GetUniformLocation(programID, $"pointLights[{i}].uPosition"), pointLights[i].Position); } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(); }
public static void FilterScreen(GLContext control) { if (Filter == null) { Init(); } GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); Filter.Bind(); GL.Viewport(0, 0, control.Width, control.Height); GL.BindTexture(TextureTarget.Texture2D, 0); var shader = GlobalShaders.GetShader("SCREEN"); shader.Enable(); shader.SetInt("flipVertical", 1); var texture = (GLTexture2D)control.ScreenBuffer.Attachments[0]; if (Filter.Width != control.Width || Filter.Height != control.Height) { Filter.Resize(control.Width, control.Height); } GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(shader, texture.ID); ScreenBuffer = (GLTexture2D)Filter.Attachments[0]; GL.Flush(); Filter.Unbind(); GL.BindTexture(TextureTarget.Texture2D, 0); GL.UseProgram(0); ScreenBuffer.Bind(); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLevel, 0); GL.BindTexture(TextureTarget.Texture2D, 0); control.ScreenBuffer.Bind(); GL.Viewport(0, 0, control.Width, control.Height); }
public static void CreateColorLookupTexture(GLTexture3D output) { GL.BindTexture(output.Target, 0); int LUT_SIZE = output.Width; if (Filter == null) { Init(LUT_SIZE); } Filter.Bind(); for (int i = 0; i < 8; i++) { GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, output.ID, 0, i); } GL.Viewport(0, 0, LUT_SIZE, LUT_SIZE); GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); var shader = GlobalShaders.GetShader("COLOR_CORRECTION"); shader.Enable(); UpdateUniforms(shader); ScreenQuadRender.Draw(); Filter.Unbind(); GL.UseProgram(0); var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (errorcheck != FramebufferErrorCode.FramebufferComplete) { throw new Exception(errorcheck.ToString()); } }
public static GLTexture2D FilterScreen(GLContext control, GLTexture2D colorTexture) { if (Filter == null) { Init(); } Filter.Bind(); GL.Viewport(0, 0, control.Width, control.Height); GL.BindTexture(TextureTarget.Texture2D, 0); var shader = GlobalShaders.GetShader("BLOOM_EXTRACT"); shader.Enable(); shader.SetFloat("bloom_intensity", 1.0f); if (Filter.Width != control.Width || Filter.Height != control.Height) { Filter.Resize(control.Width, control.Height); } GL.ClearColor(System.Drawing.Color.Black); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); ScreenQuadRender.Draw(shader, colorTexture.ID); var screenBuffer = (GLTexture2D)Filter.Attachments[0]; GL.Flush(); Filter.Unbind(); GL.BindTexture(TextureTarget.Texture2D, 0); GL.UseProgram(0); GL.Viewport(0, 0, control.Width, control.Height); return(screenBuffer); }
public static int CreateTextureRender(STGenericTexture texture, int width, int height, bool displayAlpha = true) { if (texture.RenderableTex == null) { texture.LoadRenderableTexture(); } if (texture.RenderableTex == null) { return(-1); } int ID = texture.RenderableTex.ID; if (texture.Platform.OutputFormat == TexFormat.BC5_SNORM) { var reloaded = GLTexture2D.FromGeneric(texture, new ImageParameters() { UseSoftwareDecoder = (texture.Platform.OutputFormat == TexFormat.BC5_SNORM), }); ID = reloaded.ID; } var shader = GlobalShaders.GetShader("TEXTURE_ICON"); Framebuffer frameBuffer = new Framebuffer(FramebufferTarget.Framebuffer, width, height, PixelInternalFormat.Rgba, 1); frameBuffer.Bind(); GL.ClearColor(0, 0, 0, 0); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Viewport(0, 0, width, height); GL.Disable(EnableCap.Blend); shader.Enable(); shader.SetBoolToInt("isSRGB", texture.IsSRGB); int[] mask = new int[4] { OpenGLHelper.GetSwizzle(texture.RedChannel), OpenGLHelper.GetSwizzle(texture.GreenChannel), OpenGLHelper.GetSwizzle(texture.BlueChannel), OpenGLHelper.GetSwizzle(displayAlpha ? texture.AlphaChannel : STChannelType.One), }; ((GLTexture)texture.RenderableTex).Bind(); GL.TexParameter(((GLTexture)texture.RenderableTex).Target, TextureParameterName.TextureSwizzleRgba, mask); //Draw the texture onto the framebuffer ScreenQuadRender.Draw(shader, ID); //Disable shader and textures GL.UseProgram(0); GL.BindTexture(TextureTarget.Texture2D, 0); var image = (GLTexture2D)frameBuffer.Attachments[0]; return(image.ID); /* //Dispose frame buffer * frameBuffer.Dispoe(); * frameBuffer.DisposeRenderBuffer();*/ }