Team Spartan Cookies & Milk Forums
rendertarget issue when drawing text before polys / lines - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: rendertarget issue when drawing text before polys / lines (/thread-3658.html)



rendertarget issue when drawing text before polys / lines - The17thDoctor - 11-06-2021

This one was pretty hard to nail down quite precisely but I found out that when creating fonts and then drawing first a text then polygons, the polygons in a rendertarget, they would not render. 
I also noticed artifacts on lines. When trying to create a line with extreme width, I found out that the line was actually textured like text.

Here is a piece of code illustrating what I found out :

--@name RenderTarget issue
--@author
--@highres

local font = render.createFont("Arial", 27) --Change the size
local rt = rendertarget.create(true, 1024, 1024)

function render.drawRegPoly(x, y, r, s, a)
    local polyTable = {}
    local delta_angle = 360 / s

    a = a or 0
    s = math.floor(s)

    for i = 1, s do
        local vertex_table = {}
        local vec = Vector(0, r):Rotate(Angle(0,(delta_angle*(i-0.5) + a)))
        vertex_table.x = x + vec.x
        vertex_table.y = y + vec.y
        polyTable[#polyTable+1] = vertex_table
    end

    render.drawPoly(polyTable)
end

hook("render", "Render RT", function()
    rt:drawToTexture(function()
        render.clear()
        render.setColor(255, 255, 255)
        render.drawText(font, 10, 10, "This is a sample text")
        for x = 0, 10 do
            for y = 5, 10 do
                render.drawRegPoly(x*102.4 - 51.2, y*102.4 - 51.2, 40, 5, 0)
            end
        end
        render.drawLine(20, 200, 1004, 200, 200)
    end)

    hook("render", "Render RT")
end)

hook("render", "Main Renderer", function()
    render.clear()
    rt:draw(0, 0, 1024, 1024)
end)


I also noticed that the post editor would not support TABs as indentation inside code.


RE: rendertarget issue when drawing text before polys / lines - Person8880 - 11-06-2021

You need to reset the texture before rendering a polygon. It's trying to use the last set texture which internally will be the texture used for the text rendering.
-- Reset the current texture to the default.
render.setTexture()
for x = 0, 10 do
    for y = 5, 10 do
        render.drawRegPoly(x*102.4 - 51.2, y*102.4 - 51.2, 40, 5, 0)
    end
end

Also as a side note, drawToTexture() doesn't need to be in a render hook. It's an asynchronous operation that renders separately (hence the function argument).