Author Topic: How to use renderTarget?  (Read 1537 times)

Offline kiffa

  • long
  • ***
  • Posts: 199
    • View Profile
How to use renderTarget?
« on: December 05, 2013, 07:19:38 am »
My purpose is to render the scene 2 times with different camera per frame, the first rendering will render to full screen, the second rendering will render to a smaller(1/4w、1/4h of the screen). But my code seems useless(the result seems like which don't do the second rendering).

Code: [Select]

  private void init(){
    mTexture = new Texture(256, 128);
    mTexture.setMipmap(false);
    TextureManager.getInstance().addTexture("rt", mTexture);
  }
 
  private void update(World world, FrameBuffer frameBuffer){
    int screenWidth = 800;
    int screenHeight = 480;
   
    //first rendering
    world.renderScene(frameBuffer);
    world.draw(frameBuffer);
   
    //second rendering
    frameBuffer.setRenderTarget(mTexture);
    world.renderScene(frameBuffer);
    world.draw(frameBuffer);
    frameBuffer.blit(mTexture, 0, 0, 0, 0, screenWidth/4, screenHeight/4, false);
    frameBuffer.removeRenderTarget();
   
  }

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: How to use renderTarget?
« Reply #1 on: December 05, 2013, 07:53:04 am »
The first thing to be aware of is, that render to texture actually requires OpenGL ES 2.0 to be useful. It might work with 1.x as well, but because of some API limitations in 1.x, it's...well...limited and it doesn't work on all devices.
 
Your code misses some calls that are needed to make this work. You should rather do something like this:
 
 
Code: [Select]
  //first rendering
  frameBuffer.setRenderTarget(mTexture);
  frameBuffer.clear();
  world.renderScene(frameBuffer);
  world.draw(frameBuffer);
  frameBuffer.display();
  frameBuffer.removeRenderTarget();
 
  //second rendering
  frameBuffer.clear();
  world.renderScene(frameBuffer);
  world.draw(frameBuffer);
  frameBuffer.blit(mTexture, 0, 0, 0, 0, screenWidth/4, screenHeight/4, false);
  frameBuffer.display();
 
 
Note that i've moved the render to texture to the front. That's because it will overwrite the actual framebuffer otherwise if you are using 1.x.
You'll also notice that the blitted result is upside-down. You can fix that by using a negative height (and maybe setting the y start to screenHeight/4...i can't remember if that's needed ATM).

Offline kiffa

  • long
  • ***
  • Posts: 199
    • View Profile
Re: How to use renderTarget?
« Reply #2 on: December 05, 2013, 01:49:53 pm »
> You'll also notice that the blitted result is upside-down. You can fix that by using a negative height (and maybe setting the y start to screenHeight/4...i can't remember if that's needed ATM).

After some try, i am not very clear how to do(to fix the upside-down). Could you give me some sample code?



Offline IZACIZAC

  • byte
  • *
  • Posts: 19
    • View Profile
Re: How to use renderTarget?
« Reply #3 on: December 05, 2013, 07:26:31 pm »
frameBuffer.blit(mTexture, 0, mTexture.getHeight(), 0, 0, mTexture.getWidth(), -mTexture.getHeight(), Display.getWidth()/4, Display.getHeight()/4, -1, false);

This will solve that problem. Also I changed to the blit method that instead scales the image you are blitting, because the previous version was cutting out some of what is shown on the screen (for me at least)

Offline kiffa

  • long
  • ***
  • Posts: 199
    • View Profile
Re: How to use renderTarget?
« Reply #4 on: December 06, 2013, 04:29:10 am »
Thanks, i have tried this before, but the result is not what i expect, which seems i got an incomplete renderTargetTexture. There is another question:

Assume the size of display is 2X2, and can be spilt to 4 parts:
12
34

If i use a 1X1 (npot)texture as the renderTarget, which one is the correct result:
a, 1

b, 3

c, 4

d, a scaled
12
34

Could i render the full scene into a smaller texture? The following code seems useless:
Code: [Select]
Texture t = new NPOTTexture(w, h);
frameBuffer.resize(w, h);
renderToTarget();
frameBuffer.resize(oldW, oldH);
framBuffer.blit(t);
« Last Edit: December 06, 2013, 04:58:18 am by kiffa »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: How to use renderTarget?
« Reply #5 on: December 06, 2013, 07:22:45 am »
d) is the correct result. And your problem is....? A screen shot might help.

Offline IZACIZAC

  • byte
  • *
  • Posts: 19
    • View Profile
Re: How to use renderTarget?
« Reply #6 on: December 06, 2013, 07:00:05 pm »
I tested it and think I had the same issue. The texture that is blitted into the corner is not the same as the other full screen rendering, it is a slightly smaller field of view, missing some from the top and bottom.

Doing    buffer.setVirtualDimensions(800, 480);  //your screen width and height
seems to help solve this, though its still a tiny bit out

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11746
    • View Profile
    • http://www.jpct.net
Re: How to use renderTarget?
« Reply #7 on: December 06, 2013, 07:20:32 pm »
jPCT calculates the fov in a way that the horizontal angle is constant and the vertical angle will adjuated to keep a 1-1 ratio in all resolutions. As mentioned you can override this by setting the virtual dimensions. Another option would be to set fov values all by yourself.