-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRenderCache.cs
173 lines (150 loc) · 5 KB
/
RenderCache.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// RenderCache.cs
//
// Author:
// Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2016 jp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL;
namespace Opuz2015
{
public class RenderCache : IDisposable
{
static Tetra.Shader cacheShader;
public static void EnableCacheShader(){
cacheShader.Enable();
}
#region CTOR
static RenderCache(){
cacheShader = new Tetra.Shader(null,"#Opuz2015.shaders.cache.frag");
cacheShader.Enable ();
cacheShader.SetMVP (OpenTK.Matrix4.CreateOrthographicOffCenter (-0.5f, 0.5f, -0.5f, 0.5f, 1, -1));
cacheShader.Disable ();
}
public RenderCache(System.Drawing.Size _cacheSize){
cacheSize = _cacheSize;
createCache ();
}
public RenderCache(System.Drawing.Size _cacheSize, DrawBuffersEnum[] _dbe)
: this (_cacheSize)
{
dbe = _dbe;
GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboId);
GL.DrawBuffers(dbe.Length, dbe);
if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
throw new Exception(GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer).ToString());
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
#endregion
protected System.Drawing.Size cacheSize;
protected bool wireframe = false;
protected bool colorCacheIsUpToDate = false;
protected bool selectionCacheIsUpToDate = false;
protected int fboId;
DrawBuffersEnum[] dbe = new DrawBuffersEnum[]
{
DrawBuffersEnum.ColorAttachment0,
};
public Tetra.Texture colorTex, depthTex;
public System.Drawing.Size CacheSize {
get { return cacheSize; }
set {
if (value == cacheSize)
return;
cacheSize = value;
createCache ();
}
}
protected virtual void createCache(){
this.Dispose();
initFbo ();
}
void initFbo()
{
GL.GenFramebuffers(1, out fboId);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboId);
configureFbo ();
GL.DrawBuffers(dbe.Length, dbe);
if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
{
throw new Exception(GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer).ToString());
}
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
}
protected virtual void configureFbo(){
Tetra.Texture.DefaultTarget = TextureTarget.Texture2DMultisample;
Tetra.Texture.GenerateMipMaps = false;
colorTex = new Tetra.Texture()
{
Width = CacheSize.Width,
Height = CacheSize.Height,
InternalFormat = PixelInternalFormat.Rgba,
PixelFormat = PixelFormat.Bgra,
PixelType = PixelType.UnsignedByte,
Samples = 4
}; colorTex.Create ();
// Create Depth Renderbuffer
// depthTex = new Tetra.Texture()
// {
// Width = CacheSize.Width,
// Height = CacheSize.Height,
// InternalFormat = PixelInternalFormat.DepthComponent32f,
// PixelFormat = PixelFormat.DepthComponent,
// PixelType = PixelType.Float,
// }; depthTex.Create ();
//
// GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment,
// TextureTarget.Texture2D, depthTex, 0);
GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0,
TextureTarget.Texture2DMultisample, colorTex, 0);
Tetra.Texture.ResetToDefaultLoadingParams ();
}
public virtual void UpdateFbo()
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboId);
onDraw ();
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
colorCacheIsUpToDate = true;
}
public void Bind(bool clear = true){
GL.BindFramebuffer(FramebufferTarget.Framebuffer, fboId);
if (!clear)
return;
GL.Clear (ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);
}
protected virtual void onDraw(){}
public void PaintCache(){
// GL.ActiveTexture (TextureUnit.Texture1);
// GL.BindTexture (TextureTarget.Texture2D, depthTex);
GL.ActiveTexture (TextureUnit.Texture0);
GL.BindTexture (TextureTarget.Texture2DMultisample, colorTex);
Tetra.ShadedTexture.quad.Render (BeginMode.TriangleStrip);
}
#region IDisposable implementation
public virtual void Dispose ()
{
if (GL.IsTexture (colorTex))
GL.DeleteTexture (colorTex);
if (GL.IsTexture (depthTex))
GL.DeleteTexture (depthTex);
if (GL.IsFramebuffer (fboId))
GL.DeleteFramebuffer (fboId);
}
#endregion
}
}