/*
   CMSC 427 - Term Project Candidate
   ---------------------------------

   Beth Weinstein - bweinste@wam.umd.edu
   Ben Stewart - stewartb@wam.umd.edu
   Nawfel Tricha - Topaz@endorproductions.com

   Minimal OpenGL project using Microsoft Visual C++.
   This project is a premilinary candidate for the term 
   project required by CMSC 427.

   ABSTRACT:
      In this project, we will develop several fast 
      rendering and collision routines which would 
      require C/C++ speed and accuracy and then 
      implement a VisualJ++ interface.

   MODULE:
      Frame Alias module.
*/

#include "global.h"

skin_t chrome_skin;

#define FRAME_MODEL "c:/DiscoStu/gfx/Frame/tris.md2"
#define FRAME_TEXTURE "c:/DiscoStu/gfx/Frame/frame0.tga"
#define CHROME_TEXTURE "c:/DiscoStu/gfx/wall.tga"

/*
   NT:
   Frame camera control:
*/
void FrameCam(edict_t *self)
{
   static float xrotate = 180, x_skip = .3f;
   vec3_t arrow = {30, 0, 0};
   arrow[1] = xrotate;
   xrotate += x_skip;
   if (xrotate < 135 || xrotate > 225)
      x_skip = -x_skip;
   AngleVectors (arrow, arrow, NULL, NULL);
   VectorMA(arrow, 35, self->origin, cam.origin);
   VectorSubtract (self->origin, cam.origin, arrow);
   VectorAngles(arrow, cam.angle);

   glLoadIdentity();   
   glRotatef(-cam.angle[ROLL], 0, 0, 1);
   glRotatef(-cam.angle[PITCH], 1, 0, 0);
   glRotatef(-cam.angle[YAW], 0, 1, 0);
   glTranslatef(-cam.origin[0], -cam.origin[1], -cam.origin[2]);      
}


/*
   NT:
   Render Frame.
*/
void FrameRender(edict_t *self)
{
   if (last_texture != self->skin_index)
      {
      glBindTexture (GL_TEXTURE_2D, self->skin_index); 
      last_texture = self->skin_index;
      }   
   glEnable(GL_DEPTH_TEST);
   RenderAliasModel(self);
   glDisable(GL_DEPTH_TEST);
}


/*
   NT:
   Spawn Chair.
*/
edict_t *FrameSpawn()
{
   static unsigned int skinned = NOT_FOUND, modeled = NOT_FOUND;
   int model_index;
   edict_t *ent;
   skin_t *skin;
   ent = EdictSpawn();

   if (modeled == NOT_FOUND)
      {
      if ((model_index = CMD2Exist(FRAME_MODEL)) == NOT_FOUND)
         {
         ent->model = new CMD2;
         if (CMD2Spawn(FRAME_MODEL, ent->model) == NOT_FOUND)
            return NULL;
         if (ent->model->Read(FRAME_MODEL) != 1)
            EngineError("SpawnFrame: Error reading Alias md2 '%s'", FRAME_MODEL);
         }
      else
         {
         ent->model = g_CMD2s[model_index]->model;
         }
      }
   else
      ent->model = g_CMD2s[modeled]->model;

   // NT: Only load skins if they are not allocated already:
   if (skinned == NOT_FOUND)
      {
      // NT: Does skin exist in library?
      if ((ent->skin_num = SkinIndex(FRAME_TEXTURE)) == NOT_FOUND)
         {
         int ix, iy;
         /*
         skin = &chrome_skin;
         memset(skin, 0, sizeof(*skin));
         if (!LoadTGA(CHROME_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", CHROME_TEXTURE);
         //glGenTextures (1, &ent->skin_index);
         chrome_skin.index = eng.skin_index++;
         glBindTexture (GL_TEXTURE_2D, chrome_skin.index);
         TGA2GLTexture (skin, 0, TGA_ABSOLUTE_BLEND, ix, iy);
	      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	      glTexImage2D (GL_TEXTURE_2D, 0, 4, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, skin->glTexture);
         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);	
         */

         skin = new skin_t;
         memset(skin, 0, sizeof(*skin));
         ent->skin_num = SkinSpawn(FRAME_TEXTURE, skin);
         if (!LoadTGA(FRAME_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", FRAME_TEXTURE);
         //glGenTextures (1, &ent->skin_index);
         ent->skin_index = eng.skin_index++;
         glBindTexture (GL_TEXTURE_2D, ent->skin_index);
         TGA2GLTexture (skin, 0, 255, ix, iy);
	      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	      glTexImage2D (GL_TEXTURE_2D, 0, 4, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, skin->glTexture);
         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	      glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE);	
         skin->index = ent->skin_index;
         skinned = ent->skin_index;
         UpdateTextureCoordinates(ent->model, skin);
         InitGL();      
         }
      else // Just use skin from already loaded list:
         ent->skin_index = g_skins[ent->skin_num]->skin->index;
      }
   else
      ent->skin_index = skinned;

   // State of entity:
   ent->state = ENT_ST_USED | ENT_ST_VISIBLE;

   ent->origin[0] = 0;
   ent->origin[1] = 60 * .4;
   ent->origin[2] = -399 * .4;
   ent->angle[YAW] = 90;
   ent->rgba[0] = 1;
   ent->rgba[1] = 1;
   ent->rgba[2] = 1;
   ent->rgba[3] = 1;
   ent->scale = 1.0f;
   ent->render = FrameRender;
   ent->camera = FrameCam;
   return ent;
}