RT texture Scope help

Hello, Today i’ve been working on creating a RT texture scope so that i could make the scopes on CS:S models actually useful, and not force me to use a crappy 2d scope. This turned out to be much harder than i’d think.
I’d like to let you know before you tell me, The Customize-able Weapons pack doesn’t have the answer i need. The coder of that adds on hollow aimpoint/acog models and then uses the model itself as a stencil, and draws a 2D render target screen behind it. Won’t work for CS:S weapons because, well, they aren’t hollow. My solution calls for a circular texture to be drawn over the scope texture.
So far, i’ve gotten this, thanks to a bunch of snippets i found:
[lua]
if CLIENT then
function SWEP:ViewModelDrawn()
self.AcogGlass = Material( “models/wystan/attachments/acog/lense” )
self.AcogRT = GetRenderTarget( “acog_glass_rt”, 512, 512, false )
self.AcogGlass:SetTexture( “$basetexture”, self.AcogRT )

	local vm = self.Owner:GetViewModel() 
	local bone = vm:LookupBone("v_weapon.sg552_Parent")
	if !bone then print("bone doesnt exist") end
	local pos, ang = Vector(0,0,0), Angle(0,0,0)
		local m = vm:GetBoneMatrix(bone)
		if (m) then
			pos, ang = m:GetTranslation(), m:GetAngles()
		end
	pos = pos + Vector(0,0,0)
	ang = ang + Angle(0,0,0)
	print(ang)
	local Radar = {}
	Radar.circle= {}
	Radar.res = 52
	Radar.radarRadius = 100;
	for i=1,Radar.res do
		Radar.circle* = {}
		Radar.circle*.x = pos.x + math.cos(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99
		Radar.circle*.y = pos.y + math.sin(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99
		Radar.circle*.u = (math.cos(math.rad(  (i*360)/Radar.res )) + 1)*0.5 
		Radar.circle*.v = -(math.sin(math.rad( (i*360)/Radar.res )) + 1)*0.5 
	end
	cam.Start3D(pos, ang , 4, pos.x, pos.y, 512,512)
		surface.SetMaterial(self.AcogGlass)
		surface.SetDrawColor(255, 255, 255, 255)
		surface.DrawPoly(Radar.circle)
		
	cam.End3D()
	render.SetViewPort( pos.x, pos.y, 20, 20 )
		render.SetRenderTarget( old )
					

end

end
[/lua]

I have the math to make a circle, and a pitiful attempt at adding it to the model itself.

Could anybody guide me in the right direction on how i can attatch a poly to a bone on a viewmodel and position it right?

Thanks in advance to anybody that takes the time to respond. i’ve been up literally the past 8+ hours trying to solve this specific problem.

bump.
Honestly nobody has a clue?

I too would like to know the math behind doing this properly. Can you link to the specific version of the ‘customizable weapons pack’ you were analyzing?

Just got the old version for the RT scope code. it does it way differently than i thought it would.

Another bump. I need to figure this out so i can finish a gamemode.

[editline]22nd December 2012[/editline]

http://puu.sh/1DKYE

[lua]
if CLIENT then
function SWEP:ViewModelDrawn()
self.AcogGlass = Material( “models/wystan/attachments/acog/lense” )
self.AcogRT = GetRenderTarget( “acog_glass_rt”, 512, 512, false )
self.AcogGlass:SetTexture( “$basetexture”, self.AcogRT )

            local vm = self.Owner:GetViewModel()
            local bone = vm:LookupBone("v_weapon.sg552_Parent")
            if !bone then print("bone doesnt exist") end
            local pos, ang = Vector(0,0,0), Angle(0,0,0)
                    local m = vm:GetBoneMatrix(bone)
                    if (m) then
                            pos, ang = m:GetTranslation(), m:GetAngles()
                    end
            pos = pos + Vector(0,0,90)
            ang = ang + Angle(0,0,0)
            --print(ang)
            local Radar = {}
            Radar.circle= {}
            Radar.res = 52
            Radar.radarRadius = 100;
            for i=1,Radar.res do
                    Radar.circle* = {}
                    Radar.circle*.x = pos.x + math.cos(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99
                    Radar.circle*.y = pos.y + math.sin(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99
                    Radar.circle*.u = (math.cos(math.rad(  (i*360)/Radar.res )) + 1)*0.5
                    Radar.circle*.v = -(math.sin(math.rad( (i*360)/Radar.res )) + 1)*0.5
            end
            cam.Start3D(pos, ang , 4, pos.x, pos.y, 512,512)
                    surface.SetMaterial(self.AcogGlass)
                    surface.SetDrawColor(255, 255, 255, 255)
                    surface.DrawPoly(Radar.circle)
                   
            cam.End3D()
            render.SetViewPort( pos.x, pos.y, 20, 20 )
                    render.SetRenderTarget( old )
                   
            render.SetMaterial(self.AcogGlass)
            render.DrawQuadEasy(EyePos() + EyeAngles():Forward() * 64, EyeAngles():Forward() * -1, 128, 128, color_white, 0)

           
                                           

    end

end
[/lua]

Code and result. i think i got it binding to the VM fine, i just need to correct how the positions work.

[editline]22nd December 2012[/editline]

Here’s an update. This set of code seems to return values that lock to the ViewModel, but i am still unable to get it to draw anything on the vm itself.
[lua]

if CLIENT then
function SWEP:ViewModelDrawn()
self.AcogGlass = Material( “models/wystan/attachments/acog/lense” )
self.AcogRT = GetRenderTarget( “acog_glass_rt”, 512, 512, false )
self.AcogGlass:SetTexture( “$basetexture”, self.AcogRT )

	local vm = self.Owner:GetViewModel() 
	local attachment = vm:LookupAttachment("2") /*or vm:LookupAttachment( "1" )*/
	if attachment == 0 then print ("Attachment Doesnt Exist") return end
	local angpos = vm:GetAttachment( attachment )
	if not angpos then return end
	local p = ( angpos.Pos /*+ vm:GetAngles():Right()  - 6 **/):ToScreen()
	local pos = Vector(p.x, p.y, p.z)
	local a = ( angpos.Ang /*+ vm:GetAngles():Right()  - 6 **/)
	local ang = Angle(a.p, a.y, a.r)



	/*if !bone then print("bone doesnt exist") end
	local pos, ang = Vector(0,0,0), Angle(0,0,0)
		local m = vm:GetBoneMatrix(bone)
		if (m) then
			pos, ang = m:GetTranslation(), m:GetAngles()
		end*/
	pos = pos + Vector(0,0,0)
	ang = ang + Angle(0,0,0)
	--print(pos)
	--print(ang)
	--print(y)
	local Radar = {}
	Radar.circle= {}
	Radar.res = 52
	Radar.radarRadius = 100;
	for i=1,Radar.res do
		Radar.circle* = {}
		Radar.circle*.x = pos.x + math.cos(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99
		Radar.circle*.y = pos.y + math.sin(math.rad( (i*360)/Radar.res ))*Radar.radarRadius*0.99
		Radar.circle*.u = (math.cos(math.rad(  (i*360)/Radar.res )) + 1)*0.5 
		Radar.circle*.v = -(math.sin(math.rad( (i*360)/Radar.res )) + 1)*0.5 
	end
	cam.Start3D(pos, ang, 4, pos.x, pos.y, 512,512)
		surface.SetMaterial(self.AcogGlass)
		surface.SetDrawColor(255, 255, 255, 255)
		surface.DrawPoly(Radar.circle)	
	cam.End3D()
		render.SetViewPort(pos.x, pos.y, 200, 200 )
		render.SetRenderTarget( old )
		
	render.SetMaterial(self.AcogGlass)
	render.DrawQuadEasy(pos, pos, 128, 128, color_white, 0)

	
					

end

end
[/lua]

Maybe this will help.

[lua]local RES = 0.01
local WIDTH = 6/RES
local HEIGHT = 0.5/RES
local smooth = 0

function SWEP:ViewModelDrawn( vm )

local bone = vm:LookupBone( "v_weapon.ump45_Parent" )
if not bone then return end

local m = vm:GetBoneMatrix( bone )
if not m then return end

local pos, ang = m:GetTranslation(), m:GetAngles()

if self.ViewModelFlip then
	ang.r = -ang.r -- Fix
end

pos = pos + ang:Right() * 5.3
pos = pos + ang:Forward() * -0.25
pos = pos + ang:Up() * 5.25

ang:RotateAroundAxis( ang:Right(), -90 )

local offset = 0

if IsValid( self ) then
	offset = self:EntIndex() * 50
end

local bulletColor = HSVToColor( ( offset + CurTime() * 30 ) % 360, 0.75, 1 )

cam.Start3D2D( pos, ang, RES )
	surface.SetDrawColor( 255, 0, 0, 255 )
	surface.DrawRect( 0, 0, WIDTH, HEIGHT )
	
	surface.SetDrawColor( bulletColor )
	
	local percent = self:GetBattery() / self.Primary.ClipSize
	smooth = smooth + ( percent - smooth ) / ( 500 * RealFrameTime() )
    
	surface.DrawRect( 0, 0, WIDTH * smooth, HEIGHT )
	
	surface.SetDrawColor( 0, 0, 0, 255 )
	surface.DrawRect( WIDTH * smooth, 0, 0.1/RES, HEIGHT )
cam.End3D2D()

end[/lua]

The code that will draw a little battery meter for a gun I made for my servers.

Maybe it will help you figure out how to position your poly.