[Solved]How would one render the screen to just a portion of a rendertarget ?

I want to render the screen to just a portion of a render target.
The idea is to have the camera face one direction render everything scaled down to a portion of the rendertarget say the top left sixth of it.
Turn the camera 90 degrees in another direction with the same render target
Render to the top middle of it.

Id like to do this for all six sides of a cube, as im about to try to make a shadow cube map and im coding the cube atm…
I did the regular shadow map before on a single plane but its not so great i’ve never tried to make a shadow cube yet.

is that not possible
or a bad idea

Really i have no idea how to do a shadow cube map skybox’s are pretty simple but the shadow cube to me seems fairly confusing especially the part of figuring out were on which texture a pixel equates to when actually drawing the shadows.
I keep imagining about 6 if’s in a shader to boot im sort of thinking that isn’t the way its done ? Or is it ?

use Viewport

nviewport = new Viewport(Rectangle(desired area on rendertarget));
GraphicsDevice.Viewport = nviewport;

Oh nice is there any problems that can arise if you just use a single rendertarget for shadowmaping ? In relation to u,v coordinates i never did a shadow cube.

I would think it would be a bit cheaper to just use one render target.
Or is this just more confusing when your trying to figure out which part to get the depth from.

You are probably better off just using a RenderTargetCube instead.

I don’t think so. You won’t save any memory and it will probably be slower, because figuring out the correct uv coordinates makes your shader more complicated. With cubemaps you can just pass a direction into the texture lookup.

Do you mean its something pre-built in ?
Is there a example for its usage ?

Yes, GPU’s have cubemap support. In MonoGame you create a TextureCube for static cubemaps, or a RenderTargetCube for dynamic ones.

The basic steps are:

  • create a camera with 90 degrees field of view horizontally and vertically.
  • point the camera in the positive x direction.
  • set the cubemap render target to the graphics device using CubeMapFace.PositiveX.
  • render your scene (shadow casting objects)
  • repeat for all other faces

You have to do it this way for mobile (switching targets is too expensive to do more than strictly necessary) and any of the tiled/cluster lighting lighting methods anyways.

Cubemap sampling is pretty simple and the method is documented: old nVidia article on it, math and tables towards the bottom, the old GL extension documents for cube-textures also cover the same.

It’s pretty branchy though, even with most of it just being tables.

An alternative to doing the branchy coordinate conversion in the shader is to prebuild two mip-less cubemaps, one for face selection and another for indirection (bitwise operators can make it one RGBA16 but that chops away alot of hardware). The face selection contains the multiplier to collapse the sampling vector on the axis, and the indirection is the coordinate remapping. A bit more branchiness can ditch the indirection cubemap (packing an index into the face-selection Alpha). altDevBlog article

Shipped that approach, worked fine even on toasters.

Im going to try.to get this working tonight or tommorow.
Wish their was a monogame example. Google really doesnt bring up anything.

So are you going with the single-texture or the cubemap approach? With a cubemap the lookup in the shader should be as simple as this:

float3 dir = pixelPos - lightPos;
float shadowDepth = texCUBE(shadowSampler, float4(dir, 0)).x;

Im going to try to get a cube shadow map working for sure.

I did a single texture shadowmap before though i never did get it to align perfectly to the scene.
It looked pretty good as far as i got it though.
I stoped when i realized i would need a cubemap, to do things properly and put it all on the backburner. I sort of need it now as its a missing piece that i need to tackle.

I do this for my cubemap shadows in the deferred engine

Find the code here

Im going to post to a new topic with a appropriate title i have a feeling this will take some time to get it right. [Solved]Render Target Cube Texture Cube How to questions.

Kosmonaut that is impressive. Though im was just shooting for a barebones example.

Well i got a initial test up and running i won’t know how well its working till i continue it im tired. It appears to be shadowing, All though its a piss poor testing scene i tossed up a pic.

This turned out to be a crucial peice of the puzzle would not of known how to do this…

float3 dir = pixelPos - lightPos;
float shadowDepth = texCUBE(shadowSampler, float4(dir, 0)).x;