....
Rectangle box1 = new Rectangle(0f, 0f, 10f, 10f, this.getVertexBufferObjectManager()) {
private boolean mRenderTextureInitialized;
private RenderTexture mRenderTexture;
private UncoloredSprite mRenderTextureSprite;
//static values for testing purposes
int surfaceWidth = 10;
int surfaceHeight = 10;
RenderTexture mRenderTextureBox2 = new RenderTexture(MainActivity.this.getTextureManager(), surfaceWidth, surfaceHeight);
@Override
protected void preDraw(final GLState pGLState, final Camera pCamera) {
this.setShaderProgram(SampleShaderProgram2.getInstance());
super.preDraw(pGLState, pCamera);
}
@Override
protected void draw(final GLState pGLState, final Camera pCamera) {
final boolean firstFrame = !mRenderTextureInitialized;
if (firstFrame) {
this.initRenderTextures(pGLState);
mRenderTextureInitialized = true;
}
mRenderTexture.begin(pGLState);
{
/*
* Draw current frame.
*/
super.draw(pGLState, pCamera);
}
mRenderTexture.end(pGLState);
/*
* Draw rendered texture with custom shader.
*/
{
pGLState.pushProjectionGLMatrix();
pGLState.orthoProjectionGLMatrixf(0, surfaceWidth, 0, surfaceHeight, -1, 1);
{
mRenderTextureSprite.onDraw(pGLState, pCamera);
}
pGLState.popProjectionGLMatrix();
}
}
private void initRenderTextures(final GLState pGLState) {
mRenderTexture = new RenderTexture(MainActivity.this.getTextureManager(), surfaceWidth, surfaceHeight);
mRenderTexture.init(pGLState);
final ITextureRegion renderTextureTextureRegion = TextureRegionFactory.extractFromTexture(mRenderTexture);
mRenderTextureSprite = new UncoloredSprite(0, 0, renderTextureTextureRegion, this.getVertexBufferObjectManager()) {
@Override
protected void preDraw(final GLState pGLState, final Camera pCamera) {
if (MainActivity.this.customShaders) {
this.setShaderProgram(SampleShaderProgram2.getInstance());
} else {
this.setShaderProgram(PositionTextureCoordinatesShaderProgram.getInstance());
}
super.preDraw(pGLState, pCamera);
}
};
}
};
box1.setShaderProgram(SampleShaderProgram2.getInstance());
mScene.attachChild(box1);
....
public static class SampleShaderProgram2 extends ShaderProgram {
private boolean mRenderTextureInitialized;
private RenderTexture mRenderTexture;
private UncoloredSprite mRenderTextureSprite;
private static SampleShaderProgram2 instance;
Random r = new Random();
public static SampleShaderProgram2 getInstance() {
if (instance == null) {
instance = new SampleShaderProgram2();
}
return instance;
}
public static final String DEFAULT_VERTEXSHADER = "uniform mat4 " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + ";\n"
+ "attribute vec4 " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n"
+ "void main() \n"
+ "{ \n"
+ " gl_Position = " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + " * " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n"
+ "}";
public static final String DEFAULT_FRAGMENTSHADER = "precision mediump float; \n"
+ "uniform vec4 " + ShaderProgramConstants.UNIFORM_COLOR + "; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = " + ShaderProgramConstants.UNIFORM_COLOR + "; \n"
//+ " gl_FragColor = " + "vec4(1.0, 0.0, 0.0, 1.0)" + "; \n"
+ "}";
private SampleShaderProgram2() {
super(DEFAULT_VERTEXSHADER, DEFAULT_FRAGMENTSHADER);
}
public static int sUniformModelViewPositionMatrixLocation = ShaderProgram.LOCATION_INVALID;
public static int sUniformTexture0Location = ShaderProgram.LOCATION_INVALID;
public static int sUniformColorLocation = ShaderProgram.LOCATION_INVALID;
@Override
protected void link(final GLState pGLState) throws ShaderProgramLinkException {
GLES20.glBindAttribLocation(this.mProgramID, ShaderProgramConstants.ATTRIBUTE_POSITION_LOCATION, ShaderProgramConstants.ATTRIBUTE_POSITION);
//GLES20.glBindAttribLocation(this.mProgramID, ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION, ShaderProgramConstants.ATTRIBUTE_COLOR);
//GLES20.glBindAttribLocation(this.mProgramID, ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES_LOCATION, ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES);
super.link(pGLState);
SampleShaderProgram2.sUniformModelViewPositionMatrixLocation = this.getUniformLocation(ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX);
SampleShaderProgram2.sUniformColorLocation = this.getUniformLocation(ShaderProgramConstants.UNIFORM_COLOR);
//SampleShaderProgram2.sUniformTexture0Location = this.getUniformLocation(ShaderProgramConstants.UNIFORM_TEXTURE_0);
}
@Override
public void bind(final GLState pGLState, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) {
//GLES20.glDisableVertexAttribArray(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION);
super.bind(pGLState, pVertexBufferObjectAttributes);
GLES20.glUniformMatrix4fv(SampleShaderProgram2.sUniformModelViewPositionMatrixLocation, 1, false, pGLState.getModelViewProjectionGLMatrix(), 0);
//GLES20.glUniform1i(SampleShaderProgram2.sUniformTexture0Location, 0);
GLES20.glUniform4f(SampleShaderProgram2.sUniformColorLocation, r.nextFloat(), r.nextFloat(), r.nextFloat(), r.nextFloat());
}
@Override
public void unbind(final GLState pGLState) throws ShaderProgramException {
//GLES20.glEnableVertexAttribArray(ShaderProgramConstants.ATTRIBUTE_COLOR_LOCATION);
super.unbind(pGLState);
}
}