Resize windo

Dear OpenGl developers,
I am new to this forum and to OpenGL, so hello everyone and thanks in advance for your help,
and please accept my sincerest apologies in advance for the stupid questions I will ask here,
and that will bother you more than anything.

I am learning (self education) OpenGL and I have a problem when resizing my window scene,
thus I found many tutorials on line with the following lines (roughly):

void reshape (int width, int height)
{
  double aspect;
 
  glViewport (0, 0, width, height);
  aspect = (double) width / (double) height;
  double fos = 10.0;
  double near = 1.0;
  double far = 10.0;
  double top, bottom, right, left;

  top = tan (fos * pi / 360.0) * near;
  right = top * aspect;
  left = - right;
  bottom = - top;
  
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  
  gluPerspective (fos, aspect, near, far);

/* or alternatively with glFrustum
  glFrustum (left, right, bottom, top, near, far); */

  glMatrixMode (GL_MODELVIEW);
  glLoadIdentity ();
} 

First I hope that this code is ok, otherwise any help to correct it will be highly appreciated.
At this point I face some troubles:

(1) the first being the use of the instruction:

glLoadMatrix (GL_MODELVIEW)

if I use this instruction then I have an empty screen after resize
(or at least I can not see what should be displayed … 2 spheres drawn using gluSphere in my case) …
in order to use that instruction I have to insert before that a new instruction:

glLoadIdentity ();

any idea why ? … is there anything wrong ?

(2) after resize I want to keep an aspect ratio that will not stretch my 2 spheres,
but no matter what I am trying the image always appears to be stretched …
… any idea of what I am doing wrong here ?

Again thanks in advance for your help.

S.

If I use this instruction then I have an empty screen after resize

It could be that your models are ending up behind the eye (camera). Apply a translation such as glTranslatef(0.0, 0.0, -10.0) or whatever z value that is needed.

PS: they are called functions, not instructions.

Hello and thanks for your answer,
well I corrected few things already including that black screen problem, and yes you were right about the ‘behind the eye’ camera.
Here is my new code, but still my spheres are stretched and no way to keep them spherical after re-size:

typedef struct {
  gboolean init;
  GLdouble ang;
  GLdouble left, right;
  GLdouble bottom, top;
  GLdouble near, far;
} glwin;
 
void reshape (glwin * view, int width, int height)
{
  double aspect;
  if (view -> init)
  {
    glViewport (0, 0, (GLsizei) width, (GLsizei) height);
    aspect = (double) width / (double) height;
 
    view -> ang = 10.0;
    view -> near = sin (view -> ang  * pi / 360.0) - 1.0;
    view -> far = sin (view -> ang * pi / 360.0) + 1.0;
    if (width > height)
    {
      view -> top = tan (view -> ang * pi / 360.0) * view -> near;
      view -> right = view -> top * aspect;
    }
    else
    {
      view -> right = tan (view -> ang * pi / 360.0) * view -> near;
      view -> top = view -> right / aspect;
    }
    view -> left = - view -> right;
    view -> bottom = - view -> top;
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
 
    // gluPerspective (view -> ang, aspect, view -> near, view -> far);
    // glOrtho (view -> left, view -> right, view -> bottom, view -> top, view -> near, view -> far);
    glFrustum (view -> left, view -> right, view -> bottom, view -> top, view -> near, view -> far);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
  }
}

To keep the aspect ratio stable, you need to do width/height


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, (float)width/(float)height, near, far);

and that keeps the aspect stable no matter what the window size is.
If weird things are happening on resizing window, then you have some code that is changing the projection matrix from elsewhere in your code.

Thanks everyone, problem solved, a bad matrix transformation somewhere, here is my new code for info:


enum representation {
  ORTHOGRAPHIC = 0,
  PERSPECTIVE = 1,
};
 
typedef struct {
  GLdouble near, far;
  int rep;                      // orthographique ou perspective
} glwin;
 
void reshape (glwin * view, int width, int height)
{
  view -> near = 1.0;
  view -> far = 5.0;
  glLoadIdentity ();
  glViewport (0, 0, (GLsizei) width, (GLsizei) height);
  aspect = (double) width / (double) height;
  glLoadIdentity ();
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  if (view -> rep == PERSPECTIVE)
  {
    if (aspect > 1.0)
    {
      glFrustum (- aspect, aspect, -1.0, 1.0, view -> near, view -> far);
    }
    else
    {
      glFrustum (-1.0, 1.0, -1.0 / aspect, 1.0 / aspect, view -> near, view -> far);
    }
  }
  else
  {
    if (aspect > 1.0)
    {
      glOrtho (- aspect, aspect, -1.0, 1.0, view -> far, view -> far);
    }
    else
    {
      glOrtho (-1.0, 1.0, -1.0 / aspect, 1.0 / aspect, view -> far, view -> far);
    }
  }
  glMatrixMode (GL_MODELVIEW);
  glLoadIdentity ();
}

Thanks again for your help !