render/GetRenderTarget

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Regarding the edit before me, I'm not sure whether it's good practice to link to the same article we're in; it doesn't even work and is just bold and weird, but I'm leaving it in for now)
(Do not promote the crappy incorrect way of doing render targets)
 
Line 5: Line 5:
 
|Realm=Client
 
|Realm=Client
 
|IsClass=No
 
|IsClass=No
|Name=GetRenderTarget
 
|Parent=render
 
 
}}
 
}}
 
{{Ret
 
{{Ret
Line 14: Line 12:
 
{{Example
 
{{Example
 
|Description=Render something to a different render target, then restore the old render target
 
|Description=Render something to a different render target, then restore the old render target
|Code=local oldRt = render.GetRenderTarget() -- we'll save the old screen and draw on a new one!
 
local w, h = ScrW(), ScrH()
 
local customRt = GetRenderTarget( "some_unique_render_target_nameeeee", w, h, true )
 
 
render.SetRenderTarget( customRt )
 
render.Clear( 0, 0, 255, 255, true ) -- fill the background with blue!
 
 
-- draw all props on the blue background!
 
for key, prop in pairs(ents.FindByClass( "prop_physics" )) do
 
    prop:DrawModel()
 
end
 
 
-- save the picture to the garrysmod/data folder.  ~format="jpg" will not work.
 
local data = render.Capture({ format = "jpeg", quality = 70, x = 0, y = 0, h = h, w = w })
 
local pictureFile = file.Open( "RenderTargetsAreAwesome.jpg", "wb", "DATA" )
 
pictureFile:Write( data )
 
pictureFile:Close()
 
 
render.SetRenderTarget( oldRt )
 
}}
 
{{Example
 
|Description=Same as above, but using {{LibraryFunction|render|PushRenderTarget}} and {{LibraryFunction|render|PopRenderTarget}} instead of {{LibraryFunction|render|GetRenderTarget}} and {{LibraryFunction|render|SetRenderTarget}}
 
 
|Code=local w, h = ScrW(), ScrH()
 
|Code=local w, h = ScrW(), ScrH()
 
local customRt = GetRenderTarget( "some_unique_render_target_nameeeee", w, h, true )
 
local customRt = GetRenderTarget( "some_unique_render_target_nameeeee", w, h, true )

Latest revision as of 16:02, 17 October 2019

 render.GetRenderTarget( )

Contents

Description

Returns the currently active render target.

Instead of saving the current render target using this function and restoring to it later, it is generally better practice to use render.PushRenderTarget and render.PopRenderTarget.

Returns

ITexture

The currently active Render Target

Examples

Example

Render something to a different render target, then restore the old render target

local w, h = ScrW(), ScrH()
local customRt = GetRenderTarget( "some_unique_render_target_nameeeee", w, h, true )

render.PushRenderTarget( customRt )
    render.Clear( 0, 0, 255, 255, true ) -- fill the background with blue!

    -- draw all props on the blue background!
    for key, prop in pairs(ents.FindByClass( "prop_physics" )) do
        prop:DrawModel()
    end

    -- save the picture to the garrysmod/data folder.  ~format="jpg" will not work.
    local data = render.Capture({ format = "jpeg", quality = 70, x = 0, y = 0, h = h, w = w })	
    local pictureFile = file.Open( "RenderTargetsAreAwesome.jpg", "wb", "DATA" )	
    pictureFile:Write( data )
    pictureFile:Close()
render.PopRenderTarget()


Personal tools
Navigation