-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathScreenBufferTexture.cs
84 lines (65 loc) · 2.96 KB
/
ScreenBufferTexture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK.Graphics.OpenGL;
namespace GLFrameworkEngine
{
/// <summary>
/// Creates a screen buffer texture of the current color buffer.
/// This is drawn before meshes with the UserColorPass setting used.
/// Meshes with UserColorPass can obtain the buffer with GetColorBuffer()
/// </summary>
public class ScreenBufferTexture
{
private static Framebuffer Filter;
public static GLTexture2D ScreenBuffer;
public static DepthTexture DepthBuffer;
public static void Init()
{
DepthBuffer = new DepthTexture(640, 720, PixelInternalFormat.DepthComponent24);
Filter = new Framebuffer(FramebufferTarget.Framebuffer,
640, 720, PixelInternalFormat.Rgba, 1, false);
Filter.AddAttachment(FramebufferAttachment.DepthAttachment, DepthBuffer);
}
public static GLTexture2D GetColorBuffer() {
return ScreenBuffer;
}
public static DepthTexture GetDepthBuffer() {
return DepthBuffer;
}
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);
}
}
}