JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
RedSquareMappedES2.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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.JoglVersion;
33import com.jogamp.opengl.util.GLArrayDataServer;
34import com.jogamp.opengl.util.PMVMatrix;
35import com.jogamp.opengl.util.TileRendererBase;
36import com.jogamp.opengl.util.glsl.ShaderCode;
37import com.jogamp.opengl.util.glsl.ShaderProgram;
38import com.jogamp.opengl.util.glsl.ShaderState;
39
40import com.jogamp.opengl.GL;
41import com.jogamp.opengl.GL2ES2;
42import com.jogamp.opengl.GLAutoDrawable;
43import com.jogamp.opengl.GLBufferStorage;
44import com.jogamp.opengl.GLEventListener;
45import com.jogamp.opengl.GLUniformData;
46import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
47
48public class RedSquareMappedES2 implements GLEventListener, TileRendererBase.TileRendererListener {
49 private ShaderState st;
50 private PMVMatrix pmvMatrix;
51 private GLUniformData pmvMatrixUniform;
52 private GLArrayDataServer vertices ;
53 private GLArrayDataServer colors ;
54 private long t0;
55 private int swapInterval = 0;
56 private float aspect = 1.0f;
57 private boolean doRotate = true;
58 private boolean clearBuffers = true;
59 private TileRendererBase tileRendererInUse = null;
60 private boolean doRotateBeforePrinting;
61
62 public RedSquareMappedES2(final int swapInterval) {
63 this.swapInterval = swapInterval;
64 }
65
67 this.swapInterval = 1;
68 }
69
70 @Override
72 tileRendererInUse = tr;
73 doRotateBeforePrinting = doRotate;
74 setDoRotation(false);
75 }
76 @Override
78 tileRendererInUse = null;
79 setDoRotation(doRotateBeforePrinting);
80 }
81 @Override
82 public void startTileRendering(final TileRendererBase tr) {
83 System.err.println("RedSquareES2.startTileRendering: "+tr);
84 }
85 @Override
86 public void endTileRendering(final TileRendererBase tr) {
87 System.err.println("RedSquareES2.endTileRendering: "+tr);
88 }
89
90 public void setAspect(final float aspect) { this.aspect = aspect; }
91 public void setDoRotation(final boolean rotate) { this.doRotate = rotate; }
92 public void setClearBuffers(final boolean v) { clearBuffers = v; }
93
94 @Override
95 public void init(final GLAutoDrawable glad) {
96 System.err.println(Thread.currentThread()+" RedSquareES2.init: tileRendererInUse "+tileRendererInUse);
97 final GL2ES2 gl = glad.getGL().getGL2ES2();
98
99 System.err.println("RedSquareES2 init on "+Thread.currentThread());
100 System.err.println("Chosen GLCapabilities: " + glad.getChosenGLCapabilities());
101 System.err.println("INIT GL IS: " + gl.getClass().getName());
102 System.err.println(JoglVersion.getGLStrings(gl, null, false).toString());
103 if( !gl.hasGLSL() ) {
104 System.err.println("No GLSL available, no rendering.");
105 return;
106 }
107 st = new ShaderState();
108 st.setVerbose(true);
109 final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader",
110 "shader/bin", "RedSquareShader", true);
111 final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader",
112 "shader/bin", "RedSquareShader", true);
113 vp0.defaultShaderCustomization(gl, true, true);
114 fp0.defaultShaderCustomization(gl, true, true);
115 final ShaderProgram sp0 = new ShaderProgram();
116 sp0.add(gl, vp0, System.err);
117 sp0.add(gl, fp0, System.err);
118 st.attachShaderProgram(gl, sp0, true);
119
120 // setup mgl_PMVMatrix
121 pmvMatrix = new PMVMatrix();
123 pmvMatrix.glLoadIdentity();
125 pmvMatrix.glLoadIdentity();
126 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv()); // P, Mv
127 st.ownUniform(pmvMatrixUniform);
128 st.uniform(gl, pmvMatrixUniform);
129
130 // Allocate Vertex Array
131 vertices = GLArrayDataServer.createGLSLMapped("mgl_Vertex", 3, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
132 {
133 final GLArrayDataServer ad = vertices;
134 final GLBufferStorage store = ad.mapStorage(gl, GL.GL_WRITE_ONLY);
135 {
136 final FloatBuffer fb = store.getMappedBuffer().asFloatBuffer();
137 fb.put(-2); fb.put( 2); fb.put( 0);
138 fb.put( 2); fb.put( 2); fb.put( 0);
139 fb.put(-2); fb.put(-2); fb.put( 0);
140 fb.put( 2); fb.put(-2); fb.put( 0);
141 }
142 ad.unmapStorage(gl);
143 }
144 vertices.seal(gl, true);
145 st.ownAttribute(vertices, true);
146 vertices.enableBuffer(gl, false);
147
148 // Allocate Color Array
149 colors = GLArrayDataServer.createGLSLMapped("mgl_Color", 4, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
150 {
151 final GLArrayDataServer ad = colors;
152 final GLBufferStorage store = ad.mapStorage(gl, GL.GL_WRITE_ONLY);
153 {
154 final FloatBuffer fb = store.getMappedBuffer().asFloatBuffer();
155 fb.put(1); fb.put(0); fb.put(0); fb.put(1);
156 fb.put(0); fb.put(0); fb.put(1); fb.put(1);
157 fb.put(1); fb.put(0); fb.put(0); fb.put(1);
158 fb.put(1); fb.put(0); fb.put(0); fb.put(1);
159 }
160 ad.unmapStorage(gl);
161 }
162 colors.seal(gl, true);
163 st.ownAttribute(colors, true);
164 colors.enableBuffer(gl, false);
165
166 // OpenGL Render Settings
168 st.useProgram(gl, false);
169
170 t0 = System.currentTimeMillis();
171 System.err.println(Thread.currentThread()+" RedSquareES2.init FIN");
172 }
173
174 @Override
175 public void display(final GLAutoDrawable glad) {
176 final long t1 = System.currentTimeMillis();
177
178 final GL2ES2 gl = glad.getGL().getGL2ES2();
179 if( clearBuffers ) {
180 if( null != tileRendererInUse ) {
181 gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
182 } else {
183 gl.glClearColor(0, 0, 0, 0);
184 }
186 }
187 if( !gl.hasGLSL() ) {
188 return;
189 }
190 st.useProgram(gl, true);
191 // One rotation every four seconds
193 pmvMatrix.glLoadIdentity();
194 pmvMatrix.glTranslatef(0, 0, -10);
195 if(doRotate) {
196 final float ang = ((t1 - t0) * 360.0F) / 4000.0F;
197 pmvMatrix.glRotatef(ang, 0, 0, 1);
198 pmvMatrix.glRotatef(ang, 0, 1, 0);
199 }
200 st.uniform(gl, pmvMatrixUniform);
201
202 // Draw a square
203 vertices.enableBuffer(gl, true);
204 colors.enableBuffer(gl, true);
206 vertices.enableBuffer(gl, false);
207 colors.enableBuffer(gl, false);
208 st.useProgram(gl, false);
209 }
210
211 @Override
212 public void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height) {
213 final GL2ES2 gl = glad.getGL().getGL2ES2();
214 gl.setSwapInterval(swapInterval);
215 reshapeImpl(gl, x, y, width, height, width, height);
216 }
217
218 @Override
219 public void reshapeTile(final TileRendererBase tr,
220 final int tileX, final int tileY, final int tileWidth, final int tileHeight,
221 final int imageWidth, final int imageHeight) {
222 final GL2ES2 gl = tr.getAttachedDrawable().getGL().getGL2ES2();
223 gl.setSwapInterval(0);
224 reshapeImpl(gl, tileX, tileY, tileWidth, tileHeight, imageWidth, imageHeight);
225 }
226
227 void reshapeImpl(final GL2ES2 gl, final int tileX, final int tileY, final int tileWidth, final int tileHeight, final int imageWidth, final int imageHeight) {
228 System.err.println(Thread.currentThread()+" RedSquareES2.reshape "+tileX+"/"+tileY+" "+tileWidth+"x"+tileHeight+" of "+imageWidth+"x"+imageHeight+", swapInterval "+swapInterval+", drawable 0x"+Long.toHexString(gl.getContext().getGLDrawable().getHandle())+", tileRendererInUse "+tileRendererInUse);
229 // Thread.dumpStack();
230 if( !gl.hasGLSL() ) {
231 return;
232 }
233
234 st.useProgram(gl, true);
235 // Set location in front of camera
236 pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
237 pmvMatrix.glLoadIdentity();
238
239 // compute projection parameters 'normal' perspective
240 final float fovy=45f;
241 final float aspect2 = ( (float) imageWidth / (float) imageHeight ) / aspect;
242 final float zNear=1f;
243 final float zFar=100f;
244
245 // compute projection parameters 'normal' frustum
246 final float top=(float)Math.tan(fovy*((float)Math.PI)/360.0f)*zNear;
247 final float bottom=-1.0f*top;
248 final float left=aspect2*bottom;
249 final float right=aspect2*top;
250 final float w = right - left;
251 final float h = top - bottom;
252
253 // compute projection parameters 'tiled'
254 final float l = left + tileX * w / imageWidth;
255 final float r = l + tileWidth * w / imageWidth;
256 final float b = bottom + tileY * h / imageHeight;
257 final float t = b + tileHeight * h / imageHeight;
258
259 pmvMatrix.glFrustumf(l, r, b, t, zNear, zFar);
260 //pmvMatrix.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f);
261 st.uniform(gl, pmvMatrixUniform);
262 st.useProgram(gl, false);
263
264 System.err.println(Thread.currentThread()+" RedSquareES2.reshape FIN");
265 }
266
267 @Override
268 public void dispose(final GLAutoDrawable glad) {
269 System.err.println(Thread.currentThread()+" RedSquareES2.dispose: tileRendererInUse "+tileRendererInUse);
270 final GL2ES2 gl = glad.getGL().getGL2ES2();
271 if( !gl.hasGLSL() ) {
272 return;
273 }
274 st.destroy(gl);
275 st = null;
276 pmvMatrix = null;
277 System.err.println(Thread.currentThread()+" RedSquareES2.dispose FIN");
278 }
279}
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
OpenGL buffer storage object reflecting it's.
final ByteBuffer getMappedBuffer()
Returns the mapped ByteBuffer, or null if not mapped.
abstract GLDrawable getGLDrawable()
Returns the write-drawable this context uses for framebuffer operations.
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
static StringBuilder getGLStrings(final GL gl, final StringBuilder sb)
void reshapeTile(final TileRendererBase tr, final int tileX, final int tileY, final int tileWidth, final int tileHeight, final int imageWidth, final int imageHeight)
Called by the TileRendererBase during tile-rendering via an attached GLAutoDrawable's GLAutoDrawable#...
void startTileRendering(final TileRendererBase tr)
Called by the TileRendererBase during tile-rendering after TileRendererBase#beginTile(GL) and before ...
void addTileRendererNotify(final TileRendererBase tr)
The owning GLAutoDrawable is attached to the given TileRendererBase instance.
void dispose(final GLAutoDrawable glad)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void init(final GLAutoDrawable glad)
Called by the drawable immediately after the OpenGL context is initialized.
void endTileRendering(final TileRendererBase tr)
Called by the TileRenderer during tile-rendering after TileRendererBase#endTile(GL) and GLAutoDrawabl...
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 display(final GLAutoDrawable glad)
Called by the drawable to initiate OpenGL rendering by the client.
void removeTileRendererNotify(final TileRendererBase tr)
The owning GLAutoDrawable is detached from the given TileRendererBase instance.
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 createGLSLMapped(final String name, final int compsPerElement, final int dataType, final boolean normalized, final int mappedElementCount, final int vboUsage)
Create a VBO, using a custom GLSL array attribute name intended for GPU buffer storage mapping,...
GLBufferStorage mapStorage(final GL gl, final int access)
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
Definition: PMVMatrix.java:62
final void glTranslatef(final float x, final float y, final float z)
Translate the current matrix.
Definition: PMVMatrix.java:379
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
Definition: PMVMatrix.java:218
final void glFrustumf(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Multiply the current matrix with the frustum matrix.
Definition: PMVMatrix.java:481
final void glRotatef(final float ang_deg, final float x, final float y, final float z)
Rotate the current matrix.
Definition: PMVMatrix.java:413
final void glLoadIdentity()
Load the current matrix with the identity matrix.
Definition: PMVMatrix.java:325
A fairly direct port of Brian Paul's tile rendering library, found at http://www.mesa3d....
final GLAutoDrawable getAttachedDrawable()
Returns a previously attached GLAutoDrawable, null if none is attached.
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.
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.
boolean hasGLSL()
Indicates whether this GL object supports GLSL.
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.
GLContext getContext()
Returns the GLContext associated which this GL object.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
long getHandle()
Returns the GL drawable handle, guaranteed to be valid after realization and while it's surface is be...
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
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_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 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_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_WRITE_ONLY
GL_VERSION_1_5, GL_ES_VERSION_3_1, GL_OES_mapbuffer, GL_ARB_vertex_buffer_object Alias for: GL_WRITE_...
Definition: GL.java:670
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.