I have put together some matrix code - it''s meant to behave exactly as a call to glLoadIdentity, but it doesn''t.
The relevant snips
static GLfloat MATRIX[16];
static GLfloat* ptMATRIX = &MATRIX[0];
// These are globals
static void make_identity(void)
{
int i = 0;
for(i = 0; i < 16; i++)
{
MATRIX = 0.0;
}
MATRIX[0] = 1.0;
MATRIX[5] = 1.0;
MATRIX[10] = 1.0;
MATRIX[15] = 1.0;
}
// Relevant snippet of code from draw loop
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// glTranslatef(0.0, 0.0, -30.0);
make_identity;
// MATRIX[11] = -30.0;
glLoadMatrixf(ptMATRIX);
I imagine I''ve made some mess up with that pointer
>static GLfloat* ptMATRIX = &MATRIX[0]; id prefer ptMATRIX=MATRIX; or no ptMATRIX at all. in all regards that matter to the programmer an array is just a pointer to the first element.
>int j = 0; >for(j = 0; j < 16; j++) why doing it twice? for (int j=0; ...) is fine, as is int j=0; for ( ;j<16;..)
>MATRIX[j] = 0.0; if you plan on calling this a lot i would prefer memset(MATRIX, 0, 16*sizeof(GLfloat));
is this supposed to be the same? if so, the index should be 14, not 11. youre overwriting the 4th element of your local z axis which should always be 0. also changing anything except indices 12,13,14 like that will most likely screw up your matrix.
if youre unsure about the difference between your matrix and what it should look like i would compare it to the result of glGetFloatv(GL_MODELVIEW_MATRIX, somewhere); without that i would still be wondering why my cam matrix conversion is doing strange things (did i mention i grow to hate opengls coord system more and more every day? *g*)
I always initialize my matrices inline fashion. Sure, the code''s a little bigger, but who cares. It''s easier to see what the code is doing.
M[0] = ... M[1] = ... M[2] = ... M[3] = ...
M[4] = ... M[5] = ...
In the case of OpenGL, you might want to initialize them like this, since each column corresponds to a vector...
M[0] = ... M[4] = ... M[8] = ... M[12] = ...
M[1] = ... M[5] = ... M[9] = ...
As was just pointed out, M[14] is the value that corresponds to Z''s translation.
And, use glGetFloatv (... whenever you are in doubt.
Take heart, you are not the only one who spends hourse orienting in your mind how a matrix sits in memory. I also get really screwed up when you throw right hand and left hand coordinate systems into the mix, counter clockwise vs. clockwise vectors, the order in which you pass vectors into a cross-product function, and so on.
Thanks for the tips Trienco and Bishop_pass, they will go into my bag of tricks. Yes, code is sloppy, I know.
The MATRIX[11] was a bit daft - my matrix was err "rotated" so to speak.
Here was the crux of my woe :
make_identity;
Should have course been
make_identity();
I did in fact get a compiler warning for this "statement does nothing". But of course I was suppressing warnings wasn''t I. Matrix (my first by-hand one) is now rotating and translating which is quite a buzz.
Worth the struggle for the hints though Thanks again.
*fg* wait until you want to use it as camera and do it yourself instead of calling glulookat. 1 hour in which i always told myself ''ok, just make sure the number of sign errors is even''. code looks uglier but i still prefer doing what glulookat does myself instead of first building parameters from the matrix that glulookat will convert back into front and up vectors to calculate the right and true up vector which have been around in the matrix all the time anyway *g*. luckily its extremly easy to invert the matrix in that special case.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform.
You can accept analytics or continue with necessary cookies only.
Learn more