scale first or translate? that is the big question.

Started by
1 comment, last by GekkoCube 20 years, 7 months ago

glPushMatrix();
         //glLoadIdentity();

         glScalef(scale, scale, scale);
         glTranslatef(x, y, z);
         
         glCallList(box);
glPopMatrix();
i believe im doing something wrong with not using loadidentity (because nehe uses it before every time a new glCallList is called within a loop in the display list tutorial). anyway, scaling and translating, or translating and scaling...either way i get incorrect resulsts. i want objects in the display list to appear at 3d coordinates i pass in. when Translated and scaled (in that order) i get something close, but the obects are rendered offset, some of them. when i scale and then translate, which seems to be the right order, then everything is clumped together near the origin. can anyone please help..?
Advertisement
The object is scaled from 0,0,0.
So if you have a cube (1x1x1) at 2,2,2 pos.
After scale 2x2x2 it will be from 2,2,2 to 4,4,4.
You have to move it to the 0,0,0 point.
Then scale, and then translate.

For example: object at Lx,Ly,Lz pos. Scale Sx,Sy,Sz, target pos Tx,Ty,Tz.
glTranslate3f(-Lx,-Ly,-Lz);
glScalef(Sx,Sy,Sz);
glTranslate3f(Tx/Sx,Ty/Sy,Tz/Sz);

I''m not sure, because I don''t use scale, because I want matrix ortonormal.

PS:sorry for my english


From what I know, because of OpenGL's weird matrix convention, the transformations you apply are performed in reverse order. For example, if you want to scale, and then translate the object, in OpenGL you first have to apply translate and then scale.
glTranslate, glScale and glRotate functions multiply the transformations to the current MODELVIEW matrix. All these transformations are thus acummulated in the matrix. glLoadIdentity is the function that resets the matrix. If the MODELVIEW wasn't the identity matrix when you apply your transformations, then any transformation that was in it will also apply to your data.
Scale, as someone noted before, scales each vertex from point (0, 0, 0). Usually, when you scale, you want to scale in respect to the center of the object, so you should first translate the center of the object to the origin. Then scale. Then you want to translate it back where it was plus where you want it to be.
BUT since your objects in the call list should be defined around point (0, 0, 0)(the origin) anyway, so that this is the aproximate center of the object, then you don't have to perform the first step anymore. So you would just want to scale and then translate where you want it to be. So you would translate first and then scale... because of OpenGL's matrix order as noted before.
At least that's what i think. Didn't try it. Why doesn't it work when you apply glLoadIdentity?

[edited by - madu on September 24, 2003 8:05:03 PM]

This topic is closed to new replies.

Advertisement