#include "exp004reshape.h"
#include "../view/exp004state0.h"
#include <GL/glut.h>
#include <stdio.h>

#define S exp004state0

void
exp004reshape (int w, int h)
{
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();

  /*
   * This scaling produces an odd effect when the coordinates are not
   * centered at 0,0.  When 0,0 is the lower left rather than the
   * center this reshape is a bit unnatural since the image is not
   * centered in the middle of the window.
   */

  if (w <= h)
    {      
      S.ortho.min_x = 
	S.ortho_min;
      S.ortho.max_x = 
	S.ortho_max;
      S.ortho.min_y = 
	S.ortho_min * (GLfloat) h / (GLfloat) w;
      S.ortho.max_y = 
	S.ortho_max * (GLfloat) h / (GLfloat) w;
    }
  else
    {
      S.ortho.min_x =
	S.ortho_min * (GLfloat) w / (GLfloat) h;
      S.ortho.max_x =
	S.ortho_max * (GLfloat) w / (GLfloat) h;
      S.ortho.min_y =
	S.ortho_min;
      S.ortho.max_y =
	S.ortho_max;
    }

  gluOrtho2D(S.ortho.min_x,
	     S.ortho.max_x,
	     S.ortho.min_y,
	     S.ortho.max_y);

  printf ("Ortho: %f %f %f %f\n",
	  S.ortho.min_x,
	  S.ortho.max_x,
	  S.ortho.min_y,
	  S.ortho.max_y);

  glMatrixMode (GL_MODELVIEW);

  // Set the viewport equal to the size of the window.
  glViewport (0, 0, (GLsizei) w, (GLsizei) h);

  S.viewport.w = w;
  S.viewport.h = h;

  return;
}