#include <iostream>
#include "SpanC.h"
#include "triangle.h"
#include "polygon.h"
using namespace std;


// f1:xの増加量  f2:yの増加量  i:.......    base:基本となる頂点のy座標
static float calc(float f1, float f2,int i, float base){
	float ans;
	ans = f1/f2*i + base;
	return ans;
}


static Vertex *vMid1(Vertex *vMid1,Vertex *vMin, Vertex *vMid, Vertex *vMax) {
	float d,d1;

	vMid1->y = vMid->y;
	//vMid1->tex_y = vMid->tex_y;
    d = (int)vMax->y-(int)vMin->y;
	d1 = (int)vMid->y - (int)vMin->y;
	/*

	if (-1.0<=vMax->y&&vMax->y<=1.0) {
		a = 0.5;
	} else {
		a = (vMax->x-vMin->x)/d;
	}
	*/
	//vMid1->tex_x  = vMin->tex_x + a * (vMax->tex_x-vMin->tex_x);
	vMid1->tex_x  = calc(vMax->tex_x - vMin->tex_x, d, (int)d1 , vMin->tex_x);
	vMid1->tex_y  = calc(vMax->tex_y - vMin->tex_y, d, (int)d1 , vMin->tex_y);
	//vMid1->x      = vMin->x     + a * (vMax->y     -vMin->y);
	vMid1->x      = calc(vMax->x - vMin->x, d, (int)d1 , vMin->x);
	//vMid1->z      = vMin->z     + a * (vMax->y     -vMin->y);
	vMid1->z      = calc(vMax->z - vMin->z, d, (int)d1 , vMin->z);
	//printf("x:%f y:%f z:%f tex_x:%f tex_y:%f\n",vMid1->x,vMid1->y,vMid1->z,vMid1->tex_x, vMid1->tex_y);
	return vMid1;
}



void
Span_c::half_triangle(Vertex *vMin, Vertex *vMid, Vertex *vMid1,
		    SDL_Surface *image)
{

	float tmp_z,tmp_tex1, tmp_tex2 ,tmp_tey1,tmp_tey2;
	//tmp_x,tmp_y,tmp_ypos ......unused
	float tex_x, tex_y,tex_z;
	float tmp_xpos,tmp_end,tmp_zpos;
	int tex_xpos,tex_ypos,tex_zpos;
	int i,j;
	float div_y;
	Uint32 rgb;
	float z,zpos;
	int x,y;
	int k =0;
	int l = 1;
	//float incli_x1, incli_x2;
	//float incli_z1, incli_z2;
	//float base_x1, base_x2  , base_z1, base_z2;
	//int base_y;

	/*
	incli_x1 = vMid1->x - vMin->x;
	incli_x2 = vMid->x  - vMin->x;
	incli_z1 = vMid1->z - vMin->z;
	incli_z2 = vMid->z  - vMin->z;
	base_x1 = vMin->x;
	base_x2 = vMin->x;
	base_z1 = vMin->z;
	base_z2 = vMin->z;
	*/
	//cout << vMin->x << vMin->y << vMin->z << endl;
	int start_y = (int)vMid->y;
	int end_y   = (int)vMin->y;
	
	if (start_y<end_y) { 
	    int i; i=end_y; end_y=start_y; start_y = i;   //yの入れ替え 
	    //Vertex *v; v = vMin; vMin = vMid; vMid = v;	
	    /*
	      incli_x1 = vMin->x - vMid->x; incli_x2 = vMin->x - vMid1->x;
	      incli_z1 = vMin->z - vMid->z; incli_z2 = vMin->z - vMid1->z;
	      base_z1 = vMid->z; base_z2 = vMid1->z;
	      base_y = (int)vMid->y;
	    */
	    k = 1;
	    l = -1;
	}



	div_y = start_y - end_y;  // > 0

	//if(div_y < 1 && div_y > -1) div_y = 1;
	for(i = k; i < div_y+1; i++) {
		//ここでspanの左端と右端のx,zを求めてる
		tmp_xpos = calc(vMid1->x - vMin->x ,div_y, i, vMin->x);
		tmp_end =  calc(vMid->x  - vMin->x ,div_y, i, vMin->x);	
		tmp_z =    calc(vMid1->z - vMin->z ,div_y, i, vMin->z);
	   	tmp_zpos = calc(vMid->z  - vMin->z ,div_y, i, vMin->z);
		//printf("x:%f end:%f z:%f zpos:%f\n",tmp_xpos,tmp_end,tmp_z,tmp_zpos);
		//ここからspanの左端と右端に対応するテクスチャを計算する
		tmp_tex1 =((i/(div_y)) * vMid1->tex_x) + \
			   ( ((div_y - i)/(div_y)) * vMin->tex_x); 
		tmp_tex2 =( (i/(div_y)) * vMid->tex_x) + \
			   ( ((div_y - i)/(div_y)) * vMin->tex_x); 

		tmp_tey1 =( (i/(div_y)) * vMid1->tex_y) + \
			   ( ((div_y - i)/(div_y)) * vMin->tex_y); 
		tmp_tey2 =( (i/(div_y)) * vMid->tex_y) + \
			   ( ((div_y - i)/(div_y)) * vMin->tex_y); 

		//ここで左端がxposかendを判断し、描画するspanを作る
		if(tmp_xpos > tmp_end) {
			x = (int)tmp_end;
			y = (int)vMin->y + i*l;
			end = (int)(tmp_xpos)-(int)(tmp_end)+1;
			z = tmp_zpos;
			zpos = tmp_z;
			tex1 = tmp_tex2;
			tex2 = tmp_tex1;
			tey1 = tmp_tey2;
			tey2 = tmp_tey1;
		} else {
			x = (int)tmp_xpos;
			y = (int)vMin->y + i*l;
			end = (int)(tmp_end)-(int)(tmp_xpos)+1;
			z = tmp_z;
			zpos = tmp_zpos;
			tex1 = tmp_tex1;
			tex2 = tmp_tex2;
			tey1 = tmp_tey1;
			tey2 = tmp_tey2;
		}
		//printf("%d:%f,%f  ",(int)vMin->y + i,xpos,end);
		//printf("%d:%d:%d:x:%d,end:%d\n",x, y , end,(int)(tmp_end),(int)(tmp_xpos));
		//printf("x:%d y:%d end:%d z:%f zpos:%f\n",x,y,end,z , zpos);
		//printf("tex1:%f tex2:%f tey1:%f tey2:%f\n",tex1,tex2,tey1,tey2); 
		if(end == 1) {
				//printf("tex_x:%f tex_y:%f\n",tex1,tex2);
				if(tex1 > 1) tex1 = 1;
				if(tey1 > 1) tey1 = 1;
				tex_xpos = (int)((image->h-1) * tex1); 
				tex_ypos = (int)((image->w-1) * tey1); 
				tex_zpos = (int)z;
				//printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);	
				//printf("image->h:%d tex_x:%f\n",image->h,tex1);
				rgb = p->get_rgb(tex_xpos,tex_ypos);	
				//viewer->write_pixel(x,y,zpos,rgb);
		}else {
			for(j = 0; j < end; j++) {
				tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
				tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
				tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
				if(tex_x > 1) tex_x = 1;
				if(tex_y > 1) tex_y = 1;
				tex_xpos = (int)((image->h-1) * tex_x); 
				tex_ypos = (int)((image->w-1) * tex_y); 
				//printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);	
				//printf("z:%f zpos:%f tex_z:%f\n",z,zpos,tex_z);
				//printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
				rgb = p->get_rgb(tex_xpos,tex_ypos);	
				//viewer->write_pixel(j+x,y,tex_z,rgb);
			}
		}
	}
}

void Span_c::create_span(Triangle *tri,SDL_Surface *image) {
	Vertex *vMin , *vMid, *vMax;
	/*
	float tmp_z,tmp_tex1, tmp_tex2 ,tmp_tey1,tmp_tey2;
	//tmp_x,tmp_y,tmp_ypos ......unused
	float tex_x, tex_y,tex_z;
	float tmp_xpos,tmp_end,tmp_zpos;
	int top_triangle;
	int tex_xpos,tex_ypos,tex_zpos;
	int i,j;
	float div_x,div_y;
	float z,zpos;
	Uint32 rgb;
	*/

	//cout << "x = " << tri->vertex1->x << endl;
	if(tri->vertex1->y <= tri->vertex2->y) {
		if(tri->vertex2->y <= tri->vertex3->y) {
			//printf("condition 1\n");
			vMin = tri->vertex1;
			vMid = tri->vertex2;
			vMax = tri->vertex3;
		} else if(tri->vertex3->y <= tri->vertex1->y) {
			//printf("condition 2\n");
			vMin = tri->vertex3;
			vMid = tri->vertex1;
			vMax = tri->vertex2;
		} else {
			//printf("condition 3\n");
			vMin = tri->vertex1;
			vMid = tri->vertex3;
			vMax = tri->vertex2;
		}
	} else {
		if(tri->vertex1->y <= tri->vertex3->y) {
			//printf("condition 4\n");
			vMin = tri->vertex2;
			vMid = tri->vertex1;
			vMax = tri->vertex3;
		} else if(tri->vertex3->y <= tri->vertex2->y) {
			//printf("condition 5\n");
			vMin = tri->vertex3;
			vMid = tri->vertex2;
			vMax = tri->vertex1;
		} else {
			//printf("condition 6\n");
			vMin = tri->vertex2;
			vMid = tri->vertex3;
			vMax = tri->vertex1;
		}
	}

	Vertex *vMid10 =  new Vertex(0,0,0,0,0);
	vMid1(vMid10,vMin,vMid,vMax);

	half_triangle(vMin,vMid,vMid10,image);
	half_triangle(vMax,vMid,vMid10,image);

	free(vMid10);
#if 0
	//cout << vMin->x << vMin->y << vMin->z << endl;
	div_x = (int)vMid->y - (int)vMin->y;
	if(div_x < 1 && div_x > -1) div_x = 1;
	div_y = (int)vMax->y - (int)vMin->y;
	if(div_y < 1 && div_y > -1) div_y = 1;

	for(i = 0; i < div_x+1; i++) {
		//ここでspanの左端と右端のx,zを求めてる
		tmp_xpos = calc(vMax->x - vMin->x,div_y, i, vMin->x);
		tmp_end =  calc(vMid->x - vMin->x,div_x, i, vMin->x);	
		tmp_z =    calc(vMax->z - vMin->z,div_y, i, vMin->z);
	   	tmp_zpos = calc(vMid->z - vMin->z,div_x, i, vMin->z);

		//ここからspanの左端と右端に対応するテクスチャを計算する
		tmp_tex1 =( (i/(div_y)) * vMax->tex_x) + \
			   ( ((div_y - i)/(div_y)) * vMin->tex_x); 
		tmp_tex2 =( (i/(div_x)) * vMid->tex_x) + \
			   ( ((div_x - i)/(div_x)) * vMin->tex_x); 

		tmp_tey1 =( (i/(div_y)) * vMax->tex_y) + \
			   ( ((div_y - i)/(div_y)) * vMin->tex_y); 
		tmp_tey2 =( (i/(div_x)) * vMid->tex_y) + \
			   ( ((div_x - i)/(div_x)) * vMin->tex_y); 

		//ここで左端がxposかendを判断し、描画するspanを作る
		if(tmp_xpos > tmp_end) {
			x = (int)tmp_end;
			y = (int)vMin->y + i;
			end = (int)(tmp_xpos)-(int)(tmp_end)+1;
			z = tmp_z;
			zpos = tmp_zpos;
			tex1 = tmp_tex2;
			tex2 = tmp_tex1;
			tey1 = tmp_tey2;
			tey2 = tmp_tey1;
		} else {
			x = (int)tmp_xpos;
			y = (int)vMin->y + i;
			end = (int)(tmp_end)-(int)(tmp_xpos)+1;
			z = tmp_zpos;
			zpos = tmp_z;
			tex1 = tmp_tex1;
			tex2 = tmp_tex2;
			tey1 = tmp_tey1;
			tey2 = tmp_tey2;
		}
		//printf("%d:%f,%f  ",(int)vMin->y + i,xpos,end);
		//printf("%d:%d:%d:x:%d,end:%d\n",x, y , end,(int)(tmp_end),(int)(tmp_xpos));
		//printf("x:%d y:%d end:%d z:%f zpos:%f\n",x,y,end,z , zpos);
		//printf("tex1:%f tex2:%f tey1:%f tey2:%f\n",tex1,tex2,tey1,tey2); 
		if(end == 1) {
				//printf("tex_x:%f tex_y:%f\n",tex1,tex2);
				if(tex1 > 1) tex1 = 1;
				if(tey1 > 1) tey1 = 1;
				tex_xpos = (int)(image->h-1) * tex1; 
				tex_ypos = (int)(image->w-1) * tey1; 
				tex_zpos = z;
				//printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);	
				//printf("image->h:%d tex_x:%f\n",image->h,tex1);
				rgb = p->get_rgb(tex_xpos,tex_ypos);	
				//viewer->write_pixel(x,y,zpos,rgb);
		}else {
			for(j = 0; j < end; j++) {
				tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
				tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
				tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
				if(tex_x > 1) tex_x = 1;
				if(tex_y > 1) tex_y = 1;
				tex_xpos = (int)(image->h-1) * tex_x; 
				tex_ypos = (int)(image->w-1) * tex_y; 
				//printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);	
				//printf("z:%f zpos:%f tex_z:%f\n",z,zpos,tex_z);
				//printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
				rgb = p->get_rgb(tex_xpos,tex_ypos);	
				//viewer->write_pixel(j+x,y,tex_z,rgb);
			}
		}
	}

	top_triangle = (int)vMid->y - (int)vMin->y;
	div_x = (int)vMax->y - (int)vMid->y;
	if(div_x < 1 && div_x > -1) div_x = 1;
	div_y = (int)vMax->y - (int)vMin->y;
	if(div_y < 1 && div_y > -1) div_y = 1;
	
	for(i = 0; i < div_x; i++) {
		//ここでspanの左端と右端のx,zを求めてる
		tmp_xpos = calc(vMax->x-vMin->x, div_y, (i+1+top_triangle), vMin->x);
		tmp_end =  calc(vMax->x-vMid->x, div_x, (i+1), vMid->x);	
		tmp_z =    calc(vMax->z-vMin->z, div_y, (i+1+top_triangle), vMin->z);
        tmp_zpos = calc(vMax->z-vMid->z, div_x, (i+1), vMid->z);

		tmp_tex1 =( ((i + 1 +top_triangle)/(div_y)) * vMax->tex_x) + \
			   ( ((div_y - i - 1 - top_triangle)/(div_y)) * vMin->tex_x); 
		tmp_tex2 =( ((i+1)/(div_x)) * vMax->tex_x) + \
			   ( ((div_x - i -1)/(div_x)) * vMid->tex_x); 

		tmp_tey1 =( ((i+1+top_triangle)/(div_y)) * vMax->tex_y) + \
			   ( ((div_y - i - 1 - top_triangle)/(div_y)) * vMin->tex_y); 
		tmp_tey2 =( ((i+1)/(div_x)) * vMax->tex_y) + \
			   ( ((div_x - i -1)/(div_x)) * vMid->tex_y); 
		if(tmp_xpos > tmp_end) {
			x = (int)tmp_end;
			y = (int)vMid->y+i+1;
			end = (int)(tmp_xpos)-(int)(tmp_end)+1;
			z = tmp_z;
			zpos = tmp_zpos;
			tex1 = tmp_tex2;
			tex2 = tmp_tex1;
			tey1 = tmp_tey2;
			tey2 = tmp_tey1;
		} else {
			x = (int)tmp_xpos;
			y = (int)vMid->y+i+1;
			end = (int)(tmp_end)-(int)(tmp_xpos)+1;
			z = tmp_zpos;
			zpos = tmp_z;
			tex1 = tmp_tex1;
			tex2 = tmp_tex2;
			tey1 = tmp_tey1;
			tey2 = tmp_tey2;
		}
		if(end == 0) end = 1;
		//printf("%d:%d:%d\n",x, y , end);
		//printf("x:%d y:%d end:%d z:%f zpos:%f\n",x,y,end,z,zpos);
		//printf("tex1:%f tex2:%f tey1:%f tey2:%f\n",tex1,tex2,tey1,tey2); 
		if(end == 1) {
				//printf("tex_x:%f tex_y:%f\n",tex1,tex2);
				if(tex1 > 1) tex1 = 1;
				if(tey1 > 1) tey1 = 1;
				tex_xpos = (int)(image->h-1) * tex1; 
				tex_ypos = (int)(image->w-1) * tey1; 
				tex_zpos = z;
				//printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
				//printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);	
				//printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
				rgb = p->get_rgb(tex_xpos,tex_ypos);	
				//viewer->write_pixel(x,y,tex_zpos,rgb);
		}else {
			for(j = 0; j < end; j++) {
				tex_x = tex1*(end-1-j)/(end-1) + tex2*j/(end-1);
				tex_y = tey1*(end-1-j)/(end-1) + tey2*j/(end-1);
				tex_z = z*(end-1-j)/(end-1) + zpos*j/(end-1);
				//printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
				if(tex_x > 1) tex_x = 1;
				if(tex_y > 1) tex_y = 1;
				tex_xpos = (int)(image->h-2) * tex_x; 
				tex_ypos = (int)(image->w-1) * tex_y; 
				//printf("z:%f zpos:%f tex_z:%f\n",z,zpos,tex_z);
				//printf("tex_xpos:%d tex_ypos:%d\n",tex_xpos,tex_ypos);	
				//printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
				//printf("tex_x:%f tex_y:%f\n",tex_x,tex_y);
				rgb = p->get_rgb(tex_xpos,tex_ypos);	
				//viewer->write_pixel(j+x,y,tex_z,rgb);
			}
		}
	}
#endif
}

// i:分子、f1:長さ   f2:対応する頂点1    f3:対応する頂点2
/*
float Span::calc2(int i, float f1, float f2, float f3) {
	float ans;
	ans = i/f1 * f2 + (f1-i)/f1  * f3;

	*/
