Jump to content
  • Advertisement
Sign in to follow this  
callidus-red

OpenGL alternative to gluLookAt

This topic is 4422 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

Hi all, I am implementing an alternative function for gluLookAt in my matrix lib and have the following problem. Nothing is renderd except under a specific situation when useing my 'mtxLookAt' function over gluLookAt. :S I guess the matrix is not generated or perhaps not used correctly, either way this is driving me nuts so i have included my code for relevent matrix operations and rendering snipet in the hope that someone might spot the error...
void mtxLookAt( const vec& _eye, 
                const vec& _center, 
                const vec& _up,
                mtx& _o ) 
{
    vec F;
    vec S;
    vec U;
		
    vec3Sub(_center,_eye,F);
    vec3Normalise(F,F);
		
    // S = F x U
    vec3Cross(F,_up,S);
    vec3Normalise(S,S);
		
    // recompute up: U = S x F
    vec3Cross(S,F,U);
    vec3Normalise(U,U); // aparently unit x unit != unit somtimes
		
    mtx tmpMtx;
    mtx trnMtx;
		
    tmpMtx[0] = S[x__]; tmpMtx[1] = U[x__]; tmpMtx[2] = -F[x__]; tmpMtx[3] = 0.0f;
    tmpMtx[4] = S[y__]; tmpMtx[5] = U[y__]; tmpMtx[6] = -F[y__]; tmpMtx[7] = 0.0f;
    tmpMtx[8] = S[z__]; tmpMtx[9] = U[z__]; tmpMtx[10]= -F[z__]; tmpMtx[11]= 0.0f;
    tmpMtx[12]= 0.0f;   tmpMtx[13]= 0.0f;   tmpMtx[14]= 0.0f;    tmpMtx[15]= 1.0f;
    
    // /*
   mtxTranslation(trnMtx,-_eye[x__],-_eye[y__],-_eye[z__]);
   mtx4MulMtx4(tmpMtx,trnMtx,_o);
}


the matrix mult function....
void mtx4MulMtx4(const mtx& _l, const mtx& _r, mtx& _o)
{
  _o[0]  = (_r[0]  * _l[0]) + (_r[1]  * _l[4]) + (_r[2]  * _l[8])  + (_r[3]  * _l[12]);
  _o[4]  = (_r[4]  * _l[0]) + (_r[5]  * _l[4]) + (_r[6]  * _l[8])  + (_r[7]  * _l[12]);
  _o[8]  = (_r[8]  * _l[0]) + (_r[9]  * _l[4]) + (_r[10] * _l[8])  + (_r[11] * _l[12]);
  _o[12] = (_r[12] * _l[0]) + (_r[13] * _l[4]) + (_r[14] * _l[8])  + (_r[15] * _l[12]);
  _o[1]  = (_r[0]  * _l[1]) + (_r[1]  * _l[5]) + (_r[2]  * _l[9])  + (_r[3]  * _l[13]);
  _o[5]  = (_r[4]  * _l[1]) + (_r[5]  * _l[5]) + (_r[6]  * _l[9])  + (_r[7]  * _l[13]);
  _o[9]  = (_r[8]  * _l[1]) + (_r[9]  * _l[5]) + (_r[10] * _l[9])  + (_r[11] * _l[13]);
  _o[13] = (_r[12] * _l[1]) + (_r[13] * _l[5]) + (_r[14] * _l[9])  + (_r[15] * _l[13]);
  _o[2]  = (_r[0]  * _l[2]) + (_r[1]  * _l[6]) + (_r[2]  * _l[10]) + (_r[3]  * _l[14]);
  _o[6]  = (_r[4]  * _l[2]) + (_r[5]  * _l[6]) + (_r[6]  * _l[10]) + (_r[7]  * _l[14]);
  _o[10] = (_r[8]  * _l[2]) + (_r[9]  * _l[6]) + (_r[10] * _l[10]) + (_r[11] * _l[14]);
  _o[14] = (_r[12] * _l[2]) + (_r[13] * _l[6]) + (_r[14] * _l[10]) + (_r[15] * _l[14]);
  _o[3]  = (_r[0]  * _l[3]) + (_r[1]  * _l[7]) + (_r[2]  * _l[11]) + (_r[3]  * _l[15]);
  _o[7]  = (_r[4]  * _l[3]) + (_r[5]  * _l[7]) + (_r[6]  * _l[11]) + (_r[7]  * _l[15]);
  _o[11] = (_r[8]  * _l[3]) + (_r[9]  * _l[7]) + (_r[10] * _l[11]) + (_r[11] * _l[15]);
  _o[15] = (_r[12] * _l[3]) + (_r[13] * _l[7]) + (_r[14] * _l[11]) + (_r[15] * _l[15]);
}

and the rendering snipet...
void cRenderer::setViewMtx(MAT::cMtx4& _m)
{
  // /*
  glMatrixMode(GL_MODELVIEW);
  //mV = _m;
  glLoadMatrixf(_m.mMtx);
  // */
}

I have printed out the matrix generated by gluLookAt and by my mtxLookAt in the hope that i may spot the error, often the two matrix look the same ? The print outs look as follows... //-------------------------------------------- eye = 300,0,600 center = 0,0,0 up = 0,1,0 openGL ----> 0.894427 0 -0.447214 0 ----> 0 1 0 0 ----> 0.447214 0 0.894427 -670.82 ----> 0 0 0 1 my func ----> 0.894427 0 -0.447214 0 ----> 0 1 0 0 ----> 0.447214 0 0.894427 -670.82 ----> 0 0 0 1 this looks OK but will not render anything ?? //-------------------------------------------- //-------------------------------------------- eye = 0,0,600 center = 0,0,0 up = 0,1,0 openGL ----> 1 0 0 0 ----> 0 1 0 0 ----> 0 0 1 -600 ----> 0 0 0 1 my func ----> 1 0 0 0 ----> 0 1 0 0 ----> 0 0 1 -600 ----> 0 0 0 1 this renders the scene properly //-------------------------------------------- //-------------------------------------------- eye = 300,300,600 center = 0,0,0 up = 0,1,0 openGL ----> 0.894427 0 -0.447214 0 ----> -0.182574 0.912871 -0.365148 1.3411e-05 ----> 0.408248 0.408248 0.816497 -734.847 ----> 0 0 0 1 my func ----> 0.894427 0 -0.447214 0 ----> -0.182574 0.912871 -0.365148 -8.9407e-06 ----> 0.408248 0.408248 0.816497 -734.847 ----> 0 0 0 1 this wont render anything and exposes the error in ogl gives 1.3411e-05 in elem 7 and i have -8.9407e-06 ? //-------------------------------------------- This problem is driving me slowly insane and any help at all would be greatly apreciated. Regards Tim Kelsey

Share this post


Link to post
Share on other sites
Advertisement
That last one is just a precision error. GLU uses double and maybe you are using floats. If you use gluLookAt, it generate the matrix and calls glMultMatrixd. You should call glMultMatrixf.
Also, you may be able to just do glLoadMatrixf if modelview was identity.

Share this post


Link to post
Share on other sites
Here's an implementation of gluLookAt:
vec3_t x, y, z;
mat4_t m;

// view direction
z[0] = eyex - centerx;
z[1] = eyey - centery;
z[2] = eyez - centerz;

// default y to up
y[0] = upx;
y[1] = upy;
y[2] = upz;

// force x, y and z to be all perpendicular to each other
vec3_Normalise(z);
vec3_Cross(y, z, x);
vec3_Cross(z, x, y);

// normalise
vec3_Normalise(x);
vec3_Normalise(y);

// Stuff them all into a matrix
m[0] = x[0]; m[1] = y[0]; m[2] = z[0]; m[3] = 0;
m[4] = x[1]; m[5] = y[1]; m[6] = z[1]; m[7] = 0;
m[8] = x[2]; m[9] = y[2]; m[10] = z[2]; m[11] = 0;
m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1;

// tell gl all about it
mat4_Translate(-eyex, -eyey, -eyez, m);
mat4_Mult(modelview, m, modelview);

Share this post


Link to post
Share on other sites
Thanks V-man, cdoty

I think my implementation is OK, it is basicly the same as cdoty's post. Ill have a play with some doubles instead of floats but the number looks too far off for precision error, enought to prevent openGL rendering a frame :o

Share this post


Link to post
Share on other sites
Quote:
Original post by callidus-red
...but the number looks too far off for precision error...
Are you talking about this:
Quote:
ogl gives 1.3411e-05 in elem 7 and i have -8.9407e-06 ?
?

If so, those numbers are both nearly zero (in the given context at least), and the difference is almost certainly just a precision error. I'd be willing to bet that the problem lies elsewhere.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!