OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010, Google Inc. | 2 * Copyright 2010, Google Inc. |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 wireframeIndices[j++] = a; | 326 wireframeIndices[j++] = a; |
327 a = b; | 327 a = b; |
328 b = c; | 328 b = c; |
329 } | 329 } |
330 this.wireframeIndexBuffer_.unlock(); | 330 this.wireframeIndexBuffer_.unlock(); |
331 } | 331 } |
332 break; | 332 break; |
333 } | 333 } |
334 }; | 334 }; |
335 | 335 |
| 336 /** |
| 337 * Returns the three indices of the n-th triangle of this primitive. If the |
| 338 * primitive has no index buffer, then the buffer is assumed to be [0 ... n-1]. |
| 339 * These indices can then be used to reference the vertex buffer and get the |
| 340 * triangle vertices' positions. |
| 341 * |
| 342 * @param {number} n The number of the triangle we want. Zero-indexed. |
| 343 * @return {!Array.<Number>} Array containing three indices that correspond to |
| 344 * the n-th triangle of this primitive. |
| 345 * @private |
| 346 */ |
| 347 o3d.Primitive.prototype.computeTriangleIndices_ = function(n) { |
| 348 var indices; |
| 349 switch (this.primitiveType) { |
| 350 case o3d.Primitive.TRIANGLESTRIP: |
| 351 if (n % 2 == 0) { |
| 352 indices = [n, n + 1, n + 2]; |
| 353 } else { |
| 354 indices = [n + 1, n, n + 2]; |
| 355 } |
| 356 break; |
| 357 case o3d.Primitive.TRIANGLEFAN: |
| 358 indices = [0, n + 1, n + 2]; |
| 359 break; |
| 360 case o3d.Primitive.TRIANGLELIST: |
| 361 default: |
| 362 indices = [3 * n, 3 * n + 1, 3 * n + 2]; |
| 363 break; |
| 364 } |
| 365 if (this.indexBuffer) { |
| 366 var buffer = this.indexBuffer.array_; |
| 367 return [buffer[indices[0]], |
| 368 buffer[indices[1]], |
| 369 buffer[indices[2]]]; |
| 370 } else { |
| 371 return indices; |
| 372 } |
| 373 }; |
336 | 374 |
337 /** | 375 /** |
338 * Computes the intersection of a ray in the coordinate system of | 376 * Computes the intersection of a ray in the coordinate system of |
339 * the specified POSITION stream. | 377 * the specified POSITION stream. |
340 * @param {number} position_stream_index Index of POSITION stream. | 378 * @param {number} position_stream_index Index of POSITION stream. |
341 * @param {o3d.Cull} cull which side of the triangles to ignore. | 379 * @param {o3d.Cull} cull which side of the triangles to ignore. |
342 * @param {!o3d.math.Point3} start position of start of ray in local space. | 380 * @param {!o3d.math.Point3} start position of start of ray in local space. |
343 * @param {!o3d.math.Point3} end position of end of ray. in local space. | 381 * @param {!o3d.math.Point3} end position of end of ray. in local space. |
344 * @return {!o3d.RayIntersectionInfo} RayIntersectionInfo class. If valid() | 382 * @return {!o3d.RayIntersectionInfo} RayIntersectionInfo class. If valid() |
345 * is false then something was wrong, Check GetLastError(). If | 383 * is false then something was wrong, Check GetLastError(). If |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 var vy = z * ux - x * uz; | 423 var vy = z * ux - x * uz; |
386 var vz = x * uy - y * ux; | 424 var vz = x * uy - y * ux; |
387 | 425 |
388 var udotstart = ux * start[0] + uy * start[1] + uz * start[2]; | 426 var udotstart = ux * start[0] + uy * start[1] + uz * start[2]; |
389 var vdotstart = vx * start[0] + vy * start[1] + vz * start[2]; | 427 var vdotstart = vx * start[0] + vy * start[1] + vz * start[2]; |
390 | 428 |
391 // As we search for an intersection point, we keep track of how far out | 429 // As we search for an intersection point, we keep track of how far out |
392 // from the start the point with this variable. | 430 // from the start the point with this variable. |
393 var min_distance = 0; | 431 var min_distance = 0; |
394 | 432 |
395 // Iterate through the indices three at a time. Each triple of indices | 433 // Iterate through the indices and examine triples of indices that each |
396 // defines a triangle. For each triangle, we test for intersection with | 434 // define a triangle. For each triangle, we test for intersection with |
397 // the ray. We need to find the closest one to start, so we have to | 435 // the ray. We need to find the closest one to start, so we have to |
398 // check them all. | 436 // check them all. |
399 var a = indexBuffer.array_; | 437 |
400 for (var i = 0; i < a.length / 3; ++i) { | 438 var numIndices = indexBuffer ? indexBuffer.array_.length : numPoints; |
401 // Indices of the triangle. | 439 switch (this.primitiveType) { |
402 var t = 3 * i; | 440 case o3d.Primitive.TRIANGLESTRIP: |
403 var indices = [a[t], a[t + 1], a[t + 2]]; | 441 numTriangles = numIndices - 2; |
| 442 break; |
| 443 case o3d.Primitive.TRIANGLEFAN: |
| 444 numTriangles = numIndices - 2; |
| 445 break; |
| 446 case o3d.Primitive.TRIANGLELIST: |
| 447 default: |
| 448 numTriangles = numIndices / 3; |
| 449 break; |
| 450 } |
| 451 |
| 452 for (var i = 0; i < numTriangles; ++i) { |
| 453 var indices = this.computeTriangleIndices_(i); |
404 | 454 |
405 // Check if the current triangle is too far to one side of the ray | 455 // Check if the current triangle is too far to one side of the ray |
406 // to intersect at all. (This is what the orthogonal vectors are for) | 456 // to intersect at all. (This is what the orthogonal vectors are for) |
407 var u_sides = [false, false, false]; | 457 var u_sides = [false, false, false]; |
408 var v_sides = [false, false, false]; | 458 var v_sides = [false, false, false]; |
409 for (var j = 0; j < 3; ++j) { | 459 for (var j = 0; j < 3; ++j) { |
410 var t = 3 * indices[j]; | 460 var t = 3 * indices[j]; |
411 var r = elements.slice(t, t + 3); | 461 var r = elements.slice(t, t + 3); |
412 u_sides[j] = ux * r[0] + uy * r[1] + uz * r[2] - udotstart > 0; | 462 u_sides[j] = ux * r[0] + uy * r[1] + uz * r[2] - udotstart > 0; |
413 v_sides[j] = vx * r[0] + vy * r[1] + vz * r[2] - vdotstart > 0; | 463 v_sides[j] = vx * r[0] + vy * r[1] + vz * r[2] - vdotstart > 0; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 } | 567 } |
518 points.push(p); | 568 points.push(p); |
519 } | 569 } |
520 | 570 |
521 o3d.BoundingBox.fitBoxToPoints_(points, this.boundingBox); | 571 o3d.BoundingBox.fitBoxToPoints_(points, this.boundingBox); |
522 return this.boundingBox; | 572 return this.boundingBox; |
523 }; | 573 }; |
524 | 574 |
525 | 575 |
526 | 576 |
OLD | NEW |