diff options
author | petersont@google.com <petersont@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-17 21:36:08 +0000 |
---|---|---|
committer | petersont@google.com <petersont@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-17 21:36:08 +0000 |
commit | 422e69530732e4f54acb649ea8130ed22e3ccb53 (patch) | |
tree | cd556e693f9448a83d63a764e13561c67c883d6e | |
parent | a81e44e1ccd4c19de283dcd1e90da04bf8ae0208 (diff) | |
download | chromium_src-422e69530732e4f54acb649ea8130ed22e3ccb53.zip chromium_src-422e69530732e4f54acb649ea8130ed22e3ccb53.tar.gz chromium_src-422e69530732e4f54acb649ea8130ed22e3ccb53.tar.bz2 |
Sorts drawlists by priority or by depth as indicated by the flag in the DrawPass.
Review URL: http://codereview.chromium.org/2075003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47459 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | o3d/samples/o3d-webgl-samples/zsorting.html | 203 | ||||
-rw-r--r-- | o3d/samples/o3d-webgl/draw_list.js | 47 | ||||
-rw-r--r-- | o3d/samples/o3d-webgl/draw_pass.js | 3 | ||||
-rw-r--r-- | o3d/samples/o3d-webgl/transform.js | 26 |
4 files changed, 272 insertions, 7 deletions
diff --git a/o3d/samples/o3d-webgl-samples/zsorting.html b/o3d/samples/o3d-webgl-samples/zsorting.html new file mode 100644 index 0000000..36a7658 --- /dev/null +++ b/o3d/samples/o3d-webgl-samples/zsorting.html @@ -0,0 +1,203 @@ +<!-- +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. +--> + +<!-- +O3D ZSorting. +--> +<!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> +ZSorting. +</title> +<!-- Include sample javascript library functions--> +<script type="text/javascript" src="../o3d-webgl/base.js"></script> +<script type="text/javascript" src="../o3djs/base.js"></script> + +<!-- Our javascript code --> +<script type="text/javascript" id="o3dscript"> +o3djs.base.o3d = o3d; +o3djs.require('o3djs.webgl'); +o3djs.require('o3djs.util'); +o3djs.require('o3djs.math'); +o3djs.require('o3djs.rendergraph'); +o3djs.require('o3djs.primitives'); +o3djs.require('o3djs.material'); + +// Events +// init() once the page has finished loading. +// unload() when the page is unloaded. +window.onload = init; +window.onunload = unload; + +// global variables +var g_timeMult = 1.0; +var g_framesRendered = 0; +var g_o3d; +var g_math; +var g_client; +var g_viewInfo; +var g_pack; +var g_clock = 0; +var g_finished = false; // for selenium testing +var g_groupTransforms = []; +var GROUPS_ACROSS = 2; +var UNITS_ACROSS_GROUP = 2; +var TOTAL_ACROSS = GROUPS_ACROSS * UNITS_ACROSS_GROUP; +var HALF_WIDTH = TOTAL_ACROSS * 0.0; +var UNIT_SPACING = 100; + +/** + * Make a grid of transforms and put a teapot instance on each one + * @param {!o3d.Pack} pack Pack to create transforms in. + * @param {!o3d.Shape} shape Shape to instance. + */ +function createInstances(pack, shape) { + for (var g = 0; g < GROUPS_ACROSS; g++) { + for (var h = 0; h < GROUPS_ACROSS; h++) { + for (var i = 0; i < GROUPS_ACROSS; i++) { + var groupTransform = pack.createObject('Transform'); + g_groupTransforms[g_groupTransforms.length] = groupTransform; + groupTransform.parent = g_client.root; + groupTransform.translate( + (g * UNITS_ACROSS_GROUP - HALF_WIDTH) * UNIT_SPACING, + (h * UNITS_ACROSS_GROUP - HALF_WIDTH) * UNIT_SPACING, + (i * UNITS_ACROSS_GROUP - HALF_WIDTH) * UNIT_SPACING); + for (var x = 0; x < UNITS_ACROSS_GROUP; x++) { + for (var y = 0; y < UNITS_ACROSS_GROUP; y++) { + for (var z = 0; z < UNITS_ACROSS_GROUP; z++) { + var transform = pack.createObject('Transform'); + transform.parent = groupTransform; + transform.addShape(shape); + transform.translate( + (x - UNITS_ACROSS_GROUP * 0.5) * UNIT_SPACING, + (y - UNITS_ACROSS_GROUP * 0.5) * UNIT_SPACING, + (z - UNITS_ACROSS_GROUP * 0.5) * UNIT_SPACING); + transform.createParam('diffuse', 'ParamFloat4').value = [ + (g * UNITS_ACROSS_GROUP + x) * (1 / TOTAL_ACROSS), + (h * UNITS_ACROSS_GROUP + y) * (1 / TOTAL_ACROSS), + (i * UNITS_ACROSS_GROUP + z) * (1 / TOTAL_ACROSS), + 0.5]; + } + } + } + } + } + } +} + +/** + * Creates the client area. + */ +function init() { + o3djs.webgl.makeClients(initStep2); +} + +/** + * Initializes O3D and creates one shape. + * @param {Array} clientElements Array of o3d object elements. + */ +function initStep2(clientElements) { + // Initialize global variables and libraries. + var o3dElement = clientElements[0]; + g_o3d = o3dElement.o3d; + g_math = o3djs.math; + g_client = o3dElement.client; + + // Creates a pack to manage our resources/assets + g_pack = g_client.createPack(); + + g_viewInfo = o3djs.rendergraph.createBasicView( + g_pack, + g_client.root, + g_client.renderGraphRoot); + + // Create our projection matrix, with a vertical field of view of 45 + // degrees a near clipping plane of 0.1 and far clipping plane of 10000. + g_viewInfo.drawContext.projection = g_math.matrix4.perspective( + g_math.degToRad(45), + g_client.width / g_client.height, + 0.1, + 10000); + + // Create a Material. + var material = o3djs.material.createBasicMaterial( + g_pack, g_viewInfo, [1, 1, 1, 1], true); + + // Create a sphere. + var shape = o3djs.primitives.createSphere(g_pack, material, 30, 20, 20); + + createInstances(g_pack, shape); + + // Setup an onrender callback for animation. + g_client.setRenderCallback(onrender); + + g_finished = true; // for selenium testing. +} + +// spin the camera. +function onrender(renderEvent) { + g_framesRendered++; + // Get the number of seconds since the last render. + var elapsedTime = renderEvent.elapsedTime; + g_clock += elapsedTime * g_timeMult; + + var x = Math.sin(g_clock * 0.1) * 400; + var z = Math.cos(g_clock * 0.1) * 400; + var y = Math.sin(g_clock * 0.2) * 400; + + g_viewInfo.drawContext.view = g_math.matrix4.lookAt( + [x, y, z], // eye + [0, 0, 0], // target + [0, 1, 0]); // up +} + +/** + * 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> +<h1>ZSorting</h1> +Objects get zsorted for transparency. +<br/> +<!-- Start of O3D plugin --> +<div id="o3d" style="width: 800px; height: 600px"></div> +<!-- End of O3D plugin --> +</body> +</html> diff --git a/o3d/samples/o3d-webgl/draw_list.js b/o3d/samples/o3d-webgl/draw_list.js index 875f237..6132a8b 100644 --- a/o3d/samples/o3d-webgl/draw_list.js +++ b/o3d/samples/o3d-webgl/draw_list.js @@ -69,10 +69,55 @@ o3d.DrawList.BY_PRIORITY = 2; /** + * Compare function for by-priority sort. + */ +o3d.DrawList.comparePriority_ = function(drawElementInfoA, drawElementInfoB) { + return drawElementInfoA.drawElement.owner.priority - + drawElementInfoB.drawElement.owner.priority; +}; + + +/** + * Compare function for by-z-coordinate sort. + * @param {!o3d.DrawElement} drawElementInfoA + * @param {!o3d.DrawElement} drawElementInfoB + */ +o3d.DrawList.compareZ_ = function(drawElementInfoA, drawElementInfoB) { + return o3d.Transform.transformPointZOnly( + drawElementInfoA.worldViewProjection, + drawElementInfoA.drawElement.owner.zSortPoint) - + o3d.Transform.transformPointZOnly( + drawElementInfoB.worldViewProjection, + drawElementInfoB.drawElement.owner.zSortPoint); +}; + + +/** + * Sorts this list according to the given sort method. + * @param {o3d.DrawList.SortMethod} sort_method Which method to use. + * @private + */ +o3d.DrawList.prototype.sort_ = function(sort_method) { + switch (sort_method) { + case o3d.DrawList.BY_PRIORITY: + this.list_.sort(o3d.DrawList.comparePriority_); + break; + + case o3d.DrawList.BY_Z_ORDER: + this.list_.sort(o3d.DrawList.compareZ_); + break; + + case o3d.DrawList.BY_PERFORMANCE: + default: + break; + } +}; + + +/** * Renders the draw list. */ o3d.DrawList.prototype.render = function() { - // TODO(petersont): Add sort. for (var i = 0; i < this.list_.length; ++i) { var drawElementInfo = this.list_[i]; var world = drawElementInfo.world; diff --git a/o3d/samples/o3d-webgl/draw_pass.js b/o3d/samples/o3d-webgl/draw_pass.js index 600f641..c02601b 100644 --- a/o3d/samples/o3d-webgl/draw_pass.js +++ b/o3d/samples/o3d-webgl/draw_pass.js @@ -32,7 +32,7 @@ /** * A DrawPass renders a DrawList. - * + * * @param {o3d.DrawList} drawList The DrawList used by this DrawPass. * @param {o3d.DrawPass.SortMethod} sortMethod ParamInteger The method * of sorting this DrawPass. @@ -69,6 +69,7 @@ o3d.ParamObject.setUpO3DParam_(o3d.DrawPass, 'sortMethod', 'ParamInteger'); */ o3d.DrawPass.prototype.before = function() { if (this.drawList) { + this.drawList.sort_(this.sortMethod); this.drawList.render(); } }; diff --git a/o3d/samples/o3d-webgl/transform.js b/o3d/samples/o3d-webgl/transform.js index 929abfd..6499da2 100644 --- a/o3d/samples/o3d-webgl/transform.js +++ b/o3d/samples/o3d-webgl/transform.js @@ -612,11 +612,6 @@ o3d.Transform.transformPoint = function(m, v) { var v0 = v[0]; var v1 = v[1]; var v2 = v[2]; - - if (!m) { - debugger; - } - var m0 = m[0]; var m1 = m[1]; var m2 = m[2]; @@ -629,6 +624,27 @@ o3d.Transform.transformPoint = function(m, v) { }; +/** + * Takes a 4-by-4 matrix and a vector with 3 entries, + * interprets the vector as a point, transforms that point by the matrix, + * returning the z-component of the result only. + * @param {!o3djs.math.Matrix4} m The matrix. + * @param {!o3djs.math.Vector3} v The point. + * @return {number} The z coordinate of the transformed point. + */ +o3d.Transform.transformPointZOnly = function(m, v) { + var v0 = v[0]; + var v1 = v[1]; + var v2 = v[2]; + var m0 = m[0]; + var m1 = m[1]; + var m2 = m[2]; + var m3 = m[3]; + + var d = (v0 * m0[2] + v1 * m1[2] + v2 * m2[2] + m3[2]) / + (v0 * m0[3] + v1 * m1[3] + v2 * m2[3] + m3[3]); +}; + /** * Pre-composes the local matrix of this Transform with a rotation about the |