#include "testApp.h"
#include "textfile.h"

GLint loc;
GLhandleARB v,f,f2,p;



void setShaders() {

	char *vs = NULL,*fs = NULL;

	v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
	f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
	f2 = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);


	vs = textFileRead("textureSimple.vert");
	fs = textFileRead("textureSimple.frag");

	const char * vv = vs;
	const char * ff = fs;

	glShaderSourceARB(v, 1, &vv,NULL);
	glShaderSourceARB(f, 1, &ff,NULL);

	free(vs);free(fs);

	glCompileShaderARB(v);
	glCompileShaderARB(f);

	
	p = glCreateProgramObjectARB();
	glAttachObjectARB(p,v);
	glAttachObjectARB(p,f);

	glLinkProgramARB(p);
	
	glUseProgramObjectARB(p);

	//loc = glGetUniformLocationARB(p,"time");


}




void drawCube() {

	float hd = 100.0;

	glColor3f(1,0,0);
	glBegin(GL_QUADS);
		glVertex3f(-hd,-hd,-hd);
		glVertex3f(-hd,hd,-hd);
		glVertex3f(hd,hd,-hd);
		glVertex3f(hd,-hd,-hd);
	glEnd();

	glColor3f(1,1,0);
	glBegin(GL_QUADS);
		glVertex3f(-hd,-hd,-hd);
		glVertex3f(hd,-hd,-hd);
		glVertex3f(hd,-hd,hd);
		glVertex3f(-hd,-hd,hd);
	glEnd();

	glColor3f(1,0,1);
	glBegin(GL_QUADS);
		glVertex3f(-hd,-hd,-hd);
		glVertex3f(-hd,-hd,hd);
		glVertex3f(-hd,hd,hd);
		glVertex3f(-hd,hd,-hd);
	glEnd();

	glColor3f(0,1,0);
	glBegin(GL_QUADS);
		glVertex3f(-hd,-hd,hd);
		glVertex3f(hd,-hd,hd);
		glVertex3f(hd,hd,hd);
		glVertex3f(-hd,hd,hd);
	glEnd();

	glColor3f(0,0,1);
	glBegin(GL_QUADS);
		glVertex3f(-hd,hd,-hd);
		glVertex3f(-hd,hd,hd);
		glVertex3f(hd,hd,hd);
		glVertex3f(hd,hd,-hd);
	glEnd();

	glColor3f(0,1,1);
	glBegin(GL_QUADS);
		glVertex3f(hd,-hd,-hd);
		glVertex3f(hd,hd,-hd);
		glVertex3f(hd,hd,hd);
		glVertex3f(hd,-hd,hd);
	glEnd();


}




//--------------------------------------------------------------
void testApp::setup(){	 
	bikers.loadImage("images/bikers.jpg");
	gears.loadImage("images/gears.gif");
	tdf.loadImage("images/tdf_1972_poster.jpg");
	tdfSmall.loadImage("images/tdf_1972_poster.jpg");
	tdfSmall.resize(tdf.width / 4, tdf.height / 4);
	tdfSmall.setImageType(OF_IMAGE_GRAYSCALE);
	transparency.loadImage("images/transparency.png");
	bikeIcon.loadImage("images/bike_icon.png");
	bikeIcon.setImageType(OF_IMAGE_GRAYSCALE);
	
	
	setShaders();
	ofSetVerticalSync(0);
}


//--------------------------------------------------------------
void testApp::update(){
	ofBackground(255,255,255);	
	printf("%f \n", ofGetFrameRate());
}

float a = 0;
//--------------------------------------------------------------
void testApp::draw(){


	//glUseProgramObjectARB(0);
	

	ofSetupScreen();
	
	ofSetColor(0xFFFFFF);

	bikers.draw(0,0);
	gears.draw(600,0);
	tdf.draw(600,300);
	
	ofSetColor(0xDD3333);
	tdfSmall.draw(200,300);
	
	ofSetColor(0xFFFFFF);
	ofEnableAlphaBlending();
	transparency.draw(sin(ofGetElapsedTimeMillis()/1000.0f) * 100 + 500,20);
	ofDisableAlphaBlending();
	 
	
	ofSetColor(0x000000);
	
	// getting the pixels out of an image, 
	// and then use the values to draw circles
	unsigned char * pixels = bikeIcon.getPixels();
	int w = bikeIcon.width;
	int h = bikeIcon.height;
	for (int i = 0; i < w; i++){
		for (int j = 0; j < h; j++){
			int value = pixels[j * w + i];
			float pct = 1 - (value / 255.0f);
			ofCircle(i*10,500 + j*10,1 + 5*pct);		
		}
	}
	
	ofSetColor(0xFFFFFF);
	bikeIcon.draw(300,500, 200,200);
	
	//glUseProgramObjectARB(p);

	//glUniform1f(loc, ofRandomuf()*1000.0f);
	//loc += 1;
	glTranslatef(300,300,0);
	glRotatef(a,1,1,0);
	drawCube();;
	a+=0.1;
	
	
}


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

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

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

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
	glutEnterGameMode();
}

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

}
