How do I correctly render to a different sized render target?

I am currently learning about shaders and rendering stuff, I thought it would be good to know how to render to a target that is a different size than the actual screen. But I am stuck so here I am.

I have a render target that is rendered onto a quad as a texture, the quad is rendered infront of a sprite that look like the picture bellow. It is rendered with the current parameters. Set in the init()

local color_params = { format = render.FORMAT_RGBA,
                   width = render.get_window_width(),
                   height = render.get_window_height() } 
local target_params = {[render.BUFFER_COLOR_BIT] = color_params }

self.target = render.render_target("effects", target_params)

with the viewport, view and projection in update()

render.set_viewport(0, 0, render.get_window_width(), render.get_window_height()) 
render.set_view(vmath.matrix4())
render.set_projection(vmath.matrix4_orthographic(0, render.get_window_width(), 0, render.get_window_height(), -1, 1))

This gives me the following result. It looks as expected.

I then change the parameters of the color parameters in init()

local color_params = { format = render.FORMAT_RGBA,
                   width = render.get_window_width() * 0.5,
                   height = render.get_window_height() * 0.5 } 

This should give me a render target of half the size, is this a correct assumption? It now looks like this.

I assume that this is because the render tagets size now only fills the bottom left quarter of the projection?

Halving the viewport will not work because that controls which parts of the screen is rendered, halving it makes it look like this

// With the halved color_params above
render.set_viewport(0, 0, render.get_window_width() * 0.5, render.get_window_height() * 0.5) 

Changing the projection only changes how much of the quad is seen by the camera.

It almost feels like the UVs (does a render target have UVs? It should right?) of the render target is also halved, but I want to keep it at the screen size. Is this thinking correct? Any points on how to do this would be awesome.

Doing render.set_viewport with half width/height, just like you are doing, should do the trick. Remember to restore the viewport to the original dimensions if you are rendering stuff at full resolution afterwards.

If you like you can invite me to the project and I can have a look (johan.beck-noren[at]king.com).

1 Like

Well it doesn’t! Would be very thankful if you could take a quick look :slight_smile: Invited you

The half viewport needs to be set before drawing to the half-size render target, and the restored back to full dimensions when rendering using the render target as a texture.

...
render.set_viewport(0, 0, render.get_width()*0.5, render.get_height()*0.5)
render.enable_render_target(self.target)
...

and later

...
render.set_viewport(0, 0, render.get_width(), render.get_height())
...
render.enable_texture(0, self.target, render.BUFFER_COLOR_BIT) 
...

This results in the half-res render target being rendered across the full viewport.

5 Likes

Of course that makes a lot of sense!

I need to resize the original viewport that is rendering everything that I want to draw to the render target. Now I was basically trying to resize something that had already been drawn to a texture!

Thanks for checking it out

3 Likes