PDA

View Full Version : glPushMatrix & glTranslate



MrWendell1982
03-03-2003, 07:04 AM
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.

shinpaughp
03-03-2003, 07:14 AM
It shouldn't happen. Show some code.

MrWendell1982
03-03-2003, 07:26 AM
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..,\n";
glPushMatrix();
objects[0]->draw();
glPopMatrix();
}

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

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

glTranslatef(pos[0], pos[1], pos[2]);
cout << "Drawing Sphere...\n";
glutSolidSphere(size[0], size[1], size[2]);
}

MrWendell1982
03-03-2003, 07:27 AM
Sorry, this is what the setupScene() should look like, thanks!

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

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

nexusone
03-04-2003, 12:20 PM
Are you using gluPerspective?

MrWendell1982
03-04-2003, 01:23 PM
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

nexusone
03-05-2003, 01:19 PM
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:
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

MrWendell1982
03-06-2003, 09:57 AM
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!

nexusone
03-06-2003, 12:03 PM
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:
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!



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