Thread: gluLookat doesnt work...

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    gluLookat doesnt work...

    ok, I am still having troubles with gluLookAt in my engine I am making. I am using it the same way I have in the past when it worked and was wondering what the requirements for gluLookAt to work are. My code has included glu.h and glu32.lib and it doesnt give me any errors, but gluLookat does not affect anything, any values I put into any of the positions still gives me the standard projection/modelview matrix.

    It wouldnt be such a problem except it kind of puts me in a bind with my terrain work currently


    I have included my code, I believe the problem to be somewhere within 3DMath.cpp or EchApplix.cpp

  2. #2
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27
    I'm not sure what the gluLookat problem is but the functions I see that call it, rotateview and updateveiw, don't do a glmatrixmode(MODELVIEW), glLoadIdentity() before they do the glulookat. Is the program guaranteed to call these gl functions before update/rotateview? If that's not the problem keep in mind that any subsequent calls to Translate,Scale,Rotate, and loadidentity will mess up the matrix glulookat builds. If you need to transform the modelview matrix, do a pushmatrix-transform-popmatrix and you'll have the glulookat matrix back.

    One thing I see is
    Code:
    DllExport Vector3d Reflect(Vector3d vIncom,Vector3d vNormal)
    {
    
    	Vector3d vReflect = Vector3d();
    	Vector3d vNormIn;
    	Vector3d vPerp;
    	float mag = Magnitude(vIncom);
    	vNormIn = Normalize(vIncom);
    
    	vNormIn = vNormIn * -1;
    	vPerp = vNormal * Dot(vNormIn,vNormal);
    
    	vReflect= (vPerp*2) - vNormIn;
    
    	vReflect = vReflect * mag;
    
    	return vReflect;
    }
    doesn't renormalize vReflect. vReflect isn't normalized after the line I put in bold, even though vnormin and vnormal are normalized. The magnitude of vperp*2 is 2cos( &theta; ) and the magnitude of vnormin is 1. So all you know about the magnitude of vReflect is that it is <= 1 + |2cos( &theta; )|. You need to renormalize vReflect.

    [edit]

    I'm completly wrong about this. The length of the vector is sqrt(1+(2cos&theta;)^2 - 2(1)(2cos&theta;)cos(&theta;))=1. I was quick to point it out because it seemed to have fixed a problem for me only a couple of nights ago. Now I realize my problem must be weirder than I thought. Your code is correct 100%. Sorry for the sloppy advice; I would erase this but Silvercord already responded to me. He's right the vector is very *almost* normalized. jeez.
    Last edited by grady; 01-04-2004 at 10:09 PM.

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I dont' really know what your problem is, in order for gluLookAt to work, you give it:

    a position
    a point to look at
    an up orientation

    your math library looks really good, and I'm surprised you can get all of that other math and not get gluLookAt working, but you'll get it eventually.

    grady's advice is good, but I found that when you add the projection of the vector onto the axis back into the reflected vector before returning it you *almost* preserve length, so you can very well have two different versions of the mirror function: one that approximates the mirrored velocity for routines that don't need 100% accuracy (which may be more than you think) and the one that gives you 100% accuracy but is forced to call sqrt whenever you use it.

    I also noticed some redundancy with your code, and subsequently in mine:

    Code:
    DllExport Vector3d RotateAroundAAxis(Vector3d A, Vector3d P, float degrees)
    {
    	degrees = (3.14159 * degrees) / 180; //convert to radians
    	float costheta=float(cos(degrees));
    	float sintheta=float(sin(degrees));
    	Vector3d vRotated = Vector3d(0,0,0);
        float mag = Magnitude(P);
    	vRotated = (P - (A * Dot(A,P)))*costheta + (Cross(A,P) * sintheta)  + (A * Dot(A,P));
    	return vRotated;
    }
    you flat out call A * Dot(A,P) multiple times, you save some speed by not recomputing it, but it's not really a huge deal (however if you profile your code, you'll end up finding dotproduct is the single most used function, which is why so many people try to optimzie it using SSE and 3D now and stuff)

  4. #4
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    well, see , the damned function works in all the different programs I have(I do know how to use it), but doesnt in this one, I am trying to think of ways it could be whacked up and have come up with the following:

    1) wrong matrix mode selected, yet glulookat is supposed to be used with the modelview matrix for this purpose and as far as I see, it is in modelview matrix when I call it.

    2) maybe my class based approach to my windows initialization is messing it up somewhere.

  5. #5
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27
    The way I would debug this is to write a function like this
    Code:
    void dbg(const char *str)
    {
            int i=0;
            float mat[16];
            printf("called at %s\n",str);
            glGetFloatv(GL_MODELVIEW_MATRIX,mat);
            for(;i<16;++i)printf("%f ",mat[i]);
            printf("\n");
    }
    and call it right before and after gluLookAt. Then sprinkle it around your render code and see where the matrix changes.
    Last edited by grady; 01-05-2004 at 12:46 AM.

  6. #6
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I also couldn't play around with it because the project you sent was to compile a dll not an exe, and I can't really do much unless I have an exe to play with.

    EDIT:
    Have you tested the reflect function? I don't think what you had was actually correct, I put comments above the lines in error withwhat I see as the correct math:
    Code:
    Vector3d vReflect = Vector3d();
    	Vector3d vNormIn;
    	Vector3d vPerp;
    	float mag = Magnitude(vIncom);
    	vNormIn = Normalize(vIncom);
    
    	vNormIn = vNormIn * -1;
    //vPerp = vNormIn - (vNormal * Dot(vNormIn,vNormal));
    	vPerp = vNormal * Dot(vNormIn,vNormal);
    
    //vReflect = vNormIn - (vPerp*2);
    	vReflect= (vPerp*2) - vNormIn; 
    
    	vReflect = vReflect * mag; 
    
    	return vReflect;
    lol, sorry, we're finding tons of other stuff in your code but nto what you actually want, lol.
    Last edited by Silvercord; 01-05-2004 at 05:06 AM.

  7. #7
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    no problem, i havent exactly proofread all the functions yet, reflect was one I just coded up real quickly while I was busy, also, I sometimes have the habit of accidently deleting parts of my code by accidently selecting things by holding CTRL down.

    also, i forgot to note, 3dmath is in fact a DLL as you mentioned silvercord. I have Redlight(my main project) dependent on it(which I may change, as I am unfortunately doing the "game in exe, engine in modules" format of a game, instead of the "engine in exe, game in modules" which lends itself more to modability) in my workspace(how do you make a copy of the workspace in VS 5?). to solve for this, just make an empty workspace, include those two projects, and then just set dependencies to have 3dmath required by Redlight.

    or, you can just use the EXEs I am including.

    EDIT: I am actually going more for the hardly used engine in modules, game in modules. that way, if I dont need a part of my engine, such as... say a shader module, I dont include it. This makes the main EXE more of an interface between the modules.
    Last edited by EvBladeRunnervE; 01-05-2004 at 08:42 AM.

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    in my experience about the worst thing you can do is code up a function, especially a math one, and not use it immediately, that sets you up for weird bugs and having absolutely no clue where it comes from. I've just started coding up a matrix class yesterday. Why am I just starting to code a matrix class? Because after a year of graphics programming this is the first time I've *absolutely needed* a matrix class (skeletal animation). Up until now I've just used the standard rotation equations, and I haven't done any seriously complex rendering coding (no per pixel lights or specular highlights or bump maps or stencil shadows)

    And, if there are bugs with the matrix class, they'll mostly all be found immediately.

  9. #9
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    yes, and I have tested the function out by loading the library into a console function, feeding the function values, then checking the output by manually solving the equation myself.

    btw, the function was correct, I just was stupid and wrongly labelled the variable.

    vPerp should be vParallel. The simplified version of the equation is:

    Code:
    R = 2A(A.P) - L
    cause the original equation is:

    Code:
    R = L - 2(L - A(A.P))
    I have been looking at matrical math again since I to am probably going to need it. most of it is easy, but I need to spend more time on determinants and inverses to actually be able to implement them in code.
    Last edited by EvBladeRunnervE; 01-05-2004 at 01:04 PM.

  10. #10
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    eww ick you're doing the evil forms of the equations from that book, I see where you're getting that from, but I absolutely loate the simplified versions of the equations, because whenever I've used them I'd have to go back a few weeks later, and I'd have *absolutely* no clue how the original equations were derived, but you are right the original was mathematically sound.

    EDIT:
    did you ever get the problem with gluLookAt fixed?
    Last edited by Silvercord; 01-05-2004 at 02:27 PM.

  11. #11
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    Nope, I honestly cant figure out what is going wrong when it works on other programs. This is honestly very wierd, when the initialization code is practically the same except for being in a class based system now.

    EDIT: BTW, would you just reccomend I just go with the original version of the reflection equation?

  12. #12
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I suggest you use whatever works best for you. For me, I don't really like using the simplified versions, but to be fair it is a bit faster because it seems like there are fewer calculations and it's mathematically the same, but as you know it had me confused, and being confused sucks

    EDIT:
    also, something really really important, when you start using matrices, and you want to concatenate matrices by multiplying them together, make sure that you
    A) use the right hand coordinate system matrices (on page 60 in your book)
    B) multiply them in this order to get a final transformation matrix:
    Y axis matrix * X axis matrix * Z axis matrix

    then, as you know, you multiply the resulting matrix by your view vector and that transforms it in the same way as the rotation about an arbitrary axis equation. To be honest, I tried getting this to work before I used the rotation about an arbitrary axis equation, but I wasn't multiplying the matrices in the right order together, and I want to save you from this hell (I used many note pages doing this crap out manually trying to see what my friggin problem was)
    EDIT1:
    okay, some more stuff, you can leave out the z axis rotation matrix if you don't want roll. Here's the equation of the Y axis rotation matrix multiplied by the X axis rotation matrix multiplied by your view vector:
    Code:
    /*
    	//Matrix equations for rotating about X and Y axes
    	temp.x = (cosy * mView.x) + (siny * sinx * mView.y) + (siny * cosx * mView.z);
    	temp.y = (cosx * mView.y) - (sinx * mView.z);
    	temp.z = (-siny * mView.x) + (cosy * sinx * mView.y) + (cosy* cosx * mView.z);
    */
    EDIT2:
    Can you please give me the source for just your exe? I don't know what you have to do to get that to work because I've never compiled my own dll before (well, I have but it was like two years ago and I don't remember anything about it). If you can do that, I might be able to find your error and get you on your merry way!
    Last edited by Silvercord; 01-05-2004 at 04:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM