globals = {
	camera = { x = 0, y=0, z=5 },
	fov = 68, 
	target = { x = 0, y=0.0, z=0 },
	light0 = {   	direction = {x=1, y=0, z=1 }, 
				color = {x=1, y=1, z=1}, 
			},
	up = {x=0, y=1, z=0},
	ambient = { x = 0, y = 0, z = 0 },
     
	sky = 0,
	shadow_scale = 0.2,
	isPaused = 0,
	raymarch_steps = 80,
	raymarch_shadow_steps = 40

	
}

output = {
	resolution = { width = 160, height=200},
	output_type = 1, -- c64 image type
	c64_multicolor = 1,
	c64_imageType = 0, -- 0 = character set (4 colors, compressable), 1 = multicolor image 
	aspect = 1,
	dither = 1,
	ditherStrength = {x=1, y=1, z=1},
	index_colors = { 0,4,6,1 }
}


Material0 = {
   color = { x=1, y=1.0, z=1 },
	reflection = 0.5,
	shininess = 60,
	shininess_intensity = 1 
}

Material1 = {
   color = { x=1, y=1.0, z=1 },
	reflection = 0.6,
	shininess = 50,
	shininess_intensity = 1 
}


-- Init is called on start  
 

-- Number of toruses
N = 6
distance = 10
frame = 0
noFrames = 22.0
char = 0
curSave = 1

hasSaved = 0

function Init()

	radius = 10
	for i=0, N do
     	x= sin(i*3.1415965*2.0/N);
         y= cos(i*3.1415965*2.0/N);
       
		name = "tor"..tostring(i)
  		AddObject("torus",name, "", "Material0", x*radius,0,y*radius,    2.3,0.7, 3 )
  		AddObject("cylinder","cyl"..tostring(i), "", "Material1", x*radius,0,y*radius,    0.3,1, 1.2 )


	end
	
	UpdateScene()

end



function UpdateScene()

--	SetRotation("char1",0,t*180+90,0);
--	t=t*3.14159*2;
--	SetPosition("char1",cos(t)*r,0,sin(t)*r);
    t = (2*3.14165/N)*frame/noFrames;
--  	t = ((frame)/noFrames) 

	r=22 --21

	globals.camera.x = sin(t)*r
	globals.camera.z = cos(t)*r
	globals.camera.y = 0.3*24

	globals.target.y = 1.1
	globals.target.x = 0
	

	t=t+1.5

	globals.light0.direction.x = sin(t)*r
	globals.light0.direction.z = cos(t)*r
	globals.light0.direction.y = 10


	for i=0, N do
     	j = 3.14159*(i);
       	y = cos(t*6 + 3.14/2)*0.6
 
  		SetY("cyl"..tostring(i),y) 

	end


end

-- Experiment with the compression level - between 0(no compression) and 1 (compresses to 0 chars)
compression = 5

function Update()
	
	if (hasSaved == 0) then 
--		CompressCharset(0,40,13,25,  7, 128)
		CompressCharset(0,40,10,22,  compression, 128)

		if (frame==5 or frame == 11 or frame == 17 or frame ==19 or frame ==21) then			
			SaveScreenAndCharset("data/screendata" ..tostring(curSave)..".bin", "data/chardata" ..tostring(curSave)..".bin");
			curSave=curSave+1
			Message("Please wait...")
		end

	end

	frame=frame+1
	if (frame==noFrames) then 
		hasSaved = 1
		Message("Compressed data saved to disk! You can stop the rendering now");

	end	

	UpdateScene()


	


end
