How to properly move (move in and out of an object) in OpenGL

I've searched many forums (here and elsewhere) to find out how to properly scale an object in OpenGL. After reading a lot, I finally figured out how to use GluLookAt (). I can get the camera to move the way I want without any problem. I know it works because objects disappear as they should as the camera moves. However, I want to get closer to the subject when I pan the camera. I thought the camera position would take care of this, but updating the camera position doesn't seem to help. I also tried gScalef (), but it was only called once when I press the "w" key.

void keyboard(unsigned char c, int x,int y)
{
  if(c==27)
    {exit(0);}
  if (c=='w')
    {
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      float forward_x, forward_y,forward_z;
      gluLookAt(current[0], current[1], current[2],
        current[0]+center_x, current[1]+center_y, current[2]+center_z,
        0.0, 1.0, 0.0);  
      // glScalef(1.2,1.2,1.2);
      current[0]= (current[0]- 0.1*center_x);
      current[1]= (current[1]-0.1*center_y);
      current[2]= (current[2]- 0.1*center_z);

    }

}

void Camera_rotation(int x, int y)
{
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  center_x = cos(-0.05*x)*sin(-.05*y);
  center_y = cos(-.05*y);
  center_z = sin(0.05*x)*sin(.05*y);
  eye_x = x; eye_y = 1.0,
  float new_x = x; 
  gluLookAt(current[0], current[1], current[2],
            current[0]+center_x,
            current[1]+center_y,
            current[2]+center_z,
            0.0, 1.0, 0.0);
  render();
}

      

'current' and 'center_x [yz]' are global variables. Can anyone please help?

+3


source to share


2 answers


While the camera is aiming at the subject, you can set FOV

(field of view) to Perspective Projection Matrix

to achieve a zoom effect. The smaller FOV

, the smaller the viewing area, which creates the illusion of scaling.



A brilliant tutorial on the subject can be found here . He talks about scaling at the end.

+3


source


Possible solution after discussion:

In the render () function (or your render function) specify the projection matrix. This determines how your camera works. Code:



glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0,1.0,-1.0,1.0);// FOV = 30 degrees, aspect ratio is one and I can see throughout the plane

      

0


source







All Articles