--[[
	MOD/addons/SenHack v1/lua/client/libs/glib/render.lua [#6101 (#6101), 3337855438, UID:985068360]
	(ARP)konrad50024 | STEAM_0:0:36097632 <92.234.95.154:27005> | [11.05.14 04:00:14PM]
	===BadFile===
]]

/* * * * * * * * * * * * * * * * 
 * 	 ______     __  __     ______     __  __     ______     ______     __  __    
 *	/\  __ \   /\ \/\ \   /\  == \   /\ \_\ \   /\  __ \   /\  ___\   /\ \/ /    
 *	\ \ \/\ \  \ \ \_\ \  \ \  __<   \ \  __ \  \ \  __ \  \ \ \____  \ \  _"-.  
 *	 \ \_____\  \ \_____\  \ \_____\  \ \_\ \_\  \ \_\ \_\  \ \_____\  \ \_\ \_\ 
 *	  \/_____/   \/_____/   \/_____/   \/_/\/_/   \/_/\/_/   \/_____/   \/_/\/_/ 
 *
 *  - Version : 3L
 *  - By Oubliette
 * * * * * * * * * * * * * * * */

--
local arg = { ... };
local g = arg[ 1 ]
--

local render = g.table.Copy( render or {} );
local cam = g.table.Copy( cam or {} );

local C_Material = g.Material

function g.Material( name, words )

	if ( !words ) then return C_Material( name ) end
	
	local str = "";
	
	if ( words:find( "vertexlitgeneric" ) ) then str = str .. "1" 
	elseif ( words:find( "unlitgeneric" ) ) then str = str .. "0" 
	else str = str .. "0" end
	
	if ( words:find( "nocull" ) ) then str = str .. "1" 
	else str = str .. "0" end
	
	if ( words:find( "alphatest" ) ) then str = str .. "1" 
	else str = str .. "0" end
	
	if ( words:find( "mips" ) ) then str = str .. "1" 
	else str = str .. "0" end
	
	if ( words:find( "noclamp" ) ) then str = str .. "1" 
	else str = str .. "0" end
	
	if ( words:find( "smooth" ) ) then str = str .. "1" 
	else str = str .. "0" end
	
	return C_Material( name, str )

end

if ( !render ) then return end

--[[---------------------------------------------------------
  Short aliases for stencil constants
-----------------------------------------------------------]]  

g.STENCIL_NEVER = g.STENCILCOMPARISONFUNCTION_NEVER
g.STENCIL_LESS = g.STENCILCOMPARISONFUNCTION_LESS
g.STENCIL_EQUAL = g.STENCILCOMPARISONFUNCTION_EQUAL
g.STENCIL_LESSEQUAL = g.STENCILCOMPARISONFUNCTION_LESSEQUAL
g.STENCIL_GREATER = g.STENCILCOMPARISONFUNCTION_GREATER
g.STENCIL_NOTEQUAL = g.STENCILCOMPARISONFUNCTION_NOTEQUAL
g.STENCIL_GREATEREQUAL = g.STENCILCOMPARISONFUNCTION_GREATEREQUAL
g.STENCIL_ALWAYS = g.STENCILCOMPARISONFUNCTION_ALWAYS

g.STENCIL_KEEP = g.STENCILOPERATION_KEEP
g.STENCIL_ZERO = g.STENCILOPERATION_ZERO
g.STENCIL_REPLACE = g.STENCILOPERATION_REPLACE
g.STENCIL_INCRSAT = g.STENCILOPERATION_INCRSAT
g.STENCIL_DECRSAT = g.STENCILOPERATION_DECRSAT
g.STENCIL_INVERT = g.STENCILOPERATION_INVERT
g.STENCIL_INCR = g.STENCILOPERATION_INCR
g.STENCIL_DECR = g.STENCILOPERATION_DECR

--[[---------------------------------------------------------
   Name:	ClearRenderTarget
   Params: 	<texture> <color>
   Desc:	Clear a render target
-----------------------------------------------------------]]   
function render.ClearRenderTarget( rt, color )

	local OldRT = render.GetRenderTarget();
		render.SetRenderTarget( rt )
		render.Clear( color.r, color.g, color.b, color.a )
	render.SetRenderTarget( OldRT )

end


--[[---------------------------------------------------------
   Name:	SupportsHDR
   Params: 	
   Desc:	Return true if the client supports HDR
-----------------------------------------------------------]]   
function render.SupportsHDR( )

	if ( render.GetDXLevel() < 80 ) then return false end

	return true
	
end


--[[---------------------------------------------------------
   Name:	CopyTexture
   Params: 	<texture from> <texture to>
   Desc:	Copy the contents of one texture to another
-----------------------------------------------------------]]   
function render.CopyTexture( from, to )

	local OldRT = render.GetRenderTarget();
		
		render.SetRenderTarget( from )
		render.CopyRenderTargetToTexture( to )
		
	render.SetRenderTarget( OldRT )

end

local matColor = g.Material( "color" )

function render.SetColorMaterial()
	render.SetMaterial( matColor )
end

local matColorIgnoreZ = g.Material( "color_ignorez" )

function render.SetColorMaterialIgnoreZ()
	render.SetMaterial( matColorIgnoreZ )
end

local mat_BlurX			= g.Material( "pp/blurx" )
local mat_BlurY			= g.Material( "pp/blury" )
local tex_Bloom1		= render.GetBloomTex1()

function render.BlurRenderTarget( rt, sizex, sizey, passes )

	mat_BlurX:SetTexture( "$basetexture", rt )
	mat_BlurY:SetTexture( "$basetexture", tex_Bloom1  )
	mat_BlurX:SetFloat( "$size", sizex )
	mat_BlurY:SetFloat( "$size", sizey )
	
	for i=1, passes+1 do

		render.SetRenderTarget( tex_Bloom1 )
		render.SetMaterial( mat_BlurX )
		render.DrawScreenQuad()

		render.SetRenderTarget( rt )
		render.SetMaterial( mat_BlurY )
		render.DrawScreenQuad()

	end

end

function cam.Start2D()

	return cam.Start( { type = '2D' } )

end

function cam.Start3D( pos, ang, fov, x, y, w, h, znear, zfar )

	local tab = {}

	tab.type = '3D';
	tab.origin = pos
	tab.angles = ang

	if ( fov != nil ) then tab.fov = fov end

	if ( x != nil && y != nil && w != nil && h != nil ) then

		tab.x			= x
		tab.y			= y
		tab.w			= w
		tab.h			= h
		tab.aspect		= (w / h)

	end

	if ( znear != nil && zfar != nil ) then

		tab.znear	= znear
		tab.zfar	= zfar

	end

	return cam.Start( tab )

end

local matFSB			= g.Material( "pp/motionblur" )

function render.DrawTextureToScreen( tex )

	matFSB:SetFloat( "$alpha", 1.0 )
	matFSB:SetTexture( "$basetexture", tex )

	render.SetMaterial( matFSB )
	render.DrawScreenQuad()

end

function render.DrawTextureToScreenRect( tex, x, y, w, h )

	matFSB:SetFloat( "$alpha", 1.0 )
	matFSB:SetTexture( "$basetexture", tex )

	render.SetMaterial( matFSB )
	render.DrawScreenQuadEx( x, y, w, h )

end


--
-- This isn't very fast. If you're doing something every frame you should find a way to 
-- cache a ClientsideModel and keep it around! This is fine for rendering to a render 
-- target once - or something.
--

function render.Model( tbl, ent )

	local inent = ent

	if ( ent == nil ) then
		ent = g.ClientsideModel( tbl.model or "error.mdl", RENDERGROUP_OTHER )
	end
	
	if ( !g.IsValid( ent ) ) then return end

	ent:SetModel( tbl.model or "error.mdl" )
	ent:SetNoDraw( true )

	ent:SetPos( tbl.pos or Vector( 0, 0, 0 ) )
	ent:SetAngles( tbl.angle or Angle( 0, 0, 0 ) )
	ent:DrawModel()

	--
	-- If we created the model, then remove it!
	--
	if ( inent != ent ) then
		ent:Remove()
	end

end

return render, cam;