jmonkeyengine icon indicating copy to clipboard operation
jmonkeyengine copied to clipboard

Update examples to use new framebuffer apis

Open riccardobl opened this issue 3 years ago • 3 comments

1. Create the targets

There are two types of targets:

1. Buffer target : render to a buffer

To create this target you need to specify a valid format

FrameBufferBufferTarget depthTarget= FrameBufferTarget.newTarget(Format.Depth);
FrameBufferBufferTarget colorTarget0= FrameBufferTarget.newTarget(Format.RGBA16F);
FrameBufferBufferTarget colorTarget1= FrameBufferTarget.newTarget(Format.RGB8);

2. Texture target : render to a texture

To create this target you need a valid texture, you might use an existing texture, or create a new one:

Texture2D myTexture = new Texture2D(512, 512, Format.RGBA8);

Note: we use this constructor to specify width,height and format, since those parameters are required by the renderer ( we cannot render on something of which we don't know sizes or format). You can also use the constructor that takes int numSamples, if you want to render on a multisample texture.

You can then create a target

FrameBufferTextureTarget colorTarget0= FrameBufferTarget.newTarget(myTexture);

Note: the type differs from the previous one ( FrameBufferTextureTarget vs FrameBufferBufferTarget) as you can guess, there are two overloaded newTarget methods one that takes Texture and outputs FrameBufferTextureTarget and one that takes Format and outputs FrameBufferBufferTarget .

2. Attach everything to the framebuffer

Once you have your targets, you can attach them to the framebuffer

Firstly we create the framebuffer

FrameBuffer fb = new FrameBuffer(512, 512, 1);

Note: if we are using texture targets, they must be of the same size of the framebuffer and have the same number of samples (>1 for multisample textures), in this case we have 512x512 textures without multisample (=1 sample), that's why we are passing those params.

Then we attach the targets

fb.setDepthTarget(depthTarget);
fb.addColorTarget(colorTarget);

We can have many color targets, just attach all of them with addColorTarget in the correct order.

3. Additional settings

  1. Enable multi render target
fb.setMultiTarget(true);

A MRT compatible shader will now be able to render on multiple color targets at the same time

  1. Render in srgb mode
myTexture.getImage().setColorSpace(ColorSpace.sRGB);
fb.setSrgb(true);

Everything that is rendered to this framebuffer will be converted from linear to sRGB space.

riccardobl avatar Oct 05 '21 19:10 riccardobl

Thanks for the explanation of targets.

stephengold avatar Oct 13 '21 18:10 stephengold

@riccardobl are you still available to work on this?

stephengold avatar Dec 06 '21 23:12 stephengold

What is the state of this PR? I see there are some conflicts reported.

Ali-RS avatar Dec 31 '22 11:12 Ali-RS