JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
RedSquareES1.java
Go to the documentation of this file.
1package com.jogamp.opengl.test.junit.jogl.demos.es1;
2
3import com.jogamp.common.nio.Buffers;
4
5import java.nio.*;
6
7import com.jogamp.opengl.*;
8import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
9import com.jogamp.opengl.fixedfunc.GLPointerFunc;
10
11import com.jogamp.opengl.JoglVersion;
12import com.jogamp.opengl.util.TileRendererBase;
13import com.jogamp.opengl.util.glsl.fixedfunc.*;
14
15public class RedSquareES1 implements GLEventListener, TileRendererBase.TileRendererListener {
16
17 public static boolean oneThread = false;
18 public static boolean useAnimator = false;
19 private boolean debugFFPEmu = false;
20 private boolean verboseFFPEmu = false;
21 private boolean traceFFPEmu = false;
22 private boolean forceFFPEmu = false;
23 private boolean debug = false ;
24 private boolean trace = false ;
25 private int swapInterval = 0;
26 private final float aspect = 1.0f;
27 private boolean doRotate = true;
28 private TileRendererBase tileRendererInUse = null;
29 private boolean doRotateBeforePrinting;
30 private boolean flipVerticalInGLOrientation = false;
31
32 long startTime = 0;
33 long curTime = 0;
34
35 public RedSquareES1(final int swapInterval) {
36 this.swapInterval = swapInterval;
37 }
38
39 public RedSquareES1() {
40 this.swapInterval = 1;
41 }
42
43 @Override
45 tileRendererInUse = tr;
46 doRotateBeforePrinting = doRotate;
47 setDoRotation(false);
48 }
49 @Override
51 tileRendererInUse = null;
52 setDoRotation(doRotateBeforePrinting);
53 }
54 @Override
55 public void startTileRendering(final TileRendererBase tr) {
56 System.err.println("RedSquareES1.startTileRendering: "+tr);
57 }
58 @Override
59 public void endTileRendering(final TileRendererBase tr) {
60 System.err.println("RedSquareES1.endTileRendering: "+tr);
61 }
62
63 public void setDoRotation(final boolean rotate) { this.doRotate = rotate; }
64 public void setForceFFPEmu(final boolean forceFFPEmu, final boolean verboseFFPEmu, final boolean debugFFPEmu, final boolean traceFFPEmu) {
65 this.forceFFPEmu = forceFFPEmu;
66 this.verboseFFPEmu = verboseFFPEmu;
67 this.debugFFPEmu = debugFFPEmu;
68 this.traceFFPEmu = traceFFPEmu;
69 }
70 public void setFlipVerticalInGLOrientation(final boolean v) { flipVerticalInGLOrientation=v; }
71
72 // FIXME: we must add storage of the pointers in the GL state to
73 // the GLImpl classes. The need for this can be seen by making
74 // these variables method local instead of instance members. The
75 // square will disappear after a second or so due to garbage
76 // collection. On desktop OpenGL this implies a stack of
77 // references due to the existence of glPush/PopClientAttrib. On
78 // OpenGL ES 1/2 it can simply be one set of references.
79 private FloatBuffer colors;
80 private FloatBuffer vertices;
81
82 @Override
83 public void init(final GLAutoDrawable drawable) {
84 System.err.println(Thread.currentThread()+" RedSquareES1.init ...");
85 GL _gl = drawable.getGL();
86
87 if(debugFFPEmu) {
88 // Debug ..
89 _gl = _gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Debug", GL2ES2.class, _gl, null) );
90 debug = false;
91 }
92 if(traceFFPEmu) {
93 // Trace ..
94 _gl = _gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Trace", GL2ES2.class, _gl, new Object[] { System.err } ) );
95 trace = false;
96 }
97 GL2ES1 gl = FixedFuncUtil.wrapFixedFuncEmul(_gl, ShaderSelectionMode.AUTO, null, forceFFPEmu, verboseFFPEmu);
98
99 if(debug) {
100 try {
101 // Debug ..
102 gl = (GL2ES1) gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Debug", GL2ES1.class, gl, null) );
103 } catch (final Exception e) {e.printStackTrace();}
104 }
105 if(trace) {
106 try {
107 // Trace ..
108 gl = (GL2ES1) gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Trace", GL2ES1.class, gl, new Object[] { System.err } ) );
109 } catch (final Exception e) {e.printStackTrace();}
110 }
111
112 System.err.println("RedSquareES1 init on "+Thread.currentThread());
113 System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
114 System.err.println("INIT GL IS: " + gl.getClass().getName());
115 System.err.println(JoglVersion.getGLStrings(gl, null, false).toString());
116
117 // Allocate vertex arrays
118 colors = Buffers.newDirectFloatBuffer(16);
119 vertices = Buffers.newDirectFloatBuffer(12);
120 // Fill them up
121 colors.put( 0, 1); colors.put( 1, 0); colors.put( 2, 0); colors.put( 3, 1);
122 colors.put( 4, 0); colors.put( 5, 0); colors.put( 6, 1); colors.put( 7, 1);
123 colors.put( 8, 1); colors.put( 9, 0); colors.put(10, 0); colors.put(11, 1);
124 colors.put(12, 1); colors.put(13, 0); colors.put(14, 0); colors.put(15, 1);
125 vertices.put(0, -2); vertices.put( 1, 2); vertices.put( 2, 0);
126 vertices.put(3, 2); vertices.put( 4, 2); vertices.put( 5, 0);
127 vertices.put(6, -2); vertices.put( 7, -2); vertices.put( 8, 0);
128 vertices.put(9, 2); vertices.put(10, -2); vertices.put(11, 0);
129
130 gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices);
131 gl.glColorPointer(4, GL.GL_FLOAT, 0, colors);
132
133 // OpenGL Render Settings
135
136 startTime = System.currentTimeMillis();
137 curTime = startTime;
138 System.err.println(Thread.currentThread()+" RedSquareES1.init FIN");
139 }
140
141 @Override
142 public void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height) {
143 final GL2ES1 gl = glad.getGL().getGL2ES1();
144 gl.setSwapInterval(swapInterval);
145 reshapeImpl(gl, x, y, width, height, width, height);
146 }
147
148 @Override
149 public void reshapeTile(final TileRendererBase tr,
150 final int tileX, final int tileY, final int tileWidth, final int tileHeight,
151 final int imageWidth, final int imageHeight) {
152 final GL2ES1 gl = tr.getAttachedDrawable().getGL().getGL2ES1();
153 gl.setSwapInterval(0);
154 reshapeImpl(gl, tileX, tileY, tileWidth, tileHeight, imageWidth, imageHeight);
155 }
156
157 void reshapeImpl(final GL2ES1 gl, final int tileX, final int tileY, final int tileWidth, final int tileHeight, final int imageWidth, final int imageHeight) {
158 System.err.println(Thread.currentThread()+" RedSquareES1.reshape "+tileX+"/"+tileY+" "+tileWidth+"x"+tileHeight+" of "+imageWidth+"x"+imageHeight+", swapInterval "+swapInterval+", drawable 0x"+Long.toHexString(gl.getContext().getGLDrawable().getHandle())+", tileRendererInUse "+tileRendererInUse);
159
160 // Set location in front of camera
162 gl.glLoadIdentity();
163 if( flipVerticalInGLOrientation && gl.getContext().getGLDrawable().isGLOriented() ) {
164 gl.glScalef(1f, -1f, 1f);
165 }
166
167 // compute projection parameters 'normal' perspective
168 final float fovy=45f;
169 final float aspect2 = ( (float) imageWidth / (float) imageHeight ) / aspect;
170 final float zNear=1f;
171 final float zFar=100f;
172
173 // compute projection parameters 'normal' frustum
174 final float top=(float)Math.tan(fovy*((float)Math.PI)/360.0f)*zNear;
175 final float bottom=-1.0f*top;
176 final float left=aspect2*bottom;
177 final float right=aspect2*top;
178 final float w = right - left;
179 final float h = top - bottom;
180
181 // compute projection parameters 'tiled'
182 final float l = left + tileX * w / imageWidth;
183 final float r = l + tileWidth * w / imageWidth;
184 final float b = bottom + tileY * h / imageHeight;
185 final float t = b + tileHeight * h / imageHeight;
186
187 gl.glFrustumf(l, r, b, t, zNear, zFar);
188 // gl.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f);
189
190 gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
191 gl.glLoadIdentity();
192
193 System.err.println(Thread.currentThread()+" RedSquareES1.reshape FIN");
194 }
195
196 @Override
197 public void display(final GLAutoDrawable drawable) {
198 curTime = System.currentTimeMillis();
199 final GL2ES1 gl = drawable.getGL().getGL2ES1();
200 if( null != tileRendererInUse ) {
201 gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
202 } else {
203 gl.glClearColor(0, 0, 0, 0);
204 }
206
207 // One rotation every four seconds
209 gl.glLoadIdentity();
210 gl.glTranslatef(0, 0, -10);
211 final float ang = doRotate ? ((curTime - startTime) * 360.0f) / 4000.0f : 1f;
212 gl.glRotatef(ang, 0, 0, 1);
213 gl.glRotatef(ang, 0, 1, 0);
214
215 // Draw a square
221 }
222
223 @Override
224 public void dispose(final GLAutoDrawable drawable) {
225 System.err.println(Thread.currentThread()+" RedSquareES1.dispose ... ");
226 final GL2ES1 gl = drawable.getGL().getGL2ES1();
229 colors.clear();
230 colors = null;
231 vertices.clear();
232 vertices = null;
233 System.err.println(Thread.currentThread()+" RedSquareES1.dispose FIN");
234 }
235}
abstract GLDrawable getGLDrawable()
Returns the write-drawable this context uses for framebuffer operations.
abstract GL setGL(GL gl)
Sets the GL pipeline object for this GLContext.
Factory for pipelining GL instances.
static final GL create(final String pipelineClazzBaseName, final Class<?> reqInterface, final GL downstream, final Object[] additionalArgs)
Creates a pipelined GL instance using the given downstream downstream and optional arguments addition...
static StringBuilder getGLStrings(final GL gl, final StringBuilder sb)
void addTileRendererNotify(final TileRendererBase tr)
The owning GLAutoDrawable is attached to the given TileRendererBase instance.
void setForceFFPEmu(final boolean forceFFPEmu, final boolean verboseFFPEmu, final boolean debugFFPEmu, final boolean traceFFPEmu)
void startTileRendering(final TileRendererBase tr)
Called by the TileRendererBase during tile-rendering after TileRendererBase#beginTile(GL) and before ...
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void removeTileRendererNotify(final TileRendererBase tr)
The owning GLAutoDrawable is detached from the given TileRendererBase instance.
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 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.
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 endTileRendering(final TileRendererBase tr)
Called by the TileRenderer during tile-rendering after TileRendererBase#endTile(GL) and GLAutoDrawabl...
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.
Tool to pipeline GL2ES2 into a fixed function emulation implementing GL2ES1.
static final GL2ES1 wrapFixedFuncEmul(final GL gl, final ShaderSelectionMode mode, final PMVMatrix pmvMatrix, final boolean force, final boolean verbose)
AUTO
Auto shader selection, based upon FFP states.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL2ES1 getGL2ES1()
Casts this object to the GL2ES1 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.
long getHandle()
Returns the GL drawable handle, guaranteed to be valid after realization and while it's surface is be...
boolean isGLOriented()
Returns true if the drawable is rendered in OpenGL's coordinate system, origin at bottom left.
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_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
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
void glFrustumf(float left, float right, float bottom, float top, float zNear, float zFar)
Multiply the current matrix with the frustum matrix.
void glTranslatef(float x, float y, float z)
Translate the current matrix.
void glRotatef(float angle, float x, float y, float z)
Rotate the current matrix.
static final int GL_MODELVIEW
Matrix mode modelview.
void glScalef(float x, float y, float z)
Scale the current matrix.
void glLoadIdentity()
Load the current matrix with the identity matrix.
void glMatrixMode(int mode)
Sets the current matrix mode.