Program Three_Dimensional;

Uses
    Threed, NDC;

Const
    MaxSegments		= 1000;
    NumAngle		= 180;
    NumPages		= 2;	{Must be 1 if VGAHI is used in NDC.PAS}

Type
    OrderedTriple	= Record
			    X	: Real;
			    Y	: Real;
			    Z	: Real;
			  End;

    LineSegment		= Array [1..2] of OrderedTriple;
    FigureType		= Array [1..MaxSegments] of LineSegment;
    ColorArray		= Array [1..MaxSegments] of Integer;

    LineSegment_2	= Array [1..2] of Point;
    FigureType_2		= Array [1..MaxSegments] of LineSegment_2;

Var
    M1, M2, P	: Matrix;
    V		: Vector;
    Figure	: FigureType;
    Colors	: ColorArray;
    Figure_2	: FigureType_2;
    I, J	: Integer;
    KeyMeasure	: KeyboardMeasure;
    Device	: DeviceType;
    Ex, Ey, Ez	: Real;
    Vx, Vy, Vz	: Real;
    X, Y	: Real;
    D, Dist	: Real;
    Done	: Boolean;
    Page	: Integer;
    Figure_Size	: Integer;
    Little_Angle: Real;
    Big_Angle	: Real;

(*-------------------------------------------------------------------------*)

Procedure Read_Picture (Name : String; Var F : FigureType;
			Var C : ColorArray; Var N : Integer);

Var
    DataFile	: Text;

Begin
    Assign (DataFile, Name);
    Reset  (DataFile);
    N := 0;
    While not EOF(DataFile) do Begin
	N := N + 1;
	Readln (DataFile, F[N][1].X, F[N][1].Y, F[N][1].Z,
			  F[N][2].X, F[N][2].Y, F[N][2].Z, C[N]);
    End; {While}
    Close (DataFile);
End; {Read_Picture}

(*-------------------------------------------------------------------------*)
(*-------------------------------------------------------------------------*)
(*-------------------------------------------------------------------------*)
(*-------------------------------------------------------------------------*)
(*-------------------------------------------------------------------------*)
(*-------------------------------------------------------------------------*)

Begin
    Read_Picture ('PICTURE.DAT', Figure, Colors, Figure_Size);

    Dist := 20.0;
    D := 1.0;

    NDC_Begin (480, 480);
    NDC_SetInputMode (KEYBOARD, EVENT);
    NDC_SetKeyboardProcessingMode (RAW);

    Make_Identity_Matrix (M1);
    Set_Coordinates (Dist, 0.0, 0.0,  0.0, 0.0, 0.0,  0.0, 1.0, 0.0,  M2);
    Make_Perspective_Matrix (D, P);
    Matrix_Matrix_Multiply (M2, P, M2);

    Little_Angle :=  1.0 * Pi / 180.0;
    Big_Angle    := 10.0 * Pi / 180.0;

    Page := 0;
    Done := False;
    While not Done do Begin
	For I := 1 to Figure_Size do Begin
	    V[1] := Figure[I][1].X;
	    V[2] := Figure[I][1].Y;
	    V[3] := Figure[I][1].Z;
	    V[4] := 1.0;
	    Vector_Matrix_Multiply (V, M1, V);
	    Deflate (V, M2, X, Y);
	    NDC_DefPoint (X, Y, Figure_2[I][1]);
	    V[1] := Figure[I][2].X;
	    V[2] := Figure[I][2].Y;
	    V[3] := Figure[I][2].Z;
	    V[4] := 1.0;
	    Vector_Matrix_Multiply (V, M1, V);
	    Deflate (V, M2, X, Y);
	    NDC_DefPoint (X, Y, Figure_2[I][2]);
	End; {For}

	NDC_SetActivePage (Page);
	NDC_ClearDisplay;

	For I := 1 to Figure_Size do Begin
	    NDC_SetColor (Colors[I]);
	    NDC_Line (Figure_2[I][1], Figure_2[I][2]);
	End; {For}

	NDC_SetVisualPage (Page);
	Page := (Page + 1) mod NumPages;

	NDC_WaitEvent (INDEFINITE, Device);
	If Device = KEYBOARD then Begin
	    NDC_GetKeyboard (KeyMeasure);
	    Case KeyMeasure[1] of
		'q','Q':
			Done := True;
		'z':
			Begin
			    Dist := Dist * 2.0;
			    Set_Coordinates (Dist, 0.0, 0.0,
					      0.0, 0.0, 0.0,
					      0.0, 1.0, 0.0,  M2);
			    Matrix_Matrix_Multiply (M2, P, M2);
			End;
		'Z':
			Begin
			    Dist := Dist / 2.0;
			    Set_Coordinates (Dist, 0.0, 0.0,
					      0.0, 0.0, 0.0,
					      0.0, 1.0, 0.0,  M2);
			    Matrix_Matrix_Multiply (M2, P, M2);
			End;
		'b':
			Rotate_X_Matrix_3 (Little_Angle, M1);
		'y':
			Rotate_X_Matrix_3 (-Little_Angle, M1);
		'j':
			Rotate_Y_Matrix_3 (Little_Angle, M1);
		'h':
			Rotate_Y_Matrix_3 (-Little_Angle, M1);
		'u':
			Rotate_Z_Matrix_3 (Little_Angle, M1);
		'n':
			Rotate_Z_Matrix_3 (-Little_Angle, M1);
		'B':
			Rotate_X_Matrix_3 (Big_Angle, M1);
		'Y':
			Rotate_X_Matrix_3 (-Big_Angle, M1);
		'J':
			Rotate_Y_Matrix_3 (Big_Angle, M1);
		'H':
			Rotate_Y_Matrix_3 (-Big_Angle, M1);
		'U':
			Rotate_Z_Matrix_3 (Big_Angle, M1);
		'N':
			Rotate_Z_Matrix_3 (-Big_Angle, M1);
	    End; {Case}
	End; {If}
    End; {While}
    NDC_End;
End. {Three_Dimensional}
