program Object2;
@define signedmath_zeropage $20 // $20-25
@define matrix_zeropage $c0
@define lib3d_zeropage $90
@define lib3d_clearlines 0 // Clear lines with black
@define muldivtables $2700
@define objPos $3000
@define max3dobjects 1
@define linemethod "Gfx::Line"

//@projectsettings "startaddress" $1280
/*
	The second 3d object in the demo

*/

@use "screen"
@use "files"
@use "demotools"
@use "compression/compression"
@use "gfx/lib3d"
//@userdata $3100 $5800 "Music"

var
	bb,x1,x2,y1,y2,l,i,j,k,f,x3,y3,time,tx,ty,tz,time2,t: byte;
	xx:byte = 110;
	keep,zp : pointer;
	ip:pointer of integer;
	
	obj1 : incbin("data/t2.bin");
	
	const sh: byte = 8;


	matX, matY, matZ, matT : Matrix::mat4;

	x,y,char : byte=0;
	len:byte=1;
	ix,m:integer;
	
//    sinx : array[256] of byte = BuildTable("(Math.sin(i/256.0*Math.PI*2.0)*79+80)/2.0");
/*    siny : array[256] of byte = BuildTable("Math.sin(i/256.0*Math.PI*2.0*2)*79+80");
  */  
//	tab8 : array[160] of byte= BuildTable("i*8
/*
@ifndef IS_DEMO
@define PLAYER_LOCATION 2

@use "player"
	music : incbin("resources/music/maze.vgc", @musicPos);

@endif
*/

procedure Render();
begin
	Lib3d::FlipPointPointers(0);
	
	
	Matrix::RotateX(#matX, tx);
	Matrix::RotateY(#matY, ty);
	Matrix::RotateZ(#matZ, tz);
	
	Matrix::MatMul3(#matX, #matY, #matT);
	Matrix::MatMul3(#matT, #matZ, Lib3d::objects[0].rotmat);


	Lib3d::RotateVertices(0);	

//	Lib3d::ProjectSimple(0,80,50,80 + sinx[time*5]);
		
//	Lib3d::ProjectSimple(0,80,50,127+(Tables::sine128[time*16]+127)/4, 60);
	Lib3d::ProjectSimple(0,100,75,95, 60);
	
	Lib3d::RenderWireframe(0);
	


end;

begin
	
@ifndef IS_DEMO
	Screen::InitStandardScreenAndIRQ();
	Screen::SetMode5Palette(#Screen::colorsBlue);	
	Screen::SetMode5();
	Screen::SetMode5Palette(#Screen::colorsBlue);	
	
@endif
	
	Gfx::shiftX:=8*4;
	Lib3d::InitializeData(@objPos);	
	Lib3d::LoadObject(0, #obj1);
	// place rotmat in z

//	Lib3d::objects[0].rotmat := $70;// place at $a0
/*	Screen_FillBanks(#mode5_vram,0,$28);
	j:=0;
	for i:=0 to 4 do
		j+=Screen::pixel5Tab[i<<2 +2];
		
	zp:=#mode5_vram;
	fill(zp,j,0);	 asm(" inc zp+1");
	fill(zp,j,0);	 asm(" inc zp+1");
	fill(zp,j,128);	
	zp:=#mode5_vram+320*26;
	j:=0;
	for i:=0 to 4 do
		j+=Screen::pixel5Tab[i<<2 +1];
	fill(zp,j,0);	 asm(" inc zp+1");
	fill(zp,j,0);	 asm(" inc zp+1");
	fill(zp,j,128);	
*/

	
//	Lib3d::FillColors(0, 1);	
	
	zp:=Lib3d::objects[0].color;
	k:=1;
	
	for i:=0 to Lib3d::objects[0].noFaces do
	begin
		zp[i]:=k;	
		k:=k+1;
		if (k=3) then k:=1;
	end;

//	keep := Screen::irqProcedure;
//	Screen::irqProcedure := #IRQ;

	Matrix::Identity(#matX);
	Matrix::Identity(#matY);
	Matrix::Identity(#matZ);
	Matrix::Identity(Lib3d::objects[0].rotmat);


	DemoTools::InitEffect(24);
	
	While(DemoTools::isDone = false) do
	begin
		time+=1;
		xx :=(Tables::sine128[time*4]+127)>>2+40;
		Render();
		tx+=3;
		ty+=2;
		tz+=4;
		DemoTools_IncreaseTimer();
		//if (time&1=0) then
		//if (xx>30) then xx-=1;
		
	end;
	
	DemoTools::EndEffect();
	return;
end.
