22package com.jogamp.opengl.test.junit.jogl.demos.es1;
24import java.nio.FloatBuffer;
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;
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;
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 ;
62 private final float pos[] = { 5.0f, 5.0f, 10.0f, 0.0f };
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;
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;
80 private int prevMouseX, prevMouseY;
83 this.swapInterval = swapInterval;
87 this.swapInterval = 1;
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;
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;
104 sharedGears = shared;
128 if(
null != sharedGears && !sharedGears.isInit() ) {
129 System.err.println(Thread.currentThread()+
" GearsES1.init.0: pending shared Gears .. re-init later XXXXX");
133 System.err.println(Thread.currentThread()+
" GearsES1.init ...");
156 }
catch (
final Exception e) {e.printStackTrace();}
162 }
catch (
final Exception e) {e.printStackTrace();}
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());
177 if(
null != sharedGears ) {
181 usesSharedGears =
true;
182 System.err.println(
"gear1 reuse: "+gear1);
183 System.err.println(
"gear2 reuse: "+gear2);
184 System.err.println(
"gear3 reuse: "+gear3);
186 syncObjects = sharedGears;
189 syncObjects =
new Object();
190 System.err.println(
"Shared GearsES1: Unsynchronized Objects");
193 gear1 =
new GearsObjectES1(gl, useMappedBuffers, gear1Color, 1.0f, 4.0f, 1.0f, 20, 0.7f, validateBuffers);
194 System.err.println(
"gear1 created: "+gear1);
196 gear2 =
new GearsObjectES1(gl, useMappedBuffers, gear2Color, 0.5f, 2.0f, 2.0f, 10, 0.7f, validateBuffers);
197 System.err.println(
"gear2 created: "+gear2);
199 gear3 =
new GearsObjectES1(gl, useMappedBuffers, gear3Color, 1.3f, 2.0f, 0.5f, 10, 0.7f, validateBuffers);
200 System.err.println(
"gear3 created: "+gear3);
202 syncObjects =
new Object();
205 gl.glEnable(GLLightingFunc.GL_NORMALIZE);
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);
218 System.err.println(Thread.currentThread()+
" GearsES1.init FIN");
224 if( !isInit ) {
return; }
225 System.err.println(Thread.currentThread()+
" GearsES1.reshape "+x+
"/"+y+
" "+width+
"x"+height+
", swapInterval "+swapInterval);
234 final float h = (float)height / (
float)width;
235 gl.
glFrustumf(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
237 final float h = (float)width / (
float)height;
238 gl.
glFrustumf(-h, h, -1.0f, 1.0f, 5.0f, 60.0f);
243 System.err.println(Thread.currentThread()+
" GearsES1.reshape FIN");
247 if( !isInit ) {
return; }
249 System.err.println(Thread.currentThread()+
" GearsES1.dispose ... ");
251 if (upstreamWidget instanceof
Window) {
256 final GL gl = drawable.
getGL();
265 System.err.println(Thread.currentThread()+
" GearsES1.dispose FIN");
269 if( !isInit ) {
return; }
277 final boolean hasFocus;
293 (drawable instanceof com.jogamp.opengl.awt.GLJPanel) &&
294 !((com.jogamp.opengl.awt.GLJPanel) drawable).isOpaque() &&
295 ((com.jogamp.opengl.awt.GLJPanel) drawable).shouldPreserveColorBufferIfTranslucent()) {
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);
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);
323 public void keyPressed(
final KeyEvent e) {
327 }
else if(KeyEvent.VK_RIGHT == kc) {
329 }
else if(KeyEvent.VK_UP == kc) {
331 }
else if(KeyEvent.VK_DOWN == kc) {
337 class GearsMouseAdapter
extends MouseAdapter {
338 public void mousePressed(
final MouseEvent e) {
339 prevMouseX = e.getX();
340 prevMouseY = e.getY();
343 public void mouseReleased(
final MouseEvent e) {
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();
362 height=comp.getHeight();
364 throw new RuntimeException(
"Event source neither Window nor Component: "+source);
366 final float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)width);
367 final float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)height);
static final short VK_LEFT
Constant for the cursor- or numerical-pad left arrow key.
final short getKeyCode()
Returns the virtual key code using a fixed mapping to the US keyboard layout.
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.
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)
static final FloatBuffer blue
abstract void draw(GL gl, float x, float y, float angle)
static final FloatBuffer green
void destroy(final GL gl)
static final FloatBuffer red
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void setSharedGears(final GearsES1 shared)
GearsES1(final int swapInterval)
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
boolean usesSharedGears()
void setGearsColors(final FloatBuffer gear1Color, final FloatBuffer gear2Color, final FloatBuffer gear3Color)
void setValidateBuffers(final boolean v)
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 setForceFFPEmu(final boolean forceFFPEmu, final boolean verboseFFPEmu, final boolean debugFFPEmu, final boolean traceFFPEmu)
void setUseMappedBuffers(final boolean v)
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
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:
void removeKeyListener(KeyListener l)
void removeMouseListener(MouseListener l)
Removes the given MouseListener from the list.
Listener for MouseEvents.
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...
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...
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...
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...
static final int GL_POSITION
static final int GL_LIGHTING
static final int GL_LIGHT0
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.