
#include <stdafx.h>



#include "..\openglObj\openglObj.h"
#include "..\openglObj\GlDefines.h"
#include "..\openglObj\glutstroke.h"
#include "..\windowObj\windowObj.hpp"
#include "..\lwoObj\lwoObj.hpp"
#include <math.h>

#include "glUtil.h"



extern class	win			ge;







void gluLookAt( float eyex, float eyey, float eyez,
                float centerx, float centery, float centerz,
                float upx, float upy, float upz ) {
 double m[16], x[3], y[3], z[3], mag;
 /* Make rotation matrix */
 /* Z vector */
 z[0] = eyex - centerx; z[1] = eyey - centery; z[2] = eyez - centerz;
 mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
 if (mag) { z[0] /= mag; z[1] /= mag; z[2] /= mag; }
 /* Y vector */
 y[0] = upx; y[1] = upy; y[2] = upz;
 /* X vector = Y cross Z */
 x[0] =  y[1]*z[2] - y[2]*z[1]; x[1] = -y[0]*z[2] + y[2]*z[0]; x[2] =  y[0]*z[1] - y[1]*z[0];
 /* Recompute Y = Z cross X */
 y[0] =  z[1]*x[2] - z[2]*x[1]; y[1] = -z[0]*x[2] + z[2]*x[0]; y[2] =  z[0]*x[1] - z[1]*x[0];
 mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
 if (mag) { x[0] /= mag; x[1] /= mag; x[2] /= mag; }
 mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
 if (mag) { y[0] /= mag; y[1] /= mag; y[2] /= mag; }
 #define M(row,col)  m[col*4+row]
  M(0,0) = x[0];  M(0,1) = x[1];  M(0,2) = x[2];  M(0,3) = 0.0; M(1,0) = y[0];  M(1,1) = y[1];  M(1,2) = y[2];  M(1,3) = 0.0; M(2,0) = z[0];  M(2,1) = z[1];  M(2,2) = z[2];  M(2,3) = 0.0; M(3,0) = 0.0;   M(3,1) = 0.0;   M(3,2) = 0.0;   M(3,3) = 1.0;
 #undef M
 ge.dll.glMultMatrixd( m );
 /* Translate Eye to Origin */
 ge.dll.glTranslatef( -eyex, -eyey, -eyez );
}



void gluPerspective( float fovy, float aspect,
                     float zNear, float zFar ) {
 double xmin, xmax, ymin, ymax;
 ymax = zNear * tan( fovy * 3.14159 / 360.0 );
 ymin = -ymax; xmin = ymin * aspect; xmax = ymax * aspect;
 ge.dll.glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
}








StrokeFontRec glutStrokeRoman; // = { "Roman", 128, chars, 119.048, -33.3333 };



void *__glutFont ( void *font ) {
//  switch((int)font) {
//  case (int)GLUT_STROKE_ROMAN:
    return &glutStrokeRoman;
/*
  case (int)GLUT_STROKE_MONO_ROMAN:
    return &glutStrokeMonoRoman;
  case (int)GLUT_BITMAP_9_BY_15:
    return &glutBitmap9By15;
  case (int)GLUT_BITMAP_8_BY_13:
    return &glutBitmap8By13;
  case (int)GLUT_BITMAP_TIMES_ROMAN_10:
    return &glutBitmapTimesRoman10;
  case (int)GLUT_BITMAP_TIMES_ROMAN_24:
    return &glutBitmapTimesRoman24;
  case (int)GLUT_BITMAP_HELVETICA_10:
    return &glutBitmapHelvetica10;
  case (int)GLUT_BITMAP_HELVETICA_12:
    return &glutBitmapHelvetica12;
  case (int)GLUT_BITMAP_HELVETICA_18:
    return &glutBitmapHelvetica18;
*/
  //}
}

void glutStrokeCharacter( GLUTstrokeFont font, int c)
{
  const StrokeCharRec *ch;
  const StrokeRec *stroke;
  const CoordRec *coord;
  StrokeFontPtr fontinfo;
  int i, j;


#if defined(WIN32)
  fontinfo = (StrokeFontPtr) __glutFont(font);
#else
  fontinfo = (StrokeFontPtr) font;
#endif

  if (c < 0 || c >= fontinfo->num_chars)
    return;
  ch = &(fontinfo->ch[c]);
  if (ch) {
    for (i = ch->num_strokes, stroke = ch->stroke;
      i > 0; i--, stroke++) {
      ge.dll.glBegin(GL_LINE_STRIP);
      for (j = stroke->num_coords, coord = stroke->coord;
        j > 0; j--, coord++) {
        ge.dll.glVertex2f(coord->x, coord->y);
      }
      ge.dll.glEnd();
    }
    ge.dll.glTranslatef(ch->right, 0.0, 0.0);
  }
}






void gluOrtho2D( double left, double right,
                 double bottom, double top )
{
   ge.dll.glOrtho( left, right, bottom, top, -1.0, 1.0 );
}









/* 
   general purpose text routine.  draws a string according to the
   format in a stroke font at x, y after scaling it by the scale
   specified.  x, y and scale are all in window-space [i.e., pixels]
   with origin at the lower-left.
*/

void text(int x, int y, float scale, char* format, ...) {
    va_list args;
    char buffer[255], *p;
    GLfloat font_scale =(float) (119.05 + 33.33);

    va_start(args, format);
    vsprintf(buffer, format, args);
    va_end(args);

    ge.dll.glMatrixMode(GL_PROJECTION);
    ge.dll.glPushMatrix();
    ge.dll.glLoadIdentity();
    gluOrtho2D(0, 640, 0, 480);

    ge.dll.glMatrixMode(GL_MODELVIEW);
    ge.dll.glPushMatrix();
    ge.dll.glLoadIdentity();

    ge.dll.glPushAttrib(GL_ENABLE_BIT);
    ge.dll.glDisable(GL_LIGHTING);
    ge.dll.glDisable(GL_TEXTURE_2D);
    ge.dll.glEnable(GL_DEPTH_TEST);
    ge.dll.glEnable(GL_BLEND);
    ge.dll.glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    ge.dll.glTranslatef(x, y, 0.0);

    ge.dll.glScalef(scale/font_scale, scale/font_scale, scale/font_scale);

    for(p = buffer; *p; p++)
//	glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
	glutStrokeCharacter(0, *p);
  
    ge.dll.glPopAttrib();

    ge.dll.glPopMatrix();
    ge.dll.glMatrixMode(GL_PROJECTION);
    ge.dll.glPopMatrix();
    ge.dll.glMatrixMode(GL_MODELVIEW);
}
