#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <sys/time.h>
#include <math.h>

struct screen {

        unsigned char red;
        unsigned char green;
        unsigned char blue;
        unsigned char alpha;
};

struct screen ***world;


void OpenGLInit(void);
static void Animate(void );
static void ResizeWindow(int w, int h);
struct timespec ts;
struct timeval starttime,endtime;

double fps = 0;
double time1 = 0;
double time2 = 0;

int K, J;
int X;
int Y;  
int RES;
int DEPTH = 3;
//float rgbcalcs[8]= { 0 } ;
//float rgbcalcs[8];
//#include "alpha.h"
//#include "ls_shapes.h"



static void Animate(void)
{

	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();

	long unsigned int i;
	long unsigned int counter1, counter2, counter3;


	
	glBegin(GL_POINTS);
        for(counter1=1; counter1 > 0; counter1--) {
                for(counter2=0; counter2 < X; counter2++) {
                        for(counter3=0; counter3 < Y; counter3++) {
                                alpha( counter1, counter2, counter3 );
				//glLoadIdentity();
                                glColor3f( rgbcalcs[0], rgbcalcs[1], rgbcalcs[2]);
                                glVertex3f(counter2, counter3, counter1);
			}
		}
	}
	glEnd();
	

	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();

	time2++;

	gettimeofday(&endtime, NULL);
	time1=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;

        if (time2 == 5)
        {
                fps = time2 / time1;
                printf("fps: %lf - %lf frames in %lf seconds\n", fps, time2, time1);
                gettimeofday(&starttime, NULL);
                time2 = 0;

	}





}


void OpenGLInit(void)           
{       
        glShadeModel( GL_FLAT );
        glClearColor( 0.0, 0.0, 0.0, 0.0 );
        glClear(GL_COLOR_BUFFER_BIT);
        glDisable( GL_DEPTH_TEST );
	//glClearDepth(100.0f);							// Depth Buffer Setup
	//glEnable(GL_DEPTH_BUFFER_BIT);
	//glEnable(GL_DEPTH_TEST);						// Enables Depth Testing
	//glEnable(GL_DEPTH_TEST);
	//glDepthMask(GL_TRUE);
	//glEnable(GL_TEXTURE_3D);

	//glDepthFunc(GL_LEQUAL);							// The Type Of Depth Test To Do

}
        
        
static void ResizeWindow(int w, int h)  
{
        float aspectRatio;
        h = (h == 0) ? 1 : h;
        w = (w == 0) ? 1 : w;
        glViewport( 0, 0, w, h );       // View port uses whole window
        aspectRatio = (float)w/(float)h;

        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
	//glOrtho(0,w,0,h,-100,100);
        glOrtho (0, w, h, 0, -1, 100);
        glMatrixMode( GL_MODELVIEW );
	
}       




int main(int argc, char** argv) {

        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
        glutInitWindowPosition( 0, 0 );

	Y = glutGet(GLUT_SCREEN_HEIGHT); 
	X = glutGet(GLUT_SCREEN_WIDTH);
	RES = X*Y;
	register long unsigned int i, counter1, counter2 ,counter3;

	printf("res: %d x: %d y: %d\n", RES, X, Y);


	int j;


	world= (struct screen ***)malloc(1*sizeof(struct screen *) );
	for( i=0; i<1 ; i++ )
	{
		world[ i ] = ( struct screen **)malloc(X* sizeof(struct screen **) );
		for( j=0; j<X; j++)
		{
            		world[ i ][ j ] = ( struct screen *)malloc(Y* sizeof(struct screen ***) );
		}
	}



	printf("world initialized! size: %lu \n", sizeof(world));


        for(counter1=0; counter1 < 1; counter1++) {
                for(counter2=0; counter2 < X; counter2++) {
                        for(counter3=0; counter3 < Y; counter3++) {
				//printf("z: %lu x: %lu y %lu\n", counter1, counter2, counter3);
                                world[counter1][counter2][counter3].red = 0;
                                world[counter1][counter2][counter3].green = 0;
                                world[counter1][counter2][counter3].blue = 0;
                                world[counter1][counter2][counter3].alpha = 0;
                        }
                }
        }



	//printf("world populated!\n");
	printf("res*depth %d\n", RES*DEPTH);


	glutInitWindowSize( X, Y );
	glutCreateWindow( "lsrender test" );


	OpenGLInit();
	glutReshapeFunc( ResizeWindow );
	glutDisplayFunc( Animate );
	glutMainLoop(  );



	return 0;
}
