//
//	法線生成
//
function _Shade()
{
	PolyShade( NORMAL_CREATE );
	UpdatePers( CLEAR );
	UpdateObject();
}

//
//	スムースシェーディング
//
function SmoothShade()
{
	PolyShade( ON );
	UpdatePers( CLEAR );
	UpdateObject();
}

//
//	法線削除
//
function UnShade()
{
	PolyShade( OFF );
	UpdatePers( CLEAR );
	UpdateObject();
}

function UnMap()
{
	PolyMap( FALSE );
	UpdateObject();
}

//
//	簡易マッピング
//
function AutoMap()
{
	var	sel1, sel1s, i, j;
	var	mae_vec, mat, move_vec, u_vec, v_vec;
	var	pos[2], temp, len;

	DlogOpen( "簡易マッピング", 1 );
	DlogString( 0, "係数", "1", 8 );
	if( DlogWait())
		len = atof(DlogAnswer(0));
	else
		return;
	sel1 = Select();
	sel1s = SelectPolygons();
	for( i = 0; i< sel1s; i++ )
	{
		mae_vec = vector(0,0,0);
		SelectNumber( sel1, i );
		PolyLoad();
		mae_vec = PolyVector();
		u_vec = unit(Position( PolyGetVertex(1))-Position(PolyGetVertex(0)))*len;
		v_vec = unit(u_vec*mae_vec)*len;
		move_vec = Position( PolyGetVertex(0));
		mat = move(UNIT_MATRIX,move_vec*(-1)) / Matrix( u_vec, v_vec, mae_vec, vector(0,0,0));
		PolyMap( TRUE, mat );
	}
	UpdateObject();
}

//
//	点の法線にベクトルを加える
//
function AddVectoNormalVec()
{
	var	prev, pos[200];
	var	i;

	i = 0;
	cur = Cursor();
	prev = cur - Vertex( 10, 10, 10 );
	while( TRUE )
	{
		if( cur != PolyVertex( cur ))
			Warning();
		else
		{
			if( cur == prev )
				break;
			else
			{
				pos[i] = cur;
				i++;
			}
		}
	}
}

//
//	位置ベクトルを法線ベクトルに（みたいなもんや）
//	中心点（中心軸）を指定するとばらばらと法線をたててくれる
//
function CreateRadialNormalVec()
{
	var	axis[2], axis_pos[2];
	var	key, vers, len, ty, u, v, uv[2];
	var	axis_vec, mae_vec, yoko_vec;
	var	temp, temp_mat, temp_pos, temp_vec;
	var	rot_mat, move_mat;
	var	z_mat, draw_edge;
	var	box[2], b0[3], b1[3], rot_box[8], rot_box_pos[3];
	var	i, j, k, theta;
	var	line[5][2];
	var	pick, draw_flag = ON;

	if( SelectPolygons() == 0 )
	{
		Warning();
		if( MESSAGE == ON )
			Message( error_msg3 );
		return;
	}

	PushMenu();
	Status_org = Status_title;
	MenuPosition( Menu( " 動作設定(&S)",
			"生成中止	ESC",	MenuQuit
		), Menu_Title );
	ClearStatus();
	Status_title[0] = "【放射状ベクトル生成中】";
	DrawStatus();

	quit_flag = FALSE;

	cur = Cursor();
	axis[0] = cur;
	fix_vertex = cur;
	vers = 1;
	pick = cur;
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
			DrawLine( axis[0], cur );
		key = WaitEvent();
		if( key )
			input_flag = ON;
		key = KeyCode();
		if( key == 0 )
		{
			if( (( ShiftStat() & 1 ) == 1))
			{
				cur = Cursor();
				TrimCursor(fix_vertex, MouseWindow());
				Cursor( cur );
			}
			else
				cur = Cursor();
		}
		else
			cur = Cursor();
		if( input_flag | key == INPUT_KEY )
		{
			axis[1] = cur;
			break;
		}
		else if( key == ESC | key == BS | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			return;
		}
		else
			temp = KeyProcess( key );
		if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
		{
			draw_flag = ON;
			DrawLine( axis[0], pick );
		}
		else
			draw_flag = OFF;
		pick = Cursor();
		input_flag = OFF;
	}

	axis_vec = unit( Position( axis[1]) - Position( axis[0] ));
	axis_pos[0] = Position( axis[0] );
	axis_pos[1] = Position( axis[1] );
	PolyLoadInit();
	while( PolyLoad())
	{
		vers = PolyVertexs();
		for( i = 0; i< vers;i++ )
		{
			temp = PolyGetVertex( i );
			temp_pos = Position( temp );
			Mapping( temp, uv );
			if( axis[0] == axis[1] )
			{
				if( temp == axis[0] )
					temp_vec = unit(PolyVector())*VEC_LEN;
				else
					temp_vec = unit(temp_pos - axis_pos[0] )*VEC_LEN;
			}
			else
			{
				if(abs(( axis_pos[0] - temp_pos)*axis_vec) < 0.00001 )
					temp_vec = PolyVector();
				else
					temp_vec = (temp_pos - axis_pos[0] );
				temp_vec = unit(axis_vec*(temp_vec*axis_vec))*VEC_LEN;
			}
			PolySetVertex( Vertex( temp_pos, temp_vec, uv[0], uv[1] ), i );
		}
		PolyType( PolyType() | POLY_SHADE );
		PolySave();
	}
	UniformNormalVec();
	Clear( OVERWRITE );
	UpdateObject();
	Update( CLEAR, WIN_PERS );
	PopMenu();
	Status_title = Status_org;
	DrawStatus();
}

//
//	放射状法線生成
//
function RadialNormalVec()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:放射状法線生成】";
	DrawStatus();
	InputEvent( CreateRadialNormalVec );
	KeyEvent( CreateRadialNormalVec, INPUT_KEY );
}

//
//	平面マッピング
//
function MapPlane()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:平面ﾏｯﾋﾟﾝｸﾞ】";
	DrawStatus();
	InputEvent( _MapPlane );
	KeyEvent( _MapPlane, INPUT_KEY );
}

//
//	円筒マッピング
//
function CylinderMap()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:円筒ﾏｯﾋﾟﾝｸﾞ】";
	DrawStatus();
	InputEvent( _CylinderMap );
	KeyEvent( _CylinderMap, INPUT_KEY );
}

MenuPosition(
	Menu( " ﾏｯﾌﾟ･ｼｪｰﾄﾞ(&U)",
		"法線生成(&N)",			_Shade,SelectCheck,
		"ｽﾑｰｽｼｪｰﾃﾞｨﾝｸﾞ(&S)",		SmoothShade,SelectCheck,
		"法線削除(&D)",			UnShade,SelectCheck,
		"----------------",		SEPARATE_MENU,
		"放射状法線生成(&R)",		RadialNormalVec,SelectCheck,
		"選択境界の法線変更",		EditNormalVecforSelect,SelectCheck,
//		"点の法線合成",			SumNormalVec,MENU_DISABLE,
//		"点の法線分解",			SeparateNormalVec,MENU_DISABLE,
		"----------------",		SEPARATE_MENU,
		"簡易ﾏｯﾋﾟﾝｸﾞ(&E)",		AutoMap,SelectCheck,
		"平面ﾏｯﾋﾟﾝｸﾞ(&P)",		MapPlane,SelectCheck,
		"円筒ﾏｯﾋﾟﾝｸﾞ(&C)",		CylinderMap,SelectCheck,
//		"立方体ﾏｯﾋﾟﾝｸﾞ",		SEPARATE_MENU,MENU_DISABLE,
		"ﾏｯﾋﾟﾝｸﾞ解除(&U)",		UnMap,SelectCheck
	), Menu_mapandshade
);
