problem with camera orbiting around object -- gluLookAt

Hi,

I am totally beginner with opengl. I have a very simple task: given a 3d model, I need to rotate a camera around it on a semi-sphere (y>0) and get the views from every angle.
I use gluLookAt, with spherical coordinates for eyex,eyey,eyez. The object is in (0,0,0) and up vector is (0,1,0). The camera moves on a sphere of radius 2.
My camera starts near (0,0,2) and walks around the object. The problem is when eyez gets <=0. My object gets flipped and “goes back”; it does not do the full 360 rotation. And the images I obtain when eyez <=0 are actually mirror versions of my object. The other images are ok.
I checked the eye coordinates, they are correct; also in the modelview matrix, the vectors seem coherent, so I don’t understand why the flipping.
I am putting here some code, maybe someone can spot the error(s).

This is the loop where I do the camera orbiting:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(15.0f,1.0f,0.1f,4.0f);  
step = 10;
theta = 80; 
while( theta > 0 )
{        
  phi = 0;
  while( phi < 360 )
    {
      get_pos( radius, theta, phi, camera_pos );            
      glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      gluLookAt( camera_pos[0], camera_pos[1], camera_pos[2], 0, 0, 0, 0, 1, 0 ); */          
      draw_triangles();
      glutSwapBuffers();            
            
      glGetDoublev(GL_MODELVIEW_MATRIX, modelview);    
      glGetDoublev(GL_PROJECTION_MATRIX, projection);    
      glGetIntegerv(GL_VIEWPORT, viewport);
      
      write_image( imname , compression_params ); 
      phi += step;           
    }
  theta -= step; 
}

To get the EYE coordinates I have:


void get_pos( double radius, double theta, double phi, double *camera_pos )
{
  /* get angles in radians */
  double th = theta*0.0174532;
  double ph = phi*0.0174532;
  camera_pos[0] = radius * sin(th) * sin(ph);
  camera_pos[1] = radius * cos(th);
  camera_pos[2] = radius * sin(th) * cos(ph);
}

If anyone could give me a hint of what I’m doing wrong, that would be really great. I doubt it’s a gimbal lock problem, I suspect more a silly beginner error.

Thanks in advance !