0

In my program I first render a scene seen by some camera, and then I want to retrieve this scene (Colorbuffer) to a Texture object so I can then paint it to a Quad taking up the whole screen. Though it looks stupid, the idea is just to see if the Texture is working fine, so I can use it in another not-so-stupid rendering (I'm trying to apply shadow mapping); and sadly, it's not working as I'd like. The getColorBufferTexture() call looks like it's returning a black texture, no matter what final colour I give to FragColor in the first render Fragment Shader. Testing just the first render, with the default ColorBuffer, is displaying on the screen what it has to, so there's no problem with the first rendering. Here's the code:

First rendering,

FrameBuffer shadowBuffer = new FrameBuffer(Pixmap.Format.RGBA8888,20, 20, true);
        shadowBuffer.begin();
        shadowShaderProgram.begin();
        for(DisplayableObject obj : objects) {
            shadowShaderProgram.setUniformMatrix("u_modelViewProjectionMatrix", shadowCamera.getPVMatrix().mul(obj.getTMatrix()));
            shadowShaderProgram.setUniformMatrix("u_modelViewMatrix", shadowCamera.getVMatrix().mul(obj.getTMatrix()));
            obj.getTexture().bind();
            obj.getMesh().render(shadowShaderProgram, GL20.GL_TRIANGLES);
        }
        shadowShaderProgram.end();
        shadowBuffer.end();           
        Texture shadowMap = shadowBuffer.getColorBufferTexture();

And then the second, trying to paint the texture,

Mesh mesh = new Mesh(true, 4, 6, new VertexAttribute(VertexAttributes.Usage.Position, 4, "a_position"), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoord0"));
        mesh.setVertices(new float[] { //position, texCoord
            -1f, -1f, 0f, 1f, 0f, 0f,
            1f, -1f, 0f, 1f, 1f, 0f,
            1f, 1f, 0f, 1f, 1f, 1f,
            -1f, 1f, 0f, 1f, 0f, 1f
   });
        mesh.setIndices(new short[]{0,1,2,2,3,0});
        String vs = Gdx.files.internal("auxShaderVert.glsl").readString();
        String fs = Gdx.files.internal("auxShaderFrag.glsl").readString();
        ShaderProgram auxShader = new ShaderProgram(vs,fs);
        auxShader.begin();
        shadowMap.bind();
        auxShader.setUniformi("shadowMap", 0);
        mesh.render(auxShader,GL20.GL_TRIANGLES);
        auxShader.end();

The shaders of this second rendering are, respectively:

VS,

attribute vec4 a_position;
attribute vec2 a_texCoord0;

varying vec2 v_texCoord;

void main() {
    v_texCoord = a_texCoord0;
    gl_Position = a_position;
}

FS,

uniform sampler2D shadowMap;
varying vec2 v_texCoord;

void main() {
    gl_FragColor = texture2D(shadowMap, v_texCoord);
}

I could not find much help through posts and questions online to see what's happening with the Texture retrieving; hope you can help me, I'm sure it must be something really stupid...

Thanks.

jcasado94
  • 185
  • 1
  • 1
  • 7
  • Make sure to call `Gdx.gl.glClear(...)` directly after binding a new render target, so directly after `shadowBuffer.begin()`. Besides that, having a FBO of 20x20 pixels is probably not very usable, you might want to consider using e.g. 1024x1024 for that. – Xoppa Nov 15 '15 at 00:02
  • Thanks, man! it worked. My I know the reason for the glClear? :/ – jcasado94 Nov 15 '15 at 00:35
  • Also had to add the dispose method for the FrameBuffer, I didn't know libGdx "stored" them forever... :/ – jcasado94 Nov 15 '15 at 00:41
  • It's to avoid having to reconstruct the fbo, e.g. have a look at http://stackoverflow.com/a/11052366/2512687. As for dispose, have a look at: https://github.com/libgdx/libgdx/wiki/Memory-management – Xoppa Nov 15 '15 at 11:54

0 Answers0