Skip to main content
gamedev.net
🔒 Locked

Matrix confusion

Started by Pres 5 replies 300+ views
Original Post
Pres
Pres
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
jamessharpe
jamessharpe
You''re forgeting to index the array in the loop it should read:

MATRIX = 0.0f;

You''re assigning the first element to 0.0 16 times!
Pres
Pres
Actually, I am indexing that array - but my variable, i, has been interpreted as a flag for italics, silly me. I''ll try again.


  
static GLfloat MATRIX[16];
static GLfloat* ptMATRIX = &MATRIX[0];

static void make_identity(void)
{
int j = 0;
for(j = 0; j < 16; j++)
{
MATRIX[j] = 0.0;
}
MATRIX[0] = 1.0;
MATRIX[5] = 1.0;
MATRIX[10] = 1.0;
MATRIX[15] = 1.0;
}

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// glTranslatef(0.0, 0.0, -30.0);

make_identity;
// MATRIX[11] = -30.0;

glLoadMatrixf(ptMATRIX);


Hope that''s better
Trienco
Trienco
>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));

>glTranslatef(0.0, 0.0, -30.0);
>MATRIX[11] = -30.0;

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*)
f@dzhttp://festini.device-zero.de
bishop_pass
bishop_pass
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.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in
Pres
Pres
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.


Trienco
Trienco
*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.
f@dzhttp://festini.device-zero.de

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.