Program Example05;
{ Example program demonstrating the loading of a 3d object and       }
{ displaying it using gouraud shading and using the camera functions }
{ to move around the object                                          }

Uses
  Types,   { Inertia type declarations     }
  Inertia, { the 3d engine                 }
  Mode13h, { Basic Mode 13h graphics unit  }
  Crt;

Var
  View : ViewObject;   { The View System }
  Obj  : VectorObject; { A vector object }
  i    : Integer;

Begin
  If (LoadPalette('COPPRENV.PAL', 0) <> I_OK)       { load the palette }
    Then Begin
      Writeln('Error loading Palette.');
      Halt(0);
    End;
  If (LoadIntensityTable('COPPRENV.IT', 0) <> I_OK) { load intensity table }
    Then Begin
      Writeln('Error loading Intensity table.');
      Halt(0);
    End;
  If (LoadGVO(Obj, 'TORUS.GVO', 0) <> I_OK)         { load the vector object }
    Then Begin
      Writeln('Error loading TORUS object.');
      Halt(0);
    End;
  If (InitView(View, Obj.Num_Polygons) <> I_OK) { init the view }
    Then Begin
      Writeln('Error Initializing view.');
      Halt(0);
    End;

  SetMode($13);              { Set graphics mode }
  SetPalette(GlobalPalette); { Set the palette }

  SetAbsoluteLocation(Obj, 0, 0, 0); { Set the object's location }
  PushLightSource(View, 0, 0, -1);  { Add a light source }

  SetSurface(Obj, SMOOTH);    { set smooth surface }
  SetShading(Obj, GOURAUD);   { set gouraud rendering for object }

  SetBaseColor(Obj, 36);   { set the polygon colors }

  i := 0;
  Repeat
    SetDeltaRotation(Obj, 1, 0, 1);
    SetViewLocation(View, Round(Cos(i*2*Pi/1024) * Obj.Radius * 5), 0, Round(Sin(i*2*Pi/1024) * Obj.Radius * 5));
    SetFocusPoint(View, 0, 0, 0, 0);
    AddtoRenderList(View, Obj);      { add it to the view }
    Render(View, VirtualPage);       { render to the virtual page }
    Flip;                            { Copy the page over }

    i := (i + 1) And 1023;
  Until KeyPressed;

  FreeView(View);         { Free the view's allocated memory }
  FreeVectorObject(Obj);  { free the vector object's allocated memory }
  FreeIntensityTable;     { free the intensity table from memory }

  FreeTexture(0);         { Free the phong map }

  SetMode($03);           { return to text mode }
End.
