#include <CtrlLib/CtrlLib.h>
#include <GLCtrl/GLCtrl.h>
#include "cube.h"
#include "box_calc.h"

using namespace Upp;




class MyGL : public GLCtrl {
	public:
		MyGL(){
			graph_array=new GLfloat[40];
		};
		~MyGL(){
			delete[] graph_array;	
		}
		virtual void GLInit();
		virtual void GLPaint();
		virtual void MouseMove(Point p, dword);
		void DrawCube(float x1, float y1, float z1, float x2, float y2, float z2);
		void working();
	private:	
		// all these are initiazie in GLInt() and used in GLPaint()
		float x1,y1,z1, x2,y2,z2, rotqube; 
		cube myCube;
		GLfloat *graph_array;
};

void MyGL::working(){
    float x_max=0.0f, x_min=0.0f;
    float v_max=0.0f, v_min=0.0f;
    float x_max1=0.0f, x_min1=0.0f, v_max1=0.0f, v_min1=0.0f;
    float length=60.0f, width=60.0f, tmp=0.0f, temp_x=0.0f;;
    int j=0;
    coordinate_translator ctx, ctv;
    
	Calculate_Maxima_Minima(length, width, &x_max, &x_min);

 
    v_max=Calculate_Volume(x_min, length, width);
	v_min=Calculate_Volume(x_max, length, width);
	
	x_min1=0.0f;
	x_max1=x_max;
	
	v_min1=Calculate_Volume(x_max1, length, width);
	v_max1=Calculate_Volume(x_min1, length, width);;
	
 	ctx.recalculate(x_max1, x_min1, 1.0f, 0.0f);
 	ctv.recalculate(v_max, v_min, 1.0f, 0.0f);

	graph_array[0]=ctx.translate(x_min1);
	graph_array[1]=ctv.translate(v_max1);
	graph_array[38]=ctx.translate(x_max1);
	graph_array[39]=ctv.translate(v_min1);

 	tmp=(x_max1-x_min1)/20.0f;
 	temp_x=x_min1;
 	
    for(int i=2; i<38; i+=2){
        temp_x=temp_x+tmp; 
		graph_array[i]=ctx.translate(temp_x);
		graph_array[i+1]=ctv.translate(Calculate_Volume(temp_x,length,width));
    }
}

void MyGL::DrawCube(float x1, float y1, float z1, float x2, float y2, float z2){
/*
	float vc[3][8]={
		{x1,y1,z1},{x2,y1,z1},{x2,y2,z1},{x1,y2,z1},
		{x1,y2,z2},{x2,y2,z2},{x2,y2,z1},{x1,y2,z1},
		{x1,y1,z2},{x1,y1,z1},{x1,y2,z1},{x1,y2,z2},
		{x1,y1,z2},{x2,y1,z2},{x2,y2,z2},{x1,y2,z2},
		{x1,y1,z2},{x2,y1,z2},{x2,y1,z1},{x1,y1,z1},
		{x2,y1,z2},{x2,y1,z1},{x2,y2,z1},{x2,y2,z2}
	};
*/	
}

void MyGL::GLInit(){

	float w=700.0f, h=700.0f;

	
	glClearColor(0.7f, 0.7f, 0.7f, 0.0f);
		
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f, 700.0f / 700.0f, 1.0f, 500.0f);
	 
	//glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
//		glEnable(GL_DEPTH);	
//  	
  	glEnable(GL_DEPTH_TEST);
  	glDepthMask(GL_TRUE);
  	//glEnable(GL_CULL_FACE);
  	//glCullFace(GL_BACK);
  	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
	
	myCube.set_coordinate(0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f);
	
/*	
	x1=0.2f;
	y1=0.2f;
	z1=0.2f;
	
	x2=-0.2f;
	y2=-0.2f;
	z2=-0.2f;
*/	
	rotqube=0;

	working();
}

void MyGL::GLPaint(){
	glClear (GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
  	glLoadIdentity();
  	glTranslatef(0.0f, 1.0f, -5.00f);
	//glScalef(1.2f, 1.2f, 1.2f);  	
	glPushMatrix();
	
	glTranslatef(-1.0f, 0.0f, -1.50f);
	
	GLfloat colors[] = {1,1,1,  1,1,0,  1,0,0,  1,0,1,              // v0-v1-v2-v3
                    1,1,1,  1,0,1,  0,0,1,  0,1,1,              // v0-v3-v4-v5
                    1,1,1,  0,1,1,  0,1,0,  1,1,0,              // v0-v5-v6-v1
                    1,1,0,  0,1,0,  0,0,0,  1,0,0,              // v1-v6-v7-v2
                    0,0,0,  0,0,1,  1,0,1,  1,0,0,              // v7-v4-v3-v2
                    0,0,1,  0,0,0,  0,1,0,  0,1,1};             // v4-v7-v6-v5
                
	
	
	//glRotatef(rotqube, 1.0f, 1.0f, 1.0f);
	//glRotatef(rotqube, 1.0f, 0.0f, 0.0f);
	//glRotatef(rotqube, 0.0f, 1.0f, 0.0f);
	glRotatef(rotqube, 1.0f, 1.0f, 1.0f);
	
	
	 
	 
		// activate and specify pointer to vertex array
		glEnableClientState(GL_COLOR_ARRAY);
		glColorPointer(3, GL_FLOAT, 0, colors);
		
		glEnableClientState(GL_VERTEX_ARRAY);
		glVertexPointer(3, GL_FLOAT, 0, myCube.get_vertices_array());
		
		// draw a cube
		glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, myCube.get_indices_array());
		
		// deactivate vertex arrays after drawing
		glDisableClientState(GL_COLOR_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);	 
		
/*	
	 glBegin(GL_QUADS);		// Draw The Cube Using quads	  
		
		glColor3f(1.0f,0.0f,0.0f);	// Color Red 
	    glVertex3f(x1,y1,z1);	
	    glColor3f(1.0f,1.0f,1.0f);	// Color Green
	    glVertex3f(x2,y1,z1);
	    glColor3f(0.0f,1.0f,0.0f);	// Color Blue
	    glVertex3f(x2,y2,z1);
	    glColor3f(1.0f,1.0f,1.0f);	// Color Yellow	 
	    glVertex3f(x1,y2,z1);


	    glColor3f(0.0f,0.5f,0.0f);	// Color Green	
	    glVertex3f(x1,y2,z2);
		//glColor3f(0.0f,0.5f,0.5f);	// Color Brown
	    glVertex3f(x2,y2,z2 );	    	
	   // glColor3f(0.0f,0.0f,0.5f);	// Color Blue
	    glVertex3f(x2,y2,z1);	
	   // glColor3f(0.5f,0.5f,0.0f);	// Color Yellow	
	    glVertex3f(x1,y2,z1);
	        	

	    glColor3f(0.0f,0.0f,0.5f);	// Color Blue
	    glVertex3f(x1,y1,z2);
	    //glColor3f(0.5f,0.0f,0.0f);	// Color Red 	
	    glVertex3f(x1,y1,z1 );	
		//glColor3f(0.5f,0.5f,0.0f);	// Color Yellow	    
	    glVertex3f(x1,y2,z1);	
	   // glColor3f(0.5f,0.0f,0.5f);	// Color Purple
	    glVertex3f(x1,y2,z2);


	    glColor3f(0.5f,0.0f,0.5f);	// Color Purple
	    glVertex3f(x1,y1,z2);
	    //glColor3f(1.0f,1.5f,1.0f);	// Color White
	    glVertex3f(x2,y1,z2 );	
	    //glColor3f(0.0f,0.5f,0.5f);	// Color Brown
	    glVertex3f(x2,y2,z2);	
	   // glColor3f(0.5f,0.0f,0.5f);	// Color Purple	
	    glVertex3f(x1,y2,z2);	    
	    
	   
	    glColor3f(0.5f,0.5f,0.0f);	// Color Yellow	 
	    glVertex3f(x1,y1,z2);
	    //glColor3f(1.0f,1.5f,1.0f);	// Color White
	    glVertex3f(x2,y1,z2 );	
	    //glColor3f(0.0f,0.5f,0.0f);	// Color Green
	    glVertex3f(x2,y1,z1);
	    //glColor3f(0.5f,0.0f,0.0f);	// Color Red 	
	    glVertex3f(x1,y1,z1); 

	    
 		glColor3f(0.0f,0.5f,0.5f);	// Color Brown
	    glVertex3f(x2,y1,z2);
	    //glColor3f(0.0f,0.5f,0.0f);	// Color Green
	    glVertex3f(x2,y1,z1 );	
	    //glColor3f(0.0f,0.0f,0.5f);	// Color Blue
	    glVertex3f(x2,y2,z1);	
		//glColor3f(0.0f,0.5f,0.5f);	// Color Brown	    
	    glVertex3f(x2,y2,z2); 
	    
	glEnd();			// End Drawing The Cube
*/	
	glPopMatrix();
  	//glLoadIdentity();
  	glTranslatef(1.0f, 0.0f, -1.0f);
	//glRotatef(90.0f, 0.0f, 1.0f, 0.0f);  		
	
	
	glBegin(GL_LINES); //Drawing The Axis
	  		glColor3f(1.0f,0.3f,0.3f);
	  		glVertex3f( 1.0f, -0.005f, -0.01f);
	  		glVertex3f( -1.0f, -0.005f, -0.01f);
	  	
			glColor3f(0.3f,1.0f,0.3f);
	  		glVertex3f( -0.005f, 1.0f, -0.01f);
	  		glVertex3f( -0.005f, -1.0f, -0.01f);
			
			glColor3f(0.3f,0.3f,1.0f);
	  		glVertex3f(-0.005f, -0.005f, 2.0f);
	  		glVertex3f(-0.005f, -0.005f, -1.0f);	  
	glEnd();


	// activate and specify pointer to vertex array
	//glEnableClientState(GL_COLOR_ARRAY);
	//glColorPointer(3, GL_FLOAT, 0, colors);
	

	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(2, GL_FLOAT, 0, graph_array);
	glColor3f(1.0f,0.0f,0.0f);
	// draw a cube
	glDrawArrays(GL_LINES, 0, 20);
	
	// deactivate vertex arrays after drawing
	//glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);	
	glPopMatrix();


	if(rotqube>361){rotqube=0;}else{rotqube+=1.0f;}


	glFlush();
	        
}

void MyGL::MouseMove(Point p, dword) {
        Refresh();
}


GUI_APP_MAIN{
    TopWindow win;
    MyGL gl;
    win.Add(gl.LeftPos(10, 700).TopPos(10, 700));
    win.Sizeable().Zoomable();
    win.Open();
    win.Run();
}

