Hi,
with a TextureRenderer, I want to use a shader and write to multiple textures. I can do that in glsl, if I write to gl_FragData[…].
vertex2.vert
void main()
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord1;
}
fragment2.frag:
void main ()
{
gl_FragData[0]=vec4(0.0,0.0,1.0,1.0);
gl_FragData[1]=vec4(0.0,1.0,0.0,0.0);
}
fragment3.frag
uniform sampler2D texSampler;
uniform sampler2D texSampler2;
void main ()
{
vec4 color=texture2D(texSampler, gl_TexCoord[0].xy);
gl_FragColor=color+texture2D(texSampler2, gl_TexCoord[1].xy);
}
Here is, what I tried with java:
oTexture=new Texture();
oTexture2=new Texture();
TextureState oState=renderer.createTextureState();
oState.setEnabled(true);
oState.setTexture(oTexture,0);
FloatBuffer texCoords = BufferUtils.createVector2Buffer(4);
texCoords.put(-1).put(-1);
texCoords.put(-1).put(1);
texCoords.put(1).put(1);
texCoords.put(1).put(-1);
oQuad=new Quad("myQuad",width,height);
oQuad.setLocalTranslation(new Vector3f(width/2,height/2,0));
oQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);
oQuad.setTextureBuffer(0, texCoords);
oQuad.setRenderState(oState);
oTextureRenderer=DisplaySystem.getDisplaySystem().createTextureRenderer(width, height,TextureRenderer.RENDER_TEXTURE_2D);
oTextureRenderer.setupTexture(oTexture);
oTextureRenderer.setupTexture(oTexture2);
so=renderer.createGLSLShaderObjectsState();
so.load(getClass().getClassLoader().getResource(
"vertex2.vert"),
getClass().getClassLoader().getResource(
"fragment2.frag"));
so.setEnabled(true);
oQuad2=new Quad("myQuad2",width,height);
oQuad2.setLocalTranslation(new Vector3f(width/2,height/2,0));
oQuad2.setRenderQueueMode(Renderer.QUEUE_ORTHO);
oQuad2.setTextureBuffer(0, texCoords);
oQuad2.setRenderState(so);
oQuad2.updateRenderState();
GLSLShaderObjectsState so2=renderer.createGLSLShaderObjectsState();
so2.load(getClass().getClassLoader().getResource(
"vertex2.vert"),
getClass().getClassLoader().getResource(
"fragment3.frag"));
so2.setEnabled(true);
so2.setUniform("texSampler", 0);
so2.setUniform("texSampler2", 1);
oQuad3=new Quad("myQuad3",width,height);
oQuad3.setLocalTranslation(new Vector3f(width/2,height/2,0));
oQuad3.setRenderQueueMode(Renderer.QUEUE_ORTHO);
oQuad3.setTextureBuffer(0, texCoords);
oQuad3.setRenderState(so2);
oQuad3.updateRenderState();
rootNode.attachChild(oQuad);
rootNode.updateRenderState();
oTextureRenderer.setMultipleTargets(true);
oTextureRenderer.render(oQuad2, oTexture, false);
oTextureRenderer.render(oQuad3, oTexture, false);
The first shader with fragment2.frag should write colors to oTexture and oTexture2. The second shader with fragment3.frag should read these colors, add them and write them back. But, my java code doesn't work. Do you know, what's wrong with that and how to fix it?
Best,
Andreas