glPushMatrix & glTranslate

Hi,

I am trying to translate and draw two spheres but i am having some trouble. At the moment my main display function calls another method that pushes the matrix on the stack, translates along the x-axis and draws the sphere, the matrix is then popped off the stack and the same is repeated for the second sphere.

The problem is the second sphere is not only translated but is also scaled by the same amount as the translation (and there is no call to glScalef() anywhere).

How do i solve this? Many thanks.

It shouldn’t happen. Show some code.

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

gluLookAt(cam[0], cam[1], cam[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
scn1.setupScene();

}

/////////////////////////////////////////

void OGLScene::setupScene()
{

//cout << "Setting up scene..,

";
glPushMatrix();
objects[0]->draw();
glPopMatrix();
}

///////////////////////////////////////////

void Sphere::draw()
{
glMaterialfv(GL_FRONT, GL_DIFFUSE, DIFFUSE);

	glTranslatef(pos[0], pos[1], pos[2]);
	cout << "Drawing Sphere...

";
glutSolidSphere(size[0], size[1], size[2]);
}

Sorry, this is what the setupScene() should look like, thanks!

void OGLScene::setupScene()
{
//cout << "Setting up scene…,
";
glPushMatrix();
objects[0]->draw();
glPopMatrix();

glPushMatrix();
objects[1]-&gt;draw();
glPopMatrix();

}

Are you using gluPerspective?

Hi,

Yes i am using gluPerspective(), and i think this is where the problem lies.

I changed the code to use glOrtho and this problem stopped, however there was obviously no perspective. Do you know how i can solve this problem

Thanks in advance

You need to understand the diffrence between ortho and perspective views.

Ortho is draw without any adjustment for distance like a 2D drawing.

Perspective add’s depth to the scene by scaling the object’s as the get farther away.
In real life a Ball even though it’s size has not changed, looks smaller at a distance.
This is what perspective view does, it changes the balls size based on distance from the camara and all other objects the same way. This way your objects locations are the same in the world and scale of objects between each other does not have to be adjusted since the perspective view does it for you.

Originally posted by MrWendell1982:
[b]Hi,

Yes i am using gluPerspective(), and i think this is where the problem lies.

I changed the code to use glOrtho and this problem stopped, however there was obviously no perspective. Do you know how i can solve this problem

Thanks in advance[/b]

Hi,

Thanks for your reply. I was aware of perspective and what it did its just that when i translated the sphere it turned egg shaped quite dramtically! I did not expect it to have as great an effect as it did. Anyway, i moved the sphere into the distance a bit and reduced the viewing angle and this seemed to help.

Thanks once again!

Yea, in perspective view too large of view angle can distort the image, I use a 60 degree angle.

Also look at your near/far setting in the perspective, that also can have a big effect.
The near can not be less or equal zero, also not good to have a large distance between near and far.

Originally posted by MrWendell1982:
[b]Hi,

Thanks for your reply. I was aware of perspective and what it did its just that when i translated the sphere it turned egg shaped quite dramtically! I did not expect it to have as great an effect as it did. Anyway, i moved the sphere into the distance a bit and reduced the viewing angle and this seemed to help.

Thanks once again![/b]

[This message has been edited by nexusone (edited 03-06-2003).]