JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
FBOMix2DemosES2.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.opengl.test.junit.jogl.demos.es2;
29
30import java.nio.FloatBuffer;
31
32import com.jogamp.opengl.GL;
33import com.jogamp.opengl.GL2ES2;
34import com.jogamp.opengl.GLAutoDrawable;
35import com.jogamp.opengl.GLEventListener;
36import com.jogamp.opengl.GLUniformData;
37import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
38
39import com.jogamp.opengl.FBObject;
40import com.jogamp.opengl.FBObject.TextureAttachment;
41import com.jogamp.opengl.FBObject.Attachment.Type;
42import com.jogamp.opengl.util.GLArrayDataServer;
43import com.jogamp.opengl.util.PMVMatrix;
44import com.jogamp.opengl.util.glsl.ShaderCode;
45import com.jogamp.opengl.util.glsl.ShaderProgram;
46import com.jogamp.opengl.util.glsl.ShaderState;
47
48public class FBOMix2DemosES2 implements GLEventListener {
49 private final GearsES2 demo0;
50 private final RedSquareES2 demo1;
51 private final int swapInterval;
52 private volatile int numSamples;
53 private boolean demo0Only;
54
55
56 private final ShaderState st;
57 private final PMVMatrix pmvMatrix;
58
59 private final FBObject fbo0;
60 private final FBObject fbo1;
61
62 private TextureAttachment fbo0Tex;
63 private TextureAttachment fbo1Tex;
64
65 private ShaderProgram sp0;
66 private GLUniformData pmvMatrixUniform;
67 private GLArrayDataServer interleavedVBO;
68 private GLUniformData texUnit0;
69 private GLUniformData texUnit1;
70
71 public FBOMix2DemosES2(final int swapInterval) {
72 demo0 = new GearsES2(-1);
73 demo0.setIgnoreFocus(true);
74 demo1 = new RedSquareES2(-1);
75 this.swapInterval = swapInterval;
76
77 st = new ShaderState();
78 // st.setVerbose(true);
79 pmvMatrix = new PMVMatrix();
80
81 fbo0 = new FBObject();
82 fbo1 = new FBObject();
83
84 numSamples = 0;
85 demo0Only = false;
86 }
87
88 public void setDemo0Only(final boolean v) {
89 this.demo0Only = v;
90 }
91 public boolean getDemo0Only() { return demo0Only; }
92
93 public void setMSAA(final int numSamples) {
94 this.numSamples=numSamples;
95 }
96 public int getMSAA() { return numSamples; }
97
98 public void setDoRotation(final boolean rotate) { demo1.setDoRotation(rotate); }
99
100 @Override
101 public void init(final GLAutoDrawable drawable) {
102 final GL2ES2 gl = drawable.getGL().getGL2ES2();
103
104 demo0.init(drawable);
105 demo1.init(drawable);
106
107 final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, FBOMix2DemosES2.class, "shader",
108 "shader/bin", "texture01_xxx", true);
109 final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, FBOMix2DemosES2.class, "shader",
110 "shader/bin", "texture02_xxx", true);
111 vp0.defaultShaderCustomization(gl, true, true);
112 fp0.defaultShaderCustomization(gl, true, true);
113
114 sp0 = new ShaderProgram();
115 sp0.add(gl, vp0, System.err);
116 sp0.add(gl, fp0, System.err);
117 st.attachShaderProgram(gl, sp0, true);
118
119 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv());
120 st.ownUniform(pmvMatrixUniform);
121 st.uniform(gl, pmvMatrixUniform);
122
123 interleavedVBO = GLArrayDataServer.createGLSLInterleaved(3+4+2, GL.GL_FLOAT, false, 3*4, GL.GL_STATIC_DRAW);
124 {
125 interleavedVBO.addGLSLSubArray("mgl_Vertex", 3, GL.GL_ARRAY_BUFFER);
126 interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER);
127 //interleavedVBO.addGLSLSubArray("mgl_Normal", 3, GL.GL_ARRAY_BUFFER);
128 interleavedVBO.addGLSLSubArray("mgl_MultiTexCoord", 2, GL.GL_ARRAY_BUFFER);
129
130 final FloatBuffer ib = (FloatBuffer)interleavedVBO.getBuffer();
131
132 for(int i=0; i<4; i++) {
133 ib.put(s_quadVertices, i*3, 3);
134 ib.put(s_quadColors, i*4, 4);
135 //ib.put(s_cubeNormals, i*3, 3);
136 ib.put(s_quadTexCoords, i*2, 2);
137 }
138 }
139 interleavedVBO.seal(gl, true);
140 interleavedVBO.enableBuffer(gl, false);
141 st.ownAttribute(interleavedVBO, true);
142
143 texUnit0 = new GLUniformData("mgl_Texture0", 0);
144 st.ownUniform(texUnit0);
145 st.uniform(gl, texUnit0);
146 texUnit1 = new GLUniformData("mgl_Texture1", 1);
147 st.ownUniform(texUnit1);
148 st.uniform(gl, texUnit1);
149
150 st.useProgram(gl, false);
151
152 System.err.println("**** Init");
153 initFBOs(gl, drawable);
154
156 }
157
158 private void initFBOs(final GL gl, final GLAutoDrawable drawable) {
159 fbo0.init(gl, drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), numSamples);
160 fbo1.init(gl, drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), numSamples);
161 if(fbo0.getNumSamples() != fbo1.getNumSamples()) {
162 throw new InternalError("sample size mismatch: \n\t0: "+fbo0+"\n\t1: "+fbo1);
163 }
164 numSamples = fbo0.getNumSamples();
165
166 if(numSamples>0) {
167 fbo0.attachColorbuffer(gl, 0, true);
168 fbo0.resetSamplingSink(gl);
169 fbo1.attachColorbuffer(gl, 0, true);
170 fbo1.resetSamplingSink(gl);
171 fbo0Tex = fbo0.getSamplingSink().getTextureAttachment();
172 fbo1Tex = fbo1.getSamplingSink().getTextureAttachment();
173 } else {
174 fbo0Tex = fbo0.attachTexture2D(gl, 0, true);
175 fbo1Tex = fbo1.attachTexture2D(gl, 0, true);
176 }
177 numSamples=fbo0.getNumSamples();
179 fbo0.unbind(gl);
181 fbo1.unbind(gl);
182 }
183
184 private void resetFBOs(final GL gl, final GLAutoDrawable drawable) {
185 fbo0.reset(gl, drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), numSamples);
186 fbo1.reset(gl, drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), numSamples);
187 if(fbo0.getNumSamples() != fbo1.getNumSamples()) {
188 throw new InternalError("sample size mismatch: \n\t0: "+fbo0+"\n\t1: "+fbo1);
189 }
190 numSamples = fbo0.getNumSamples();
191 if(numSamples>0) {
192 fbo0Tex = fbo0.getSamplingSink().getTextureAttachment();
193 fbo1Tex = fbo1.getSamplingSink().getTextureAttachment();
194 } else {
195 fbo0Tex = fbo0.getColorbuffer(0).getTextureAttachment();
196 fbo1Tex = fbo1.getColorbuffer(0).getTextureAttachment();
197 }
198 }
199
200 @Override
201 public void dispose(final GLAutoDrawable drawable) {
202 final GL2ES2 gl = drawable.getGL().getGL2ES2();
203 demo0.dispose(drawable);
204 demo1.dispose(drawable);
205 fbo0.destroy(gl);
206 fbo1.destroy(gl);
207 st.destroy(gl);
208
209 fbo0Tex = null;
210 fbo1Tex = null;
211 sp0 = null;
212 pmvMatrixUniform = null;
213 interleavedVBO = null;
214 }
215
216 @Override
217 public void display(final GLAutoDrawable drawable) {
218 final GL2ES2 gl = drawable.getGL().getGL2ES2();
219
220 if( fbo0.getNumSamples() != numSamples ) {
221 System.err.println("**** NumSamples: "+fbo0.getNumSamples()+" -> "+numSamples);
222 resetFBOs(gl, drawable);
223 }
224
225 if(0 < numSamples) {
227 }
228
229 fbo0.bind(gl);
230 demo0.display(drawable);
231 fbo0.unbind(gl);
232
233 if(!demo0Only) {
234 fbo1.bind(gl);
235 demo1.display(drawable);
236 fbo1.unbind(gl);
237 }
238
239 st.useProgram(gl, true);
240 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
242
243 gl.glActiveTexture(GL.GL_TEXTURE0 + texUnit0.intValue());
244 fbo0.use(gl, fbo0Tex);
245 if(!demo0Only) {
246 gl.glActiveTexture(GL.GL_TEXTURE0 + texUnit1.intValue());
247 fbo1.use(gl, fbo1Tex);
248 }
249 interleavedVBO.enableBuffer(gl, true);
250
251 if( !gl.isGLcore() ) {
253 }
254
256
257 interleavedVBO.enableBuffer(gl, false);
258 fbo0.unuse(gl);
259 if(!demo0Only) {
260 fbo1.unuse(gl);
261 }
262
263 st.useProgram(gl, false);
264 }
265
266 @Override
267 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
268 final GL2ES2 gl = drawable.getGL().getGL2ES2();
269
270 gl.setSwapInterval(swapInterval); // in case switching the drawable (impl. may bound attribute there)
271
272 System.err.println("**** Reshape: "+width+"x"+height);
273 resetFBOs(gl, drawable);
274
275 fbo0.bind(gl);
276 demo0.reshape(drawable, x, y, width, height);
277 fbo0.unbind(gl);
278 fbo1.bind(gl);
279 demo1.reshape(drawable, x, y, width, height);
280 fbo1.unbind(gl);
281
283 pmvMatrix.glLoadIdentity();
284 pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 10.0f);
285
287 pmvMatrix.glLoadIdentity();
288
289 st.useProgram(gl, true);
290 st.uniform(gl, pmvMatrixUniform);
291 st.useProgram(gl, false);
292
293 }
294
295 private static final float[] s_quadVertices = {
296 -1f, -1f, 0f, // LB
297 1f, -1f, 0f, // RB
298 -1f, 1f, 0f, // LT
299 1f, 1f, 0f // RT
300 };
301 private static final float[] s_quadColors = {
302 1f, 1f, 1f, 1f,
303 1f, 1f, 1f, 1f,
304 1f, 1f, 1f, 1f,
305 1f, 1f, 1f, 1f };
306 private static final float[] s_quadTexCoords = {
307 0f, 0f, // LB
308 1f, 0f, // RB
309 0f, 1f, // LT
310 1f, 1f // RT
311 };
312}
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
Core utility class simplifying usage of framebuffer objects (FBO) with all GLProfiles.
Definition: FBObject.java:53
final void attachRenderbuffer(final GL gl, final Attachment.Type atype, final int reqBits)
Attaches one depth, stencil or packed-depth-stencil buffer to this FBO's instance,...
Definition: FBObject.java:1691
final void bind(final GL gl)
Bind this FBO, i.e.
Definition: FBObject.java:2540
final TextureAttachment attachTexture2D(final GL gl, final int attachmentPoint, final boolean alpha)
Attaches a Colorbuffer, i.e.
Definition: FBObject.java:1387
final int getNumSamples()
Returns the number of samples for multisampling (MSAA).
Definition: FBObject.java:2757
final void destroy(final GL gl)
Definition: FBObject.java:2253
final void unuse(final GL gl)
Unbind texture, ie bind 'non' texture 0.
Definition: FBObject.java:2684
final void use(final GL gl, final TextureAttachment ta)
Synchronize the sampling sink and bind the given TextureAttachment, if not null.
Definition: FBObject.java:2672
final Colorbuffer getSamplingSink()
Return the multisampling Colorbuffer sink, if using multisampling.
Definition: FBObject.java:2782
final boolean resetSamplingSink(final GL gl)
Manually validates the MSAA sampling sink, if used.
Definition: FBObject.java:2345
final ColorAttachment attachColorbuffer(final GL gl, final int attachmentPoint, final boolean alpha)
Attaches a newly created and initialized Colorbuffer, i.e.
Definition: FBObject.java:1496
final void unbind(final GL gl)
Unbind this FBO, i.e.
Definition: FBObject.java:2563
final boolean reset(final GL gl, int newWidth, int newHeight, int newSamples)
Resets this FBO's instance.
Definition: FBObject.java:1140
static final int CHOSEN_BITS
Request current context drawable's chosen depth- or stencil-bits; value {@value}.
Definition: FBObject.java:1658
void init(final GL gl, final int newWidth, final int newHeight, final int newSamples)
Initializes this FBO's instance.
Definition: FBObject.java:1008
final Colorbuffer getColorbuffer(final int attachmentPoint)
Return the Colorbuffer attachment at attachmentPoint if it is attached to this FBO,...
Definition: FBObject.java:886
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
Definition: GearsES2.java:198
void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
Definition: GearsES2.java:326
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
Definition: GearsES2.java:468
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
Definition: GearsES2.java:507
void display(final GLAutoDrawable glad)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable glad)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void init(final GLAutoDrawable glad)
Called by the drawable immediately after the OpenGL context is initialized.
void seal(final GL gl, final boolean seal)
Convenience method calling seal(boolean) and enableBuffer(GL, boolean).
void enableBuffer(final GL gl, final boolean enable)
Enables the buffer if enable is true, and transfers the data if required.
static GLArrayDataServer createGLSLInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO for GLSL interleaved array data starting with a new created Buffer object with initialEl...
GLArrayDataWrapper addGLSLSubArray(final String name, final int comps, final int vboTarget)
Configure a segment of this GLSL interleaved array (see createGLSLInterleaved(int,...
Buffer getBuffer()
The Buffer holding the data, may be null if a GPU buffer without client bound data.
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
Definition: PMVMatrix.java:62
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
Definition: PMVMatrix.java:218
final void glOrthof(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Multiply the current matrix with the orthogonal matrix.
Definition: PMVMatrix.java:469
final void glLoadIdentity()
Load the current matrix with the identity matrix.
Definition: PMVMatrix.java:325
Convenient shader code class to use and instantiate vertex or fragment programs.
Definition: ShaderCode.java:75
final int defaultShaderCustomization(final GL2ES2 gl, final boolean preludeVersion, final boolean addDefaultPrecision)
Default customization of this shader source code.
static ShaderCode create(final GL2ES2 gl, final int type, final int count, final Class<?> context, final String[] sourceFiles, final boolean mutableStringBuilder)
Creates a complete ShaderCode object while reading all shader source of sourceFiles,...
synchronized void add(final ShaderCode shaderCode)
Adds a new shader to this program.
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
void ownAttribute(final GLArrayData attribute, final boolean own)
Binds or unbinds the GLArrayData lifecycle to this ShaderState.
synchronized void useProgram(final GL2ES2 gl, final boolean on)
Turns the shader program on or off.
synchronized boolean attachShaderProgram(final GL2ES2 gl, final ShaderProgram prog, final boolean enable)
Attach or switch a shader program.
synchronized void destroy(final GL2ES2 gl)
Calls release(gl, true, true, true).
boolean uniform(final GL2ES2 gl, final GLUniformData data)
Set the uniform data, if it's location is valid, i.e.
void ownUniform(final GLUniformData uniform)
Bind the GLUniform lifecycle to this ShaderState.
TextureAttachment getTextureAttachment()
Casts this object to a TextureAttachment reference, see isTextureAttachment().
static final int GL_VERTEX_SHADER
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_EXT_vertex_shader, GL_ARB_vertex_shader Alias for: GL_VERTEX_SH...
Definition: GL2ES2.java:39
static final int GL_FRAGMENT_SHADER
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_ATI_fragment_shader, GL_ARB_fragment_shader Alias for: GL_FRAGM...
Definition: GL2ES2.java:541
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
boolean isGLcore()
Indicates whether this GL object uses a GL core profile.
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
static final int GL_MULTISAMPLE
Common in ES1, GL2 and GL3.
Definition: GL.java:1249
void glDrawArrays(int mode, int first, int count)
Entry point to C language function: void {@native glDrawArrays}(GLenum mode, GLint first,...
static final int GL_STATIC_DRAW
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_STATI...
Definition: GL.java:673
static final int GL_TEXTURE_2D
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_2D" with expr...
Definition: GL.java:491
static final int GL_FLOAT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FLOAT" with expressio...
Definition: GL.java:786
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
static final int GL_DEPTH_TEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expr...
Definition: GL.java:43
void glActiveTexture(int texture)
Entry point to C language function: void {@native glActiveTexture}(GLenum texture) Part of GL_ES_V...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
static final int GL_TRIANGLE_STRIP
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLE_STRIP" with ...
Definition: GL.java:760
static final int GL_TEXTURE0
GL_ES_VERSION_2_0, GL_VERSION_1_3, GL_VERSION_ES_1_0, GL_ARB_multitexture Alias for: GL_TEXTURE0_ARB ...
Definition: GL.java:71
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738
static final int GL_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
Definition: GL.java:633
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW
Matrix mode modelview.