/*
   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:
      Hatch module.
*/


#include "global.h"

#define HATCH_TEXTURE "c:/DiscoStu/gfx/floor2.tga"
extern skin_t r_chrome_skin;

/*
   NT:
   Render Hatch:
*/
void HatchRender(edict_t *self)
{
   register float env_x, env_y;
   env_x = cam.origin[0] / 330.0f;
   env_y = -cam.origin[2] / 330.0f;

   if (last_texture != self->skin_index)
      glBindTexture (GL_TEXTURE_2D, self->skin_index); 
   glEnable(GL_DEPTH_TEST);
   glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2f(0, 0);
      glVertex3f(-20, 0, 20);
      glTexCoord2f(1, 0);
      glVertex3f(20, 0, 20);
      glTexCoord2f(0, 1);
      glVertex3f(-20, 0, -20);
      glTexCoord2f(1, 1);
      glVertex3f(20, 0, -20);
   glEnd();

   glBindTexture (GL_TEXTURE_2D, r_chrome_skin.index); 
   last_texture = r_chrome_skin.index;
   glColor4f(1, 1, 1, .22f);
   glDepthMask(false);
   glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2f(0 + env_x, .10 + env_y);
      glVertex3f(-20, 1, 20);
      glTexCoord2f(.10 + env_x, .10 + env_y);      
      glVertex3f(20, 1, 20);
      glTexCoord2f(0 + env_x, 0 + env_y);
      glVertex3f(-20, 1, -20);
      glTexCoord2f(.10 + env_x, 0 + env_y);
      glVertex3f(20, 1, -20);
   glEnd();
   glDepthMask(true);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glDisable(GL_DEPTH_TEST);
}

/*
   NT:
   Spawn Hatch.
*/
edict_t *HatchSpawn()
{
   static unsigned int skinned = NOT_FOUND;
   edict_t *ent;
   skin_t *skin;
   ent = EdictSpawn();

   // 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(HATCH_TEXTURE)) == NOT_FOUND)
         {
         int ix, iy;
         skin = new skin_t;
         memset(skin, 0, sizeof(*skin));
         ent->skin_num = SkinSpawn(HATCH_TEXTURE, skin);
         if (!LoadTGA(HATCH_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", HATCH_TEXTURE);
         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;
         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_PHYS | ENT_ST_VISIBLE;

   ent->origin[0] = 0;
   ent->origin[1] = -24;
   ent->origin[2] = 0;
   ent->rgba[0] = 1;
   ent->rgba[1] = 1;
   ent->rgba[2] = 1;
   ent->rgba[3] = 1;
   ent->scale = 0.4f;
   ent->render = HatchRender;
   return ent;
}