libGDX Draw viewport only partly while cutting off the rest

Innerwolf :

This problem seemed very obvious for me to solve, but whatever I try, it doesn't work. What I'm trying to do is to incorporate a mini-version of my PlayScreen in a ScrollPane as a tutorial where you can read text and try it out immediately.

Because I didn't find any better solution to add this to the Table inside the ScrollPane, I edited the draw() method of the PlayScreen to take the ScrollPane.getScrollPercentY() and offset the camera of the PlayScreen accordingly.

What I want to do now is to only render only part of the viewport that would be normally visible in the real game. Subsequently, I want to be able to control the size and position of this "window".

libGdx Problem visualised

I also want to be able to resize and move the content, while cutting off the edges that are not visible to the camera. This is what I tried inside the PlayScreenDraw:

public void draw(final float yOffset,
                 final int xTiles,
                 final int yTiles) {
    view.getCamera().position.y = yTiles / 2f - yOffset * yTiles / HEIGHT; // HEIGHT = 800
    view.getCamera().position.x = xTiles / 2f;
    view.setWorldSize(xTiles, yTiles); //Do i even need to change the world size?
    b.setProjectionMatrix(view.getCamera().combined);

    b.begin();
    ...
    b.end();

    view.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

What this gives me, in terms of the picture above, is this

libGDX Problem visualised 2

How do I need to change the viewport and/or the camera? Btw., this is how i set the two up:

cam = new OrthographicCamera();
cam.setToOrtho(false, WIDTH, HEIGHT); // WIDTH = 8, HEIGHT = 16
batch.setProjectionMatrix(cam.combined);
view = new FitViewport(WIDTH, HEIGHT, cam);
Luis Fernando Frontanilla :

The Pixmap class can help you achieve what you want since you stated that you wanted to "cut off" the parts outside of the green selection box.

You need to render what the camera sees to an FBO and then get the pixmap from the FBO itself.

Framebuffer Objects are OpenGL Objects, which allow for the creation of user-defined Framebuffers. With them, one can render to non-Default Framebuffer locations, and thus render without disturbing the main screen.

-- OpenGL wiki

// Construct an FBO and keep a reference to it. Remember to dispose of it.
FrameBuffer fbo = new FrameBuffer(Format.RGBA8888, width, height, false);

public void render() {
//Start rendering to the fbo.
fbo.begin();
//From the camera's perspective.
batch.setProjectionMatrix(camera.combined);
batch.begin();

//Draw whatever you want to draw with the camera.

batch.end();
// Finished drawing, get pixmap.
Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
//Stop drawing to your fbo.
fbo.end();
}

After getting the pixmap you can iterate through the pixels and set the alpha of the pixels outside your green selection window to 0 making them invisible or "cutting them off"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24714&siteId=1