Deniz Yılmaz
Deniz Yılmaz

Reputation: 1094

Flipped TextureRegion to Texture

I am using FBO and trying to flip texture on y axis. I remember that i was able to do it with TextureRegion.flip().

However when i try to flip now its not working.

    GLFrameBuffer.FrameBufferBuilder frameBufferBuilder = new GLFrameBuffer.FrameBufferBuilder(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    frameBufferBuilder.addColorTextureAttachment(GL30.GL_RGBA8, GL30.GL_RGBA, GL30.GL_UNSIGNED_BYTE);
    FrameBuffer frameBuffer = frameBufferBuilder.build();
    frameBuffer.begin();
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT | (Gdx.graphics.getBufferFormat().coverageSampling ? GL30.GL_COVERAGE_BUFFER_BIT_NV : 0));
    batch.begin();

    if (textures[1] != null) {
        batch.draw(textures[1], x + marginx[1], y + marginy[1]);
    }

    batch.end();

    frameBuffer.end();

    Texture texturefbo = frameBuffer.getColorBufferTexture();
    framebuffer.dispose(); 
    textreg = new TextureRegion(texturefbo);
    textreg.flip(false, true);
    texturedraw = textreg.getTexture();

I know I can use TextureRegion in place of Texture but im still want to learn that why converting from textureregion to texture not transferring uvu2v2.

Upvotes: 0

Views: 238

Answers (1)

Tenfour04
Tenfour04

Reputation: 93902

Flipping a TextureRegion doesn't do anything to the Texture it is a part of. A Texture object does not contain any UV data. It is an image and nothing more. When you pass a Texture to SpriteBatch.draw(Texture), SpriteBatch internally uses UVs that stretch it to the rectangle X-right and Y-up. You can also pass in a negative width to an overloaded SpriteBatch.draw() method to flip it horizontally, for example. The flipping has no effect on the Texture object, only the SpriteBatch's internal drawing data.

Also, if you dispose of the FrameBuffer that owns the Texture, your Texture is not guaranteed to work. In fact, I don't think it should at all.

If you really needed to flip a Texture, you could do one of these.

  1. Flip the source image ahead of time.
  2. Load the image as a Pixmap, iterate through the pixels to draw them to another Pixmap in reverse rows, dispose the first Pixmap, and finally pass the second Pixmap into a Texture constructor.
  3. If the Texture is not from an image file, you could use GLUtils to copy the pixels from the Texture object into a Pixmap and then do step 2.

Of course, 2 and 3 are way more complicated than simply using a flipped TextureRegion.

Upvotes: 1

Related Questions