package org.helioviewer.filter.runningdifference;

import javax.media.opengl.GL;

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 static int ID = 0;
    int mode = -1;

    public void setIsDifference(GL gl, float isDifference) {
    	this.isDifference.setValue(gl, isDifference);
    }

    /**
     * {@inheritDoc}
     */
    protected void buildImpl(GLShaderBuilder shaderBuilder) {

        try {
            isDifference = 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));
            shaderBuilder.addMainFragment(program);
            System.out.println("SHADERDIFF: " + shaderBuilder.getCode());
        } catch (GLBuildShaderException e) {
            e.printStackTrace();
        }
    }

	public void activateDifferenceTexture(GL gl) {
	}
}	

@@ -75,16 +75,63 @@
 gl.GL;%0A%0A
+import org.helioviewer.base.physics.Constants;%0A
 import o
@@ -199,16 +199,16 @@
 rogram;%0A
-
 import o
@@ -544,16 +544,66 @@
 erence;%0A
+    private GLTextureCoordinate differenceAngle;%0A%0A
 %09private
@@ -760,17 +760,146 @@
 ;%0A    %7D%0A
+    public void setDifferenceAngle(GL gl, float differenceAngle) %7B%0A    %09this.differenceAngle.setValue(gl, differenceAngle);%0A    %7D
 %0A
-
     /**%0A
@@ -1070,16 +1070,86 @@
 ter(1);%0A
+            differenceAngle = shaderBuilder.addTexCoordParameter(1);%0A%0A
         
@@ -1197,32 +1197,33 @@
 )%7B%5Ctoutput.r = (
+ 
 output.r - tex2D
@@ -1257,24 +1257,25 @@
 .xy).r)/
+(
 output.r
 ;%22;%0A    
@@ -1266,16 +1266,17 @@
 output.r
+)
 ;%22;%0A    
@@ -1719,16 +1719,16 @@
 ) & 15;%0A
-
         
@@ -1801,24 +1801,367 @@
 tifier(1));%0A
+            program = program.replace(%22differenceAngle%22, differenceAngle.getIdentifier(1));%0A            %0A            program = program.replace(%22physicalPosition%22, shaderBuilder.useStandardParameter(%22float4%22, %22TEXCOORD0%22));%0A            program = program.replace(%22sunRadius%22, Double.toString(Constants.SunRadius).replace(',', '.'));            %0A
             

put difference angle in difference shader