summaryrefslogtreecommitdiffstats
path: root/src/gleem/ExaminerViewer.java
blob: e88ecf7ec09491aa54618955525488d03eb47a79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
 * gleem -- OpenGL Extremely Easy-To-Use Manipulators.
 * Copyright (C) 1998-2003 Kenneth B. Russell (kbrussel@alum.mit.edu)
 *
 * Copying, distribution and use of this software in source and binary
 * forms, with or without modification, is permitted provided that the
 * following conditions are met:
 *
 * Distributions of source code must reproduce the copyright notice,
 * this list of conditions and the following disclaimer in the source
 * code header files; and Distributions of binary code must reproduce
 * the copyright notice, this list of conditions and the following
 * disclaimer in the documentation, Read me file, license file and/or
 * other materials provided with the software distribution.
 *
 * The names of Sun Microsystems, Inc. ("Sun") and/or the copyright
 * holder may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF ANY
 * KIND. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, NON-INTERFERENCE, ACCURACY OF
 * INFORMATIONAL CONTENT OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. THE
 * COPYRIGHT HOLDER, SUN AND SUN'S LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL THE
 * COPYRIGHT HOLDER, SUN OR SUN'S LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGES. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT
 * DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION,
 * OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY. THE COPYRIGHT
 * HOLDER, SUN AND SUN'S LICENSORS DISCLAIM ANY EXPRESS OR IMPLIED
 * WARRANTY OF FITNESS FOR SUCH USES.
 */

package gleem;

import gleem.linalg.Mat4f;
import gleem.linalg.MathUtil;
import gleem.linalg.Rotf;
import gleem.linalg.Vec3f;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.AWTGLAutoDrawable;

/** <P> This is an application-level class, not part of the
    manipulator hierarchy. It is an example of how you might integrate
    gleem with another application which uses the mouse. </P>

    <P> For the given GLAutoDrawable, the ExaminerViewer takes over the
    setting of the view position. It passes along mouse events it is
    not interested in to the ManipManager's mouse routines. </P>

    <P> The ExaminerViewer's controls are similar to those of Open
    Inventor's Examiner Viewer. Alt + Left mouse button causes
    rotation about the focal point. Alt + Right mouse button causes
    translation parallel to the image plane. Alt + both mouse buttons,
    combined with up/down mouse motion, causes zooming out and in
    along the view vector. (On platforms with a "Meta" key, that key
    can be substituted in place of the Alt key.) The method
    <code>setNoAltKeyMode</code> can be used to cause the
    ExaminerViewer to take control of all mouse interactions in the
    window, avoiding the need to hold down the Alt key. </P>

    <P>NOTE: the current ExaminerViewer implementation assumes a
    minimum of two mouse buttons. For the Mac OS, the code needs to be
    adjusted to use e.g., the Control key as the "right" mouse
    button. </P> */

public class ExaminerViewer {
  private AWTGLAutoDrawable window;
  /** Simple state machine for figuring out whether we are grabbing
      events */
  private boolean interactionUnderway;
  private boolean iOwnInteraction;

  private boolean noAltKeyMode;
  private boolean autoRedrawMode = true;

  /** Simple state machine for computing distance dragged */
  private boolean button1Down;
  private boolean button2Down;
  private boolean button3Down;
  private int lastX;
  private int lastY;

  /** Camera parameters */
  private final float minFocalDist = 1.0f;
  private final Vec3f dolly        = new Vec3f(0, 0, 10); // Amount we have "backed up" from focal point
  private final Vec3f center       = new Vec3f(0, 0,  0); // Position of focal point in world coordinates
  private Rotf  orientation  = new Rotf();
  private Vec3f upVector     = null;
  private float rotateSpeed       = 1.0f;
  private final float minRotateSpeed    = 0.0001f;
  private float dollySpeed        = 2.0f;
  private final float minDollySpeed     = 0.0001f;
  private float zNear             = 1.0f;
  private float zFar              = 100.0f;
  private float vertFOVScale      = 1.0f;
  private final CameraParameters params = new CameraParameters();

  /** Our bounding sphere provider (for viewAll()) */
  private BSphereProvider provider;

  private final MouseMotionAdapter mouseMotionListener = new MouseMotionAdapter() {
      @Override
	public void mouseDragged(MouseEvent e) {
        motionMethod(e, e.getX(), e.getY());
      }

      @Override
	public void mouseMoved(MouseEvent e) {
        if (interactionUnderway && iOwnInteraction) {
          // Hack for AWT behavior where Ctrl + Alt + Left mouse button is treated
          // as motion instead of drag
          motionMethod(e, e.getX(), e.getY());
        } else {
          passiveMotionMethod(e);
        }
      }
    };

  private final MouseAdapter mouseListener = new MouseAdapter() {
      @Override
	public void mousePressed(MouseEvent e) {
        mouseMethod(e, e.getModifiersEx(), true, e.getX(), e.getY());
      }

      @Override
	public void mouseReleased(MouseEvent e) {
        mouseMethod(e, e.getModifiersEx(), false, e.getX(), e.getY());
      }
    };

  private final GLEventListener glListener = new GLEventListener() {
      @Override
	public void init(GLAutoDrawable drawable) {}
      @Override
	public void display(GLAutoDrawable drawable) {}
      @Override
	public void dispose(GLAutoDrawable drawable) { }
      @Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        reshapeMethod(width, height);
      }
      public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
    };

  public ExaminerViewer() {
  }

  /** <P> Attaches this ExaminerViewer to the given GLAutoDrawable. This
      causes the ManipManager's mouse routines to be removed from the
      window (using ManipManager.removeMouseListeners) and the
      ExaminerViewer's to be installed. The GLAutoDrawable should be
      registered with the ManipManager before the ExaminerViewer is
      attached to it. </P>

      <P> In order for the viewer to do anything useful, you need to
      provide a BSphereProvider to it to allow "view all"
      functionality. </P> */
  public void attach(AWTGLAutoDrawable window, BSphereProvider provider) {
    this.window = window;
    this.provider = provider;
    init();
    setupListeners();
  }

  /** Detaches from the given window. This causes the ManipManager's
      mouse listeners to be reinstalled on the GLAutoDrawable and the
      ExaminerViewer's to be removed. */
  public void detach() {
    removeListeners();
    this.window = null;
    this.provider = null;
  }

  /** Call this at the end of your display() method to cause the
      Modelview matrix to be recomputed for the next frame. */
  public void update(GL2 gl) {
    recalc(gl);
  }

  /** Call this to apply the inverse rotation matrix of the camera to
      the current matrix. This is useful for drawing a skybox. Does
      not update which OpenGL matrix is currently being modified or
      the ExaminerViewer's camera parameters. */
  public void updateInverseRotation(GL2 gl) {
    recalcInverseRotation(gl);
  }

  /** Call this to force the ExaminerViewer to update its
      CameraParameters without touching the OpenGL state. */
  public void update() {
    recalc();
  }

  /** Call this from within your display() method to cause the
      ExaminerViewer to recompute its position based on the visible
      geometry. A BSphereProvider must have already been set or this
      method has no effect. */
  public void viewAll(GL2 gl) {
    if (provider == null) {
      return;
    }
    // Figure out how far to move
    float vertFOV, horizFOV, minFOV;
    float adjustedVertFOV = params.getVertFOV() * vertFOVScale;
    vertFOV = 2.0f * adjustedVertFOV;
    horizFOV = 2.0f * (float) Math.atan(params.getImagePlaneAspectRatio() *
                                        Math.tan(adjustedVertFOV));
    if (vertFOV < horizFOV)
      minFOV = vertFOV;
    else
      minFOV = horizFOV;
    if (minFOV == 0.0f) {
      throw new RuntimeException("Minimum field of view was zero");
    }
    BSphere bsph = provider.getBoundingSphere();
    float dist = bsph.getRadius() / (float) Math.sin(minFOV / 2.0f);
    dolly.setZ(dist);
    center.set(bsph.getCenter());
    recalc(gl);
  }

  /** Get the camera parameters out of this Examiner Viewer (for
      example, to pass to ManipManager.updateCameraParameters()). Note
      that mutating the returned object is not recommended but
      regardless will have no effect on the ExaminerViewer. */
  public CameraParameters getCameraParameters() {
    return params;
  }

  /** These routines can be hooked into a GUI by calling them from
      ActionEvent listeners for buttons elsewhere in the application. */
  public void rotateFaster() {
    rotateSpeed *= 2.0f;
  }

  public void rotateSlower() {
    if (rotateSpeed < minRotateSpeed)
      return;
    else
      rotateSpeed /= 2.0f;
  }

  public void dollyFaster() {
    dollySpeed *= 2.0f;
  }

  public void dollySlower() {
    if (dollySpeed < minDollySpeed)
      return;
    else
      dollySpeed /= 2.0f;
  }

  public float getZNear() {
    return zNear;
  }

  public void setZNear(float zNear) {
    this.zNear = zNear;
  }

  public float getZFar() {
    return zFar;
  }

  public void setZFar(float zFar) {
    this.zFar = zFar;
  }

  /** Takes HALF of the vertical angular span of the frustum,
      specified in radians. For example, if your <b>fovy</b> argument
      to gluPerspective() is 90, then this would be Math.PI / 4. Note
      that the ExaminerViewer's algorithms break down if the vertical
      field of view approaches or exceeds 180 degrees, or Math.PI /
      2. */
  public void setVertFOV(float vertFOV) {
    vertFOVScale = (float) (vertFOV / (Math.PI / 4));
  }

  /** Sets the position of this ExaminerViewer. */
  public void setPosition(Vec3f position) {
    Vec3f tmp = orientation.rotateVector(Vec3f.NEG_Z_AXIS);
    tmp.scale(dolly.z());
    center.add(position, tmp);
  }

  /** Sets the orientation of this ExaminerViewer. */
  public void setOrientation(Rotf orientation) {
    this.orientation.set(orientation);
  }

  /** Sets the preferred up vector of this ExaminerViewer. The default
      is null. */
  public void setUpVector(Vec3f up) {
    if (up == null) {
      upVector = null;
    } else {
      upVector = up.copy();
    }
  }

  public void setNoAltKeyMode(boolean noAltKeyMode) {
    this.noAltKeyMode = noAltKeyMode;
  }

  public boolean getNoAltKeyMode() {
    return noAltKeyMode;
  }

  /** Enables or disables the automatic redrawing of the
      GLAutoDrawable to which this ExaminerViewer is attached. If the
      GLAutoDrawable is already being animated, disabling auto redraw
      mode may provide better performance. Defaults to on. */
  public void setAutoRedrawMode(boolean onOrOff) {
    autoRedrawMode = onOrOff;
  }

  /** Returns whether this ExaminerViewer automatically redraws the
      GLAutoDrawable to which it is attached upon updates. */
  public boolean getAutoRedrawMode() {
    return autoRedrawMode;
  }

  /** Rotates this ExaminerViewer about the focal point by the
      specified incremental rotation; performs postmultiplication,
      i.e. the incremental rotation is applied after the current
      orientation. */
  public void rotateAboutFocalPoint(Rotf rot) {
    orientation = rot.times(orientation);
    orientation.normalize();
  }

  //----------------------------------------------------------------------
  // Internals only below this point
  //

  private static final float EPSILON = 0.0001f;

  private void setupListeners() {
    ManipManager.getManipManager().removeMouseListeners(window);
    window.addMouseMotionListener(mouseMotionListener);
    window.addMouseListener(mouseListener);
    window.addGLEventListener(glListener);
  }

  private void removeListeners() {
    if (window != null) {
      window.removeMouseMotionListener(mouseMotionListener);
      window.removeMouseListener(mouseListener);
      window.removeGLEventListener(glListener);
      ManipManager.getManipManager().setupMouseListeners(window);
    }
  }

  private void passiveMotionMethod(MouseEvent e) {
    ManipManager.getManipManager().mouseMoved(e);
  }

  private boolean modifiersMatch(MouseEvent e, int mods) {
    if (noAltKeyMode) {
      if ((mods & MouseEvent.BUTTON1_DOWN_MASK) != 0 &&
          (mods & MouseEvent.BUTTON2_DOWN_MASK) == 0 &&
          (mods & MouseEvent.BUTTON3_DOWN_MASK) == 0) {
        return (!e.isAltDown() && !e.isMetaDown() && !e.isControlDown() && !e.isShiftDown());
      } else {
        // At least on Windows, meta seems to be declared to be down on right button presses
        return !e.isControlDown() && !e.isShiftDown();
      }
    } else {
      return (e.isAltDown() || e.isMetaDown());
    }
  }

  private void init() {
    interactionUnderway = false;
    iOwnInteraction = false;
    button1Down = false;
    button2Down = false;

    int xSize = window.getSurfaceWidth();
    int ySize = window.getSurfaceHeight();
    params.setOrientation(orientation);
    params.setPosition(computePosition(new Vec3f()));
    params.setForwardDirection(Vec3f.NEG_Z_AXIS);
    params.setUpDirection(Vec3f.Y_AXIS);
    params.setVertFOV((float) Math.PI / 8.0f);
    params.setImagePlaneAspectRatio((float) xSize / (float) ySize);
    params.setXSize(xSize);
    params.setYSize(ySize);
  }

  private void motionMethod(MouseEvent e, int x, int y) {
    if (interactionUnderway && !iOwnInteraction) {
      ManipManager.getManipManager().mouseDragged(e);
    } else {
      int dx = x - lastX;
      int dy = y - lastY;

      lastX = x;
      lastY = y;

      // The intent is to match Maya's camera controls, which are the de facto standard:
      //   - Alt + Left   = rotation
      //   - Alt + Right  = zoom
      //   - Alt + Middle = translation
      // With the hack that Ctrl + Alt + Right on a trackpad be the same as translation,
      // so that gesture can be done without a 3-button mouse hooked up.

      boolean doRotation =
        (button1Down && !button2Down && !button3Down);
      boolean doTranslation =
        (button2Down && !button1Down && !button3Down);
      boolean doZoom =
        (button3Down && !button1Down && !button2Down);

      // Hack to allow us to use Ctrl + Alt + LMB to translate so
      // that we can do that gesture on the trackpad
      if (e.isControlDown() && (doRotation || doZoom ||
                                (button1Down && button2Down))) {
        doRotation = false;
        doZoom = false;
        doTranslation = true;
      }

      if (doRotation) {

        // Rotation functionality
        float xRads = (float) Math.PI * -1.0f * dy * rotateSpeed / 1000.0f;
        float yRads = (float) Math.PI * -1.0f * dx * rotateSpeed / 1000.0f;
        Rotf xRot = new Rotf(Vec3f.X_AXIS, xRads);
        Rotf yRot = new Rotf(Vec3f.Y_AXIS, yRads);
        Rotf newRot = yRot.times(xRot);
        orientation = orientation.times(newRot);

        if (upVector != null) {
          // FIXME: has degenerate behavior when pointing parallel to up vector
          Vec3f cameraUp = orientation.rotateVector(Vec3f.Y_AXIS);
          float dotp = cameraUp.dot(upVector);
          if (Math.abs(dotp) > MathUtil.ZERO_TOLERANCE) {
            // Form orthonormal basis
            Vec3f back = orientation.rotateVector(Vec3f.Z_AXIS);
            Vec3f right = upVector.cross(back);
            right.normalize();
            cameraUp.cross(back, right);
            Mat4f mat = new Mat4f();
            mat.setRotation(right, cameraUp, back);
            orientation.fromMatrix(mat);
          }
        }

      } else if (doTranslation) {

        // Translate functionality
        // Compute the local coordinate system's difference vector
        Vec3f localDiff = new Vec3f(dollySpeed * -1.0f * dx / 100.0f,
                                    dollySpeed * dy / 100.0f,
                                    0.0f);
        // Rotate this by camera's orientation
        Vec3f worldDiff = orientation.rotateVector(localDiff);
        // Add on to center
        center.add(worldDiff);

      } else if (doZoom) {

        // FIXME: implement this in terms of mouse wheel
        float diff = dollySpeed * (-1.0f * dy - dx) / 100.0f;
        float newDolly = dolly.z() + diff;
        if (newDolly < minFocalDist) {
          newDolly = minFocalDist;
        }
        dolly.setZ(newDolly);

      }

      if (autoRedrawMode) {
        // Force redraw
        window.repaint();
      }
    }
  }

  private void mouseMethod(MouseEvent e, int mods, boolean press,
                           int x, int y) {
    if ((interactionUnderway && !iOwnInteraction) ||
        (!modifiersMatch(e, mods))) {
      // Update state and pass this event along to the ManipManager
      if (press) {
        interactionUnderway = true;
        iOwnInteraction = false;
        ManipManager.getManipManager().mousePressed(e);
      } else {
        interactionUnderway = false;
        iOwnInteraction = false;
        ManipManager.getManipManager().mouseReleased(e);
      }
    } else {
      if ((mods & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
        button1Down = true;
      } else {
        button1Down = false;
      }
      if ((mods & MouseEvent.BUTTON2_DOWN_MASK) != 0) {
        button2Down = true;
      } else {
        button2Down = false;
      }
      if ((mods & MouseEvent.BUTTON3_DOWN_MASK) != 0) {
        button3Down = true;
      } else {
        button3Down = false;
      }

      lastX = x;
      lastY = y;

      if (button1Down || button2Down || button3Down) {
        interactionUnderway = true;
        iOwnInteraction = true;
      } else {
        interactionUnderway = false;
        iOwnInteraction = false;
      }

      if (autoRedrawMode) {
        // Force redraw
        window.repaint();
      }
    }
  }

  private void reshapeMethod(int w, int h) {
    float aspect, theta;
    aspect = (float) w / (float) h;
    if (w >= h)
      theta = 45;
    else
      theta = (float) Math.toDegrees(Math.atan(1 / aspect));
    theta *= vertFOVScale;
    params.setVertFOV((float) (Math.toRadians(theta) / 2.0));
    params.setImagePlaneAspectRatio(aspect);
    params.setXSize(w);
    params.setYSize(h);
  }

  private void recalc() {
    // Recompute position, forward and up vectors
    Vec3f tmp = new Vec3f();
    params.setPosition(computePosition(tmp));
    orientation.rotateVector(Vec3f.NEG_Z_AXIS, tmp);
    params.setForwardDirection(tmp);
    orientation.rotateVector(Vec3f.Y_AXIS, tmp);
    params.setUpDirection(tmp);
    params.setOrientation(orientation);

    // Compute modelview matrix based on camera parameters, position and
    // orientation
    Mat4f tmpMat = new Mat4f();
    tmpMat.makeIdent();
    tmpMat.setRotation(orientation);
    tmpMat.setTranslation(params.getPosition());
    tmpMat.invertRigid();
    params.setModelviewMatrix(tmpMat);

    // Compute perspective matrix given camera parameters
    float deltaZ = zFar - zNear;
    float aspect = params.getImagePlaneAspectRatio();
    float radians = params.getVertFOV();
    float sine = (float) Math.sin(radians);
    if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
      tmpMat.makeIdent();
      params.setProjectionMatrix(tmpMat);
      return;
    }
    float cotangent = (float) Math.cos(radians) / sine;
    tmpMat.makeIdent();
    tmpMat.set(0, 0, cotangent / aspect);
    tmpMat.set(1, 1, cotangent);
    tmpMat.set(2, 2, -(zFar + zNear) / deltaZ);
    tmpMat.set(3, 2, -1);
    tmpMat.set(2, 3, -2 * zNear * zFar / deltaZ);
    tmpMat.set(3, 3, 0);
    params.setProjectionMatrix(tmpMat);


    /********************

    // Recompute position, forward and up vectors
    params.setPosition(position);
    Vec3f tmp = new Vec3f();
    orientation.rotateVector(Vec3f.NEG_Z_AXIS, tmp);
    params.setForwardDirection(tmp);
    orientation.rotateVector(Vec3f.Y_AXIS, tmp);
    params.setUpDirection(tmp);
    params.setOrientation(orientation);

    // Compute modelview matrix based on camera parameters, position and
    // orientation
    Mat4f tmpMat = new Mat4f();
    tmpMat.makeIdent();
    tmpMat.setRotation(orientation);
    tmpMat.setTranslation(position);
    tmpMat.invertRigid();
    params.setModelviewMatrix(tmpMat);

    // Compute perspective matrix given camera parameters
    float deltaZ = zFar - zNear;
    float aspect = params.getImagePlaneAspectRatio();
    float radians = params.getVertFOV();
    float sine = (float) Math.sin(radians);
    if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
      tmpMat.makeIdent();
      params.setProjectionMatrix(tmpMat);
      return;
    }
    float cotangent = (float) Math.cos(radians) / sine;
    tmpMat.makeIdent();
    tmpMat.set(0, 0, cotangent / aspect);
    tmpMat.set(1, 1, cotangent);
    tmpMat.set(2, 2, -(zFar + zNear) / deltaZ);
    tmpMat.set(3, 2, -1);
    tmpMat.set(2, 3, -2 * zNear * zFar / deltaZ);
    tmpMat.set(3, 3, 0);
    params.setProjectionMatrix(tmpMat);

    **********************/
  }

  private void recalc(GL2 gl) {
    recalc();

    gl.glMatrixMode(GL2ES1.GL_MODELVIEW);
    float[] data = new float[16];
    params.getModelviewMatrix().getColumnMajorData(data);
    gl.glLoadMatrixf(data, 0);

    gl.glMatrixMode(GL2ES1.GL_PROJECTION);
    params.getProjectionMatrix().getColumnMajorData(data);
    gl.glLoadMatrixf(data, 0);
  }

  private void recalcInverseRotation(GL2 gl) {
    Rotf oriInv = orientation.inverse();
    Vec3f tmp = new Vec3f();
    float ang = orientation.get(tmp);
    if (tmp.lengthSquared() > EPSILON)
      gl.glRotatef((float) Math.toDegrees(ang), tmp.x(), tmp.y(), tmp.z());
  }

  private Vec3f computePosition(Vec3f tmp) {
    orientation.rotateVector(dolly, tmp);
    tmp.add(center);
    return tmp;
  }
}