nishant jain
nishant jain

Reputation: 1

Cube rotation issue in opengl android

I want to detect the face(which face is infront of user) whenever I am rotating the cube.

Upvotes: 0

Views: 283

Answers (2)

Mikola
Mikola

Reputation: 9326

datenwolf's answer has the right idea, but as written it isn't quite correct. Let P denote the 3x3 upper triangular submatrix of the model-view-projection matrix you are using to draw the cube. Then the third row of P (call it v for the sake argument) represents your view direction, unnormalized. Now to check if a face is pointing toward the camera assuming outward pointing unit normals, you would simply check if the dot product of its normal with v is < 0 (not maximized). This follows from the fact that:

 dot(a, b) = |a| |b| cos(theta)

Where the theta is the angle between them. This can only be negative if they are pointing in opposite directions, which is the necessary and sufficient condition for a face to be pointing toward the camera, assuming it is physically positioned in front of the camera.

Upvotes: 0

datenwolf
datenwolf

Reputation: 162327

Calculate the scalar product (dot product) of each face normal with the +Z vector. The face for which the scalar product is maximal faces the viewer.

Upvotes: 1

Related Questions