I am trying to remove the hidden surfaces on a cube rotating around its own diagonal axis and around general y
-axis.
I am using gluLookAt(0, 0, 20, 0, 0, 1, 0, 10, 0)
for the view stuff.
However, it doesn't entirely produce a well removed surfaces.
I am calculating the normal vector by:
GLfloat CalculateVectorNormal(GLfloat fVert1[], GLfloat fVert2[], GLfloat fVert3[], GLfloat fNormalZ)
{
GLfloat Qx, Qy, Qz, Px, Py, Pz;
Qx = fVert2[0]-fVert1[0];
Qy = fVert2[1]-fVert1[1];
Qz = fVert2[2]-fVert1[2];
Px = fVert3[0]-fVert1[0];
Py = fVert3[1]-fVert1[1];
Pz = fVert3[2]-fVert1[2];
fNormalZ = (Px*Qy - Py*Qx);
return fNormalZ;
}
and I am checking if fNormalZ
value开发者_如何学Cs is less than 0. I think I am making a mistake with the view stuff. I believe I am looking from only -z
axis. That's why I just check for fNormalZ
.
I can't play with view a lot either since then I lose the cube from sight.
Any help is appreciated. I really need some urgent help. Thank you very much in advance.
(Homework question, gl functions not allowed.)
A normal is a vector, not a scalar. You should calculate the components for X, Y and Z and return that.
You need to subtract the camera position (and inversely apply its rotation) to the face coordinates before calculating the vector. You then do a dotProduct between one of the points and the calculated normal to find the angle of the face. If the angle is less than zero, it's facing away from the camera viewpoint, and can be culled.
精彩评论