21package com.jogamp.opengl.demos.es2;
23import java.nio.FloatBuffer;
25import com.jogamp.common.nio.Buffers;
26import com.jogamp.common.util.VersionUtil;
27import com.jogamp.math.Matrix4f;
28import com.jogamp.math.Quaternion;
29import com.jogamp.math.Vec3f;
30import com.jogamp.math.util.PMVMatrix4f;
31import com.jogamp.newt.Window;
32import com.jogamp.newt.event.GestureHandler;
33import com.jogamp.newt.event.GestureHandler.GestureEvent;
34import com.jogamp.newt.event.KeyAdapter;
35import com.jogamp.newt.event.KeyEvent;
36import com.jogamp.newt.event.KeyListener;
37import com.jogamp.newt.event.MouseEvent;
38import com.jogamp.newt.event.MouseListener;
39import com.jogamp.newt.event.PinchToZoomGesture;
40import com.jogamp.newt.event.WindowAdapter;
41import com.jogamp.newt.event.WindowEvent;
42import com.jogamp.newt.opengl.GLWindow;
43import com.jogamp.opengl.GL;
44import com.jogamp.opengl.GL2ES2;
45import com.jogamp.opengl.GLAutoDrawable;
46import com.jogamp.opengl.GLCapabilities;
47import com.jogamp.opengl.GLUniformData;
48import com.jogamp.opengl.JoglVersion;
49import com.jogamp.opengl.demos.GearsObject;
50import com.jogamp.opengl.demos.util.CommandlineOptions;
51import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
52import com.jogamp.opengl.util.Animator;
53import com.jogamp.opengl.util.CustomGLEventListener;
54import com.jogamp.opengl.util.PMVMatrix;
55import com.jogamp.opengl.util.caps.NonFSAAGLCapsChooser;
56import com.jogamp.opengl.util.glsl.ShaderCode;
57import com.jogamp.opengl.util.glsl.ShaderProgram;
58import com.jogamp.opengl.util.glsl.ShaderState;
59import com.jogamp.opengl.util.stereo.EyeParameter;
60import com.jogamp.opengl.util.stereo.StereoGLEventListener;
61import com.jogamp.opengl.util.stereo.ViewerPose;
68 private final FloatBuffer lightPos = Buffers.newDirectFloatBuffer(
new float[] { 5.0f, 5.0f, 10.0f } );
74 private volatile float view_rotx = 20.0f, view_roty = 30.0f;
76 private final float view_rotz = 0.0f;
77 private float panX = 0.0f, panY = 0.0f, panZ=0.0f;
78 private float scalexyz=1.0f;
80 private volatile GearsObjectES2 gear1=
null, gear2=
null, gear3=
null;
81 private boolean useMappedBuffers =
false;
82 private boolean validateBuffers =
false;
83 private float angle = 0.0f;
84 private int swapInterval = 0;
89 private boolean doRotate =
true;
90 private float[] clearColor =
null;
91 private boolean clearBuffers =
true;
92 private boolean clearStencilBuffer =
false;
93 private boolean verbose =
true;
94 private volatile boolean isInit =
false;
100 this.swapInterval = swapInterval;
104 this.swapInterval = 1;
114 this.clearColor = clearColor;
117 public void setGearsColor(
final int idx,
final float r,
final float g,
final float b,
final float a) {
124 case 0: gear1.
setColor(r, g, b, a);
return;
125 case 1: gear2.
setColor(r, g, b, a);
return;
126 case 2: gear3.
setColor(r, g, b, a);
return;
137 public void setScale(
final float v) { scalexyz=v; }
141 public void setRotX(
final float v) { view_rotx = v; }
142 public void setRotY(
final float v) { view_roty = v; }
144 public void addPanning(
final float x,
final float y,
final float z) { panX += x; panY += y; panZ += z; }
168 private static final int TIME_OUT = 2000;
169 private static final int POLL_DIVIDER = 20;
170 private static final int TIME_SLICE = TIME_OUT / POLL_DIVIDER ;
175 public boolean waitForInit(
final boolean initialized)
throws InterruptedException {
177 for (wait=0; wait<POLL_DIVIDER && initialized != isInit ; wait++) {
178 Thread.sleep(TIME_SLICE);
180 return wait<POLL_DIVIDER;
183 private final String sid() {
return "0x"+Integer.toHexString(hashCode()); }
189 System.err.println(Thread.currentThread()+
" GearsES2.init.0 "+sid()+
", "+
this);
190 System.err.println(
"GearsES2 init "+sid()+
" on "+Thread.currentThread());
192 System.err.println(
"INIT GL IS: " + gl.getClass().getName());
196 System.err.println(
"No GLSL available, no rendering.");
203 "shader/bin",
"gears",
true);
205 "shader/bin",
"gears",
true);
209 sp0.
add(gl, vp0, System.err);
210 sp0.
add(gl, fp0, System.err);
219 st.
uniform(gl, pmvMatrixUniform);
225 colorU =
new GLUniformData(
"color", 4, Buffers.newDirectFloatBuffer(4));
230 gear1 =
new GearsObjectES2(gl, useMappedBuffers, st,
GearsObject.
red, 1.0f, 4.0f, 1.0f, 20, 0.7f, pmvMatrix, pmvMatrixUniform, colorU, validateBuffers);
232 System.err.println(
"gear1 "+sid()+
" created: "+gear1);
235 gear2 =
new GearsObjectES2(gl, useMappedBuffers, st,
GearsObject.
green, 0.5f, 2.0f, 2.0f, 10, 0.7f, pmvMatrix, pmvMatrixUniform, colorU, validateBuffers);
237 System.err.println(
"gear2 "+sid()+
" created: "+gear2);
240 gear3 =
new GearsObjectES2(gl, useMappedBuffers, st,
GearsObject.
blue, 1.3f, 2.0f, 0.5f, 10, 0.7f, pmvMatrix, pmvMatrixUniform, colorU, validateBuffers);
242 System.err.println(
"gear3 "+sid()+
" created: "+gear2);
247 if (upstreamWidget instanceof
Window) {
262 System.err.println(Thread.currentThread()+
" GearsES2.init.X "+sid()+
" FIN "+
this);
272 final float zoom = ze.getZoom();
273 panZ = zoom * 30f - 30f;
277 private float zNear = 5f;
278 private float zFar = 10000f;
279 private float zViewDist = 40.0f;
281 public void setZ(
final float zNear,
final float zFar,
final float zViewDist) {
284 this.zViewDist = zViewDist;
292 if( !isInit ) {
return; }
296 System.err.println(Thread.currentThread()+
" GearsES2.reshape "+sid()+
" "+x+
"/"+y+
" "+width+
"x"+height+
", swapInterval "+swapInterval+
307 if( height > width ) {
308 final float a = (float)height / (
float)width;
314 final float a = (float)width / (
float)height;
322 pmvMatrix.
glFrustumf(l, r, b, t, zNear, zFar);
328 st.
uniform(gl, pmvMatrixUniform);
339 private static final float scalePos = 20f;
364 shiftedEyePos.
scale(scalePos);
370 final Vec3f center = forward.
add(shiftedEyePos);
376 mat4.
translate(0, 0, -zViewDist, mat4Tmp1);
380 st.
uniform(gl, pmvMatrixUniform);
386 if( !isInit ) {
return; }
389 System.err.println(Thread.currentThread()+
" GearsES2.dispose "+sid());
392 if (upstreamWidget instanceof
Window) {
397 pinchToZoomGesture =
null;
417 System.err.println(Thread.currentThread()+
" GearsES2.dispose "+sid()+
" FIN");
428 if( !isInit ) {
return; }
430 final boolean repeatedFrame = 0 != ( CustomGLEventListener.DISPLAY_REPEAT & flags );
431 final boolean dontClear = 0 != ( CustomGLEventListener.DISPLAY_DONTCLEAR & flags );
434 if( doRotate && !repeatedFrame ) {
441 if( clearBuffers && !dontClear ) {
442 if(
null != clearColor ) {
443 gl.
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
447 if( clearStencilBuffer ) {
457 setGLStatesImpl(gl,
true);
462 pmvMatrix.
glScalef(scalexyz, scalexyz, scalexyz);
463 pmvMatrix.
glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);
464 pmvMatrix.
glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
465 pmvMatrix.
glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);
467 gear1.draw(gl, -3.0f, -2.0f, 1f * angle - 0f);
468 gear2.draw(gl, 3.1f, -2.0f, -2f * angle - 9.0f);
469 gear3.draw(gl, -3.1f, 4.2f, -2f * angle - 25.0f);
474 setGLStatesImpl(gl,
false);
477 private void setGLStatesImpl(
final GL2ES2 gl,
final boolean enable) {
495 return "GearsES2[obj "+sid()+
" isInit "+isInit+
", 1 "+gear1+
", 2 "+gear2+
", 3 "+gear3+
"]";
503 public void keyPressed(
final KeyEvent e) {
507 }
else if(KeyEvent.VK_RIGHT == kc) {
509 }
else if(KeyEvent.VK_UP == kc) {
511 }
else if(KeyEvent.VK_DOWN == kc) {
517 class GearsMouseAdapter
implements MouseListener{
518 private int prevMouseX, prevMouseY;
521 public void mouseClicked(
final MouseEvent e) {
525 public void mouseEntered(
final MouseEvent e) {
529 public void mouseExited(
final MouseEvent e) {
533 public void mouseWheelMoved(
final MouseEvent e) {
534 final float[] rot = e.getRotation();
535 if( e.isControlDown() ) {
537 final float incr = e.isShiftDown() ? rot[0] : rot[1] * 0.5f ;
539 System.err.println(
"panZ.2: incr "+incr+
", dblZoom "+e.isShiftDown()+
" -> "+panZ);
544 System.err.println(
"panXY.2: incr ("+rot[0]+
", "+rot[1]+
") -> ("+panX+
", "+panY+
")");
549 public void mousePressed(
final MouseEvent e) {
550 if( e.getPointerCount()==1 ) {
551 prevMouseX = e.getX();
552 prevMouseY = e.getY();
553 }
else if( e.getPointerCount() == 4 ) {
554 final Object src = e.getSource();
555 if( e.getPressure(0,
true) > 0.7f && src instanceof Window) {
556 ((Window) src).setKeyboardVisible(
true);
562 public void mouseReleased(
final MouseEvent e) {
566 public void mouseMoved(
final MouseEvent e) {
567 if( e.isConfined() ) {
572 prevMouseX = e.getX();
573 prevMouseY = e.getY();
578 public void mouseDragged(
final MouseEvent e) {
582 private void navigate(
final MouseEvent e) {
583 final int x = e.getX();
584 final int y = e.getY();
587 final Object source = e.getSource();
588 Window window =
null;
589 if(source instanceof Window) {
590 window = (Window) source;
591 width=window.getSurfaceWidth();
592 height=window.getSurfaceHeight();
593 }
else if (source instanceof GLAutoDrawable) {
594 final GLAutoDrawable glad = (GLAutoDrawable) source;
595 width = glad.getSurfaceWidth();
596 height = glad.getSurfaceHeight();
598 throw new RuntimeException(
"Event source neither Window nor Component: "+source);
600 final float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)width);
601 final float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)height);
610 public static void main(
final String[] args) {
613 System.err.println(options);
614 System.err.println(VersionUtil.getPlatformInfo());
618 System.out.println(
"Requested: " + reqCaps);
631 animator.
add(window);
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
final Matrix4f mul(final Matrix4f b)
Multiply matrix: [this] = [this] x [b].
final Matrix4f translate(final float x, final float y, final float z, final Matrix4f tmp)
Translate this matrix, i.e.
Matrix4f setToPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Set this matrix to perspective frustum projection.
Matrix4f setToLookAt(final Vec3f eye, final Vec3f center, final Vec3f up, final Matrix4f tmp)
Set this matrix to the look-at matrix based on given parameters.
final Matrix4f setToTranslation(final float x, final float y, final float z)
Set this matrix to translation.
Quaternion implementation supporting Gimbal-Lock free rotations.
final Vec3f rotateVector(final Vec3f vecIn, final Vec3f vecOut)
final Quaternion mult(final Quaternion q)
Multiply this quaternion by the param quaternion.
3D Vector based upon three float components.
Vec3f scale(final float s)
this = this * s, returns this.
static final Vec3f UNIT_Z_NEG
static final Vec3f UNIT_Y
Vec3f set(final Vec3f o)
this = o, returns this.
Vec3f add(final float dx, final float dy, final float dz)
this = this + { dx, dy, dz }, returns this.
PMVMatrix4f implements the basic computer graphics Matrix4f pack using projection (P),...
static final int INVERSE_TRANSPOSED_MODELVIEW
Bit value for inverse transposed modelview matrix (Mvit), updated via update().
static final int INVERSE_MODELVIEW
Bit value for inverse modelview matrix (Mvi), updated via update().
final SyncMatrices4f getSyncPMvMviMvit()
Returns SyncMatrices4f of 4 matrices within one FloatBuffer: P, Mv, Mvi and Mvit if requested.
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.
A GestureHandler.GestureEvent denominating zoom.
NEWT Window events are provided for notification purposes ONLY.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
final void setTitle(final String title)
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
CapabilitiesChooser setCapabilitiesChooser(final CapabilitiesChooser chooser)
Set the CapabilitiesChooser to help determine the native visual type.
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Specifies a set of OpenGL capabilities.
abstract GLDrawable getGLDrawable()
Returns the write-drawable this context uses for framebuffer operations.
static StringBuilder getGLStrings(final GL gl, final StringBuilder sb)
static final FloatBuffer green
void destroy(final GL gl)
static final FloatBuffer red
static final FloatBuffer blue
void setColor(final float r, final float g, final float b, final float a)
void setClearStencilBuffer(final boolean v)
void setGearsColor(final int idx, final float r, final float g, final float b, final float a)
void setVerbose(final boolean v)
void display(final GLAutoDrawable drawable, final int flags)
Extended display method, allowing to pass a display flag, e.g.
void setDoRotation(final boolean rotate)
void setClearColor(final float[] clearColor)
float[4]
void setUseMappedBuffers(final boolean v)
GearsES2(final int swapInterval)
void setValidateBuffers(final boolean v)
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 dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void setClearBuffers(final boolean v)
static void main(final String[] args)
GearsObjectES2 getGear2()
boolean waitForInit(final boolean initialized)
void reshapeForEye(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height, final EyeParameter eyeParam, final ViewerPose viewerPose)
Stereo capable specialization of reshape(GLAutoDrawable, int, int, int, int) for one StereoDeviceRend...
void setZ(final float zNear, final float zFar, final float zViewDist)
GearsObjectES2 getGear3()
MouseListener getMouseListener()
void addPanning(final float x, final float y, final float z)
void setRotX(final float v)
void setScale(final float v)
PMVMatrix4f getPMVMatrix()
GearsObjectES2 getGear1()
void setRotY(final float v)
KeyListener getKeyListener()
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.
GLCapabilities getGLCaps()
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
final synchronized boolean stop()
Stops this animator.
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
final void glTranslatef(final float x, final float y, final float z)
Translate the current matrix.
final void glScalef(final float x, final float y, final float z)
Scale the current matrix.
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
final void glPushMatrix()
Push the current matrix to it's stack, while preserving it's values.
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.
final void glRotatef(final float ang_deg, final float x, final float y, final float z)
Rotate the current matrix.
final void glPopMatrix()
Pop the current matrix from it's stack.
final void glLoadIdentity()
Load the current matrix with the identity matrix.
final void glLoadMatrixf(final float[] values, final int offset)
Load the current matrix w/ the provided one.
Custom GLCapabilitiesChooser, filtering out all full screen anti-aliasing (FSAA, multisample) capabil...
Convenient shader code class to use and instantiate vertex or fragment programs.
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 ...
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.
final Object attachObject(final String name, final Object obj)
Attach user object for the given name to this ShaderState.
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.
Constant single eye parameter of the viewer, relative to its ViewerPose.
final float distNoseToPupilX
IPD related horizontal distance from nose to pupil in meter.
final FovHVHalves fovhv
Field of view in both directions, may not be centered, either FovHVHalves#inTangents or radians.
final float distMiddleToPupilY
Vertical distance from middle-line to pupil in meter.
final Vec3f positionOffset
eye position vector used to define eye height in meter relative to actor.
final float eyeReliefZ
Z-axis eye relief in meter.
position and orientation of viewer.
final Quaternion orientation
Orientation of viewer.
final Vec3f position
position of viewer in meter.
Specifying NEWT's Window functionality:
void addGestureListener(GestureHandler.GestureListener gl)
Appends the given GestureHandler.GestureListener to the end of the list.
void addKeyListener(KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
void removeGestureHandler(GestureHandler gh)
Removes the given GestureHandler from the list.
void removeKeyListener(KeyListener l)
void addMouseListener(MouseListener l)
Appends the given MouseListener to the end of the list.
void removeGestureListener(GestureHandler.GestureListener gl)
Removes the given GestureHandler.GestureListener from the list.
void addGestureHandler(GestureHandler gh)
Appends the given GestureHandler to the end of the list.
void removeMouseListener(MouseListener l)
Removes the given MouseListener from the list.
Listener for GestureEvents.
Generic gesture handler interface designed to allow pass-through filtering of InputEvents.
Listener for MouseEvents.
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...
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...
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
Object getUpstreamWidget()
Method may return the upstream UI toolkit object holding this GLAutoDrawable instance,...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
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.
boolean getSampleBuffers()
Returns whether sample buffers for full-scene antialiasing (FSAA) should be allocated for this drawab...
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...
boolean isGLOriented()
Returns true if the drawable is rendered in OpenGL's coordinate system, origin at bottom left.
NativeSurface getNativeSurface()
Returns the associated NativeSurface of this NativeSurfaceHolder.
void glDisable(int cap)
Entry point to C language function: void {@native glDisable}(GLenum cap) Part of GL_ES_VERSION_2_0...
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" with expression '0x...
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_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expression '0x0B71',...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
void glFinish()
Entry point to C language function: void {@native glFinish}() Part of GL_ES_VERSION_2_0,...
static final int GL_STENCIL_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_STENCIL_BUFFER_BIT" with expression '...
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" with expression '0x...
static final int GL_CULL_FACE
GL_ES_VERSION_2_0, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_CULL_FACE" with expression '0x0B44',...
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.
Extended GLEventListener and CustomGLEventListener interface supporting stereoscopic client rendering...