-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderTarget.cs
100 lines (81 loc) · 3.19 KB
/
RenderTarget.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Drawing;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace GLTerrain {
public class RenderTarget : IDisposable {
public Texture Target { get; private set; }
public PixelInternalFormat Format { get; private set; }
public int ID { get; private set; }
public int DepthID { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
public RenderTarget(Texture tex) {
Format = PixelInternalFormat.Rgba;
int id = -1;
GL.GenFramebuffers(1, out id);
ID = id;
ChangeTarget(tex);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, ID);
int depthbufferID = -1;
GL.GenRenderbuffers(1, out depthbufferID);
DepthID = depthbufferID;
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, depthbufferID);
GL.RenderbufferStorage(
RenderbufferTarget.Renderbuffer,
RenderbufferStorage.DepthComponent32f,
Width,
Height);
GL.FramebufferRenderbuffer(
FramebufferTarget.Framebuffer,
FramebufferAttachment.DepthAttachment,
RenderbufferTarget.Renderbuffer,
depthbufferID);
FramebufferErrorCode code = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
if (code != FramebufferErrorCode.FramebufferComplete) {
throw new Exception("Frame buffer failed to create! " + code.ToString());
}
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
public RenderTarget(int width, int height, PixelInternalFormat format, TextureUnit unit)
: this(new Texture(new Bitmap(width, height), true, true))
{
Format = format;
}
public void ChangeTarget(Texture newTarget) {
Target = newTarget;
GL.BindFramebuffer(FramebufferTarget.Framebuffer, ID);
GL.FramebufferTexture2D(
FramebufferTarget.Framebuffer,
FramebufferAttachment.ColorAttachment0,
TextureTarget.Texture2D,
Target.ID,
0);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
Width = Target.Width;
Height = Target.Height;
}
public void Bind() {
GL.BindFramebuffer(FramebufferTarget.Framebuffer, ID);
GL.PushAttrib(AttribMask.ViewportBit);
GL.Viewport(0, 0, Width, Height);
}
public void Unbind(bool buildMipmap) {
GL.PopAttrib();
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
if (buildMipmap) {
Target.Bind();
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
Target.Unbind();
}
}
public void Dispose() {
int id = ID;
GL.DeleteFramebuffers(1, ref id);
id = DepthID;
GL.DeleteRenderbuffers(1, ref id);
Target.Dispose();
}
}
}