/*

tesla 2 efekt

return:
  0 - doslo k chybe
  1 - este nezacal
  2 - prebehol v poriadku
  3 - uz skoncil
*/

#include <iostream>
#include "api3ds.h"
#include "efekt.h"
#include "efekt_12.h"
#include "syncs.h"

extern int sync_id;
extern BOOL usefog;
extern bool showlogo;

int  efekt_12::init()
{
  cout << "Efekt 12 init ... ";
  start=TRUE;
  counter=ZACIATOK12*refresh;

  tex_x_counter1=0;
  tex_y_counter1=0.0;
  uhol_counter1=0.0;
  real_in1=0.0;

  tex_x_counter2=0;
  tex_y_counter2=0.0;
  uhol_counter2=0.0;
  real_in2=0.0;

  int nas=500;

  tex_x_counter1=nas*txadd1;
  tex_y_counter1=nas*tyadd1;
  uhol_counter1 =nas*roll_speed1;
 
  tex_x_counter2=nas*txadd2;
  tex_y_counter2=nas*tyadd2;
  uhol_counter2 =nas*roll_speed2;


  intens1=0.0;
  intens2=0.0;
  rolovanie=0.0;

  cout << "ok!" << endl;
  return 1;
}

int  efekt_12::load()
{
    cout << "Loading efekt12 ... ";
    FILE *stream;

    stream=fopen(CFGNAME12,"r");
    if (stream==NULL) return 0;

    fscanf(stream,"pocet ploch          %i\n",&plochy1);
    fscanf(stream,"intenzita            %f\n",&intens1);
    fscanf(stream,"x-krok               %f\n",&txadd1);
    fscanf(stream,"y-krok               %f\n",&tyadd1);
    fscanf(stream,"roll speed           %f\n",&roll_speed1);
    fscanf(stream,"podstava valca       %i\n",&podstava1);
    fscanf(stream,"vyska valca          %i\n",&vyska1);
    float cl_r,cl_g,cl_b;
    fscanf(stream,"background color     %f,%f,%f\n",&cl_r,&cl_g,&cl_b);
    fscanf(stream,"visibility           %f\n",&vis);
    fscanf(stream,"minvisibility        %f\n",&minvis);
    fscanf(stream,"Descent origin       (%f,%f,%f)\n",&o1.x,&o1.y,&o1.z);
    fscanf(stream,"Descent right        (%f,%f,%f)\n",&i1.x,&i1.y,&i1.z);
    fscanf(stream,"Descent up           (%f,%f,%f)\n",&j1.x,&j1.y,&j1.z);
    fscanf(stream,"Descent forward      (%f,%f,%f)\n",&k1.x,&k1.y,&k1.z);

    fscanf(stream,"pocet ploch          %i\n",&plochy2);
    fscanf(stream,"intenzita            %f\n",&intens2);
    fscanf(stream,"x-krok               %f\n",&txadd2);
    fscanf(stream,"y-krok               %f\n",&tyadd2);
    fscanf(stream,"roll speed           %f\n",&roll_speed2);
    fscanf(stream,"podstava valca       %i\n",&podstava2);
    fscanf(stream,"vyska valca          %i\n",&vyska2);
    fscanf(stream,"Descent origin       (%f,%f,%f)\n",&o2.x,&o2.y,&o2.z);
    fscanf(stream,"Descent right        (%f,%f,%f)\n",&i2.x,&i2.y,&i2.z);
    fscanf(stream,"Descent up           (%f,%f,%f)\n",&j2.x,&j2.y,&j2.z);
    fscanf(stream,"Descent forward      (%f,%f,%f)\n",&k2.x,&k2.y,&k2.z);
    fclose(stream);

    camera1.SetAll(o1,i1,j1,k1);
    camera1.FarClipplane(vis);
    camera1.NearClipplane(minvis);
    camera1.HorizontalFOV(PI/3);
    camera1.VerticalFOV(PI/4);

    camera2.SetAll(o2,i2,j2,k2);
    camera2.FarClipplane(vis);
    camera2.NearClipplane(minvis);
    camera2.HorizontalFOV(0.63);
    camera2.VerticalFOV(0.47);

    texla=texture_library->GetOrCreate("NICETEXT.JPG");

    cout << "ok!"<<endl;
}

int  efekt_12::free()  //vrati 3 ako ok, 0 ak doslo k chybe
{
showlogo=false;
end=true;
return 3;
}

int  efekt_12::update()
{
id=Sync[GetSyncNum(float(counter)/float(refresh))].id;

rolovanie-=0.002;

tex_x_counter1+=txadd1;
tex_y_counter1+=tyadd1;
uhol_counter1+=roll_speed1;

tex_x_counter2+=txadd2;
tex_y_counter2+=tyadd2;
uhol_counter2+=roll_speed2;

if (counter/refresh>16.9) 
  {
  if (intens1>0.0) intens1-=0.02; else intens1=0.0;
  if (intens2>0.0) intens2-=0.02; else intens2=0.0;
  }
 else
  {
  if (intens1<1.0) intens1+=0.01; else intens1=1.0;
  if (intens2<1.0) intens2+=0.01; else intens2=1.0;
  }

return 1;
}

int efekt_12::go(double t)
{
if (t<ZACIATOK12) return 1;
if (end) return 3;
if (counter>=KONIEC12*refresh) return free();

if (!start) if (!init()) return 0;
int cur_frm=(int)(t*refresh);
if (cur_frm>KONIEC12*refresh) cur_frm=int(KONIEC12*refresh);
if (cur_frm>counter)
  while (counter<cur_frm)
    {
    counter++;
    if (counter<KONIEC12*refresh) update();
    }
if (counter>=KONIEC12*refresh) return free();

//tu sa kresli->
  
  glEnable(GL_TEXTURE_2D);
  glDisable(GL_LIGHTING);
  glDisable(GL_DEPTH_TEST);
  glDisable(GL_CULL_FACE);
  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE);
  glDisable(GL_FOG);

  texla->GL();
  glBlendFunc(GL_ONE,GL_ONE);

  camera1.SetAll(o1,i1,j1,k1);
  camera1.NRoll(rolovanie);
  camera1.GL();
  if (showlogo)
    {
    if (intens1||intens2) showlogo=false;
    }

   glBegin(GL_QUADS);
   if (intens1>0.0)
     for (int i=plochy1;i;i--)
       {
       float y=podstava1*cos(uhol_counter1+i*2*PI/plochy1);
       float z=podstava1*sin(uhol_counter1+i*2*PI/plochy1);

       glColor3f(0.0,0.0,0.0);
       glTexCoord2f( tex_x_counter1, tex_y_counter1);
       glVertex3f(-vyska2, y, z);

       glColor3f(intens1,intens1,intens1*0.3);
       glTexCoord2f( tex_x_counter1, tex_y_counter1+1.0);
       glVertex3f(-vyska2, 0, 0);

       glTexCoord2f( tex_x_counter1+1.0, tex_y_counter1+1.0);
       glVertex3f( vyska2, 0, 0);

       glColor3f(0.0,0.0,0.0);
       glTexCoord2f( tex_x_counter1+1.0, tex_y_counter1);
       glVertex3f( vyska2, y, z);
       }
   glEnd();


  glBlendFunc(GL_ZERO,GL_ONE_MINUS_SRC_COLOR);
   camera2.GL();
   glBegin(GL_QUADS);
   if (intens2>0.0)
     for (int i=plochy2;i;i--)
       {
       float y=podstava2*cos(uhol_counter2+i*2*PI/plochy2);
       float z=podstava2*sin(uhol_counter2+i*2*PI/plochy2);

       glColor3f(0.0,0.0,0.0);
       glTexCoord2f( tex_x_counter2, tex_y_counter2);
       glVertex3f(-vyska2, y, z);

       glColor3f(intens2,intens2,intens2);
       glTexCoord2f( tex_x_counter2, tex_y_counter2+1.0);
       glVertex3f(-vyska2, 0, 0);
                        
       glTexCoord2f( tex_x_counter2+1.0, tex_y_counter2+1.0);
       glVertex3f( vyska2, 0, 0);

       glColor3f(0.0,0.0,0.0);
       glTexCoord2f( tex_x_counter2+1.0, tex_y_counter2);
       glVertex3f( vyska2, y, z);
       }
   glEnd();

//<-
return 2;
}