Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(522)

Side by Side Diff: samples/gpu2d/svgsample.js

Issue 652016: Added the bulk of the algorithm for GPU accelerated 2D vector curve... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/o3d/
Patch Set: '' Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/gpu2d/svgloader.js ('k') | samples/gpu2d/thincrescent.svg » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright 2010, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /**
33 * @fileoverview Framework for samples that load and display SVG files.
34 *
35 * This is purely *example* code, showing how to use the SVG loader.
36 */
37
38 o3djs.require('o3djs.cameracontroller');
39 o3djs.require('o3djs.event');
40 o3djs.require('o3djs.gpu2d');
41 o3djs.require('o3djs.math');
42 o3djs.require('o3djs.picking');
43 o3djs.require('o3djs.rendergraph');
44 o3djs.require('o3djs.util');
45 // Also requires the following files to be loaded by the containing page:
46 // - svgloader.js
47 // - ../third_party/xmljs/tinyxmlsax.js
48
49 // Globals
50 var g_filename;
51 var g_o3d;
52 var g_math;
53 var g_client;
54 var g_pack;
55 var g_viewInfo;
56 var g_o3dElement;
57 var g_finished = false; // for selenium testing.
58 var g_clientWidth;
59 var g_clientHeight;
60 var g_cameraController;
61
62 if (!("console" in window)) {
63 var logElement = document.getElementById('log');
64 window.console =
65 { log: function(s) {
66 if (logElement) {
67 logElement.innerHTML = logElement.innerHTML + "<span>" +
68 s.toString() + "</span><br>";
69 }
70 }
71 };
72 }
73
74 /**
75 * Initializes the sample with the given URL.
76 * @param {string} filename The URL of the SVG file to load.
77 */
78 function init(filename) {
79 g_filename = filename;
80 o3djs.util.makeClients(initStep2);
81 }
82
83 /**
84 * Remove any callbacks so they don't get called after the page has unloaded.
85 */
86 function unload() {
87 if (g_client) {
88 g_client.cleanup();
89 }
90 }
91
92 /**
93 * Completes initialization of the sample.
94 * @param {!Array.<!Element>} clientElements Array of o3d object elements.
95 */
96 function initStep2(clientElements) {
97 // Initializes global variables and libraries.
98 var o3dElement = clientElements[0];
99 g_o3dElement = o3dElement;
100 g_o3d = o3dElement.o3d;
101 g_math = o3djs.math;
102 g_client = o3dElement.client;
103
104 // Creates a pack to manage our resources/assets
105 g_pack = g_client.createPack();
106
107 g_viewInfo = o3djs.rendergraph.createBasicView(
108 g_pack,
109 g_client.root,
110 g_client.renderGraphRoot);
111
112 // Set the background color to light gray.
113 g_viewInfo.clearBuffer.clearColor = [0.8, 0.8, 0.8, 1];
114
115 // Load the file.
116 var loader = new SVGLoader();
117 loader.load(g_filename,
118 true,
119 g_pack,
120 g_viewInfo.zOrderedDrawList,
121 g_client.root,
122 function(url, success, detail) {
123 if (!success) {
124 window.console.log('Failed to load ' + url + ": " + detail);
125 } else {
126 var tmpManager =
127 o3djs.picking.createPickManager(g_client.root);
128 tmpManager.update();
129 var bbox = tmpManager.getTransformInfo(g_client.root).
130 getBoundingBox();
131 g_cameraController.viewAll(bbox,
132 g_client.width / g_client.height);
133 updateViewAndProjectionMatrices();
134 }
135 });
136
137 // Set up the view and projection transformations.
138 initContext();
139
140 // Set up event handlers for mouse interaction.
141 o3djs.event.addEventListener(o3dElement, 'mousedown', onMouseDown);
142 o3djs.event.addEventListener(o3dElement, 'mousemove', onMouseMove);
143 o3djs.event.addEventListener(o3dElement, 'mouseup', onMouseUp);
144
145 g_finished = true; // for selenium testing.
146 }
147
148 /**
149 * Event handler that gets called when a mouse click takes place in
150 * the O3D element. It changes the state of the camera controller
151 * based on which modifier keys are pressed.
152 * @param {!Event} e The mouse down event.
153 */
154 function onMouseDown(e) {
155 if (e.button == 0) {
156 if (!e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey) {
157 g_cameraController.setDragMode(
158 o3djs.cameracontroller.DragMode.MOVE_CENTER_IN_VIEW_PLANE, e.x, e.y);
159 } else if (e.metaKey || e.altKey) {
160 g_cameraController.setDragMode(
161 o3djs.cameracontroller.DragMode.SPIN_ABOUT_CENTER, e.x, e.y);
162 } else if (!e.shiftKey && e.ctrlKey) {
163 g_cameraController.setDragMode(
164 o3djs.cameracontroller.DragMode.DOLLY_IN_OUT, e.x, e.y);
165 } else if (e.shiftKey && !e.ctrlKey) {
166 g_cameraController.setDragMode(
167 o3djs.cameracontroller.DragMode.ZOOM_IN_OUT, e.x, e.y);
168 } else if (e.shiftKey && e.ctrlKey) {
169 g_cameraController.setDragMode(
170 o3djs.cameracontroller.DragMode.DOLLY_ZOOM, e.x, e.y);
171 }
172 }
173 }
174
175 /**
176 * Event handler that gets called when a mouse move event takes place
177 * in the O3D element. It tells the camera controller that the mouse
178 * has moved.
179 * @param {!Event} e The mouse move event.
180 */
181 function onMouseMove(e) {
182 g_cameraController.mouseMoved(e.x, e.y);
183 }
184
185 /**
186 * Event handler that gets called when a mouse up event takes place in
187 * the O3D element. It tells the camera controller that the mouse has
188 * been released.
189 * @param {!Event} e The mouse up event.
190 */
191 function onMouseUp(e) {
192 g_cameraController.setDragMode(
193 o3djs.cameracontroller.DragMode.NONE, e.x, e.y);
194 }
195
196 /**
197 * Sets up reasonable view and projection matrices.
198 */
199 function initContext() {
200 // Set up our CameraController.
201 g_cameraController = o3djs.cameracontroller.createCameraController(
202 [0, 0, 0], // centerPos
203 500, // backpedal
204 0, // heightAngle
205 0, // rotationAngle
206 g_math.degToRad(15), // fieldOfViewAngle
207 updateViewAndProjectionMatrices); // opt_onChange
208 g_cameraController.distancePerUnit = 100.0;
209
210 updateViewAndProjectionMatrices();
211 }
212
213 /**
214 * Updates the view and projection matrices.
215 */
216 function updateViewAndProjectionMatrices() {
217 g_viewInfo.drawContext.view = g_cameraController.calculateViewMatrix();
218
219 // Set up a perspective transformation for the projection.
220 g_viewInfo.drawContext.projection = g_math.matrix4.perspective(
221 g_cameraController.fieldOfViewAngle * 2, // Frustum angle.
222 g_o3dElement.clientWidth / g_o3dElement.clientHeight, // Aspect ratio.
223 1, // Near plane.
224 5000); // Far plane.
225 }
226
OLDNEW
« no previous file with comments | « samples/gpu2d/svgloader.js ('k') | samples/gpu2d/thincrescent.svg » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698