RenderTarget
A framebuffer you draw into instead of the window, and then sample. Render-to-texture, which is the door to most of what comes after simple forward rendering: a post-process pass, a shadow map, a reflection, a mirror, a minimap. ## The viewport goes with the target Bind sets the viewport to the target's size and Unbind restores the window's, because getting that wrong is the classic render-to-texture bug: a 512x512 target still rendering through a 1280x720 viewport draws a quarter of the scene into one corner, with no GL error anywhere. Unbind needs the window in order to know what to restore to -- which is why it takes one. ## Colour is a texture, depth is not Colour goes to a texture because sampling the result is the entire point. Depth goes to a renderbuffer, which is the cheaper option for a buffer that is written and never read. A shadow map wants the reverse, and will need its own constructor for that.
Example
target := RenderTarget->New(512, 512);
target->Bind(); # draw into the texture
GL->Clear(ClearBit->GL_COLOR_BUFFER_BIT or ClearBit->GL_DEPTH_BUFFER_BIT);
scene->Draw(view_projection);
target->Unbind(window); # back to the window
post->Use();
post->SetInt("tex", 0);
target->GetTexture()->Bind(0);
quad->Draw();Operations
BindAsCube #
Bind a cube target's texture for sampling. Separate from Texture2D->Bind because the target enum differs: a cube map bound as GL_TEXTURE_2D leaves the sampler reading nothing at all, with no error.
method : public : BindAsCube(unit:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| unit | Int | the texture unit |
BindFace #
Draw into ONE face of a cube target, and set the viewport to match.
method : public : BindFace(face:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| face | Int | 0..5, in GL's order: +X, -X, +Y, -Y, +Z, -Z |
Depth # function
A DEPTH-ONLY target: no colour buffer at all, and the depth is a texture that can be sampled. The reverse of the usual arrangement, and what a shadow map needs -- depth read, colour never. ShadowMap wraps this along with everything else the technique requires; reach for it directly only for something ShadowMap does not cover.
function : Depth(size:Int) ~ RenderTargetParameters
| Name | Type | Description |
|---|---|---|
| size | Int | the map's width and height; powers of two are conventional |
Return
| Type | Description |
|---|---|
| RenderTarget | the target; check IsOk() |
DepthCube # function
A CUBE depth target: six square depth faces under one texture name. What a point light needs. A point light shines in every direction, so no single flat map can hold its shadows -- the scene is rendered six times, once down each axis, and sampled by DIRECTION rather than by a projected coordinate. PointShadow wraps all of that; reach for this directly only for something PointShadow does not cover.
function : DepthCube(size:Int) ~ RenderTargetParameters
| Name | Type | Description |
|---|---|---|
| size | Int | the width and height of each face |
Return
| Type | Description |
|---|---|
| RenderTarget | the target; check IsOk() |
Free #
Release the framebuffer, its colour texture and its depth buffer. Call it while the GL context is still current -- see the shutdown order on the GL class.
method : public : Free() ~ NilGetAspect #
method : public : GetAspect() ~ FloatReturn
| Type | Description |
|---|---|
| Float | width over height, for a projection matched to this target |
GetError #
Why creation failed -- an incomplete framebuffer, a size the driver refuses, or the entry points not being loaded.
method : public : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | the reason, or an empty string |
GetTexture #
What was drawn, as a texture to bind and sample. The same object every call, and it does NOT own the underlying texture -- this target does. Freeing the returned Texture2D would delete a texture the framebuffer is still attached to, so do not; free the target instead.
method : public : GetTexture() ~ Texture2DReturn
| Type | Description |
|---|---|
| Texture2D | the colour texture, or Nil when this target is not ok |
IsCube #
method : public : IsCube() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true when this target is a cube map rather than a flat one |
IsOk #
method : public : IsOk() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true when the framebuffer was created AND reported complete |
New # constructor
A target of the given size, smoothly sampled.
New(width:Int, height:Int)Parameters
| Name | Type | Description |
|---|---|---|
| width | Int | in pixels |
| height | Int | in pixels |
New # constructor
A target with an explicit sampling filter. Use GL_NEAREST when the result is read exactly rather than scaled -- a deliberately low-resolution pass, or data rather than a picture.
New(width:Int, height:Int, filter:Int)Parameters
| Name | Type | Description |
|---|---|---|
| width | Int | in pixels |
| height | Int | in pixels |
| filter | Int | a TextureFilter |