/* 
 * @param ctx - drawing context (from canvas)
 * 
 * @param shape_data - a dict with 2 keys:
 *    "vertices" - a list of 3d vertices
 *    "edges" - a list of lists of indices into
 *              the vertices list (each sublist
 *              defines a face)
 *
 */
shape = function(ctx, shape_data, initial_position){
	/*
	 * draw the shape!
	 */
	this.draw = function(){
		var pos = initial_position;	
		var faces = shape_data["faces"];
		var vertices = shape_data["vertices"];
		var tmp_ctx = ctx;

		for (var i=0; i < faces.length; i++){ // each face
			var face = faces[i];
			
			tmp_ctx.beginPath();
			tmp_ctx.moveTo(pos[0] + vertices[face[0]][0], pos[1] + vertices[face[0]][1]);

			for (var j=1; j < faces[i].length; j++){ // each vertex within the face
				var vertex = face[j];
				tmp_ctx.lineTo(pos[0] + vertices[vertex][0], pos[1] + vertices[vertex][1]);
			}
			// finally, connect back to the first vertex
			tmp_ctx.lineTo(pos[0] + vertices[face[0]][0], pos[1] + vertices[face[0]][1]);
			tmp_ctx.stroke();
		}
	}
	return this;
}

ALL_SHAPES = {
	"cargo": {
		"vertices": [
			[ 24.0, 16.0,  0.0],
			[ 24.0,  5.0, 15.0],
			[ 24.0,-13.0,  9.0],
			[ 24.0,-13.0, -9.0],
			[ 24.0,  5.0,-15.0],
			[-24.0, 16.0,  0.0],
			[-24.0,  5.0, 15.0],
			[-24.0,-13.0,  9.0],
			[-24.0,-13.0, -9.0],
			[-24.0,  5.0,-15.0]
		],
		// each number in each faces list is an index into
		// the vertices list - each adjacent pair of numbers
		// in the face list defines an edge.
		// The order we connect the vertices in each face is 
		// important for back-face culling (using some vector
		// maths tricks).
		"faces":[
			[0,1,2,3,4],
			[9,8,7,6,5],
			[5,6,1,0],
			[0,4,9,5],
			[6,7,2,1],
			[7,8,3,2],
			[8,9,4,3]
		]
	},
	"cobra_mk3":{
		"vertices":[
			[  40, 0.0, 95],
			[ -40, 0.0, 95],
			[  00, 32.5, 30],
			[ -150,-3.8,-10],
			[  150,-3.8,-10],
			[ -110, 20,-50],
			[  110, 20,-50],
			[  160,-10,-50],
			[ -160,-10,-50],
			[  0, 32.5,-50],
			[ -40,-30,-50],
			[  40,-30,-50],
			[ -45, 10,-55],
			[ -10, 15,-55],
			[  10, 15,-55],
			[  45, 10,-55],      
			[  45,-15,-55],
			[  10,-20,-55],
			[ -10,-20,-55],
			[ -45,-15,-55],
			[ -2,-2, 95],
			[ -2,-2, 112.5],
			[ -100,-7.5,-55],
			[ -100, 7.5,-55],
			[ -110, 0,-55],
			[  100, 7.5,-55],
			[  110, 0,-55],
			[  100,-7.5,-55],
			[  2,-2, 95],
			[  2,-2, 112.5],
			[  2, 2, 95],
			[  2, 2, 112.5],
			[ -2, 2, 95],
			[ -2, 2, 112.5]
		],
		"faces":[
			[2,1,0],
			[0,1,10,11],
			[6,2,0],
			[0,4,6],
			[0,11,7,4],
			[1,2,5],
			[5,3,1],
			[1,3,8,10],
			[5,2,9],
			[2,6,9],
			[5,8,3],
			[7,6,4],
			[9,6,7,11,10,8,5],
			[14,15,16,17],
			[12,13,18,19],
			[25,26,27],
			[24,23,22],
			[21,29,28,20],
			[31,29,28,30],
			[32,30,31,33],
			[21,33,32,20],
			[20,29,31,33]
		]
	}
}


