简体   繁体   中英

Calculate vector sign(positive/negative)

I'm trying to calculate a normal vector by 3 points, getting 2 vectors from them and to calculate the normal. But I need to know if the normal is positive or negative. Pls help :)

the normal is another vector, so you can say that is has a direction, and a value (or magnitude or length), but no sign.

the direction of the normal depends on the order, you pass the values.

I assume that you are asking for the culling, wether another vector would look from the front or the back at the face? then you can calculate the dot-product of the vector and your normal.

result > 0 they point in the same direction,
result < 0 they point in opposite directions

function pt(x,y,z){ return { x: +x||0, y: +y||0, z: +z||0 } }

function dot(a,b){ return b.x*a.x + b.y*a.y + b.z-a.z }

function delta(a,b){ return pt(b.x-a.x, b.y-a.y, b.z-a.z) }
function cross(a,b){
    return pt( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x )
}

var face = [
    pt(0,0,0), 
    pt(1,0,0),
    pt(0,1,0)
];

var vector;
var normal = cross(
    delta(face[0], face[1]),
    delta(face[1], face[2])
);

console.log('normal', normal);

console.log(vector = pt(0, 0, 10), dot(normal,vector));
console.log(vector = pt(0, 0, -1), dot(normal,vector));

//pretty flat angle but still enough to determine the direction
console.log(vector = pt(1, 1, .00005), dot(normal,vector));

Just a calculation of normal vector tells you nothing.

First of all you need to know the way the faces of the mesh have been defined. No matter if it's made out of triangles or quads you have to make sure you have the right order of the vertices - counterclockwise or clockwise (read this to see what a clockwise face is in OpenGL). The order determines which side of the face is visible (can be influenced by the properties of light) and which - not.

After that you need to calculate the dot product of your direction vector of camera and this normal vector. Depending on the order of the vertices you will get a negative or positive result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM