#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
#include "Unit1.h"
//#include <gl/glut.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include <math.h>

#define PI 3.1415926535897
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
double priblizeni = 400;
double barva[9] = {0.5, 0, 0, 0,0.5,0,0,0,0.5};
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{
}


void analyza(int indix);
bool mouseclicked=false;
int X1,X2,Y1,Y2,absolutx=0,absoluty=0;
BOOL bSetupPixelFormat(HDC hDC)
{
    static PIXELFORMATDESCRIPTOR pfd = {
	sizeof(PIXELFORMATDESCRIPTOR),	// velikost pdf v  bajtech
	1,						// cislo verze
	PFD_DRAW_TO_WINDOW |	// vykreslovani do okna
	PFD_SUPPORT_OPENGL |	// přes OpenGL
    //PFD_DOUBLEBUFFER,		// double buffer
	PFD_TYPE_RGBA,			// barevný model
	24,						// 16-bit barevna hloubka , 8 bitu na alfa kanal
	0, 0, 0, 0, 0, 0,
	0,
	0,
	0,
	0, 0, 0, 0,
	32,
	0,
	0,
	PFD_MAIN_PLANE,
	0,
	0, 0, 0
    };
    int pixelformat;

    if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0)
    {
        MessageBox(NULL, "Špatně zvolený PixelFormat.", "Error", MB_OK);
        return FALSE;
    }

    if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
    {
        MessageBox(NULL, "Chybně nastavený PixelFormat", "Error", MB_OK);
        return FALSE;
    }

    return TRUE;
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button1Click(TObject *Sender)
{
Form2->Visible=0;
Form1->Visible=1;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Form2Close(TObject *Sender, TCloseAction &Action)
{
Form1->Visible=1;
}

//---------------------------------------------------------------------------
void TForm2::DrawScene(void)
{
    glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
    glLoadIdentity();

    glTranslatef(absolutx/5.0, absoluty/5.0, 0.0f);
  //  glMatrixMode(GL_PROJECTION);  
 //   glLoadIdentity();
 //   glRotatef(0.2, 0.0f, 1.0f, 0.0f);  // rotace podle osy y
  //  glRotatef(0.2, 1.0f, 0.0f, 0.0f);  // rotace podle osy x
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT );    //   | GL_DEPTH_BUFFER_BIT

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  //  glLoadIdentity();

    glTranslatef(0, 0.0, -priblizeni);
    glBegin(GL_QUADS);
    for(int i=0;i<199;i++)
    {
    for(int j=0;j<199;j++)
    {
    if(poledat[38][i][j]!=NULL)
    {
    if(Form2->CheckBox4->Checked){
    if(100*poledat[38][i][j]>Form2->ScrollBar1->Position) glColor3f(1,1,1); else glColor3f(0,0,0);
    } else
    glColor3f(poledat[38][i][j],poledat[38][i][j],poledat[38][i][j]);
    }
    else {glColor3f(0,0,0);}
    glVertex3i(i-100,j-100,0);

    glVertex3i(i+1-100,j-100,0);

    glVertex3i(i+1-100,j+1-100,0);
    glVertex3i(i-100,j+1-100,0);

    }}



  /*  glColor3f(barva[0],barva[1],barva[2]);
		glVertex3f( 0.0f, 100.0f, 0.0f);
    glColor3f(barva[3],barva[4],barva[5]);
		glVertex3f(-100.0f,-100.0f, 0.0f);
    glColor3f(barva[6],barva[7],barva[8]);
		glVertex3f( 100.0f,-100.0f, 0.0f);     */
    glEnd();

     if(Form2->CheckBox2->Checked)
     {
     glBegin(GL_LINE_STRIP);
     glColor3f(0.5,0,0);
     glVertex3i(-100,-100,0);
     glVertex3i(100,-100,0);
     glVertex3i(100,100,0);
     glVertex3i(-100,100,0);
     glVertex3i(-100,-100,0);
     glEnd();
     }
          if(Form2->CheckBox1->Checked)
     {
     glBegin(GL_LINE_STRIP);
     glColor3f(0.5,0.5,0);
     glVertex3i(-99,-100,0);
     glVertex3i(1,-100,0);
     glVertex3i(1,0,0);
     glVertex3i(-99,0,0);
     glVertex3i(-99,-100,0);
     glEnd();
     }
          if(Form2->CheckBox3->Checked)
     {
     glBegin(GL_LINE_STRIP);
     glColor3f(0,0.5,0.5);
     glVertex3i(-100,-100,0);
     glVertex3i(-50,-100,0);
     glVertex3i(-50,-50,0);
     glVertex3i(-100,-50,0);
     glVertex3i(-100,-100,0);
     glEnd();
     }

 //   glFinish();

    glFlush();
   // glutSwapBuffers();
}  

//---------------------------------------------------------------------------

void __fastcall TForm2::FormCreate(TObject *Sender)
{

 bSetupPixelFormat(Canvas->Handle);
 hRC = wglCreateContext(Canvas->Handle);
 if (hRC == NULL) {
    MessageBox(0,"Nepodařilo se vytvořit Rendering Context!","Form Create", MB_OK);
    return;
 }
 Form2->ScrollBar1->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormDestroy(TObject *Sender)
{
// zruš okno OpenGL
 wglDeleteContext(hRC);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormResize(TObject *Sender)
{
 GLdouble gldAspect;

 // změna velikosti okna
 wglDeleteContext(hRC);
 bSetupPixelFormat(Canvas->Handle);
 hRC = wglCreateContext(Canvas->Handle);

 wglMakeCurrent(Canvas->Handle, hRC);
 gldAspect=1.65;
// gldAspect = ClientWidth / ClientHeight;
// glutInitDisplayMode(GLUT_RGBA |GLUT_DOUBLE);

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(45.0,        //zorný uhel kamery
                gldAspect,   // pomer výšky a šířky projekčního plátna
                0.1f,         // nejmenší vzdálenost, od které je vidět objekt
                6000.0f);       // největší vzdálenost od které už objekt vidět není
 glViewport(0, 0, ClientWidth, ClientHeight);
 glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
 glLoadIdentity();
 DrawScene();
 wglMakeCurrent(NULL, NULL);
 Invalidate();

}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormPaint(TObject *Sender)
{

// Nastav vytvoreny RC jako aktualni
 wglMakeCurrent(Canvas->Handle, hRC);
 DrawScene();
 // Zrus nastaveni aktualniho RC
 wglMakeCurrent(NULL, NULL);
}
//---------------------------------------------------------------------------



void TForm2::normalize(int indix)
{
// vynulovani zobrazovane matice
for(int i=0;i<199;i++) for(int j=0;j<199;j++) poledat[38][i][j]=NULL;
// hledani nejmensiho prvku a nejvetsiho prvku
float maxval=-10,minval=10;
for(int pom1=0;pom1<32;pom1++) for(int i=0;i<199;i++) for(int j=0;j<199;j++)
if(poledat[pom1][i][j]!=NULL)
        {
        if(poledat[pom1][i][j]<minval&&poledat[pom1][i][j]!=0) minval=poledat[pom1][i][j];
        if(poledat[pom1][i][j]>maxval) maxval=poledat[pom1][i][j];
        }
float rozpeti=maxval-minval;

// provedeni normalizace
bool smer=false;


for(int i=0;i<199;i++)
{
for(int j=0;j<199;j++)
{
//if(poledat[indix][i][j]!=NULL)

poledat[38][i][j]=(poledat[indix][i][j]-minval)/rozpeti;
}}
analyza(indix);
}


void analyza(int indix)
{
//statisticke udaje
}




void __fastcall TForm2::Form2combochange(TObject *Sender)
{
normalize(Form2->ComboBox1->ItemIndex);
}
//---------------------------------------------------------------------------

void __fastcall TForm2::mysstlacena(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
if(Button==mbLeft)
{
mouseclicked=true;
X1=X;Y1=Y;X2=X;Y2=Y;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::mysuvolnena(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
if((Button==mbLeft)&&mouseclicked)
{
mouseclicked=false;
// akce  openGL
X2=X;Y2=Y;
    absolutx=absolutx+(X2-X1);
    absoluty=absoluty-(Y2-Y1);
wglMakeCurrent(Canvas->Handle, hRC);

DrawScene();
wglMakeCurrent(NULL, NULL);
}

}
//---------------------------------------------------------------------------

void __fastcall TForm2::mousemove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
if(mouseclicked)
{

}
}
//---------------------------------------------------------------------------

void __fastcall TForm2::weelup(TObject *Sender, TShiftState Shift,
      TPoint &MousePos, bool &Handled)
{
priblizeni-=10;
Form2->FormPaint(Sender);        
}
//---------------------------------------------------------------------------

void __fastcall TForm2::weeldown(TObject *Sender, TShiftState Shift,
      TPoint &MousePos, bool &Handled)
{
priblizeni+=10;
Form2->FormPaint(Sender);
}
//---------------------------------------------------------------------------



void __fastcall TForm2::CheckBox2Click(TObject *Sender)
{
Form2->FormPaint(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox1Click(TObject *Sender)
{
Form2->FormPaint(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox3Click(TObject *Sender)
{
Form2->FormPaint(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox4Click(TObject *Sender)
{
if(Form2->CheckBox4->Checked)Form2->ScrollBar1->Visible=true; else Form2->ScrollBar1->Visible=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm2::ScrollBar1Change(TObject *Sender)
{
Form2->FormPaint(Sender);
}
//---------------------------------------------------------------------------

