#include "testApp.h"
#include "stdio.h"

//--------------------------------------------------------------
void testApp::setup(){	 
	//we run at 60 fps!
	ofSetVerticalSync(true);
	
	//load in our shader from the data folder
	temp.loadShader((char *)ofToDataPath("warp").c_str());
	temp.setShaderActive(true);
	
	//set our four corner points in 0 - 1 ratio
	// order is TL TR BL BR
	temp.setUniformVariable("Q1x", 0.0);
	temp.setUniformVariable("Q1y", 0.0);
	
	temp.setUniformVariable("Q2x", 1.0);
	temp.setUniformVariable("Q2y", 0.0);
	
	temp.setUniformVariable("Q3x", 0.0);
	temp.setUniformVariable("Q3y", 1.0);
	
	temp.setUniformVariable("Q4x", 1.0);
	temp.setUniformVariable("Q4y", 1.0);

	//we need to scale down each vertex also to 0 - 1 
	//range for the calculation so we need to send our screen size
	temp.setUniformVariable("totalWidth", (float)ofGetWidth());
	temp.setUniformVariable("totalHeight", (float)ofGetHeight());
	
	//this is just for gui / mouse handles
	corners[0].x = 0.0;
	corners[0].y = 0.0;

	corners[1].x = 1.0;
	corners[1].y = 0.0;

	corners[2].x = 0.0;
	corners[2].y = 1.0;
	
	corners[3].x = 1.0;
	corners[3].y = 1.0;
	
	//shaders don't easily work with textures
	//so you won't see anything here
	img.loadImage("ofTheo.jpg");
	
	//lets setup some stupid particles
	for(int i = 0; i < 20; i++){
		balls[i].setup(ofRandom(100, ofGetWidth()-100), ofRandom(50, ofGetHeight()-50), ofRandom(5, 25));
		balls[i].vel.x = ofRandom(1.5, 2.8);
		balls[i].vel.y = ofRandom(1.5, 2.8);
	}
		
	
}

//--------------------------------------------------------------
void testApp::update(){	

	ofBackground(20, 20, 20);
	ofSetWindowShape(800, 600);	

	//we need to keep our shader up to date on our window size
	temp.setUniformVariable("totalWidth", (float)ofGetWidth());
	temp.setUniformVariable("totalHeight", (float)ofGetHeight());
	
	for(int i = 0; i < 20; i++){
		balls[i].update(ofGetWidth(), ofGetHeight());
	}
}

//--------------------------------------------------------------
void testApp::draw(){

	//textures don't work for now
	//ofSetColor(0xFFFFFF);
	//img.draw(100, 100);
	
	temp.setShaderActive(true);	
	//lets draw our bounding box
	ofNoFill();
	ofSetColor(0x00FFFF);
	ofRect(1, 1, ofGetWidth()-2, ofGetHeight()-2);
  
	//our particles
	ofFill();
	for(int i = 0; i < 20; i++)balls[i].draw();

	temp.setShaderActive(false);	
	ofSetColor(0xFFFFFF);
	ofDrawBitmapString("grab corners to warp openGL graphics", 10, 18);
	temp.setShaderActive(true);	

//  int my_sampler_uniform_location = glGetUniformLocationARB(temp.shader, "my_color_texture");
//  
//  glActiveTexture(GL_TEXTURE0);
//  glBindTexture(GL_TEXTURE_2D, img.tex.textureName[0]);
//
//  glUniform1iARB(my_sampler_uniform_location, 0);	
	
//		
//	GLfloat myMatrix[16];
//	
//	for(int i = 0; i < 16; i++){
//		if(i % 5 == 0) myMatrix[i] = 1.0;
//		else myMatrix[i] = 0.0;
//	}
//	
//	myMatrix[0]  = 1.8;
//	myMatrix[0]  = 0.8;
//	
//	glMultMatrixf(myMatrix);

	//glTranslatef(ofGetWidth()/2, ofGetHeight()/2, 0);
	//glRotatef(45, 0.5, 0.5, 0);
	//glutWireCube(50.0);
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){ 
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){ 
}


//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

		float scaleX = (float)x / ofGetWidth();
		float scaleY = (float)y / ofGetHeight();
		
		if(whichCorner >= 0){
			corners[whichCorner].x = scaleX;
			corners[whichCorner].y = scaleY;			
		}

		if(whichCorner == 0){
			temp.setUniformVariable("Q1x", scaleX);
			temp.setUniformVariable("Q1y", scaleY);
		}else
		if(whichCorner == 1){
			temp.setUniformVariable("Q2x", scaleX );
			temp.setUniformVariable("Q2y", scaleY );
		}else
		if(whichCorner == 2){
			temp.setUniformVariable("Q3x", scaleX );
			temp.setUniformVariable("Q3y", scaleY );
		}else
		if(whichCorner == 3){
			temp.setUniformVariable("Q4x", scaleX );
			temp.setUniformVariable("Q4y", scaleY );
		}		
	
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	
	float smallestDist = 1.0;
	whichCorner = -1;

	for(int i = 0; i < 4; i++){
		float distx = corners[i].x - (float)x/ofGetWidth();
		float disty = corners[i].y - (float)y/ofGetHeight();
		float dist  = sqrt( distx * distx + disty * disty);
		
		if(dist < smallestDist && dist < 0.1){
			whichCorner = i;
			smallestDist = dist;
		}
	}
}

//--------------------------------------------------------------
void testApp::mouseReleased(){
	whichCorner = -1;
}
