fix Triangle::getNormal, add normal display to rayPickExample

This commit is contained in:
Brad Hefta-Gaub 2015-09-28 09:05:39 -07:00
parent ce3da099f6
commit 3e758f3c6b
2 changed files with 34 additions and 12 deletions

View file

@ -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);
});

View file

@ -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,