I am having trouble trying to rotate the face of a 3d model so it faces the camera directly.
I have this function so far, but the result is inconsistent. I would normally call this just before drawing the model. Assume the normal vector in these cases has been normalized so the distance is 1.
void doRotation (PVector theNormal){
//theNormal is the normal of face of model
//direction we want it to face, towards the camera which is facing (0,0,1)
PVector upVector = new PVector (0,0,-1);
float angleOfRotation = PVector.angleBetween(n, upVector);
PVector axisOfRotation = n.cross(upVector);
rotate( (-angleOfRotation), axisOfRotation.x, axisOfRotation.y, axisOfRotation.z );
}
I usually confirm the normal by drawing it as a line with two spheres… maybe this format is incorrect? The face is correctly rotated if the smaller blue sphere is inside the larger red sphere.
void drawNormal(PVector tc, PVector tn, int distance){
/// tc = face center, tn = face normal
line (tc.x, tc.y, tc.z, tn.x*distance, tn.y*distance, tn.z*distance);
pushMatrix();
stroke(255,0,0);
translate(tc.x, tc.y, tc.z);
sphere(30);
popMatrix();
pushMatrix();
stroke(0,0,255);
translate(tn.x*distance, tn.y*distance, tn.z*distance);
sphere(10);
popMatrix();
stroke(0);
}
I am using the HE_Mesh library for the model itself, but wanted to keep the problem spots of the code simpler.