//#include "Png_Funcs.h"
#include "OpenGL.h"

struct image_attr *im_p;
GLuint Image_Texture;
double Eye_X = 0;
double Eye_Y = 0;
double Eye_Z = -4;


static void special (int key, int x, int y){
	switch(key){
		case GLUT_KEY_UP:
			Eye_Z++;
			break;
		case GLUT_KEY_DOWN:
			Eye_Z--;
			break;
		case GLUT_KEY_RIGHT:
			Eye_X++;
			//printf("increment num1: %lf\n", num1);
			break;
		case GLUT_KEY_LEFT: 
			Eye_X--;
			//printf: %lf\n", num1);
			break;
	}
}


GLuint LoadTexture( struct image_attr *im ){
	//GLuint texture;
	glGenTextures( 1, &Image_Texture );
	glBindTexture( GL_TEXTURE_2D, Image_Texture );
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
//	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
//	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
//	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, im->width, im->height, 0, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)im->image);
	return 1;
}

void FreeTexture( GLuint texture ){
	glDeleteTextures( 1, &texture );
}

void BindTexture(GLuint texture){
	glBindTexture( GL_TEXTURE_2D, texture );
	//glRotatef( 180, 0.0f, 0.0f, 1.0f );
	//float aspect = im_p->width / im_p->height;
	//float aspect2 = im_p->height / im_p->width;
//	float y = 1;
	//float y = (aspect>1)? x/aspect: x*aspect;
//	float x = y*aspect;
	float x = 1;
	float y = 1;
	//glScalef(aspect1, aspect2, 1);
/*
    glBegin (GL_QUADS);
    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0); //with 
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0); //so that
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();
	

*/
    glBegin (GL_QUADS);
    glTexCoord2d(0.0,y); glVertex2d(x,-1.0*y);
    glTexCoord2d(0.0,0.0); glVertex2d(x,y);
    glTexCoord2d(x,0.0); glVertex2d(-1.0*x,y);
    glTexCoord2d(x,y); glVertex2d(-1.0*x,-1.0*y);
    glEnd();


	//This is how texture coordinates are arranged
	//
	//  0,1   ---   1,1
	//       |     |
	//       |     |
	//       |     |
	//  0,0   ---   1,0


}

void display(void){
    glClearColor (0.0,0.0,0.0,1.0);
    //glClear (GL_COLOR_BUFFER_BIT);
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glEnable( GL_TEXTURE_2D );
	//GLuint texture;
	//texture = LoadTexture( im_p );
	gluLookAt ( Eye_X, Eye_Y, Eye_Z,  Eye_X, Eye_Y, 0.0, 0.0, 0.1, 0.0);
	//gluLookAt( , Eye_Y, Eye_Z, Look_X, Look_Y, 20, 0, .1, 0);
	(void)LoadTexture( im_p );
	BindTexture(Image_Texture);
	
	//glutSwapBuffers();
	glFlush();
    glutSwapBuffers();
	sleep(1);
//glutPostRedisplay();
	FreeTexture( Image_Texture );

	//sleep(1);
	//FreeTexture(texture);

}


void reshape(int w, int h){
	GLfloat aspect = (GLfloat)im_p->width / (GLfloat)im_p->height;
	//GLsizei width;
	//GLzisei height; 
	

    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
	gluPerspective (60, (GLfloat)w / (GLfloat)h/aspect, 0.0 , 100.0);
    //gluPerspective (60, (GLfloat)im_p->width / (GLfloat)im_p->height, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);

}



int Run_GL(struct image_attr *im){
	im_p = im;

	//int bla1;
	//char **bla2 = NULL;
    //glutInit (&bla1, bla2);

    //query_ext(); 
    glutInitDisplayMode (GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);
//	glutInitDisplayMode (GLUT_DOUBLE);

printf("w: %zu h: %zu\n", im->width, im->height);
    //glutInitWindowSize((int)im->width/4, (int)im->height/4);
	glutInitWindowSize(1200, 1200); 

   glutInitWindowPosition (0, 0);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);
    glutReshapeFunc (reshape);
    //glutMotionFunc(mouse_active);
    //glutPassiveMotionFunc(mouse_passive); 
    //query_ext();
    glutSpecialFunc(special);
	//(void)LoadTexture( im_p );
    glutMainLoop ();
	//FreeTexture( Image_Texture ); 
   return 0;
}