dofile(LockOn_Options.common_script_path.."elements_defs.lua")

SetScale(FOV)


render_tv					= CreateElement "ceTexPoly"
render_tv.name				= "screen"
ixmax  = 16
iymax  = 16
x_render_size = 1.0
y_render_size = 0.66
radius_X = 0.65/GetScale()
radius_Y = 0.65/GetScale()
amax   = 1.03 -- x_render_size -- 0.9 -- x_render_size  --math.asin(x_render_size/radius)
bmax   = 0.7 -- y_render_size -- 0.9 -- y_render_size --math.asin(y_render_size/radius)
kellips = 1
verts   = {}
indices = {} 
k = 0
local a = 0.59/GetScale()
local b = 0.5/GetScale()
local c = 0.65/GetScale()


function ellipsoid(x,y)

	local a2 = a*a
	local b2 = b*b
	local c2 = c*c
	local z2 = c2*x*x/a2 + c2*y*y/b2
	local z = 0
	if z2 > c2 then
		z = -math.sqrt(-c2 + z2)
	else
	    z =  math.sqrt(c2 - z2)
	 end
	return z
end

for j = 2,iymax do
	local y1 =  -bmax + (j-1 - 1)*(2*bmax)/(iymax-1)
	local y2 =  -bmax + (j-1    )*(2*bmax)/(iymax-1)
	for i = 2 ,ixmax do
		local x1 = -amax + (i-1 - 1)*(2*amax)/(ixmax-1)
		local x2 = -amax + (i-1    )*(2*amax)/(ixmax-1)
		
		local z0 = c - ellipsoid(x1,y1) 
		local z1 = c - ellipsoid(x2,y1)
		local z2 = c - ellipsoid(x2,y2) 
		local z3 = c - ellipsoid(x1,y2) 

		local n0  = (j-1 - 1)*ixmax + i - 1 - 1
		local n1  = (j-1 - 1)*ixmax + i - 0 - 1
		local n2  = (j-0 - 1)*ixmax + i - 1 - 1
		local n3  = (j-0 - 1)*ixmax + i - 0 - 1
		
		verts[n0 + 1]  = {x1,y1,z0}
		verts[n1 + 1]  = {x2,y1,z1}
		verts[n2 + 1]  = {x2,y2,z2}
		verts[n3 + 1]  = {x1,y2,z3}
		dbg_grid = false 
		indices[6*k + 1]  = n0
		indices[6*k + 2]  = n1
		indices[6*k + 3]  = n2
		if dbg_grid then
		indices[6*k + 4]  = 0
		indices[6*k + 5]  = 0
		indices[6*k + 6]  = 0
		else
		indices[6*k + 4]  = n2
		indices[6*k + 5]  = n1
		indices[6*k + 6]  = n3
		end
		k = k + 1
 	end
end

render_tv.vertices			= verts 
render_tv.indices			= indices 
render_tv.tex_params		= {0.5, 0.5,1/(2*amax),1/(2*bmax)}
render_tv.material			= "render_target_1"
render_tv.init_pos			= {0.0,-0.09, 0.051}
render_tv.h_clip_relation   = h_clip_relations.REWRITE_LEVEL
render_tv.level		  	    = DEFAULT_LEVEL + 1
render_tv.controllers		= {{"render_purpose",render_purpose.GENERAL},{"tv_control"}}
Add(render_tv)

frame                                       = CreateElement "ceMeshPoly"
frame.name                                  = "fr"
frame.primitivetype                         = "lines"
frame.material                              = "BLACK2"
frame.vertices                  			= {{-1.15, 0.78}, {1.15, 0.78}, {1.15, -0.78}, {-1.15, -0.78}}
frame.indices			                    = {0,1,1,2,2,3,3,0}
frame.controllers                           = {{"render_purpose",render_purpose.HUD_ONLY_VIEW,
															     render_purpose.SCREENSPACE_OUTSIDE_COCKPIT,
																 render_purpose.SCREENSPACE_INSIDE_COCKPIT},{"tv_control"}}
Add(frame)

render_tv_in_HUD_only_view					= CreateElement "ceTexPoly"
render_tv_in_HUD_only_view.name				= "render_tv_in_HUD_only_view"
render_tv_in_HUD_only_view.vertices			= {{-1.15, 0.78}, {1.15, 0.78}, {1.15, -0.78}, {-1.15, -0.78}}
render_tv_in_HUD_only_view.indices			= {0, 1, 2, 0, 2, 3}
render_tv_in_HUD_only_view.tex_params		= {0.5, 0.5,1/(2*1.15),1/(2* 0.78)}
render_tv_in_HUD_only_view.material			= "render_target_1"
render_tv_in_HUD_only_view.parent_element   =  frame.name
Add(render_tv_in_HUD_only_view)

if LockOn_Options.flight.easy_radar then


local 				 screen_height =  2.0;
local 				 screen_width  = screen_height * LockOn_Options.screen.aspect;
local 				 scaled_width  = 1
local 				 scaled_height =  0.78 * scaled_width
					 scaled_width  =  1.15 * scaled_width 

frame2                                       = Copy(frame)
frame2.controllers                           = {{"render_purpose",render_purpose.SCREENSPACE_OUTSIDE_COCKPIT},{"tv_control"}}
Add(frame2)

render_tv_in_HUD_arcade					 = Copy(render_tv_in_HUD_only_view)
render_tv_in_HUD_arcade.parent_element   =  frame2.name
Add(render_tv_in_HUD_arcade)
end





