JOGL v2.6.0-rc-20250822
JOGL, High-Performance Graphics Binding for Java™ (public API).
GearsES2.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 */
21package com.jogamp.opengl.demos.es2;
22
23import java.nio.FloatBuffer;
24
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;
62
63/**
64 * GearsES2.java <BR>
65 * @author Brian Paul (converted to Java by Ron Cemer and Sven Gothel) <P>
66 */
67public class GearsES2 implements StereoGLEventListener {
68 private final FloatBuffer lightPos = Buffers.newDirectFloatBuffer( new float[] { 5.0f, 5.0f, 10.0f } );
69
70 private ShaderState st = null;
71 private PMVMatrix pmvMatrix = null;
72 private GLUniformData pmvMatrixUniform = null;
73 private GLUniformData colorU = null;
74 private volatile float view_rotx = 20.0f, view_roty = 30.0f;
75
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;
79
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;
85 // private MouseListener gearsMouse = new TraceMouseAdapter(new GearsMouseAdapter());
86 public MouseListener gearsMouse = new GearsMouseAdapter();
87 public KeyListener gearsKeys = new GearsKeyAdapter();
88
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;
95
96 private PinchToZoomGesture pinchToZoomGesture = null;
97
98
99 public GearsES2(final int swapInterval) {
100 this.swapInterval = swapInterval;
101 }
102
103 public GearsES2() {
104 this.swapInterval = 1;
105 }
106
107 public void setDoRotation(final boolean rotate) { this.doRotate = rotate; }
108 public void setClearBuffers(final boolean v) { clearBuffers = v; }
109 public void setClearStencilBuffer(final boolean v) { clearStencilBuffer = v; }
110 public void setVerbose(final boolean v) { verbose = v; }
111
112 /** float[4] */
113 public void setClearColor(final float[] clearColor) {
114 this.clearColor = clearColor;
115 }
116
117 public void setGearsColor(final int idx, final float r, final float g, final float b, final float a) {
118 switch(idx) {
119 case -1:
120 gear1.setColor(r, g, b, a);
121 gear2.setColor(r, g, b, a);
122 gear3.setColor(r, g, b, a);
123 return;
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;
127 default: return;
128 }
129 }
130 public void resetGearsColor() {
131 gear1.setColor(GearsObject.red.get(0), GearsObject.red.get(1), GearsObject.red.get(2), GearsObject.red.get(3));
132 gear2.setColor(GearsObject.green.get(0), GearsObject.green.get(1), GearsObject.green.get(2), GearsObject.green.get(3));
133 gear3.setColor(GearsObject.blue.get(0), GearsObject.blue.get(1), GearsObject.blue.get(2), GearsObject.blue.get(3));
134 }
135
136 public float getScale() { return scalexyz; }
137 public void setScale(final float v) { scalexyz=v; }
138
139 public float getRotX() { return view_rotx; }
140 public float getRotY() { return view_roty; }
141 public void setRotX(final float v) { view_rotx = v; }
142 public void setRotY(final float v) { view_roty = v; }
143
144 public void addPanning(final float x, final float y, final float z) { panX += x; panY += y; panZ += z; }
145
146 /**
147 * @return gear1
148 */
149 public GearsObjectES2 getGear1() { return gear1; }
150
151 /**
152 * @return gear2
153 */
154 public GearsObjectES2 getGear2() { return gear2; }
155
156 /**
157 * @return gear3
158 */
159 public GearsObjectES2 getGear3() { return gear3; }
160
161 public void setUseMappedBuffers(final boolean v) { useMappedBuffers = v; }
162 public void setValidateBuffers(final boolean v) { validateBuffers = v; }
163
165 return pmvMatrix;
166 }
167
168 private static final int TIME_OUT = 2000; // 2s
169 private static final int POLL_DIVIDER = 20; // TO/20
170 private static final int TIME_SLICE = TIME_OUT / POLL_DIVIDER ;
171
172 /**
173 * @return True if this GLEventListener became initialized within TIME_OUT 2s
174 */
175 public boolean waitForInit(final boolean initialized) throws InterruptedException {
176 int wait;
177 for (wait=0; wait<POLL_DIVIDER && initialized != isInit ; wait++) {
178 Thread.sleep(TIME_SLICE);
179 }
180 return wait<POLL_DIVIDER;
181 }
182
183 private final String sid() { return "0x"+Integer.toHexString(hashCode()); }
184
185 @Override
186 public void init(final GLAutoDrawable drawable) {
187 final GL2ES2 gl = drawable.getGL().getGL2ES2();
188 if(verbose) {
189 System.err.println(Thread.currentThread()+" GearsES2.init.0 "+sid()+", "+this);
190 System.err.println("GearsES2 init "+sid()+" on "+Thread.currentThread());
191 System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
192 System.err.println("INIT GL IS: " + gl.getClass().getName());
193 System.err.println(JoglVersion.getGLStrings(gl, null, false).toString());
194 }
195 if( !gl.hasGLSL() ) {
196 System.err.println("No GLSL available, no rendering.");
197 return;
198 }
199
200 st = new ShaderState();
201 // st.setVerbose(true);
202 final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader",
203 "shader/bin", "gears", true);
204 final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader",
205 "shader/bin", "gears", true);
206 vp0.defaultShaderCustomization(gl, true, true);
207 fp0.defaultShaderCustomization(gl, true, true);
208 final ShaderProgram sp0 = new ShaderProgram();
209 sp0.add(gl, vp0, System.err);
210 sp0.add(gl, fp0, System.err);
211 st.attachShaderProgram(gl, sp0, true);
212 // Use debug pipeline
213 // drawable.setGL(new DebugGL(drawable.getGL()));
214
216 st.attachObject("pmvMatrix", pmvMatrix);
217 pmvMatrixUniform = new GLUniformData("pmvMatrix", 4, 4, pmvMatrix.getSyncPMvMviMvit()); // P, Mv, Mvi and Mvit
218 st.ownUniform(pmvMatrixUniform);
219 st.uniform(gl, pmvMatrixUniform);
220
221 final GLUniformData lightU = new GLUniformData("lightPos", 3, lightPos);
222 st.ownUniform(lightU);
223 st.uniform(gl, lightU);
224
225 colorU = new GLUniformData("color", 4, Buffers.newDirectFloatBuffer(4));
226 st.ownUniform(colorU);
227 st.uniform(gl, colorU);
228
229 {
230 gear1 = new GearsObjectES2(gl, useMappedBuffers, st, GearsObject.red, 1.0f, 4.0f, 1.0f, 20, 0.7f, pmvMatrix, pmvMatrixUniform, colorU, validateBuffers);
231 if(verbose) {
232 System.err.println("gear1 "+sid()+" created: "+gear1);
233 }
234
235 gear2 = new GearsObjectES2(gl, useMappedBuffers, st, GearsObject.green, 0.5f, 2.0f, 2.0f, 10, 0.7f, pmvMatrix, pmvMatrixUniform, colorU, validateBuffers);
236 if(verbose) {
237 System.err.println("gear2 "+sid()+" created: "+gear2);
238 }
239
240 gear3 = new GearsObjectES2(gl, useMappedBuffers, st, GearsObject.blue, 1.3f, 2.0f, 0.5f, 10, 0.7f, pmvMatrix, pmvMatrixUniform, colorU, validateBuffers);
241 if(verbose) {
242 System.err.println("gear3 "+sid()+" created: "+gear2);
243 }
244 }
245
246 final Object upstreamWidget = drawable.getUpstreamWidget();
247 if (upstreamWidget instanceof Window) {
248 final Window window = (Window) upstreamWidget;
251 window.addGestureListener(pinchToZoomListener);
252 pinchToZoomGesture = new PinchToZoomGesture(drawable.getNativeSurface(), false);
253 window.addGestureHandler(pinchToZoomGesture);
254 }
255
256 st.useProgram(gl, false);
257
258 gl.glFinish(); // make sure .. for shared context (impacts OSX 10.9)
259
260 isInit = true;
261 if(verbose) {
262 System.err.println(Thread.currentThread()+" GearsES2.init.X "+sid()+" FIN "+this);
263 }
264 }
265
266 public final boolean isInit() { return isInit; }
267
268 private final GestureHandler.GestureListener pinchToZoomListener = new GestureHandler.GestureListener() {
269 @Override
270 public void gestureDetected(final GestureEvent gh) {
271 final PinchToZoomGesture.ZoomEvent ze = (PinchToZoomGesture.ZoomEvent) gh;
272 final float zoom = ze.getZoom(); // * ( ze.getTrigger().getPointerCount() - 1 ); <- too much ..
273 panZ = zoom * 30f - 30f; // [0 .. 2] -> [-30f .. 30f]
274 }
275 };
276
277 private float zNear = 5f;
278 private float zFar = 10000f;
279 private float zViewDist = 40.0f;
280
281 public void setZ(final float zNear, final float zFar, final float zViewDist) {
282 this.zNear = zNear;
283 this.zFar = zFar;
284 this.zViewDist = zViewDist;
285 }
286 public float getZNear() { return zNear; }
287 public float getZFar() { return zFar; }
288 public float getZViewDist() { return zViewDist; }
289
290 @Override
291 public void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height) {
292 if( !isInit ) { return; }
293 final GL2ES2 gl = glad.getGL().getGL2ES2();
295 if(verbose) {
296 System.err.println(Thread.currentThread()+" GearsES2.reshape "+sid()+" "+x+"/"+y+" "+width+"x"+height+", swapInterval "+swapInterval+
297 ", drawable 0x"+Long.toHexString(gl.getContext().getGLDrawable().getHandle())+", msaa "+msaa);
298 }
299 gl.setSwapInterval(swapInterval);
300
301 if( !gl.hasGLSL() ) {
302 return;
303 }
304
305 // compute projection parameters
306 float l, r, b, t;
307 if( height > width ) {
308 final float a = (float)height / (float)width;
309 l = -1.0f;
310 r = 1.0f;
311 b = -a;
312 t = a;
313 } else {
314 final float a = (float)width / (float)height;
315 l = -a;
316 r = a;
317 b = -1.0f;
318 t = 1.0f;
319 }
321 pmvMatrix.glLoadIdentity();
322 pmvMatrix.glFrustumf(l, r, b, t, zNear, zFar);
323
325 pmvMatrix.glLoadIdentity();
326 pmvMatrix.glTranslatef(0.0f, 0.0f, -zViewDist);
327 st.useProgram(gl, true);
328 st.uniform(gl, pmvMatrixUniform);
329 st.useProgram(gl, false);
330 }
331 // private boolean useAndroidDebug = false;
332
333 private final Matrix4f mat4Tmp1 = new Matrix4f();
334 private final Matrix4f mat4Tmp2 = new Matrix4f();
335 private final Vec3f vec3Tmp1 = new Vec3f();
336 private final Vec3f vec3Tmp2 = new Vec3f();
337 private final Vec3f vec3Tmp3 = new Vec3f();
338
339 private static final float scalePos = 20f;
340
341 @Override
342 public void reshapeForEye(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height,
343 final EyeParameter eyeParam, final ViewerPose viewerPose) {
344 final GL2ES2 gl = drawable.getGL().getGL2ES2();
345
346 {
347 //
348 // Projection
349 //
350 final Matrix4f mat4 = new Matrix4f();
352 mat4.setToPerspective(eyeParam.fovhv, zNear, zFar);
353 pmvMatrix.glLoadMatrixf(mat4);
354
355 //
356 // Modelview
357 //
359 final Quaternion rollPitchYaw = new Quaternion();
360 // private final float eyeYaw = FloatUtil.PI; // 180 degrees in radians
361 // rollPitchYaw.rotateByAngleY(eyeYaw);
362 // final Vec3f shiftedEyePos = rollPitchYaw.rotateVector(viewerPose.position, vec3Tmp1);
363 final Vec3f shiftedEyePos = vec3Tmp1.set(viewerPose.position);
364 shiftedEyePos.scale(scalePos); // amplify viewerPose position
365 shiftedEyePos.add(eyeParam.positionOffset);
366
367 rollPitchYaw.mult(viewerPose.orientation);
368 final Vec3f up = rollPitchYaw.rotateVector(Vec3f.UNIT_Y, vec3Tmp2);
369 final Vec3f forward = rollPitchYaw.rotateVector(Vec3f.UNIT_Z_NEG, vec3Tmp3); // -> center
370 final Vec3f center = forward.add(shiftedEyePos);
371
372 final Matrix4f mLookAt = mat4Tmp2.setToLookAt(shiftedEyePos, center, up, mat4Tmp1);
373 mat4.mul( mat4Tmp1.setToTranslation( eyeParam.distNoseToPupilX,
374 eyeParam.distMiddleToPupilY,
375 eyeParam.eyeReliefZ ), mLookAt);
376 mat4.translate(0, 0, -zViewDist, mat4Tmp1);
377 pmvMatrix.glLoadMatrixf(mat4);
378 }
379 st.useProgram(gl, true);
380 st.uniform(gl, pmvMatrixUniform);
381 st.useProgram(gl, false);
382 }
383
384 @Override
385 public void dispose(final GLAutoDrawable drawable) {
386 if( !isInit ) { return; }
387 isInit = false;
388 if(verbose) {
389 System.err.println(Thread.currentThread()+" GearsES2.dispose "+sid());
390 }
391 final Object upstreamWidget = drawable.getUpstreamWidget();
392 if (upstreamWidget instanceof Window) {
393 final Window window = (Window) upstreamWidget;
396 window.removeGestureHandler(pinchToZoomGesture);
397 pinchToZoomGesture = null;
398 window.removeGestureListener(pinchToZoomListener);
399 }
400 final GL2ES2 gl = drawable.getGL().getGL2ES2();
401 if( !gl.hasGLSL() ) {
402 return;
403 }
404 st.useProgram(gl, false);
405 gear1.destroy(gl);
406 gear1 = null;
407 gear2.destroy(gl);
408 gear2 = null;
409 gear3.destroy(gl);
410 gear3 = null;
411 pmvMatrix = null;
412 colorU = null;
413 st.destroy(gl);
414 st = null;
415
416 if(verbose) {
417 System.err.println(Thread.currentThread()+" GearsES2.dispose "+sid()+" FIN");
418 }
419 }
420
421 @Override
422 public void display(final GLAutoDrawable drawable) {
423 display(drawable, 0);
424 }
425
426 @Override
427 public void display(final GLAutoDrawable drawable, final int flags) {
428 if( !isInit ) { return; }
429
430 final boolean repeatedFrame = 0 != ( CustomGLEventListener.DISPLAY_REPEAT & flags );
431 final boolean dontClear = 0 != ( CustomGLEventListener.DISPLAY_DONTCLEAR & flags );
432
433 // Turn the gears' teeth
434 if( doRotate && !repeatedFrame ) {
435 angle += 0.5f;
436 }
437
438 // Get the GL corresponding to the drawable we are animating
439 final GL2ES2 gl = drawable.getGL().getGL2ES2();
440
441 if( clearBuffers && !dontClear ) {
442 if( null != clearColor ) {
443 gl.glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
444 } else {
445 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
446 }
447 if( clearStencilBuffer ) {
449 } else {
451 }
452 }
453 if( !gl.hasGLSL() ) {
454 return;
455 }
456
457 setGLStatesImpl(gl, true);
458
459 st.useProgram(gl, true);
460 pmvMatrix.glPushMatrix();
461 pmvMatrix.glTranslatef(panX, panY, panZ);
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);
466 {
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);
470 }
471 pmvMatrix.glPopMatrix();
472 st.useProgram(gl, false);
473
474 setGLStatesImpl(gl, false);
475 }
476
477 private void setGLStatesImpl(final GL2ES2 gl, final boolean enable) {
478 // Culling only possible if we do not flip the projection matrix
479 final boolean useCullFace = !gl.getContext().getGLDrawable().isGLOriented();
480 if( enable ) {
482 if( useCullFace ) {
484 }
485 } else {
487 if( useCullFace ) {
489 }
490 }
491 }
492
493 @Override
494 public String toString() {
495 return "GearsES2[obj "+sid()+" isInit "+isInit+", 1 "+gear1+", 2 "+gear2+", 3 "+gear3+"]";
496 }
497
498 public KeyListener getKeyListener() { return this.gearsKeys; }
499 public MouseListener getMouseListener() { return this.gearsMouse; }
500
501 class GearsKeyAdapter extends KeyAdapter {
502 @Override
503 public void keyPressed(final KeyEvent e) {
504 final int kc = e.getKeyCode();
505 if(KeyEvent.VK_LEFT == kc) {
506 view_roty -= 1;
507 } else if(KeyEvent.VK_RIGHT == kc) {
508 view_roty += 1;
509 } else if(KeyEvent.VK_UP == kc) {
510 view_rotx -= 1;
511 } else if(KeyEvent.VK_DOWN == kc) {
512 view_rotx += 1;
513 }
514 }
515 }
516
517 class GearsMouseAdapter implements MouseListener{
518 private int prevMouseX, prevMouseY;
519
520 @Override
521 public void mouseClicked(final MouseEvent e) {
522 }
523
524 @Override
525 public void mouseEntered(final MouseEvent e) {
526 }
527
528 @Override
529 public void mouseExited(final MouseEvent e) {
530 }
531
532 @Override
533 public void mouseWheelMoved(final MouseEvent e) {
534 final float[] rot = e.getRotation();
535 if( e.isControlDown() ) {
536 // alternative zoom
537 final float incr = e.isShiftDown() ? rot[0] : rot[1] * 0.5f ;
538 panZ += incr;
539 System.err.println("panZ.2: incr "+incr+", dblZoom "+e.isShiftDown()+" -> "+panZ);
540 } else {
541 // panning
542 panX -= rot[0]; // positive -> left
543 panY += rot[1]; // positive -> up
544 System.err.println("panXY.2: incr ("+rot[0]+", "+rot[1]+") -> ("+panX+", "+panY+")");
545 }
546 }
547
548 @Override
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) { // show Keyboard
556 ((Window) src).setKeyboardVisible(true);
557 }
558 }
559 }
560
561 @Override
562 public void mouseReleased(final MouseEvent e) {
563 }
564
565 @Override
566 public void mouseMoved(final MouseEvent e) {
567 if( e.isConfined() ) {
568 navigate(e);
569 } else {
570 // track prev. position so we don't have 'jumps'
571 // in case we move to confined navigation.
572 prevMouseX = e.getX();
573 prevMouseY = e.getY();
574 }
575 }
576
577 @Override
578 public void mouseDragged(final MouseEvent e) {
579 navigate(e);
580 }
581
582 private void navigate(final MouseEvent e) {
583 final int x = e.getX();
584 final int y = e.getY();
585
586 int width, height;
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();
597 } else {
598 throw new RuntimeException("Event source neither Window nor Component: "+source);
599 }
600 final float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)width);
601 final float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)height);
602 view_rotx += thetaX;
603 view_roty += thetaY;
604 prevMouseX = x;
605 prevMouseY = y;
606 // System.err.println("rotXY.1: "+view_rotx+"/"+view_roty+", source "+e);
607 }
608 }
609
610 public static void main(final String[] args) {
611 final CommandlineOptions options = new CommandlineOptions(1280, 720, 0);
612
613 System.err.println(options);
614 System.err.println(VersionUtil.getPlatformInfo());
615 // System.err.println(JoglVersion.getAllAvailableCapabilitiesInfo(dpy.getGraphicsDevice(), null).toString());
616
617 final GLCapabilities reqCaps = options.getGLCaps();
618 System.out.println("Requested: " + reqCaps);
619
620 final GLWindow window = GLWindow.create(reqCaps);
621 if( 0 == options.sceneMSAASamples ) {
623 }
624 window.setSize(options.surface_width, options.surface_height);
625 window.setTitle(GearsES2.class.getSimpleName());
626
627 window.addGLEventListener(new GearsES2(1));
628
629 final Animator animator = new Animator(0 /* w/o AWT */);
630 animator.setUpdateFPSFrames(5*60, null);
631 animator.add(window);
632
633 window.addWindowListener(new WindowAdapter() {
634 @Override
635 public void windowDestroyed(final WindowEvent e) {
636 animator.stop();
637 }
638 });
639
640 window.setVisible(true);
641 animator.start();
642 }
643
644}
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
Definition: Matrix4f.java:89
final Matrix4f mul(final Matrix4f b)
Multiply matrix: [this] = [this] x [b].
Definition: Matrix4f.java:726
final Matrix4f translate(final float x, final float y, final float z, final Matrix4f tmp)
Translate this matrix, i.e.
Definition: Matrix4f.java:1558
Matrix4f setToPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Set this matrix to perspective frustum projection.
Definition: Matrix4f.java:1303
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.
Definition: Matrix4f.java:1432
final Matrix4f setToTranslation(final float x, final float y, final float z)
Set this matrix to translation.
Definition: Matrix4f.java:887
Quaternion implementation supporting Gimbal-Lock free rotations.
Definition: Quaternion.java:45
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.
Definition: Vec3f.java:37
Vec3f scale(final float s)
this = this * s, returns this.
Definition: Vec3f.java:218
static final Vec3f UNIT_Z_NEG
Definition: Vec3f.java:44
static final Vec3f UNIT_Y
Definition: Vec3f.java:41
Vec3f set(final Vec3f o)
this = o, returns this.
Definition: Vec3f.java:79
Vec3f add(final float dx, final float dy, final float dz)
this = this + { dx, dy, dz }, returns this.
Definition: Vec3f.java:239
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.
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
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,...
Definition: GLWindow.java:121
final void setTitle(final String title)
Definition: GLWindow.java:297
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
CapabilitiesChooser setCapabilitiesChooser(final CapabilitiesChooser chooser)
Set the CapabilitiesChooser to help determine the native visual type.
Definition: GLWindow.java:261
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies a set of OpenGL capabilities.
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)
static final FloatBuffer green
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)
Definition: GearsES2.java:109
void setGearsColor(final int idx, final float r, final float g, final float b, final float a)
Definition: GearsES2.java:117
void setVerbose(final boolean v)
Definition: GearsES2.java:110
void display(final GLAutoDrawable drawable, final int flags)
Extended display method, allowing to pass a display flag, e.g.
Definition: GearsES2.java:427
void setDoRotation(final boolean rotate)
Definition: GearsES2.java:107
void setClearColor(final float[] clearColor)
float[4]
Definition: GearsES2.java:113
void setUseMappedBuffers(final boolean v)
Definition: GearsES2.java:161
GearsES2(final int swapInterval)
Definition: GearsES2.java:99
void setValidateBuffers(final boolean v)
Definition: GearsES2.java:162
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
Definition: GearsES2.java:422
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
Definition: GearsES2.java:186
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
Definition: GearsES2.java:385
void setClearBuffers(final boolean v)
Definition: GearsES2.java:108
static void main(final String[] args)
Definition: GearsES2.java:610
boolean waitForInit(final boolean initialized)
Definition: GearsES2.java:175
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...
Definition: GearsES2.java:342
void setZ(final float zNear, final float zFar, final float zViewDist)
Definition: GearsES2.java:281
void addPanning(final float x, final float y, final float z)
Definition: GearsES2.java:144
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.
Definition: GearsES2.java:291
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.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
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 glScalef(final float x, final float y, final float z)
Scale the current matrix.
Definition: PMVMatrix.java:396
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
Definition: PMVMatrix.java:218
final void glPushMatrix()
Push the current matrix to it's stack, while preserving it's values.
Definition: PMVMatrix.java:458
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 glPopMatrix()
Pop the current matrix from it's stack.
Definition: PMVMatrix.java:447
final void glLoadIdentity()
Load the current matrix with the identity matrix.
Definition: PMVMatrix.java:325
final void glLoadMatrixf(final float[] values, final int offset)
Load the current matrix w/ the provided one.
Definition: PMVMatrix.java:271
Custom GLCapabilitiesChooser, filtering out all full screen anti-aliasing (FSAA, multisample) capabil...
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 ...
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.
Definition: ViewerPose.java:36
final Quaternion orientation
Orientation of viewer.
Definition: ViewerPose.java:50
final Vec3f position
position of viewer in meter.
Definition: ViewerPose.java:47
Specifying NEWT's Window functionality:
Definition: Window.java:115
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.
Generic gesture handler interface designed to allow pass-through filtering of InputEvents.
Listener for KeyEvents.
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:585
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...
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_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expression '0x0B71',...
Definition: GL.java:43
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 '...
Definition: GL.java:60
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...
Definition: GL.java:738
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',...
Definition: GL.java:720
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...