• Advertisement
Sign in to follow this  

Camera not working as expected

This topic is 894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anyone check my camera matrix code (in the zip file as matrix_utils.cpp and uv_camera.cpp) is coded properly? The source is attached to this post.

 

The command to run the app is "view fractal.stl"

 

I'm trying to make my camera look at the centre of my fractal but it is not doing it correctly. Use the left mouse button to rotate the camera.

 

Any help is greatly appreciated.

 

Share this post


Link to post
Share on other sites
Advertisement

The problem existed in my matrix_utils.cpp file, I changed it to the following code and it works now:

 

void get_look_at_matrix(float eyex, float eyey, float eyez, float centrex, float centrey, float centrez, float upx, float upy, float upz, float (&mat)[16])
{
    // https://www.opengl.org/sdk/docs/man2/xhtml/gluLookAt.xml
    // http://www.cs.virginia.edu/~gfx/Courses/1999/intro.fall99.html/lookat.html
    vertex_3 f, up, s, u;
    
    f.x = centrex - eyex;
    f.y = centrey - eyey;
    f.z = centrez - eyez;
    f.normalize();
    
    up.x = upx;
    up.y = upy;
    up.z = upz;
    up.normalize();
    
    s = f.cross(up);
    s.normalize();
    
    u = s.cross(f);
    u.normalize();
    
    mat[0] = s.x;  mat[4] = s.y;  mat[8] = s.z;   mat[12] = 0;
    mat[1] = u.x;  mat[5] = u.y;  mat[9] = u.z;   mat[13] = 0;
    mat[2] = -f.x; mat[6] = -f.y; mat[10] = -f.z; mat[14] = 0;
    mat[3] = 0;    mat[7] = 0;    mat[11] = 0;    mat[15] = 1;


    float translate[16];

    translate[0] = 1; translate[4] = 0; translate[8] = 0;  translate[12] = -eyex;
    translate[1] = 0; translate[5] = 1; translate[9] = 0;  translate[13] = -eyey;
    translate[2] = 0; translate[6] = 0; translate[10] = 1; translate[14] = -eyez;
    translate[3] = 0; translate[7] = 0; translate[11] = 0; translate[15] = 1;

    float temp[16];

    multiply_4x4_matrices(mat, translate, temp);

    for(size_t i = 0; i < 16; i++)
        mat[i] = temp[i];
}
 

Share this post


Link to post
Share on other sites

Your implementation of look at matrix is not efficient at all, here my implementation :

void CMatrix4::LookAt( const CVector3& From, const CVector3& To, const CVector3& Up )
{
  const CVector3 ZAxis = ( To - From ).Normalized();
  const CVector3 XAxis = VectorCross( Up, ZAxis ).Normalized();
  const CVector3 YAxis = VectorCross( ZAxis, XAxis ).Normalized();
  m44[ 0 ][ 0 ] = XAxis.x; m44[ 0 ][ 1 ] = XAxis.y; m44[ 0 ][ 2 ] = XAxis.z; m44[ 0 ][ 3 ] = -VectorDot( XAxis, From );
  m44[ 1 ][ 0 ] = YAxis.x; m44[ 1 ][ 1 ] = YAxis.y; m44[ 1 ][ 2 ] = YAxis.z; m44[ 1 ][ 3 ] = -VectorDot( YAxis, From );
  m44[ 2 ][ 0 ] = ZAxis.x; m44[ 2 ][ 1 ] = ZAxis.y; m44[ 2 ][ 2 ] = ZAxis.z; m44[ 2 ][ 3 ] = -VectorDot( ZAxis, From );
  m44[ 3 ][ 0 ] = 0.0f;    m44[ 3 ][ 1 ] = 0.0f;    m44[ 3 ][ 2 ] = 0.0f;    m44[ 3 ][ 3 ] = 1.0f;
}
Edited by Alundra

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement