package com.graphicsengine.spritemesh;

import com.nucleus.assets.AssetManager;
import com.nucleus.common.Constants;
import com.nucleus.geometry.Mesh;
import com.nucleus.io.ExternalReference;
import com.nucleus.opengl.GLES20Wrapper;
import com.nucleus.opengl.GLException;
import com.nucleus.renderer.Pass;
import com.nucleus.shader.ShaderVariables;
import com.nucleus.texturing.Texture2D;
import com.nucleus.texturing.TextureFactory;
import com.nucleus.texturing.TextureParameter;
import com.nucleus.texturing.TextureType;
import com.nucleus.texturing.TextureUtils;
import com.nucleus.vecmath.Matrix;

/**
 * Shader for second shadow pass
 * This should combine the sprite program with shadow render
 *
 */
public class ShadowPass2Program extends TiledSpriteProgram {

    Texture2D shadow;

    public ShadowPass2Program(Pass pass, String category, Texture2D.Shading shading) {
        super(pass, shading, category);
        shadow = TextureFactory.createTexture(TextureType.Texture2D);
        ExternalReference ref = new ExternalReference(ExternalReference.ID_LOOKUP + "DEPTHshadow");
        shadow.setExternalReference(ref);
        shadow.set(new TextureParameter(TextureParameter.DEFAULT_TEXTURE_PARAMETERS));
    }

    @Override
    public void setUniformMatrices(float[] uniforms, float[][] matrices, Mesh mesh) {
        // Refresh the uniform matrix using light matrix
        System.arraycopy(matrices[0], 0, getUniforms(),
                shaderVariables[ShaderVariables.uMVMatrix.index].getOffset(),
                Matrix.MATRIX_ELEMENTS);
        System.arraycopy(matrices[1], 0, getUniforms(),
                shaderVariables[ShaderVariables.uProjectionMatrix.index].getOffset(),
                Matrix.MATRIX_ELEMENTS);
        System.arraycopy(matrices[2], 0, getUniforms(),
                shaderVariables[ShaderVariables.uLightMatrix.index].getOffset(),
                Matrix.MATRIX_ELEMENTS);
    }

    @Override
    public void prepareTextures(GLES20Wrapper gles, Mesh mesh) throws GLException {
        int textureID = shadow.getName();
        if (textureID == Constants.NO_VALUE) {
            AssetManager.getInstance().getIdReference(shadow);
            textureID = shadow.getName();
        }
        int unit = samplers[shaderVariables[ShaderVariables.uShadowTexture.index].getOffset()];
        TextureUtils.prepareTexture(gles, shadow, unit);
        Texture2D texture = mesh.getTexture(Texture2D.TEXTURE_0);
        if (texture != null && texture.textureType != TextureType.Untextured) {
            TextureUtils.prepareTexture(gles, texture,
                    samplers[shaderVariables[ShaderVariables.uTexture.index].getOffset()]);
        }
    }

}

@@ -222,24 +222,116 @@
 S20Wrapper;%0A
+import com.nucleus.opengl.GLESWrapper.GLES20;%0Aimport com.nucleus.opengl.GLESWrapper.GLES30;%0A
 import com.n
@@ -357,16 +357,16 @@
 eption;%0A
-
 import c
@@ -434,16 +434,60 @@
 iables;%0A
+import com.nucleus.texturing.ParameterData;%0A
 import c
@@ -606,24 +606,240 @@
 eParameter;%0A
+import com.nucleus.texturing.TextureParameter.Name;%0Aimport com.nucleus.texturing.TextureParameter.Param;%0Aimport com.nucleus.texturing.TextureParameter.Parameter;%0Aimport com.nucleus.texturing.TextureParameter.Target;%0A
 import com.n
@@ -1264,16 +1264,84 @@
 egory);%0A
+        // This defines the texture parameters for the shadow pass.%0A
         
@@ -1588,53 +1588,375 @@
 ter(
-TextureParameter.DEFAULT_TEXTURE_PARAMETERS))
+%0A                new Parameter%5B%5D %7B Parameter.LINEAR, Parameter.LINEAR, Parameter.CLAMP, Parameter.CLAMP %7D));%0A        ParameterData%5B%5D extra = new ParameterData%5B%5D %7B%0A                new ParameterData(Target.TEXTURE_2D, Name.TEXTURE_COMPARE_MODE, Param.COMPARE_REF_TO_TEXTURE),%0A                new ParameterData(Target.TEXTURE_2D, Name.TEXTURE_COMPARE_FUNC, Param.LESS) %7D
 ;%0A  
@@ -3058,16 +3058,16 @@
 set()%5D;%0A
-
         
@@ -3115,16 +3115,234 @@
  unit);%0A
+        gles.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES30.GL_TEXTURE_COMPARE_MODE, GLES30.GL_COMPARE_REF_TO_TEXTURE);%0A        gles.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES30.GL_TEXTURE_COMPARE_FUNC, GLES20.GL_LESS);%0A
         

Set shadow render texture parameters in shaderprogram