summaryrefslogtreecommitdiffstats
path: root/o3d/samples/manipulators/translate2.html
blob: a284f89118e8b1a97390a8f48ef02ae5989002ad (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
<!--
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.
-->

<!--
Translate2 manipulator

This sample shows how to use the o3djs.manipulators.Translate2 class
to interactively drag objects around the scene.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Translate2 Manipulator
</title>
<script type="text/javascript" src="../o3djs/base.js"></script>
<script type="text/javascript" id="o3dscript">
o3djs.require('o3djs.util');
o3djs.require('o3djs.math');
o3djs.require('o3djs.quaternions');
o3djs.require('o3djs.rendergraph');
o3djs.require('o3djs.primitives');
o3djs.require('o3djs.manipulators');
o3djs.require('o3djs.effect');
o3djs.require('o3djs.cameracontroller');

// global variables
var g_o3dElement;
var g_client;
var g_o3d;
var g_math;
var g_mainPack;
var g_mainViewInfo;
var g_mainRoot;
var g_lightPosition = [5, 5, 7];
var g_lastCubeTransform;
var g_primitives = [];
var g_manager;
var g_cameraController;

/**
 * Creates the client area.
 */
function initClient() {
  window.g_finished = false;  // for selenium testing.

  // Runs the sample in V8. Comment out this line to run it in the browser
  // JavaScript engine, for example if you want to debug it.
  // TODO(kbr): we need to investigate why turning this on is
  //     significantly slower than leaving it disabled.
  // o3djs.util.setMainEngine(o3djs.util.Engine.V8);

  o3djs.util.makeClients(main);
}

/**
 * Initializes global variables, positions camera, draws shapes.
 * @param {Array} clientElements Array of o3d object elements.
 */
function main(clientElements) {
  var o3dElement = clientElements[0];

  // Init global variables.
  initGlobals(clientElements);

  // Add the shapes to the transform heirarchy.
  createShapes();

  // Add the manipulators to the transform hierarchy.
  createManipulators();

  // Set up the view and projection transformations.
  initContext();

  // Start picking; it won't do anything until the scene finishes loading.
  o3djs.event.addEventListener(o3dElement, 'mousedown', onMouseDown);
  o3djs.event.addEventListener(o3dElement, 'mousemove', onMouseMove);
  o3djs.event.addEventListener(o3dElement, 'mouseup', onMouseUp);

  window.g_finished = true;  // for selenium testing.
}

function onMouseDown(e) {
  if(e.button == 2) {
    g_cameraController.mousedown(e);
  } else {
    g_manager.mousedown(e.x, e.y,
                        g_mainViewInfo.drawContext.view,
                        g_mainViewInfo.drawContext.projection,
                        g_client.width,
                        g_client.height);
  }
}

function onMouseMove(e) {
  g_cameraController.mousemove(e);
  g_mainViewInfo.drawContext.view = g_cameraController.calculateViewMatrix();
  g_manager.mousemove(e.x, e.y,
                      g_mainViewInfo.drawContext.view,
                      g_mainViewInfo.drawContext.projection,
                      g_client.width,
                      g_client.height);
  g_manager.updateInactiveManipulators();
}

function onMouseUp(e) {
  if(e.button == 2) {
    g_cameraController.mouseup(e);
  } else {
    g_manager.mouseup();
    g_manager.updateInactiveManipulators();
  }
}

/**
 * Initializes global variables and libraries.
 */
function initGlobals(clientElements) {
  g_o3dElement = clientElements[0];
  window.g_client = g_client = g_o3dElement.client;
  g_o3d = g_o3dElement.o3d;
  g_math = o3djs.math;

  // Create a pack to manage the objects created.
  g_mainPack = g_client.createPack();

  // Make a root transform for our scene.
  g_mainRoot = g_mainPack.createObject('Transform');

  // Create the render graph for the scene view.
  g_mainViewInfo = o3djs.rendergraph.createBasicView(
      g_mainPack,
      g_mainRoot,
      g_client.renderGraphRoot);
}

/**
 * Sets up reasonable view and projection matrices.
 */
function initContext() {
  // Set up a perspective transformation for the projection.
  g_mainViewInfo.drawContext.projection = g_math.matrix4.perspective(
      g_math.degToRad(30),  // 30 degree frustum.
      g_o3dElement.clientWidth / g_o3dElement.clientHeight, // Aspect ratio.
      1,                    // Near plane.
      5000);                // Far plane.

  // Set up our view transformation using our CameraController.
  g_cameraController = o3djs.cameracontroller.createCameraController(
      [0, 2, 0], // centerPos
      23,        // backpedal
      0,         // heightAngle
      0);        // rotationAngle
  g_mainViewInfo.drawContext.view = g_cameraController.calculateViewMatrix();
}

/**
 * Creates shapes using the primitives utility library, and adds them to the
 * transform graph at the root node.
 */
function createShapes() {
  // Create a little tree-like hierarchy of cubes
  createCubeTree(2, 1.5, [0, 0, 0], g_mainRoot);
  g_lastCubeTransform.scale(3, 1, 1);
}

/**
 * Creates a small tree of cubes to demonstrate interaction with a
 * hierarchy of shapes.
 */
function createCubeTree(depth, edgeLength, translation, parent) {
  var cur = createCube(edgeLength, translation, parent);
  if (depth > 0) {
    createCubeTree(depth - 1,
                   edgeLength / 1.5,
                   o3djs.math.addVector(translation,
                                        [-1.2 * edgeLength,
                                          1.0 * edgeLength,
                                          0]),
                   cur);
    createCubeTree(depth - 1,
                   edgeLength / 1.5,
                   o3djs.math.addVector(translation,
                                        [1.2 * edgeLength,
                                         1.0 * edgeLength,
                                         0]),
                   cur);
  }
  return cur;
}

/**
 * Creates a cube shape using the primitives utility library, with an
 * optional translation and parent. Returns the newly-created
 * transform for the cube.
 */
function createCube(edgeLength, opt_translation, opt_parent) {
  var cube = o3djs.primitives.createCube(
      g_mainPack,
      // A green phong-shaded material.
      o3djs.material.createBasicMaterial(g_mainPack,
                                         g_mainViewInfo,
                                         [0, 1, 0, 1]),
      edgeLength);
  var transform = g_mainPack.createObject('Transform');
  g_lastCubeTransform = transform;
  transform.addShape(cube);
  if (opt_translation) {
    transform.translate(opt_translation);
  }
  if (opt_parent) {
    transform.parent = opt_parent;
  } else {
    transform.parent = g_mainRoot;
  }
  g_primitives.push(transform);
  return transform;
}

/**
 * Creates manipulators attached to the objects we've just created.
 */
function createManipulators() {
  // Create a separate pack for the manipulators so they don't get mixed in
  // with the main scene's objects.
  var pack = g_client.createPack();

  // Create a root transform for the manipulators so they are 100% separate
  // from the scene's transforms.
  var manipulatorRoot = pack.createObject('Transform');

  g_manager = o3djs.manipulators.createManager(
      pack,
      manipulatorRoot,
      g_client.renderGraphRoot,
      g_mainViewInfo.root.priority + 1,
      g_mainViewInfo.drawContext);

  var jj = 2;
  for (var ii = 0; ii < g_primitives.length; ii++) {
    var manip = g_manager.createTranslate2();
    manip.attachTo(g_primitives[ii]);
    manip.setOffsetTranslation([0, -1.5, 0]);
    // Demonstrate that we can drag along arbitrary directions.
    if ((++jj % 4) == 0) {
      manip.setOffsetRotation(o3djs.quaternions.rotationY(Math.PI / 4));
    }
  }
}

/**
 * Removes any callbacks so they don't get called after the page has unloaded.
 */
function unload() {
  if (g_client) {
    g_client.cleanup();
  }
}
</script>
</head>
<body onload="initClient()" onunload="unload()">
<h1>Translate2 Manipulator Sample</h1>
This example shows how to move objects around the scene using the
Translate2 manipulator.
<br/>
<!-- Start of O3D plugin -->
<div id="o3d" style="width: 600px; height: 600px;"></div>
<!-- End of O3D plugin -->
</body>
</html>