/* I seem to need this for glGenBuffers as per
   http://www.gamedev.net/community/forums/topic.asp?topic_id=422358 */
#define GL_GLEXT_PROTOTYPES

#include "exp004base.h"
#include "../view/exp004state0.h"
#include "../util/check_error.h"
#include <GL/glut.h>
#include <stdio.h>
#include <string.h>
#include "sqlca.h"
extern struct sqlca sqlca;

/*
 * A simple alias to make the code more readable.
 */
#define S exp004state0

void
exp004base (void)
{
  /* 
   * This implementation can be improved by mapping the video memory
   * directly rather than loading into system memory and then copying
   * into video memory.
  */

  /*
   * db2dclgn -d exp004 -t coordinates
   */
  EXEC SQL BEGIN DECLARE SECTION;
  EXEC SQL INCLUDE 'model/coordinates.h';  
  EXEC SQL END DECLARE SECTION;

  EXEC SQL DECLARE c2 CURSOR FOR
    SELECT * FROM coordinates;

  EXEC SQL OPEN c2;

  /* 
   * Initialize the bounding box of the points.
   */
  S.bb.min_x = 0.0;
  S.bb.max_x = 0.0;
  S.bb.min_y = 0.0;
  S.bb.max_y = 0.0;

  EXEC SQL FETCH c2 INTO :coordinates;
  while (sqlca.sqlcode != 100)
    {
      int i = coordinates.coord_id - 1;

      strncpy(S.gi_data[i], 
	      coordinates.gi.data, 
	      sizeof (S.gi_data[i]));

      S.base_vertices_data[i][0] = coordinates.x;
      S.base_vertices_data[i][1] = coordinates.y;

      if (S.base_vertices_data[i][0] < S.bb.min_x)
	S.bb.min_x = S.base_vertices_data[i][0];
      if (S.base_vertices_data[i][0] > S.bb.max_x)
	S.bb.max_x = S.base_vertices_data[i][0];
      if (S.base_vertices_data[i][1] < S.bb.min_y)
	S.bb.min_y = S.base_vertices_data[i][1];
      if (S.base_vertices_data[i][1] > S.bb.max_y)
	S.bb.max_y = S.base_vertices_data[i][1];

      /*
       * Deselected by default.
       */
      S.selection.set[i] = false;

      S.base_colors_data[i][0] = DEFAULT_COLOR_R;
      S.base_colors_data[i][1] = DEFAULT_COLOR_G;
      S.base_colors_data[i][2] = DEFAULT_COLOR_B;
      S.base_colors_data[i][3] = DEFAULT_COLOR_A;

      EXEC SQL FETCH c2 INTO :coordinates;
    }

  EXEC SQL CLOSE c2;

  EXEC SQL COMMIT;

  /*
   * Find the largest axis and use it to setup the projection.  This
   * is done to preserve the aspect ratio.  The aspect ratio should be
   * preserved since relative distance is a meaningful indicator in
   * the map.
   */

  // Min of min x or min y.
  if (S.bb.min_x <= S.bb.min_y)
    S.ortho_min = S.bb.min_x;
  else
    S.ortho_min = S.bb.min_y;
  S.ortho_min--;

  // Max of max x or max y.
  if (S.bb.max_x >= S.bb.max_y)
    S.ortho_max = S.bb.max_x;
  else
    S.ortho_max = S.bb.max_y;
  S.ortho_max++;

  // Invert the y coordinate to match up with the LGL Java viewer.
  for (int i = 0; i < ROWS; i++)
    S.base_vertices_data[i][1] = 
      S.ortho_max - S.base_vertices_data[i][1];

  // Move the origin (0,0) to the center of the data.
  S.ortho_min = 0.0;
  S.ortho_max = 0.0;

  for (int i = 0; i < ROWS; i++)
    {
      S.base_vertices_data[i][0] = 
	S.base_vertices_data[i][0] -
	(0.5 * (S.bb.max_x - S.bb.min_x));

      if (S.ortho_min > S.base_vertices_data[i][0])
	S.ortho_min = S.base_vertices_data[i][0];

      if (S.ortho_max < S.base_vertices_data[i][0])
	S.ortho_max = S.base_vertices_data[i][0];

      S.base_vertices_data[i][1] = 
	S.base_vertices_data[i][1] -
	(0.5 * (S.bb.max_y - S.bb.min_y));

      if (S.ortho_min > S.base_vertices_data[i][1])
	S.ortho_min = S.base_vertices_data[i][1];

      if (S.ortho_max < S.base_vertices_data[i][1])
	S.ortho_max = S.base_vertices_data[i][1];
    }

  glGenBuffers (2, S.buffers);

  glBindBuffer (GL_ARRAY_BUFFER, S.buffers[BASE_VERTICES]);
  glVertexPointer (2, GL_FLOAT, 0, 0);
  glBufferData (GL_ARRAY_BUFFER, 
		sizeof (S.base_vertices_data), S.base_vertices_data,
		GL_STATIC_DRAW);

  glBindBuffer (GL_ARRAY_BUFFER, S.buffers[BASE_COLORS]);
  glColorPointer (4, GL_FLOAT, 0, 0);
  glBufferData (GL_ARRAY_BUFFER,
		sizeof (S.base_colors_data), S.base_colors_data,
		GL_STATIC_DRAW);

  return;
}