JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
GearsES1.java
Go to the documentation of this file.
1/**
2 * Copyright (C) 2011 JogAmp Community. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22package com.jogamp.opengl.test.junit.jogl.demos.es1;
23
24import java.nio.FloatBuffer;
25
26import com.jogamp.nativewindow.NativeWindow;
27import com.jogamp.opengl.GL;
28import com.jogamp.opengl.GL2ES1;
29import com.jogamp.opengl.GL2ES2;
30import com.jogamp.opengl.GLAutoDrawable;
31import com.jogamp.opengl.GLEventListener;
32import com.jogamp.opengl.GLPipelineFactory;
33import com.jogamp.opengl.GLProfile;
34import com.jogamp.opengl.fixedfunc.GLLightingFunc;
35import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
36
37import com.jogamp.newt.Window;
38import com.jogamp.newt.event.KeyAdapter;
39import com.jogamp.newt.event.KeyEvent;
40import com.jogamp.newt.event.KeyListener;
41import com.jogamp.newt.event.MouseAdapter;
42import com.jogamp.newt.event.MouseEvent;
43import com.jogamp.newt.event.MouseListener;
44import com.jogamp.opengl.GLRendererQuirks;
45import com.jogamp.opengl.JoglVersion;
46import com.jogamp.opengl.test.junit.jogl.demos.GearsObject;
47import com.jogamp.opengl.util.glsl.fixedfunc.FixedFuncUtil;
48import com.jogamp.opengl.util.glsl.fixedfunc.ShaderSelectionMode;
49
50/**
51 * GearsES1.java <BR>
52 * @author Brian Paul (converted to Java by Ron Cemer and Sven Gothel) <P>
53 */
54public class GearsES1 implements GLEventListener {
55 private boolean debugFFPEmu = false;
56 private boolean verboseFFPEmu = false;
57 private boolean traceFFPEmu = false;
58 private boolean forceFFPEmu = false;
59 private boolean debug = false ;
60 private boolean trace = false ;
61
62 private final float pos[] = { 5.0f, 5.0f, 10.0f, 0.0f };
63
64 private float view_rotx = 20.0f, view_roty = 30.0f;
65 private final float view_rotz = 0.0f;
66 private GearsObject gear1=null, gear2=null, gear3=null;
67 private FloatBuffer gear1Color=GearsObject.red, gear2Color=GearsObject.green, gear3Color=GearsObject.blue;
68 private GearsES1 sharedGears;
69 private Object syncObjects;
70 private volatile boolean usesSharedGears = false;
71 private boolean useMappedBuffers = false;
72 private boolean validateBuffers = false;
73 private float angle = 0.0f;
74 private final int swapInterval;
75 private final MouseListener gearsMouse = new GearsMouseAdapter();
76 private final KeyListener gearsKeys = new GearsKeyAdapter();
77 private volatile boolean isInit = false;
78
79
80 private int prevMouseX, prevMouseY;
81
82 public GearsES1(final int swapInterval) {
83 this.swapInterval = swapInterval;
84 }
85
86 public GearsES1() {
87 this.swapInterval = 1;
88 }
89
90 public void setForceFFPEmu(final boolean forceFFPEmu, final boolean verboseFFPEmu, final boolean debugFFPEmu, final boolean traceFFPEmu) {
91 this.forceFFPEmu = forceFFPEmu;
92 this.verboseFFPEmu = verboseFFPEmu;
93 this.debugFFPEmu = debugFFPEmu;
94 this.traceFFPEmu = traceFFPEmu;
95 }
96
97 public void setGearsColors(final FloatBuffer gear1Color, final FloatBuffer gear2Color, final FloatBuffer gear3Color) {
98 this.gear1Color = gear1Color;
99 this.gear2Color = gear2Color;
100 this.gear3Color = gear3Color;
101 }
102
103 public void setSharedGears(final GearsES1 shared) {
104 sharedGears = shared;
105 }
106
107 /**
108 * @return gear1
109 */
110 public GearsObject getGear1() { return gear1; }
111
112 /**
113 * @return gear2
114 */
115 public GearsObject getGear2() { return gear2; }
116
117 /**
118 * @return gear3
119 */
120 public GearsObject getGear3() { return gear3; }
121
122 public boolean usesSharedGears() { return usesSharedGears; }
123
124 public void setUseMappedBuffers(final boolean v) { useMappedBuffers = v; }
125 public void setValidateBuffers(final boolean v) { validateBuffers = v; }
126
127 public void init(final GLAutoDrawable drawable) {
128 if(null != sharedGears && !sharedGears.isInit() ) {
129 System.err.println(Thread.currentThread()+" GearsES1.init.0: pending shared Gears .. re-init later XXXXX");
130 drawable.setGLEventListenerInitState(this, false);
131 return;
132 }
133 System.err.println(Thread.currentThread()+" GearsES1.init ...");
134
135 // Use debug pipeline
136 // drawable.setGL(new DebugGL(drawable.getGL()));
137
138 GL _gl = drawable.getGL();
139
140 if(debugFFPEmu) {
141 // Debug ..
142 _gl = _gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Debug", GL2ES2.class, _gl, null) );
143 debug = false;
144 }
145 if(traceFFPEmu) {
146 // Trace ..
147 _gl = _gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Trace", GL2ES2.class, _gl, new Object[] { System.err } ) );
148 trace = false;
149 }
150 GL2ES1 gl = FixedFuncUtil.wrapFixedFuncEmul(_gl, ShaderSelectionMode.AUTO, null, forceFFPEmu, verboseFFPEmu);
151
152 if(debug) {
153 try {
154 // Debug ..
155 gl = (GL2ES1) gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Debug", GL2ES1.class, gl, null) );
156 } catch (final Exception e) {e.printStackTrace();}
157 }
158 if(trace) {
159 try {
160 // Trace ..
161 gl = (GL2ES1) gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Trace", GL2ES1.class, gl, new Object[] { System.err } ) );
162 } catch (final Exception e) {e.printStackTrace();}
163 }
164
165 System.err.println("GearsES1 init on "+Thread.currentThread());
166 System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
167 System.err.println("INIT GL IS: " + gl.getClass().getName());
168 System.err.println(JoglVersion.getGLStrings(gl, null, false).toString());
169
170 gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, pos, 0);
172 gl.glEnable(GLLightingFunc.GL_LIGHTING);
173 gl.glEnable(GLLightingFunc.GL_LIGHT0);
174 gl.glEnable(GL.GL_DEPTH_TEST);
175
176 /* make the gears */
177 if( null != sharedGears ) {
178 gear1 = new GearsObjectES1(sharedGears.getGear1());
179 gear2 = new GearsObjectES1(sharedGears.getGear2());
180 gear3 = new GearsObjectES1(sharedGears.getGear3());
181 usesSharedGears = true;
182 System.err.println("gear1 reuse: "+gear1);
183 System.err.println("gear2 reuse: "+gear2);
184 System.err.println("gear3 reuse: "+gear3);
185 if( gl.getContext().hasRendererQuirk(GLRendererQuirks.NeedSharedObjectSync) ) {
186 syncObjects = sharedGears;
187 System.err.println("Shared GearsES1: Synchronized Objects due to quirk "+GLRendererQuirks.toString(GLRendererQuirks.NeedSharedObjectSync));
188 } else {
189 syncObjects = new Object();
190 System.err.println("Shared GearsES1: Unsynchronized Objects");
191 }
192 } else {
193 gear1 = new GearsObjectES1(gl, useMappedBuffers, gear1Color, 1.0f, 4.0f, 1.0f, 20, 0.7f, validateBuffers);
194 System.err.println("gear1 created: "+gear1);
195
196 gear2 = new GearsObjectES1(gl, useMappedBuffers, gear2Color, 0.5f, 2.0f, 2.0f, 10, 0.7f, validateBuffers);
197 System.err.println("gear2 created: "+gear2);
198
199 gear3 = new GearsObjectES1(gl, useMappedBuffers, gear3Color, 1.3f, 2.0f, 0.5f, 10, 0.7f, validateBuffers);
200 System.err.println("gear3 created: "+gear3);
201
202 syncObjects = new Object();
203 }
204
205 gl.glEnable(GLLightingFunc.GL_NORMALIZE);
206
207 final Object upstreamWidget = drawable.getUpstreamWidget();
208 if (upstreamWidget instanceof Window) {
209 final Window window = (Window) upstreamWidget;
210 window.addMouseListener(gearsMouse);
211 window.addKeyListener(gearsKeys);
212 } else if (GLProfile.isAWTAvailable() && upstreamWidget instanceof java.awt.Component) {
213 final java.awt.Component comp = (java.awt.Component) upstreamWidget;
214 new com.jogamp.newt.event.awt.AWTMouseAdapter(gearsMouse, drawable).addTo(comp);
215 new com.jogamp.newt.event.awt.AWTKeyAdapter(gearsKeys, drawable).addTo(comp);
216 }
217 isInit = true;
218 System.err.println(Thread.currentThread()+" GearsES1.init FIN");
219 }
220
221 public final boolean isInit() { return isInit; }
222
223 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
224 if( !isInit ) { return; }
225 System.err.println(Thread.currentThread()+" GearsES1.reshape "+x+"/"+y+" "+width+"x"+height+", swapInterval "+swapInterval);
226 final GL2ES1 gl = drawable.getGL().getGL2ES1();
227
228 gl.setSwapInterval(swapInterval);
229
231
232 gl.glLoadIdentity();
233 if(height>width) {
234 final float h = (float)height / (float)width;
235 gl.glFrustumf(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
236 } else {
237 final float h = (float)width / (float)height;
238 gl.glFrustumf(-h, h, -1.0f, 1.0f, 5.0f, 60.0f);
239 }
241 gl.glLoadIdentity();
242 gl.glTranslatef(0.0f, 0.0f, -40.0f);
243 System.err.println(Thread.currentThread()+" GearsES1.reshape FIN");
244 }
245
246 public void dispose(final GLAutoDrawable drawable) {
247 if( !isInit ) { return; }
248 isInit = false;
249 System.err.println(Thread.currentThread()+" GearsES1.dispose ... ");
250 final Object upstreamWidget = drawable.getUpstreamWidget();
251 if (upstreamWidget instanceof Window) {
252 final Window window = (Window) upstreamWidget;
253 window.removeMouseListener(gearsMouse);
254 window.removeKeyListener(gearsKeys);
255 }
256 final GL gl = drawable.getGL();
257 gear1.destroy(gl);
258 gear1 = null;
259 gear2.destroy(gl);
260 gear2 = null;
261 gear3.destroy(gl);
262 gear3 = null;
263 sharedGears = null;
264 syncObjects = null;
265 System.err.println(Thread.currentThread()+" GearsES1.dispose FIN");
266 }
267
268 public void display(final GLAutoDrawable drawable) {
269 if( !isInit ) { return; }
270
271 // Turn the gears' teeth
272 angle += 0.5f;
273
274 // Get the GL corresponding to the drawable we are animating
275 final GL2ES1 gl = drawable.getGL().getGL2ES1();
276
277 final boolean hasFocus;
278 final Object upstreamWidget = drawable.getUpstreamWidget();
279 if(upstreamWidget instanceof NativeWindow) {
280 hasFocus = ((NativeWindow)upstreamWidget).hasFocus();
281 } else {
282 hasFocus = true;
283 }
284 if(hasFocus) {
285 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
286 } else {
287 gl.glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
288 }
289
290 // Special handling for the case where the GLJPanel is translucent
291 // and wants to be composited with other Java 2D content
292 if (GLProfile.isAWTAvailable() &&
293 (drawable instanceof com.jogamp.opengl.awt.GLJPanel) &&
294 !((com.jogamp.opengl.awt.GLJPanel) drawable).isOpaque() &&
295 ((com.jogamp.opengl.awt.GLJPanel) drawable).shouldPreserveColorBufferIfTranslucent()) {
297 } else {
299 }
300
301 gl.glNormal3f(0.0f, 0.0f, 1.0f);
302
303 // Rotate the entire assembly of gears based on how the user
304 // dragged the mouse around
305 gl.glPushMatrix();
306 gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);
307 gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
308 gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);
309
310 synchronized ( syncObjects ) {
311 gear1.draw(gl, -3.0f, -2.0f, angle);
312 gear2.draw(gl, 3.1f, -2.0f, -2.0f * angle - 9.0f);
313 gear3.draw(gl, -3.1f, 4.2f, -2.0f * angle - 25.0f);
314 }
315
316 // Remember that every push needs a pop; this one is paired with
317 // rotating the entire gear assembly
318 gl.glPopMatrix();
319 }
320
321
322 class GearsKeyAdapter extends KeyAdapter {
323 public void keyPressed(final KeyEvent e) {
324 final int kc = e.getKeyCode();
325 if(KeyEvent.VK_LEFT == kc) {
326 view_roty -= 1;
327 } else if(KeyEvent.VK_RIGHT == kc) {
328 view_roty += 1;
329 } else if(KeyEvent.VK_UP == kc) {
330 view_rotx -= 1;
331 } else if(KeyEvent.VK_DOWN == kc) {
332 view_rotx += 1;
333 }
334 }
335 }
336
337 class GearsMouseAdapter extends MouseAdapter {
338 public void mousePressed(final MouseEvent e) {
339 prevMouseX = e.getX();
340 prevMouseY = e.getY();
341 }
342
343 public void mouseReleased(final MouseEvent e) {
344 }
345
346 public void mouseDragged(final MouseEvent e) {
347 final int x = e.getX();
348 final int y = e.getY();
349 int width=0, height=0;
350 final Object source = e.getSource();
351 if(source instanceof Window) {
352 final Window window = (Window) source;
353 width=window.getSurfaceWidth();
354 height=window.getSurfaceHeight();
355 } else if (source instanceof GLAutoDrawable) {
356 final GLAutoDrawable glad = (GLAutoDrawable) source;
357 width = glad.getSurfaceWidth();
358 height = glad.getSurfaceHeight();
359 } else if (GLProfile.isAWTAvailable() && source instanceof java.awt.Component) {
360 final java.awt.Component comp = (java.awt.Component) source;
361 width=comp.getWidth(); // FIXME HiDPI: May need to convert window units -> pixel units!
362 height=comp.getHeight();
363 } else {
364 throw new RuntimeException("Event source neither Window nor Component: "+source);
365 }
366 final float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)width);
367 final float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)height);
368
369 prevMouseX = x;
370 prevMouseY = y;
371
372 view_rotx += thetaX;
373 view_roty += thetaY;
374 }
375 }
376}
static final short VK_LEFT
Constant for the cursor- or numerical-pad left arrow key.
Definition: KeyEvent.java:811
final short getKeyCode()
Returns the virtual key code using a fixed mapping to the US keyboard layout.
Definition: KeyEvent.java:195
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...
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
GLRendererQuirks contains information of known bugs of various GL renderer.
static final int NeedSharedObjectSync
Need GL objects (VBO, ..) to be synchronized when utilized concurrently from multiple threads via a s...
final StringBuilder toString(StringBuilder sb)
static StringBuilder getGLStrings(final GL gl, final StringBuilder sb)
abstract void draw(GL gl, float x, float y, float angle)
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
Definition: GearsES1.java:246
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
Definition: GearsES1.java:268
void setGearsColors(final FloatBuffer gear1Color, final FloatBuffer gear2Color, final FloatBuffer gear3Color)
Definition: GearsES1.java:97
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.
Definition: GearsES1.java:223
void setForceFFPEmu(final boolean forceFFPEmu, final boolean verboseFFPEmu, final boolean debugFFPEmu, final boolean traceFFPEmu)
Definition: GearsES1.java:90
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
Definition: GearsES1.java:127
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.
Extend the NativeSurface interface with windowing information such as window-handle,...
Specifying NEWT's Window functionality:
Definition: Window.java:115
void removeKeyListener(KeyListener l)
void removeMouseListener(MouseListener l)
Removes the given MouseListener from the list.
Listener for KeyEvents.
void glNormal3f(float nx, float ny, float nz)
Entry point to C language function: void {@native glNormal3f}(GLfloat nx, GLfloat ny,...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void setGLEventListenerInitState(GLEventListener listener, boolean initialized)
Sets the given listener's initialized state.
Object getUpstreamWidget()
Method may return the upstream UI toolkit object holding this GLAutoDrawable instance,...
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.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
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_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_CULL_FACE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_CULL_FACE" with expre...
Definition: GL.java:720
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
void glPushMatrix()
Push the current matrix to it's stack, while preserving it's values.
void glPopMatrix()
Pop the current matrix from it's stack.
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 glLoadIdentity()
Load the current matrix with the identity matrix.
void glMatrixMode(int mode)
Sets the current matrix mode.