All pastes #634834 Raw Edit

Something

public text v1 · immutable
#634834 ·published 2007-07-26 18:07 UTC
rendered paste body
struct MTL{
	char[] name;
	//Ka
	Color ambient = {[51,51,51,255]};
	//Kd and d
	Color diffuse = {[204,204,204,255]};
	//Ks
	Color specular = {[255,255,255,255]};
	//Ns
	float shininess = 0.0;
	//map_Ka
	char[] texture;
}

MTL[] handleMTL(char[] source){
	char[] file = cast(char[])read(source);
	char[][] lines = splitlines(file);
	
	MTL[] mats;
	
	void handleLine(char[] line){
		char[][] words = split(line, " ");
		if(words.length != 0){
			switch(words[0]){
				case "newmtl":
					MTL m;
					m.name = line[7..$];
					mats ~= m;
					break;
				case "Ka":
					mats[$-1].ambient = Color(fromWords3(words));
					break;
				case "Kd":
					Vec3f rgb = fromWords3(words);
					mats[$-1].diffuse.r = cast(ubyte)(rgb.x * 255.0);
					mats[$-1].diffuse.g = cast(ubyte)(rgb.y * 255.0);
					mats[$-1].diffuse.b = cast(ubyte)(rgb.z * 255.0);
					break;
				case "Ks":
					mats[$-1].specular = Color(fromWords3(words));
					break;
				case "Tr":
				case "d":
					mats[$-1].diffuse.a = cast(ubyte)(toFloat(words[1]) * 255.0);
					break;
				case "Ns":
					mats[$-1].shininess = toFloat(words[1]);
					break;
				case "map_Ka":
					mats[$-1].texture = words[1];
					break;
				case "illum":
					break;
				default:
					break;
			}
		}
	}
	
	foreach(inout line; lines){
		line = strip(line);
		handleLine(line);
	}
	
	return mats;
}