|
@@ -0,0 +1,444 @@
|
|
1
|
+#include "framebuffer.hpp"
|
|
2
|
+
|
|
3
|
+slop::Framebuffer::Framebuffer() {
|
|
4
|
+ m_shader = new slop::Shader( "simple", resource->getRealPath( "simple.vert" ), resource->getRealPath( "simple.frag" ) );
|
|
5
|
+ generatedBuffers = false;
|
|
6
|
+ m_width = 0;
|
|
7
|
+ m_height = 0;
|
|
8
|
+ m_flags = (slop::Framebuffer::buffers)0;
|
|
9
|
+ m_depth = 0;
|
|
10
|
+ m_stencil = 0;
|
|
11
|
+ m_frame = 0;
|
|
12
|
+ m_buffers[0] = 0;
|
|
13
|
+ m_buffers[1] = 0;
|
|
14
|
+ m_texture = 0;
|
|
15
|
+}
|
|
16
|
+
|
|
17
|
+void slop::Framebuffer::setShader( std::string shader ) {
|
|
18
|
+ delete m_shader;
|
|
19
|
+ m_shader = new slop::Shader( shader, resource->getRealPath( shader + ".vert" ), resource->getRealPath( shader + ".frag" ) );
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+slop::Framebuffer::Framebuffer( unsigned int width, unsigned int height, unsigned char flags, std::string shader ) {
|
|
23
|
+ m_shader = new slop::Shader( shader, resource->getRealPath( shader + ".vert" ), resource->getRealPath( shader + ".frag" ) );
|
|
24
|
+ generatedBuffers = false;
|
|
25
|
+ m_width = width;
|
|
26
|
+ m_height = height;
|
|
27
|
+ m_flags = flags;
|
|
28
|
+ if ( m_flags & stencil && m_flags & depth ) {
|
|
29
|
+ fprintf( stderr, "Most hardware doesn't support using a FBO with a depth and stencil attached! Condensing them to one render buffer..." );
|
|
30
|
+ }
|
|
31
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
32
|
+ glGenFramebuffers( 1, &m_frame );
|
|
33
|
+ glBindFramebuffer( GL_FRAMEBUFFER, m_frame );
|
|
34
|
+ if ( m_flags & color ) {
|
|
35
|
+ glGenTextures( 1, &m_texture );
|
|
36
|
+ glBindTexture( GL_TEXTURE_2D, m_texture );
|
|
37
|
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
|
|
38
|
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
|
39
|
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
|
40
|
+ glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture, 0 );
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
44
|
+ glGenRenderbuffers( 1, &m_depth );
|
|
45
|
+ glBindRenderbuffer( GL_RENDERBUFFER, m_depth );
|
|
46
|
+ glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_width, m_height );
|
|
47
|
+ glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depth );
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ //This is the only way I could get a proper stencil buffer, by making a GL_DEPTH_STENCIL attachment to both points.
|
|
51
|
+ if ( m_flags & stencil ) {
|
|
52
|
+ glGenRenderbuffers( 1, &m_stencil );
|
|
53
|
+ glBindRenderbuffer( GL_RENDERBUFFER, m_stencil );
|
|
54
|
+ glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_STENCIL, m_width, m_height );
|
|
55
|
+ glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_stencil );
|
|
56
|
+ glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_stencil );
|
|
57
|
+ }
|
|
58
|
+ check();
|
|
59
|
+ //Make sure we unbind when we're done.
|
|
60
|
+ glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
|
61
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
62
|
+ glGenFramebuffersEXT( 1, &m_frame );
|
|
63
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_frame );
|
|
64
|
+
|
|
65
|
+ if ( m_flags & color ) {
|
|
66
|
+ glGenTextures( 1, &m_texture );
|
|
67
|
+ glBindTexture( GL_TEXTURE_2D, m_texture );
|
|
68
|
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
|
|
69
|
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
|
70
|
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
|
71
|
+ glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_texture, 0 );
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
75
|
+ glGenRenderbuffersEXT( 1, &m_depth );
|
|
76
|
+ glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_depth );
|
|
77
|
+ glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, m_width, m_height );
|
|
78
|
+ glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_depth );
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ //This is the only way I could get a proper stencil buffer, by making a GL_DEPTH_STENCIL attachment to both points.
|
|
82
|
+ if ( m_flags & stencil ) {
|
|
83
|
+ glGenRenderbuffersEXT( 1, &m_stencil );
|
|
84
|
+ glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_stencil );
|
|
85
|
+ glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, m_width, m_height );
|
|
86
|
+ glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_stencil );
|
|
87
|
+ glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_stencil );
|
|
88
|
+ }
|
|
89
|
+ check();
|
|
90
|
+ //Make sure we unbind when we're done.
|
|
91
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
|
|
92
|
+ } else {
|
|
93
|
+ fprintf( stderr, "Error: Failed to create framebuffer! You need OpenGL 3.0 or the GL_EXT_framebuffer_object extension!\n" );
|
|
94
|
+ }
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+slop::Framebuffer::~Framebuffer() {
|
|
98
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
99
|
+ glDeleteFramebuffers( 1, &m_frame );
|
|
100
|
+ if ( m_flags & color ) {
|
|
101
|
+ glDeleteTextures( 1, &m_texture );
|
|
102
|
+ }
|
|
103
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
104
|
+ glDeleteRenderbuffers( 1, &m_depth );
|
|
105
|
+ }
|
|
106
|
+ if ( m_flags & stencil ) {
|
|
107
|
+ glDeleteRenderbuffers( 1, &m_stencil );
|
|
108
|
+ }
|
|
109
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
110
|
+ glDeleteFramebuffersEXT( 1, &m_frame );
|
|
111
|
+ if ( m_flags & color ) {
|
|
112
|
+ glDeleteTextures( 1, &m_texture );
|
|
113
|
+ }
|
|
114
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
115
|
+ glDeleteRenderbuffersEXT( 1, &m_depth );
|
|
116
|
+ }
|
|
117
|
+ if ( m_flags & stencil ) {
|
|
118
|
+ glDeleteRenderbuffersEXT( 1, &m_stencil );
|
|
119
|
+ }
|
|
120
|
+ }
|
|
121
|
+ if ( generatedBuffers ) {
|
|
122
|
+ glDeleteBuffers( 2, m_buffers );
|
|
123
|
+ }
|
|
124
|
+ delete m_shader;
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+void slop::Framebuffer::bind() {
|
|
128
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
129
|
+ glBindFramebuffer( GL_FRAMEBUFFER, m_frame );
|
|
130
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
131
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_frame );
|
|
132
|
+ }
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+void slop::Framebuffer::unbind() {
|
|
136
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
137
|
+ glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
|
138
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
139
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
|
|
140
|
+ }
|
|
141
|
+}
|
|
142
|
+
|
|
143
|
+void slop::Framebuffer::createFromTexture( unsigned int texture, std::string shader ) {
|
|
144
|
+ // First make sure we're empty.
|
|
145
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
146
|
+ glDeleteFramebuffers( 1, &m_frame );
|
|
147
|
+ if ( m_flags & color ) {
|
|
148
|
+ glDeleteTextures( 1, &m_texture );
|
|
149
|
+ }
|
|
150
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
151
|
+ glDeleteRenderbuffers( 1, &m_depth );
|
|
152
|
+ }
|
|
153
|
+ if ( m_flags & stencil ) {
|
|
154
|
+ glDeleteRenderbuffers( 1, &m_stencil );
|
|
155
|
+ }
|
|
156
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
157
|
+ glDeleteFramebuffersEXT( 1, &m_frame );
|
|
158
|
+ if ( m_flags & color ) {
|
|
159
|
+ glDeleteTextures( 1, &m_texture );
|
|
160
|
+ }
|
|
161
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
162
|
+ glDeleteRenderbuffersEXT( 1, &m_depth );
|
|
163
|
+ }
|
|
164
|
+ if ( m_flags & stencil ) {
|
|
165
|
+ glDeleteRenderbuffersEXT( 1, &m_stencil );
|
|
166
|
+ }
|
|
167
|
+ }
|
|
168
|
+ delete m_shader;
|
|
169
|
+ m_shader = new slop::Shader( shader, resource->getRealPath( shader + ".vert" ), resource->getRealPath( shader + ".frag" ) );
|
|
170
|
+ // Set flags to 0 so we don't delete the texture on deconstruction.
|
|
171
|
+ m_flags = 0;
|
|
172
|
+ m_texture = texture;
|
|
173
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
174
|
+ glGenFramebuffers( 1, &m_frame );
|
|
175
|
+ glBindFramebuffer( GL_FRAMEBUFFER, m_frame );
|
|
176
|
+ glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture, 0 );
|
|
177
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
178
|
+ glGenFramebuffersEXT( 1, &m_frame );
|
|
179
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_frame );
|
|
180
|
+ glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_texture, 0 );
|
|
181
|
+ }
|
|
182
|
+ check();
|
|
183
|
+ glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
|
184
|
+}
|
|
185
|
+
|
|
186
|
+void slop::Framebuffer::create( unsigned int width, unsigned int height, unsigned char flags, std::string shader ) {
|
|
187
|
+ if ( m_width == width && m_height == height && m_flags == flags ) {
|
|
188
|
+ return;
|
|
189
|
+ }
|
|
190
|
+ delete m_shader;
|
|
191
|
+ m_shader = new slop::Shader( shader, resource->getRealPath( shader + ".vert" ), resource->getRealPath( shader + ".frag" ) );
|
|
192
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
193
|
+ glDeleteFramebuffers( 1, &m_frame );
|
|
194
|
+ if ( m_flags & color ) {
|
|
195
|
+ glDeleteTextures( 1, &m_texture );
|
|
196
|
+ }
|
|
197
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
198
|
+ glDeleteRenderbuffers( 1, &m_depth );
|
|
199
|
+ }
|
|
200
|
+ if ( m_flags & stencil ) {
|
|
201
|
+ glDeleteRenderbuffers( 1, &m_stencil );
|
|
202
|
+ }
|
|
203
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
204
|
+ glDeleteFramebuffersEXT( 1, &m_frame );
|
|
205
|
+ if ( m_flags & color ) {
|
|
206
|
+ glDeleteTextures( 1, &m_texture );
|
|
207
|
+ }
|
|
208
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
209
|
+ glDeleteRenderbuffersEXT( 1, &m_depth );
|
|
210
|
+ }
|
|
211
|
+ if ( m_flags & stencil ) {
|
|
212
|
+ glDeleteRenderbuffersEXT( 1, &m_stencil );
|
|
213
|
+ }
|
|
214
|
+ }
|
|
215
|
+ m_flags = flags;
|
|
216
|
+ m_width = width;
|
|
217
|
+ m_height = height;
|
|
218
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
219
|
+ glGenFramebuffers( 1, &m_frame );
|
|
220
|
+ glBindFramebuffer( GL_FRAMEBUFFER, m_frame );
|
|
221
|
+
|
|
222
|
+ if ( m_flags & color ) {
|
|
223
|
+ glGenTextures( 1, &m_texture );
|
|
224
|
+ glBindTexture( GL_TEXTURE_2D, m_texture );
|
|
225
|
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0 );
|
|
226
|
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
|
227
|
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
|
228
|
+ glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture, 0 );
|
|
229
|
+ }
|
|
230
|
+
|
|
231
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
232
|
+ glGenRenderbuffers( 1, &m_depth );
|
|
233
|
+ glBindRenderbuffer( GL_RENDERBUFFER, m_depth );
|
|
234
|
+ glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_width, m_height );
|
|
235
|
+ glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depth );
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ //This is the only way I could get a proper stencil buffer, by making a GL_DEPTH_STENCIL attachment to both points.
|
|
239
|
+ if ( m_flags & stencil ) {
|
|
240
|
+ glGenRenderbuffers( 1, &m_stencil );
|
|
241
|
+ glBindRenderbuffer( GL_RENDERBUFFER, m_stencil );
|
|
242
|
+ glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_STENCIL, m_width, m_height );
|
|
243
|
+ glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_stencil );
|
|
244
|
+ glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_stencil );
|
|
245
|
+ }
|
|
246
|
+ //Make sure we unbind when we're done.
|
|
247
|
+ glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
|
248
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
249
|
+ glGenFramebuffersEXT( 1, &m_frame );
|
|
250
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_frame );
|
|
251
|
+
|
|
252
|
+ if ( m_flags & color ) {
|
|
253
|
+ glGenTextures( 1, &m_texture );
|
|
254
|
+ glBindTexture( GL_TEXTURE_2D, m_texture );
|
|
255
|
+ glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0 );
|
|
256
|
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
|
257
|
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
|
|
258
|
+ glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_texture, 0 );
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ if ( m_flags & depth && !( m_flags & stencil ) ) {
|
|
262
|
+ glGenRenderbuffersEXT( 1, &m_depth );
|
|
263
|
+ glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_depth );
|
|
264
|
+ glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, m_width, m_height );
|
|
265
|
+ glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_depth );
|
|
266
|
+ }
|
|
267
|
+
|
|
268
|
+ //This is the only way I could get a proper stencil buffer, by making a GL_DEPTH_STENCIL attachment to both points.
|
|
269
|
+ if ( m_flags & stencil ) {
|
|
270
|
+ glGenRenderbuffersEXT( 1, &m_stencil );
|
|
271
|
+ glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, m_stencil );
|
|
272
|
+ glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, m_width, m_height );
|
|
273
|
+ glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_stencil );
|
|
274
|
+ glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_stencil );
|
|
275
|
+ }
|
|
276
|
+ //Make sure we unbind when we're done.
|
|
277
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
|
|
278
|
+ }
|
|
279
|
+}
|
|
280
|
+
|
|
281
|
+void slop::Framebuffer::check()
|
|
282
|
+{
|
|
283
|
+ if ( GLEW_VERSION_3_0 ) {
|
|
284
|
+ glBindFramebuffer( GL_FRAMEBUFFER, m_frame );
|
|
285
|
+ unsigned int err = glCheckFramebufferStatus( GL_FRAMEBUFFER );
|
|
286
|
+ if ( err == GL_FRAMEBUFFER_COMPLETE ) {
|
|
287
|
+ return;
|
|
288
|
+ }
|
|
289
|
+ fprintf( stderr, "Error: Framebuffer failed a check procedure for reason: " );
|
|
290
|
+ switch ( err ) {
|
|
291
|
+ case GL_FRAMEBUFFER_UNDEFINED: {
|
|
292
|
+ fprintf( stderr, "GL_FRAMEBUFFER_UNDEFINED\n" );
|
|
293
|
+ break;
|
|
294
|
+ }
|
|
295
|
+ case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: {
|
|
296
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n" );
|
|
297
|
+ break;
|
|
298
|
+ }
|
|
299
|
+ case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: {
|
|
300
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT\n" );
|
|
301
|
+ break;
|
|
302
|
+ }
|
|
303
|
+ case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: {
|
|
304
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER\n" );
|
|
305
|
+ break;
|
|
306
|
+ }
|
|
307
|
+ case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: {
|
|
308
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER\n" );
|
|
309
|
+ break;
|
|
310
|
+ }
|
|
311
|
+ case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: {
|
|
312
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS\n" );
|
|
313
|
+ break;
|
|
314
|
+ }
|
|
315
|
+ case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: {
|
|
316
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE\n" );
|
|
317
|
+ break;
|
|
318
|
+ }
|
|
319
|
+ case GL_FRAMEBUFFER_UNSUPPORTED: {
|
|
320
|
+ fprintf( stderr, "GL_FRAMEBUFFER_UNSUPPORTED\n" );
|
|
321
|
+ break;
|
|
322
|
+ }
|
|
323
|
+ default: {
|
|
324
|
+ fprintf( stderr, "UNKNOWN\n" );
|
|
325
|
+ break;
|
|
326
|
+ }
|
|
327
|
+ }
|
|
328
|
+ glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
|
329
|
+ } else if ( GLEW_EXT_framebuffer_object ) {
|
|
330
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, m_frame );
|
|
331
|
+ unsigned int err = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
|
|
332
|
+ if ( err == GL_FRAMEBUFFER_COMPLETE_EXT ) {
|
|
333
|
+ return;
|
|
334
|
+ }
|
|
335
|
+ switch ( err ) {
|
|
336
|
+ /* Apparently doesn't exist as an extension.
|
|
337
|
+ case GL_FRAMEBUFFER_UNDEFINED_EXT: {
|
|
338
|
+ fprintf( stderr, "GL_FRAMEBUFFER_UNDEFINED_EXT\n" );
|
|
339
|
+ break;
|
|
340
|
+ }*/
|
|
341
|
+ case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: {
|
|
342
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT\n" );
|
|
343
|
+ break;
|
|
344
|
+ }
|
|
345
|
+ case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: {
|
|
346
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT\n" );
|
|
347
|
+ break;
|
|
348
|
+ }
|
|
349
|
+ case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: {
|
|
350
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT\n" );
|
|
351
|
+ break;
|
|
352
|
+ }
|
|
353
|
+ case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: {
|
|
354
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT\n" );
|
|
355
|
+ break;
|
|
356
|
+ }
|
|
357
|
+ case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT: {
|
|
358
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT\n" );
|
|
359
|
+ break;
|
|
360
|
+ }
|
|
361
|
+ case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT: {
|
|
362
|
+ fprintf( stderr, "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT\n" );
|
|
363
|
+ break;
|
|
364
|
+ }
|
|
365
|
+ case GL_FRAMEBUFFER_UNSUPPORTED_EXT: {
|
|
366
|
+ fprintf( stderr, "GL_FRAMEBUFFER_UNSUPPORTED_EXT\n" );
|
|
367
|
+ break;
|
|
368
|
+ }
|
|
369
|
+ default: {
|
|
370
|
+ fprintf( stderr, "Unknown framebuffer error!\n" );
|
|
371
|
+ break;
|
|
372
|
+ }
|
|
373
|
+ }
|
|
374
|
+ glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
|
|
375
|
+ }
|
|
376
|
+}
|
|
377
|
+
|
|
378
|
+void slop::Framebuffer::generateBuffers() {
|
|
379
|
+ if ( generatedBuffers ) {
|
|
380
|
+ return;
|
|
381
|
+ }
|
|
382
|
+ std::vector<glm::vec2> verts;
|
|
383
|
+ std::vector<glm::vec2> uvs;
|
|
384
|
+ verts.push_back( glm::vec2( -1, -1 ) );
|
|
385
|
+ verts.push_back( glm::vec2( 1, -1 ) );
|
|
386
|
+ verts.push_back( glm::vec2( 1, 1 ) );
|
|
387
|
+ verts.push_back( glm::vec2( -1, 1 ) );
|
|
388
|
+ uvs.push_back( glm::vec2( 0, 0 ) );
|
|
389
|
+ uvs.push_back( glm::vec2( 1, 0 ) );
|
|
390
|
+ uvs.push_back( glm::vec2( 1, 1 ) );
|
|
391
|
+ uvs.push_back( glm::vec2( 0, 1 ) );
|
|
392
|
+
|
|
393
|
+ glGenBuffers( 2, m_buffers );
|
|
394
|
+ glBindBuffer( GL_ARRAY_BUFFER, m_buffers[0] );
|
|
395
|
+ glBufferData( GL_ARRAY_BUFFER, verts.size() * sizeof( glm::vec2 ), &verts[0], GL_STATIC_DRAW );
|
|
396
|
+ glBindBuffer( GL_ARRAY_BUFFER, m_buffers[1] );
|
|
397
|
+ glBufferData( GL_ARRAY_BUFFER, uvs.size() * sizeof( glm::vec2 ), &uvs[0], GL_STATIC_DRAW );
|
|
398
|
+ generatedBuffers = true;
|
|
399
|
+}
|
|
400
|
+
|
|
401
|
+void slop::Framebuffer::clear( glm::vec4 clearcolor, unsigned char flags ) {
|
|
402
|
+ // FIXME: This should auto-bind and unbind, but I was too lazy to find out how to push and pop gl states.
|
|
403
|
+ unsigned int colorbit = 0;
|
|
404
|
+ if ( flags & color ) {
|
|
405
|
+ colorbit = colorbit | GL_COLOR_BUFFER_BIT;
|
|
406
|
+ }
|
|
407
|
+ if ( m_flags & depth && flags & depth ) {
|
|
408
|
+ colorbit = colorbit | GL_DEPTH_BUFFER_BIT;
|
|
409
|
+ }
|
|
410
|
+ if ( m_flags & stencil && flags & stencil) {
|
|
411
|
+ colorbit = colorbit | GL_STENCIL_BUFFER_BIT;
|
|
412
|
+ }
|
|
413
|
+ glClearColor( clearcolor.x, clearcolor.y, clearcolor.z, clearcolor.w );
|
|
414
|
+ glClear( colorbit );
|
|
415
|
+}
|
|
416
|
+
|
|
417
|
+void slop::Framebuffer::draw( float time, unsigned int desktop ) {
|
|
418
|
+ if ( !( m_flags & color ) ) {
|
|
419
|
+ return;
|
|
420
|
+ }
|
|
421
|
+
|
|
422
|
+ generateBuffers();
|
|
423
|
+
|
|
424
|
+ //slop::Shader* shader = shaders->get( "simple" );
|
|
425
|
+ m_shader->bind();
|
|
426
|
+ m_shader->setParameter( "texture", 0 );
|
|
427
|
+ m_shader->setParameter( "desktop", 1 );
|
|
428
|
+ m_shader->setParameter( "time", time );
|
|
429
|
+ m_shader->setParameter( "desktopWidth", (int)xengine->getWidth() );
|
|
430
|
+ m_shader->setParameter( "desktopHeight", (int)xengine->getHeight() );
|
|
431
|
+ m_shader->setAttribute( "vertex", m_buffers[0], 2 );
|
|
432
|
+ m_shader->setAttribute( "uv", m_buffers[1], 2 );
|
|
433
|
+
|
|
434
|
+ glActiveTexture(GL_TEXTURE0 + 0);
|
|
435
|
+ glBindTexture( GL_TEXTURE_2D, m_texture );
|
|
436
|
+ glActiveTexture(GL_TEXTURE0 + 1);
|
|
437
|
+ glBindTexture( GL_TEXTURE_2D, desktop );
|
|
438
|
+ glActiveTexture(GL_TEXTURE0 + 0);
|
|
439
|
+ glEnable( GL_TEXTURE_2D );
|
|
440
|
+ glDrawArrays( GL_QUADS, 0, 4 );
|
|
441
|
+ glDisable( GL_TEXTURE_2D );
|
|
442
|
+
|
|
443
|
+ m_shader->unbind();
|
|
444
|
+}
|