2
\$\begingroup\$

I'm working on a radiosity processor in DirectX 9. The process requires that the camera be placed at the center of a mesh face and a 'screenshot' be taken facing 5 different directions...forward...up...down...left...right...

...The problem is that when the mesh face is facing up( look vector: 0, 1, 0 )...a view matrix cannot be determined using standard trigonometry functions:

Matrix4 LookAt( Vector3 eye, Vector3 target, Vector3 up ) 
    {   
        // The "look-at" vector.  
        Vector3 zaxis = normal(target - eye);        
        // The "right" vector.
        Vector3 xaxis = normal(cross(up, zaxis)); 
        // The "up" vector.
        Vector3 yaxis = cross(zaxis, xaxis); 
        // Create a 4x4 orientation matrix from the right, up, and at vectors     
        Matrix4 orientation = 
        {         
            xaxis.x, yaxis.x, zaxis.x, 0,         
            xaxis.y, yaxis.y, zaxis.y, 0,         
            xaxis.z, yaxis.z, zaxis.z, 0,           
            0,       0,       0,     1     
        };           

        // Create a 4x4 translation matrix by negating the eye position.     

        Matrix4 translation = 
        {       
            1,      0,      0,     0,          
            0,      1,      0,     0,            
            0,      0,      1,     0,        
            -eye.x, -eye.y, -eye.z,  1     
        };       
        // Combine the orientation and translation to compute the view matrix     
        return ( translation * orientation ); 
    } 

The above function comes from http://3dgep.com/?p=1700...

...Is there a mathematical approach to this problem?

\$\endgroup\$
5
\$\begingroup\$

You can check if cross(up, zaxis) is too close to zero (use an epsilon like 1e-4 or something like that), and switch to an alternative up-vector if so. For instance, if your usual up-vector is (0, 1, 0), you could switch to (1, 0, 0).

\$\endgroup\$
1
  • \$\begingroup\$ thanks...that is the best answer...I was trying to avoid adding more logic but it is the only way...if only computers could decide for themselves... \$\endgroup\$
    – P. Avery
    Aug 28 '13 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.