correct cross product calculation

This commit is contained in:
Rachel Hwang 2017-06-26 12:47:08 -04:00
parent a07ef6a3ab
commit 2706b95b5b

View File

@ -57,13 +57,6 @@ var facePattern2 = /f(\s+-?\d+\/-?\d+){3,}/;
var facePattern3 = /f(\s+-?\d+\/-?\d+\/-?\d+){3,}/; // f vertex/uv/normal vertex/uv/normal vertex/uv/normal ... var facePattern3 = /f(\s+-?\d+\/-?\d+\/-?\d+){3,}/; // f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
var facePattern4 = /f(\s+-?\d+\/\/-?\d+){3,}/; // f vertex//normal vertex//normal vertex//normal ... var facePattern4 = /f(\s+-?\d+\/\/-?\d+){3,}/; // f vertex//normal vertex//normal vertex//normal ...
// Just for line continuations
var facePattern5 = /((\s|^)+-?\d+\/?(\s|$)){1,}/; // f vertex vertex vertex ...
var facePattern6 = /((\s|^)+-?\d+\/-?\d+(\s|$)){1,}/; // f vertex/uv vertex/uv vertex/uv ...
var facePattern7 = /((\s|^)+-?\d+\/-?\d+\/-?\d+(\s|$)+){1,}/; // f vertex/uv/normal vertex/uv/normal vertex/uv/normal ...
var facePattern8 = /((\s|^)+-?\d+\/\/-?\d+(\s|$)){1,}/; // f vertex//normal vertex//normal vertex//normal ...
var faceSpacePattern = /(f?\s+)|(\s+\/)|(\s*\\)/g; var faceSpacePattern = /(f?\s+)|(\s+\/)|(\s*\\)/g;
var faceSpaceOrSlashPattern = /(f?\s+)|(\/+\s*)|(\s+\/)|(\s*\\)/g; var faceSpaceOrSlashPattern = /(f?\s+)|(\/+\s*)|(\s+\/)|(\s*\\)/g;
var scratchCartesian = new Cartesian3(); var scratchCartesian = new Cartesian3();
@ -344,13 +337,13 @@ function loadObj(objPath, options) {
var scratch3 = new Cartesian3(); var scratch3 = new Cartesian3();
// Checks if winding order matches the given normal. // Checks if winding order matches the given normal.
function isWindingCorrect(positionIndex1, positionIndex2, positionIndex3, normal) { function checkWindingCorrect(positionIndex1, positionIndex2, positionIndex3, normal) {
var A = get3DPoint(positionIndex1, scratch1); var A = get3DPoint(positionIndex1, scratch1);
var B = get3DPoint(positionIndex2, scratch2); var B = get3DPoint(positionIndex2, scratch2);
var C = get3DPoint(positionIndex3, scratch3); var C = get3DPoint(positionIndex3, scratch3);
Cartesian3.subtract(A, B, scratch1); Cartesian3.subtract(B, A, B);
Cartesian3.subtract(C, B, scratch2); Cartesian3.subtract(C, A, C);
var cross = Cartesian3.cross(A, C, scratch1); var cross = Cartesian3.cross(A, C, scratch1);
return (Cartesian3.dot(normal, cross) >= 0); return (Cartesian3.dot(normal, cross) >= 0);
@ -372,14 +365,14 @@ function loadObj(objPath, options) {
function addFace(vertices, positions, uvs, normals) { function addFace(vertices, positions, uvs, normals) {
var u1, u2, u3, n1, n2, n3; var u1, u2, u3, n1, n2, n3;
var windingCorrect = true; var isWindingCorrect = true;
var faceNormal; var faceNormal;
// If normals are defined, find a face normal to use in winding order sanitization. // If normals are defined, find a face normal to use in winding order sanitization.
// If no face normal, we have to assume the winding is correct. // If no face normal, we have to assume the winding is correct.
if (normals) { if (normals) {
faceNormal = get3DNormal(normals[0], scratchNormal); faceNormal = get3DNormal(normals[0], scratchNormal);
windingCorrect = isWindingCorrect(positions[0], positions[1], positions[2], faceNormal); isWindingCorrect = checkWindingCorrect(positions[0], positions[1], positions[2], faceNormal);
} }
if (vertices.length === 3) { if (vertices.length === 3) {
@ -400,7 +393,7 @@ function loadObj(objPath, options) {
var index2 = addVertex(vertices[1], positions[1], u2, n2); var index2 = addVertex(vertices[1], positions[1], u2, n2);
var index3 = addVertex(vertices[2], positions[2], u3, n3); var index3 = addVertex(vertices[2], positions[2], u3, n3);
addTriangle(index1, index2, index3, windingCorrect); addTriangle(index1, index2, index3, isWindingCorrect);
} else { // Triangulate if the face is not a triangle } else { // Triangulate if the face is not a triangle
var positions3D = []; var positions3D = [];
var vertexIndices = []; var vertexIndices = [];
@ -422,7 +415,7 @@ function loadObj(objPath, options) {
if (isConvex(positions2D)) { if (isConvex(positions2D)) {
for (i=1; i < vertices.length-1; ++i) { for (i=1; i < vertices.length-1; ++i) {
addTriangle(vertexIndices[0], vertexIndices[i], vertexIndices[i+1], windingCorrect); addTriangle(vertexIndices[0], vertexIndices[i], vertexIndices[i+1], isWindingCorrect);
} }
} else { } else {
// Since the projection doesn't preserve winding order, reverse the order of // Since the projection doesn't preserve winding order, reverse the order of
@ -435,7 +428,7 @@ function loadObj(objPath, options) {
// Use an ear-clipping algorithm to triangulate // Use an ear-clipping algorithm to triangulate
var positionIndices = PolygonPipeline.triangulate(positions2D); var positionIndices = PolygonPipeline.triangulate(positions2D);
for (i = 0; i < positionIndices.length-2; i += 3) { for (i = 0; i < positionIndices.length-2; i += 3) {
addTriangle(vertexIndices[positionIndices[i]], vertexIndices[positionIndices[i+1]], vertexIndices[positionIndices[i+2]], windingCorrect); addTriangle(vertexIndices[positionIndices[i]], vertexIndices[positionIndices[i+1]], vertexIndices[positionIndices[i+2]], isWindingCorrect);
} }
} }
} }