package javax.microedition.m3g;

import javax.microedition.khronos.opengles.GL10;

public class Material extends Object3D {
	public static final int AMBIENT = 1024;
	public static final int DIFFUSE = 2048;
	public static final int EMISSIVE = 4096;
	public static final int SPECULAR = 8192;

	private int ambientColor = 0x00333333;
	private int diffuseColor = 0xFFCCCCCC;
	private int emissiveColor = 0;
	private int specularColor = 0;
	private float shininess = 10.0f;
	private boolean isVertexColorTrackingEnabled = false;

	public Material() {
	}

	Object3D duplicateImpl() {
		Material copy = new Material();
		copy.ambientColor = ambientColor;
		copy.diffuseColor = diffuseColor;
		copy.emissiveColor = emissiveColor;
		copy.specularColor = specularColor;
		copy.shininess = shininess;
		copy.isVertexColorTrackingEnabled = isVertexColorTrackingEnabled;
		return copy;
	}

	@Override
	void updateProperty(int property, float[] value) {
		switch (property) {
			case AnimationTrack.ALPHA:
				diffuseColor = (diffuseColor | 0xFF000000) & (ColConv.alpha1f(value[0]) << 24);
				break;
			case AnimationTrack.AMBIENT_COLOR:
				ambientColor = ColConv.color3f(value[0], value[1], value[2]);
				break;
			case AnimationTrack.DIFFUSE_COLOR:
				diffuseColor = (diffuseColor | 0x00FFFFFF) & (ColConv.color3f(value[0], value[1], value[2]));
				break;
			case AnimationTrack.EMISSIVE_COLOR:
				emissiveColor = (ColConv.color3f(value[0], value[1], value[2]) & 0x00FFFFFF);
				break;
			case AnimationTrack.SHININESS:
				shininess = Math.max(0.f, Math.min(128.f, value[0]));
				break;
			case AnimationTrack.SPECULAR_COLOR:
				specularColor = ColConv.color3f(value[0], value[1], value[2]);
				break;
			default:
				super.updateProperty(property, value);
		}
	}

	public void setColor(int target, int color) {
		if ((target & AMBIENT) != 0)
			this.ambientColor = color;
		if ((target & DIFFUSE) != 0)
			this.diffuseColor = color;
		if ((target & EMISSIVE) != 0)
			this.emissiveColor = color;
		if ((target & SPECULAR) != 0)
			this.specularColor = color;
	}

	public int getColor(int target) {
		if (target == AMBIENT)
			return ambientColor;
		else if (target == DIFFUSE)
			return diffuseColor;
		else if (target == EMISSIVE)
			return emissiveColor;
		else if (target == SPECULAR)
			return specularColor;
		throw new IllegalArgumentException("Invalid color target");
	}

	public void setShininess(float shininess) {
		this.shininess = shininess;
	}

	public float getShininess() {
		return shininess;
	}

	public void setVertexColorTrackingEnable(boolean isVertexColorTrackingEnabled) {
		this.isVertexColorTrackingEnabled = isVertexColorTrackingEnabled;
	}

	public boolean isVertexColorTrackingEnabled() {
		return isVertexColorTrackingEnabled;
	}

	void setupGL(GL10 gl) {
		if (isVertexColorTrackingEnabled)
			gl.glEnable(GL10.GL_COLOR_MATERIAL);
		else {
			gl.glDisable(GL10.GL_COLOR_MATERIAL);

			gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, Color.intToFloatArray(ambientColor), 0);
			gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, Color.intToFloatArray(diffuseColor), 0);
		}
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_EMISSION, Color.intToFloatArray(emissiveColor), 0);
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, Color.intToFloatArray(specularColor), 0);
		gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, shininess);

		gl.glEnable(GL10.GL_LIGHTING);
	}

	boolean isCompatible(AnimationTrack track) {
		switch (track.getTargetProperty()) {
			case AnimationTrack.ALPHA:
			case AnimationTrack.AMBIENT_COLOR:
			case AnimationTrack.DIFFUSE_COLOR:
			case AnimationTrack.EMISSIVE_COLOR:
			case AnimationTrack.SHININESS:
			case AnimationTrack.SPECULAR_COLOR:
				return true;
			default:
				return super.isCompatible(track);
		}
	}
}

@@ -469,17 +469,16 @@
 iness = 
-1
 0.0f;%0D%0A%09
@@ -2161,26 +2161,30 @@
 t) %7B%0D%0A%09%09
-if
+switch
  (target
  == AMBI
@@ -2179,19 +2179,28 @@
 rget
- ==
+) %7B%0D%0A%09%09%09case
  AMBIENT
 )%0D%0A%09
@@ -2195,19 +2195,20 @@
  AMBIENT
-)
+:
 %0D%0A
+%09
 %09%09%09retur
@@ -2230,34 +2230,21 @@
 %0D%0A%09%09
-else if (target ==
+%09case
  DIFFUSE
 )%0D%0A%09
@@ -2239,19 +2239,20 @@
  DIFFUSE
-)
+:
 %0D%0A
+%09
 %09%09%09retur
@@ -2270,34 +2270,21 @@
 lor;%0D%0A%09%09
-else if (target ==
+%09case
  EMISSIV
@@ -2284,19 +2284,20 @@
 EMISSIVE
-)
+:
 %0D%0A
+%09
 %09%09%09retur
@@ -2320,26 +2320,13 @@
 %0D%0A%09%09
-else if (target ==
+%09case
  SPE
@@ -2330,19 +2330,20 @@
 SPECULAR
-)
+:
 %0D%0A
+%09
 %09%09%09retur
@@ -2360,16 +2360,31 @@
 Color;%0D%0A
+%09%09%09default:%0D%0A%09%09
 %09%09throw 
@@ -2434,24 +2434,29 @@
  target%22);%0D%0A
+%09%09%7D%0D%0A
 %09%7D%0D%0A%0D%0A%09publi

Fix material