OpenGl transformation help

Hi all,

Some one know how make view scaling dependent of the view position?
Until now i’ve used this lines of code:



glLoadIdentity();

  glTranslatef(xPan, yPan, -10);
  glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
  glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
  glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
  glScalef(zoomFactor,zoomFactor,zoomFactor);

  glPushAttrib(GL_ALL_ATTRIB_BITS);
  if (mScene)
    {  
      glPushMatrix();  
      glTranslatef(-helper.objectLocation.x,
                   -helper.objectLocation.y,
                   -helper.objectLocation.z);

      renderScene();   
      glPopMatrix();
    }

but doing so the scaling is relative to the point(0,0,0), it always zoom to the origin of the axis.
Instead i want that it zoom to the current viewport position or better to mouse position, just like blender does.

I assume your helper.objectLocation is the offset of the origin for the scale operation?



glLoadIdentity();

  glTranslatef(xPan, yPan, -10);
  glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
  glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
  glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);

  glPushAttrib(GL_ALL_ATTRIB_BITS);
  if (mScene)
    {  
      glPushMatrix();  
      glTranslatef(-helper.objectLocation.x,
                   -helper.objectLocation.y,
                   -helper.objectLocation.z);
      glScalef(zoomFactor,zoomFactor,zoomFactor);
      renderScene();   
      glPopMatrix();
    }

Haven’t tried it or thought too deep about it. Should work though.

Thanks for the reply!

but… for helper.objectLocation.y, yes you’r right, i use it so i have always the mesh centered in the origin of the axis. Instead your solution don’t work cause it scale the mesh object, but keep the grid at previous scale. When i call renderScene() i render the mesh with it’s material without the grid.

this is the wall code

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  glTranslatef(xPan, yPan, -10);
  glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
  glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
  glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
  glScalef(zoomFactor,zoomFactor,zoomFactor);

  glPushAttrib(GL_ALL_ATTRIB_BITS);
  if (mScene)
    {
      glPushMatrix(); //TODO aggiungere l'opzione
      glTranslatef(-helper.objectLocation.x,
                   -helper.objectLocation.y,
                   -helper.objectLocation.z);
      
      renderScene();
      if (mRenderVertNormals)
        renderVertNormals();
      if (mRenderFaceNormals)
        renderFaceNormals();
      //NOTE: mesh Center render
      glDisable(GL_LIGHTING);
      glDisable(GL_DEPTH_TEST);
      glPointSize(5);
      glColor3f(1,1,0);
      glBegin(GL_POINTS);
      glVertex3fv(&helper.objectLocation.x);
      glEnd();
      glPopAttrib();
      glPopMatrix();
    }
  glCallList(grid); //here i render the grid
  drawHUD(time.restart());

the scene is’t present at startup, cause i load it from file. so with your solution i can’t zoom if the scene is’nt present.

Well the cause and solution are obvious.
With the grid drawn outside the scaled matrix (after the pop) it’ll not appear scaled.
You have to differ between world and object transformations.
If you drag the grid inside the push/pop it will also be rotated and translated. Don’t know if those are world or object transformation parameters.
I think you haven’t fully grasped the matrix stack and transformation operations yet - a tricky beast.

You need to be aware of what you want, what you do and what parameters you got.

drawGrid()
pushMatrix()
scale()
drawObject()
popMatrix()
scales the object from its current axis origin not touching the grid.

pushMatrix()
scale()
drawGrid()
drawObject()
popMatrix()
scales the whole “scene” from its current axis origin, the object and the grid.

And if you start to add scale and rotate you have to be careful.
rotate->translate is not the same as translate->rotate

http://www.glprogramming.com/red/chapter03.html#name2

At the end i’ve discovered what to do i’ve added glTranslatef(0, 0, zoomFactor) before any transformation… doing so i move the camera only in the z axis as result of scaling. The only disadvantage is if i am far away from the origin, the panning become slower for low delta of mouse movement!
Anyway thanks for the help!!