Dima
Dima

Reputation: 1189

Draw in FrameBuffer

I work with a program which draws some 2D textures and work with them. In this program was used libgdx. I have some problem with using FrameBuffer. I try draw some texture in my FrameBuffer and after that I need save changed texture(or draw) and use this texture in this FrameBuffer on more time. I try save texture via

Texture texture = mFrameBuffer.getColorBufferTexture() 

and I try just bind texture from FrameBuffer

mFilterBuffer.getColorBufferTexture().bind();

For first iteration all work good. But when I try use in FrameBuffer his ColorBufferTexture like texture I have fully black texture. Code:

public void process(MySprite psObject, float startX, float startY, 
float endX, float endY, int mWidth, int mHeight) {
         boolean frst = false;
         if(psObject.getFrameBuffer() == null){
             psObject.setFrameBuffer(new FrameBuffer(Pixmap.Format.RGBA8888,     psObject.getTexture().getWidth(), psObject.getTexture().getHeight(), true));
         }
         if(pSprite == null || pSprite != psObject){
             mFrameBuffer = psObject.getFrameBuffer();
             frst = true;
             pSprite = psObject;
         }
         mFrameBuffer.begin();
         Gdx.gl.glViewport(0, 0, psObject.getTexture().getWidth(), psObject.getTexture().getHeight());
         Gdx.graphics.getGL20().glClearColor(0f, 0f, 0f, 1f);
         Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT);
         ShaderProgram shader = MyUtils.newInstance().getCurrentShader();
         if(!shader.isCompiled()){
             Log.i("ERROR", "SHERROR " + shader.getLog());
         }
         if(shader != null){
             if(frst){
                 psObject.getTexture().bind();
             }else{
       mFrameBuffer.getColorBufferTexture().bind();
             }
             shader.begin();
             Matrix4 matrix = new Matrix4();
             matrix.setToRotation(1, 0, 0, 180);
             matrix.scale(scaleSizeInFilterProcessor, scaleSizeInFilterProcessor, 1);
             shader.setUniformMatrix("u_worldView", matrix);
             shader.setUniformi("u_texture", 0);
             float [] start = new float[]{0f,0};
             float [] end = new float[]{1f,1f};
             MyUtils.newInstance().getShaderData(shader, start, end, mWidth, mHeight);
             psObject.getMesh().render(shader, GL20.GL_TRIANGLES);
             shader.end();
         }
         mFrameBuffer.end();
     }

Upvotes: 1

Views: 895

Answers (1)

aacotroneo
aacotroneo

Reputation: 2220

Your code needs some refactoring ;). Anyway, you can't read and write from/to the same FBO if that's your question. You'll need 2 FBO's (say A and B)

Draw scene to A,
Bind A's color texture
Draw scene to B (now you can read from A).

Note that you can extend libgdx FBO to have many textures associated with the same FBO.

Upvotes: 1

Related Questions