Facepunch / garrysmod-issues

Garry's Mod issue tracker
147 stars 56 forks source link

Drawing a textured rect to a rendertarget fails #992

Closed wyozi closed 10 years ago

wyozi commented 10 years ago

I'm trying to first draw to a rendertarget, and then draw the contents of that rendertarget to another rendertarget (https://gist.github.com/10576674), but just trying to draw a simple texture to a rendertarget fails as well.

robotboy655 commented 10 years ago

First of all, the render target MUST be po2 size, means, 128, 256, 512, etc.

And here's an example on how to use render targets: https://github.com/garrynewman/garrysmod/blob/master/garrysmod/gamemodes/sandbox/entities/weapons/gmod_tool/cl_viewscreen.lua#L48

wyozi commented 10 years ago

I have gotten the rendertarget to work, but the problem is I'm not able to draw a rendertarget texture to another rendertarget. If you take my code and change the part where the rendertarget is drawn to screen to use the rt1 texture, you'll see that it works fine. The problem is in drawing a rendertarget to another one, which I suspect might be a bug.

I doubt changing the size to po2 will fix it.

robotboy655 commented 10 years ago

I did some tests, and it seems this happens because you are trying to use a single texture for two different things. Using different texture when rendering second render targets works.

wyozi commented 10 years ago

Could you give me the code you got to work? I tried something like that earlier but couldn't get it to work.

robotboy655 commented 10 years ago
local rt1 = GetRenderTarget("TestRT1", 500, 500)
local rt2 = GetRenderTarget("TestRT2", 500, 500)

local rt_mat = Material("models/weapons/v_toolgun/screen")
local rt_mat2 = Material("models/weapons/v_toolgun/screen_bg")

hook.Add("HUDPaint", "TestRTHook", function()
    render.PushRenderTarget(rt1)
        render.Clear(0, 0, 0, 255)

        cam.Start2D()
            surface.SetDrawColor(HSVToColor(CurTime()*50, 0.95, 0.5))

            local x, y = 250+math.cos(CurTime())*200, 250+math.sin(CurTime())*200
            surface.DrawRect(x-10, y-10, 20, 20)
        cam.End2D()
    render.PopRenderTarget()

    render.PushRenderTarget(rt2)
        render.Clear(0, 0, 0, 255)

        cam.Start2D()
            surface.SetDrawColor(255, 255, 255)
            rt_mat2:SetTexture("$basetexture", rt1)
            surface.SetMaterial(rt_mat2)
            surface.DrawTexturedRect(100, 100, 300, 300)
        cam.End2D()
    render.PopRenderTarget()

    rt_mat:SetTexture("$basetexture", rt2)
    surface.SetMaterial(rt_mat)
    surface.DrawTexturedRect(100, 100, 500, 500)
end)
robotboy655 commented 10 years ago

Uh, apparently your code now works out of the box for me.. I have no idea.

EDIT: nevermind, I had old render targets.

wyozi commented 10 years ago

Strange, seems like if you draw a rendertarget texture to two different places, you need a separate material to do it. As soon as I changed the second place's material to have the _bg postfix, it started working.

I'm not sure if this should be classified as a bug or not.

robotboy655 commented 10 years ago

Me neither.

It's probably just how Source Engine works.

xaviergmail commented 6 years ago

In case anyone else stumbles upon this from the googles after trying to draw a texture that has some non-255 alpha pixels, generate your RT using this: GetRenderTargetEx("RT_NAME", <RT_WIDTH>, <RT_HEIGHT>, RT_SIZE_OFFSCREEN, MATERIAL_RT_DEPTH_NONE, 0, 0, IMAGE_FORMAT_RGBA8888)