summaryrefslogtreecommitdiffstats
path: root/o3d/samples/waterdemo/cameracontrol.js
blob: 271c863ec1f231bc979a419d0be0eb70aed50314 (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
/*
 * Copyright 2009, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


/**
 * @fileoverview This file contains functions used controlling the camera.
 *
 */

 /**
  * Clamps a value between bounds.
  *
  * @param {number} inputValue Value to be clamped.
  * @param {number} lowerBound Lower limit.
  * @param {number} upperBound Upper limit.
  * @return {number} The clamped value.
  */
function clamp(inputValue, lowerBound, upperBound) {
  var resultValue = inputValue;
  if (inputValue < lowerBound) resultValue = lowerBound;
  if (inputValue > upperBound) resultValue = upperBound;
  return resultValue;
}

/**
 * Functions for control based on the mouse
 */
var dragging = false;
var g_rotX = 2.85;
var g_rotY = 0;
var g_thisClick;
var g_lastClick;
var g_cameraZoom = 1.0;

function startDragging(e) {
  g_thisClick = e;

  dragging = true;
}

/**
 * Scroll wheel handler. Adjusts g_cameraZoom global according to wheel
 * movement then causes a recalculation of the projection matrix.
 * The goal is to simulate a zoom lens rather than to move the viewpoint.
 * @param {Event} e Wheel event to handle.
 */
function scrollMe(e) {
  if (e.deltaY) {
    g_cameraZoom = clamp(g_cameraZoom * ((e.deltaY < 0 ? 13 : 11) / 12),
                         0.1,
                         4.0);
    resize();
  }
}

function drag(e) {
  if (dragging) {
    var scale = -.001 * g_cameraZoom;

    g_lastClick = g_thisClick;
    g_thisClick = e;

    var xLowerLimit = 2.3;
    var xUpperLimit = 4.2;
    var yLowerLimit = -0.6;
    var yUpperLimit = 0.25;

    g_rotX = clamp(g_rotX - scale * (g_thisClick.x - g_lastClick.x),
                   xLowerLimit, xUpperLimit);
    g_rotY = clamp(g_rotY + scale * (g_thisClick.y - g_lastClick.y),
                   yLowerLimit, yUpperLimit);

    setViewFromRotation();
  }
}

function setViewFromRotation() {
  var rotMatX = g_math.matrix4.rotationY(g_rotX);
  var right = g_math.matrix4.transformDirection(rotMatX, [0, 0, 1]);
  var camera_X = g_math.normalize(g_math.cross([0, 1, 0], right));
  var rotMatY = g_math.matrix4.axisRotation(camera_X, g_rotY);
  var rot_mat = g_math.matrix4.mul(rotMatX, rotMatY);
  g_root.identity();

  // TODO: eye position should be a variable
  var eye = [-2.751, 3.529, -8.563];
  g_viewInfo.drawContext.view = g_math.matrix4.lookAt(
      eye,
      g_math.addVector(eye, g_math.mulVectorMatrix([0, 0, 1, 0], rot_mat)),
      g_math.mulVectorMatrix([0, 1, 0, 0], rot_mat));
}

function stopDragging(e) {
  dragging = false;
}