#include "eixos.inc"
#include "retalho-simples.inc"

// CORES E TEXTURAS
#declare txg = 
  texture{
    pigment{ color rgb < 1, 1, 1 > }
    finish{ diffuse 0.9 ambient 0.1 }
 	}

#declare txr = 
  texture{
    pigment{ color rgb < 1, 0, 1 > }
    finish{ diffuse 0.9 ambient 0.1 }
 	}

#declare tx_fosca3 = 
  texture{
    pigment{ color rgb < 0.3, 0.6, 0.154 > }
    finish{ diffuse 0.9 ambient 0.1 }
 	}

#declare tx_fosca4 = 
  texture{
    pigment{ color rgb < 1, 0, 0 > }
    finish{ diffuse 0.9 ambient 0.1 }
 	}
// ======================================================================
// DESCRIÇÃO DA CENA 

#macro emenda_retalho(PP, r0, s0, r1, s1)
	//horizontal
	#if(r0 = r1 & s0 + 1 = s1)
		emenda_horizontal(PP, r0, s0, r1, s1)
	#end

	//vertical
	#if(r0+1 = r1 & s0 = s1)
		emenda_vertical(PP, r0, s0, r1, s1)
	#end
#end

#macro emenda_vertical(PP, r0, s0, r1, s1)
	#for(j, 0, 3, 1)
		#local ponto_medio = (PP[r0][s0][2][j] + PP[r1][s1][1][j]) /2;
		#declare PP[r0][s0][3][j] =	ponto_medio;
		#declare PP[r1][s1][0][j] =	ponto_medio;
	#end
#end

#macro emenda_horizontal(PP, r0, s0, r1, s1)
	#for(i, 0, 3, 1)
		#local ponto_medio = (PP[r0][s0][i][2] + PP[r1][s1][i][1]) /2;
		#declare PP[r0][s0][i][3] =	ponto_medio;
		#declare PP[r1][s1][i][0] =	ponto_medio;
	#end
#end

#macro carroceria(PP, NH, NV)
	union{
		#for(i, 0, NV - 1, 1)
			#for(j, 0, NH - 1, 1)
				#local P = array[4][4];
				
				#for(k, 0, 3, 1)
					#for(l, 0, 3, 1)
						#local P[k][l] = PP[i][j][k][l];
					#end
				#end

				object { retalho(P, 0.04, txg, txr) }
			#end
		#end
	}
#end

#macro frenteCarro(PP, NH, NV)
	
	#local p = PP[1][0][3][0];
	#local PP[1][0][3][0] = <p.x, p.y, p.z - 3>;

	#local p = PP[0][0][0][0];
	#local PP[0][0][0][0] = <p.x, p.y, p.z - 3>;

	#local p = PP[0][0][0][3];
	#local PP[0][0][0][3] = <p.x, p.y, p.z + 3>;

	#local p = PP[0][0][1][3];
	#local PP[0][0][1][3] = <p.x, p.y, p.z + 3>;

	#local p = PP[0][0][2][3];
	#local PP[0][0][2][3] = <p.x, p.y, p.z + 3>;

	#local p = PP[0][0][3][3];
	#local PP[0][0][3][3] = <p.x, p.y, p.z + 3>;
	
	#local p = PP[1][0][0][3];
	#local PP[1][0][0][3] = <p.x, p.y, p.z + 3>;

	#local p = PP[1][0][1][3];
	#local PP[1][0][1][3] = <p.x, p.y, p.z + 3>;

	#local p = PP[1][0][2][3];
	#local PP[1][0][2][3] = <p.x, p.y, p.z + 3>;

	#local p = PP[1][0][3][3];
	#local PP[1][0][3][3] = <p.x, p.y, p.z + 3>;

	PP
#end

#macro matrizPrincipal(NH, NV)

	#local PP = array[NV][NH][4][4];
	
	#for(i, 0, NV - 1, 1)
		#for(j, 0, NH - 1, 1)
			#for(k, 0, 3, 1)
				#for(l, 0, 3, 1)
					#local PP[i][j][k][l] = <i * 3 + k, j * 3 + l, 0>;
				#end
			#end
		#end
	#end
	PP
#end

#macro tetoCarro(PP, NH, NV)
	#for(teto_i, 0, 1, 1)
		#for(i, 0, 2, 1)
			#for(j, 0, 2, 1)
				#local p = PP[teto_i][1][i][j];
				#local PP[teto_i][1][i][j] = <p.x, p.y, p.z + 3>;
			#end
		#end
	#end
	PP
#end

#macro traseiraCarro(PP, NH, NV)
	
	#local p = PP[1][2][0][3];
	#local PP[1][2][0][3] = <p.x, p.y, p.z - 3>;

	#local p = PP[1][2][1][3];
	#local PP[1][2][1][3] = <p.x, p.y, p.z - 3>;

	#local p = PP[1][2][2][3];
	#local PP[1][2][2][3] = <p.x, p.y, p.z - 3>;

	#local p = PP[1][2][3][3];
	#local PP[1][2][3][3] = <p.x, p.y, p.z - 3>;

	//bagageiro R
	#local p = PP[0][2][0][3];
	#local PP[0][2][0][3] = <p.x, p.y, p.z - 3>;

	#local p = PP[0][2][1][3];
	#local PP[0][2][1][3] = <p.x, p.y, p.z - 3>;

	#local p = PP[0][2][2][3];
	#local PP[0][2][2][3] = <p.x, p.y, p.z - 3>;

	#local p = PP[0][2][3][3];
	#local PP[0][2][3][3] = <p.x, p.y, p.z - 3>;
	PP
#end
						
// Partes da cena:

union{
	object{ eixos(3.00) }

	#local NH = 3;
	#local NV = 2;	
	#local PP = matrizPrincipal(NH, NV);
	#local PP = frenteCarro(PP, NH, NV);
	#local PP = tetoCarro(PP, NH, NV);
	#local PP = traseiraCarro(PP, NH, NV);
	//emenda horizontal
	emenda_retalho(PP, 0,0,0,1)
	emenda_retalho(PP, 0,0,1,0)
	emenda_retalho(PP, 1,0,1,1)
	emenda_retalho(PP, 1,1,1,2)
	//emenda vertical
	emenda_retalho(PP, 0,0,1,0)
	emenda_retalho(PP, 0,1,1,1)
	emenda_retalho(PP, 0,2,1,2)

	
	object{ carroceria(PP, NH, NV)}
}

#include "camlight.inc"
#declare centro_cena = < 0,2,-2>;
#declare raio_cena = 13;
#declare dir_camera = < 2,1,1 >;
#declare dist_camera = 2*raio_cena;
#declare intens_luz = 1.20;
camlight(centro_cena, raio_cena, dir_camera, dist_camera , z, intens_luz)