2
\$\begingroup\$

Libgdx allows to fill area on a Pixmap just with circles, rectangles and triangles, but I need to fill complex figure that can be described with Polygon class, so how can I do that?

I was thinking about polygon triangulation, but it seems sophisticated to me.

May be exists some better way to fill complicated shapes with pixels?

\$\endgroup\$
2
  • \$\begingroup\$ Do you need to work on it as a Pixmap? Can you use a ShapeRenderer on a FrameBuffer and then get the Pixmap from that? \$\endgroup\$
    – bornander
    Commented Jul 18, 2018 at 14:26
  • \$\begingroup\$ I never used FrameBuffer, can you show simple code example of what you mean please? \$\endgroup\$
    – Divelix
    Commented Jul 19, 2018 at 4:50

1 Answer 1

1
\$\begingroup\$

Depending on what you need the Pixmap for you can probably use a FrameBuffer to draw anything on, including complex shapes using a ShapeRenderer.

Using a FrameBuffer you get a render target where you can draw whatever you like and then get a Texture of that that you can then draw onto the screen.

An example of using a FrameBuffer might look like this;

package com.bornander.playground3d;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

public class Playground3DGame extends ApplicationAdapter {
    private OrthographicCamera camera;
    private ShapeRenderer shapeRenderer;
    private FrameBuffer frameBuffer;
    private SpriteBatch spriteBatch;

    @Override
    public void create() {
        float aspectRatio = (float)Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth();
        float width = 100;
        float height = width * aspectRatio;
        camera = new OrthographicCamera(width, height);
        shapeRenderer = new ShapeRenderer();
        // Create a FrameBuffer that is 50x50 pixels
        frameBuffer = new FrameBuffer(Format.RGBA8888, 50, 50, true);
        spriteBatch = new SpriteBatch();
    }

    @Override
    public void render() {
        camera.update();
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        // Begin the FrameBuffer, this means render calls after this will
        // affect the FrameBuffer and not the screen
        frameBuffer.begin();

        // Use the ShapeRenderer to render what-ever shapes you need
        // (you can also draw anything you can draw onto the screen onto the FrameBuffer)
        shapeRenderer.setProjectionMatrix(camera.combined);
        shapeRenderer.begin(ShapeType.Filled);
        shapeRenderer.setColor(Color.RED);
        shapeRenderer.circle(0, 0, 16);
        shapeRenderer.end();

        frameBuffer.end();

        // Get the Texture from the FrameBuffer when done drawing
        Texture texture = frameBuffer.getColorBufferTexture();

        // If you still need to, you can get a Pixmap from the Texture
        Pixmap pixmap = null;
        TextureData textureData = texture.getTextureData();
        if (!textureData.isPrepared()) {
            textureData.prepare();
            pixmap = textureData.consumePixmap();
        }

        // If you don't need the Pixmap, you can draw the texture directly
        // onto the screen
        spriteBatch.begin();
        spriteBatch.draw(texture, 0, 0);
        spriteBatch.end();
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .