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

#include "global.h"

static skin_t front_speaker_skin,
              sub_speaker_skin;

static float sub_scale;
#define SPEAKER_TEXTURE "C:/DiscoStu/gfx/Speaker/box.tga"
#define FRONT_SPEAKER_TEXTURE "C:/DiscoStu/gfx/Speaker/boxfront.tga"
#define SUB_SPEAKER_TEXTURE "C:/DiscoStu/gfx/Speaker/sub.tga"
// BS: My first character =)
/*
   Listen to DSP:
*/
void SpeakerListen1(edict_t *self, struct winampVisModule *thismod)
{
   sub_scale = .4 +  dsp.kick / 2;
   if (sub_scale > 1)
      sub_scale = 1;   
   sub_scale *= .3f;
}

/*
   BS:
   Render Boba Fett.
*/
void SpeakerRender(edict_t *self)
{
   glEnable(GL_DEPTH_TEST);
   glBindTexture (GL_TEXTURE_2D, self->skin_index); 
   last_texture = self->skin_index;
   // TOP SIDE:
   glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2f(1, 1);
      glVertex3f(-15, 30, -15);
      glTexCoord2f(1, 0);
      glVertex3f(-15, 30, 15);
      glTexCoord2f(0, 1);
      glVertex3f(15, 30, -15);
      glTexCoord2f(0, 0);
      glVertex3f(15, 30, 15);
   glEnd();   
   // RIGHT SIDE:
   glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2f(1, 1);
      glVertex3f(-15, 30, -15);
      glTexCoord2f(1, 0);
      glVertex3f(-15, 0, -15);
      glTexCoord2f(0, 1);
      glVertex3f(-15, 30, 15);
      glTexCoord2f(0, 0);
      glVertex3f(-15, 0, 15);
   glEnd();   
   // LEFT SIDE:
   glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2f(1, 1);
      glVertex3f(15, 30, 15);
      glTexCoord2f(1, 0);
      glVertex3f(15, 0, 15);
      glTexCoord2f(0, 1);
      glVertex3f(15, 30, -15);
      glTexCoord2f(0, 0);
      glVertex3f(15, 0, -15);
   glEnd();   

   glBindTexture (GL_TEXTURE_2D, front_speaker_skin.index); 
   // FRONT SIDE:
   glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2f(1, 0);
      glVertex3f(-15, 30, 15);
      glTexCoord2f(1, 1);
      glVertex3f(-15, 0, 15);
      glTexCoord2f(0, 0);
      glVertex3f(15, 30, 15);
      glTexCoord2f(0, 1);
      glVertex3f(15, 0, 15);
   glEnd();   
   
   glBindTexture (GL_TEXTURE_2D, sub_speaker_skin.index); 
   glDepthMask(0);
   glTranslatef(0, 10, 0);
   glScalef(sub_scale, sub_scale, 1);
   glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2f(1, 0);
      glVertex3f(-15, 15, 15.5);
      glTexCoord2f(1, 1);
      glVertex3f(-15, -15, 15.5);
      glTexCoord2f(0, 0);
      glVertex3f(15, 15, 15.5);
      glTexCoord2f(0, 1);
      glVertex3f(15, -15, 15.5);
   glEnd();   
   glDepthMask(1);
   glDisable(GL_DEPTH_TEST);
}

/*
   BS:
   Think function for Boba Fett.
*/
void SpeakerThink(edict_t *self)
{
}

/*
   BS:
   Spawn Boba Fett.
*/
edict_t *SpeakerSpawn()
{
   static unsigned int skinned = NOT_FOUND, modeled = NOT_FOUND;
   edict_t *ent;
   skin_t *skin;
   ent = EdictSpawn();

   // BS: Only load skins if they are not allocated already:
   if (skinned == NOT_FOUND)
      {
      // BS: Does skin exist in library?
      if ((ent->skin_num = SkinIndex(SPEAKER_TEXTURE)) == NOT_FOUND)
         {
         int ix, iy;
         skin = &front_speaker_skin;
         memset(skin, 0, sizeof(*skin));
         if (!LoadTGA(FRONT_SPEAKER_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", FRONT_SPEAKER_TEXTURE);
         //glGenTextures (1, &ent->skin_index);
         front_speaker_skin.index = eng.skin_index++;
         glBindTexture (GL_TEXTURE_2D, front_speaker_skin.index);
         TGA2GLTexture (skin, 0, 0, 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 = &sub_speaker_skin;
         memset(skin, 0, sizeof(*skin));
         if (!LoadTGA(SUB_SPEAKER_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", SUB_SPEAKER_TEXTURE);
         //glGenTextures (1, &ent->skin_index);
         sub_speaker_skin.index = eng.skin_index++;
         glBindTexture (GL_TEXTURE_2D, sub_speaker_skin.index);
         TGA2GLTexture (skin, 0, TGA_ABSOLUTE_BLEND, 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 = new skin_t;
         memset(skin, 0, sizeof(*skin));
         ent->skin_num = SkinSpawn(SPEAKER_TEXTURE, skin);
         if (!LoadTGA(SPEAKER_TEXTURE, &skin->tga, skin->width, skin->height))
            EngineError("TGA LOAD: Skin '%s' could not be loaded.\n", SPEAKER_TEXTURE);
         //glGenTextures (1, &ent->skin_index);
         ent->skin_index = eng.skin_index++;
         glBindTexture (GL_TEXTURE_2D, ent->skin_index);
         TGA2GLTexture (skin, 0, 0, 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;

   VectorSet(ent->rgba, 1, 1, 1);
   ent->rgba[3] = 1;
   ent->loop_frame = -1;
   ent->scale = .4f;
   ent->render = SpeakerRender;
   ent->listen = SpeakerListen1;
   return ent;
}