Jump to content

Trouble with Render Targets


thehankinator
 Share

Recommended Posts

I am trying to make a split screen effect.

 

For some reason, it appears that the it only renders once to the texture, never updates. Is this a known limitation? If I disable the render targets the FPSPlayer behaves normally so I think everything is in order there.

 

I've created 2 textures, black.tex and green.tex. Then, I assigned a texture to each camera. Inside Main.lua I've done the following:

 

Before the while loop:

local player1view = Texture:Load("Materials/RenderTargets/black.tex")
local player2view = Texture:Load("Materials/RenderTargets/green.tex")

 

 

After world:Render()

context:SetColor(1,1,1)
context:DrawImage(player1view, 0, 0, context:GetWidth(), context:GetHeight()/2)
context:DrawImage(player2view, 0, context:GetHeight()/2, context:GetWidth(), context:GetHeight()/2)

 

Edit: I'll also add I've tried setting one of the cameras to Hidden, no effect change in result.

Edit2: I've also tried creating a new texture pragmatically and rendering that, same result.

Link to comment
Share on other sites

I had a chance to come up with a minimal script that removes the need for you to download the whole project. This has only one render target but has the same symptoms. You can press T to toggle between the render to texture and a straight render to the screen.

 

Main.lua

--Initialize Steamworks (optional)
Steamworks:Initialize()
--Set the application title
title="MyGame"
--Create a window
local windowstyle = window.Titlebar
if System:GetProperty("fullscreen")=="1" then windowstyle=windowstyle+window.FullScreen end
window=Window:Create(title,0,0,System:GetProperty("screenwidth","1024"),System:GetProperty("screenheight","768"),windowstyle)
window:HideMouse()
--Create the graphics context
context=Context:Create(window,0)
if context==nil then return end
--Create a world
world=World:Create()
local render_target = Texture:Create(512, 512)

local dir_light = DirectionalLight:Create()
dir_light:SetPosition(0,4,0)

local model = Model:Box()
model:SetColor(0.0,1.0,0.0)
model:SetPosition(0,0,0)

local camera = Camera:Create()
camera:SetPosition(-1,0,-1)
camera:SetRotation(0,45,0)
camera:SetRenderTarget(render_target)

local rot = 0;
local use_render_target = true

while window:KeyDown(Key.Escape)==false do

 --If window has been closed, end the program
 if window:Closed() then break end

 --Update the app timing
 Time:Update()

 context:SetColor(0,0,0)
 context:Clear()

 model:SetRotation(rot, 0, 0)
 rot = rot + Time:GetSpeed()

 if window:KeyHit(Key.T) then
   use_render_target = not use_render_target

   if use_render_target then
     camera:SetRenderTarget(render_target)
   else
     camera:SetRenderTarget(nil)
   end
 end

 --Update the world
 world:Update()

 --Render the world
 world:Render()

 if use_render_target then
   context:SetColor(1,1,1)
   context:DrawImage(render_target, 0, 0, 1024, 768)
 end

 context:SetBlendMode(Blend.Alpha)
 context:DrawText("Press T to enable/disable render target", 300, 2)

 --Refresh the screen
 context:Sync(true)
end

 

Edit: Why does the CODE tag always murder code formatting?

Link to comment
Share on other sites

I'm pretty sure what's happening is because the texture isn't bound during a world render, the camera assumes it has been culled and is skipping rendering.

 

I need to think about how best to handle this.

 

You could also just use a buffer, which is undocumented / unofficial. Basically like this:

buffer = Buffer:Create()

--Loop:
buffer:Enable()
world:Render()
context:Enable()
tex = buffer:GetColorTexture()
context:DrawImage(tex)

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Here's an example for render targets:

--Create a window
window = Window:Create()
context = Context:Create(window)
world = World:Create()

camera = Camera:Create()
camera:SetRotation(35,0,0)
camera:Move(0,0,-6)

camera2 = Camera:Create()
camera2:Move(0,0,-4)
camera2:SetClearColor(1,0,0)
pivot = Pivot:Create()
camera2:SetParent(pivot)

local tex = Texture:Create(512,512)
local mtl = Material:Create()
mtl:SetShader("Shaders/Model/Diffuse.shader")
mtl:SetTexture(tex)
camera2:SetRenderTarget(tex)

local light = DirectionalLight:Create() 
light:SetRotation(35,35,0) 

local model = NULL 

model = Model:Box() 
model:SetPosition(-2,0,0) 
model:SetMaterial(mtl)

model = Model:Cylinder() 
model:SetColor(0.0,1.0,0.0) 
model:SetPosition(0,0,0) 

model = Model:Cone() 
model:SetColor(0.0,0.0,1.0) 
model:SetPosition(2,0,0) 

while true do

   pivot:Turn(0,0.5,0)

   if window:Closed() or window:KeyHit(Key.Escape) then return false end

   Time:Update()
   world:Update()
   world:Render()
   context:Sync()
end

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Buffer class looks much more simple, will it be going away? Will it ever become supported/documented?

I doubt it will go away since the post effect shaders wouldn't work without it. At this point, I don't understand why its not documented.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

 

I doubt it will go away since the post effect shaders wouldn't work without it. At this point, I don't understand why its not documented.

I think it's time. There's a very specific case where this is useful.
  • Upvote 2

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...