• Advertisement
Sign in to follow this  

Need quick help - Simple matrix transformation not working

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

Im try to do things without using glTranslatef/glRotatef etc. Im doing the following:
float m[16];

m[0]  = 1.0f;
m[1]  = 0.0f;
m[2]  = 0.0f;
m[3]  = 0.0f;
m[4]  = 0.0f;
m[5]  = 1.0f;
m[6]  = 0.0f;
m[7]  = 0.0f;
m[8]  = 0.0f;
m[9]  = 0.0f;
m[10] = 1.0f;
m[11] = 0.0f;
m[12] = 10.0f;
m[13] = 10.0f;
m[14] = 10.0f;
m[15] = 1.0f;
	
glMatrixMode(GL_MODELVIEW);

glPushMatrix();
	
// Reset matrix
glLoadIdentity();

// Load my matrix
glLoadMatrixf(m);

// Draw the object
drawObject();

glPopMatrix();

This draws nothing, but if i replace glLoadMatrixf(m) with glTranslatef(10.0,10.0,10.0) then it works. Why isnt it workin? Thx

Share this post


Link to post
Share on other sites
Advertisement
I don't see anything wrong with your code. Are you doing it exactly like what you posted, or is something else involved?

Share this post


Link to post
Share on other sites
Yeh, thats everything, if I do this it works:


glPushMatrix();
glTranslatef(10.0, 10.0, 10.0);
draw();
glPopMatrix();




Im reading through books and online stuff and it tells me to do the same things but no joy!

Share this post


Link to post
Share on other sites
however Ive just tried this and it works:

// Do Matrix stuff
m[0] ...
...

glPushMatrix();
glMultMatrixf();
draw();
glPopMatrix();

.... But why isnt glLoadIdentity and glLoadMatrixf working?


[Edited by - dawberj3 on April 20, 2006 4:33:52 AM]

Share this post


Link to post
Share on other sites
I assumed (for no particular reason) that your view matrix was identity, in which case your first example would work fine. But if you have a non-identity view matrix set up (that is, you've moved your camera), your first example would erase that matrix, most likely resulting in your object not being visible.

Using glMultMatrix() instead of glLoadIdentity()/glLoadMatrix() combines the model matrix with the view matrix rather than overwriting it, which I'm guessing is what you wanted to do in the first place.

Share this post


Link to post
Share on other sites
Yeah I am moving my camera first, thx for clearing it up, I have the transformations working now, just one more question:

How can I load a new matrix properly, or is this not possible? :

-----------------------------------------------------------------------

// Do Matrix Stuff
...
m[12] = 10.0f;
m[13] = 10.0f;
m[14] = 10.0f;

glPushMatrix();
glMultMatrix();
glTranslatef(10.0,10.0,10.0);
draw();

glGetFloatv(GL_MODELVIEW_MATRIX, m); // <-- Wrong function perhaps?

// By printing the matrix im expected m[12], m[13] and m[14] to be 20.0
// buts the entire matrix has loads of wierd values.

glPopMatrix()

Thx

Share this post


Link to post
Share on other sites
Sorry for being a pain but I also have another problem.

I need to rotate things so Ive got a function to create a rotation matrix, but my object is being squashed and skeewd(sp :P) in some places, ive taken the function to create the rotationMatrix from a book so it should work. (The object is indeed rotating correctly but the object is being squashed??)


void Train::createRotationMatrix(float angle, float x, float y, float z)
{
float sinSave, cosSave, oneMinusCos;
float xx, yy, zz, xy, yz, zx, xs, ys, zs;

// If NULL vector passed in, this will blow up...
if(x == 0.0f && y == 0.0f && z == 0.0f)
return;

sinSave = (float)sin(angle);
cosSave = (float)cos(angle);
oneMinusCos = 1.0f - cosSave;

xx = x * x;
yy = y * y;
zz = z * z;
xy = x * y;
yz = y * z;
zx = z * x;
xs = x * sinSave;
ys = y * sinSave;
zs = z * sinSave;

this->m[0] = (oneMinusCos * xx) + cosSave;
this->m[4] = (oneMinusCos * xy) - zs;
this->m[8] = (oneMinusCos * zx) + ys;
this->m[12] = 0.0f;

this->m[1] = (oneMinusCos * xy) + zs;
this->m[5] = (oneMinusCos * yy) + cosSave;
this->m[9] = (oneMinusCos * yz) - xs;
this->m[13] = 0.0f;

this->m[2] = (oneMinusCos * zx) - ys;
this->m[6] = (oneMinusCos * yz) + xs;
this->m[10] = (oneMinusCos * zz) + cosSave;
this->m[14] = 0.0f;

this->m[3] = 0.0f;
this->m[7] = 0.0f;
this->m[11] = 0.0f;
this->m[15] = 1.0f;
}
[/souce]

Can anyone spot anything?

Thx

Share this post


Link to post
Share on other sites
EDIT: Because I misread some stuff.

In the example where you call glGetFloatv(), is the matrix identity before you push it? If it isn't you're going to get results different from what you might expect.

Share this post


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

  • Advertisement