Advertisement

Translated/Translatef

Started by April 05, 2009 06:39 PM
4 comments, last by Caglow 15 years, 7 months ago
Sorry about all the questions. I have (attempted) research on all OpenGL related topics and this is one (very basic I assume) principle that I just don't seem to get. How does Translate work? When placed inside a PushMatrix/PopMatrix and before the drawing of an object, I assume it would translate it by the specified 3D coordinates. ie, translatef(0.0, 0.0, 1.0f) would move the object 1 unit away from the camera. From the tutorials, I can tell that I'm clearly wrong about the z. When I try it myself, it moves the object down by 1 unit. Someone, please correct me and my mistake. How does translate REALLY work (I'd prefer if you don't go until matrices unless that's the only way to explain). And how would you move the object 1 unit away like I intended? Again, thanks for any and all help I recieve. [Edited by - Caglow on April 5, 2009 8:30:07 PM]
Hey,

your understanding of translatef should be correct, you just didn't notice that we're having a right handed coordinate system where +X goes to the right, +Y to the top and +Z towards the camera, so just pass -1.0 to glTranslatef and you should get the expected result.



-Carsten
Advertisement
I see...well, that fixes the X/Y part of it but the Z axis still doesn't seem to want to work as you say it should.

The code:

PushMatrix();
glTranslated(0, 0, 10);
glutSolidSphere(1.4, 150, 150);
PopMatrix();

According to what you say, that should move the "glutSolidSphere" closer to me by 10 units. However, this is what it does: http://aciqra.sf.net/images/z10.PNG

This is the original without a translate: http://aciqra.sf.net/images/z0.PNG

I'm not sure what it's exactly doing here...it's either moving the camera around the object or it's moving the light source.

Thanks.

EDIT: From what I just realized...I think translating an object closer or farther doesn't change its size. How would I go about "linking" the translate with a size change? It feels very obvious but I can't figure it out.
Set a perspective projection instead of an orthographic one. Check out glFrustum or gluPerspective instead of glOrtho or gluOrtho2D.
well according to your code it does move towards the screen, the only problem is that it moves "behind" the camera, and it can do this because i think you have an orthographic projection matrix and not a perspective one as one normally uses.
thus no matter how it moves along the z the size won't change.

the code for that looks something like this

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,1.3f,0.1f,1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

just place it as the first thing in your rendering loop
Now THAT works like magic! Thanks for the help!

This topic is closed to new replies.

Advertisement