/* CS184 Fall 1998 - Project 2
 * Raul 'Joni' Chu - cs184-bq - 12782756
 * Yuan Kui Shen   - cs184-ac - 12940472
 *
 * Planet surface shader - basically just some funky random-ish continents and water. 
 * Takes in optional parameters for land_color and sea_color.
 */

surface planet_surface(float Ka = 1.0, Kd = 1.0; color land_color = (0.5, 0.5, 0.2), sea_color = (0.1, 0.1, 0.5)) {
	float   surface_type = 0.0, 	/* what are we over? - land or sea? */
            	freq = 1.0,		/* frequency with which our continents are broken up (ie 10 would give a planet of islands) */
		i = 0;			/* temp variable for land jaggedness while loop */
	point   Pnt, 			/* land formation location */
		Nf;			/* normalized position vector (point really) */
	color Ccurr;			/* color of the surface we're currently flying over */

	Pnt = 2 * transform("shader", P);
	/* create borders of continents */
	while (i < 10) {
	        surface_type += (2 * noise(Pnt * freq) - 1) / freq;
        	freq *= 2;
		i += 1;
	} 
	/* color the surface, land or sea */
    	if (surface_type >= 0) {
        	Ccurr = land_color;
	}
    	else {
        	Ccurr = sea_color;
	}
	/* blend the planet's surface */
	Nf = normalize(faceforward(N, I));
	Ci = Os * (Ccurr * (Ka * ambient() + Kd * diffuse(Nf)));
}