package org.helioviewer.filter.runningdifference;

import javax.media.opengl.GL;

import org.helioviewer.base.physics.Constants;
import org.helioviewer.viewmodel.view.opengl.shader.GLFragmentShaderProgram;
import org.helioviewer.viewmodel.view.opengl.shader.GLShaderBuilder;
import org.helioviewer.viewmodel.view.opengl.shader.GLTextureCoordinate;
import org.helioviewer.viewmodel.view.opengl.shader.GLShaderBuilder.GLBuildShaderException;

public class DifferenceShader extends GLFragmentShaderProgram {
    private GLTextureCoordinate isDifference;
    private GLTextureCoordinate differenceAngle;

	private static int ID = 0;
    int mode = -1;

    public void setIsDifference(GL gl, float isDifference) {
    	this.isDifference.setValue(gl, isDifference);
    }
    public void setDifferenceAngle(GL gl, float differenceAngle) {
    	this.differenceAngle.setValue(gl, differenceAngle);
    }
    /**
     * {@inheritDoc}
     */
    protected void buildImpl(GLShaderBuilder shaderBuilder) {

        try {
            isDifference = shaderBuilder.addTexCoordParameter(1);
            differenceAngle = shaderBuilder.addTexCoordParameter(1);

            String program = "if(isdifference>0.5){\toutput.r = ( output.r - tex2D(differenceImage, texcoord0.xy).r)/(output.r);";
            program += "\tvec4 tr = vec4(0.05f,0.05f,0.05f,0.05f);";
            program += "output.r = (output.r + 1.0f)/2.0f;}";
            program = program.replaceAll("output", shaderBuilder.useOutputValue("float4", "COLOR"));
            
            mode = shaderBuilder.addTextureParameter("sampler2D differenceImage" + 3);
            program = program.replaceAll("differenceImage", "differenceImage" + 3);
            ID = (ID + 1) & 15;
            program = program.replace("isdifference", isDifference.getIdentifier(1));
            program = program.replace("differenceAngle", differenceAngle.getIdentifier(1));
            
            program = program.replace("physicalPosition", shaderBuilder.useStandardParameter("float4", "TEXCOORD0"));
            program = program.replace("sunRadius", Double.toString(Constants.SunRadius).replace(',', '.'));            
            shaderBuilder.addMainFragment(program);
            System.out.println("SHADERDIFF: " + shaderBuilder.getCode());
        } catch (GLBuildShaderException e) {
            e.printStackTrace();
        }
    }

	public void activateDifferenceTexture(GL gl) {
	}
}	

@@ -1139,17 +1139,16 @@
 ter(1);%0A
-%0A
         
@@ -1167,16 +1167,42 @@
 rogram =
+ %22%22;%0A            program =
  %22if(isd
@@ -1427,16 +1427,101 @@
 .0f;%7D%22;%0A
+            //program = %22%5Ctoutput.rgb = tex2D(differenceImage, texcoord0.xy).rgb;%22;%0A%0A
         
@@ -1710,96 +1710,8 @@
 age%22
- + 3);%0A            program = program.replaceAll(%22differenceImage%22, %22differenceImage%22 + 3
 );%0A 
@@ -1936,246 +1936,8 @@
    %0A
-            program = program.replace(%22physicalPosition%22, shaderBuilder.useStandardParameter(%22float4%22, %22TEXCOORD0%22));%0A            program = program.replace(%22sunRadius%22, Double.toString(Constants.SunRadius).replace(',', '.'));            %0A
     

remove some junk from shader