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.GLESWrapper.GLES20;
import com.nucleus.opengl.GLException;
import com.nucleus.opengl.GLUtils;
import com.nucleus.renderer.Pass;
import com.nucleus.shader.ShaderVariables;
import com.nucleus.shader.VariableMapping;
import com.nucleus.texturing.Texture2D;
import com.nucleus.texturing.TextureFactory;
import com.nucleus.texturing.TextureParameter;
import com.nucleus.texturing.TextureType;
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 setUniformData(float[] uniforms, Mesh mesh) {
        setScreenSize(uniforms, shaderVariables[ShaderVariables.uScreenSize.index]);
        setTextureUniforms(uniforms, mesh.getTexture(Texture2D.TEXTURE_0));
    }

    @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 setUniforms(GLES20Wrapper gles, float[] uniforms, VariableMapping[] uniformMapping) throws GLException {
        int textureID = shadow.getName();
        if (textureID == Constants.NO_VALUE) {
            AssetManager.getInstance().getIdReference(shadow);
            textureID = shadow.getName();
            gles.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
            gles.uploadTexParameters(shadow.getTexParams());
        } else {
            gles.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
            GLUtils.handleError(gles, "glBindTexture()");
        }
        super.setUniforms(gles, uniforms, uniformMapping);
    }

}

@@ -259,97 +259,16 @@
 .GLE
-SWrapper.GLES20;%0Aimport com.nucleus.opengl.GLException;%0Aimport com.nucleus.opengl.GLUtils
+xception
 ;%0Aim
@@ -346,51 +346,8 @@
 es;%0A
-import com.nucleus.shader.VariableMapping;%0A
 impo
@@ -516,16 +516,59 @@
 reType;%0A
+import com.nucleus.texturing.TextureUtils;%0A
 import c
@@ -1222,252 +1222,8 @@
  %7D%0A%0A
-    @Override%0A    public void setUniformData(float%5B%5D uniforms, Mesh mesh) %7B%0A        setScreenSize(uniforms, shaderVariables%5BShaderVariables.uScreenSize.index%5D);%0A        setTextureUniforms(uniforms, mesh.getTexture(Texture2D.TEXTURE_0));%0A    %7D%0A%0A
     
@@ -1948,26 +1948,30 @@
 ic void 
-setUniform
+prepareTexture
 s(GLES20
@@ -1988,58 +1988,17 @@
 es, 
-float%5B%5D uniforms, VariableMapping%5B%5D uniformMapping
+Mesh mesh
 ) th
@@ -2222,207 +2222,308 @@
     
-    gles.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);%0A            gles.uploadTexParameters(shadow.getTexParams());%0A        %7D else %7B%0A            gles.glBindTexture(GLES20.GL_TEXTURE_2D, 
+%7D%0A        int unit = samplers%5BshaderVariables%5BShaderVariables.uShadowTexture.index%5D.getOffset()%5D;%0A        TextureUtils.prepareTexture(gles, shadow, unit);%0A        Texture2D texture = mesh.getTexture(Texture2D.TEXTURE_0);%0A        if (texture != null && texture.textureType != TextureType.Un
 texture
-ID);
+d) %7B
 %0A   
@@ -2535,53 +2535,50 @@
     
-GLUtils.handleError(gles, %22glBindT
+TextureUtils.prepareTexture(gles, t
 exture
-()%22);
+,
 %0A   
@@ -2582,26 +2582,25 @@
         
-%7D%0A
+ 
         
 super.se
@@ -2595,58 +2595,92 @@
     
-super.setUniforms(gles, uniforms, uniformMapping);
+   samplers%5BshaderVariables%5BShaderVariables.uTexture.index%5D.getOffset()%5D);%0A        %7D
 %0A   

Fix multitexture, choose texture unit