//**************************************************************************
/*  
    smooth.c
    Nate Robins, 1997

    Model viewer program.  Excercises the glm library.
 */
//**************************************************************************
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include "global.h"
#include "OBJlib.h"
#include "skyfly.h"
//**************************************************************************
GLuint     model_list = 0;              /* display list for object */
char      model_file[200];              /* name of the obect file */
GLboolean  facet_normal = GL_FALSE;     /* draw with facet normal? */
GLMmodel*  model;
GLfloat    smoothing_angle = 90.0;      /* smoothing angle */
GLfloat    scale;                       /* scaling factor */
GLdouble   pan_x = 0.0;
GLdouble   pan_y = 0.0;
GLdouble   pan_z = 0.0;
GLboolean  performance = GL_FALSE;
GLboolean  stats = GL_FALSE;
GLfloat    weld_distance = 0.00001;
GLuint     material_mode = 0;
//**************************************************************************
void OBJinit(void);
//**************************************************************************
void drawOBJ(int wingID)
{
double plane_width = vort->wing[wingID].span;   // in metres

int shade_model;

glGetIntegerv(GL_SHADE_MODEL, &shade_model);
glShadeModel(GL_SMOOTH);

glMatrixMode (GL_MODELVIEW);
glPushMatrix();

double scaling_factor = plane_width*PLANE_XYZ_FACTOR/2.0;

glScalef(scaling_factor, scaling_factor, scaling_factor);


if (wingID == GV.trackingID)
        {
         GLfloat orig_color[4];
         GLfloat new_color[] = { 1.0, 0.0, 0.0, 1.0 };
         glGetFloatv(GL_LIGHT_MODEL_AMBIENT, orig_color);
         glLightfv(GL_LIGHT0, GL_DIFFUSE, new_color);
         glLightfv(GL_LIGHT0, GL_SPECULAR, new_color);

         glCallList(model_list);

         glLightfv(GL_LIGHT0, GL_DIFFUSE, orig_color);
         glLightfv(GL_LIGHT0, GL_SPECULAR, orig_color);
        }
else
        glCallList(model_list);

glPopMatrix();
glShadeModel(shade_model);
}
//**************************************************************************
int loadOBJfile(char *filename)
{
  strncpy(model_file, filename, 200);
  if (!model_file) {
    fprintf(stderr, "usage: loadOBJfile model_file.obj\n");
    exit(1);
  }

  OBJinit();

return 0;
}
//**************************************************************************
/* text: general purpose text routine.  draws a string according to
 * format in a stroke font at x, y after scaling it by the scale
 * specified (scale is in window-space (lower-left origin) pixels).  
 *
 * x      - position in x (in window-space)
 * y      - position in y (in window-space)
 * scale  - scale in pixels
 * format - as in printf()
 */
//**************************************************************************
void lists(void)
{
  GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  GLfloat diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
  GLfloat specular[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat shininess = 65.0;

  if (model_list)
    glDeleteLists(model_list, 1);

  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
  glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);

  /* generate a list */
  if (material_mode == 0) {
    if (facet_normal)
      model_list = glmList(model, GLM_FLAT);
    else
      model_list = glmList(model, GLM_SMOOTH);
  } else if (material_mode == 1) {
    if (facet_normal)
      model_list = glmList(model, GLM_FLAT | GLM_COLOR);
    else
      model_list = glmList(model, GLM_SMOOTH | GLM_COLOR);
  } else if (material_mode == 2) {
    if (facet_normal)
      model_list = glmList(model, GLM_FLAT | GLM_MATERIAL);
    else
      model_list = glmList(model, GLM_SMOOTH | GLM_MATERIAL);
  }
}
//**************************************************************************
void OBJinit(void)
{
  /* read in the model */
  model = glmReadOBJ(model_file);
  scale = glmUnitize(model);
  glmFacetNormals(model);
  glmVertexNormals(model, smoothing_angle);

  if (model->nummaterials > 0)
      material_mode = 2;

  /* create new display lists */
  lists();

  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

  glEnable(GL_DEPTH_TEST);

  glEnable(GL_CULL_FACE);
}
//**************************************************************************