From 3e758f3c6b89738f046bdbed318c34ef244dec64 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Mon, 28 Sep 2015 09:05:39 -0700 Subject: [PATCH] fix Triangle::getNormal, add normal display to rayPickExample --- examples/example/scripts/rayPickExample.js | 34 ++++++++++++++++++++-- libraries/shared/src/GeometryUtil.cpp | 12 ++------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/examples/example/scripts/rayPickExample.js b/examples/example/scripts/rayPickExample.js index e573226f19..d85138211e 100644 --- a/examples/example/scripts/rayPickExample.js +++ b/examples/example/scripts/rayPickExample.js @@ -11,6 +11,17 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // + +var normalDisplay = Entities.addEntity({ + type: "Line", + name: "normalDisplay", + visible: false, + color: { red: 255, green: 0, blue: 0 }, + dimensions: { x: 100, y: 100, z: 100 } +}); + +var wasVisible = false; + function mouseMoveEvent(event) { print("mouseMoveEvent event.x,y=" + event.x + ", " + event.y); var pickRay = Camera.computePickRay(event.x, event.y); @@ -34,12 +45,29 @@ function mouseMoveEvent(event) { + intersection.intersection.y + ", " + intersection.intersection.z); print("intersection surfaceNormal.x/y/z=" + intersection.surfaceNormal.x + ", " + intersection.surfaceNormal.y + ", " + intersection.surfaceNormal.z); + + + // Note: line entity takes points in "entity relative frame" + var lineStart = { x: 0, y: 0, z: 0 }; + var lineEnd = intersection.surfaceNormal; + + Entities.editEntity(normalDisplay, { + visible: true, + position: intersection.intersection, + linePoints: [lineStart, lineEnd], + }); + wasVisible = true; + } else { + if (wasVisible) { + Entities.editEntity(normalDisplay, { visible: false }); + wasVisible = false; + } } } Controller.mouseMoveEvent.connect(mouseMoveEvent); -function scriptEnding() { -} -Script.scriptEnding.connect(scriptEnding); +Script.scriptEnding.connect(function() { + Entities.deleteEntity(normalDisplay); +}); diff --git a/libraries/shared/src/GeometryUtil.cpp b/libraries/shared/src/GeometryUtil.cpp index 9d109e5970..96c4d3bdea 100644 --- a/libraries/shared/src/GeometryUtil.cpp +++ b/libraries/shared/src/GeometryUtil.cpp @@ -257,15 +257,9 @@ bool findRayCapsuleIntersection(const glm::vec3& origin, const glm::vec3& direct // reference https://www.opengl.org/wiki/Calculating_a_Surface_Normal glm::vec3 Triangle::getNormal() const { - glm::vec3 u = v1 - v0; - glm::vec3 v = v2 - v0; - glm::vec3 result; - - result.x = (u.y * v.z) - (u.z * v.y); - result.x = (u.z * v.x) - (u.x * v.z); - result.x = (u.x * v.y) - (u.y * v.x); - - return glm::normalize(result); + glm::vec3 edge1 = v1 - v0; + glm::vec3 edge2 = v2 - v0; + return glm::normalize(glm::cross(edge1, edge2)); } bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3& direction,