#include <GL/glut.h>
#include <math.h>

const int update_ms = 30;	// Adjust for question 0

int window_width, window_height;

void drawPinwheel(float t);
void gammaCorrect(double gamma);

void display(){	
	// animation time counter
	static float t = 0;
	t += update_ms;

	typedef struct{
		float x, y, weight;
	}sample_t;

	// You should play with this array to set the sample
	// locations and weights for questions 3-9.

	// This is a single sample in the middle of the pixel.
	sample_t samples[] = {{0, 0, 1}};

	// This is a 2x2 regular grid pattern with equal sample weights.
	/*sample_t samples[] =
		{{-0.333, -0.333, 0.25},
		{+0.333, -0.333, 0.25},
		{-0.333, +0.333, 0.25},
		{+0.333, +0.333, 0.25}
	};*/

	// This is an 15x15 regular grid pattern with equal sample weights
	/*sample_t samples[225];
	for (int x = 0; x < 15; x++) {
		for (int y = 0; y < 15; y++) {
			samples[x*15+y].x = (x - 7)/15.0;
			samples[x*15+y].y = (y - 7)/15.0;
			samples[x*15+y].weight = 1/225.0;
		}
	}*/

	// clear the accumulation buffer
	glAccum(GL_LOAD, 0);

	// for each sample position
	int sample_count = sizeof(samples)/sizeof(sample_t);
	for (int i = 0; i < sample_count; i++) {
		glPushMatrix();

		// jitter the scene
		glTranslatef(-samples[i].x, -samples[i].y, 0);

		// draw the scene into the accumulation buffer
		glClear(GL_COLOR_BUFFER_BIT);
		drawPinwheel(t);
		glAccum(GL_ACCUM, samples[i].weight);

		glPopMatrix();
	}

	// now display the accumulation buffer
	glAccum(GL_RETURN, 1);
	
	// Uncomment for question 4 and for capturing screen shots.
	//gammaCorrect(1.0);
	
	glutSwapBuffers();
}

void gammaCorrect(double gamma){
	const int mapsize = 256;

	static double current_gamma = 0.0;	
	static float gammamap[mapsize];

	static int current_width = -1, current_height = -1;
	static float* pixels = 0;

	if (current_gamma != gamma) {
		current_gamma = gamma;
		
		for (int i=0; i < mapsize; ++i) {
			double fbval = (double)i / (double)(mapsize-1);
			double e = 1.0 / gamma;
			gammamap[i] = pow(fbval, e) / pow(1.0, e);
		}
	}

	if (current_width != window_width || current_height != window_height) {
		if (pixels != 0)
			delete[] pixels;
		pixels = new float[window_width*window_height*3];
		current_width = window_width;
		current_height = window_height;
	}
	
	glReadPixels(0, 0, current_width, current_height, GL_RGB, GL_FLOAT, pixels);

	for (int i = 0; i < current_width*current_height*3; i++) {
		pixels[i] = gammamap[(int)(pixels[i]*(mapsize-1))];
	}
	
	glDrawPixels(current_width, current_height, GL_RGB, GL_FLOAT, pixels);
}

void reshape(int w, int h){
	window_width = w;
	window_height = h;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	// set up a coordinate system with the origin in the middle,
	// where 1 unit is one pixel
	gluOrtho2D(-w/2, w/2, -h/2, h/2);
	glMatrixMode(GL_MODELVIEW);
}

void update(int){
	// call display every update_ms milliseconds
	glutPostRedisplay();
	glutTimerFunc(update_ms, update, 0);
}

int main(int arc, char **argv){
	glutInit(&arc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_ACCUM);
	glutInitWindowSize(500, 500);
	glutCreateWindow("CS248 Project 2");
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutTimerFunc(0, update, 0);
	glutMainLoop();
	return 0;
}


#define PI 3.14159265

// a slowly rotating pinwheel
void drawPinwheel(float t){
	const int arms = 32;	// Adjust the number of arms for question 1.

	glPushMatrix();
	glRotatef(t*0.005, 0, 0, 1);

	glBegin(GL_TRIANGLES);
	for(int i = 0; i < arms; i++) {
		glColor3f(i%2, 1-i%2, 0);
		glVertex2f(0, 0);
		glVertex2f(200*cos((i+0.0)/arms*2*PI), 200*sin((i+0.0)/arms*2*PI));
		glVertex2f(200*cos((i+0.5)/arms*2*PI), 200*sin((i+0.5)/arms*2*PI));
	}
	glEnd();

	glPopMatrix();
}

// add your own scenes here!

