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

Unified Diff: samples/o3d-webgl/primitive.js

Issue 2805101: o3d-webgl: Adding support for triangle fan/strip in picking. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/o3d/
Patch Set: '' Created 10 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « samples/o3d-webgl-samples/picking-more.html ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: samples/o3d-webgl/primitive.js
===================================================================
--- samples/o3d-webgl/primitive.js (revision 55009)
+++ samples/o3d-webgl/primitive.js (working copy)
@@ -333,6 +333,44 @@
}
};
+/**
+ * Returns the three indices of the n-th triangle of this primitive. If the
+ * primitive has no index buffer, then the buffer is assumed to be [0 ... n-1].
+ * These indices can then be used to reference the vertex buffer and get the
+ * triangle vertices' positions.
+ *
+ * @param {number} n The number of the triangle we want. Zero-indexed.
+ * @return {!Array.<Number>} Array containing three indices that correspond to
+ * the n-th triangle of this primitive.
+ * @private
+ */
+o3d.Primitive.prototype.computeTriangleIndices_ = function(n) {
+ var indices;
+ switch (this.primitiveType) {
+ case o3d.Primitive.TRIANGLESTRIP:
+ if (n % 2 == 0) {
+ indices = [n, n + 1, n + 2];
+ } else {
+ indices = [n + 1, n, n + 2];
+ }
+ break;
+ case o3d.Primitive.TRIANGLEFAN:
+ indices = [0, n + 1, n + 2];
+ break;
+ case o3d.Primitive.TRIANGLELIST:
+ default:
+ indices = [3 * n, 3 * n + 1, 3 * n + 2];
+ break;
+ }
+ if (this.indexBuffer) {
+ var buffer = this.indexBuffer.array_;
+ return [buffer[indices[0]],
+ buffer[indices[1]],
+ buffer[indices[2]]];
+ } else {
+ return indices;
+ }
+};
/**
* Computes the intersection of a ray in the coordinate system of
@@ -392,16 +430,28 @@
// from the start the point with this variable.
var min_distance = 0;
- // Iterate through the indices three at a time. Each triple of indices
- // defines a triangle. For each triangle, we test for intersection with
+ // Iterate through the indices and examine triples of indices that each
+ // define a triangle. For each triangle, we test for intersection with
// the ray. We need to find the closest one to start, so we have to
// check them all.
- var a = indexBuffer.array_;
- for (var i = 0; i < a.length / 3; ++i) {
- // Indices of the triangle.
- var t = 3 * i;
- var indices = [a[t], a[t + 1], a[t + 2]];
+ var numIndices = indexBuffer ? indexBuffer.array_.length : numPoints;
+ switch (this.primitiveType) {
+ case o3d.Primitive.TRIANGLESTRIP:
+ numTriangles = numIndices - 2;
+ break;
+ case o3d.Primitive.TRIANGLEFAN:
+ numTriangles = numIndices - 2;
+ break;
+ case o3d.Primitive.TRIANGLELIST:
+ default:
+ numTriangles = numIndices / 3;
+ break;
+ }
+
+ for (var i = 0; i < numTriangles; ++i) {
+ var indices = this.computeTriangleIndices_(i);
+
// Check if the current triangle is too far to one side of the ray
// to intersect at all. (This is what the orthogonal vectors are for)
var u_sides = [false, false, false];
« no previous file with comments | « samples/o3d-webgl-samples/picking-more.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698