FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/core/src/processing/opengl/FrameBuffer.java at master · 2php/processing · GitHub
2php
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
core
/
src
/
processing
/
opengl
/
FrameBuffer.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
503 lines (387 loc) · 12.8 KB
Breadcrumbs
processing
/
core
/
src
/
processing
/
opengl
/
FrameBuffer.java
Copy path
File metadata and controls
503 lines (387 loc) · 12.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package
processing
.
opengl
;
import
processing
.
core
.
PApplet
;
import
processing
.
core
.
PConstants
;
import
processing
.
opengl
.
PGraphicsOpenGL
.
GLResourceFrameBuffer
;
import
java
.
nio
.
IntBuffer
;
/**
* Encapsulates a Frame Buffer Object for offscreen rendering.
* When created with onscreen == true, it represents the normal
* framebuffer. Needed by the stack mechanism in OPENGL2 to return
* to onscreen rendering after a sequence of pushFramebuffer calls.
* It transparently handles the situations when the FBO extension is
* not available.
*
* By Andres Colubri.
*/
public
class
FrameBuffer
implements
PConstants
{
protected
PGraphicsOpenGL
pg
;
protected
PGL
pgl
;
protected
int
context
;
// The context that created this framebuffer.
public
int
glFbo
;
public
int
glDepth
;
public
int
glStencil
;
public
int
glDepthStencil
;
public
int
glMultisample
;
public
int
width
;
public
int
height
;
private
GLResourceFrameBuffer
glres
;
protected
int
depthBits
;
protected
int
stencilBits
;
protected
boolean
packedDepthStencil
;
protected
boolean
multisample
;
protected
int
nsamples
;
protected
int
numColorBuffers
;
protected
Texture
[]
colorBufferTex
;
protected
boolean
screenFb
;
protected
boolean
noDepth
;
protected
IntBuffer
pixelBuffer
;
FrameBuffer
(
PGraphicsOpenGL
pg
) {
this
.
pg
=
pg
;
pgl
=
pg
.
pgl
;
context
=
pgl
.
createEmptyContext
();
}
FrameBuffer
(
PGraphicsOpenGL
pg
,
int
w
,
int
h
,
int
samples
,
int
colorBuffers
,
int
depthBits
,
int
stencilBits
,
boolean
packedDepthStencil
,
boolean
screen
) {
this
(
pg
);
glFbo
=
0
;
glDepth
=
0
;
glStencil
=
0
;
glDepthStencil
=
0
;
glMultisample
=
0
;
if
(
screen
) {
// If this framebuffer is used to represent a on-screen buffer,
// then it doesn't make it sense for it to have multisampling,
// color, depth or stencil buffers.
depthBits
=
stencilBits
=
samples
=
colorBuffers
=
0
;
}
width
=
w
;
height
=
h
;
if
(
1
<
samples
) {
multisample
=
true
;
nsamples
=
samples
;
}
else
{
multisample
=
false
;
nsamples
=
1
;
}
numColorBuffers
=
colorBuffers
;
colorBufferTex
=
new
Texture
[
numColorBuffers
];
for
(
int
i
=
0
;
i
<
numColorBuffers
;
i
++) {
colorBufferTex
[
i
] =
null
;
}
if
(
depthBits
<
1
&&
stencilBits
<
1
) {
this
.
depthBits
=
0
;
this
.
stencilBits
=
0
;
this
.
packedDepthStencil
=
false
;
}
else
{
if
(
packedDepthStencil
) {
// When combined depth/stencil format is required, the depth and stencil
// bits are overriden and the 24/8 combination for a 32 bits surface is
// used.
this
.
depthBits
=
24
;
this
.
stencilBits
=
8
;
this
.
packedDepthStencil
=
true
;
}
else
{
this
.
depthBits
=
depthBits
;
this
.
stencilBits
=
stencilBits
;
this
.
packedDepthStencil
=
false
;
}
}
screenFb
=
screen
;
allocate
();
noDepth
=
false
;
pixelBuffer
=
null
;
}
FrameBuffer
(
PGraphicsOpenGL
pg
,
int
w
,
int
h
) {
this
(
pg
,
w
,
h
,
1
,
1
,
0
,
0
,
false
,
false
);
}
FrameBuffer
(
PGraphicsOpenGL
pg
,
int
w
,
int
h
,
boolean
screen
) {
this
(
pg
,
w
,
h
,
1
,
1
,
0
,
0
,
false
,
screen
);
}
public
void
clear
() {
pg
.
pushFramebuffer
();
pg
.
setFramebuffer
(
this
);
pgl
.
clearDepth
(
1
);
pgl
.
clearStencil
(
0
);
pgl
.
clearColor
(
0
,
0
,
0
,
0
);
pgl
.
clear
(
PGL
.
DEPTH_BUFFER_BIT
|
PGL
.
STENCIL_BUFFER_BIT
|
PGL
.
COLOR_BUFFER_BIT
);
pg
.
popFramebuffer
();
}
public
void
copyColor
(
FrameBuffer
dest
) {
copy
(
dest
,
PGL
.
COLOR_BUFFER_BIT
);
}
public
void
copyDepth
(
FrameBuffer
dest
) {
copy
(
dest
,
PGL
.
DEPTH_BUFFER_BIT
);
}
public
void
copyStencil
(
FrameBuffer
dest
) {
copy
(
dest
,
PGL
.
STENCIL_BUFFER_BIT
);
}
public
void
copy
(
FrameBuffer
dest
,
int
mask
) {
pgl
.
bindFramebufferImpl
(
PGL
.
READ_FRAMEBUFFER
,
this
.
glFbo
);
pgl
.
bindFramebufferImpl
(
PGL
.
DRAW_FRAMEBUFFER
,
dest
.
glFbo
);
pgl
.
blitFramebuffer
(
0
,
0
,
this
.
width
,
this
.
height
,
0
,
0
,
dest
.
width
,
dest
.
height
,
mask
,
PGL
.
NEAREST
);
pgl
.
bindFramebufferImpl
(
PGL
.
READ_FRAMEBUFFER
,
pg
.
getCurrentFB
().
glFbo
);
pgl
.
bindFramebufferImpl
(
PGL
.
DRAW_FRAMEBUFFER
,
pg
.
getCurrentFB
().
glFbo
);
}
public
void
bind
() {
pgl
.
bindFramebufferImpl
(
PGL
.
FRAMEBUFFER
,
glFbo
);
}
public
void
disableDepthTest
() {
noDepth
=
true
;
}
public
void
finish
() {
if
(
noDepth
) {
// No need to clear depth buffer because depth testing was disabled.
if
(
pg
.
getHint
(
ENABLE_DEPTH_TEST
)) {
pgl
.
enable
(
PGL
.
DEPTH_TEST
);
}
else
{
pgl
.
disable
(
PGL
.
DEPTH_TEST
);
}
}
}
public
void
readPixels
() {
if
(
pixelBuffer
==
null
)
createPixelBuffer
();
pixelBuffer
.
rewind
();
pgl
.
readPixels
(
0
,
0
,
width
,
height
,
PGL
.
RGBA
,
PGL
.
UNSIGNED_BYTE
,
pixelBuffer
);
}
public
void
getPixels
(
int
[]
pixels
) {
if
(
pixelBuffer
!=
null
) {
pixelBuffer
.
get
(
pixels
,
0
,
pixels
.
length
);
pixelBuffer
.
rewind
();
}
}
public
IntBuffer
getPixelBuffer
() {
return
pixelBuffer
;
}
public
boolean
hasDepthBuffer
() {
return
0
<
depthBits
;
}
public
boolean
hasStencilBuffer
() {
return
0
<
stencilBits
;
}
public
void
setFBO
(
int
id
) {
if
(
screenFb
) {
glFbo
=
id
;
}
}
///////////////////////////////////////////////////////////
// Color buffer setters.
public
void
setColorBuffer
(
Texture
tex
) {
setColorBuffers
(
new
Texture
[] {
tex
},
1
);
}
public
void
setColorBuffers
(
Texture
[]
textures
) {
setColorBuffers
(
textures
,
textures
.
length
);
}
public
void
setColorBuffers
(
Texture
[]
textures
,
int
n
) {
if
(
screenFb
)
return
;
if
(
numColorBuffers
!=
PApplet
.
min
(
n
,
textures
.
length
)) {
throw
new
RuntimeException
(
"Wrong number of textures to set the color "
+
"buffers."
);
}
for
(
int
i
=
0
;
i
<
numColorBuffers
;
i
++) {
colorBufferTex
[
i
] =
textures
[
i
];
}
pg
.
pushFramebuffer
();
pg
.
setFramebuffer
(
this
);
// Making sure nothing is attached.
for
(
int
i
=
0
;
i
<
numColorBuffers
;
i
++) {
pgl
.
framebufferTexture2D
(
PGL
.
FRAMEBUFFER
,
PGL
.
COLOR_ATTACHMENT0
+
i
,
PGL
.
TEXTURE_2D
,
0
,
0
);
}
for
(
int
i
=
0
;
i
<
numColorBuffers
;
i
++) {
pgl
.
framebufferTexture2D
(
PGL
.
FRAMEBUFFER
,
PGL
.
COLOR_ATTACHMENT0
+
i
,
colorBufferTex
[
i
].
glTarget
,
colorBufferTex
[
i
].
glName
,
0
);
}
pgl
.
validateFramebuffer
();
pg
.
popFramebuffer
();
}
public
void
swapColorBuffers
() {
for
(
int
i
=
0
;
i
<
numColorBuffers
-
1
;
i
++) {
int
i1
= (
i
+
1
);
Texture
tmp
=
colorBufferTex
[
i
];
colorBufferTex
[
i
] =
colorBufferTex
[
i1
];
colorBufferTex
[
i1
] =
tmp
;
}
pg
.
pushFramebuffer
();
pg
.
setFramebuffer
(
this
);
for
(
int
i
=
0
;
i
<
numColorBuffers
;
i
++) {
pgl
.
framebufferTexture2D
(
PGL
.
FRAMEBUFFER
,
PGL
.
COLOR_ATTACHMENT0
+
i
,
colorBufferTex
[
i
].
glTarget
,
colorBufferTex
[
i
].
glName
,
0
);
}
pgl
.
validateFramebuffer
();
pg
.
popFramebuffer
();
}
public
int
getDefaultReadBuffer
() {
if
(
screenFb
) {
return
pgl
.
getDefaultReadBuffer
();
}
else
{
return
PGL
.
COLOR_ATTACHMENT0
;
}
}
public
int
getDefaultDrawBuffer
() {
if
(
screenFb
) {
return
pgl
.
getDefaultDrawBuffer
();
}
else
{
return
PGL
.
COLOR_ATTACHMENT0
;
}
}
///////////////////////////////////////////////////////////
// Allocate/release framebuffer.
protected
void
allocate
() {
dispose
();
// Just in the case this object is being re-allocated.
context
=
pgl
.
getCurrentContext
();
glres
=
new
GLResourceFrameBuffer
(
this
);
// create the FBO resources...
if
(
screenFb
) {
glFbo
=
0
;
}
else
{
if
(
multisample
) {
initColorBufferMultisample
();
}
if
(
packedDepthStencil
) {
initPackedDepthStencilBuffer
();
}
else
{
if
(
0
<
depthBits
) {
initDepthBuffer
();
}
if
(
0
<
stencilBits
) {
initStencilBuffer
();
}
}
}
}
protected
void
dispose
() {
if
(
screenFb
)
return
;
if
(
glres
!=
null
) {
glres
.
dispose
();
glFbo
=
0
;
glDepth
=
0
;
glStencil
=
0
;
glMultisample
=
0
;
glDepthStencil
=
0
;
glres
=
null
;
}
}
protected
boolean
contextIsOutdated
() {
if
(
screenFb
)
return
false
;
boolean
outdated
= !
pgl
.
contextIsCurrent
(
context
);
if
(
outdated
) {
dispose
();
for
(
int
i
=
0
;
i
<
numColorBuffers
;
i
++) {
colorBufferTex
[
i
] =
null
;
}
}
return
outdated
;
}
protected
void
initColorBufferMultisample
() {
if
(
screenFb
)
return
;
pg
.
pushFramebuffer
();
pg
.
setFramebuffer
(
this
);
pgl
.
bindRenderbuffer
(
PGL
.
RENDERBUFFER
,
glMultisample
);
pgl
.
renderbufferStorageMultisample
(
PGL
.
RENDERBUFFER
,
nsamples
,
PGL
.
RGBA8
,
width
,
height
);
pgl
.
framebufferRenderbuffer
(
PGL
.
FRAMEBUFFER
,
PGL
.
COLOR_ATTACHMENT0
,
PGL
.
RENDERBUFFER
,
glMultisample
);
pg
.
popFramebuffer
();
}
protected
void
initPackedDepthStencilBuffer
() {
if
(
screenFb
)
return
;
if
(
width
==
0
||
height
==
0
) {
throw
new
RuntimeException
(
"PFramebuffer: size undefined."
);
}
pg
.
pushFramebuffer
();
pg
.
setFramebuffer
(
this
);
pgl
.
bindRenderbuffer
(
PGL
.
RENDERBUFFER
,
glDepthStencil
);
if
(
multisample
) {
pgl
.
renderbufferStorageMultisample
(
PGL
.
RENDERBUFFER
,
nsamples
,
PGL
.
DEPTH24_STENCIL8
,
width
,
height
);
}
else
{
pgl
.
renderbufferStorage
(
PGL
.
RENDERBUFFER
,
PGL
.
DEPTH24_STENCIL8
,
width
,
height
);
}
pgl
.
framebufferRenderbuffer
(
PGL
.
FRAMEBUFFER
,
PGL
.
DEPTH_ATTACHMENT
,
PGL
.
RENDERBUFFER
,
glDepthStencil
);
pgl
.
framebufferRenderbuffer
(
PGL
.
FRAMEBUFFER
,
PGL
.
STENCIL_ATTACHMENT
,
PGL
.
RENDERBUFFER
,
glDepthStencil
);
pg
.
popFramebuffer
();
}
protected
void
initDepthBuffer
() {
if
(
screenFb
)
return
;
if
(
width
==
0
||
height
==
0
) {
throw
new
RuntimeException
(
"PFramebuffer: size undefined."
);
}
pg
.
pushFramebuffer
();
pg
.
setFramebuffer
(
this
);
pgl
.
bindRenderbuffer
(
PGL
.
RENDERBUFFER
,
glDepth
);
int
glConst
=
PGL
.
DEPTH_COMPONENT16
;
if
(
depthBits
==
16
) {
glConst
=
PGL
.
DEPTH_COMPONENT16
;
}
else
if
(
depthBits
==
24
) {
glConst
=
PGL
.
DEPTH_COMPONENT24
;
}
else
if
(
depthBits
==
32
) {
glConst
=
PGL
.
DEPTH_COMPONENT32
;
}
if
(
multisample
) {
pgl
.
renderbufferStorageMultisample
(
PGL
.
RENDERBUFFER
,
nsamples
,
glConst
,
width
,
height
);
}
else
{
pgl
.
renderbufferStorage
(
PGL
.
RENDERBUFFER
,
glConst
,
width
,
height
);
}
pgl
.
framebufferRenderbuffer
(
PGL
.
FRAMEBUFFER
,
PGL
.
DEPTH_ATTACHMENT
,
PGL
.
RENDERBUFFER
,
glDepth
);
pg
.
popFramebuffer
();
}
protected
void
initStencilBuffer
() {
if
(
screenFb
)
return
;
if
(
width
==
0
||
height
==
0
) {
throw
new
RuntimeException
(
"PFramebuffer: size undefined."
);
}
pg
.
pushFramebuffer
();
pg
.
setFramebuffer
(
this
);
pgl
.
bindRenderbuffer
(
PGL
.
RENDERBUFFER
,
glStencil
);
int
glConst
=
PGL
.
STENCIL_INDEX1
;
if
(
stencilBits
==
1
) {
glConst
=
PGL
.
STENCIL_INDEX1
;
}
else
if
(
stencilBits
==
4
) {
glConst
=
PGL
.
STENCIL_INDEX4
;
}
else
if
(
stencilBits
==
8
) {
glConst
=
PGL
.
STENCIL_INDEX8
;
}
if
(
multisample
) {
pgl
.
renderbufferStorageMultisample
(
PGL
.
RENDERBUFFER
,
nsamples
,
glConst
,
width
,
height
);
}
else
{
pgl
.
renderbufferStorage
(
PGL
.
RENDERBUFFER
,
glConst
,
width
,
height
);
}
pgl
.
framebufferRenderbuffer
(
PGL
.
FRAMEBUFFER
,
PGL
.
STENCIL_ATTACHMENT
,
PGL
.
RENDERBUFFER
,
glStencil
);
pg
.
popFramebuffer
();
}
protected
void
createPixelBuffer
() {
pixelBuffer
=
IntBuffer
.
allocate
(
width
*
height
);
pixelBuffer
.
rewind
();
}
}
Back
|
FazBrowse Home
|
New Git URL